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,
37 instrs :: Maybe Integer,
38 mem_reads :: Maybe Integer,
39 mem_writes :: Maybe Integer,
40 cache_misses :: Maybe Integer,
41 gc_work :: Maybe Integer,
43 allocs :: Maybe Integer,
45 compile_status :: Status
48 emptyResults :: Results
49 emptyResults = Results {
50 compile_time = Map.empty,
51 module_size = Map.empty,
52 binary_size = Nothing,
59 cache_misses = Nothing,
63 compile_status = NotDone,
67 -----------------------------------------------------------------------------
73 ==nofib== awards: size of QSort.o follows...
74 ==nofib== banner: size of banner follows...
75 ==nofib== awards: time to link awards follows...
76 ==nofib== awards: time to run awards follows...
77 ==nofib== boyer2: time to compile Checker follows...
80 -- NB. the hyphen must come last (or first) inside [...] to stand for itself.
82 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"
85 This regexp for the output of "time" works on FreeBSD, other versions
86 of "time" will need different regexps.
89 time_re :: String -> Maybe (Float, Float, Float)
90 time_re s = case matchRegex re s of
91 Just [real, user, system] ->
92 Just (read real, read user, read system)
93 Just _ -> error "time_re: Can't happen"
95 where re = mkRegex "^[ \t]*([0-9.]+)[ \t]+real[ \t]+([0-9.]+)[ \t]+user[ \t]+([0-9.]+)[ \t]+sys[ \t]*$"
97 time_gnu17_re :: String -> Maybe (Float, Float, String)
98 time_gnu17_re s = case matchRegex re s of
99 Just [user, system, elapsed] ->
100 Just (read user, read system, elapsed)
101 Just _ -> error "time_gnu17_re: Can't happen"
103 where re = mkRegex "^[ \t]*([0-9.]+)user[ \t]+([0-9.]+)system[ \t]+([0-9.:]+)elapsed"
104 -- /usr/bin/time --version reports: GNU time 1.7
105 -- notice the order is different, and the elapsed time
108 size_re :: String -> Maybe (Int, Int, Int)
109 size_re s = case matchRegex re s of
110 Just [text, datas, bss] ->
111 Just (read text, read datas, read bss)
112 Just _ -> error "size_re: Can't happen"
114 where re = mkRegex "^[ \t]*([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)"
117 <<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>>
119 = (bytes, gcs, avg_resid, max_resid, samples, gc_work,
120 init, init_elapsed, mut, mut_elapsed, gc, gc_elapsed)
122 ghc1_re = pre GHC 4.02
123 ghc2_re = GHC 4.02 (includes "xxM in use")
124 ghc3_re = GHC 4.03 (includes "xxxx bytes GC work")
127 ghc1_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float)
128 ghc1_re s = case matchRegex re s of
129 Just [allocations, gcs, avg_residency, max_residency, samples, gc_work', initialisation, initialisation_elapsed, mut, mut_elapsed, gc, gc_elapsed] ->
130 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)
131 Just _ -> error "ghc1_re: Can't happen"
133 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>>"
135 ghc2_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float)
136 ghc2_re s = case matchRegex re s of
137 Just [allocations, gcs, avg_residency, max_residency, samples, in_use, initialisation, initialisation_elapsed, mut, mut_elapsed, gc, gc_elapsed] ->
138 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)
139 Just _ -> error "ghc2_re: Can't happen"
141 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>>"
143 ghc3_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float)
144 ghc3_re s = case matchRegex re s of
145 Just [allocations, gcs, avg_residency, max_residency, samples, gc_work', in_use, initialisation, initialisation_elapsed, mut, mut_elapsed, gc, gc_elapsed] ->
146 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)
147 Just _ -> error "ghc3_re: Can't happen"
149 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>>"
151 ghc4_re :: String -> Maybe (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Float, Float, Float, Float, Float, Float, Integer, Integer, Integer, Integer)
152 ghc4_re s = case matchRegex re s of
153 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] ->
154 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)
155 Just _ -> error "ghc4_re: Can't happen"
157 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>>"
159 wrong_exit_status, wrong_output, out_of_heap, out_of_stack :: Regex
160 wrong_exit_status = mkRegex "^\\**[ \t]*expected exit status ([0-9]+) not seen ; got ([0-9]+)"
161 wrong_output = mkRegex "^expected (stdout|stderr) not matched by reality$"
162 out_of_heap = mkRegex "^\\+ Heap exhausted;$"
163 out_of_stack = mkRegex "^\\+ Stack space overflow:"
165 parse_log :: String -> ResultTable
167 = combine_results -- collate information
169 . map process_chunk -- get information from each chunk
170 . tail -- first chunk is junk
171 . chunk_log [] [] -- break at banner lines
174 combine_results :: [(String,Results)] -> Map String Results
175 combine_results = foldr f Map.empty
177 f (prog,results) fm = Map.insertWith (flip combine2Results) prog results fm
179 combine2Results :: Results -> Results -> Results
181 Results{ compile_time = ct1, link_time = lt1,
183 run_time = rt1, mut_time = mt1,
184 instrs = is1, mem_reads = mr1, mem_writes = mw1,
186 gc_time = gt1, gc_work = gw1,
187 binary_size = bs1, allocs = al1,
188 run_status = rs1, compile_status = cs1 }
189 Results{ compile_time = ct2, link_time = lt2,
191 run_time = rt2, mut_time = mt2,
192 instrs = is2, mem_reads = mr2, mem_writes = mw2,
194 gc_time = gt2, gc_work = gw2,
195 binary_size = bs2, allocs = al2,
196 run_status = rs2, compile_status = cs2 }
197 = Results{ compile_time = Map.unionWith (flip const) ct1 ct2,
198 module_size = Map.unionWith (flip const) ms1 ms2,
199 link_time = lt1 `mplus` lt2,
200 run_time = rt1 ++ rt2,
201 mut_time = mt1 ++ mt2,
202 instrs = is1 `mplus` is2,
203 mem_reads = mr1 `mplus` mr2,
204 mem_writes = mw1 `mplus` mw2,
205 cache_misses = cm1 `mplus` cm2,
206 gc_time = gt1 ++ gt2,
207 gc_work = gw1 `mplus` gw2,
208 binary_size = bs1 `mplus` bs2,
209 allocs = al1 `mplus` al2,
210 run_status = combStatus rs1 rs2,
211 compile_status = combStatus cs1 cs2 }
213 combStatus :: Status -> Status -> Status
214 combStatus NotDone y = y
215 combStatus x NotDone = x
218 chunk_log :: [String] -> [String] -> [String] -> [([String],[String])]
219 chunk_log header chunk [] = [(header,chunk)]
220 chunk_log header chunk (l:ls) =
221 case matchRegex banner_re l of
222 Nothing -> chunk_log header (l:chunk) ls
223 Just stuff -> (header,chunk) : chunk_log stuff [] ls
225 process_chunk :: ([String],[String]) -> [(String,Results)]
226 process_chunk (progName : what : modName : _, chk) =
228 "time to compile" -> parse_compile_time progName modName chk
229 "time to run" -> parse_run_time progName (reverse chk) emptyResults NotDone
230 "time to compile & run" -> parse_compile_time progName modName chk
231 ++ parse_run_time progName (reverse chk) emptyResults NotDone
232 "time to link" -> parse_link_time progName chk
233 "size of" -> parse_size progName modName chk
234 _ -> error ("process_chunk: "++what)
235 process_chunk _ = error "process_chunk: Can't happen"
237 parse_compile_time :: String -> String -> [String] -> [(String, Results)]
238 parse_compile_time _ _ [] = []
239 parse_compile_time progName modName (l:ls) =
241 Just (_real, user, _system) ->
242 let ct = Map.singleton modName user
244 [(progName, emptyResults{compile_time = ct})];
247 case time_gnu17_re l of {
248 Just (user, _system, _elapsed) ->
249 let ct = Map.singleton modName user
251 [(progName, emptyResults{compile_time = ct})];
255 Just (_, _, _, _, _, _, initialisation, _, mut, _, gc, _) ->
257 time = (initialisation + mut + gc) :: Float
258 ct = Map.singleton modName time
260 [(progName, emptyResults{compile_time = ct})];
264 Just (_, _, _, _, _, _, initialisation, _, mut, _, gc, _) ->
265 let ct = Map.singleton modName (initialisation + mut + gc)
267 [(progName, emptyResults{compile_time = ct})];
271 Just (_, _, _, _, _, _, _, initialisation, _, mut, _, gc, _) ->
272 let ct = Map.singleton modName (initialisation + mut + gc)
274 [(progName, emptyResults{compile_time = ct})];
278 Just (_, _, _, _, _, _, _, initialisation, _, mut, _, gc, _, _, _, _, _) ->
279 let ct = Map.singleton modName (initialisation + mut + gc)
281 [(progName, emptyResults{compile_time = ct})];
284 parse_compile_time progName modName ls
287 parse_link_time :: String -> [String] -> [(String, Results)]
288 parse_link_time _ [] = []
289 parse_link_time prog (l:ls) =
291 Just (_real, user, _system) ->
292 [(prog,emptyResults{link_time = Just user})];
295 case time_gnu17_re l of {
296 Just (user, _system, _elapsed) ->
297 [(prog,emptyResults{link_time = Just user})];
300 parse_link_time prog ls
304 -- There might be multiple runs of the program, so we have to collect up
305 -- all the results. Variable results like runtimes are aggregated into
306 -- a list, whereas the non-variable aspects are just kept singly.
307 parse_run_time :: String -> [String] -> Results -> Status
308 -> [(String, Results)]
309 parse_run_time _ [] _ NotDone = []
310 parse_run_time prog [] res ex = [(prog, res{run_status=ex})]
311 parse_run_time prog (l:ls) res ex =
313 Just (allocations, _, _, _, _, _, initialisation, _, mut, _, gc, _) ->
314 got_run_result allocations initialisation mut gc Nothing
315 Nothing Nothing Nothing Nothing;
319 Just (allocations, _, _, _, _, _, initialisation, _, mut, _, gc, _) ->
320 got_run_result allocations initialisation mut gc Nothing
321 Nothing Nothing Nothing Nothing;
326 Just (allocations, _, _, _, _, gc_work', _, initialisation, _, mut, _, gc, _) ->
327 got_run_result allocations initialisation mut gc
328 (Just gc_work') Nothing Nothing Nothing Nothing;
333 Just (allocations, _, _, _, _, gc_work', _, initialisation, _, mut, _, gc, _, is, mem_rs, mem_ws, cache_misses') ->
334 got_run_result allocations initialisation mut gc
335 (Just gc_work') (Just is) (Just mem_rs)
336 (Just mem_ws) (Just cache_misses');
340 case matchRegex wrong_output l of {
342 parse_run_time prog ls res (combineRunResult WrongStdout ex);
344 parse_run_time prog ls res (combineRunResult WrongStderr ex);
345 Just _ -> error "wrong_output: Can't happen";
348 case matchRegex wrong_exit_status l of {
349 Just [_wanted, got] ->
350 parse_run_time prog ls res (combineRunResult (Exit (read got)) ex);
351 Just _ -> error "wrong_exit_status: Can't happen";
354 case matchRegex out_of_heap l of {
356 parse_run_time prog ls res (combineRunResult OutOfHeap ex);
359 case matchRegex out_of_stack l of {
361 parse_run_time prog ls res (combineRunResult OutOfStack ex);
363 parse_run_time prog ls res ex;
367 got_run_result allocations initialisation mut gc gc_work' instrs' mem_rs mem_ws cache_misses'
368 = -- trace ("got_run_result: " ++ initialisation ++ ", " ++ mut ++ ", " ++ gc) $
370 time = initialisation + mut + gc
371 res' = combine2Results res
372 emptyResults{ run_time = [time],
376 allocs = Just allocations,
380 cache_misses = cache_misses',
384 parse_run_time prog ls res' Success
386 combineRunResult :: Status -> Status -> Status
387 combineRunResult OutOfHeap _ = OutOfHeap
388 combineRunResult _ OutOfHeap = OutOfHeap
389 combineRunResult OutOfStack _ = OutOfStack
390 combineRunResult _ OutOfStack = OutOfStack
391 combineRunResult (Exit e) _ = Exit e
392 combineRunResult _ (Exit e) = Exit e
393 combineRunResult exit _ = exit
395 parse_size :: String -> String -> [String] -> [(String, Results)]
396 parse_size _ _ [] = []
397 parse_size progName modName (l:ls) =
399 Nothing -> parse_size progName modName ls
400 Just (text, datas, _bss)
401 | progName == modName ->
402 [(progName,emptyResults{binary_size =
404 compile_status = Success})]
406 let ms = Map.singleton modName (text + datas)
408 [(progName,emptyResults{module_size = ms})]