-
Notifications
You must be signed in to change notification settings - Fork 392
Add tracing_chrome under "tracing" feature #4406
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod setup; | ||
mod tracing_chrome; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use std::env::{self, VarError}; | ||
use std::str::FromStr; | ||
use std::sync::{Mutex, OnceLock}; | ||
|
||
use rustc_middle::ty::TyCtxt; | ||
use rustc_session::{CtfeBacktrace, EarlyDiagCtxt}; | ||
|
||
/// The tracing layer from `tracing-chrome` starts a thread in the background that saves data to | ||
/// file and closes the file when stopped. If the thread is not stopped properly, the file will be | ||
/// missing end terminators (`]` for JSON arrays) and other data may also not be flushed. Therefore | ||
/// we need to keep a guard that, when [Drop]ped, will send a signal to stop the thread. Make sure | ||
/// to manually drop this guard using [deinit_loggers], if you are exiting the program with | ||
/// [std::process::exit]! | ||
#[must_use] | ||
struct TracingGuard { | ||
#[cfg(feature = "tracing")] | ||
_chrome: super::tracing_chrome::FlushGuard, | ||
_no_construct: (), | ||
} | ||
|
||
// This ensures TracingGuard is always a drop-type, even when the `_chrome` field is disabled. | ||
impl Drop for TracingGuard { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
fn rustc_logger_config() -> rustc_log::LoggerConfig { | ||
// Start with the usual env vars. | ||
let mut cfg = rustc_log::LoggerConfig::from_env("RUSTC_LOG"); | ||
|
||
// Overwrite if MIRI_LOG is set. | ||
if let Ok(var) = env::var("MIRI_LOG") { | ||
// MIRI_LOG serves as default for RUSTC_LOG, if that is not set. | ||
if matches!(cfg.filter, Err(VarError::NotPresent)) { | ||
// We try to be a bit clever here: if `MIRI_LOG` is just a single level | ||
// used for everything, we only apply it to the parts of rustc that are | ||
// CTFE-related. Otherwise, we use it verbatim for `RUSTC_LOG`. | ||
// This way, if you set `MIRI_LOG=trace`, you get only the right parts of | ||
// rustc traced, but you can also do `MIRI_LOG=miri=trace,rustc_const_eval::interpret=debug`. | ||
if tracing::Level::from_str(&var).is_ok() { | ||
cfg.filter = Ok(format!( | ||
"rustc_middle::mir::interpret={var},rustc_const_eval::interpret={var},miri={var}" | ||
)); | ||
} else { | ||
cfg.filter = Ok(var); | ||
} | ||
} | ||
} | ||
|
||
cfg | ||
} | ||
|
||
/// The global logger can only be set once per process, so track whether that already happened and | ||
/// keep a [TracingGuard] so it can be [Drop]ped later using [deinit_loggers]. | ||
static LOGGER_INITED: OnceLock<Mutex<Option<TracingGuard>>> = OnceLock::new(); | ||
|
||
fn init_logger_once(early_dcx: &EarlyDiagCtxt) { | ||
// If the logger is not yet initialized, initialize it. | ||
LOGGER_INITED.get_or_init(|| { | ||
let guard = if env::var_os("MIRI_TRACING").is_some() { | ||
#[cfg(not(feature = "tracing"))] | ||
{ | ||
crate::fatal_error!( | ||
"fatal error: cannot enable MIRI_TRACING since Miri was not built with the \"tracing\" feature" | ||
); | ||
} | ||
|
||
#[cfg(feature = "tracing")] | ||
{ | ||
let (chrome_layer, chrome_guard) = | ||
super::tracing_chrome::ChromeLayerBuilder::new().include_args(true).build(); | ||
rustc_driver::init_logger_with_additional_layer( | ||
early_dcx, | ||
rustc_logger_config(), | ||
|| { | ||
tracing_subscriber::layer::SubscriberExt::with( | ||
tracing_subscriber::Registry::default(), | ||
chrome_layer, | ||
) | ||
}, | ||
); | ||
|
||
Some(TracingGuard { _chrome: chrome_guard, _no_construct: () }) | ||
} | ||
} else { | ||
// initialize the logger without any tracing enabled | ||
rustc_driver::init_logger(early_dcx, rustc_logger_config()); | ||
None | ||
}; | ||
Mutex::new(guard) | ||
}); | ||
} | ||
|
||
pub fn init_early_loggers(early_dcx: &EarlyDiagCtxt) { | ||
// We only initialize `rustc` if the env var is set (so the user asked for it). | ||
// If it is not set, we avoid initializing now so that we can initialize later with our custom | ||
// settings, and *not* log anything for what happens before `miri` starts interpreting. | ||
if env::var_os("RUSTC_LOG").is_some() { | ||
init_logger_once(early_dcx); | ||
} | ||
} | ||
|
||
pub fn init_late_loggers(early_dcx: &EarlyDiagCtxt, tcx: TyCtxt<'_>) { | ||
// If the logger is not yet initialized, initialize it. | ||
init_logger_once(early_dcx); | ||
|
||
// If `MIRI_BACKTRACE` is set and `RUSTC_CTFE_BACKTRACE` is not, set `RUSTC_CTFE_BACKTRACE`. | ||
// Do this late, so we ideally only apply this to Miri's errors. | ||
if let Some(val) = env::var_os("MIRI_BACKTRACE") { | ||
let ctfe_backtrace = match &*val.to_string_lossy() { | ||
"immediate" => CtfeBacktrace::Immediate, | ||
"0" => CtfeBacktrace::Disabled, | ||
_ => CtfeBacktrace::Capture, | ||
}; | ||
*tcx.sess.ctfe_backtrace.borrow_mut() = ctfe_backtrace; | ||
} | ||
} | ||
|
||
/// Must be called before the program terminates to ensure the trace file is closed correctly. Not | ||
/// doing so will result in invalid trace files. Also see [TracingGuard]. | ||
pub fn deinit_loggers() { | ||
if let Some(guard) = LOGGER_INITED.get() | ||
&& let Ok(mut guard) = guard.lock() | ||
{ | ||
std::mem::drop(guard.take()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder, does https://profiler.firefox.com/ also work? Apparently it can do more than just Firefox traces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it works too, though it visualizes the trace without the tree structure. Should I add it there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it still useful or does one need the tree to deal with the data?
I have no idea what it looks like in Perfetto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how it looks in Perfetto. I feel like the tree view is especially useful to see which spans are nested, which is hard to do in the firefox visualizer without zooming in enough.