Skip to content

Commit

Permalink
libstore: Add load-limit setting to dynamically control parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
centromere committed Aug 2, 2022
1 parent 780a479 commit 601e940
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/manual/src/release-notes/rl-next.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Release X.Y (202?-??-??)

* Add new `load-limit` setting to control the `-lN` flag of GNU Make.
3 changes: 3 additions & 0 deletions src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,9 @@ void LocalDerivationGoal::initEnv()
/* The maximum number of cores to utilize for parallel building. */
env["NIX_BUILD_CORES"] = (format("%d") % settings.buildCores).str();

/* If the load average is greater than N, disable parallel building. */
env["NIX_LOAD_LIMIT"] = (format("%d") % settings.loadLimit).str();

initTmpDir();

/* Compatibility hack with Nix <= 0.7: if this is a fixed-output
Expand Down
24 changes: 24 additions & 0 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ public:
)",
{"build-cores"}, false};

Setting<unsigned int> loadLimit{
this,
getDefaultCores(),
"load-limit",
R"(
Sets the value of the `NIX_LOAD_LIMIT` environment variable in the
invocation of builders. Builders can use this value at their discretion
to dynamically control the amount of parallelism with respect to the
machine's load average. For instance, in Nixpkgs, if this value is >0,
the builder passes the `-lN` flag to GNU Make. In this case, if the
load average of the machine exceeds `N`, the amount of parallelism will
be dynamically reduced to 1.
By default, it is set to the number of cores available to Nix.
On busy machines where Nix co-exists with other workloads, parallelism
may not work as intended. For example, consider a 64 core machine whose
load average is 24 and where Nix is limited to 8 cores. By default,
`-j8 -l8` will be passed to GNU Make. Since the load average exceeds 8,
no parallelism will take place despite the fact that 8 cores are
available. In this case, `load-limit` should be set to `0` to prevent
the `-lN` flag from being passed to GNU Make.
)"};

/* Read-only mode. Don't copy stuff to the store, don't change
the database. */
bool readOnlyMode = false;
Expand Down
1 change: 1 addition & 0 deletions src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ static void main_nix_build(int argc, char * * argv)
env["NIX_BUILD_TOP"] = env["TMPDIR"] = env["TEMPDIR"] = env["TMP"] = env["TEMP"] = *tmp;
env["NIX_STORE"] = store->storeDir;
env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores);
env["NIX_LOAD_LIMIT"] = std::to_string(settings.loadLimit);

auto passAsFile = tokenizeString<StringSet>(getOr(drv.env, "passAsFile", ""));

Expand Down

0 comments on commit 601e940

Please sign in to comment.