Skip to content

Commit a9f2aad

Browse files
committed
Auto merge of #143525 - Shourya742:2025-07-06-add-profiler, r=Kobzol
Add profiler to bootstrap command This PR adds command profiling to the bootstrap command. It tracks the total execution time and records cache hits for each command. It also provides the ability to export execution result to a JSON file. Integrating this with Chrome tracing could further enhance observability. r? `@Kobzol`
2 parents 78a6e13 + 7de1174 commit a9f2aad

File tree

6 files changed

+227
-30
lines changed

6 files changed

+227
-30
lines changed

src/bootstrap/src/bin/main.rs

Lines changed: 14 additions & 2 deletions
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::{
@@ -17,11 +18,17 @@ use bootstrap::{
1718
#[cfg(feature = "tracing")]
1819
use tracing::instrument;
1920

21+
fn is_bootstrap_profiling_enabled() -> bool {
22+
env::var("BOOTSTRAP_PROFILE").is_ok_and(|v| v == "1")
23+
}
24+
2025
#[cfg_attr(feature = "tracing", instrument(level = "trace", name = "main"))]
2126
fn main() {
2227
#[cfg(feature = "tracing")]
2328
let _guard = setup_tracing();
2429

30+
let start_time = Instant::now();
31+
2532
let args = env::args().skip(1).collect::<Vec<_>>();
2633

2734
if Flags::try_parse_verbose_help(&args) {
@@ -96,7 +103,8 @@ fn main() {
96103
let out_dir = config.out.clone();
97104

98105
debug!("creating new build based on config");
99-
Build::new(config).build();
106+
let mut build = Build::new(config);
107+
build.build();
100108

101109
if suggest_setup {
102110
println!("WARNING: you have not made a `bootstrap.toml`");
@@ -147,6 +155,10 @@ fn main() {
147155
t!(file.write_all(lines.join("\n").as_bytes()));
148156
}
149157
}
158+
159+
if is_bootstrap_profiling_enabled() {
160+
build.report_summary(start_time);
161+
}
150162
}
151163

152164
fn check_version(config: &Config) -> Option<String> {
@@ -226,7 +238,7 @@ fn setup_tracing() -> impl Drop {
226238
let mut chrome_layer = tracing_chrome::ChromeLayerBuilder::new().include_args(true);
227239

228240
// Writes the Chrome profile to trace-<unix-timestamp>.json if enabled
229-
if !env::var("BOOTSTRAP_PROFILE").is_ok_and(|v| v == "1") {
241+
if !is_bootstrap_profiling_enabled() {
230242
chrome_layer = chrome_layer.writer(io::sink());
231243
}
232244

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,7 @@ pub fn stream_cargo(
25762576
}
25772577

25782578
// Make sure Cargo actually succeeded after we read all of its stdout.
2579-
let status = t!(streaming_command.wait());
2579+
let status = t!(streaming_command.wait(&builder.config.exec_ctx));
25802580
if builder.is_verbose() && !status.success() {
25812581
eprintln!(
25822582
"command did not execute successfully: {cmd:?}\n\

src/bootstrap/src/lib.rs

Lines changed: 5 additions & 1 deletion
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;
@@ -1928,6 +1928,10 @@ to download LLVM rather than building it.
19281928
pub fn exec_ctx(&self) -> &ExecutionContext {
19291929
&self.config.exec_ctx
19301930
}
1931+
1932+
pub fn report_summary(&self, start_time: Instant) {
1933+
self.config.exec_ctx.profiler().report_summary(start_time);
1934+
}
19311935
}
19321936

19331937
impl AsRef<ExecutionContext> for Build {

0 commit comments

Comments
 (0)