about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2017-03-23 22:57:29 +0100
committerQuietMisdreavus <grey@quietmisdreavus.net>2017-05-16 16:25:57 -0500
commit1a7375b7e0fe41aa791ecf69dcceea7880302279 (patch)
treeeca7b4391da7793c7e53b2b291d07e2d13d6adfb
parent4d09a0eb5d201c831d19f3fe93fbe636eca7d97d (diff)
downloadrust-1a7375b7e0fe41aa791ecf69dcceea7880302279.tar.gz
rust-1a7375b7e0fe41aa791ecf69dcceea7880302279.zip
Add an option to run rustbuild on low priority
This is a resurrection of #40776, combining their Windows setup with an
additional setup on Unix to set the program group's niceness to +10
(low-but-not-lowest priority) when the `low_priority` option is on.
-rw-r--r--src/bootstrap/config.rs3
-rw-r--r--src/bootstrap/config.toml.example3
-rw-r--r--src/bootstrap/job.rs9
-rw-r--r--src/bootstrap/lib.rs29
4 files changed, 40 insertions, 4 deletions
diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs
index 9c536111811..4f750945b72 100644
--- a/src/bootstrap/config.rs
+++ b/src/bootstrap/config.rs
@@ -94,6 +94,7 @@ pub struct Config {
     pub backtrace: bool, // support for RUST_BACKTRACE
 
     // misc
+    pub low_priority: bool,
     pub channel: String,
     pub quiet_tests: bool,
     // Fallback musl-root for all targets
@@ -146,6 +147,7 @@ struct Build {
     target: Vec<String>,
     cargo: Option<String>,
     rustc: Option<String>,
+    low_priority: Option<bool>,
     compiler_docs: Option<bool>,
     docs: Option<bool>,
     submodules: Option<bool>,
@@ -302,6 +304,7 @@ impl Config {
         config.nodejs = build.nodejs.map(PathBuf::from);
         config.gdb = build.gdb.map(PathBuf::from);
         config.python = build.python.map(PathBuf::from);
+        set(&mut config.low_priority, build.low_priority);
         set(&mut config.compiler_docs, build.compiler_docs);
         set(&mut config.docs, build.docs);
         set(&mut config.submodules, build.submodules);
diff --git a/src/bootstrap/config.toml.example b/src/bootstrap/config.toml.example
index 25da976a555..52937e6ac9b 100644
--- a/src/bootstrap/config.toml.example
+++ b/src/bootstrap/config.toml.example
@@ -152,6 +152,9 @@
 # known-good version of OpenSSL, compile it, and link it to Cargo.
 #openssl-static = false
 
+# Run the build with low priority
+#low_priority = false
+
 # =============================================================================
 # General install configuration options
 # =============================================================================
diff --git a/src/bootstrap/job.rs b/src/bootstrap/job.rs
index c3859275e6f..72a5d1338b8 100644
--- a/src/bootstrap/job.rs
+++ b/src/bootstrap/job.rs
@@ -42,6 +42,7 @@
 use std::env;
 use std::io;
 use std::mem;
+use Build;
 
 type HANDLE = *mut u8;
 type BOOL = i32;
@@ -60,8 +61,10 @@ const DUPLICATE_SAME_ACCESS: DWORD = 0x2;
 const PROCESS_DUP_HANDLE: DWORD = 0x40;
 const JobObjectExtendedLimitInformation: JOBOBJECTINFOCLASS = 9;
 const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE: DWORD = 0x2000;
+const JOB_OBJECT_LIMIT_PRIORITY_CLASS: DWORD = 0x00000020;
 const SEM_FAILCRITICALERRORS: UINT = 0x0001;
 const SEM_NOGPFAULTERRORBOX: UINT = 0x0002;
+const BELOW_NORMAL_PRIORITY_CLASS: DWORD = 0x00004000;
 
 extern "system" {
     fn CreateJobObjectW(lpJobAttributes: *mut u8, lpName: *const u8) -> HANDLE;
@@ -118,7 +121,7 @@ struct JOBOBJECT_BASIC_LIMIT_INFORMATION {
     SchedulingClass: DWORD,
 }
 
-pub unsafe fn setup() {
+pub unsafe fn setup(build: &mut Build) {
     // Tell Windows to not show any UI on errors (such as not finding a required dll
     // during startup or terminating abnormally).  This is important for running tests,
     // since some of them use abnormal termination by design.
@@ -136,6 +139,10 @@ pub unsafe fn setup() {
     // children will reside in the job by default.
     let mut info = mem::zeroed::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>();
     info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
+    if build.config.low_priority {
+        info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_PRIORITY_CLASS;
+        info.BasicLimitInformation.PriorityClass = BELOW_NORMAL_PRIORITY_CLASS;
+    }
     let r = SetInformationJobObject(job,
                                     JobObjectExtendedLimitInformation,
                                     &mut info as *mut _ as LPVOID,
diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs
index ea0b521a2ce..a9f53cda7ed 100644
--- a/src/bootstrap/lib.rs
+++ b/src/bootstrap/lib.rs
@@ -76,6 +76,9 @@ extern crate num_cpus;
 extern crate rustc_serialize;
 extern crate toml;
 
+#[cfg(unix)]
+extern crate libc;
+
 use std::cmp;
 use std::collections::HashMap;
 use std::env;
@@ -108,9 +111,29 @@ pub mod util;
 #[cfg(windows)]
 mod job;
 
-#[cfg(not(windows))]
+#[cfg(unix)]
 mod job {
-    pub unsafe fn setup() {}
+    use libc;
+
+    //apparently glibc defines their own enum for this parameter, in a different type
+    #[cfg(not(any(target_env = "musl", target_env = "musleabi", target_env = "musleabihf",
+                  target_os = "emscripten", target_arch = "mips", target_arch = "mipsel")))]
+    const PRIO_PGRP: libc::c_uint = libc::PRIO_PGRP as libc::c_uint;
+    #[cfg(any(target_env = "musl", target_env = "musleabi", target_env = "musleabihf",
+              target_os = "emscripten", target_arch = "mips", target_arch = "mipsel"))]
+    const PRIO_PGRP: libc::c_int = libc::PRIO_PGRP;
+
+    pub unsafe fn setup(build: &mut ::Build) {
+        if build.config.low_priority {
+            libc::setpriority(PRIO_PGRP, 0, 10);
+        }
+    }
+}
+
+#[cfg(not(any(unix, windows)))]
+mod job {
+    pub unsafe fn setup(_build: &mut ::Build) {
+    }
 }
 
 pub use config::Config;
@@ -263,7 +286,7 @@ impl Build {
     /// Executes the entire build, as configured by the flags and configuration.
     pub fn build(&mut self) {
         unsafe {
-            job::setup();
+            job::setup(self);
         }
 
         if let Subcommand::Clean = self.flags.cmd {