Skip to content

Commit

Permalink
libstore: change max-jobs default to auto
Browse files Browse the repository at this point in the history
  • Loading branch information
Enzime committed Nov 5, 2023
1 parent c13f005 commit 6ca89c7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
4 changes: 3 additions & 1 deletion doc/manual/src/release-notes/rl-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@

- The flake-specific flags `--recreate-lock-file` and `--update-input` have been removed from all commands operating on installables.
They are superceded by `nix flake update`.

- Commit signature verification for the [`builtins.fetchGit`](@docroot@/language/builtins.md#builtins-fetchGit) is added as the new [`verified-fetches` experimental feature](@docroot@/contributing/experimental-features.md#xp-feature-verified-fetches).

- The [`max-jobs` setting](@docroot@/command-ref/conf-file.md#conf-max-jobs) now defaults to `auto`.
16 changes: 7 additions & 9 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,12 @@ std::vector<Path> getUserConfigFiles()

unsigned int Settings::getDefaultCores()
{
const unsigned int concurrency = std::max(1U, std::thread::hardware_concurrency());
const unsigned int maxCPU = getMaxCPU();

if (maxCPU > 0)
return maxCPU;
else
return concurrency;
return getMaxThreads();
}

#if __APPLE__
Expand Down Expand Up @@ -297,13 +296,12 @@ template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::s

unsigned int MaxBuildJobsSetting::parse(const std::string & str) const
{
if (str == "auto") return std::max(1U, std::thread::hardware_concurrency());
else {
if (auto n = string2Int<decltype(value)>(str))
return *n;
else
throw UsageError("configuration setting '%s' should be 'auto' or an integer", name);
}
if (str == "auto") return getMaxThreads();

if (auto n = string2Int<decltype(value)>(str))
return *n;
else
throw UsageError("configuration setting '%s' should be 'auto' or an integer", name);
}


Expand Down
16 changes: 8 additions & 8 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ public:
"the log to show if a build fails."};

MaxBuildJobsSetting maxBuildJobs{
this, 1, "max-jobs",
this, getMaxThreads(), "max-jobs",
R"(
This option defines the maximum number of jobs that Nix will try to
build in parallel. The default is `1`. The special value `auto`
causes Nix to use the number of CPUs in your system. `0` is useful
when using remote builders to prevent any local builds (except for
`preferLocalBuild` derivation attribute which executes locally
regardless). It can be overridden using the `--max-jobs` (`-j`)
command line switch.
build in parallel. The default is `auto` which causes Nix to use
the number of CPUs in your system.
It can be overridden using the `--max-jobs` / `-j` command line option.
Setting this to `0` when using remote builders will prevent any local builds.
The [`preferLocalBuild`](@docroot@/language/advanced-attributes.md#adv-attr-preferLocalBuild) derivation attribute will make the build always execute locally.
)",
{"build-max-jobs"}};
{"build-max-jobs"}, "`auto`"};

Setting<unsigned int> maxSubstitutionJobs{
this, 16, "max-substitution-jobs",
Expand Down
5 changes: 5 additions & 0 deletions src/libutil/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,11 @@ void drainFD(int fd, Sink & sink, bool block)

//////////////////////////////////////////////////////////////////////

unsigned int getMaxThreads()
{
return std::max(1U, std::thread::hardware_concurrency());
}

unsigned int getMaxCPU()
{
#if __linux__
Expand Down
5 changes: 5 additions & 0 deletions src/libutil/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ std::string drainFD(int fd, bool block = true, const size_t reserveSize=0);

void drainFD(int fd, Sink & sink, bool block = true);

/**
* Returns the number of threads available for simultaneous execution if possible, otherwise returns 1.
*/
unsigned int getMaxThreads();

/**
* If cgroups are active, attempt to calculate the number of CPUs available.
* If cgroups are unavailable or if cpu.max is set to "max", return 0.
Expand Down

0 comments on commit 6ca89c7

Please sign in to comment.