diff options
| author | Jakub Beránek <berykubik@gmail.com> | 2025-08-11 17:33:47 +0200 |
|---|---|---|
| committer | Jakub Beránek <berykubik@gmail.com> | 2025-08-11 18:21:32 +0200 |
| commit | b4a357fbaaac1f79cb6648e8a0a519e96f4cd0fa (patch) | |
| tree | d0bd6d10baadc751845e6b6670bddad765de5927 /src/bootstrap | |
| parent | c3682b24d792d5348f2c95017a007a619934b521 (diff) | |
| download | rust-b4a357fbaaac1f79cb6648e8a0a519e96f4cd0fa.tar.gz rust-b4a357fbaaac1f79cb6648e8a0a519e96f4cd0fa.zip | |
Always profile commands and generate Chrome profile when tracing is enabled
Diffstat (limited to 'src/bootstrap')
| -rw-r--r-- | src/bootstrap/src/bin/main.rs | 22 | ||||
| -rw-r--r-- | src/bootstrap/src/utils/tracing.rs | 71 |
2 files changed, 37 insertions, 56 deletions
diff --git a/src/bootstrap/src/bin/main.rs b/src/bootstrap/src/bin/main.rs index 0dc3cad0cd8..95bd242baf7 100644 --- a/src/bootstrap/src/bin/main.rs +++ b/src/bootstrap/src/bin/main.rs @@ -16,19 +16,15 @@ use bootstrap::{ find_recent_config_change_ids, human_readable_changes, symlink_dir, t, }; -fn is_profiling_enabled() -> bool { - env::var("BOOTSTRAP_PROFILE").is_ok_and(|v| v == "1") -} - fn is_tracing_enabled() -> bool { - is_profiling_enabled() || cfg!(feature = "tracing") + cfg!(feature = "tracing") } fn main() { #[cfg(feature = "tracing")] - let guard = bootstrap::setup_tracing(is_profiling_enabled()); + let guard = bootstrap::setup_tracing("BOOTSTRAP_TRACING"); - let start_time = Instant::now(); + let _start_time = Instant::now(); let args = env::args().skip(1).collect::<Vec<_>>(); @@ -175,19 +171,11 @@ fn main() { } } - if is_profiling_enabled() { - build.report_summary(&tracing_dir.join("command-stats.txt"), start_time); - } - #[cfg(feature = "tracing")] { + build.report_summary(&tracing_dir.join("command-stats.txt"), _start_time); build.report_step_graph(&tracing_dir); - if let Some(guard) = guard { - guard.copy_to_dir(&tracing_dir); - } - } - - if tracing_enabled { + guard.copy_to_dir(&tracing_dir); eprintln!("Tracing/profiling output has been written to {}", latest_trace_dir.display()); } } diff --git a/src/bootstrap/src/utils/tracing.rs b/src/bootstrap/src/utils/tracing.rs index 18d19738de9..8b9d6ca213c 100644 --- a/src/bootstrap/src/utils/tracing.rs +++ b/src/bootstrap/src/utils/tracing.rs @@ -92,50 +92,43 @@ mod inner { use crate::STEP_SPAN_TARGET; use crate::utils::tracing::COMMAND_SPAN_TARGET; - pub fn setup_tracing(profiling_enabled: bool) -> Option<TracingGuard> { - let filter = EnvFilter::from_env("BOOTSTRAP_TRACING"); + pub fn setup_tracing(env_name: &str) -> TracingGuard { + let filter = EnvFilter::from_env(env_name); let registry = tracing_subscriber::registry().with(filter).with(TracingPrinter::default()); - let guard = if profiling_enabled { - // When we're creating this layer, we do not yet know the location of the tracing output - // directory, because it is stored in the output directory determined after Config is parsed, - // but we already want to make tracing calls during (and before) config parsing. - // So we store the output into a temporary file, and then move it to the tracing directory - // before bootstrap ends. - let tempdir = tempfile::TempDir::new().expect("Cannot create temporary directory"); - let chrome_tracing_path = tempdir.path().join("bootstrap-trace.json"); - let file = std::io::BufWriter::new(File::create(&chrome_tracing_path).unwrap()); - - let chrome_layer = tracing_chrome::ChromeLayerBuilder::new() - .writer(file) - .include_args(true) - .name_fn(Box::new(|event_or_span| match event_or_span { - tracing_chrome::EventOrSpan::Event(e) => e.metadata().name().to_string(), - tracing_chrome::EventOrSpan::Span(s) => { - if s.metadata().target() == STEP_SPAN_TARGET - && let Some(extension) = s.extensions().get::<StepNameExtension>() - { - extension.0.clone() - } else if s.metadata().target() == COMMAND_SPAN_TARGET - && let Some(extension) = s.extensions().get::<CommandNameExtension>() - { - extension.0.clone() - } else { - s.metadata().name().to_string() - } + // When we're creating this layer, we do not yet know the location of the tracing output + // directory, because it is stored in the output directory determined after Config is parsed, + // but we already want to make tracing calls during (and before) config parsing. + // So we store the output into a temporary file, and then move it to the tracing directory + // before bootstrap ends. + let tempdir = tempfile::TempDir::new().expect("Cannot create temporary directory"); + let chrome_tracing_path = tempdir.path().join("bootstrap-trace.json"); + let file = std::io::BufWriter::new(File::create(&chrome_tracing_path).unwrap()); + + let chrome_layer = tracing_chrome::ChromeLayerBuilder::new() + .writer(file) + .include_args(true) + .name_fn(Box::new(|event_or_span| match event_or_span { + tracing_chrome::EventOrSpan::Event(e) => e.metadata().name().to_string(), + tracing_chrome::EventOrSpan::Span(s) => { + if s.metadata().target() == STEP_SPAN_TARGET + && let Some(extension) = s.extensions().get::<StepNameExtension>() + { + extension.0.clone() + } else if s.metadata().target() == COMMAND_SPAN_TARGET + && let Some(extension) = s.extensions().get::<CommandNameExtension>() + { + extension.0.clone() + } else { + s.metadata().name().to_string() } - })); - let (chrome_layer, guard) = chrome_layer.build(); - - tracing::subscriber::set_global_default(registry.with(chrome_layer)).unwrap(); - Some(TracingGuard { guard, _tempdir: tempdir, chrome_tracing_path }) - } else { - tracing::subscriber::set_global_default(registry).unwrap(); - None - }; + } + })); + let (chrome_layer, guard) = chrome_layer.build(); - guard + tracing::subscriber::set_global_default(registry.with(chrome_layer)).unwrap(); + TracingGuard { guard, _tempdir: tempdir, chrome_tracing_path } } pub struct TracingGuard { |
