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 Sep 25, 2023
1 parent 1e99b7a commit f25f19a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions doc/manual/src/release-notes/rl-next.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Release X.Y (202?-??-??)

- The `max-jobs` setting now defaults to `auto`.
5 changes: 2 additions & 3 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,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();
}

StringSet Settings::getDefaultSystemFeatures()
Expand Down Expand Up @@ -266,7 +265,7 @@ 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());
if (str == "auto") return getMaxThreads();
else {
if (auto n = string2Int<decltype(value)>(str))
return *n;
Expand Down
13 changes: 6 additions & 7 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,14 @@ 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. `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-max-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 amount of threads available 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 f25f19a

Please sign in to comment.