Skip to content

Add --experimental_local_jobserver for cooperative local parallelism - #30229

Open
HackAttack wants to merge 15 commits into
bazelbuild:masterfrom
HackAttack:jobserver
Open

HackAttack wants to merge 15 commits into
bazelbuild:masterfrom
HackAttack:jobserver

Conversation

@HackAttack

@HackAttack HackAttack commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

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 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 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_jobserver flag for cooperative local parallelism

@github-actions github-actions Bot added area-Windows Windows-specific issues and feature requests team-Performance Issues for Performance teams area-EngProd Bazel CI, infrastructure, bootstrapping, release, and distribution tooling team-Local-Exec Issues and PRs for the Execution (Local) team awaiting-review PR is awaiting review from an assigned reviewer labels Jul 10, 2026
@fmeum

fmeum commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

@meisterT @bigelephant29

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.
@bigelephant29

Copy link
Copy Markdown
Contributor

@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 .md file sitting next to the code, or feel free to use https://github.com/bazelbuild/proposals.

Some topics we need to discuss on the doc:

  • What’s the architecture, protocol of a job server?
  • How is it integrated into the existing local execution machinery?
  • What’s the change in C++ about? Is it required?
  • What’s the added “Windows Semaphore” about? What’s the difference between Windows and other platforms?

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.
@HackAttack

HackAttack commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor Author

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:

  • A jobserver is a synchronization primitive (fifo on POSIX, semaphore on Windows) to which bytes are written/read to represent CPU cores
  • ResourceManager and LocalJobserver coordinate with each other to share info on how many CPUs are being used by actions they are aware of
  • The native code is required because there is no Java-native way to make the required primitives, nor to nonblockingly count the pool (we could however make it nicer with java.lang.foreign but this would require --enable-preview or upgrading to Java 22)

Footnotes

  1. I called it README.md even though it’s only scoped to the jobserver because all other md files under src/ are so named…not ideal and potentially a little misleading, open to alternatives. ↩

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.
@bazel-docs-pr-commenter

bazel-docs-pr-commenter Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

✅ Bazel docs preview is ready!

Updated pages

Preview URL: https://bazel-pr-30229.mintlify.app/

Updated for e7c77eacab3c8d99c4e49f85d9219c394d07d8de

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-EngProd Bazel CI, infrastructure, bootstrapping, release, and distribution tooling area-Windows Windows-specific issues and feature requests awaiting-review PR is awaiting review from an assigned reviewer team-Local-Exec Issues and PRs for the Execution (Local) team team-Performance Issues for Performance teams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cooperative Parallelism

3 participants