Skip to content

tools: Add adaptive Bazel wrapper#1905

Open
jrajahalme wants to merge 1 commit into
mainfrom
bazel-adaptive-wrapper
Open

tools: Add adaptive Bazel wrapper#1905
jrajahalme wants to merge 1 commit into
mainfrom
bazel-adaptive-wrapper

Conversation

@jrajahalme

@jrajahalme jrajahalme commented May 17, 2026

Copy link
Copy Markdown
Member

NOTE: This bazel wrapper tools/bazel_adaptive.py is NOT used by default. Set the BAZEL environment variable to use it with make, e.g., BAZEL=tools/bazel_adaptive.py make tests.

tools/bazel_adaptive.py runs Bazel with a concrete integer --jobs value, monitors progress output and memory pressure, stops and resumes bazel build processes as needed under memory pressure, and if that does not relieve memory pressure, restarts with lower or higher parallelism as memory allows. Integer --jobs values are used directly; Bazel-style HOST_CPUS and HOST_RAM values with optional multipliers, such as HOST_CPUS*.5 and HOST_RAM*.0002, are resolved once as the maximum adaptive jobs cap.

The wrapper rewrites bazel output to indicate the number of paused jobs (if any). "(13 actions, 12 running)" is shown as "(13 actions, 2 paused, 10 running)" when 2 of the running actions have been paused by the wrapper.

This wrapper is only modestly faster than builds with carefully tuned --jobs count and ample available swap space. The main benefit of this wrapper is that it removes the tuning work and automatically restarts bazel when if fails for out-of-memory or job termination.

To allow Bazel to get through memory bottlenecks during the build without unnecessary restarts the build environment should have swap available. This wrapper was tested on a 24 GB VM with 16 GB swap.

Running the wrapper with BAZEL_ADAPTIVE_DISABLE_PTY=1 defined allows more of the diagnostic lines to remain visible even when running in an interactive terminal. Each diagnostic line starts with "[bazel-adaptive/s]", where "" is the number of seconds since the wrapper started.

Written by Codex through an iterative session where codex has run the included Python tests and has observed the behavior of live runs for runtime behavior.

@jrajahalme jrajahalme requested a review from a team as a code owner May 17, 2026 17:04
@jrajahalme jrajahalme requested a review from sayboras May 17, 2026 17:04
@jrajahalme jrajahalme marked this pull request as draft May 17, 2026 17:04
@jrajahalme jrajahalme force-pushed the bazel-adaptive-wrapper branch 2 times, most recently from 89f4d3f to 68e2640 Compare May 18, 2026 13:04
@jrajahalme jrajahalme force-pushed the bazel-adaptive-wrapper branch 2 times, most recently from 0ab1dde to f1e30e7 Compare May 31, 2026 23:39
@jrajahalme jrajahalme marked this pull request as ready for review June 1, 2026 08:02
tools/bazel_adaptive.py runs Bazel with a concrete integer --jobs value,
monitors progress output and memory pressure, stops and resumes bazel
build processes as needed under memory pressure, and if that does not
relieve memory pressure, restarts with lower or higher parallelism as
memory allows. Integer --jobs values are used directly; Bazel-style
HOST_CPUS and HOST_RAM values with optional multipliers, such as
HOST_CPUS*.5 and HOST_RAM*.0002, are resolved once as the maximum
adaptive jobs cap.

The wrapper rewrites bazel output to indicate the number of paused jobs
(if any).  "(13 actions, 12 running)" is shown as "(13 actions, 2 paused,
10 running)" when 2 of the running actions have been paused by the
wrapper.

This wrapper is only modestly faster than builds with carefully tuned
--jobs count and ample available swap space. The main benefit of this
wrapper is that it removes the tuning work and automatically restarts
bazel when if fails for out-of-memory or job termination.

To allow Bazel to get through memory bottlenecks during the build without
unnecessary restarts the build environment should have swap
available. This wrapper was tested on a 24 GB VM with 16 GB swap.

Running the wrapper with BAZEL_ADAPTIVE_DISABLE_PTY=1 defined allows more
of the diagnostic lines to remain visible even when running in an
interactive terminal. Each diagnostic line starts with
"[bazel-adaptive/<XXX>s]", where "<XXX>" is the number of seconds since
the wrapper started.

Written by Codex through an iterative session where codex has run the
included Python tests and has observed the behavior of live runs to
observe the runtime behavior.

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
@jrajahalme jrajahalme force-pushed the bazel-adaptive-wrapper branch from f1e30e7 to c549fc8 Compare June 1, 2026 08:48
@jrajahalme

Copy link
Copy Markdown
Member Author

Added a section in README.md.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant