Author: simons
Date: 2010-06-23 14:34:08 +0000 (Wed, 23 Jun 2010)
New Revision: 22397

You can view the changes in this commit at:
   https://svn.nixos.org/viewvc/nix?rev=22397&view=rev

Modified:
   nix/trunk/nix.conf.example
   nix/trunk/src/libmain/shared.cc
   nix/trunk/src/libstore/build.cc
   nix/trunk/src/libstore/globals.cc
   nix/trunk/src/libstore/globals.hh

Log:
Added support for passing an (impure) NIX_BUILD_CORES variable to build 
expressions.

This patch adds the configuration file variable "build-cores" and the
command line argument "--cores". These settings specify the number of
CPU cores to utilize for parallel building within a job, i.e. by passing
an appropriate "-j" flag to GNU Make. The default value is 1, which
means that parallel building is *disabled*. If the number of build cores
is specified as 0 (synonymously: "guess" or "auto"), then the actual
value is supposed to be auto-detected by builders at run-time, i.e by
calling the nproc(1) utility from coreutils.

The environment variable $NIX_BUILD_CORES is available to builders, but
the contents of that variable does *not* influence the hash that goes
into the $out store path, i.e. the number of build cores to be utilized
can be changed at will without requiring any re-builds.

Changes:

Modified: nix/trunk/nix.conf.example
===================================================================
--- nix/trunk/nix.conf.example  2010-06-23 14:07:47 UTC (rev 22396)
+++ nix/trunk/nix.conf.example  2010-06-23 14:34:08 UTC (rev 22397)
@@ -59,6 +59,18 @@
 #build-max-jobs = 1
 
 
+### Option `build-cores'
+#
+# This option defines the number of CPU cores to utilize in parallel
+# within a build job, i.e. by passing an appropriate `-jN' flag to
+# GNU make. The default is 1, meaning that parallel building within
+# jobs is disabled. Passing the special values `0', `auto', or
+# `guess' causes Nix to try and auto-detect the number of available
+# cores on the local host. This setting can be overridden using the
+# `--cores' command line switch.
+#build-cores = 1
+
+
 ### Option `build-max-silent-time'
 #
 # This option defines the maximum number of seconds that a builder can

Modified: nix/trunk/src/libmain/shared.cc
===================================================================
--- nix/trunk/src/libmain/shared.cc     2010-06-23 14:07:47 UTC (rev 22396)
+++ nix/trunk/src/libmain/shared.cc     2010-06-23 14:34:08 UTC (rev 22397)
@@ -135,6 +135,12 @@
     /* Get some settings from the configuration file. */
     thisSystem = querySetting("system", SYSTEM);
     maxBuildJobs = queryIntSetting("build-max-jobs", 1);
+    string tmp = querySetting("build-cores", "/UNDEFINED");
+    std::transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
+    if (tmp == "auto" || tmp == "guess")
+      buildCores = 0;
+    else
+      buildCores = queryIntSetting("build-cores", 1);
     maxSilentTime = queryIntSetting("build-max-silent-time", 0);
 
     /* Catch SIGINT. */
@@ -226,6 +232,14 @@
             tryFallback = true;
         else if (arg == "--max-jobs" || arg == "-j")
             maxBuildJobs = getIntArg<unsigned int>(arg, i, args.end());
+        else if (arg == "--cores") {
+            string tmp = *(++i);
+            std::transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
+            if (tmp == "auto" || tmp == "guess")
+              buildCores = 0u;
+            else
+              buildCores = getIntArg<unsigned int>(arg, --i, args.end());
+        }
         else if (arg == "--readonly-mode")
             readOnlyMode = true;
         else if (arg == "--max-silent-time")

Modified: nix/trunk/src/libstore/build.cc
===================================================================
--- nix/trunk/src/libstore/build.cc     2010-06-23 14:07:47 UTC (rev 22396)
+++ nix/trunk/src/libstore/build.cc     2010-06-23 14:34:08 UTC (rev 22397)
@@ -1411,6 +1411,9 @@
        in the store or in the build directory). */
     env["NIX_STORE"] = nixStore;
 
+    /* The maximum number of cores to utilize for parallel building. */
+    env["NIX_BUILD_CORES"] = (format("%d") % buildCores).str();
+
     /* Add all bindings specified in the derivation. */
     foreach (StringPairs::iterator, i, drv.env)
         env[i->first] = i->second;

Modified: nix/trunk/src/libstore/globals.cc
===================================================================
--- nix/trunk/src/libstore/globals.cc   2010-06-23 14:07:47 UTC (rev 22396)
+++ nix/trunk/src/libstore/globals.cc   2010-06-23 14:34:08 UTC (rev 22397)
@@ -22,6 +22,7 @@
 bool tryFallback = false;
 Verbosity buildVerbosity = lvlInfo;
 unsigned int maxBuildJobs = 1;
+unsigned int buildCores = 1;
 bool readOnlyMode = false;
 string thisSystem = "unset";
 time_t maxSilentTime = 0;

Modified: nix/trunk/src/libstore/globals.hh
===================================================================
--- nix/trunk/src/libstore/globals.hh   2010-06-23 14:07:47 UTC (rev 22396)
+++ nix/trunk/src/libstore/globals.hh   2010-06-23 14:34:08 UTC (rev 22397)
@@ -55,6 +55,11 @@
 /* Maximum number of parallel build jobs.  0 means unlimited. */
 extern unsigned int maxBuildJobs;
 
+/* Number of CPU cores to utilize in parallel within a build, i.e. by passing
+   this number to Make via '-j'. 0 means that the number of actual CPU cores on
+   the local host ought to be auto-detected. */
+extern unsigned int buildCores;
+
 /* Read-only mode.  Don't copy stuff to the store, don't change the
    database. */
 extern bool readOnlyMode;

_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to