Skip to content

Commit 30b44dc

Browse files
committed
integration profiler to command execution
1 parent d520d7c commit 30b44dc

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/bootstrap/src/utils/exec.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::path::Path;
1616
use std::process::{
1717
Child, ChildStderr, ChildStdout, Command, CommandArgs, CommandEnvs, ExitStatus, Output, Stdio,
1818
};
19-
use std::time::{Duration, Instant};
2019
use std::sync::{Arc, Mutex};
20+
use std::time::{Duration, Instant};
2121

2222
use build_helper::ci::CiEnv;
2323
use build_helper::drop_bomb::DropBomb;
@@ -84,7 +84,6 @@ pub struct CommandProfiler {
8484
stats: Mutex<HashMap<CommandCacheKey, CommandProfile>>,
8585
}
8686

87-
8887
impl CommandProfiler {
8988
pub fn record(&self, key: CommandCacheKey, duration: Duration) {
9089
let mut stats = self.stats.lock().unwrap();
@@ -102,9 +101,7 @@ impl CommandProfiler {
102101
for (key, profile) in entries.into_iter().take(top_n) {
103102
println!(
104103
"- {:?} (count: {}, max_duration: {:.2?})",
105-
key.program,
106-
profile.count,
107-
profile.max_duration,
104+
key.program, profile.count, profile.max_duration,
108105
);
109106
}
110107
}
@@ -507,6 +504,7 @@ enum CommandState<'a> {
507504
stderr: OutputMode,
508505
executed_at: &'a Location<'a>,
509506
cache_key: Option<CommandCacheKey>,
507+
start_time: Instant,
510508
},
511509
}
512510

@@ -543,6 +541,10 @@ impl ExecutionContext {
543541
}
544542
}
545543

544+
pub fn profiler(&self) -> &CommandProfiler {
545+
&self.profiler
546+
}
547+
546548
pub fn get_dry_run(&self) -> &DryRun {
547549
&self.dry_run
548550
}
@@ -605,6 +607,7 @@ impl ExecutionContext {
605607
{
606608
command.mark_as_executed();
607609
self.verbose(|| println!("Cache hit: {command:?}"));
610+
self.profiler.record(cache_key.unwrap(), Duration::from_secs(0));
608611
return DeferredCommand { state: CommandState::Cached(cached_output) };
609612
}
610613

@@ -620,6 +623,7 @@ impl ExecutionContext {
620623
stderr,
621624
executed_at,
622625
cache_key,
626+
start_time: Instant::now(),
623627
},
624628
};
625629
}
@@ -635,6 +639,8 @@ impl ExecutionContext {
635639
cmd.stdout(stdout.stdio());
636640
cmd.stderr(stderr.stdio());
637641

642+
let start_time = Instant::now();
643+
638644
let child = cmd.spawn();
639645

640646
DeferredCommand {
@@ -645,6 +651,7 @@ impl ExecutionContext {
645651
stderr,
646652
executed_at,
647653
cache_key,
654+
start_time,
648655
},
649656
}
650657
}
@@ -729,7 +736,15 @@ impl<'a> DeferredCommand<'a> {
729736
pub fn wait_for_output(self, exec_ctx: impl AsRef<ExecutionContext>) -> CommandOutput {
730737
match self.state {
731738
CommandState::Cached(output) => output,
732-
CommandState::Deferred { process, command, stdout, stderr, executed_at, cache_key } => {
739+
CommandState::Deferred {
740+
process,
741+
command,
742+
stdout,
743+
stderr,
744+
executed_at,
745+
cache_key,
746+
start_time,
747+
} => {
733748
let exec_ctx = exec_ctx.as_ref();
734749

735750
let output =
@@ -739,6 +754,8 @@ impl<'a> DeferredCommand<'a> {
739754
&& let (Some(cache_key), Some(_)) = (&cache_key, output.status())
740755
{
741756
exec_ctx.command_cache.insert(cache_key.clone(), output.clone());
757+
let duration = start_time.elapsed();
758+
exec_ctx.profiler.record(cache_key.clone(), duration);
742759
}
743760

744761
output

0 commit comments

Comments
 (0)