Repository navigation
Add --experimental_local_jobserver for cooperative local parallelism - #30229
HackAttack wants to merge 15 commits into
Conversation
Parallelism-aware tools such as rustc and make can grow their own internal parallelism to fill idle cores, but when Bazel runs several of them at once they each assume they own the machine and oversubscribe the CPU. This change adds an opt-in local jobserver that lets those tools share Bazel's view of idle CPU instead of fighting over it. With --experimental_local_jobserver set, Bazel runs a GNU-make-protocol jobserver whose token pool is continuously resized to the number of local CPUs not currently reserved by running actions. Local spawns tagged `supports-jobserver` get `MAKEFLAGS=--jobserver-auth=...` injected into their environment; jobserver-aware tools parse this, take one token per additional thread of parallelism, and return them when the job finishes. Held tokens are reported back to ResourceManager as in-use CPUs, so action scheduling and tool parallelism stay coupled in both directions.
Replace the one-element int[]/IOException[] holders in drainUpTo with AtomicInteger/AtomicReference. Both work (the array reference is effectively final, and join() provides the happens-before), but the atomics make the cross-thread handoff explicit and self-documenting.
Drain and refill the named semaphore on every tick so ResourceManager sees all held tokens, including during steady and growing pool states. Distinguish an empty semaphore from a failed wait and remove the now-obsolete CPU-load-scheduling warning.
A per-build UUID prevents unrelated output bases or surviving clients from attaching a new jobserver to a stale named semaphore with an incompatible count.
Wire a real FIFO-backed LocalJobserver to ResourceManager and show that tokens held by two self-parallelizing actions block a third action until one token is returned.
Do not evaluate the held-token supplier on the measured-load path, where its value is intentionally ignored to avoid double-counting tool threads.
Document the cache, implicit-slot, polling-race, and persistent-worker boundaries at their public seams. Remove the obsolete blocking-reclaim wake hook and name the sandbox API by its platform-neutral writable-directory contract.
Avoid advertising a FIFO auth path that clients truncate when the output base contains whitespace.
The coordinator already supplies nonnegative targets, and the desired-token calculation independently clamps its result.
|
@HackAttack Thanks for the PR! The code change is massive, so we need to be careful when we make progress on this. In order to make review and future maintenance easier, we would like to ask you to create a document about the architectural design behind this change. It can be a Some topics we need to discuss on the doc:
Another question we need to answer is the ownership of the feature. Are you willing to help maintain this feature going forward? We need to figure this out as this is unlikely to be used in Google internally, so we expect there will be limited support from us. |
Move drainFifoNonBlocking onto the NativePosixFilesService interface so the impl exposes no public method outside its contract, and have ExecutionTool pass the already-registered BlazeService instance to PosixJobserverBackend instead of the backend constructing its own impl. BazelServices stays the sole in-server instantiation point and the backend depends only on the interface. Also drop the hardcoded poll interval from a backend comment.
Expose POLL_MILLIS so LocalJobserverTest can express its poll-cycle sleep in terms of the constant instead of hardcoding the interval.
Colocate the test with LocalJobserver instead of leaving it under actions. The only thing tying it to the actions package was ResourceManager.getWaitCount(), so widen that (already @VisibleForTesting) method to public. The standalone java_test and its per-OS presubmit entry move to the exec package accordingly.
|
Thanks for looking! Yes, I am willing to help support this feature. I added a README;1 I kept it pretty high-level, let me know if more detail is needed. I think your questions are answered—to briefly reproduce here:
Footnotes
|
Turn LocalJobserver.Backend from an interface into an abstract class that owns the per-tick issued/held/desired accounting, so the POSIX and Windows backends share it under one vocabulary and each implements only its OS-specific peek (drainPool) and refill (refillPool). Collapses POSIX's written/drained counters into the base's single bounded issued field. The interface had a single implementer, so folding the contract and the shared accounting into one Backend drops a layer of indirection.
…ounds Size the Windows semaphore's max count to the configured local CPU budget rather than the host core count, so an over-provisioned --local_cpu_resources grows the pool to the budget as POSIX already does instead of capping at cores. With the ceiling now equal to the budget, the poll target can never exceed it, so clampTarget is removed. Also drop drainPool's issued argument. POSIX drains the fifo exactly in one non-blocking read, and Windows drains the semaphore until tryAcquire fails; the base clamps held at max(0, issued - available), so a tool that over-releases is absorbed rather than corrupting the count.
Parallelism-aware tools such as rustc and make can grow their own internal parallelism to fill idle cores, but when Bazel runs them in conjunction with other actions they assume they own the machine and oversubscribe the CPU. This change adds an opt-in local jobserver that lets those tools share Bazel's view of idle CPU instead of fighting over it.
With
--experimental_local_jobserver, Bazel runs a GNU-make-protocol jobserver whose token pool tracks CPUs not currently reserved by running actions. Standalone local spawns taggedsupports-jobservergetMAKEFLAGS=--jobserver-auth=...injected into their environment; jobserver-aware tools parse this, take one token per additional thread of parallelism, and return them as parallel work completes. Held tokens are reported back to ResourceManager as in-use CPUs, so action scheduling and tool parallelism stay coupled in both directions. With CPU load scheduling enabled, measured CPU usage remains the source of truth.Resolves #10443
Related:
cc @dhbaird @hofbi @UebelAndre @jwnimmer-tri @mattgodbolt @pauldraper @matts1 @nvkjw @orlitzky
RELNOTES[NEW]: Added
--experimental_local_jobserverflag for cooperative local parallelism