From 7251d048fa812d2551b7003bc9f13a8f5d4c95a5 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 28 Feb 2017 12:54:50 +0100 Subject: Support auto-configuration of build-max-jobs "build-max-jobs" and the "-j" option can now be set to "auto" to use the number of CPUs in the system. (Unlike build-cores, it doesn't use 0 to imply auto-configuration, because a) magic values are a bad idea in general; b) 0 is a legitimate value used to disable local building.) Fixes #1198. --- doc/manual/command-ref/conf-file.xml | 6 +++--- doc/manual/command-ref/opt-common.xml | 5 +++-- src/libmain/shared.cc | 5 ++++- src/libstore/globals.cc | 9 ++++++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/doc/manual/command-ref/conf-file.xml b/doc/manual/command-ref/conf-file.xml index 0f7a2deba049..96f8a4b608a4 100644 --- a/doc/manual/command-ref/conf-file.xml +++ b/doc/manual/command-ref/conf-file.xml @@ -101,9 +101,9 @@ flag, e.g. --option gc-keep-outputs false. This option defines the maximum number of jobs that Nix will try to build in parallel. The default is - 1. You should generally set it to the number - of CPUs in your system (e.g., 2 on an Athlon 64 - X2). It can be overridden using the () command line switch. diff --git a/doc/manual/command-ref/opt-common.xml b/doc/manual/command-ref/opt-common.xml index 2a076877a1b4..2aa41c4d4389 100644 --- a/doc/manual/command-ref/opt-common.xml +++ b/doc/manual/command-ref/opt-common.xml @@ -93,8 +93,9 @@ Sets the maximum number of build jobs that Nix will - perform in parallel to the specified number. The default is - specified by the auto to use the number of CPUs in the system. + The default is specified by the build-max-jobs configuration setting, which itself defaults to 1. A higher value is useful on SMP systems or to diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 53fa83fe0dee..326202d295fb 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -167,6 +167,10 @@ struct LegacyArgs : public MixCommonArgs settings.set("build-fallback", "true"); }); + mkFlag1('j', "max-jobs", "jobs", "maximum number of parallel builds", [=](std::string s) { + settings.set("build-max-jobs", s); + }); + auto intSettingAlias = [&](char shortName, const std::string & longName, const std::string & description, const std::string & dest) { mkFlag(shortName, longName, description, [=](unsigned int n) { @@ -174,7 +178,6 @@ struct LegacyArgs : public MixCommonArgs }); }; - intSettingAlias('j', "max-jobs", "maximum number of parallel builds", "build-max-jobs"); intSettingAlias(0, "cores", "maximum number of CPU cores to use inside a build", "build-cores"); intSettingAlias(0, "max-silent-time", "number of seconds of silence before a build is killed", "build-max-silent-time"); intSettingAlias(0, "timeout", "number of seconds before a build is killed", "build-timeout"); diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index 12e2a3cf43dd..07af629260af 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -147,7 +147,14 @@ int Settings::get(const string & name, int def) void Settings::update() { _get(tryFallback, "build-fallback"); - _get(maxBuildJobs, "build-max-jobs"); + + auto s = get("build-max-jobs", std::string("1")); + if (s == "auto") + maxBuildJobs = std::max(1U, std::thread::hardware_concurrency()); + else + if (!string2Int(s, maxBuildJobs)) + throw Error("configuration setting ‘build-max-jobs’ should be ‘auto’ or an integer"); + _get(buildCores, "build-cores"); _get(thisSystem, "system"); _get(maxSilentTime, "build-max-silent-time"); -- cgit 1.4.1