1 ---------------------------------------------------------
2 -- The main program for the hpc-add tool, part of HPC.
4 ---------------------------------------------------------
6 module HpcCombine (sum_plugin,combine_plugin,map_plugin) where
14 import qualified HpcSet as Set
15 import qualified HpcMap as Map
16 import System.Environment
18 ------------------------------------------------------------------------------
25 sum_plugin = Plugin { name = "sum"
26 , usage = "[OPTION] .. <TIX_FILE> [<TIX_FILE> [<TIX_FILE> ..]]"
27 , options = sum_options
28 , summary = "Sum multiple .tix files in a single .tix file"
29 , implementation = sum_main
30 , init_flags = default_flags
31 , final_flags = default_final_flags
42 combine_plugin = Plugin { name = "combine"
43 , usage = "[OPTION] .. <TIX_FILE> <TIX_FILE>"
44 , options = combine_options
45 , summary = "Combine two .tix files in a single .tix file"
46 , implementation = combine_main
47 , init_flags = default_flags
48 , final_flags = default_final_flags
59 map_plugin = Plugin { name = "map"
60 , usage = "[OPTION] .. <TIX_FILE> "
61 , options = map_options
62 , summary = "Map a function over a single .tix file"
63 , implementation = map_main
64 , init_flags = default_flags
65 , final_flags = default_final_flags
68 ------------------------------------------------------------------------------
70 sum_main :: Flags -> [String] -> IO ()
71 sum_main flags [] = hpcError sum_plugin $ "no .tix file specified"
72 sum_main flags (first_file:more_files) = do
73 Just tix <- readTix first_file
75 tix' <- foldM (mergeTixFile flags (+))
79 case outputFile flags of
80 "-" -> putStrLn (show tix')
81 out -> writeTix out tix'
83 combine_main :: Flags -> [String] -> IO ()
84 combine_main flags [first_file,second_file] = do
85 let f = theCombineFun (combineFun flags)
87 Just tix1 <- readTix first_file
88 Just tix2 <- readTix second_file
90 let tix = mergeTix (mergeModule flags)
92 (filterTix flags tix1)
93 (filterTix flags tix2)
95 case outputFile flags of
96 "-" -> putStrLn (show tix)
97 out -> writeTix out tix
98 combine_main flags [] = hpcError sum_plugin $ "need exactly two .tix files to combine"
100 map_main :: Flags -> [String] -> IO ()
101 map_main flags [first_file] = do
102 let f = thePostFun (postFun flags)
104 Just tix <- readTix first_file
106 let (Tix inside_tix) = filterTix flags tix
107 let tix' = Tix [ TixModule m p i (map f t)
108 | TixModule m p i t <- inside_tix
111 case outputFile flags of
112 "-" -> putStrLn (show tix')
113 out -> writeTix out tix'
114 map_main flags [] = hpcError sum_plugin $ "no .tix file specified"
115 map_main flags _ = hpcError sum_plugin $ "to many .tix files specified"
117 mergeTixFile :: Flags -> (Integer -> Integer -> Integer) -> Tix -> String -> IO Tix
118 mergeTixFile flags fn tix file_name = do
119 Just new_tix <- readTix file_name
120 return $! strict $ mergeTix (mergeModule flags) fn tix (filterTix flags new_tix)
122 -- could allow different numbering on the module info,
123 -- as long as the total is the same; will require normalization.
126 -> (Integer -> Integer -> Integer) -> Tix -> Tix -> Tix
130 [ case (Map.lookup m fm1,Map.lookup m fm2) of
131 -- todo, revisit the semantics of this combination
132 (Just (TixModule _ hash1 len1 tix1),Just (TixModule _ hash2 len2 tix2))
134 || length tix1 /= length tix2
135 || len1 /= length tix1
136 || len2 /= length tix2
137 -> error $ "mismatched in module " ++ m
139 TixModule m hash1 len1 (zipWith f tix1 tix2)
144 _ -> error "impossible"
145 | m <- Set.toList (theMergeFun modComb m1s m2s)
148 m1s = Set.fromList $ map tixModuleName t1
149 m2s = Set.fromList $ map tixModuleName t2
151 fm1 = Map.fromList [ (tixModuleName tix,tix)
154 fm2 = Map.fromList [ (tixModuleName tix,tix)
159 -- What I would give for a hyperstrict :-)
160 -- This makes things about 100 times faster.
164 instance Strict Integer where
167 instance Strict Int where
170 instance Strict Hash where -- should be fine, because Hash is a newtype round an Int
173 instance Strict Char where
176 instance Strict a => Strict [a] where
177 strict (a:as) = (((:) $! strict a) $! strict as)
180 instance (Strict a, Strict b) => Strict (a,b) where
181 strict (a,b) = (((,) $! strict a) $! strict b)
183 instance Strict Tix where
187 instance Strict TixModule where
188 strict (TixModule m1 p1 i1 t1) =
189 ((((TixModule $! strict m1) $! strict p1) $! strict i1) $! strict t1)