[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / glafp-utils / nofib-analyse / Main.hs
index ad1a7ab..c073eab 100644 (file)
@@ -1,5 +1,5 @@
 -----------------------------------------------------------------------------
--- $Id: Main.hs,v 1.1 1999/11/12 11:54:17 simonmar Exp $
+-- $Id: Main.hs,v 1.8 2002/09/18 12:36:39 simonmar Exp $
 
 -- (c) Simon Marlow 1997-1999
 -----------------------------------------------------------------------------
@@ -9,9 +9,10 @@ module Main where
 import GenUtils
 import Printf
 import Slurp
-import DataHtml
 import CmdLine
 
+import Html hiding ((!))
+import qualified Html ((!))
 import GlaExts
 import FiniteMap
 import GetOpt
@@ -22,6 +23,8 @@ import Array
 import System
 import List
 
+(<!) = (Html.!)
+
 -----------------------------------------------------------------------------
 -- Top level stuff
 
@@ -44,6 +47,10 @@ main = do
        then die "Can't produce both ASCII and HTML"
        else do
 
+ if devs && nodevs
+       then die "Can't both display and hide deviations"
+       else do
+
  results <- parse_logs other_args
 
  let column_headings = map (reverse . takeWhile (/= '/') . reverse) other_args
@@ -92,6 +99,7 @@ per_prog_result_tab =
        , SpecP "Instructions" "instrs" instrs run_status always_ok
        , SpecP "Memory Reads" "mem-reads" mem_reads run_status always_ok
        , SpecP "Memory Writes" "mem-writes" mem_writes run_status always_ok
+       , SpecP "Cache Misses" "cache-misses" cache_misses run_status always_ok
        ]
 
 per_module_result_tab =
@@ -108,39 +116,39 @@ time_ok t = t > tooquick_threshold
 -----------------------------------------------------------------------------
 -- HTML page generation
 
+--htmlPage :: Results -> [String] -> Html
 htmlPage results args
-   =  header [] (theTitle [] (htmlStr "NoFib Results"))
-         +++ bar []
+   =  header << thetitle << reportTitle
+         +++ hr
+          +++ h1 << reportTitle
          +++ gen_menu
-         +++ bar []
-         +++ body [] (gen_tables results args)
+         +++ hr
+         +++ body (gen_tables results args)
 
-gen_menu = ul [] (foldr1 (+++) (map (li [] +++)
-       (map (prog_menu_item)   per_prog_result_tab
-      ++ map (module_menu_item) per_module_result_tab)))
+gen_menu = unordList (map (prog_menu_item) per_prog_result_tab
+                     ++ map (module_menu_item) per_module_result_tab)
 
-prog_menu_item (SpecP name anc _ _ _) = anchor [href ('#':anc)] (htmlStr name)
-module_menu_item (SpecM name anc _ _) = anchor [href ('#':anc)] (htmlStr name)
+prog_menu_item (SpecP name anc _ _ _) = anchor <! [href ('#':anc)] << name
+module_menu_item (SpecM name anc _ _) = anchor <! [href ('#':anc)] << name
 
 gen_tables results args =
   foldr1 (+++) (map (htmlGenProgTable results args) per_prog_result_tab)
   +++ foldr1 (+++) (map (htmlGenModTable results args) per_module_result_tab)
 
 htmlGenProgTable results args (SpecP title anc get_result get_status result_ok)
-  =   sectHeading title anc 
-  +++ font [size 1] (
-         mkTable (htmlShowResults results args get_result get_status result_ok))
-  +++ bar []
+  =   sectHeading title anc
+  +++ font <! [size "1"]
+       << mkTable (htmlShowResults results args get_result get_status result_ok)
+  +++ hr
 
 htmlGenModTable results args (SpecM title anc get_result result_ok)
   =   sectHeading title anc 
-  +++ font [size 1] (
-         mkTable (htmlShowMultiResults results args get_result result_ok))
-  +++ bar []
+  +++ font <![size "1"] 
+        << mkTable (htmlShowMultiResults results args get_result result_ok)
+  +++ hr
 
 sectHeading :: String -> String -> Html
-sectHeading s nm
-       = h2 [] (anchor [name nm] (htmlStr s))
+sectHeading s nm = h2 << anchor <! [name nm] << s
 
 htmlShowResults 
     :: Result a
@@ -153,14 +161,17 @@ htmlShowResults
 
 htmlShowResults (r:rs) ss f stat result_ok
   =   tabHeader ss
-  +/+ foldr1 (+/+) (zipWith tableRow [1..] results_per_prog)
-  +/+ tableRow (-1) ("Average", geometric_means)
+  </> aboves (zipWith tableRow [1..] results_per_prog)
+  </> aboves ((if nodevs then []
+                         else [tableRow (-1) ("-1 s.d.", lows),
+                               tableRow (-1) ("+1 s.d.", highs)])
+                    ++ [tableRow (-1) ("Average", gms)])
  where
        -- results_per_prog :: [ (String,[BoxValue a]) ]
        results_per_prog = map (calc_result rs f stat result_ok) (fmToList r)
        
-       results_per_run = transpose (map snd results_per_prog)
-       geometric_means = map calc_gm results_per_run
+       results_per_run  = transpose (map snd results_per_prog)
+       (lows,gms,highs) = unzip3 (map calc_gmsd results_per_run)
 
 htmlShowMultiResults
     :: Result a
@@ -172,37 +183,49 @@ htmlShowMultiResults
 
 htmlShowMultiResults (r:rs) ss f result_ok =
        multiTabHeader ss 
-        +/+ foldr1 (+/+) (map show_results_for_prog base_results)
+        </> aboves (map show_results_for_prog results_per_prog_mod_run)
+         </> aboves ((if nodevs then []
+                                      else [td << bold << "-1 s.d."
+                                            <-> tableRow (-1) ("", lows),
+                                            td << bold << "+1 s.d."
+                                            <-> tableRow (-1) ("", highs)])
+                           ++ [td << bold << "Average"
+                               <-> tableRow (-1) ("", gms)])
 
   where
        base_results = fmToList r :: [(String,Results)]
 
-       show_results_for_prog (prog,r) =
-           cellHtml [valign "top"] (bold [] (htmlStr prog))
-           +-+ (if null base then
-                  cellHtml [] (htmlStr "(no modules compiled)")
-                else
-                  foldr1 (+/+) (map (show_one_result fms) base))
+        -- results_per_prog_mod_run :: [(String,[(String,[BoxValue a])])]
+        results_per_prog_mod_run = map get_results_for_prog base_results
+
+        -- get_results_for_prog :: (String,Results) -> (String,[BoxValue a])
+        get_results_for_prog (prog,r) = (prog, map get_results_for_mod (fmToList (f r)))
 
-         where
-           base = fmToList (f r)
-           fms = map (get_results_for prog) rs
+           where fms = map get_run_results rs
 
-       get_results_for prog m = case lookupFM m prog of
-                                  Nothing -> emptyFM
-                                  Just r -> f r
+                 get_run_results fm = case lookupFM fm prog of
+                                        Nothing  -> emptyFM
+                                        Just res -> f res
 
-       show_one_result other_results (id,attribute) = 
-               tableRow 0 (
-                       calc_result other_results Just (const Success) 
-                               result_ok (id,attribute) 
-               )
+                 get_results_for_mod (id,attr) = calc_result fms Just (const Success)
+                                                             result_ok (id,attr)
+
+        show_results_for_prog (prog,mrs) =
+           td <! [valign "top"] << bold << prog
+           <-> (if null mrs then
+                  td << "(no modules compiled)"
+                else
+                  toHtml (aboves (map (tableRow 0) mrs)))
+
+        results_per_run  = transpose [xs | (_,mods) <- results_per_prog_mod_run,
+                                           (_,xs) <- mods]
+        (lows,gms,highs) = unzip3 (map calc_gmsd results_per_run)
 
 tableRow :: Result a => Int -> (String, [BoxValue a]) -> HtmlTable
 tableRow row_no (prog, results)
-       =   cellHtml [bgcolor left_column_color] (htmlStr prog)
-       +-+ foldr1 (+-+) (map (cellHtml [align "right", clr] 
-                              . htmlStr . show_box) results)
+       =   td <! [bgcolor left_column_color] << prog
+       <-> besides (map (\s -> td <! [align "right", clr] << show_box s) 
+                               results)
   where clr | row_no < 0  = bgcolor average_row_color
            | even row_no = bgcolor even_row_color
            | otherwise   = bgcolor odd_row_color
@@ -227,20 +250,18 @@ findBest stuff@(Result base : rest)
 -}
 
 logHeaders ss
-  = foldr1 (+-+) (map (\s -> cellHtml [align "right", width "100"] 
-       (bold [] (htmlStr s))) ss)
+  = besides (map (\s -> (td <! [align "right", width "100"] << bold << s)) ss)
 
-mkTable :: HtmlTable -> Html
-mkTable = renderTable [cellspacing 0, cellpadding 0, border 0]
+mkTable t = table <! [cellspacing 0, cellpadding 0, border 0] << t
 
 tabHeader ss
-  =   cellHtml [align "left", width "100"] (bold [] (htmlStr "Program"))
-  +-+ logHeaders ss
+  =   (td <! [align "left", width "100"] << bold << "Program") 
+  <-> logHeaders ss
 
 multiTabHeader ss
-  =   cellHtml [align "left", width "100"] (bold [] (htmlStr "Program"))
-  +-+ cellHtml [align "left", width "100"] (bold [] (htmlStr "Module"))
-  +-+ logHeaders ss
+  =   (td <! [align "left", width "100"] << bold << "Program")
+  <-> (td <! [align "left", width "100"] << bold << "Module")
+  <-> logHeaders ss
 
 -- Calculate a color ranging from bright blue for -100% to bright red for +100%.
 
@@ -260,7 +281,9 @@ hexDig i | i > 10 = chr (i-10 + ord 'a')
 -- ASCII page generation
 
 asciiPage results args =
-  ( interleave "\n\n" (map (asciiGenProgTable results args) per_prog_result_tab)
+  ( str reportTitle
+  . str "\n\n"
+  . interleave "\n\n" (map (asciiGenProgTable results args) per_prog_result_tab)
   . str "\n"
   . interleave "\n\n" (map (asciiGenModTable results args)  per_module_result_tab)
   ) "\n"
@@ -294,14 +317,19 @@ ascii_show_results
 ascii_show_results (r:rs) ss f stat result_ok
        = ascii_header ss
        . interleave "\n" (map show_per_prog_results results_per_prog)
+        . if nodevs then id
+                    else   str "\n"
+                        . show_per_prog_results ("-1 s.d.",lows)
+                        . str "\n"
+                        . show_per_prog_results ("+1 s.d.",highs)
        . str "\n"
-       . show_per_prog_results ("Average",geometric_means)
+       . show_per_prog_results ("Average",gms)
  where
        -- results_per_prog :: [ (String,[BoxValue a]) ]
        results_per_prog = map (calc_result rs f stat result_ok) (fmToList r)
        
-       results_per_run = transpose (map snd results_per_prog)
-       geometric_means = map calc_gm results_per_run
+       results_per_run  = transpose (map snd results_per_prog)
+        (lows,gms,highs) = unzip3 (map calc_gmsd results_per_run)
 
 ascii_show_multi_results
    :: Result a
@@ -313,30 +341,43 @@ ascii_show_multi_results
 
 ascii_show_multi_results (r:rs) ss f result_ok
        = ascii_header ss 
-       . interleave "\n" (map show_results_for_prog base_results)
+       . interleave "\n" (map show_results_for_prog results_per_prog_mod_run)
+       . str "\n"
+        . if nodevs then id
+                    else   str "\n"
+                        . show_per_prog_results ("-1 s.d.",lows)
+                        . str "\n"
+                        . show_per_prog_results ("+1 s.d.",highs)
+       . str "\n"
+       . show_per_prog_results ("Average",gms)
   where
        base_results = fmToList r :: [(String,Results)]
 
-       show_results_for_prog (prog,r) =
-             str ("\n"++prog++"\n")
-           . (if null base then
-                str "(no modules compiled)\n"
-              else
-                interleave "\n" (map (show_one_result fms) base))
+        -- results_per_prog_mod_run :: [(String,[(String,[BoxValue a])])]
+        results_per_prog_mod_run = map get_results_for_prog base_results
 
-         where
-           base = fmToList (f r)
-           fms = map (get_results_for prog) rs
+        -- get_results_for_prog :: (String,Results) -> (String,[BoxValue a])
+        get_results_for_prog (prog,r) = (prog, map get_results_for_mod (fmToList (f r)))
 
-       get_results_for prog m = case lookupFM m prog of
-                                  Nothing -> emptyFM
-                                  Just r -> f r
+           where fms = map get_run_results rs
 
-       show_one_result other_results (id,attribute) = 
-               show_per_prog_results (
-                       calc_result other_results Just (const Success) 
-                               result_ok (id,attribute) 
-               )
+                 get_run_results fm = case lookupFM fm prog of
+                                        Nothing  -> emptyFM
+                                        Just res -> f res
+
+                 get_results_for_mod (id,attr) = calc_result fms Just (const Success)
+                                                             result_ok (id,attr)
+
+        show_results_for_prog (prog,mrs) =
+             str ("\n"++prog++"\n")
+           . (if null mrs then
+                  str "(no modules compiled)\n"
+                else
+                  interleave "\n" (map show_per_prog_results mrs))
+
+        results_per_run  = transpose [xs | (_,mods) <- results_per_prog_mod_run,
+                                           (_,xs) <- mods]
+        (lows,gms,highs) = unzip3 (map calc_gmsd results_per_run)
 
 show_per_prog_results :: Result a => (String, [BoxValue a]) -> ShowS
 show_per_prog_results (prog,results)
@@ -355,7 +396,7 @@ class Num a => Result a where
 
 instance Result Int where
        convert_to_percentage 0 size = 100
-       convert_to_percentage base size = (fromInt size / fromInt base) * 100
+       convert_to_percentage base size = (fromIntegral size / fromIntegral base) * 100
 
        result_to_string n = show (n `div` 1024) ++ "k"
 
@@ -414,7 +455,7 @@ show_box (Percentage p) = show_pcntage p
 show_box (Result a)     = result_to_string a
 
 -----------------------------------------------------------------------------
--- Calculating geometric means
+-- Calculating geometric means and standard deviations
 
 {-
 This is done using the log method, to avoid needing really large
@@ -427,16 +468,43 @@ which is equivalent to
        e ^ ( (log a1 + ... + log an) / n )
 
 where log is the natural logarithm function.
+
+Similarly, to compute the geometric standard deviation we compute the
+deviation of each log, take the root-mean-square, and take the
+exponential again:
+
+        e ^ sqrt( ( sqr(log a1 - lbar) + ... + sqr(log an - lbar) ) / n )
+
+where lbar is the mean log,
+
+        (log a1 + ... + log an) / n
+
+This is a *factor*: i.e., the 1 s.d. points are (gm/sdf,gm*sdf); do
+not subtract 100 from gm before performing this calculation.
+
+We therefore return a (low, mean, high) triple.
+
 -}
 
-calc_gm :: [BoxValue a] -> BoxValue Float
-calc_gm xs 
-  | null percentages = RunFailed NotDone
-  | otherwise        = Percentage (exp (sum (map log percentages) / 
-                                         fromInt (length percentages)))
+calc_gmsd :: [BoxValue a] -> (BoxValue Float, BoxValue Float, BoxValue Float)
+calc_gmsd xs 
+  | null percentages = (RunFailed NotDone, RunFailed NotDone, RunFailed NotDone)
+  | otherwise        = let sqr x = x * x
+                           len   = fromIntegral (length percentages)
+                           logs  = map log percentages
+                           lbar  = sum logs / len
+                           devs  = map (sqr . (lbar-)) logs
+                           dbar  = sum devs / len
+                           gm    = exp lbar
+                           sdf   = exp (sqrt dbar)
+                       in
+                       (Percentage (gm/sdf),
+                        Percentage gm,
+                        Percentage (gm*sdf))
  where
-  percentages = [ f | Percentage f <- xs, f /= 0.0 ]
+  percentages = [ if f < 5 then 5 else f | Percentage f <- xs ]
        -- can't do log(0.0), so exclude zeros
+        -- small values have inordinate effects so cap at -95%.
 
 -----------------------------------------------------------------------------
 -- Generic stuff for results generation