Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefuly handle available CPU cores #482

Open
blackliner opened this issue Mar 5, 2024 · 4 comments
Open

Gracefuly handle available CPU cores #482

blackliner opened this issue Mar 5, 2024 · 4 comments
Labels
P3 minor: not priorized type: feature request

Comments

@blackliner
Copy link

blackliner commented Mar 5, 2024

Is your feature request related to a problem? Please describe.
As described in https://nixos.org/manual/nix/stable/advanced-topics/cores-vs-jobs, and it applies to bazel too, it can happen that you trash your system, or even go into OOM issues, when too many parallel nix derivates are being built. The goal is to reduce this number to the number of total available cores (mind: cgroups (!) in containers), to avoid this. Preferable in a dynamic way.

Describe the solution you'd like
The nix build should be executed with a limited number of available cores. Not sure if this is possible, but a good solution would also make sure that a single nix build will also "consume" multiple action slots in bazel, so bazel also "knows" about the load and does not spawn too many other things in parallel.

Describe alternatives you've considered
In our CI, we hardcode NIX_BUILD_CORES=2, but this does not have an effect.

Additional context
N/A

@benradf
Copy link
Contributor

benradf commented Mar 6, 2024

Not sure if this is possible, but a good solution would also make sure that a single nix build will also "consume" multiple action slots in bazel, so bazel also "knows" about the load and does not spawn too many other things in parallel.

Agreed, this would be ideal. Not sure if you've seen it already, but there's an open Bazel issue on this topic: Cooperative Parallelism #10443.

Essentially we need Bazel to provide some kind of API that we can use to cooperatively share available cores.

@blackliner
Copy link
Author

Thanks, yes that issue looks a) very promising and b) very stale 🤣

But bazelbuild/bazel#10443 (comment) made a good suggestion: resource_set
Can we use them to assign the nix rules one of these "resources" (lets say we have 4 of them), and then call nix with 4 cores on a 16 core machine? (TBH not 100% sure how resource_set works)

@blackliner
Copy link
Author

blackliner commented May 24, 2024

for the time being, is there any way for rules_nixpkgs to honor NIX_BUILD_CORES?
This doesn't work:

NIX_BUILD_CORES=2 bazel build @some_nix_dependency//...
...
build flags: -j32 -l32

https://bazel.build/reference/command-line-reference#flag--repo_env doesn't work either ;-/

@benradf
Copy link
Contributor

benradf commented May 24, 2024

I think the causality is reversed — you need to set the cores config, and then Nix will ensure NIX_BUILD_CORES is set to that value when the derivation gets built (though the derivation might choose to ignore it).

You can set cores globally in ~/.config/nix/nix.conf, or via the NIX_CONFIG environment variable:

bazel build @some_nix_dependency//... --repo_env=NIX_CONFIG='cores = 2'

@malt3 malt3 added type: feature request P3 minor: not priorized labels Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 minor: not priorized type: feature request
Projects
None yet
Development

No branches or pull requests

3 participants