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

Add RUSTC_PROGRESS env var to show the current queries and their arguments #113888

Closed
wants to merge 1 commit into from
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
5 changes: 3 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1979,9 +1979,9 @@ dependencies = [

[[package]]
name = "indicatif"
version = "0.17.6"
version = "0.17.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b297dc40733f23a0e52728a58fa9489a5b7638a324932de16b41adc3ef80730"
checksum = "fb28741c9db9a713d93deb3bb9515c20788cef5815265bee4980e87bde7e0f25"
dependencies = [
"console",
"instant",
Expand Down Expand Up @@ -4467,6 +4467,7 @@ version = "0.0.0"
dependencies = [
"bitflags 2.4.1",
"getopts",
"indicatif",
"libc",
"rustc_ast",
"rustc_data_structures",
Expand Down
25 changes: 21 additions & 4 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use rustc_query_system::{LayoutOfDepth, QueryOverflow};
use rustc_serialize::Decodable;
use rustc_serialize::Encodable;
use rustc_session::Limit;
use rustc_session::ProgressBars;
use rustc_span::def_id::LOCAL_CRATE;
use std::num::NonZeroU64;
use thin_vec::ThinVec;
Expand Down Expand Up @@ -420,6 +421,24 @@ where
value
}

macro_rules! trace_query {
($tcx:expr, $name:ident, $key:ident) => {
#[cfg(debug_assertions)]
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?$key).entered();
let _spinner = $tcx
.sess
.progress_bars
.as_ref()
.map(|bars| $crate::plumbing::update_spinner($tcx, bars, stringify!($name)));
};
}

#[inline(never)]
#[cold]
pub fn update_spinner(tcx: TyCtxt<'_>, bars: &ProgressBars, name: &'static str) -> impl Sized {
tcx.sess.push_spinner(bars, name)
}

fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
where
Q: QueryConfig<QueryCtxt<'tcx>>,
Expand Down Expand Up @@ -538,8 +557,7 @@ macro_rules! define_queries {
key: queries::$name::Key<'tcx>,
mode: QueryMode,
) -> Option<Erase<queries::$name::Value<'tcx>>> {
#[cfg(debug_assertions)]
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
trace_query!(tcx, $name, key);
get_query_incr(
QueryType::config(tcx),
QueryCtxt::new(tcx),
Expand Down Expand Up @@ -580,8 +598,7 @@ macro_rules! define_queries {
cache_on_disk: |tcx, key| ::rustc_middle::query::cached::$name(tcx, key),
execute_query: |tcx, key| erase(tcx.$name(key)),
compute: |tcx, key| {
#[cfg(debug_assertions)]
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
trace_query!(tcx, $name, key);
__rust_begin_short_backtrace(||
queries::$name::provided_to_erased(
tcx,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
# tidy-alphabetical-start
bitflags = "2.4.1"
getopts = "0.2"
indicatif = "0.17.7"
rustc_ast = { path = "../rustc_ast" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub mod config;
pub mod cstore;
pub mod filesearch;
mod options;
mod progress;
pub use progress::*;
pub mod search_paths;

mod session;
Expand Down
88 changes: 88 additions & 0 deletions compiler/rustc_session/src/progress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::{
cell::RefCell,
sync::mpsc::{Sender, TryRecvError},
thread::ThreadId,
time::Duration,
};

use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use rustc_data_structures::{fx::FxHashMap, sync::IntoDynSyncSend};

use crate::Session;

thread_local! {
static CURRENT_SPINNER: RefCell<Option<(ProgressBar, usize)>> = RefCell::new(None);
}

pub struct ProgressBars {
sender: IntoDynSyncSend<Sender<Msg>>,
}

enum Msg {
Pop { thread: ThreadId },
Push { thread: ThreadId, name: &'static str },
}

impl Session {
/// Starts up a thread that makes sure all the threads' messages are collected and processed
/// in one central location, and thus rendered correctly.
pub(crate) fn init_progress_bars() -> ProgressBars {
let (sender, receiver) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let bars = MultiProgress::new();
let mut threads: FxHashMap<ThreadId, Vec<_>> = FxHashMap::default();
'outer: loop {
std::thread::sleep(Duration::from_millis(100));
loop {
match receiver.try_recv() {
Ok(val) => match val {
Msg::Pop { thread } => {
threads.get_mut(&thread).unwrap().pop();
}
Msg::Push { thread, name } => {
let stack = threads.entry(thread).or_default();

let mut template = String::new();
use std::fmt::Write;
if !stack.is_empty() {
for _ in 1..stack.len() {
write!(template, " ").unwrap();
}
write!(template, "└").unwrap();
}
write!(template, "{{spinner}} {{msg}}").unwrap();

let spinner = ProgressBar::new_spinner()
.with_message(name)
.with_style(ProgressStyle::with_template(&template).unwrap());
let spinner = bars.add(spinner);
stack.push(spinner)
}
},
Err(TryRecvError::Disconnected) => break 'outer,
Err(TryRecvError::Empty) => break,
}
}
for thread in threads.values() {
for spinner in thread {
spinner.tick()
}
}
}
});
ProgressBars { sender: IntoDynSyncSend(sender) }
}

/// Append a new spinner to the current stack
pub fn push_spinner(&self, bars: &ProgressBars, name: &'static str) -> impl Sized {
let thread = std::thread::current().id();
bars.sender.send(Msg::Push { thread, name }).unwrap();
struct Spinner(Sender<Msg>, ThreadId);
impl Drop for Spinner {
fn drop(&mut self) {
self.0.send(Msg::Pop { thread: self.1 }).unwrap();
}
}
Spinner(bars.sender.0.clone(), thread)
}
}
11 changes: 11 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::config::{
use crate::config::{ErrorOutputType, Input};
use crate::errors;
use crate::parse::{add_feature_diagnostics, ParseSess};
use crate::progress::ProgressBars;
use crate::search_paths::{PathKind, SearchPath};
use crate::{filesearch, lint};

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

/// The central object where progress information should be displayed in.
/// Is `Some` if `RUSTC_PROGRESS` is set.
pub progress_bars: Option<ProgressBars>,

/// Tracks fuel info if `-zfuel=crate=n` is specified.
optimization_fuel: Lock<OptimizationFuel>,

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

let progress_bars = match std::env::var_os("RUSTC_PROGRESS") {
Some(val) if val != "0" => Some(Session::init_progress_bars()),
_ => None,
};

let sess = Session {
target: target_cfg,
host,
Expand All @@ -1167,6 +1177,7 @@ pub fn build_session(
incr_comp_session: RwLock::new(IncrCompSession::NotInitialized),
prof,
code_stats: Default::default(),
progress_bars,
optimization_fuel,
print_fuel,
jobserver: jobserver::client(),
Expand Down
6 changes: 5 additions & 1 deletion src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"cc",
"cfg-if",
"compiler_builtins",
"console", // Dependency of indicatif
"convert_case", // dependency of derive_more
"cpufeatures",
"crc32fast",
Expand All @@ -226,6 +227,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"either",
"elsa",
"ena",
"encode_unicode", // Dependency of indicatif
"equivalent",
"errno",
"expect-test",
Expand Down Expand Up @@ -255,6 +257,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"icu_provider_macros",
"ident_case",
"indexmap",
"indicatif",
"intl-memoizer",
"intl_pluralrules",
"is-terminal",
Expand All @@ -278,6 +281,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"miniz_oxide",
"nu-ansi-term",
"num_cpus",
"number_prefix", // Dependency of indicatif
"object",
"odht",
"once_cell",
Expand All @@ -288,7 +292,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"perf-event-open-sys",
"pin-project-lite",
"polonius-engine",
"portable-atomic", // dependency for platforms doesn't support `AtomicU64` in std
"portable-atomic", // dependency for platforms doesn't support `AtomicU64` in std, and indicatif
"ppv-lite86",
"proc-macro-hack",
"proc-macro2",
Expand Down
Loading