Skip to content

Commit 010f2eb

Browse files
committed
improve summary report, remove timestamp
1 parent 157beeb commit 010f2eb

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

src/bootstrap/src/bin/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use std::fs::{self, OpenOptions};
99
use std::io::{self, BufRead, BufReader, IsTerminal, Write};
1010
use std::str::FromStr;
11+
use std::time::Instant;
1112
use std::{env, process};
1213

1314
use bootstrap::{
@@ -22,6 +23,8 @@ fn main() {
2223
#[cfg(feature = "tracing")]
2324
let _guard = setup_tracing();
2425

26+
let start_time = Instant::now();
27+
2528
let args = env::args().skip(1).collect::<Vec<_>>();
2629

2730
if Flags::try_parse_verbose_help(&args) {
@@ -150,7 +153,7 @@ fn main() {
150153
}
151154

152155
if env::var("BOOTSTRAP_PROFILE").is_ok_and(|v| v == "1") {
153-
build.report_summary();
156+
build.report_summary(start_time);
154157
}
155158
}
156159

src/bootstrap/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::collections::{BTreeSet, HashMap, HashSet};
2222
use std::fmt::Display;
2323
use std::path::{Path, PathBuf};
2424
use std::sync::OnceLock;
25-
use std::time::SystemTime;
25+
use std::time::{Instant, SystemTime};
2626
use std::{env, fs, io, str};
2727

2828
use build_helper::ci::gha;
@@ -1929,8 +1929,8 @@ to download LLVM rather than building it.
19291929
&self.config.exec_ctx
19301930
}
19311931

1932-
pub fn report_summary(&self) {
1933-
self.config.exec_ctx.profiler().report_summary();
1932+
pub fn report_summary(&self, start_time: Instant) {
1933+
self.config.exec_ctx.profiler().report_summary(start_time);
19341934
}
19351935
}
19361936

src/bootstrap/src/utils/exec.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::process::{
2020
Child, ChildStderr, ChildStdout, Command, CommandArgs, CommandEnvs, ExitStatus, Output, Stdio,
2121
};
2222
use std::sync::{Arc, Mutex};
23-
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
23+
use std::time::{Duration, Instant};
2424

2525
use build_helper::ci::CiEnv;
2626
use build_helper::drop_bomb::DropBomb;
@@ -100,19 +100,16 @@ impl CommandProfiler {
100100
pub fn record_execution(&self, key: CommandFingerprint, start_time: Instant) {
101101
let mut stats = self.stats.lock().unwrap();
102102
let entry = stats.entry(key).or_default();
103-
entry.traces.push(ExecutionTrace::Executed {
104-
timestamp: SystemTime::now(),
105-
duration: start_time.elapsed(),
106-
});
103+
entry.traces.push(ExecutionTrace::Executed { duration: start_time.elapsed() });
107104
}
108105

109106
pub fn record_cache_hit(&self, key: CommandFingerprint) {
110107
let mut stats = self.stats.lock().unwrap();
111108
let entry = stats.entry(key).or_default();
112-
entry.traces.push(ExecutionTrace::CacheHit { timestamp: SystemTime::now() });
109+
entry.traces.push(ExecutionTrace::CacheHit);
113110
}
114111

115-
pub fn report_summary(&self) {
112+
pub fn report_summary(&self, start_time: Instant) {
116113
let pid = process::id();
117114
let filename = format!("bootstrap-profile-{pid}.txt");
118115

@@ -145,28 +142,35 @@ impl CommandProfiler {
145142

146143
entries.sort_by(|a, b| b.2.cmp(&a.2));
147144

148-
for (key, profile, max_duration) in entries {
145+
let mut total_fingerprints = 0;
146+
let mut total_cache_hits = 0;
147+
let mut total_execution_duration = Duration::ZERO;
148+
let mut total_saved_duration = Duration::ZERO;
149+
150+
for (key, profile, max_duration) in &entries {
149151
writeln!(writer, "Command: {:?}", key.format_short_cmd()).unwrap();
150152

151153
let mut hits = 0;
152154
let mut runs = 0;
155+
let mut command_total_duration = Duration::ZERO;
153156

154157
for trace in &profile.traces {
155158
match trace {
156-
ExecutionTrace::CacheHit { timestamp } => {
159+
ExecutionTrace::CacheHit => {
157160
hits += 1;
158-
let time = timestamp.duration_since(UNIX_EPOCH).unwrap();
159-
writeln!(writer, " - Cache hit at: {time:?}").unwrap();
160161
}
161-
ExecutionTrace::Executed { duration, timestamp } => {
162+
ExecutionTrace::Executed { duration, .. } => {
162163
runs += 1;
163-
let time = timestamp.duration_since(UNIX_EPOCH).unwrap();
164-
writeln!(writer, " - Executed at: {time:?}, duration: {duration:.2?}")
165-
.unwrap();
164+
command_total_duration += *duration;
166165
}
167166
}
168167
}
169168

169+
total_fingerprints += 1;
170+
total_cache_hits += hits;
171+
total_execution_duration += command_total_duration;
172+
total_saved_duration += command_total_duration * hits as u32;
173+
170174
let duration_str = match max_duration {
171175
Some(d) => format!("{d:.2?}"),
172176
None => "-".into(),
@@ -179,14 +183,29 @@ impl CommandProfiler {
179183
.unwrap();
180184
}
181185

186+
// Add aggregated summary
187+
let total_bootstrap_time = start_time.elapsed();
188+
let overhead_time =
189+
total_bootstrap_time.checked_sub(total_execution_duration).unwrap_or(Duration::ZERO);
190+
191+
writeln!(writer, "\n=== Aggregated Summary ===").unwrap();
192+
writeln!(writer, "Total unique commands (fingerprints): {total_fingerprints}").unwrap();
193+
writeln!(writer, "Total time spent in command executions: {total_execution_duration:.2?}")
194+
.unwrap();
195+
writeln!(writer, "Total bootstrap time: {total_bootstrap_time:.2?}").unwrap();
196+
writeln!(writer, "Time spent outside command executions: {overhead_time:.2?}").unwrap();
197+
writeln!(writer, "Total cache hits: {total_cache_hits}").unwrap();
198+
writeln!(writer, "Estimated time saved due to cache hits: {total_saved_duration:.2?}")
199+
.unwrap();
200+
182201
println!("Command profiler report saved to {filename}");
183202
}
184203
}
185204

186205
#[derive(Clone)]
187206
pub enum ExecutionTrace {
188-
CacheHit { timestamp: SystemTime },
189-
Executed { timestamp: SystemTime, duration: Duration },
207+
CacheHit,
208+
Executed { duration: Duration },
190209
}
191210

192211
/// Wrapper around `std::process::Command`.

0 commit comments

Comments
 (0)