Repository navigation
Description
Description of the bug:
In our C++ builds, we have observed Bazel eagerly running more than a few link actions at once. Each of these linker executions can easily consume 8GB, so combining just a handful together can easily exceed RAM limits. Using -c dbg bumps the memory consumption to 20GB per linker, which is ridiculous. We are currently using ld.
The puzzling thing was that even using something like --local_ram_resources to limit Bazel to a few GB, say 2GB, did not help at all. Bazel kept scheduling too many linkers at once. This made me think that the RAM computations for the link actions were not working correctly.
Upon looking I noticed commit 01c10e0, which mentions that the new limits were set based on data collected at Google. Thus I'm surprised that this new behavior didn't work well.
I ended up patching CppLinkAction.java to dump the resources computed in estimateResourceConsumptionLocal and found that all of our link actions are assumed to only need 50MBs, which is way off reality. I also noticed that our problematic actions only have about ~600 inputs.
What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
Come up with a link action that, with a small number of inputs, consumes multiple GBs. (This is what we observe but I have no idea at this point why the linker is behaving in this way.)
Craft a build file that contains multiple independent such actions.
Run the build and see Bazel trigger too many linkers at once, overwhelming the machine.
Which operating system are you running Bazel on?
Linux
What is the output of bazel info release?
release 6.0.0
If bazel info release returns development version or (@non-git), tell us how you built Bazel.
No response
What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?
No response
Have you found anything relevant by searching the web?
There is #6477 which would help if we had it, but I would prefer to avoid crafting our own rules just to paper over a deficiency of existing native rules. A tag or other attribute on the existing rules would work better from a usability perspective.
Any other information, logs, or outputs that you want to share?
I do not think the C++ link rule can provide a local resource set that works in all cases, if only because there are different linkers and they have different memory needs, and because even the same linker can exhibit vastly different behavior depending on compilation modes (see 8GB vs. 20GB when enabling dbg mode).
There should be a way for downstream users to configure the resource sets of the C++ rules without having to modify the Bazel source code. I'd suggest supporting cpu:X and ram:X tags or similar that override whatever the rules compute; this would have been sufficient for us to workaround the problem.
Activity
lm1baker commentedon Mar 10, 2023
Can report the same problem, bazel will happily start 20+ linker threads on a CI node with 108GB Ram, with each thread consuming 6GB. Setting e.g. --local-ram-resources=24576MB (about 25% of system ram) does not even help with this.
Manually tuning with --jobs (informed by OOMkiller logs) is the only workaround, though this does slow compilation and other tasks.
jmmv commentedon May 22, 2023
Another thought that came to mind: maybe it's the toolchain definition what should be able to supply the memory model for the linker and compiler. I'm not sure if this would be feasible, but maybe there could be a user-supplied function that takes the compilation mode and details about the inputs (like the total count) as parameters and returns the computed expected memory. Expressing arbitrary formulas via flags is not a great idea, and encoding limits via tags also doesn't scale.
larsrc-google commentedon Jun 6, 2023
For starlark-defined rules, you can give such a function indeed. But the CppLink function is based on the internal graph, which tends to have a huge amount of relatively small targets, where your case seems to have relatively few but large inputs. So we'd want to look at the size of the inputs, but that would be a more expensive computation.
kjteske commentedon Jul 7, 2023
We've got a similar use case, but with C++ compilations in addition to links:
Scattered in with thousands of normal-sized compiles, we have a number of large compiles (or links) where gcc takes >2GB of RAM. If enough of those get scheduled to run in parallel, we can OOM pretty easily even on beefy machines.
We generally know which of these compiles are huge, and there aren't that many of them, so we could tag these particular actions in BUILD files or our rules, and deal with the maintenance burden of keeping that up to date.
--experimental_action_resource_set gives Bazel a hook we could use... but I don't see a way to access that through the native cc rules, or even cc_common. If cc_common.compile() took an optional resource_set parameter that was forwarded to ctx.actions, I think that would give us the flexibility we're looking for.
larsrc-google commentedon Jul 10, 2023
Here's what I would suggest trying: In
CppLinkAction.createSpawn(), pass the inputs instead of merely the count of inputs intoestimateResourceConsumptionLocal. In there, compute the size of the inputs and use that as your estimate. Then see how close you get to reality. This will take longer to estimate due to looking at all the inputs, so benchmarking it would be crucial, but I think for linker-style actions it may be necessary.jmmv commentedon Jul 17, 2023
That's a good idea. I modified
CppLinkActionto capture the size of the inputs along with the counts, made it dump metrics to the log, and graphed the results. The size does seem to track consumption much better, at least for our builds with lld on Linux. This applies to fastbuild/dbg/opt with and without dynamic linking, as well as amd64 and arm64. Roughly, I'm observing that memory consumption is ~2x the size of the inputs.I've left my changes in a custom branch that you can compare against 6.2.1 here (https://github.com/bazelbuild/bazel/compare/6.2.1...Snowflake-Labs:bazel:linker-memory-estimation?expand=1) but I have not created a PR yet because the branch mixes instrumentation with memory tweaks. If you find value in the instrumentation portion alone (computing input sizes and logging them), I can split that into its own PR for inclusion. Then, you could rerun whichever analysis you previously did with your data and see what comes up. (For example, I won't be able to do this for neither macOS nor Windows.)
larsrc-google commentedon Jul 18, 2023
Nice that your estimates are stable across various options variations. Did you do any benchmarks with your resulting memory estimations?
jmmv commentedon Aug 7, 2023
@larsrc-google No, I did not. I do not think our build or link actions are sufficiently large to gather any meaningful performance data of this change (other than seeing no OOMs).
What exactly would you like to see measured? A microbenchmark could help quantify the cost, but then, a microbenchmark wouldn't be realistic. In a real build, link actions are infrequent so unless their cost is massively different, they won't impact end-to-end build times. And in any case, this change only impacts local link actions, so if the majority of link actions happen remotely (as is also commonly the case for sufficiently-large builds), then this change would have no impact...
larsrc-google commentedon Aug 7, 2023
I would suggest using the JSON profile data on realistic builds run local-only (or local-for-link-only). That would allow you to measure realistic time differences between different estimates.
Doing this with compile actions like @kjteske talks about would be easier, I guess, as you would be able to see whether the machine gets overloaded/too few actions get scheduled.
It's also interesting to know if getting the file sizes takes a non-trivial amount of time, I hope those values are in memory, but stat'ing all the inputs all the time might be problematic.
One nice thing about your change is that it defers flattening the nestedset, so the estimation implementation has more say in whether to do that.
20 remaining items
kirschem-fernride-legacy commentedon May 21, 2025
@wilwell Nice! But I think this is currently only accounting for cpu. Would be nice to also have this for RAM, which is more critical in my opinion due to OOMs, which might kill the whole build. CPU is "only" slowing down.
Is it planned to also add scheduling based on the current RAM usage?
wilwell commentedon May 21, 2025
@kirschem-fernride I've recently left Bazel, so I am not developing this repo anymore.
cc @meisterT @bigelephant29
meisterT commentedon May 21, 2025
@kirschem-fernride can you please file a feature request for this?
pzembrod commentedon Aug 1, 2025
Also relevant: #25770 (Ability to override RAM estimate for cppcompile action)
kirschem-fernride-legacy commentedon Aug 19, 2025
@meisterT done: #26794
Add cpp_compile exec_group
Add new cpp_compile exec group
keith commentedon Apr 13, 2026
this change #29284 would at least allow overriding things per target / globally, but still only statically, so it couldn't scale with the given input set
Add new cpp_compile exec group
Make local scheduling respect exec_properties resources (#29284)
[9.2.0] Make local scheduling respect exec_properties resources (baze…
Add new cpp_compile exec group
Add new cpp_compile exec group
Add cpp_compile exec_group