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 28, 2023
1 parent 1da1642 commit a405bf5
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 23 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
Expand Up @@ -3,3 +3,5 @@
- [URL flake references](@docroot@/command-ref/new-cli/nix3-flake.md#flake-references) now support [percent-encoded](https://datatracker.ietf.org/doc/html/rfc3986#section-2.1) characters.

- [Path-like flake references](@docroot@/command-ref/new-cli/nix3-flake.md#path-like-syntax) now accept arbitrary unicode characters (except `#` and `?`).

- The [`max-jobs` setting](@docroot@/command-ref/conf-file.md#conf-max-jobs) now defaults to `auto`.
2 changes: 1 addition & 1 deletion src/build-remote/build-remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static int main_build_remote(int argc, char * * argv)
settings.set(name, value);
}

auto maxBuildJobs = settings.maxBuildJobs;
auto maxBuildJobs = settings.maxBuildJobs.get();
settings.maxBuildJobs.set("1"); // hack to make tests with local?root= work

initPlugins();
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/build/derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ HookReply DerivationGoal::tryBuildHook()
/* Send the request to the hook. */
worker.hook->sink
<< "try"
<< (worker.getNrLocalBuilds() < settings.maxBuildJobs ? 1 : 0)
<< (worker.getNrLocalBuilds() < settings.maxBuildJobs.get() ? 1 : 0)
<< drv->platform
<< worker.store.printStorePath(drvPath)
<< parsedDrv->getRequiredSystemFeatures();
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void LocalDerivationGoal::killSandbox(bool getStats)
void LocalDerivationGoal::tryLocalBuild()
{
unsigned int curBuilds = worker.getNrLocalBuilds();
if (curBuilds >= settings.maxBuildJobs) {
if (curBuilds >= settings.maxBuildJobs.get()) {
state = &DerivationGoal::tryToBuild;
worker.waitForBuildSlot(shared_from_this());
outputLocks.unlock();
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/build/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void Worker::waitForBuildSlot(GoalPtr goal)
{
debug("wait for build slot");
bool isSubstitutionGoal = goal->jobCategory() == JobCategory::Substitution;
if ((!isSubstitutionGoal && getNrLocalBuilds() < settings.maxBuildJobs) ||
if ((!isSubstitutionGoal && getNrLocalBuilds() < settings.maxBuildJobs.get()) ||
(isSubstitutionGoal && getNrSubstitutions() < settings.maxSubstitutionJobs))
wakeUp(goal); /* we can do it right away */
else
Expand Down Expand Up @@ -371,7 +371,7 @@ void Worker::run(const Goals & _topGoals)
if (!children.empty() || !waitingForAWhile.empty())
waitForInput();
else {
if (awake.empty() && 0U == settings.maxBuildJobs)
if (awake.empty() && 0U == settings.maxBuildJobs.get())
{
if (getMachines().empty())
throw Error("unable to start any build; either increase '--max-jobs' "
Expand Down
22 changes: 16 additions & 6 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 @@ -264,18 +263,29 @@ template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::s
});
}

unsigned int MaxBuildJobsSetting::parse(const std::string & str) const
const unsigned int MaxBuildJobsSetting::get() const
{
if (str == "auto") return std::max(1U, std::thread::hardware_concurrency());
return value.value_or(getMaxThreads());
}

std::string MaxBuildJobsSetting::to_string() const
{
if (value == std::nullopt) return "auto";

return std::to_string(*value);
}

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


Paths PluginFilesSetting::parse(const std::string & str) const
{
if (pluginsLoaded)
Expand Down
27 changes: 16 additions & 11 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ namespace nix {

typedef enum { smEnabled, smRelaxed, smDisabled } SandboxMode;

struct MaxBuildJobsSetting : public BaseSetting<unsigned int>
class MaxBuildJobsSetting : public BaseSetting<std::optional<unsigned int>>
{
public:

MaxBuildJobsSetting(Config * options,
unsigned int def,
const std::optional<unsigned int> def,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: BaseSetting<unsigned int>(def, true, name, description, aliases)
: BaseSetting<std::optional<unsigned int>>(def, true, name, description, aliases)
{
options->addSetting(this);
}

unsigned int parse(const std::string & str) const override;
const unsigned int get() const;

std::string to_string() const override;

std::optional<unsigned int> parse(const std::string & str) const override;
};

struct PluginFilesSetting : public BaseSetting<Paths>
Expand Down Expand Up @@ -147,15 +153,14 @@ public:
"the log to show if a build fails."};

MaxBuildJobsSetting maxBuildJobs{
this, 1, "max-jobs",
this, std::nullopt, "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
2 changes: 1 addition & 1 deletion src/libstore/remote-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void RemoteStore::setOptions(Connection & conn)
<< settings.keepGoing
<< settings.tryFallback
<< verbosity
<< settings.maxBuildJobs
<< settings.maxBuildJobs.get()
<< settings.maxSilentTime
<< true
<< (settings.verboseBuild ? lvlError : lvlVomit)
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 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 a405bf5

Please sign in to comment.