Skip to content
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

Communicate over stderr jobserver token acquiring/releasing #67398

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3113,7 +3113,6 @@ dependencies = [
"chalk-engine",
"fmt_macros",
"graphviz",
"jobserver",
"log",
"measureme",
"parking_lot 0.9.0",
Expand All @@ -3127,6 +3126,7 @@ dependencies = [
"rustc_feature",
"rustc_hir",
"rustc_index",
"rustc_jobserver",
"rustc_macros",
"rustc_session",
"rustc_span",
Expand Down Expand Up @@ -3587,7 +3587,6 @@ version = "0.0.0"
dependencies = [
"bitflags",
"cc",
"jobserver",
"libc",
"log",
"memmap",
Expand All @@ -3602,6 +3601,7 @@ dependencies = [
"rustc_hir",
"rustc_incremental",
"rustc_index",
"rustc_jobserver",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -3636,7 +3636,6 @@ dependencies = [
"ena",
"graphviz",
"indexmap",
"jobserver",
"lazy_static 1.4.0",
"log",
"measureme",
Expand Down Expand Up @@ -3668,6 +3667,7 @@ dependencies = [
"rustc_feature",
"rustc_hir",
"rustc_interface",
"rustc_jobserver",
"rustc_lint",
"rustc_metadata",
"rustc_mir",
Expand Down Expand Up @@ -3796,6 +3796,7 @@ dependencies = [
"rustc_expand",
"rustc_hir",
"rustc_incremental",
"rustc_jobserver",
"rustc_lint",
"rustc_metadata",
"rustc_mir",
Expand All @@ -3818,6 +3819,16 @@ dependencies = [
"winapi 0.3.8",
]

[[package]]
name = "rustc_jobserver"
version = "0.0.0"
dependencies = [
"jobserver",
"lazy_static 1.4.0",
"log",
"serialize",
]

[[package]]
name = "rustc_lexer"
version = "0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ arena = { path = "../libarena" }
bitflags = "1.2.1"
fmt_macros = { path = "../libfmt_macros" }
graphviz = { path = "../libgraphviz" }
jobserver = "0.1"
rustc_jobserver = { path = "../librustc_jobserver" }
scoped-tls = "1.0"
log = { version = "0.4", features = ["release_max_level_info", "std"] }
rustc-rayon = "0.3.0"
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/query/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use {
rustc_data_structures::stable_hasher::{HashStable, StableHasher},
rustc_data_structures::sync::Lock,
rustc_data_structures::sync::Lrc,
rustc_data_structures::{jobserver, OnDrop},
rustc_rayon_core as rayon_core,
rustc_data_structures::OnDrop,
rustc_jobserver as jobserver, rustc_rayon_core as rayon_core,
rustc_span::DUMMY_SP,
std::iter::FromIterator,
std::{mem, process, thread},
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ num_cpus = "1.0"
memmap = "0.7"
log = "0.4.5"
libc = "0.2.44"
jobserver = "0.1.11"
tempfile = "3.1"

rustc_serialize = { path = "../libserialize", package = "serialize" }
Expand All @@ -34,3 +33,4 @@ rustc_incremental = { path = "../librustc_incremental" }
rustc_index = { path = "../librustc_index" }
rustc_target = { path = "../librustc_target" }
rustc_session = { path = "../librustc_session" }
rustc_jobserver = { path = "../librustc_jobserver" }
48 changes: 18 additions & 30 deletions src/librustc_codegen_ssa/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
};

use crate::traits::*;
use jobserver::{Acquired, Client};
use rustc::dep_graph::{WorkProduct, WorkProductFileKind, WorkProductId};
use rustc::middle::cstore::EncodedMetadata;
use rustc::middle::exported_symbols::SymbolExportLevel;
Expand All @@ -32,6 +31,7 @@ use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc_incremental::{
copy_cgu_workproducts_to_incr_comp_cache_dir, in_incr_comp_dir, in_incr_comp_dir_sess,
};
use rustc_jobserver::Acquired;
use rustc_session::cgu_reuse_tracker::CguReuseTracker;
use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::SourceMap;
Expand All @@ -41,7 +41,6 @@ use syntax::attr;

use std::any::Any;
use std::fs;
use std::io;
use std::mem;
use std::path::{Path, PathBuf};
use std::str;
Expand Down Expand Up @@ -453,7 +452,6 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
codegen_worker_send,
coordinator_receive,
total_cgus,
sess.jobserver.clone(),
Arc::new(modules_config),
Arc::new(metadata_config),
Arc::new(allocator_config),
Expand Down Expand Up @@ -905,7 +903,7 @@ fn execute_lto_work_item<B: ExtraBackendMethods>(
}

pub enum Message<B: WriteBackendMethods> {
Token(io::Result<Acquired>),
Token(Acquired),
NeedsFatLTO {
result: FatLTOInput<B>,
worker_id: usize,
Expand Down Expand Up @@ -953,7 +951,6 @@ fn start_executing_work<B: ExtraBackendMethods>(
codegen_worker_send: Sender<Message<B>>,
coordinator_receive: Receiver<Box<dyn Any + Send>>,
total_cgus: usize,
jobserver: Client,
modules_config: Arc<ModuleConfig>,
metadata_config: Arc<ModuleConfig>,
allocator_config: Arc<ModuleConfig>,
Expand Down Expand Up @@ -997,11 +994,12 @@ fn start_executing_work<B: ExtraBackendMethods>(
// get tokens on `coordinator_receive` which will
// get managed in the main loop below.
let coordinator_send2 = coordinator_send.clone();
let helper = jobserver
.into_helper_thread(move |token| {
let request_token = move || {
let coordinator_send2 = coordinator_send2.clone();
rustc_jobserver::request_token(move |token| {
drop(coordinator_send2.send(Box::new(Message::Token::<B>(token))));
})
.expect("failed to spawn helper thread");
});
};

let mut each_linked_rlib_for_lto = Vec::new();
drop(link::each_linked_rlib(crate_info, &mut |cnum, path| {
Expand Down Expand Up @@ -1308,7 +1306,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
.unwrap_or_else(|e| e);
work_items.insert(insertion_index, (work, cost));
if !cgcx.opts.debugging_opts.no_parallel_llvm {
helper.request_token();
request_token();
}
}
}
Expand Down Expand Up @@ -1390,25 +1388,15 @@ fn start_executing_work<B: ExtraBackendMethods>(
// this to spawn a new unit of work, or it may get dropped
// immediately if we have no more work to spawn.
Message::Token(token) => {
match token {
Ok(token) => {
tokens.push(token);

if main_thread_worker_state == MainThreadWorkerState::LLVMing {
// If the main thread token is used for LLVM work
// at the moment, we turn that thread into a regular
// LLVM worker thread, so the main thread is free
// to react to codegen demand.
main_thread_worker_state = MainThreadWorkerState::Idle;
running += 1;
}
}
Err(e) => {
let msg = &format!("failed to acquire jobserver token: {}", e);
shared_emitter.fatal(msg);
// Exit the coordinator thread
panic!("{}", msg)
}
tokens.push(token);

if main_thread_worker_state == MainThreadWorkerState::LLVMing {
// If the main thread token is used for LLVM work
// at the moment, we turn that thread into a regular
// LLVM worker thread, so the main thread is free
// to react to codegen demand.
main_thread_worker_state = MainThreadWorkerState::Idle;
running += 1;
}
}

Expand All @@ -1428,7 +1416,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
work_items.insert(insertion_index, (llvm_work_item, cost));

if !cgcx.opts.debugging_opts.no_parallel_llvm {
helper.request_token();
request_token();
}
assert!(!codegen_aborted);
assert_eq!(main_thread_worker_state, MainThreadWorkerState::Codegenning);
Expand Down
1 change: 0 additions & 1 deletion src/librustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ doctest = false
ena = "0.13.1"
indexmap = "1"
log = "0.4"
jobserver_crate = { version = "0.1.13", package = "jobserver" }
lazy_static = "1"
rustc_serialize = { path = "../libserialize", package = "serialize" }
graphviz = { path = "../libgraphviz" }
Expand Down
42 changes: 0 additions & 42 deletions src/librustc_data_structures/jobserver.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pub mod const_cstr;
pub mod flock;
pub mod fx;
pub mod graph;
pub mod jobserver;
pub mod macros;
pub mod obligation_forest;
pub mod owning_ref;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rustc_codegen_utils = { path = "../librustc_codegen_utils" }
rustc_error_codes = { path = "../librustc_error_codes" }
rustc_interface = { path = "../librustc_interface" }
rustc_serialize = { path = "../libserialize", package = "serialize" }
rustc_jobserver = { path = "../librustc_jobserver" }
syntax = { path = "../libsyntax" }
rustc_span = { path = "../librustc_span" }

Expand Down
1 change: 1 addition & 0 deletions src/librustc_interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ rustc_ast_passes = { path = "../librustc_ast_passes" }
rustc_incremental = { path = "../librustc_incremental" }
rustc_traits = { path = "../librustc_traits" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_jobserver = { path = "../librustc_jobserver" }
rustc_codegen_ssa = { path = "../librustc_codegen_ssa" }
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
rustc_codegen_llvm = { path = "../librustc_codegen_llvm", optional = true }
Expand Down
19 changes: 19 additions & 0 deletions src/librustc_interface/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ pub fn run_compiler_in_existing_thread_pool<R>(

pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
let stderr = config.stderr.take();

if config.opts.debugging_opts.jobserver_token_requests {
if let config::ErrorOutputType::Json { .. } = config.opts.error_format {
if stderr.is_some() {
panic!("Non-default output not supported with -Zjobserver-token-requests");
}
} else {
panic!(
"-Zjobserver-token-requests can only be specified if using \
JSON error output type"
);
}
}

rustc_jobserver::initialize(config.opts.debugging_opts.jobserver_token_requests);

util::spawn_thread_pool(
config.opts.edition,
config.opts.debugging_opts.threads,
Expand All @@ -215,6 +231,9 @@ pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R
}

pub fn default_thread_pool<R: Send>(edition: edition::Edition, f: impl FnOnce() -> R + Send) -> R {
// FIXME: allow for smart jobserver
rustc_jobserver::initialize(false);

// the 1 here is duplicating code in config.opts.debugging_opts.threads
// which also defaults to 1; it ultimately doesn't matter as the default
// isn't threaded, and just ignores this parameter
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_interface/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use rustc::ty;
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
#[cfg(parallel_compiler)]
use rustc_data_structures::jobserver;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::{Lock, Lrc};
use rustc_errors::registry::Registry;
#[cfg(parallel_compiler)]
use rustc_jobserver as jobserver;
use rustc_metadata::dynamic_lib::DynamicLibrary;
use rustc_resolve::{self, Resolver};
use rustc_session as session;
Expand Down
16 changes: 16 additions & 0 deletions src/librustc_jobserver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
authors = ["The Rust Project Developers"]
name = "rustc_jobserver"
version = "0.0.0"
edition = "2018"

[lib]
name = "rustc_jobserver"
path = "lib.rs"
doctest = false

[dependencies]
jobserver = "0.1.21"
log = "0.4"
lazy_static = "1"
rustc_serialize = { path = "../libserialize", package = "serialize" }
Loading