Skip to content

Add an option to run rustbuild on low priority on Windows and Unix #42069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you expand this to explain a bit about what this means? Referencing nice here for Unix and "low priority" job object for Windows would probably be sufficient.

#low_priority = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In TOML configuration we tend to prefer dashes, could this perhaps be low-priority?


# =============================================================================
# General install configuration options
# =============================================================================
Expand Down
9 changes: 8 additions & 1 deletion src/bootstrap/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use std::env;
use std::io;
use std::mem;
use Build;

type HANDLE = *mut u8;
type BOOL = i32;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down
29 changes: 26 additions & 3 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is defined as

pub const PRIO_PGRP: ::c_int = 1;

for all platforms, is the #[cfg] here necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The re-declaration is necessary because of how setpriority is defined for glibc platforms:

pub fn setpriority(which: ::__priority_which_t, who: ::id_t,
                                   prio: ::c_int) -> ::c_int;

Where __priority_which_t is at the top of the file:

pub type __priority_which_t = ::c_uint;

Without this re-declaration, I get a type error when calling setpriority on x86_64-unknown-linux-gnu.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep that'd do it!

#[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;
Expand Down Expand Up @@ -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 {
Expand Down