Skip to content

Commit 4339816

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

File tree

7 files changed

+197
-8
lines changed

7 files changed

+197
-8
lines changed

Cargo.lock

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,19 @@ dependencies = [
675675
"windows",
676676
]
677677

678+
[[package]]
679+
name = "console"
680+
version = "0.15.7"
681+
source = "registry+https://github.com/rust-lang/crates.io-index"
682+
checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8"
683+
dependencies = [
684+
"encode_unicode",
685+
"lazy_static",
686+
"libc",
687+
"unicode-width",
688+
"windows-sys 0.45.0",
689+
]
690+
678691
[[package]]
679692
name = "convert_case"
680693
version = "0.4.0"
@@ -999,6 +1012,12 @@ dependencies = [
9991012
"log",
10001013
]
10011014

1015+
[[package]]
1016+
name = "encode_unicode"
1017+
version = "0.3.6"
1018+
source = "registry+https://github.com/rust-lang/crates.io-index"
1019+
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
1020+
10021021
[[package]]
10031022
name = "encoding_rs"
10041023
version = "0.8.32"
@@ -1801,6 +1820,19 @@ dependencies = [
18011820
"serde",
18021821
]
18031822

1823+
[[package]]
1824+
name = "indicatif"
1825+
version = "0.17.5"
1826+
source = "registry+https://github.com/rust-lang/crates.io-index"
1827+
checksum = "8ff8cc23a7393a397ed1d7f56e6365cba772aba9f9912ab968b03043c395d057"
1828+
dependencies = [
1829+
"console",
1830+
"instant",
1831+
"number_prefix",
1832+
"portable-atomic",
1833+
"unicode-width",
1834+
]
1835+
18041836
[[package]]
18051837
name = "indoc"
18061838
version = "1.0.9"
@@ -2391,6 +2423,12 @@ dependencies = [
23912423
"libc",
23922424
]
23932425

2426+
[[package]]
2427+
name = "number_prefix"
2428+
version = "0.4.0"
2429+
source = "registry+https://github.com/rust-lang/crates.io-index"
2430+
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
2431+
23942432
[[package]]
23952433
name = "object"
23962434
version = "0.31.1"
@@ -2589,7 +2627,7 @@ dependencies = [
25892627
"libc",
25902628
"redox_syscall 0.3.5",
25912629
"smallvec",
2592-
"windows-targets",
2630+
"windows-targets 0.48.1",
25932631
]
25942632

25952633
[[package]]
@@ -2724,6 +2762,12 @@ dependencies = [
27242762
"rustc-hash",
27252763
]
27262764

2765+
[[package]]
2766+
name = "portable-atomic"
2767+
version = "1.4.1"
2768+
source = "registry+https://github.com/rust-lang/crates.io-index"
2769+
checksum = "edc55135a600d700580e406b4de0d59cb9ad25e344a3a091a97ded2622ec4ec6"
2770+
27272771
[[package]]
27282772
name = "ppv-lite86"
27292773
version = "0.2.17"
@@ -4200,6 +4244,7 @@ dependencies = [
42004244
"atty",
42014245
"bitflags 1.3.2",
42024246
"getopts",
4247+
"indicatif",
42034248
"libc",
42044249
"rustc_ast",
42054250
"rustc_data_structures",
@@ -5833,7 +5878,7 @@ version = "0.48.0"
58335878
source = "registry+https://github.com/rust-lang/crates.io-index"
58345879
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
58355880
dependencies = [
5836-
"windows-targets",
5881+
"windows-targets 0.48.1",
58375882
]
58385883

58395884
[[package]]
@@ -5867,13 +5912,37 @@ dependencies = [
58675912
"windows_x86_64_msvc 0.42.2",
58685913
]
58695914

5915+
[[package]]
5916+
name = "windows-sys"
5917+
version = "0.45.0"
5918+
source = "registry+https://github.com/rust-lang/crates.io-index"
5919+
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
5920+
dependencies = [
5921+
"windows-targets 0.42.2",
5922+
]
5923+
58705924
[[package]]
58715925
name = "windows-sys"
58725926
version = "0.48.0"
58735927
source = "registry+https://github.com/rust-lang/crates.io-index"
58745928
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
58755929
dependencies = [
5876-
"windows-targets",
5930+
"windows-targets 0.48.1",
5931+
]
5932+
5933+
[[package]]
5934+
name = "windows-targets"
5935+
version = "0.42.2"
5936+
source = "registry+https://github.com/rust-lang/crates.io-index"
5937+
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
5938+
dependencies = [
5939+
"windows_aarch64_gnullvm 0.42.2",
5940+
"windows_aarch64_msvc 0.42.2",
5941+
"windows_i686_gnu 0.42.2",
5942+
"windows_i686_msvc 0.42.2",
5943+
"windows_x86_64_gnu 0.42.2",
5944+
"windows_x86_64_gnullvm 0.42.2",
5945+
"windows_x86_64_msvc 0.42.2",
58775946
]
58785947

58795948
[[package]]

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_query_system::{LayoutOfDepth, QueryOverflow};
2727
use rustc_serialize::Decodable;
2828
use rustc_serialize::Encodable;
2929
use rustc_session::Limit;
30+
use rustc_session::ProgressBars;
3031
use rustc_span::def_id::LOCAL_CRATE;
3132
use std::num::NonZeroU64;
3233
use thin_vec::ThinVec;
@@ -413,6 +414,24 @@ where
413414
value
414415
}
415416

417+
macro_rules! trace_query {
418+
($tcx:expr, $name:ident, $key:ident) => {
419+
#[cfg(debug_assertions)]
420+
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?$key).entered();
421+
let _spinner = $tcx
422+
.sess
423+
.progress_bars
424+
.as_ref()
425+
.map(|bars| $crate::plumbing::update_spinner($tcx, bars, stringify!($name)));
426+
};
427+
}
428+
429+
#[inline(never)]
430+
#[cold]
431+
pub fn update_spinner(tcx: TyCtxt<'_>, bars: &ProgressBars, name: &'static str) -> impl Sized {
432+
tcx.sess.push_spinner(bars, name)
433+
}
434+
416435
fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
417436
where
418437
Q: QueryConfig<QueryCtxt<'tcx>>,
@@ -437,7 +456,7 @@ where
437456

438457
if let Some(key) = Q::Key::recover(tcx, &dep_node) {
439458
#[cfg(debug_assertions)]
440-
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
459+
let _guard = tracing::span!(tracing::Level::TRACE, "force", ?dep_node.kind, ?key).entered();
441460
force_query(query, QueryCtxt::new(tcx), key, dep_node);
442461
true
443462
} else {
@@ -531,8 +550,7 @@ macro_rules! define_queries {
531550
key: queries::$name::Key<'tcx>,
532551
mode: QueryMode,
533552
) -> Option<Erase<queries::$name::Value<'tcx>>> {
534-
#[cfg(debug_assertions)]
535-
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
553+
trace_query!(tcx, $name, key);
536554
get_query_incr(
537555
QueryType::config(tcx),
538556
QueryCtxt::new(tcx),
@@ -573,8 +591,7 @@ macro_rules! define_queries {
573591
cache_on_disk: |tcx, key| ::rustc_middle::query::cached::$name(tcx, key),
574592
execute_query: |tcx, key| erase(tcx.$name(key)),
575593
compute: |tcx, key| {
576-
#[cfg(debug_assertions)]
577-
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
594+
trace_query!(tcx, $name, key);
578595
__rust_begin_short_backtrace(||
579596
queries::$name::provided_to_erased(
580597
tcx,

compiler/rustc_session/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rustc_ast = { path = "../rustc_ast" }
2222
rustc_lint_defs = { path = "../rustc_lint_defs" }
2323
smallvec = "1.8.1"
2424
termize = "0.1.1"
25+
indicatif = "0.17.5"
2526

2627
[target.'cfg(unix)'.dependencies]
2728
libc = "0.2"

compiler/rustc_session/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![feature(let_chains)]
33
#![feature(min_specialization)]
44
#![feature(never_type)]
5+
#![feature(local_key_cell_methods)]
56
#![feature(lazy_cell)]
67
#![feature(option_get_or_insert_default)]
78
#![feature(rustc_attrs)]
@@ -33,6 +34,8 @@ pub mod config;
3334
pub mod cstore;
3435
pub mod filesearch;
3536
mod options;
37+
mod progress;
38+
pub use progress::*;
3639
pub mod search_paths;
3740

3841
mod session;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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;
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: 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 }
74+
}
75+
pub fn push_spinner(&self, bars: &ProgressBars, name: &'static str) -> impl Sized {
76+
let thread = std::thread::current().id();
77+
bars.sender.send(Msg::Push { thread, name }).unwrap();
78+
struct Spinner(Sender<Msg>, ThreadId);
79+
impl Drop for Spinner {
80+
fn drop(&mut self) {
81+
self.0.send(Msg::Pop { thread: self.1 }).unwrap();
82+
}
83+
}
84+
Spinner(bars.sender.clone(), thread)
85+
}
86+
}

compiler/rustc_session/src/session.rs

Lines changed: 8 additions & 0 deletions
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

@@ -176,6 +177,10 @@ pub struct Session {
176177
/// Data about code being compiled, gathered during compilation.
177178
pub code_stats: CodeStats,
178179

180+
/// The central object where progress information should be displayed in.
181+
/// Is `Some` if `RUSTC_PROGRESS` is set.
182+
pub progress_bars: Option<ProgressBars>,
183+
179184
/// Tracks fuel info if `-zfuel=crate=n` is specified.
180185
optimization_fuel: Lock<OptimizationFuel>,
181186

@@ -1506,6 +1511,8 @@ pub fn build_session(
15061511
let asm_arch =
15071512
if target_cfg.allow_asm { InlineAsmArch::from_str(&target_cfg.arch).ok() } else { None };
15081513

1514+
let progress_bars = std::env::var_os("RUSTC_PROGRESS").map(|_| Session::init_progress_bars());
1515+
15091516
let sess = Session {
15101517
target: target_cfg,
15111518
host,
@@ -1528,6 +1535,7 @@ pub fn build_session(
15281535
normalize_projection_ty: AtomicUsize::new(0),
15291536
},
15301537
code_stats: Default::default(),
1538+
progress_bars,
15311539
optimization_fuel,
15321540
print_fuel,
15331541
jobserver: jobserver::client(),

src/tools/tidy/src/deps.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
128128
"cc",
129129
"cfg-if",
130130
"compiler_builtins",
131+
"console", // Dependency of indicatif
131132
"convert_case", // dependency of derive_more
132133
"cpufeatures",
133134
"crc32fast",
@@ -146,6 +147,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
146147
"either",
147148
"elsa",
148149
"ena",
150+
"encode_unicode", // Dependency of indicatif
149151
"equivalent",
150152
"errno",
151153
"errno-dragonfly",
@@ -171,6 +173,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
171173
"icu_provider_adapters",
172174
"icu_provider_macros",
173175
"indexmap",
176+
"indicatif",
174177
"instant",
175178
"intl-memoizer",
176179
"intl_pluralrules",
@@ -194,6 +197,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
194197
"miniz_oxide",
195198
"nu-ansi-term",
196199
"num_cpus",
200+
"number_prefix", // Dependency of indicatif
197201
"object",
198202
"odht",
199203
"once_cell",
@@ -204,6 +208,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
204208
"perf-event-open-sys",
205209
"pin-project-lite",
206210
"polonius-engine",
211+
"portable-atomic", // Dependency of indicatif
207212
"ppv-lite86",
208213
"proc-macro-hack",
209214
"proc-macro2",

0 commit comments

Comments
 (0)