1 -----------------------------------------------------------------------------
2 -- $Id: Main.hs,v 1.6 2002/02/28 18:50:40 keithw Exp $
4 -- (c) Simon Marlow 1997-1999
5 -----------------------------------------------------------------------------
14 import Html hiding ((!))
15 import qualified Html ((!))
28 -----------------------------------------------------------------------------
32 die s = hPutStr stderr s >> exitWith (ExitFailure 1)
34 usageHeader = "usage: nofib-analyse [OPTION...] <logfile1> <logfile2> ..."
38 if not (null cmdline_errors) || OptHelp `elem` flags
39 then die (concat cmdline_errors ++ usageInfo usageHeader argInfo)
42 let { html = OptHTMLOutput `elem` flags;
43 ascii = OptASCIIOutput `elem` flags
47 then die "Can't produce both ASCII and HTML"
51 then die "Can't both display and hide deviations"
54 results <- parse_logs other_args
56 let column_headings = map (reverse . takeWhile (/= '/') . reverse) other_args
59 then putStr (renderHtml (htmlPage results column_headings))
60 else putStr (asciiPage results column_headings)
63 parse_logs :: [String] -> IO [ResultTable]
65 f <- hGetContents stdin
67 parse_logs log_files =
68 mapM (\f -> do h <- openFile f ReadMode
70 return (parse_log c)) log_files
72 -----------------------------------------------------------------------------
73 -- List of tables we're going to generate
75 data PerProgTableSpec =
76 forall a . Result a =>
78 String -- Name of the table
79 String -- HTML tag for the table
80 (Results -> Maybe a) -- How to get the result
81 (Results -> Status) -- How to get the status of this result
82 (a -> Bool) -- Result within reasonable limits?
84 data PerModuleTableSpec =
85 forall a . Result a =>
87 String -- Name of the table
88 String -- HTML tag for the table
89 (Results -> FiniteMap String a) -- get the module map
90 (a -> Bool) -- Result within reasonable limits?
93 [ SpecP "Binary Sizes" "binary-sizes" binary_size compile_status always_ok
94 , SpecP "Allocations" "allocations" allocs run_status always_ok
95 , SpecP "Run Time" "run-times" run_time run_status time_ok
96 , SpecP "Mutator Time" "mutator-time" mut_time run_status time_ok
97 , SpecP "GC Time" "gc-time" gc_time run_status time_ok
98 , SpecP "GC Work" "gc-work" gc_work run_status always_ok
99 , SpecP "Instructions" "instrs" instrs run_status always_ok
100 , SpecP "Memory Reads" "mem-reads" mem_reads run_status always_ok
101 , SpecP "Memory Writes" "mem-writes" mem_writes run_status always_ok
104 per_module_result_tab =
105 [ SpecM "Module Sizes" "mod-sizes" module_size always_ok
106 , SpecM "Compile Times" "compile-time" compile_time time_ok
109 always_ok :: a -> Bool
110 always_ok = const True
112 time_ok :: Float -> Bool
113 time_ok t = t > tooquick_threshold
115 -----------------------------------------------------------------------------
116 -- HTML page generation
118 --htmlPage :: Results -> [String] -> Html
119 htmlPage results args
120 = header << thetitle << reportTitle
122 +++ h1 << reportTitle
125 +++ body (gen_tables results args)
127 gen_menu = unordList (map (prog_menu_item) per_prog_result_tab
128 ++ map (module_menu_item) per_module_result_tab)
130 prog_menu_item (SpecP name anc _ _ _) = anchor <! [href ('#':anc)] << name
131 module_menu_item (SpecM name anc _ _) = anchor <! [href ('#':anc)] << name
133 gen_tables results args =
134 foldr1 (+++) (map (htmlGenProgTable results args) per_prog_result_tab)
135 +++ foldr1 (+++) (map (htmlGenModTable results args) per_module_result_tab)
137 htmlGenProgTable results args (SpecP title anc get_result get_status result_ok)
138 = sectHeading title anc
139 +++ font <! [size "1"]
140 << mkTable (htmlShowResults results args get_result get_status result_ok)
143 htmlGenModTable results args (SpecM title anc get_result result_ok)
144 = sectHeading title anc
145 +++ font <![size "1"]
146 << mkTable (htmlShowMultiResults results args get_result result_ok)
149 sectHeading :: String -> String -> Html
150 sectHeading s nm = h2 << anchor <! [name nm] << s
156 -> (Results -> Maybe a)
157 -> (Results -> Status)
161 htmlShowResults (r:rs) ss f stat result_ok
163 </> aboves (zipWith tableRow [1..] results_per_prog)
164 </> aboves ((if nodevs then []
165 else [tableRow (-1) ("-1 s.d.", lows),
166 tableRow (-1) ("+1 s.d.", highs)])
167 ++ [tableRow (-1) ("Average", gms)])
169 -- results_per_prog :: [ (String,[BoxValue a]) ]
170 results_per_prog = map (calc_result rs f stat result_ok) (fmToList r)
172 results_per_run = transpose (map snd results_per_prog)
173 (lows,gms,highs) = unzip3 (map calc_gmsd results_per_run)
179 -> (Results -> FiniteMap String a)
183 htmlShowMultiResults (r:rs) ss f result_ok =
185 </> aboves (map show_results_for_prog results_per_prog_mod_run)
186 </> aboves ((if nodevs then []
187 else [td << bold << "-1 s.d."
188 <-> tableRow (-1) ("", lows),
189 td << bold << "+1 s.d."
190 <-> tableRow (-1) ("", highs)])
191 ++ [td << bold << "Average"
192 <-> tableRow (-1) ("", gms)])
195 base_results = fmToList r :: [(String,Results)]
197 -- results_per_prog_mod_run :: [(String,[(String,[BoxValue a])])]
198 results_per_prog_mod_run = map get_results_for_prog base_results
200 -- get_results_for_prog :: (String,Results) -> (String,[BoxValue a])
201 get_results_for_prog (prog,r) = (prog, map get_results_for_mod (fmToList (f r)))
203 where fms = map get_run_results rs
205 get_run_results fm = case lookupFM fm prog of
209 get_results_for_mod (id,attr) = calc_result fms Just (const Success)
212 show_results_for_prog (prog,mrs) =
213 td <! [valign "top"] << bold << prog
214 <-> (if null mrs then
215 td << "(no modules compiled)"
217 toHtml (aboves (map (tableRow 0) mrs)))
219 results_per_run = transpose [xs | (_,mods) <- results_per_prog_mod_run,
221 (lows,gms,highs) = unzip3 (map calc_gmsd results_per_run)
223 tableRow :: Result a => Int -> (String, [BoxValue a]) -> HtmlTable
224 tableRow row_no (prog, results)
225 = td <! [bgcolor left_column_color] << prog
226 <-> besides (map (\s -> td <! [align "right", clr] << show_box s)
228 where clr | row_no < 0 = bgcolor average_row_color
229 | even row_no = bgcolor even_row_color
230 | otherwise = bgcolor odd_row_color
232 left_column_color = "#d0d0ff" -- light blue
233 odd_row_color = "#d0d0ff" -- light blue
234 even_row_color = "#f0f0ff" -- v. light blue
235 average_row_color = "#ffd0d0" -- light red
238 findBest :: Result a => [BoxValue a] -> [(Bool,BoxValue a)]
239 findBest stuff@(Result base : rest)
240 = map (\a -> (a==base, a))
242 best = snd (minimumBy (\a b -> fst a < fst b) no_pcnt_stuff
244 no_pcnt_stuff = map unPcnt stuff
246 unPcnt (r@(Percentage f) : rest) = (base * f/100, r) : unPcnt rest
247 unPcnt (r@(Result a) : rest) = (a, r) : unPcnt rest
248 unPcnt (_ : rest) = unPcnt rest
252 = besides (map (\s -> (td <! [align "right", width "100"] << bold << s)) ss)
254 mkTable t = table <! [cellspacing 0, cellpadding 0, border 0] << t
257 = (td <! [align "left", width "100"] << bold << "Program")
261 = (td <! [align "left", width "100"] << bold << "Program")
262 <-> (td <! [align "left", width "100"] << bold << "Module")
265 -- Calculate a color ranging from bright blue for -100% to bright red for +100%.
267 calcColor :: Int -> String
268 calcColor p | p >= 0 = "#" ++ (showHex red 2 "0000")
269 | otherwise = "#0000" ++ (showHex blue 2 "")
270 where red = p * 255 `div` 100
271 blue = (-p) * 255 `div` 100
273 showHex 0 f s = if f > 0 then take f (repeat '0') ++ s else s
274 showHex i f s = showHex (i `div` 16) (f-1) (hexDig (i `mod` 16) : s)
276 hexDig i | i > 10 = chr (i-10 + ord 'a')
277 | otherwise = chr (i + ord '0')
279 -----------------------------------------------------------------------------
280 -- ASCII page generation
282 asciiPage results args =
285 . interleave "\n\n" (map (asciiGenProgTable results args) per_prog_result_tab)
287 . interleave "\n\n" (map (asciiGenModTable results args) per_module_result_tab)
290 asciiGenProgTable results args (SpecP title anc get_result get_status result_ok)
293 . ascii_show_results results args get_result get_status result_ok
295 asciiGenModTable results args (SpecM title anc get_result result_ok)
298 . ascii_show_multi_results results args get_result result_ok
301 = str "\n-------------------------------------------------------------------------------\n"
302 . str (rjustify 15 "Program")
304 . foldr (.) id (map (str . rjustify fIELD_WIDTH) ss)
305 . str "\n-------------------------------------------------------------------------------\n"
311 -> (Results -> Maybe a)
312 -> (Results -> Status)
316 ascii_show_results (r:rs) ss f stat result_ok
318 . interleave "\n" (map show_per_prog_results results_per_prog)
321 . show_per_prog_results ("-1 s.d.",lows)
323 . show_per_prog_results ("+1 s.d.",highs)
325 . show_per_prog_results ("Average",gms)
327 -- results_per_prog :: [ (String,[BoxValue a]) ]
328 results_per_prog = map (calc_result rs f stat result_ok) (fmToList r)
330 results_per_run = transpose (map snd results_per_prog)
331 (lows,gms,highs) = unzip3 (map calc_gmsd results_per_run)
333 ascii_show_multi_results
337 -> (Results -> FiniteMap String a)
341 ascii_show_multi_results (r:rs) ss f result_ok
343 . interleave "\n" (map show_results_for_prog results_per_prog_mod_run)
347 . show_per_prog_results ("-1 s.d.",lows)
349 . show_per_prog_results ("+1 s.d.",highs)
351 . show_per_prog_results ("Average",gms)
353 base_results = fmToList r :: [(String,Results)]
355 -- results_per_prog_mod_run :: [(String,[(String,[BoxValue a])])]
356 results_per_prog_mod_run = map get_results_for_prog base_results
358 -- get_results_for_prog :: (String,Results) -> (String,[BoxValue a])
359 get_results_for_prog (prog,r) = (prog, map get_results_for_mod (fmToList (f r)))
361 where fms = map get_run_results rs
363 get_run_results fm = case lookupFM fm prog of
367 get_results_for_mod (id,attr) = calc_result fms Just (const Success)
370 show_results_for_prog (prog,mrs) =
371 str ("\n"++prog++"\n")
373 str "(no modules compiled)\n"
375 interleave "\n" (map show_per_prog_results mrs))
377 results_per_run = transpose [xs | (_,mods) <- results_per_prog_mod_run,
379 (lows,gms,highs) = unzip3 (map calc_gmsd results_per_run)
381 show_per_prog_results :: Result a => (String, [BoxValue a]) -> ShowS
382 show_per_prog_results (prog,results)
383 = str (rjustify 15 prog)
385 . foldr (.) id (map (str . rjustify fIELD_WIDTH . show_box) results)
387 -----------------------------------------------------------------------------
390 class Num a => Result a where
391 result_to_string :: a -> String
392 convert_to_percentage :: a -> a -> Float
394 -- We assume an Int is a size, and print it in kilobytes.
396 instance Result Int where
397 convert_to_percentage 0 size = 100
398 convert_to_percentage base size = (fromInt size / fromInt base) * 100
400 result_to_string n = show (n `div` 1024) ++ "k"
402 instance Result Integer where
403 convert_to_percentage 0 size = 100
404 convert_to_percentage base size = (fromInteger size / fromInteger base) * 100
406 result_to_string n = show (n `quot` 1024) ++ "k"
408 instance Result Float where
409 convert_to_percentage 0.0 size = 100.0
410 convert_to_percentage base size = size / base * 100
412 result_to_string = showFloat' Nothing (Just 2)
414 data BoxValue a = RunFailed Status | Percentage Float | Result a
416 -- calc_result is a nice exercise in higher-order programming...
419 => [FiniteMap String b] -- accumulated results
420 -> (b -> Maybe a) -- get a result from the b
421 -> (b -> Status) -- get a status from the b
422 -> (a -> Bool) -- is this result ok?
423 -> (String,b) -- the baseline result
424 -> (String,[BoxValue a])
426 calc_result rts get_maybe_a get_stat result_ok (prog,base_r) =
427 (prog, (just_result baseline base_stat :
430 rts' = map (\rt -> get_stuff (lookupFM rt prog)) rts
432 get_stuff Nothing = (Nothing, NotDone)
433 get_stuff (Just r) = (get_maybe_a r, get_stat r)
437 Just base | result_ok base
438 -> map (\(r,s) -> percentage r s base) rts'
440 -> map (\(r,s) -> just_result r s) rts'
443 baseline = get_maybe_a base_r
444 base_stat = get_stat base_r
446 just_result Nothing s = RunFailed s
447 just_result (Just a) s = Result a
449 percentage Nothing s base = RunFailed s
450 percentage (Just a) s base = Percentage
451 (convert_to_percentage base a)
452 show_box (RunFailed s) = show_stat s
453 show_box (Percentage p) = show_pcntage p
454 show_box (Result a) = result_to_string a
456 -----------------------------------------------------------------------------
457 -- Calculating geometric means and standard deviations
460 This is done using the log method, to avoid needing really large
461 intermediate results. The formula for a geometric mean is
463 (a1 * .... * an) ^ 1/n
465 which is equivalent to
467 e ^ ( (log a1 + ... + log an) / n )
469 where log is the natural logarithm function.
471 Similarly, to compute the geometric standard deviation we compute the
472 deviation of each log, take the root-mean-square, and take the
475 e ^ sqrt( ( sqr(log a1 - lbar) + ... + sqr(log an - lbar) ) / n )
477 where lbar is the mean log,
479 (log a1 + ... + log an) / n
481 This is a *factor*: i.e., the 1 s.d. points are (gm/sdf,gm*sdf); do
482 not subtract 100 from gm before performing this calculation.
484 We therefore return a (low, mean, high) triple.
488 calc_gmsd :: [BoxValue a] -> (BoxValue Float, BoxValue Float, BoxValue Float)
490 | null percentages = (RunFailed NotDone, RunFailed NotDone, RunFailed NotDone)
491 | otherwise = let sqr x = x * x
492 len = fromInt (length percentages)
493 logs = map log percentages
494 lbar = sum logs / len
495 devs = map (sqr . (lbar-)) logs
496 dbar = sum devs / len
498 sdf = exp (sqrt dbar)
500 (Percentage (gm/sdf),
504 percentages = [ if f < 5 then 5 else f | Percentage f <- xs ]
505 -- can't do log(0.0), so exclude zeros
506 -- small values have inordinate effects so cap at -95%.
508 -----------------------------------------------------------------------------
509 -- Generic stuff for results generation
511 show_pcntage n = show_float_signed (n-100) ++ "%"
513 show_float_signed = showFloat False False True False False Nothing (Just 2)
515 show_stat Success = "(no result)"
516 show_stat WrongStdout = "(stdout)"
517 show_stat WrongStderr = "(stderr)"
518 show_stat (Exit x) = "exit(" ++ show x ++")"
519 show_stat OutOfHeap = "(heap)"
520 show_stat OutOfStack = "(stack)"
521 show_stat NotDone = "-----"
525 interleave s = foldr1 (\a b -> a . str s . b)
527 fIELD_WIDTH = 16 :: Int
529 -----------------------------------------------------------------------------