Add ghc-throttle: transparent GHC concurrency limiter#178
Closed
angerman wants to merge 1 commit into
Closed
Conversation
8f92126 to
37e2bcd
Compare
37e2bcd to
fac0d48
Compare
8f374ac to
6e220f0
Compare
Author
|
Depends on #180 |
A drop-in wrapper that limits concurrent GHC processes using flock() + exec() on POSIX and named mutexes + CreateProcess on Windows. After acquiring a concurrency slot, the POSIX wrapper exec()s the real GHC — zero runtime overhead. When GHC exits (normally or via crash), the kernel releases the flock automatically. The Windows wrapper holds a named mutex while the child runs via CreateProcess. Key features: - Transparent to build systems (looks like a normal GHC binary) - Smart bypass for query flags (--version, --info, --print-*, etc.) - -jsem aware (skips throttling when GHC's jobserver is active) - Cross-platform: Linux, macOS, FreeBSD (flock+exec), Windows (mutexes) - Crash-safe by design (kernel auto-releases locks/mutexes on exit) - Recursion detection via GHC_THROTTLE_ACTIVE sentinel Includes ghc-throttle-status companion tool for monitoring slot usage with PID-of-holder reporting on Linux (via /proc/locks). Files: - utils/ghc-throttle/ghc-throttle.c — POSIX wrapper - utils/ghc-throttle/ghc-throttle-win.c — Windows wrapper - utils/ghc-throttle/ghc-throttle-status.c — POSIX status monitor - utils/ghc-throttle/ghc-throttle-status-win.c — Windows status monitor - utils/ghc-throttle/Makefile — standalone build - Makefile — integration targets Copyright: Moritz Angermann <moritz.angermann@iohk.io> License: Apache-2.0
6e220f0 to
adeea3d
Compare
Author
|
While this works, it's not really competitive with -jsem at higher concurrency, as jsem allows for more fine control between the ghcs. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
ghc-throttle, a transparent drop-in wrapper that limits concurrent GHC processes to prevent OOM kills and swap thrashing during parallel builds.POSIX (Linux, macOS, FreeBSD):
flock()+exec()— the wrapper acquires a file lock, thenexec()s the real GHC. After exec, there is zero wrapper overhead (the process IS GHC). When GHC exits, the kernel releases the lock automatically. Crash-safe by design.Windows: Named mutexes (
Local\ghc-throttle-slot-N) +CreateProcess. The wrapper holds the mutex while the child GHC runs. Mutexes are auto-released on process exit.Key features
--version,--info,--print-*,--supported-extensions,--show-optionsskip throttling/proc/self/exe), macOS (_NSGetExecutablePath), FreeBSD (sysctl KERN_PROC_PATHNAME), Windows (GetModuleFileNameA)GHC_THROTTLE_ACTIVEsentinel prevents infinite loopsGHC discovery (priority order)
GHC_THROTTLE_GHCenvironment variable (explicit path)<self>.realsuffix (e.g.,ghc.realnext toghc)Slot acquisition
PID % N(distributes waiters, prevents thundering herd on slot 0)max(1, min(ncpus/2, 256)), override withGHC_THROTTLE_JOBSUsage
Files
utils/ghc-throttle/ghc-throttle.cutils/ghc-throttle/ghc-throttle-win.cutils/ghc-throttle/ghc-throttle-status.c/proc/locks)utils/ghc-throttle/ghc-throttle-status-win.cutils/ghc-throttle/MakefileMakefileEnvironment variables
GHC_THROTTLE_GHCGHC_THROTTLE_JOBSncpus / 2GHC_THROTTLE_DIR/tmp/ghc-throttle-$UIDGHC_THROTTLE_DEBUG1for diagnostic outputGHC_THROTTLE_ACTIVEComparison with
-jsem-jsemghc-throttle-j?-jTest plan
cc -O2 -Wall -Wextra -pedantic -Werroron macOSghc-throttle --versionprints GHC version (with bypass)JOBS=2correctly limits to 2 parallelghc-throttle-statuscorrectly reports held/free slots with PID infoghc-throttle-status --helpprints usage