@@ -16,8 +16,8 @@ use std::path::Path;
16
16
use std:: process:: {
17
17
Child , ChildStderr , ChildStdout , Command , CommandArgs , CommandEnvs , ExitStatus , Output , Stdio ,
18
18
} ;
19
- use std:: time:: { Duration , Instant } ;
20
19
use std:: sync:: { Arc , Mutex } ;
20
+ use std:: time:: { Duration , Instant } ;
21
21
22
22
use build_helper:: ci:: CiEnv ;
23
23
use build_helper:: drop_bomb:: DropBomb ;
@@ -84,7 +84,6 @@ pub struct CommandProfiler {
84
84
stats : Mutex < HashMap < CommandCacheKey , CommandProfile > > ,
85
85
}
86
86
87
-
88
87
impl CommandProfiler {
89
88
pub fn record ( & self , key : CommandCacheKey , duration : Duration ) {
90
89
let mut stats = self . stats . lock ( ) . unwrap ( ) ;
@@ -102,9 +101,7 @@ impl CommandProfiler {
102
101
for ( key, profile) in entries. into_iter ( ) . take ( top_n) {
103
102
println ! (
104
103
"- {:?} (count: {}, max_duration: {:.2?})" ,
105
- key. program,
106
- profile. count,
107
- profile. max_duration,
104
+ key. program, profile. count, profile. max_duration,
108
105
) ;
109
106
}
110
107
}
@@ -507,6 +504,7 @@ enum CommandState<'a> {
507
504
stderr : OutputMode ,
508
505
executed_at : & ' a Location < ' a > ,
509
506
cache_key : Option < CommandCacheKey > ,
507
+ start_time : Instant ,
510
508
} ,
511
509
}
512
510
@@ -543,6 +541,10 @@ impl ExecutionContext {
543
541
}
544
542
}
545
543
544
+ pub fn profiler ( & self ) -> & CommandProfiler {
545
+ & self . profiler
546
+ }
547
+
546
548
pub fn get_dry_run ( & self ) -> & DryRun {
547
549
& self . dry_run
548
550
}
@@ -605,6 +607,7 @@ impl ExecutionContext {
605
607
{
606
608
command. mark_as_executed ( ) ;
607
609
self . verbose ( || println ! ( "Cache hit: {command:?}" ) ) ;
610
+ self . profiler . record ( cache_key. unwrap ( ) , Duration :: from_secs ( 0 ) ) ;
608
611
return DeferredCommand { state : CommandState :: Cached ( cached_output) } ;
609
612
}
610
613
@@ -620,6 +623,7 @@ impl ExecutionContext {
620
623
stderr,
621
624
executed_at,
622
625
cache_key,
626
+ start_time : Instant :: now ( ) ,
623
627
} ,
624
628
} ;
625
629
}
@@ -635,6 +639,8 @@ impl ExecutionContext {
635
639
cmd. stdout ( stdout. stdio ( ) ) ;
636
640
cmd. stderr ( stderr. stdio ( ) ) ;
637
641
642
+ let start_time = Instant :: now ( ) ;
643
+
638
644
let child = cmd. spawn ( ) ;
639
645
640
646
DeferredCommand {
@@ -645,6 +651,7 @@ impl ExecutionContext {
645
651
stderr,
646
652
executed_at,
647
653
cache_key,
654
+ start_time,
648
655
} ,
649
656
}
650
657
}
@@ -729,7 +736,15 @@ impl<'a> DeferredCommand<'a> {
729
736
pub fn wait_for_output ( self , exec_ctx : impl AsRef < ExecutionContext > ) -> CommandOutput {
730
737
match self . state {
731
738
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
+ } => {
733
748
let exec_ctx = exec_ctx. as_ref ( ) ;
734
749
735
750
let output =
@@ -739,6 +754,8 @@ impl<'a> DeferredCommand<'a> {
739
754
&& let ( Some ( cache_key) , Some ( _) ) = ( & cache_key, output. status ( ) )
740
755
{
741
756
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) ;
742
759
}
743
760
744
761
output
0 commit comments