Skip to content

Commit de0531b

Browse files
committed
Add RUSTC_PROGRESS env var to show the current queries and their arguments
1 parent fb4bca0 commit de0531b

File tree

7 files changed

+131
-7
lines changed

7 files changed

+131
-7
lines changed

Cargo.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -1979,9 +1979,9 @@ dependencies = [
19791979

19801980
[[package]]
19811981
name = "indicatif"
1982-
version = "0.17.6"
1982+
version = "0.17.7"
19831983
source = "registry+https://github.com/rust-lang/crates.io-index"
1984-
checksum = "0b297dc40733f23a0e52728a58fa9489a5b7638a324932de16b41adc3ef80730"
1984+
checksum = "fb28741c9db9a713d93deb3bb9515c20788cef5815265bee4980e87bde7e0f25"
19851985
dependencies = [
19861986
"console",
19871987
"instant",
@@ -4467,6 +4467,7 @@ version = "0.0.0"
44674467
dependencies = [
44684468
"bitflags 2.4.1",
44694469
"getopts",
4470+
"indicatif",
44704471
"libc",
44714472
"rustc_ast",
44724473
"rustc_data_structures",

compiler/rustc_query_impl/src/plumbing.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_query_system::{LayoutOfDepth, QueryOverflow};
2929
use rustc_serialize::Decodable;
3030
use rustc_serialize::Encodable;
3131
use rustc_session::Limit;
32+
use rustc_session::ProgressBars;
3233
use rustc_span::def_id::LOCAL_CRATE;
3334
use std::num::NonZeroU64;
3435
use thin_vec::ThinVec;
@@ -420,6 +421,24 @@ where
420421
value
421422
}
422423

424+
macro_rules! trace_query {
425+
($tcx:expr, $name:ident, $key:ident) => {
426+
#[cfg(debug_assertions)]
427+
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?$key).entered();
428+
let _spinner = $tcx
429+
.sess
430+
.progress_bars
431+
.as_ref()
432+
.map(|bars| $crate::plumbing::update_spinner($tcx, bars, stringify!($name)));
433+
};
434+
}
435+
436+
#[inline(never)]
437+
#[cold]
438+
pub fn update_spinner(tcx: TyCtxt<'_>, bars: &ProgressBars, name: &'static str) -> impl Sized {
439+
tcx.sess.push_spinner(bars, name)
440+
}
441+
423442
fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
424443
where
425444
Q: QueryConfig<QueryCtxt<'tcx>>,
@@ -538,8 +557,7 @@ macro_rules! define_queries {
538557
key: queries::$name::Key<'tcx>,
539558
mode: QueryMode,
540559
) -> Option<Erase<queries::$name::Value<'tcx>>> {
541-
#[cfg(debug_assertions)]
542-
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
560+
trace_query!(tcx, $name, key);
543561
get_query_incr(
544562
QueryType::config(tcx),
545563
QueryCtxt::new(tcx),
@@ -580,8 +598,7 @@ macro_rules! define_queries {
580598
cache_on_disk: |tcx, key| ::rustc_middle::query::cached::$name(tcx, key),
581599
execute_query: |tcx, key| erase(tcx.$name(key)),
582600
compute: |tcx, key| {
583-
#[cfg(debug_assertions)]
584-
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
601+
trace_query!(tcx, $name, key);
585602
__rust_begin_short_backtrace(||
586603
queries::$name::provided_to_erased(
587604
tcx,

compiler/rustc_session/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
# tidy-alphabetical-start
88
bitflags = "2.4.1"
99
getopts = "0.2"
10+
indicatif = "0.17.7"
1011
rustc_ast = { path = "../rustc_ast" }
1112
rustc_data_structures = { path = "../rustc_data_structures" }
1213
rustc_errors = { path = "../rustc_errors" }

compiler/rustc_session/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub mod config;
2626
pub mod cstore;
2727
pub mod filesearch;
2828
mod options;
29+
mod progress;
30+
pub use progress::*;
2931
pub mod search_paths;
3032

3133
mod session;
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::{
2+
cell::RefCell,
3+
sync::mpsc::{Sender, TryRecvError},
4+
thread::ThreadId,
5+
time::Duration,
6+
};
7+
8+
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
9+
use rustc_data_structures::{fx::FxHashMap, sync::IntoDynSyncSend};
10+
11+
use crate::Session;
12+
13+
thread_local! {
14+
static CURRENT_SPINNER: RefCell<Option<(ProgressBar, usize)>> = RefCell::new(None);
15+
}
16+
17+
pub struct ProgressBars {
18+
sender: IntoDynSyncSend<Sender<Msg>>,
19+
}
20+
21+
enum Msg {
22+
Pop { thread: ThreadId },
23+
Push { thread: ThreadId, name: &'static str },
24+
}
25+
26+
impl Session {
27+
/// Starts up a thread that makes sure all the threads' messages are collected and processed
28+
/// in one central location, and thus rendered correctly.
29+
pub(crate) fn init_progress_bars() -> ProgressBars {
30+
let (sender, receiver) = std::sync::mpsc::channel();
31+
std::thread::spawn(move || {
32+
let bars = MultiProgress::new();
33+
let mut threads: FxHashMap<ThreadId, Vec<_>> = FxHashMap::default();
34+
'outer: loop {
35+
std::thread::sleep(Duration::from_millis(100));
36+
loop {
37+
match receiver.try_recv() {
38+
Ok(val) => match val {
39+
Msg::Pop { thread } => {
40+
threads.get_mut(&thread).unwrap().pop();
41+
}
42+
Msg::Push { thread, name } => {
43+
let stack = threads.entry(thread).or_default();
44+
45+
let mut template = String::new();
46+
use std::fmt::Write;
47+
if !stack.is_empty() {
48+
for _ in 1..stack.len() {
49+
write!(template, " ").unwrap();
50+
}
51+
write!(template, "└").unwrap();
52+
}
53+
write!(template, "{{spinner}} {{msg}}").unwrap();
54+
55+
let spinner = ProgressBar::new_spinner()
56+
.with_message(name)
57+
.with_style(ProgressStyle::with_template(&template).unwrap());
58+
let spinner = bars.add(spinner);
59+
stack.push(spinner)
60+
}
61+
},
62+
Err(TryRecvError::Disconnected) => break 'outer,
63+
Err(TryRecvError::Empty) => break,
64+
}
65+
}
66+
for thread in threads.values() {
67+
for spinner in thread {
68+
spinner.tick()
69+
}
70+
}
71+
}
72+
});
73+
ProgressBars { sender: IntoDynSyncSend(sender) }
74+
}
75+
76+
/// Append a new spinner to the current stack
77+
pub fn push_spinner(&self, bars: &ProgressBars, name: &'static str) -> impl Sized {
78+
let thread = std::thread::current().id();
79+
bars.sender.send(Msg::Push { thread, name }).unwrap();
80+
struct Spinner(Sender<Msg>, ThreadId);
81+
impl Drop for Spinner {
82+
fn drop(&mut self) {
83+
self.0.send(Msg::Pop { thread: self.1 }).unwrap();
84+
}
85+
}
86+
Spinner(bars.sender.0.clone(), thread)
87+
}
88+
}

compiler/rustc_session/src/session.rs

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::config::{
77
use crate::config::{ErrorOutputType, Input};
88
use crate::errors;
99
use crate::parse::{add_feature_diagnostics, ParseSess};
10+
use crate::progress::ProgressBars;
1011
use crate::search_paths::{PathKind, SearchPath};
1112
use crate::{filesearch, lint};
1213

@@ -158,6 +159,10 @@ pub struct Session {
158159
/// Data about code being compiled, gathered during compilation.
159160
pub code_stats: CodeStats,
160161

162+
/// The central object where progress information should be displayed in.
163+
/// Is `Some` if `RUSTC_PROGRESS` is set.
164+
pub progress_bars: Option<ProgressBars>,
165+
161166
/// Tracks fuel info if `-zfuel=crate=n` is specified.
162167
optimization_fuel: Lock<OptimizationFuel>,
163168

@@ -1155,6 +1160,11 @@ pub fn build_session(
11551160
let asm_arch =
11561161
if target_cfg.allow_asm { InlineAsmArch::from_str(&target_cfg.arch).ok() } else { None };
11571162

1163+
let progress_bars = match std::env::var_os("RUSTC_PROGRESS") {
1164+
Some(val) if val != "0" => Some(Session::init_progress_bars()),
1165+
_ => None,
1166+
};
1167+
11581168
let sess = Session {
11591169
target: target_cfg,
11601170
host,
@@ -1167,6 +1177,7 @@ pub fn build_session(
11671177
incr_comp_session: RwLock::new(IncrCompSession::NotInitialized),
11681178
prof,
11691179
code_stats: Default::default(),
1180+
progress_bars,
11701181
optimization_fuel,
11711182
print_fuel,
11721183
jobserver: jobserver::client(),

src/tools/tidy/src/deps.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
204204
"cc",
205205
"cfg-if",
206206
"compiler_builtins",
207+
"console", // Dependency of indicatif
207208
"convert_case", // dependency of derive_more
208209
"cpufeatures",
209210
"crc32fast",
@@ -226,6 +227,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
226227
"either",
227228
"elsa",
228229
"ena",
230+
"encode_unicode", // Dependency of indicatif
229231
"equivalent",
230232
"errno",
231233
"expect-test",
@@ -255,6 +257,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
255257
"icu_provider_macros",
256258
"ident_case",
257259
"indexmap",
260+
"indicatif",
258261
"intl-memoizer",
259262
"intl_pluralrules",
260263
"is-terminal",
@@ -278,6 +281,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
278281
"miniz_oxide",
279282
"nu-ansi-term",
280283
"num_cpus",
284+
"number_prefix", // Dependency of indicatif
281285
"object",
282286
"odht",
283287
"once_cell",
@@ -288,7 +292,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
288292
"perf-event-open-sys",
289293
"pin-project-lite",
290294
"polonius-engine",
291-
"portable-atomic", // dependency for platforms doesn't support `AtomicU64` in std
295+
"portable-atomic", // dependency for platforms doesn't support `AtomicU64` in std, and indicatif
292296
"ppv-lite86",
293297
"proc-macro-hack",
294298
"proc-macro2",

0 commit comments

Comments
 (0)