Skip to content

Commit 01951a6

Browse files
committed
Auto merge of #42069 - QuietMisdreavus:low_pri, r=alexchrichton
Add an option to run rustbuild on low priority on Windows and Unix This is a resurrection of #40776, combining their Windows setup with an additional setup on Unix to set the program group's *nice*ness to +10 (low-but-not-lowest priority, mirroring the priority in the Windows setup) when the `low_priority` option is on.
2 parents 1cda810 + dd0855d commit 01951a6

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-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
@@ -148,6 +149,7 @@ struct Build {
148149
target: Vec<String>,
149150
cargo: Option<String>,
150151
rustc: Option<String>,
152+
low_priority: Option<bool>,
151153
compiler_docs: Option<bool>,
152154
docs: Option<bool>,
153155
submodules: Option<bool>,
@@ -306,6 +308,7 @@ impl Config {
306308
config.nodejs = build.nodejs.map(PathBuf::from);
307309
config.gdb = build.gdb.map(PathBuf::from);
308310
config.python = build.python.map(PathBuf::from);
311+
set(&mut config.low_priority, build.low_priority);
309312
set(&mut config.compiler_docs, build.compiler_docs);
310313
set(&mut config.docs, build.docs);
311314
set(&mut config.submodules, build.submodules);

src/bootstrap/config.toml.example

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@
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, by setting the process group's "nice" value
156+
# to +10 on Unix platforms, and by using a "low priority" job object on Windows.
157+
#low-priority = false
158+
155159
# =============================================================================
156160
# General install configuration options
157161
# =============================================================================

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

+18-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,21 @@ pub mod util;
108111
#[cfg(windows)]
109112
mod job;
110113

111-
#[cfg(not(windows))]
114+
#[cfg(unix)]
115+
mod job {
116+
use libc;
117+
118+
pub unsafe fn setup(build: &mut ::Build) {
119+
if build.config.low_priority {
120+
libc::setpriority(libc::PRIO_PGRP as _, 0, 10);
121+
}
122+
}
123+
}
124+
125+
#[cfg(not(any(unix, windows)))]
112126
mod job {
113-
pub unsafe fn setup() {}
127+
pub unsafe fn setup(_build: &mut ::Build) {
128+
}
114129
}
115130

116131
pub use config::Config;
@@ -263,7 +278,7 @@ impl Build {
263278
/// Executes the entire build, as configured by the flags and configuration.
264279
pub fn build(&mut self) {
265280
unsafe {
266-
job::setup();
281+
job::setup(self);
267282
}
268283

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

0 commit comments

Comments
 (0)