@@ -20,7 +20,7 @@ use std::process::{
2020 Child , ChildStderr , ChildStdout , Command , CommandArgs , CommandEnvs , ExitStatus , Output , Stdio ,
2121} ;
2222use std:: sync:: { Arc , Mutex } ;
23- use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
23+ use std:: time:: { Duration , Instant } ;
2424
2525use build_helper:: ci:: CiEnv ;
2626use 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 ) ]
187206pub 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