Skip to content

Comments

Optimize Nix build CPU utilization with NIX_MAX_CORES#141266

Closed
Artturin wants to merge 2 commits intoNixOS:stagingfrom
Artturin:nixmaxcores
Closed

Optimize Nix build CPU utilization with NIX_MAX_CORES#141266
Artturin wants to merge 2 commits intoNixOS:stagingfrom
Artturin:nixmaxcores

Conversation

@Artturin
Copy link
Member

@Artturin Artturin commented Oct 11, 2021

READ #31965

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

Motivation for this change

Rebased #31965, closes #31965

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added 6.topic: stdenv Standard environment 6.topic: TeX Issues regarding texlive and TeX in general labels Oct 11, 2021
@Artturin Artturin changed the base branch from master to staging October 11, 2021 16:22
@Artturin Artturin requested a review from vcunat October 11, 2021 16:28
@vcunat
Copy link
Member

vcunat commented Oct 11, 2021

I don't understand why use both guess=$(nproc 2>/dev/null || true) and the guessParallelism() function.

@Artturin
Copy link
Member Author

I don't understand why use both guess=$(nproc 2>/dev/null || true) and the guessParallelism() function.

forgot to remove it

@ofborg ofborg bot added 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch. labels Oct 11, 2021
@ofborg ofborg bot added 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. labels Oct 11, 2021
@SuperSandro2000
Copy link
Member

Does this need a doc update?

@Artturin
Copy link
Member Author

Artturin commented Oct 11, 2021

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

Artturin and others added 2 commits October 11, 2021 22:51
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]>
@pennae
Copy link
Contributor

pennae commented Oct 30, 2021

if only we could make this configurable like max-cores 🙁 -jN -l$((2 * N)) seems to be ideal to maximize cpu utilization on parallel builds, but understandably not all people would want that either

@Artturin
Copy link
Member Author

Artturin commented Nov 1, 2021

feel free to adopt this pr

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

Labels

6.topic: stdenv Standard environment 6.topic: TeX Issues regarding texlive and TeX in general 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants