1 -----------------------------------------------------------------------------
3 -- (c) Simon Marlow 1997-2005
5 -----------------------------------------------------------------------------
7 module Slurp (Status(..), Results(..), ResultTable, parse_log) where
10 import qualified Data.Map as Map
16 -----------------------------------------------------------------------------
17 -- This is the structure into which we collect our results:
19 type ResultTable = Map String Results
30 data Results = Results {
31 compile_time :: Map String Float,
32 module_size :: Map String Int,
33 binary_size :: Maybe Int,
34 link_time :: Maybe Float,
36 elapsed_time :: [Float],
38 mut_elapsed_time :: [Float],
39 instrs :: Maybe Integer,
40 mem_reads :: Maybe Integer,
41 mem_writes :: Maybe Integer,
42 cache_misses :: Maybe Integer,
43 gc_work :: Maybe Integer,
45 gc_elapsed_time :: [Float],
47 gc0_elapsed_time :: [Float],
49 gc1_elapsed_time :: [Float],
51 allocs :: Maybe Integer,
53 compile_status :: Status
56 emptyResults :: Results
57 emptyResults = Results {
58 compile_time = Map.empty,
59 module_size = Map.empty,
60 binary_size = Nothing,
65 mut_elapsed_time = [],
69 cache_misses = Nothing,
73 gc0_elapsed_time = [],
75 gc1_elapsed_time = [],
79 compile_status = NotDone,
83 -----------------------------------------------------------------------------
89 ==nofib== awards: size of QSort.o follows...
90 ==nofib== banner: size of banner follows...
91 ==nofib== awards: time to link awards follows...
92 ==nofib== awards: time to run awards follows...
93 ==nofib== boyer2: time to compile Checker follows...
96 -- NB. the hyphen must come last (or first) inside [...] to stand for itself.
98 banner_re = mkRegex "^==nofib==[ \t]+([A-Za-z0-9_-]+):[ \t]+(size of|time to link|time to run|time to compile|time to compile & run)[ \t]+([A-Za-z0-9_-]+)(\\.o)?[ \t]+follows"
101 This regexp for the output of "time" works on FreeBSD, other versions
102 of "time" will need different regexps.
105 time_re :: String -> Maybe (Float, Float, Float)
106 time_re s = case matchRegex re s of
107 Just [real, user, system] ->
108 Just (read real, read user, read system)
109 Just _ -> error "time_re: Can't happen"
111 where re = mkRegex "^[ \t]*([0-9.]+)[ \t]+real[ \t]+([0-9.]+)[ \t]+user[ \t]+([0-9.]+)[ \t]+sys[ \t]*$"
113 time_gnu17_re :: String -> Maybe (Float, Float, String)
114 time_gnu17_re s = case matchRegex re s of
115 Just [user, system, elapsed] ->
116 Just (read user, read system, elapsed)
117 Just _ -> error "time_gnu17_re: Can't happen"
119 where re = mkRegex "^[ \t]*([0-9.]+)user[ \t]+([0-9.]+)system[ \t]+([0-9.:]+)elapsed"
120 -- /usr/bin/time --version reports: GNU time 1.7
121 -- notice the order is different, and the elapsed time
124 size_re :: String -> Maybe (Int, Int, Int)
125 size_re s = case matchRegex re s of
126 Just [text, datas, bss] ->
127 Just (read text, read datas, read bss)
128 Just _ -> error "size_re: Can't happen"
130 where re = mkRegex "^[ \t]*([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)"
133 <<ghc: 5820820 bytes, 0 GCs, 0/0 avg/max bytes residency (0 samples), 41087234 bytes GC work, 0.00 INIT (0.05 elapsed), 0.08 MUT (0.18 elapsed), 0.00 GC (0.00 elapsed) :ghc>>
135 = (bytes, gcs, avg_resid, max_resid, samples, gc_work,
136 init, init_elapsed, mut, mut_elapsed, gc, gc_elapsed)
138 ghc1_re = pre GHC 4.02
139 ghc2_re = GHC 4.02 (includes "xxM in use")
140 ghc3_re = GHC 4.03 (includes "xxxx bytes GC work")
141 ghc5_re = GHC 6.9 (includes GC(0) and GC(1) times)
144 ghc1_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float)
145 ghc1_re s = case matchRegex re s of
146 Just [allocations, gcs, avg_residency, max_residency, samples, gc_work', initialisation, initialisation_elapsed, mut, mut_elapsed, gc, gc_elapsed] ->
147 Just (read allocations, read gcs, read avg_residency, read max_residency, read samples, read gc_work', read initialisation, read initialisation_elapsed, read mut, read mut_elapsed, read gc, read gc_elapsed)
148 Just _ -> error "ghc1_re: Can't happen"
150 where re = mkRegex "^<<ghc:[ \t]+([0-9]+)[ \t]+bytes,[ \t]*([0-9]+)[ \t]+GCs,[ \t]*([0-9]+)/([0-9]+)[ \t]+avg/max bytes residency \\(([0-9]+) samples\\), ([0-9]+) bytes GC work, ([0-9.]+) INIT \\(([0-9.]+) elapsed\\), ([0-9.]+) MUT \\(([0-9.]+) elapsed\\), ([0-9.]+) GC \\(([0-9.]+) elapsed\\) :ghc>>"
152 ghc2_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float)
153 ghc2_re s = case matchRegex re s of
154 Just [allocations, gcs, avg_residency, max_residency, samples, in_use, initialisation, initialisation_elapsed, mut, mut_elapsed, gc, gc_elapsed] ->
155 Just (read allocations, read gcs, read avg_residency, read max_residency, read samples, read in_use, read initialisation, read initialisation_elapsed, read mut, read mut_elapsed, read gc, read gc_elapsed)
156 Just _ -> error "ghc2_re: Can't happen"
158 where re = mkRegex "^<<ghc:[ \t]+([0-9]+)[ \t]+bytes,[ \t]*([0-9]+)[ \t]+GCs,[ \t]*([0-9]+)/([0-9]+)[ \t]+avg/max bytes residency \\(([0-9]+) samples\\), ([0-9]+)M in use, ([0-9.]+) INIT \\(([0-9.]+) elapsed\\), ([0-9.]+) MUT \\(([0-9.]+) elapsed\\), ([0-9.]+) GC \\(([0-9.]+) elapsed\\) :ghc>>"
160 ghc3_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float)
161 ghc3_re s = case matchRegex re s of
162 Just [allocations, gcs, avg_residency, max_residency, samples, gc_work', in_use, initialisation, initialisation_elapsed, mut, mut_elapsed, gc, gc_elapsed] ->
163 Just (read allocations, read gcs, read avg_residency, read max_residency, read samples, read gc_work', read in_use, read initialisation, read initialisation_elapsed, read mut, read mut_elapsed, read gc, read gc_elapsed)
164 Just _ -> error "ghc3_re: Can't happen"
166 where re = mkRegex "^<<ghc:[ \t]+([0-9]+)[ \t]+bytes,[ \t]*([0-9]+)[ \t]+GCs,[ \t]*([0-9]+)/([0-9]+)[ \t]+avg/max bytes residency \\(([0-9]+) samples\\), ([0-9]+) bytes GC work, ([0-9]+)M in use, ([0-9.]+) INIT \\(([0-9.]+) elapsed\\), ([0-9.]+) MUT \\(([0-9.]+) elapsed\\), ([0-9.]+) GC \\(([0-9.]+) elapsed\\) :ghc>>"
168 ghc4_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float, Integer, Integer, Integer, Integer)
169 ghc4_re s = case matchRegex re s of
170 Just [allocations, gcs, avg_residency, max_residency, samples, gc_work', in_use, initialisation, initialisation_elapsed, mut, mut_elapsed, gc, gc_elapsed, instructions, memory_reads, memory_writes, l2_cache_misses] ->
171 Just (read allocations, read gcs, read avg_residency, read max_residency, read samples, read gc_work', read in_use, read initialisation, read initialisation_elapsed, read mut, read mut_elapsed, read gc, read gc_elapsed, read instructions, read memory_reads, read memory_writes, read l2_cache_misses)
172 Just _ -> error "ghc4_re: Can't happen"
174 where re = mkRegex "^<<ghc-instrs:[ \t]+([0-9]+)[ \t]+bytes,[ \t]*([0-9]+)[ \t]+GCs,[ \t]*([0-9]+)/([0-9]+)[ \t]+avg/max bytes residency \\(([0-9]+) samples\\), ([0-9]+) bytes GC work, ([0-9]+)M in use, ([0-9.]+) INIT \\(([0-9.]+) elapsed\\), ([0-9.]+) MUT \\(([0-9.]+) elapsed\\), ([0-9.]+) GC \\(([0-9.]+) elapsed\\), ([0-9]+) instructions, ([0-9]+) memory reads, ([0-9]+) memory writes, ([0-9]+) L2 cache misses :ghc-instrs>>"
176 ghc5_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float,Float,Float,Float,Float,Float)
177 ghc5_re s = case matchRegex re s of
178 Just [allocations, gcs, avg_residency, max_residency, samples, gc_work', in_use, initialisation, initialisation_elapsed, mut, mut_elapsed, gc, gc_elapsed, gc0, gc0_elapsed, gc1, gc1_elapsed, bal] ->
179 Just (read allocations, read gcs, read avg_residency, read max_residency, read samples, read gc_work', read in_use, read initialisation, read initialisation_elapsed, read mut, read mut_elapsed, read gc, read gc_elapsed, read gc0, read gc0_elapsed, read gc1, read gc1_elapsed, read bal)
180 Just _ -> error "ghc3_re: Can't happen"
182 where re = mkRegex "^<<ghc:[ \t]+([0-9]+)[ \t]+bytes,[ \t]*([0-9]+)[ \t]+GCs,[ \t]*([0-9]+)/([0-9]+)[ \t]+avg/max bytes residency \\(([0-9]+) samples\\), ([0-9]+) bytes GC work, ([0-9]+)M in use, ([0-9.]+) INIT \\(([0-9.]+) elapsed\\), ([0-9.]+) MUT \\(([0-9.]+) elapsed\\), ([0-9.]+) GC \\(([0-9.]+) elapsed\\), ([0-9.]+) GC\\(0\\) \\(([0-9.]+) elapsed\\), ([0-9.]+) GC\\(1\\) \\(([0-9.]+) elapsed\\), ([0-9.]+) balance :ghc>>"
184 wrong_exit_status, wrong_output, out_of_heap, out_of_stack :: Regex
185 wrong_exit_status = mkRegex "^\\**[ \t]*expected exit status ([0-9]+) not seen ; got ([0-9]+)"
186 wrong_output = mkRegex "^expected (stdout|stderr) not matched by reality$"
187 out_of_heap = mkRegex "^\\+ Heap exhausted;$"
188 out_of_stack = mkRegex "^\\+ Stack space overflow:"
190 parse_log :: String -> ResultTable
192 = combine_results -- collate information
194 . map process_chunk -- get information from each chunk
195 . tail -- first chunk is junk
196 . chunk_log [] [] -- break at banner lines
199 combine_results :: [(String,Results)] -> Map String Results
200 combine_results = foldr f Map.empty
202 f (prog,results) fm = Map.insertWith (flip combine2Results) prog results fm
204 combine2Results :: Results -> Results -> Results
206 Results{ compile_time = ct1, link_time = lt1,
208 run_time = rt1, elapsed_time = et1, mut_time = mt1,
209 mut_elapsed_time = me1,
210 instrs = is1, mem_reads = mr1, mem_writes = mw1,
212 gc_time = gt1, gc_elapsed_time = ge1, gc_work = gw1,
213 gc0_time = g0t1, gc0_elapsed_time = g0e1,
214 gc1_time = g1t1, gc1_elapsed_time = g1e1,
216 binary_size = bs1, allocs = al1,
217 run_status = rs1, compile_status = cs1 }
218 Results{ compile_time = ct2, link_time = lt2,
220 run_time = rt2, elapsed_time = et2, mut_time = mt2,
221 mut_elapsed_time = me2,
222 instrs = is2, mem_reads = mr2, mem_writes = mw2,
224 gc_time = gt2, gc_elapsed_time = ge2, gc_work = gw2,
225 gc0_time = g0t2, gc0_elapsed_time = g0e2,
226 gc1_time = g1t2, gc1_elapsed_time = g1e2,
228 binary_size = bs2, allocs = al2,
229 run_status = rs2, compile_status = cs2 }
230 = Results{ compile_time = Map.unionWith (flip const) ct1 ct2,
231 module_size = Map.unionWith (flip const) ms1 ms2,
232 link_time = lt1 `mplus` lt2,
233 run_time = rt1 ++ rt2,
234 elapsed_time = et1 ++ et2,
235 mut_time = mt1 ++ mt2,
236 mut_elapsed_time = me1 ++ me2,
237 instrs = is1 `mplus` is2,
238 mem_reads = mr1 `mplus` mr2,
239 mem_writes = mw1 `mplus` mw2,
240 cache_misses = cm1 `mplus` cm2,
241 gc_time = gt1 ++ gt2,
242 gc_elapsed_time= ge1 ++ ge2,
243 gc0_time = g0t1 ++ g0t2,
244 gc0_elapsed_time= g0e1 ++ g0e2,
245 gc1_time = g1t1 ++ g1t2,
246 gc1_elapsed_time= g1e1 ++ g1e2,
248 gc_work = gw1 `mplus` gw2,
249 binary_size = bs1 `mplus` bs2,
250 allocs = al1 `mplus` al2,
251 run_status = combStatus rs1 rs2,
252 compile_status = combStatus cs1 cs2 }
254 combStatus :: Status -> Status -> Status
255 combStatus NotDone y = y
256 combStatus x NotDone = x
259 chunk_log :: [String] -> [String] -> [String] -> [([String],[String])]
260 chunk_log header chunk [] = [(header,chunk)]
261 chunk_log header chunk (l:ls) =
262 case matchRegex banner_re l of
263 Nothing -> chunk_log header (l:chunk) ls
264 Just stuff -> (header,chunk) : chunk_log stuff [] ls
266 process_chunk :: ([String],[String]) -> [(String,Results)]
267 process_chunk (progName : what : modName : _, chk) =
269 "time to compile" -> parse_compile_time progName modName chk
270 "time to run" -> parse_run_time progName (reverse chk) emptyResults NotDone
271 "time to compile & run" -> parse_compile_time progName modName chk
272 ++ parse_run_time progName (reverse chk) emptyResults NotDone
273 "time to link" -> parse_link_time progName chk
274 "size of" -> parse_size progName modName chk
275 _ -> error ("process_chunk: "++what)
276 process_chunk _ = error "process_chunk: Can't happen"
278 parse_compile_time :: String -> String -> [String] -> [(String, Results)]
279 parse_compile_time _ _ [] = []
280 parse_compile_time progName modName (l:ls) =
282 Just (_real, user, _system) ->
283 let ct = Map.singleton modName user
285 [(progName, emptyResults{compile_time = ct})];
288 case time_gnu17_re l of {
289 Just (user, _system, _elapsed) ->
290 let ct = Map.singleton modName user
292 [(progName, emptyResults{compile_time = ct})];
296 Just (_, _, _, _, _, _, initialisation, _, mut, _, gc, _) ->
298 time = (initialisation + mut + gc) :: Float
299 ct = Map.singleton modName time
301 [(progName, emptyResults{compile_time = ct})];
305 Just (_, _, _, _, _, _, initialisation, _, mut, _, gc, _) ->
306 let ct = Map.singleton modName (initialisation + mut + gc)
308 [(progName, emptyResults{compile_time = ct})];
312 Just (_, _, _, _, _, _, _, initialisation, _, mut, _, gc, _) ->
313 let ct = Map.singleton modName (initialisation + mut + gc)
315 [(progName, emptyResults{compile_time = ct})];
319 Just (_, _, _, _, _, _, _, initialisation, _, mut, _, gc, _, _, _, _, _) ->
320 let ct = Map.singleton modName (initialisation + mut + gc)
322 [(progName, emptyResults{compile_time = ct})];
325 parse_compile_time progName modName ls
328 parse_link_time :: String -> [String] -> [(String, Results)]
329 parse_link_time _ [] = []
330 parse_link_time prog (l:ls) =
332 Just (_real, user, _system) ->
333 [(prog,emptyResults{link_time = Just user})];
336 case time_gnu17_re l of {
337 Just (user, _system, _elapsed) ->
338 [(prog,emptyResults{link_time = Just user})];
341 parse_link_time prog ls
345 -- There might be multiple runs of the program, so we have to collect up
346 -- all the results. Variable results like runtimes are aggregated into
347 -- a list, whereas the non-variable aspects are just kept singly.
348 parse_run_time :: String -> [String] -> Results -> Status
349 -> [(String, Results)]
350 parse_run_time _ [] _ NotDone = []
351 parse_run_time prog [] res ex = [(prog, res{run_status=ex})]
352 parse_run_time prog (l:ls) res ex =
354 Just (allocations, _, _, _, _, _, initialisation, init_elapsed, mut, mut_elapsed, gc, gc_elapsed) ->
355 got_run_result allocations initialisation init_elapsed mut mut_elapsed gc gc_elapsed [] [] [] [] []
356 Nothing Nothing Nothing Nothing Nothing;
360 Just (allocations, _, _, _, _, _, initialisation, init_elapsed, mut, mut_elapsed, gc, gc_elapsed) ->
361 got_run_result allocations initialisation init_elapsed mut mut_elapsed gc gc_elapsed [] [] [] [] []
362 Nothing Nothing Nothing Nothing Nothing;
367 Just (allocations, _, _, _, _, gc_work', _, initialisation, init_elapsed, mut, mut_elapsed, gc, gc_elapsed) ->
368 got_run_result allocations initialisation init_elapsed mut mut_elapsed gc gc_elapsed [] [] [] [] []
369 (Just gc_work') Nothing Nothing Nothing Nothing;
374 Just (allocations, _, _, _, _, gc_work', _, initialisation, init_elapsed, mut, mut_elapsed, gc, gc_elapsed, is, mem_rs, mem_ws, cache_misses') ->
375 got_run_result allocations initialisation init_elapsed mut mut_elapsed gc gc_elapsed [] [] [] [] []
376 (Just gc_work') (Just is) (Just mem_rs)
377 (Just mem_ws) (Just cache_misses');
382 Just (allocations, _, _, _, _, gc_work', _, initialisation, init_elapsed, mut, mut_elapsed, gc, gc_elapsed, gc0, gc0_elapsed, gc1, gc1_elapsed, bal) ->
383 got_run_result allocations initialisation init_elapsed mut mut_elapsed gc gc_elapsed
384 [gc0] [gc0_elapsed] [gc1] [gc1_elapsed] [bal]
385 (Just gc_work') Nothing Nothing Nothing Nothing;
389 case matchRegex wrong_output l of {
391 parse_run_time prog ls res (combineRunResult WrongStdout ex);
393 parse_run_time prog ls res (combineRunResult WrongStderr ex);
394 Just _ -> error "wrong_output: Can't happen";
397 case matchRegex wrong_exit_status l of {
398 Just [_wanted, got] ->
399 parse_run_time prog ls res (combineRunResult (Exit (read got)) ex);
400 Just _ -> error "wrong_exit_status: Can't happen";
403 case matchRegex out_of_heap l of {
405 parse_run_time prog ls res (combineRunResult OutOfHeap ex);
408 case matchRegex out_of_stack l of {
410 parse_run_time prog ls res (combineRunResult OutOfStack ex);
412 parse_run_time prog ls res ex;
416 got_run_result allocations initialisation init_elapsed mut mut_elapsed gc gc_elapsed gc0 gc0_elapsed gc1 gc1_elapsed bal gc_work' instrs' mem_rs mem_ws cache_misses'
417 = -- trace ("got_run_result: " ++ initialisation ++ ", " ++ mut ++ ", " ++ gc) $
419 time = initialisation + mut + gc
420 etime = init_elapsed + mut_elapsed + gc_elapsed
421 res' = combine2Results res
422 emptyResults{ run_time = [time],
423 elapsed_time = [etime],
425 mut_elapsed_time = [mut_elapsed],
427 gc_elapsed_time = [gc_elapsed],
429 gc0_elapsed_time = gc0_elapsed,
431 gc1_elapsed_time = gc1_elapsed,
434 allocs = Just allocations,
438 cache_misses = cache_misses',
442 parse_run_time prog ls res' Success
444 combineRunResult :: Status -> Status -> Status
445 combineRunResult OutOfHeap _ = OutOfHeap
446 combineRunResult _ OutOfHeap = OutOfHeap
447 combineRunResult OutOfStack _ = OutOfStack
448 combineRunResult _ OutOfStack = OutOfStack
449 combineRunResult (Exit e) _ = Exit e
450 combineRunResult _ (Exit e) = Exit e
451 combineRunResult exit _ = exit
453 parse_size :: String -> String -> [String] -> [(String, Results)]
454 parse_size _ _ [] = []
455 parse_size progName modName (l:ls) =
457 Nothing -> parse_size progName modName ls
458 Just (text, datas, _bss)
459 | progName == modName ->
460 [(progName,emptyResults{binary_size =
462 compile_status = Success})]
464 let ms = Map.singleton modName (text + datas)
466 [(progName,emptyResults{module_size = ms})]