-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically replace blocking threads
This changes the scheduler such that it supports replacing blocking threads with backup threads. Prior to performing a blocking operation, such as reading a file, a thread signals that it's about to enter a blocking operation. When it's done with the operation it signals this accordingly. In the background a monitor thread periodically checks for blocking threads. If this monitor thread finds any threads that have been blocking for too long, it signals them to become a backup thread. The thread counts are fixed as this is easier to implement, and removes the need for synchronisation when stealing work from queues. The pool is now divided into two groups of threads: primary and backup threads. The primary thread count (P) defaults to the number of CPU cores, while the backup thread count (B) defaults to four times the number of CPU cores. When the blocked thread returns from the blocking call it continues running the process. When it would normally pick up new work, it transitions to a blocking thread. We don't transition to a backup thread immediately after returning from the blocking call, as this could result in the process taking even longer to complete. The monitor thread runs at an interval of 100 µsec, but the exact interval may be slightly higher depending on the timer resolution. Windows in particular is quite bad at this, with a default resolution of around 16 milliseconds. In general the interval is just a best-case effort to prevent Inko processes from blocking OS threads forever, rather than a hard guarantee. The cost of a thread marking itself as a blocking thread is somewhere between 100 nanoseconds and 1 µsec, depending a bit on system load and if the monitor thread needs to be woken up from a deep sleep. In this setup, there may be more than P threads performing non-bocking work, albeit only for a brief period of time. This is fine and the system eventually settles back at P number of threads. The maximum capacity is P+B. One crucial detail is that while the total capacity is P+B, there are only P queues. This means that no matter what value B is set to, threads only need to consider stealing from up to P queues. This means you can increase B without this resulting in work stealing becoming more expensive. As part of this work I considered simply having a fixed size pool for _only_ blocking processes, similar to the setup we had before version 0.10.0. I ultimately didn't go with this approach, as moving processes between pools for _every_ blocking operation would result in increased latency and poorer cache utilisation. This fixes https://gitlab.com/inko-lang/inko/-/issues/247. Changelog: performance
- Loading branch information
1 parent
959a2f8
commit 89467af
Showing
26 changed files
with
1,024 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.