Skip to content

Commit 1c61935

Browse files
committed
save profile report to file
1 parent aeec9c4 commit 1c61935

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/bootstrap/src/utils/exec.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
use std::collections::HashMap;
1111
use std::ffi::{OsStr, OsString};
1212
use std::fmt::{Debug, Formatter};
13+
use std::fs::File;
1314
use std::hash::Hash;
15+
use std::io::{BufWriter, Write};
1416
use std::panic::Location;
1517
use std::path::Path;
18+
use std::process;
1619
use std::process::{
1720
Child, ChildStderr, ChildStdout, Command, CommandArgs, CommandEnvs, ExitStatus, Output, Stdio,
1821
};
@@ -100,10 +103,22 @@ impl CommandProfiler {
100103
}
101104

102105
pub fn report_summary(&self) {
106+
let pid = process::id();
107+
let filename = format!("bootstrap-profile-{pid}.txt");
108+
109+
let file = match File::create(&filename) {
110+
Ok(f) => f,
111+
Err(e) => {
112+
eprintln!("Failed to create profiler output file: {e}");
113+
return;
114+
}
115+
};
116+
117+
let mut writer = BufWriter::new(file);
103118
let stats = self.stats.lock().unwrap();
104119

105120
for (key, profile) in stats.iter() {
106-
println!("\nCommand: {:?}", key.program);
121+
writeln!(writer, "Command: {:?}", key.program).unwrap();
107122

108123
let mut hits = 0;
109124
let mut runs = 0;
@@ -113,25 +128,37 @@ impl CommandProfiler {
113128
match trace {
114129
ExecutionTrace::CacheHit { timestamp } => {
115130
hits += 1;
116-
println!(" - Cache hit at: {timestamp:?}");
131+
writeln!(writer, " - Cache hit at: {:?}", timestamp).unwrap();
117132
}
118133
ExecutionTrace::Executed { duration, timestamp } => {
119134
runs += 1;
120135
if max_duration.is_none_or(|d| *duration > d) {
121136
max_duration = Some(*duration);
122137
}
123-
println!(" - Executed at: {timestamp:?}, duration: {duration:.2?}");
138+
writeln!(
139+
writer,
140+
" - Executed at: {:?}, duration: {:.2?}",
141+
timestamp, duration
142+
)
143+
.unwrap();
124144
}
125145
}
126146
}
127147

128148
let duration_str = match max_duration {
129-
Some(d) => format!("{d:.2?}"),
149+
Some(d) => format!("{:.2?}", d),
130150
None => "-".into(),
131151
};
132152

133-
println!("Summary: {runs} run(s), {hits} hit(s), max_duration={duration_str}");
153+
writeln!(
154+
writer,
155+
"Summary: {runs} run(s), {hits} hit(s), max_duration={duration_str}\n"
156+
)
157+
.unwrap();
134158
}
159+
160+
// Print a message to user
161+
println!("Command profiler report saved to {filename}");
135162
}
136163
}
137164

0 commit comments

Comments
 (0)