1 -----------------------------------------------------------------------------
3 -- (c) Simon Marlow 1997-2005
5 -----------------------------------------------------------------------------
7 module Slurp (Status(..), Results(..), ResultTable, parse_log) where
11 import qualified Data.Map as Map
17 -----------------------------------------------------------------------------
18 -- This is the structure into which we collect our results:
20 type ResultTable = Map String Results
31 data Results = Results {
32 compile_time :: Map String Float,
33 module_size :: Map String Int,
34 binary_size :: Maybe Int,
35 link_time :: Maybe Float,
38 instrs :: Maybe Integer,
39 mem_reads :: Maybe Integer,
40 mem_writes :: Maybe Integer,
41 cache_misses :: Maybe Integer,
42 gc_work :: Maybe Integer,
44 allocs :: Maybe Integer,
46 compile_status :: Status
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.
81 banner_re = mkRegex "^==nofib==[ \t]+([A-Za-z0-9_-]+):[ \t]+(size of|time to link|time to run|time to compile)[ \t]+([A-Za-z0-9_-]+)(\\.o)?[ \t]+follows"
84 This regexp for the output of "time" works on FreeBSD, other versions
85 of "time" will need different regexps.
88 time_re = mkRegex "^[ \t]*([0-9.]+)[ \t]+real[ \t]+([0-9.]+)[ \t]+user[ \t]+([0-9.]+)[ \t]+sys[ \t]*$"
90 time_gnu17_re = mkRegex "^[ \t]*([0-9.]+)user[ \t]+([0-9.]+)system[ \t]+([0-9.:]+)elapsed"
91 -- /usr/bin/time --version reports: GNU time 1.7
92 -- notice the order is different, and the elapsed time is [hh:]mm:ss.s
94 size_re = mkRegex "^[ \t]*([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)"
97 <<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>>
99 = (bytes, gcs, avg_resid, max_resid, samples, gc_work,
100 init, init_elapsed, mut, mut_elapsed, gc, gc_elapsed)
102 ghc1_re = pre GHC 4.02
103 ghc2_re = GHC 4.02 (includes "xxM in use")
104 ghc3_re = GHC 4.03 (includes "xxxx bytes GC work")
107 ghc1_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>>"
109 ghc2_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>>"
111 ghc3_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>>"
113 ghc4_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>>"
115 wrong_exit_status = mkRegex "^\\**[ \t]*expected exit status ([0-9]+) not seen ; got ([0-9]+)"
117 wrong_output = mkRegex "^expected (stdout|stderr) not matched by reality$"
119 out_of_heap = mkRegex "^\\+ Heap exhausted;$"
121 out_of_stack = mkRegex "^\\+ Stack space overflow:"
123 parse_log :: String -> ResultTable
125 = combine_results -- collate information
127 . map process_chunk -- get information from each chunk
128 . tail -- first chunk is junk
129 . chunk_log [] [] -- break at banner lines
132 combine_results :: [(String,Results)] -> Map String Results
133 combine_results = foldr f Map.empty
135 f (prog,results) fm = Map.insertWith (flip combine2Results) prog results fm
139 Results{ compile_time = ct1, link_time = lt1,
141 run_time = rt1, mut_time = mt1,
142 instrs = is1, mem_reads = mr1, mem_writes = mw1,
144 gc_time = gt1, gc_work = gw1,
145 binary_size = bs1, allocs = al1,
146 run_status = rs1, compile_status = cs1 }
147 Results{ compile_time = ct2, link_time = lt2,
149 run_time = rt2, mut_time = mt2,
150 instrs = is2, mem_reads = mr2, mem_writes = mw2,
152 gc_time = gt2, gc_work = gw2,
153 binary_size = bs2, allocs = al2,
154 run_status = rs2, compile_status = cs2 }
155 = Results{ compile_time = Map.unionWith (flip const) ct1 ct2,
156 module_size = Map.unionWith (flip const) ms1 ms2,
157 link_time = combMaybes lt1 lt2,
158 run_time = rt1 ++ rt2,
159 mut_time = mt1 ++ mt2,
160 instrs = combMaybes is1 is2,
161 mem_reads = combMaybes mr1 mr2,
162 mem_writes = combMaybes mw1 mw2,
163 cache_misses = combMaybes cm1 cm2,
164 gc_time = gt1 ++ gt2,
165 gc_work = combMaybes gw1 gw2,
166 binary_size = combMaybes bs1 bs2,
167 allocs = combMaybes al1 al2,
168 run_status = combStatus rs1 rs2,
169 compile_status = combStatus cs1 cs2 }
171 combMaybes m1 m2 = case maybeToList m1 ++ maybeToList m2 of
175 combStatus NotDone x = x
176 combStatus x NotDone = x
179 chunk_log :: [String] -> [String] -> [String] -> [([String],[String])]
180 chunk_log header chunk [] = [(header,chunk)]
181 chunk_log header chunk (l:ls) =
182 case matchRegex banner_re l of
183 Nothing -> chunk_log header (l:chunk) ls
184 Just stuff -> (header,chunk) : chunk_log stuff [] ls
186 process_chunk :: ([String],[String]) -> [(String,Results)]
187 process_chunk (prog : what : mod : _, chk) =
189 "time to compile" -> parse_compile_time prog mod chk
190 "time to run" -> parse_run_time prog (reverse chk) emptyResults NotDone
191 "time to link" -> parse_link_time prog chk
192 "size of" -> parse_size prog mod chk
193 _ -> error ("process_chunk: "++what)
195 parse_compile_time prog mod [] = []
196 parse_compile_time prog mod (l:ls) =
197 case matchRegex time_re l of {
198 Just (real:user:system:_) ->
199 let ct = Map.singleton mod (read user)
201 [(prog,emptyResults{compile_time = ct})];
204 case matchRegex time_gnu17_re l of {
205 Just (user:system:elapsed:_) ->
206 let ct = Map.singleton mod (read user)
208 [(prog,emptyResults{compile_time = ct})];
211 case matchRegex ghc1_re l of {
212 Just (allocs:_:_:_:_:init:_:mut:_:gc:_) ->
216 time = (read init + read_mut + read_gc) :: Float
217 ct = Map.singleton mod time
219 [(prog,emptyResults{compile_time = ct})];
222 case matchRegex ghc2_re l of {
223 Just (allocs:_:_:_:_:_:init:_:mut:_:gc:_) ->
227 time = (read init + read_mut + read_gc) :: Float
228 ct = Map.singleton mod time
230 [(prog,emptyResults{compile_time = ct})];
233 case matchRegex ghc3_re l of {
234 Just (allocs:_:_:_:_:_:_:init:_:mut:_:gc:_) ->
238 time = (read init + read_mut + read_gc) :: Float
239 ct = Map.singleton mod time
241 [(prog,emptyResults{compile_time = ct})];
244 case matchRegex ghc4_re l of {
245 Just (allocs:_:_:_:_:_:_:init:_:mut:_:gc:_:_:_:_) ->
249 time = (read init + read_mut + read_gc) :: Float
250 ct = Map.singleton mod time
252 [(prog,emptyResults{compile_time = ct})];
255 parse_compile_time prog mod ls
258 parse_link_time prog [] = []
259 parse_link_time prog (l:ls) =
260 case matchRegex time_re l of {
261 Just (real:user:system:_) ->
262 [(prog,emptyResults{link_time = Just (read user)})];
265 case matchRegex time_gnu17_re l of {
266 Just (user:system:elapsed:_) ->
267 [(prog,emptyResults{link_time = Just (read user)})];
270 parse_link_time prog ls
274 -- There might be multiple runs of the program, so we have to collect up
275 -- all the results. Variable results like runtimes are aggregated into
276 -- a list, whereas the non-variable aspects are just kept singly.
277 parse_run_time prog [] res NotDone = []
278 parse_run_time prog [] res ex = [(prog, res{run_status=ex})]
279 parse_run_time prog (l:ls) res ex =
280 case matchRegex ghc1_re l of {
281 Just (allocs:_:_:_:_:init:_:mut:_:gc:_) ->
282 got_run_result allocs init mut gc Nothing
283 Nothing Nothing Nothing Nothing;
286 case matchRegex ghc2_re l of {
287 Just (allocs:_:_:_:_:_:init:_:mut:_:gc:_) ->
288 got_run_result allocs init mut gc Nothing
289 Nothing Nothing Nothing Nothing;
293 case matchRegex ghc3_re l of {
294 Just (allocs:_:_:_:_:gc_work:_:init:_:mut:_:gc:_) ->
295 got_run_result allocs init mut gc (Just (read gc_work))
296 Nothing Nothing Nothing Nothing;
300 case matchRegex ghc4_re l of {
301 Just (allocs:_:_:_:_:gc_work:_:init:_:mut:_:gc:_:is:mem_rs:mem_ws:cache_misses:_) ->
302 got_run_result allocs init mut gc (Just (read gc_work))
303 (Just (read is)) (Just (read mem_rs))
304 (Just (read mem_ws)) (Just (read cache_misses));
308 case matchRegex wrong_output l of {
310 parse_run_time prog ls res (combineRunResult WrongStdout ex);
312 parse_run_time prog ls res (combineRunResult WrongStderr ex);
315 case matchRegex wrong_exit_status l of {
316 Just (wanted:got:_) ->
317 parse_run_time prog ls res (combineRunResult (Exit (read got)) ex);
320 case matchRegex out_of_heap l of {
322 parse_run_time prog ls res (combineRunResult OutOfHeap ex);
325 case matchRegex out_of_stack l of {
327 parse_run_time prog ls res (combineRunResult OutOfStack ex);
329 parse_run_time prog ls res ex;
333 got_run_result allocs init mut gc gc_work instrs mem_rs mem_ws cache_misses
334 = -- trace ("got_run_result: " ++ init ++ ", " ++ mut ++ ", " ++ gc) $
338 time = (read init + read_mut + read_gc) :: Float
339 res' = combine2Results res
340 emptyResults{ run_time = [time],
341 mut_time = [read_mut],
344 allocs = Just (read allocs),
348 cache_misses = cache_misses,
352 parse_run_time prog ls res' Success
355 combineRunResult OutOfHeap _ = OutOfHeap
356 combineRunResult _ OutOfHeap = OutOfHeap
357 combineRunResult OutOfStack _ = OutOfStack
358 combineRunResult _ OutOfStack = OutOfStack
359 combineRunResult (Exit e) _ = Exit e
360 combineRunResult _ (Exit e) = Exit e
361 combineRunResult exit _ = exit
363 parse_size prog mod [] = []
364 parse_size prog mod (l:ls) =
365 case matchRegex size_re l of
366 Nothing -> parse_size prog mod ls
367 Just (text:datas:bss:_)
369 [(prog,emptyResults{binary_size =
370 Just (read text + read datas),
371 compile_status = Success})]
373 let ms = Map.singleton mod (read text + read datas)
375 [(prog,emptyResults{module_size = ms})]