2727# prefix every log line -- a per-line elapsed stamp, "[ 123.45] " --
2828# and such a prefix must not change what this reports. Hours are
2929# optional, as _format_elapsed only emits them past an hour, and the
30- # fraction of a second is matched so it can be discarded.
31- ELAPSED_RE = re .compile (r"Elapsed time: (?:(\d+):)?(\d+):(\d+)(?:\.\d+)?\[h:\]min:sec" )
30+ # fraction of a second is matched so it can be discarded from the
31+ # reported seconds, but kept for the CPU/elapsed ratio.
32+ ELAPSED_RE = re .compile (r"Elapsed time: (?:(\d+):)?(\d+):(\d+)(\.\d+)?\[h:\]min:sec" )
33+
34+ # The CPU time run_command.py emits on the same line, as user and sys
35+ # seconds; reported as their sum.
36+ CPU_RE = re .compile (r"CPU time: user (\d+(?:\.\d+)?) sys (\d+(?:\.\d+)?)" )
3237
3338
3439def get_hashes (f ):
@@ -65,13 +70,18 @@ def get_hashes(f):
6570def print_log_dir_times (logdir , args ):
6671 first = True
6772 totalElapsed = 0
73+ totalCpu = 0.0
74+ # Elapsed time of only the steps that report CPU time, so the total
75+ # CPU/elapsed ratio isn't diluted by steps without it.
76+ totalCpuElapsed = 0.0
6877 total_max_memory = 0
6978 if not args .match :
7079 print (logdir )
7180
7281 # Loop on all log files in the directory
7382 for f in sorted (pathlib .Path (logdir ).glob ("**/*.log" )):
74- if any (x in str (f ) for x in ["eqy_output" , "rsz_lec_check" ]):
83+ # kepler-formal's own logs; its timing is in ${step}_lec/_sec.log
84+ if any (x in str (f ) for x in ["eqy_output" , "_lec_check" , "_sec_check" ]):
7585 continue
7686 # Extract Elapsed Time line from log file
7787 stem = os .path .splitext (os .path .basename (str (f )))[0 ]
@@ -81,6 +91,8 @@ def print_log_dir_times(logdir, args):
8191 found = False
8292 for line in logfile :
8393 elapsedTime = None
94+ elapsedFraction = 0.0
95+ cpuTime = None
8496 peak_memory = None
8597
8698 # Example line:
@@ -91,15 +103,20 @@ def print_log_dir_times(logdir, args):
91103 # so anything the line is prefixed with is ignored.
92104 timeMatch = ELAPSED_RE .search (line )
93105 if timeMatch :
94- hours , minutes , seconds = timeMatch .groups ()
106+ hours , minutes , seconds , fraction = timeMatch .groups ()
95107 # Any fraction of a second is dropped.
108+ if fraction :
109+ elapsedFraction = float (fraction )
96110 elapsedTime = int (minutes ) * 60 + int (seconds )
97111 if hours is not None :
98112 elapsedTime += int (hours ) * 3600
99113 else :
100114 print (
101115 "Elapsed time not understood in" , str (line ), file = sys .stderr
102116 )
117+ cpuMatch = CPU_RE .search (line )
118+ if cpuMatch :
119+ cpuTime = float (cpuMatch .group (1 )) + float (cpuMatch .group (2 ))
103120 # Find Peak Memory
104121 peak_memory = int (
105122 int (line .split ("Peak memory: " )[1 ].split ("KB" )[0 ]) / 1024
@@ -114,41 +131,64 @@ def print_log_dir_times(logdir, args):
114131
115132 # Print the name of the step and the corresponding elapsed time.
116133 # One row per (stage, result-file-ext); only the first row of a
117- # stage shows elapsed/peak.
118- format_str = "%-25s %-6s %10s %14s %20s"
134+ # stage shows elapsed/cpu/peak.
119135 if elapsedTime is not None and peak_memory is not None :
120136 if first and not args .noHeader :
121- print (
122- format_str
123- % (
124- "Log" ,
125- "Ext" ,
126- "Elapsed/s" ,
127- "Peak Memory/MB" ,
128- "sha1sum result [0:20)" ,
129- )
130- )
137+ print_header ()
131138 first = False
132139 stage_first = True
133140 for ext , h in hashes :
134- print (
135- format_str
136- % (
137- stem ,
138- ext ,
139- elapsedTime if stage_first else "" ,
140- peak_memory if stage_first else "" ,
141- h [0 :20 ],
142- )
143- )
141+ if stage_first :
142+ if cpuTime is None :
143+ cpu = "N/A"
144+ else :
145+ cpu = round (cpuTime )
146+ ratio = cpu_ratio (cpuTime , elapsedTime + elapsedFraction )
147+ print_row (stem , ext , elapsedTime , cpu , ratio , peak_memory , h [0 :10 ])
148+ else :
149+ print_row (stem , ext , "" , "" , "" , "" , h [0 :10 ])
144150 stage_first = False
145- if elapsedTime is not None :
151+ # LEC/SEC run inside another step (e.g. 6_report), which already
152+ # counts their time.
153+ nested = stem .endswith (("_lec" , "_sec" ))
154+ if elapsedTime is not None and not nested :
146155 totalElapsed += elapsedTime
156+ if cpuTime is not None and elapsedTime is not None and not nested :
157+ totalCpu += cpuTime
158+ totalCpuElapsed += elapsedTime + elapsedFraction
147159 if peak_memory is not None :
148160 total_max_memory = max (total_max_memory , int (peak_memory ))
149161
150162 if totalElapsed != 0 and not args .match :
151- print (format_str % ("Total" , "" , totalElapsed , total_max_memory , "" ))
163+ print_row (
164+ "Total" ,
165+ "" ,
166+ totalElapsed ,
167+ round (totalCpu ),
168+ cpu_ratio (totalCpu , totalCpuElapsed ),
169+ total_max_memory ,
170+ "" ,
171+ )
172+
173+
174+ def cpu_ratio (cpu , elapsed ):
175+ """CPU time over elapsed time: roughly the number of busy threads."""
176+ if cpu is None or not elapsed :
177+ return "N/A"
178+ return "%.2f" % (cpu / elapsed )
179+
180+
181+ def print_header ():
182+ """Print the column headers, split over two lines to keep the
183+ columns narrow."""
184+ print_row ("Log" , "Ext" , "Elapsed" , "CPU" , "CPU/" , "Peak Mem" , "sha1sum" )
185+ print_row ("" , "" , "s" , "s" , "Elapsed" , "MB" , "[0:10)" )
186+
187+
188+ def print_row (log , ext , elapsed , cpu , ratio , memory , sha ):
189+ fmt = "%-22s %-6s %8s %8s %8s %8s %s"
190+ row = fmt % (log , ext , elapsed , cpu , ratio , memory , sha )
191+ print (row .rstrip ())
152192
153193
154194def scan_logs (args ):
0 commit comments