Idle GC does not work in non-threaded RTS
In theory idle GC is available for both the threaded and non-threaded RTS. But the default is to turn it on for threaded and off for non-threaded. In practice, it does not work for the non-threaded RTS.
The scheduler has a "recent activity" mechanism. It is used to decide when to invoke idle GC, and having done so when to turn off the timer.
Here's the summary from the current code:
```
/*
* flag that tracks whether we have done any execution in this time
* slice, and controls the disabling of the interval timer.
*
* The timer interrupt transitions ACTIVITY_YES into
* ACTIVITY_MAYBE_NO, waits for RtsFlags.GcFlags.idleGCDelayTime,
* and then:
* - if idle GC is on, set ACTIVITY_INACTIVE and wakeUpRts()
* - if idle GC is off, set ACTIVITY_DONE_GC and stopTimer()
*
* If the scheduler finds ACTIVITY_INACTIVE, then it sets
* ACTIVITY_DONE_GC, performs the GC and calls stopTimer().
*
* If the scheduler finds ACTIVITY_DONE_GC and it has a thread to run,
* it enables the timer again with startTimer().
*/
enum RecentActivity {
// the RTS is active
ACTIVITY_YES = 0,
// no activity since the last timer signal
ACTIVITY_MAYBE_NO = 1,
// RtsFlags.GcFlags.idleGCDelayTime has passed with no activity
ACTIVITY_INACTIVE = 2,
// like ACTIVITY_INACTIVE, but we've done a GC too (if idle GC is
// enabled) and the interval timer is now turned off.
ACTIVITY_DONE_GC = 3,
};
extern StgWord recent_activity;
```
This currently works much the same in the threaded and non-threaded RTS ways.
As the comment says:
```
* The timer interrupt transitions ACTIVITY_YES into
* ACTIVITY_MAYBE_NO, waits for RtsFlags.GcFlags.idleGCDelayTime,
* and then:
* - if idle GC is on, set ACTIVITY_INACTIVE and wakeUpRts()
* - if idle GC is off, set ACTIVITY_DONE_GC and stopTimer()
```
The problem here is that `wakeUpRts` is a no-op on the non-threaded RTS, and thus this scheme does not work for the non-threaded RTS.
In the threaded RTS, the `wakeUpRts` implementation talks to the in-library I/O manager and gets one of its threads to return (e.g. from a blocking `epoll_wait()` call) and go back to the scheduler.
This in itself is a bit weird. Why involve the I/O manager? Capabilities already have a generic mechanism to wake up other capabilities if they are idle. When a capability sends a message to an idle capability for example it calls `releaseCapability_()` to get a task to wake up and animate the capability. Perhaps it is a constraint due to the Timer tick function being called from an interrupt context for some of the ticker implementations, and interrupt context code cannot take locks etc.
In the non-threaded RTS, the (single) capability will block on I/O or timeouts when the capability is idle. This is simply never interrupted as would be required for the existing scheme to work. But if it were interrupted (e.g. by making the I/O managers also wait on a pipe or eventfd) it would also be a rather odd design. It would mean the task blocking on I/O or timeouts -- which inherently has the capability to wait on a time -- and then interrupting it later by writing to a pipe that the task is also blocking on. A much more direct design for the non-threaded RTS would be simply to ask the I/O manager to block for a maximum time, and if that time expires with no activity then to invoke idle GC.
The only bonus for the current design is that in principle it could be consistent between the threaded and non-threaded RTSs. But of course it's not really, since it doesn't have all the pieces to actually work.
(provided)
MR !14820 attempts to solve this (amongst many other things) by adding a proper `wakeUpRts` implementation for the non-threaded RTS. It uses a pipe or eventfd.
But this illustrates another problem: such a design _cannot_ work on a platform that is fundamentally single-threaded, lacking threads or signals. Specifically wasm. The design using a timer and then notifying via a pipe relies on ... timers and pipes. These do not exist on wasm currently. Indeed the wasm backend for ghc accepts that there is no pre-emption for Haskell threads. This makes sense. In a single-threaded platform like wasm, there is no need for signals and having them introduces great complexity.
Hence, probably the better thing is to have the decision on when to invoke idle GC work somewhat differently on the non-threaded and threaded RTS ways. The calculation for the timing of idle GC can be the same, but then the mechanism be different.
* The non-threaded RTS can calculate the idle GC wakeup time, and ask `awaitCompletedTimeoutsOrIO()` to wait for at most that time.
* The threaded RTS can use much the same implementation as now, using the `recent_activity` variable.
There's a closely related issue: in the threaded RTS, the use of the `recent_activity` variable looks *very* expensive for what it is used for. It would appear that *every* time the scheduler on each capability decides to run a thread, it calls `setRecentActivity(ACTIVITY_YES)` which uses a CAS to atomically modify the `recent_activity` variable. This means that when there's lots of thread switching and lots of capabilities, all the capabilities are hammering this one shared variable. But all we need is to _eventually_ detect that all capabilities have been idle for a period of time, e.g. 0.3s. This doesn't need to be ultra-prompt. If it takes 0.301 seconds, that's ok. It should be possible to find a less expensive scheme, given that the timing is not critical. Probably something involving per-capability variables that are read across threads occasionally and can tolerate seeing an out-of-date view.
An alternative design for the threaded RTS may also eliminate the need for `wakeUpRts` to be so tightly coupled with the in-library I/O manager(s).
issue