Skip to content

Commit b4a357f

Browse files
committed
Always profile commands and generate Chrome profile when tracing is enabled
1 parent c3682b2 commit b4a357f

File tree

2 files changed

+37
-56
lines changed

2 files changed

+37
-56
lines changed

src/bootstrap/src/bin/main.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,15 @@ use bootstrap::{
1616
find_recent_config_change_ids, human_readable_changes, symlink_dir, t,
1717
};
1818

19-
fn is_profiling_enabled() -> bool {
20-
env::var("BOOTSTRAP_PROFILE").is_ok_and(|v| v == "1")
21-
}
22-
2319
fn is_tracing_enabled() -> bool {
24-
is_profiling_enabled() || cfg!(feature = "tracing")
20+
cfg!(feature = "tracing")
2521
}
2622

2723
fn main() {
2824
#[cfg(feature = "tracing")]
29-
let guard = bootstrap::setup_tracing(is_profiling_enabled());
25+
let guard = bootstrap::setup_tracing("BOOTSTRAP_TRACING");
3026

31-
let start_time = Instant::now();
27+
let _start_time = Instant::now();
3228

3329
let args = env::args().skip(1).collect::<Vec<_>>();
3430

@@ -175,19 +171,11 @@ fn main() {
175171
}
176172
}
177173

178-
if is_profiling_enabled() {
179-
build.report_summary(&tracing_dir.join("command-stats.txt"), start_time);
180-
}
181-
182174
#[cfg(feature = "tracing")]
183175
{
176+
build.report_summary(&tracing_dir.join("command-stats.txt"), _start_time);
184177
build.report_step_graph(&tracing_dir);
185-
if let Some(guard) = guard {
186-
guard.copy_to_dir(&tracing_dir);
187-
}
188-
}
189-
190-
if tracing_enabled {
178+
guard.copy_to_dir(&tracing_dir);
191179
eprintln!("Tracing/profiling output has been written to {}", latest_trace_dir.display());
192180
}
193181
}

src/bootstrap/src/utils/tracing.rs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -92,50 +92,43 @@ mod inner {
9292
use crate::STEP_SPAN_TARGET;
9393
use crate::utils::tracing::COMMAND_SPAN_TARGET;
9494

95-
pub fn setup_tracing(profiling_enabled: bool) -> Option<TracingGuard> {
96-
let filter = EnvFilter::from_env("BOOTSTRAP_TRACING");
95+
pub fn setup_tracing(env_name: &str) -> TracingGuard {
96+
let filter = EnvFilter::from_env(env_name);
9797

9898
let registry = tracing_subscriber::registry().with(filter).with(TracingPrinter::default());
9999

100-
let guard = if profiling_enabled {
101-
// When we're creating this layer, we do not yet know the location of the tracing output
102-
// directory, because it is stored in the output directory determined after Config is parsed,
103-
// but we already want to make tracing calls during (and before) config parsing.
104-
// So we store the output into a temporary file, and then move it to the tracing directory
105-
// before bootstrap ends.
106-
let tempdir = tempfile::TempDir::new().expect("Cannot create temporary directory");
107-
let chrome_tracing_path = tempdir.path().join("bootstrap-trace.json");
108-
let file = std::io::BufWriter::new(File::create(&chrome_tracing_path).unwrap());
109-
110-
let chrome_layer = tracing_chrome::ChromeLayerBuilder::new()
111-
.writer(file)
112-
.include_args(true)
113-
.name_fn(Box::new(|event_or_span| match event_or_span {
114-
tracing_chrome::EventOrSpan::Event(e) => e.metadata().name().to_string(),
115-
tracing_chrome::EventOrSpan::Span(s) => {
116-
if s.metadata().target() == STEP_SPAN_TARGET
117-
&& let Some(extension) = s.extensions().get::<StepNameExtension>()
118-
{
119-
extension.0.clone()
120-
} else if s.metadata().target() == COMMAND_SPAN_TARGET
121-
&& let Some(extension) = s.extensions().get::<CommandNameExtension>()
122-
{
123-
extension.0.clone()
124-
} else {
125-
s.metadata().name().to_string()
126-
}
100+
// When we're creating this layer, we do not yet know the location of the tracing output
101+
// directory, because it is stored in the output directory determined after Config is parsed,
102+
// but we already want to make tracing calls during (and before) config parsing.
103+
// So we store the output into a temporary file, and then move it to the tracing directory
104+
// before bootstrap ends.
105+
let tempdir = tempfile::TempDir::new().expect("Cannot create temporary directory");
106+
let chrome_tracing_path = tempdir.path().join("bootstrap-trace.json");
107+
let file = std::io::BufWriter::new(File::create(&chrome_tracing_path).unwrap());
108+
109+
let chrome_layer = tracing_chrome::ChromeLayerBuilder::new()
110+
.writer(file)
111+
.include_args(true)
112+
.name_fn(Box::new(|event_or_span| match event_or_span {
113+
tracing_chrome::EventOrSpan::Event(e) => e.metadata().name().to_string(),
114+
tracing_chrome::EventOrSpan::Span(s) => {
115+
if s.metadata().target() == STEP_SPAN_TARGET
116+
&& let Some(extension) = s.extensions().get::<StepNameExtension>()
117+
{
118+
extension.0.clone()
119+
} else if s.metadata().target() == COMMAND_SPAN_TARGET
120+
&& let Some(extension) = s.extensions().get::<CommandNameExtension>()
121+
{
122+
extension.0.clone()
123+
} else {
124+
s.metadata().name().to_string()
127125
}
128-
}));
129-
let (chrome_layer, guard) = chrome_layer.build();
130-
131-
tracing::subscriber::set_global_default(registry.with(chrome_layer)).unwrap();
132-
Some(TracingGuard { guard, _tempdir: tempdir, chrome_tracing_path })
133-
} else {
134-
tracing::subscriber::set_global_default(registry).unwrap();
135-
None
136-
};
126+
}
127+
}));
128+
let (chrome_layer, guard) = chrome_layer.build();
137129

138-
guard
130+
tracing::subscriber::set_global_default(registry.with(chrome_layer)).unwrap();
131+
TracingGuard { guard, _tempdir: tempdir, chrome_tracing_path }
139132
}
140133

141134
pub struct TracingGuard {

0 commit comments

Comments
 (0)