Optimize Nix build CPU utilization with NIX_MAX_CORES#141266
Closed
Artturin wants to merge 2 commits intoNixOS:stagingfrom
Closed
Optimize Nix build CPU utilization with NIX_MAX_CORES#141266Artturin wants to merge 2 commits intoNixOS:stagingfrom
Artturin wants to merge 2 commits intoNixOS:stagingfrom
Conversation
8 tasks
Member
|
I don't understand why use both |
Member
Author
forgot to remove it |
Member
|
Does this need a doc update? |
Member
Author
|
im using this script to test the logic #!/usr/bin/env bash
# Guess the optimal parallelism using the same formula as Ninja; return nothing
# if it can not be determined. (Note that "make" ignores "-l" without value.)
guessParallelism() {
local n
n=$(getconf _NPROCESSORS_ONLN 2>/dev/null || nproc 2>/dev/null || true)
#local n=1
if [ "$n" -ge 1 ]; then
if [ "$n" -le 2 ]; then
echo $((n+1))
else
echo $((n+2))
fi
fi
}
# NIX_MAX_CORES is the optimal load average (when known).
: "${NIX_MAX_CORES=$(guessParallelism)}"
echo $NIX_MAX_CORES
# Normalize the NIX_BUILD_CORES variable. The value might be 0, which
# means that we're supposed to try and auto-detect the number of
# available CPU cores at run-time.
NIX_BUILD_CORES=0
#NIX_BUILD_CORES=1
NIX_BUILD_CORES="${NIX_BUILD_CORES:-1}"
if ((NIX_BUILD_CORES <= 0)); then
((NIX_BUILD_CORES = NIX_MAX_CORES <= 0 ? 1 : NIX_MAX_CORES))
fi
echo NIX_MAX_CORES $NIX_MAX_CORES
echo NIX_BUILD_CORES $NIX_BUILD_CORES |
When `build-cores` is less than the number of CPUs, `make -l$NIX_BUILD_CORES` keeps the remaining CPUs unutilized even when `max-jobs` is greater than 1. This is good if you want to dedicate those CPUs to something other than Nix builds, and it is bad if you want to balance `build-cores` with `max-jobs` to maximize utilization of the system without overloading it. To achieve the latter goal `make -l` should be given the optimal load average which is now available in `$NIX_MAX_CORES`. The implementation mirrors Ninja: https://github.com/ninja-build/ninja/blob/e234a7bd/src/ninja.cc#L222-L233 https://github.com/ninja-build/ninja/blob/e234a7bd/src/util.cc#L473-L481 Co-authored-by: Orivej Desh <[email protected]>
Co-authored-by: Orivej Desh <[email protected]>
8 tasks
Contributor
|
if only we could make this configurable like max-cores 🙁 |
Member
Author
|
feel free to adopt this pr |
21 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
READ #31965
When
build-coresis less than the number of CPUs,make -l$NIX_BUILD_CORESkeeps the remaining CPUs unutilized even when
max-jobsis greater than 1.This is good if you want to dedicate those CPUs to something other than Nix
builds, and it is bad if you want to balance
build-coreswithmax-jobstomaximize utilization of the system without overloading it. To achieve the latter
goal
make -lshould be given the optimal load average which is now availablein
$NIX_MAX_CORES.The implementation mirrors Ninja:
https://github.com/ninja-build/ninja/blob/e234a7bd/src/ninja.cc#L222-L233
https://github.com/ninja-build/ninja/blob/e234a7bd/src/util.cc#L473-L481
Motivation for this change
Rebased #31965, closes #31965
Things done
sandbox = trueset innix.conf? (See Nix manual)nix-shell -p nixpkgs-review --run "nixpkgs-review wip"./result/bin/)