remove use of FiniteMap, use Text.Printf
[ghc-hetmet.git] / utils / nofib-analyse / Slurp.hs
1 -----------------------------------------------------------------------------
2 --
3 -- (c) Simon Marlow 1997-2005
4 --
5 -----------------------------------------------------------------------------
6
7 module Slurp (Status(..), Results(..), ResultTable, parse_log) where
8
9 import CmdLine
10
11 import qualified Data.Map as Map
12 import Data.Map (Map)
13 import Text.Regex
14 import Data.Maybe
15 -- import Debug.Trace
16
17 -----------------------------------------------------------------------------
18 -- This is the structure into which we collect our results:
19
20 type ResultTable = Map String Results
21
22 data Status
23         = NotDone
24         | Success
25         | OutOfHeap
26         | OutOfStack
27         | Exit Int
28         | WrongStdout
29         | WrongStderr 
30
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,
36         run_time        :: [Float],
37         mut_time        :: [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,
43         gc_time         :: [Float],
44         allocs          :: Maybe Integer,
45         run_status      :: Status,
46         compile_status  :: Status
47         }
48
49 emptyResults = Results { 
50         compile_time    = Map.empty,
51         module_size     = Map.empty,
52         binary_size     = Nothing,
53         link_time       = Nothing,
54         run_time        = [],
55         mut_time        = [],
56         instrs          = Nothing,
57         mem_reads       = Nothing,
58         mem_writes      = Nothing,
59         cache_misses    = Nothing,
60         gc_time         = [],
61         gc_work         = Nothing,
62         allocs          = Nothing,
63         compile_status  = NotDone,
64         run_status      = NotDone
65         }
66
67 -----------------------------------------------------------------------------
68 -- Parse the log file
69
70 {-
71 Various banner lines:
72
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...
78 -}
79
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"
82
83 {-
84 This regexp for the output of "time" works on FreeBSD, other versions
85 of "time" will need different regexps.
86 -}
87
88 time_re = mkRegex "^[ \t]*([0-9.]+)[ \t]+real[ \t]+([0-9.]+)[ \t]+user[ \t]+([0-9.]+)[ \t]+sys[ \t]*$"
89
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
93
94 size_re = mkRegex "^[ \t]*([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)"
95
96 {-
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>>
98
99         = (bytes, gcs, avg_resid, max_resid, samples, gc_work,
100            init, init_elapsed, mut, mut_elapsed, gc, gc_elapsed)
101
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")
105 -}
106
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>>"
108
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>>"
110
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>>"
112
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>>"
114
115 wrong_exit_status = mkRegex "^\\**[ \t]*expected exit status ([0-9]+) not seen ; got ([0-9]+)"
116
117 wrong_output = mkRegex "^expected (stdout|stderr) not matched by reality$"
118
119 out_of_heap = mkRegex "^\\+ Heap exhausted;$"
120
121 out_of_stack = mkRegex "^\\+ Stack space overflow:"
122
123 parse_log :: String -> ResultTable
124 parse_log
125         = combine_results               -- collate information
126         . concat
127         . map process_chunk             -- get information from each chunk
128         . tail                          -- first chunk is junk
129         . chunk_log [] []               -- break at banner lines
130         . lines
131
132 combine_results :: [(String,Results)] -> Map String Results
133 combine_results = foldr f Map.empty
134  where
135         f (prog,results) fm = Map.insertWith (flip combine2Results) prog results fm
136
137
138 combine2Results
139              Results{ compile_time = ct1, link_time = lt1, 
140                       module_size = ms1,
141                       run_time = rt1, mut_time = mt1, 
142                       instrs = is1, mem_reads = mr1, mem_writes = mw1,
143                       cache_misses = cm1,
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, 
148                       module_size = ms2,
149                       run_time = rt2, mut_time = mt2,
150                       instrs = is2, mem_reads = mr2, mem_writes = mw2,
151                       cache_misses = cm2,
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 }
170
171 combMaybes m1 m2 = case maybeToList m1 ++ maybeToList m2 of
172                         [] -> Nothing
173                         (x:_) -> Just x
174
175 combStatus NotDone x = x
176 combStatus x NotDone = x
177 combStatus x y = x
178
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
185
186 process_chunk :: ([String],[String]) -> [(String,Results)]
187 process_chunk (prog : what : mod : _, chk) =
188  case what of
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)
194
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)
200                 in 
201                 [(prog,emptyResults{compile_time = ct})];
202              Nothing -> 
203
204         case matchRegex time_gnu17_re l of {
205              Just (user:system:elapsed:_) ->
206                 let ct  = Map.singleton mod (read user)
207                 in 
208                 [(prog,emptyResults{compile_time = ct})];
209              Nothing -> 
210
211         case matchRegex ghc1_re l of {
212             Just (allocs:_:_:_:_:init:_:mut:_:gc:_) ->
213               let 
214                   read_mut = read mut
215                   read_gc  = read gc
216                   time = (read init + read_mut + read_gc) :: Float 
217                   ct  = Map.singleton mod time
218               in
219                 [(prog,emptyResults{compile_time = ct})];
220             Nothing ->
221
222         case matchRegex ghc2_re l of {
223            Just (allocs:_:_:_:_:_:init:_:mut:_:gc:_) ->
224               let 
225                   read_mut = read mut
226                   read_gc  = read gc
227                   time = (read init + read_mut + read_gc) :: Float 
228                   ct  = Map.singleton mod time
229               in
230                 [(prog,emptyResults{compile_time = ct})];
231             Nothing ->
232
233         case matchRegex ghc3_re l of {
234            Just (allocs:_:_:_:_:_:_:init:_:mut:_:gc:_) ->
235               let 
236                   read_mut = read mut
237                   read_gc  = read gc
238                   time = (read init + read_mut + read_gc) :: Float 
239                   ct  = Map.singleton mod time
240               in
241                 [(prog,emptyResults{compile_time = ct})];
242             Nothing ->
243
244         case matchRegex ghc4_re l of {
245            Just (allocs:_:_:_:_:_:_:init:_:mut:_:gc:_:_:_:_) ->
246               let 
247                   read_mut = read mut
248                   read_gc  = read gc
249                   time = (read init + read_mut + read_gc) :: Float 
250                   ct  = Map.singleton mod time
251               in
252                 [(prog,emptyResults{compile_time = ct})];
253             Nothing ->
254
255                 parse_compile_time prog mod ls
256         }}}}}}
257
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)})];
263              Nothing ->
264
265           case matchRegex time_gnu17_re l of {
266              Just (user:system:elapsed:_) ->
267                 [(prog,emptyResults{link_time = Just (read user)})];
268              Nothing ->
269
270           parse_link_time prog ls
271           }}
272
273
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;
284            Nothing -> 
285
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;
290
291             Nothing ->
292         
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;
297
298             Nothing ->
299         
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));
305
306             Nothing ->
307         
308         case matchRegex wrong_output l of {
309             Just ("stdout":_) -> 
310                 parse_run_time prog ls res (combineRunResult WrongStdout ex);
311             Just ("stderr":_) -> 
312                 parse_run_time prog ls res (combineRunResult WrongStderr ex);
313             Nothing ->
314                         
315         case matchRegex wrong_exit_status l of {
316             Just (wanted:got:_) -> 
317                 parse_run_time prog ls res (combineRunResult (Exit (read got)) ex);
318             Nothing -> 
319
320         case matchRegex out_of_heap l of {
321             Just _ -> 
322                 parse_run_time prog ls res (combineRunResult OutOfHeap ex);
323             Nothing -> 
324
325         case matchRegex out_of_stack l of {
326             Just _ -> 
327                 parse_run_time prog ls res (combineRunResult OutOfStack ex);
328             Nothing -> 
329                 parse_run_time prog ls res ex;
330
331         }}}}}}}}
332   where
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) $
335         let 
336           read_mut = read mut
337           read_gc  = read gc
338           time = (read init + read_mut + read_gc) :: Float 
339           res' = combine2Results res
340                         emptyResults{   run_time   = [time],
341                                         mut_time   = [read_mut],
342                                         gc_time    = [read_gc],
343                                         gc_work    = gc_work,
344                                         allocs     = Just (read allocs),
345                                         instrs     = instrs,
346                                         mem_reads  = mem_rs,
347                                         mem_writes = mem_ws,
348                                         cache_misses = cache_misses,
349                                         run_status = Success 
350                                 }
351         in
352         parse_run_time prog ls res' Success
353
354
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
362
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:_) 
368                  | prog == mod ->
369                         [(prog,emptyResults{binary_size = 
370                                               Just (read text + read datas),
371                                     compile_status = Success})]
372                  | otherwise ->
373                         let ms  = Map.singleton mod (read text + read datas)
374                         in
375                         [(prog,emptyResults{module_size = ms})]
376