Skip to content

Commit e6dddcb

Browse files
committed
Store bootstrap tracing outputs to a unified directory
1 parent 5771665 commit e6dddcb

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

src/bootstrap/src/bin/main.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ use bootstrap::{
1818
#[cfg(feature = "tracing")]
1919
use tracing::instrument;
2020

21-
fn is_bootstrap_profiling_enabled() -> bool {
21+
fn is_profiling_enabled() -> bool {
2222
env::var("BOOTSTRAP_PROFILE").is_ok_and(|v| v == "1")
2323
}
2424

25+
fn is_tracing_enabled() -> bool {
26+
is_profiling_enabled() || cfg!(feature = "tracing")
27+
}
28+
2529
#[cfg_attr(feature = "tracing", instrument(level = "trace", name = "main"))]
2630
fn main() {
2731
#[cfg(feature = "tracing")]
@@ -102,6 +106,13 @@ fn main() {
102106
let dump_bootstrap_shims = config.dump_bootstrap_shims;
103107
let out_dir = config.out.clone();
104108

109+
let tracing_enabled = is_tracing_enabled();
110+
let tracing_dir = out_dir.join("bootstrap-trace").join(std::process::id().to_string());
111+
if tracing_enabled {
112+
let _ = std::fs::remove_dir_all(&tracing_dir);
113+
std::fs::create_dir_all(&tracing_dir).unwrap();
114+
}
115+
105116
debug!("creating new build based on config");
106117
let mut build = Build::new(config);
107118
build.build();
@@ -156,12 +167,16 @@ fn main() {
156167
}
157168
}
158169

159-
if is_bootstrap_profiling_enabled() {
160-
build.report_summary(start_time);
170+
if is_profiling_enabled() {
171+
build.report_summary(&tracing_dir.join("command-stats.txt"), start_time);
161172
}
162173

163174
#[cfg(feature = "tracing")]
164-
build.report_step_graph();
175+
build.report_step_graph(&tracing_dir);
176+
177+
if tracing_enabled {
178+
eprintln!("Tracing/profiling output has been written to {}", tracing_dir.display());
179+
}
165180
}
166181

167182
fn check_version(config: &Config) -> Option<String> {
@@ -241,7 +256,7 @@ fn setup_tracing() -> impl Drop {
241256
let mut chrome_layer = tracing_chrome::ChromeLayerBuilder::new().include_args(true);
242257

243258
// Writes the Chrome profile to trace-<unix-timestamp>.json if enabled
244-
if !is_bootstrap_profiling_enabled() {
259+
if !is_profiling_enabled() {
245260
chrome_layer = chrome_layer.writer(io::sink());
246261
}
247262

src/bootstrap/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,13 +2016,13 @@ to download LLVM rather than building it.
20162016
&self.config.exec_ctx
20172017
}
20182018

2019-
pub fn report_summary(&self, start_time: Instant) {
2020-
self.config.exec_ctx.profiler().report_summary(start_time);
2019+
pub fn report_summary(&self, path: &Path, start_time: Instant) {
2020+
self.config.exec_ctx.profiler().report_summary(path, start_time);
20212021
}
20222022

20232023
#[cfg(feature = "tracing")]
2024-
pub fn report_step_graph(self) {
2025-
self.step_graph.into_inner().store_to_dot_files();
2024+
pub fn report_step_graph(self, directory: &Path) {
2025+
self.step_graph.into_inner().store_to_dot_files(directory);
20262026
}
20272027
}
20282028

src/bootstrap/src/utils/exec.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::hash::Hash;
1515
use std::io::{BufWriter, Write};
1616
use std::panic::Location;
1717
use std::path::Path;
18-
use std::process;
1918
use std::process::{
2019
Child, ChildStderr, ChildStdout, Command, CommandArgs, CommandEnvs, ExitStatus, Output, Stdio,
2120
};
@@ -26,10 +25,10 @@ use build_helper::ci::CiEnv;
2625
use build_helper::drop_bomb::DropBomb;
2726
use build_helper::exit;
2827

29-
use crate::PathBuf;
3028
use crate::core::config::DryRun;
3129
#[cfg(feature = "tracing")]
3230
use crate::trace_cmd;
31+
use crate::{PathBuf, t};
3332

3433
/// What should be done when the command fails.
3534
#[derive(Debug, Copy, Clone)]
@@ -121,17 +120,9 @@ impl CommandProfiler {
121120
entry.traces.push(ExecutionTrace::CacheHit);
122121
}
123122

124-
pub fn report_summary(&self, start_time: Instant) {
125-
let pid = process::id();
126-
let filename = format!("bootstrap-profile-{pid}.txt");
127-
128-
let file = match File::create(&filename) {
129-
Ok(f) => f,
130-
Err(e) => {
131-
eprintln!("Failed to create profiler output file: {e}");
132-
return;
133-
}
134-
};
123+
/// Report summary of executed commands file at the specified `path`.
124+
pub fn report_summary(&self, path: &Path, start_time: Instant) {
125+
let file = t!(File::create(path));
135126

136127
let mut writer = BufWriter::new(file);
137128
let stats = self.stats.lock().unwrap();
@@ -221,8 +212,6 @@ impl CommandProfiler {
221212
writeln!(writer, "Total cache hits: {total_cache_hits}").unwrap();
222213
writeln!(writer, "Estimated time saved due to cache hits: {total_saved_duration:.2?}")
223214
.unwrap();
224-
225-
println!("Command profiler report saved to {filename}");
226215
}
227216
}
228217

src/bootstrap/src/utils/step_graph.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::collections::{HashMap, HashSet};
22
use std::fmt::Debug;
33
use std::io::BufWriter;
4+
use std::path::Path;
45

56
use crate::core::builder::{AnyDebug, Step};
7+
use crate::t;
68

79
/// Records the executed steps and their dependencies in a directed graph,
810
/// which can then be rendered into a DOT file for visualization.
@@ -80,10 +82,10 @@ impl StepGraph {
8082
}
8183
}
8284

83-
pub fn store_to_dot_files(self) {
85+
pub fn store_to_dot_files(self, directory: &Path) {
8486
for (key, graph) in self.graphs.into_iter() {
85-
let filename = format!("bootstrap-steps{key}.dot");
86-
graph.render(&filename).unwrap();
87+
let filename = directory.join(format!("step-graph{key}.dot"));
88+
t!(graph.render(&filename));
8789
}
8890
}
8991
}
@@ -147,7 +149,7 @@ impl DotGraph {
147149
self.key_to_index.get(key).copied()
148150
}
149151

150-
fn render(&self, path: &str) -> std::io::Result<()> {
152+
fn render(&self, path: &Path) -> std::io::Result<()> {
151153
use std::io::Write;
152154

153155
let mut file = BufWriter::new(std::fs::File::create(path)?);

0 commit comments

Comments
 (0)