[project @ 2004-11-18 16:39:53 by stolz]
[ghc-hetmet.git] / glafp-utils / nofib-analyse / Slurp.hs
1 -----------------------------------------------------------------------------
2 -- $Id: Slurp.hs,v 1.5 2004/04/02 14:28:57 simonmar Exp $
3
4 -- (c) Simon Marlow 1997-1999
5 -----------------------------------------------------------------------------
6
7 module Slurp (Status(..), Results(..), ResultTable(..), parse_log) where
8
9 import CmdLine
10 import FiniteMap
11 import RegexString
12 import Maybe
13
14 -----------------------------------------------------------------------------
15 -- This is the structure into which we collect our results:
16
17 type ResultTable = FiniteMap String Results
18
19 data Status
20         = NotDone
21         | Success
22         | OutOfHeap
23         | OutOfStack
24         | Exit Int
25         | WrongStdout
26         | WrongStderr 
27
28 data Results = Results { 
29         compile_time    :: FiniteMap String Float,
30         module_size     :: FiniteMap String Int,
31         binary_size     :: Maybe Int,
32         link_time       :: Maybe Float,
33         run_time        :: [Float],
34         mut_time        :: [Float],
35         instrs          :: Maybe Integer,
36         mem_reads       :: Maybe Integer,
37         mem_writes      :: Maybe Integer,
38         cache_misses    :: Maybe Integer,
39         gc_work         :: Maybe Integer,
40         gc_time         :: [Float],
41         allocs          :: Maybe Integer,
42         run_status      :: Status,
43         compile_status  :: Status
44         }
45
46 emptyResults = Results { 
47         compile_time    = emptyFM,
48         module_size     = emptyFM,
49         binary_size     = Nothing,
50         link_time       = Nothing,
51         run_time        = [],
52         mut_time        = [],
53         instrs          = Nothing,
54         mem_reads       = Nothing,
55         mem_writes      = Nothing,
56         cache_misses    = Nothing,
57         gc_time         = [],
58         gc_work         = Nothing,
59         allocs          = Nothing,
60         compile_status  = NotDone,
61         run_status      = NotDone
62         }
63
64 -----------------------------------------------------------------------------
65 -- Parse the log file
66
67 {-
68 Various banner lines:
69
70 ==nofib== awards: size of QSort.o follows...
71 ==nofib== banner: size of banner follows...
72 ==nofib== awards: time to link awards follows...
73 ==nofib== awards: time to run awards follows...
74 ==nofib== boyer2: time to compile Checker follows...
75 -}
76
77 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"
78
79 {-
80 This regexp for the output of "time" works on FreeBSD, other versions
81 of "time" will need different regexps.
82 -}
83
84 time_re = mkRegex "^[ \t]*([0-9.]+)[ \t]+real[ \t]+([0-9.]+)[ \t]+user[ \t]+([0-9.]+)[ \t]+sys[ \t]*$"
85
86 time_gnu17_re = mkRegex "^[ \t]*([0-9.]+)user[ \t]+([0-9.]+)system[ \t]+([0-9.:]+)elapsed"
87                 -- /usr/bin/time --version reports: GNU time 1.7
88                 -- notice the order is different, and the elapsed time is [hh:]mm:ss.s
89
90 size_re = mkRegex "^[ \t]*([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)"
91
92 {-
93 <<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>>
94
95         = (bytes, gcs, avg_resid, max_resid, samples, gc_work,
96            init, init_elapsed, mut, mut_elapsed, gc, gc_elapsed)
97
98 ghc1_re = pre GHC 4.02
99 ghc2_re = GHC 4.02 (includes "xxM in use")
100 ghc3_re = GHC 4.03 (includes "xxxx bytes GC work")
101 -}
102
103 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>>"
104
105 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>>"
106
107 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>>"
108
109 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>>"
110
111 wrong_exit_status = mkRegex "^\\**[ \t]*expected exit status ([0-9]+) not seen ; got ([0-9]+)"
112
113 wrong_output = mkRegex "^expected (stdout|stderr) not matched by reality$"
114
115 out_of_heap = mkRegex "^\\+ Heap exhausted;$"
116
117 out_of_stack = mkRegex "^\\+ Stack space overflow:"
118
119 parse_log :: String -> ResultTable
120 parse_log
121         = combine_results               -- collate information
122         . concat
123         . map process_chunk             -- get information from each chunk
124         . tail                          -- first chunk is junk
125         . chunk_log [] []               -- break at banner lines
126         . lines
127
128 combine_results :: [(String,Results)] -> FiniteMap String Results
129 combine_results = foldr f emptyFM
130  where
131         f (prog,results) fm = addToFM_C combine2Results fm prog results
132
133
134 combine2Results
135              Results{ compile_time = ct1, link_time = lt1, 
136                       module_size = ms1,
137                       run_time = rt1, mut_time = mt1, 
138                       instrs = is1, mem_reads = mr1, mem_writes = mw1,
139                       cache_misses = cm1,
140                       gc_time = gt1, gc_work = gw1,
141                       binary_size = bs1, allocs = al1, 
142                       run_status = rs1, compile_status = cs1 }
143              Results{ compile_time = ct2, link_time = lt2, 
144                       module_size = ms2,
145                       run_time = rt2, mut_time = mt2,
146                       instrs = is2, mem_reads = mr2, mem_writes = mw2,
147                       cache_misses = cm2,
148                       gc_time = gt2, gc_work = gw2,
149                       binary_size = bs2, allocs = al2, 
150                       run_status = rs2, compile_status = cs2 }
151           =  Results{ compile_time   = plusFM_C const ct1 ct2,
152                       module_size    = plusFM_C const ms1 ms2,
153                       link_time      = combMaybes lt1 lt2,
154                       run_time       = rt1 ++ rt2,
155                       mut_time       = mt1 ++ mt2,
156                       instrs         = combMaybes is1 is2,
157                       mem_reads      = combMaybes mr1 mr2,
158                       mem_writes     = combMaybes mw1 mw2,
159                       cache_misses   = combMaybes cm1 cm2,
160                       gc_time        = gt1 ++ gt2,
161                       gc_work        = combMaybes gw1 gw2,
162                       binary_size    = combMaybes bs1 bs2,
163                       allocs         = combMaybes al1 al2,
164                       run_status     = combStatus rs1 rs2,
165                       compile_status = combStatus cs1 cs2 }
166
167 combMaybes m1 m2 = case maybeToList m1 ++ maybeToList m2 of
168                         [] -> Nothing
169                         (x:_) -> Just x
170
171 combStatus NotDone x = x
172 combStatus x NotDone = x
173 combStatus x y = x
174
175 chunk_log :: [String] -> [String] -> [String] -> [([String],[String])]
176 chunk_log header chunk [] = [(header,chunk)]
177 chunk_log header chunk (l:ls) =
178         case matchRegex banner_re l of
179                 Nothing -> chunk_log header (l:chunk) ls
180                 Just stuff -> (header,chunk) : chunk_log stuff [] ls
181
182 process_chunk :: ([String],[String]) -> [(String,Results)]
183 process_chunk (prog : what : mod : _, chk) =
184  case what of
185         "time to compile" -> parse_compile_time prog mod chk
186         "time to run"     -> parse_run_time prog (reverse chk) emptyResults NotDone
187         "time to link"    -> parse_link_time prog chk
188         "size of"         -> parse_size prog mod chk
189         _                 -> error ("process_chunk: "++what)
190
191 parse_compile_time prog mod [] = []
192 parse_compile_time prog mod (l:ls) =
193         case matchRegex time_re l of {
194              Just (real:user:system:_) ->
195                 let ct  = addToFM emptyFM mod (read user)
196                 in 
197                 [(prog,emptyResults{compile_time = ct})];
198              Nothing -> 
199
200         case matchRegex time_gnu17_re l of {
201              Just (user:system:elapsed:_) ->
202                 let ct  = addToFM emptyFM mod (read user)
203                 in 
204                 [(prog,emptyResults{compile_time = ct})];
205              Nothing -> 
206
207         case matchRegex ghc1_re l of {
208             Just (allocs:_:_:_:_:init:_:mut:_:gc:_) ->
209               let 
210                   read_mut = read mut
211                   read_gc  = read gc
212                   time = (read init + read_mut + read_gc) :: Float 
213                   ct  = addToFM emptyFM mod time
214               in
215                 [(prog,emptyResults{compile_time = ct})];
216             Nothing ->
217
218         case matchRegex ghc2_re l of {
219            Just (allocs:_:_:_:_:_:init:_:mut:_:gc:_) ->
220               let 
221                   read_mut = read mut
222                   read_gc  = read gc
223                   time = (read init + read_mut + read_gc) :: Float 
224                   ct  = addToFM emptyFM mod time
225               in
226                 [(prog,emptyResults{compile_time = ct})];
227             Nothing ->
228
229         case matchRegex ghc3_re l of {
230            Just (allocs:_:_:_:_:_:_:init:_:mut:_:gc:_) ->
231               let 
232                   read_mut = read mut
233                   read_gc  = read gc
234                   time = (read init + read_mut + read_gc) :: Float 
235                   ct  = addToFM emptyFM mod time
236               in
237                 [(prog,emptyResults{compile_time = ct})];
238             Nothing ->
239
240         case matchRegex ghc4_re l of {
241            Just (allocs:_:_:_:_:_:_:init:_:mut:_:gc:_:_:_:_) ->
242               let 
243                   read_mut = read mut
244                   read_gc  = read gc
245                   time = (read init + read_mut + read_gc) :: Float 
246                   ct  = addToFM emptyFM mod time
247               in
248                 [(prog,emptyResults{compile_time = ct})];
249             Nothing ->
250
251                 parse_compile_time prog mod ls
252         }}}}}}
253
254 parse_link_time prog [] = []
255 parse_link_time prog (l:ls) =
256           case matchRegex time_re l of {
257              Just (real:user:system:_) ->
258                 [(prog,emptyResults{link_time = Just (read user)})];
259              Nothing ->
260
261           case matchRegex time_gnu17_re l of {
262              Just (user:system:elapsed:_) ->
263                 [(prog,emptyResults{link_time = Just (read user)})];
264              Nothing ->
265
266           parse_link_time prog ls
267           }}
268
269
270 -- There might be multiple runs of the program, so we have to collect up
271 -- all the results.  Variable results like runtimes are aggregated into
272 -- a list, whereas the non-variable aspects are just kept singly.
273 parse_run_time prog [] res NotDone = []
274 parse_run_time prog [] res ex = [(prog, res{run_status=ex})]
275 parse_run_time prog (l:ls) res ex =
276         case matchRegex ghc1_re l of {
277            Just (allocs:_:_:_:_:init:_:mut:_:gc:_) ->
278                 got_run_result allocs init mut gc Nothing
279                         Nothing Nothing Nothing Nothing;
280            Nothing -> 
281
282         case matchRegex ghc2_re l of {
283            Just (allocs:_:_:_:_:_:init:_:mut:_:gc:_) ->
284                 got_run_result allocs init mut gc Nothing
285                         Nothing Nothing Nothing Nothing;
286
287             Nothing ->
288         
289         case matchRegex ghc3_re l of {
290            Just (allocs:_:_:_:_:gc_work:_:init:_:mut:_:gc:_) ->
291                 got_run_result allocs init mut gc (Just (read gc_work))
292                         Nothing Nothing Nothing Nothing;
293
294             Nothing ->
295         
296         case matchRegex ghc4_re l of {
297            Just (allocs:_:_:_:_:gc_work:_:init:_:mut:_:gc:_:is:mem_rs:mem_ws:cache_misses:_) ->
298                 got_run_result allocs init mut gc (Just (read gc_work))
299                         (Just (read is)) (Just (read mem_rs))
300                         (Just (read mem_ws)) (Just (read cache_misses));
301
302             Nothing ->
303         
304         case matchRegex wrong_output l of {
305             Just ("stdout":_) -> 
306                 parse_run_time prog ls res (combineRunResult WrongStdout ex);
307             Just ("stderr":_) -> 
308                 parse_run_time prog ls res (combineRunResult WrongStderr ex);
309             Nothing ->
310                         
311         case matchRegex wrong_exit_status l of {
312             Just (wanted:got:_) -> 
313                 parse_run_time prog ls res (combineRunResult (Exit (read got)) ex);
314             Nothing -> 
315
316         case matchRegex out_of_heap l of {
317             Just _ -> 
318                 parse_run_time prog ls res (combineRunResult OutOfHeap ex);
319             Nothing -> 
320
321         case matchRegex out_of_stack l of {
322             Just _ -> 
323                 parse_run_time prog ls res (combineRunResult OutOfStack ex);
324             Nothing -> 
325                 parse_run_time prog ls res ex;
326
327         }}}}}}}}
328   where
329   got_run_result allocs init mut gc gc_work instrs mem_rs mem_ws cache_misses
330       = let 
331           read_mut = read mut
332           read_gc  = read gc
333           time = (read init + read_mut + read_gc) :: Float 
334           res' = combine2Results res
335                         emptyResults{   run_time   = [time],
336                                         mut_time   = [read_mut],
337                                         gc_time    = [read_gc],
338                                         gc_work    = gc_work,
339                                         allocs     = Just (read allocs),
340                                         instrs     = instrs,
341                                         mem_reads  = mem_rs,
342                                         mem_writes = mem_ws,
343                                         cache_misses = cache_misses,
344                                         run_status = Success 
345                                 }
346         in
347         parse_run_time prog ls res' Success
348
349
350 combineRunResult OutOfHeap  _           = OutOfHeap
351 combineRunResult _          OutOfHeap   = OutOfHeap
352 combineRunResult OutOfStack _           = OutOfStack
353 combineRunResult _          OutOfStack  = OutOfStack
354 combineRunResult (Exit e)   _           = Exit e
355 combineRunResult _          (Exit e)    = Exit e
356 combineRunResult exit       _            = exit
357
358 parse_size prog mod [] = []
359 parse_size prog mod (l:ls) =
360         case matchRegex size_re l of
361             Nothing -> parse_size prog mod ls
362             Just (text:datas:bss:_) 
363                  | prog == mod ->
364                         [(prog,emptyResults{binary_size = 
365                                               Just (read text + read datas),
366                                     compile_status = Success})]
367                  | otherwise ->
368                         let ms  = addToFM emptyFM mod (read text + read datas)
369                         in
370                         [(prog,emptyResults{module_size = ms})]
371