Skip to content

Commit 1a7375b

Browse files
ZoxcQuietMisdreavus
authored andcommitted
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.
1 parent 4d09a0e commit 1a7375b

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/bootstrap/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub struct Config {
9494
pub backtrace: bool, // support for RUST_BACKTRACE
9595

9696
// misc
97+
pub low_priority: bool,
9798
pub channel: String,
9899
pub quiet_tests: bool,
99100
// Fallback musl-root for all targets
@@ -146,6 +147,7 @@ struct Build {
146147
target: Vec<String>,
147148
cargo: Option<String>,
148149
rustc: Option<String>,
150+
low_priority: Option<bool>,
149151
compiler_docs: Option<bool>,
150152
docs: Option<bool>,
151153
submodules: Option<bool>,
@@ -302,6 +304,7 @@ impl Config {
302304
config.nodejs = build.nodejs.map(PathBuf::from);
303305
config.gdb = build.gdb.map(PathBuf::from);
304306
config.python = build.python.map(PathBuf::from);
307+
set(&mut config.low_priority, build.low_priority);
305308
set(&mut config.compiler_docs, build.compiler_docs);
306309
set(&mut config.docs, build.docs);
307310
set(&mut config.submodules, build.submodules);

src/bootstrap/config.toml.example

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@
152152
# known-good version of OpenSSL, compile it, and link it to Cargo.
153153
#openssl-static = false
154154

155+
# Run the build with low priority
156+
#low_priority = false
157+
155158
# =============================================================================
156159
# General install configuration options
157160
# =============================================================================

src/bootstrap/job.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use std::env;
4343
use std::io;
4444
use std::mem;
45+
use Build;
4546

4647
type HANDLE = *mut u8;
4748
type BOOL = i32;
@@ -60,8 +61,10 @@ const DUPLICATE_SAME_ACCESS: DWORD = 0x2;
6061
const PROCESS_DUP_HANDLE: DWORD = 0x40;
6162
const JobObjectExtendedLimitInformation: JOBOBJECTINFOCLASS = 9;
6263
const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE: DWORD = 0x2000;
64+
const JOB_OBJECT_LIMIT_PRIORITY_CLASS: DWORD = 0x00000020;
6365
const SEM_FAILCRITICALERRORS: UINT = 0x0001;
6466
const SEM_NOGPFAULTERRORBOX: UINT = 0x0002;
67+
const BELOW_NORMAL_PRIORITY_CLASS: DWORD = 0x00004000;
6568

6669
extern "system" {
6770
fn CreateJobObjectW(lpJobAttributes: *mut u8, lpName: *const u8) -> HANDLE;
@@ -118,7 +121,7 @@ struct JOBOBJECT_BASIC_LIMIT_INFORMATION {
118121
SchedulingClass: DWORD,
119122
}
120123

121-
pub unsafe fn setup() {
124+
pub unsafe fn setup(build: &mut Build) {
122125
// Tell Windows to not show any UI on errors (such as not finding a required dll
123126
// during startup or terminating abnormally). This is important for running tests,
124127
// since some of them use abnormal termination by design.
@@ -136,6 +139,10 @@ pub unsafe fn setup() {
136139
// children will reside in the job by default.
137140
let mut info = mem::zeroed::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>();
138141
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
142+
if build.config.low_priority {
143+
info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_PRIORITY_CLASS;
144+
info.BasicLimitInformation.PriorityClass = BELOW_NORMAL_PRIORITY_CLASS;
145+
}
139146
let r = SetInformationJobObject(job,
140147
JobObjectExtendedLimitInformation,
141148
&mut info as *mut _ as LPVOID,

src/bootstrap/lib.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ extern crate num_cpus;
7676
extern crate rustc_serialize;
7777
extern crate toml;
7878

79+
#[cfg(unix)]
80+
extern crate libc;
81+
7982
use std::cmp;
8083
use std::collections::HashMap;
8184
use std::env;
@@ -108,9 +111,29 @@ pub mod util;
108111
#[cfg(windows)]
109112
mod job;
110113

111-
#[cfg(not(windows))]
114+
#[cfg(unix)]
112115
mod job {
113-
pub unsafe fn setup() {}
116+
use libc;
117+
118+
//apparently glibc defines their own enum for this parameter, in a different type
119+
#[cfg(not(any(target_env = "musl", target_env = "musleabi", target_env = "musleabihf",
120+
target_os = "emscripten", target_arch = "mips", target_arch = "mipsel")))]
121+
const PRIO_PGRP: libc::c_uint = libc::PRIO_PGRP as libc::c_uint;
122+
#[cfg(any(target_env = "musl", target_env = "musleabi", target_env = "musleabihf",
123+
target_os = "emscripten", target_arch = "mips", target_arch = "mipsel"))]
124+
const PRIO_PGRP: libc::c_int = libc::PRIO_PGRP;
125+
126+
pub unsafe fn setup(build: &mut ::Build) {
127+
if build.config.low_priority {
128+
libc::setpriority(PRIO_PGRP, 0, 10);
129+
}
130+
}
131+
}
132+
133+
#[cfg(not(any(unix, windows)))]
134+
mod job {
135+
pub unsafe fn setup(_build: &mut ::Build) {
136+
}
114137
}
115138

116139
pub use config::Config;
@@ -263,7 +286,7 @@ impl Build {
263286
/// Executes the entire build, as configured by the flags and configuration.
264287
pub fn build(&mut self) {
265288
unsafe {
266-
job::setup();
289+
job::setup(self);
267290
}
268291

269292
if let Subcommand::Clean = self.flags.cmd {

0 commit comments

Comments
 (0)