2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CostCentre]{The @CostCentre@ data type}
7 {-# OPTIONS -fno-warn-incomplete-patterns #-}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
15 CostCentre(..), CcName, IsDupdCC(..), IsCafCC(..),
16 -- All abstract except to friend: ParseIface.y
20 noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS,
21 noCostCentre, noCCAttached,
22 noCCSAttached, isCurrentCCS, isSubsumedCCS, currentOrSubsumedCCS,
23 isDerivedFromCurrentCCS, maybeSingletonCCS,
26 mkUserCC, mkAutoCC, mkAllCafsCC,
27 mkSingletonCCS, dupifyCC, pushCCOnCCS,
36 cmpCostCentre -- used for removing dups in a list
41 import Module ( Module )
45 import Util ( thenCmp )
48 A Cost Centre Stack is something that can be attached to a closure.
51 - the current cost centre stack (CCCS)
52 - a pre-defined cost centre stack (there are several
53 pre-defined CCSs, see below).
59 | CurrentCCS -- Pinned on a let(rec)-bound
60 -- thunk/function/constructor, this says that the
61 -- cost centre to be attached to the object, when it
62 -- is allocated, is whatever is in the
63 -- current-cost-centre-stack register.
65 | SubsumedCCS -- Cost centre stack for top-level subsumed functions
66 -- (CAFs get an AllCafsCC).
67 -- Its execution costs get subsumed into the caller.
68 -- This guy is *only* ever pinned on static closures,
69 -- and is *never* the cost centre for an SCC construct.
71 | OverheadCCS -- We charge costs due to the profiling-system
72 -- doing its work to "overhead".
74 -- Objects whose CCS is "Overhead"
75 -- have their *allocation* charged to "overhead",
76 -- but have the current CCS put into the object
79 -- For example, if we transform "f g" to "let
80 -- g' = g in f g'" (so that something about
81 -- profiling works better...), then we charge
82 -- the *allocation* of g' to OverheadCCS, but
83 -- we put the cost-centre of the call to f
84 -- (i.e., current CCS) into the g' object. When
85 -- g' is entered, the CCS of the call
88 | DontCareCCS -- We need a CCS to stick in static closures
89 -- (for data), but we *don't* expect them to
90 -- accumulate any costs. But we still need
91 -- the placeholder. This CCS is it.
93 | PushCC CostCentre CostCentreStack
94 -- These are used during code generation as the CCSs
95 -- attached to closures. A PushCC never appears as
96 -- the argument to an _scc_.
98 -- The tail (2nd argument) is either NoCCS, indicating
99 -- a staticly allocated CCS, or CurrentCCS indicating
100 -- a dynamically created CCS. We only support
101 -- statically allocated *singleton* CCSs at the
102 -- moment, for the purposes of initialising the CCS
105 deriving (Eq, Ord) -- needed for Ord on CLabel
108 A Cost Centre is the argument of an _scc_ expression.
112 = NoCostCentre -- Having this constructor avoids having
113 -- to use "Maybe CostCentre" all the time.
116 cc_name :: CcName, -- Name of the cost centre itself
117 cc_mod :: Module, -- Name of module defining this CC.
118 cc_is_dupd :: IsDupdCC, -- see below
119 cc_is_caf :: IsCafCC -- see below
123 cc_mod :: Module -- Name of module defining this CC.
126 type CcName = FastString
129 = OriginalCC -- This says how the CC is *used*. Saying that
130 | DupdCC -- it is DupdCC doesn't make it a different
131 -- CC, just that it a sub-expression which has
132 -- been moved ("dupd") into a different scope.
134 -- The point about a dupd SCC is that we don't
135 -- count entries to it, because it's not the
138 -- In the papers, it's called "SCCsub",
139 -- i.e. SCCsub CC == SCC DupdCC,
140 -- but we are trying to avoid confusion between
141 -- "subd" and "subsumed". So we call the former
144 data IsCafCC = CafCC | NotCafCC
146 -- synonym for triple which describes the cost centre info in the generated
147 -- code for a module.
149 = ( [CostCentre] -- local cost-centres that need to be decl'd
150 , [CostCentre] -- "extern" cost-centres
151 , [CostCentreStack] -- pre-defined "singleton" cost centre stacks
155 WILL: Would there be any merit to recording ``I am now using a
156 cost-centre from another module''? I don't know if this would help a
157 user; it might be interesting to us to know how much computation is
158 being moved across module boundaries.
160 SIMON: Maybe later...
163 noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS :: CostCentreStack
166 subsumedCCS = SubsumedCCS
167 currentCCS = CurrentCCS
168 overheadCCS = OverheadCCS
169 dontCareCCS = DontCareCCS
171 noCostCentre :: CostCentre
172 noCostCentre = NoCostCentre
175 Predicates on Cost-Centre Stacks
178 noCCSAttached :: CostCentreStack -> Bool
179 noCCSAttached NoCCS = True
180 noCCSAttached _ = False
182 noCCAttached :: CostCentre -> Bool
183 noCCAttached NoCostCentre = True
184 noCCAttached _ = False
186 isCurrentCCS :: CostCentreStack -> Bool
187 isCurrentCCS CurrentCCS = True
188 isCurrentCCS _ = False
190 isSubsumedCCS :: CostCentreStack -> Bool
191 isSubsumedCCS SubsumedCCS = True
192 isSubsumedCCS _ = False
194 isCafCCS :: CostCentreStack -> Bool
195 isCafCCS (PushCC cc NoCCS) = isCafCC cc
198 isDerivedFromCurrentCCS :: CostCentreStack -> Bool
199 isDerivedFromCurrentCCS CurrentCCS = True
200 isDerivedFromCurrentCCS (PushCC _ ccs) = isDerivedFromCurrentCCS ccs
201 isDerivedFromCurrentCCS _ = False
203 currentOrSubsumedCCS :: CostCentreStack -> Bool
204 currentOrSubsumedCCS SubsumedCCS = True
205 currentOrSubsumedCCS CurrentCCS = True
206 currentOrSubsumedCCS _ = False
208 maybeSingletonCCS :: CostCentreStack -> Maybe CostCentre
209 maybeSingletonCCS (PushCC cc NoCCS) = Just cc
210 maybeSingletonCCS _ = Nothing
213 Building cost centres
216 mkUserCC :: FastString -> Module -> CostCentre
218 = NormalCC { cc_name = cc_name, cc_mod = mod,
219 cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
222 mkAutoCC :: Id -> Module -> IsCafCC -> CostCentre
223 mkAutoCC id mod is_caf
224 = NormalCC { cc_name = str, cc_mod = mod,
225 cc_is_dupd = OriginalCC, cc_is_caf = is_caf
229 -- beware: we might be making an auto CC for a compiler-generated
230 -- thing (like a CAF when -caf-all is on), so include the uniq.
231 -- See bug #249, tests prof001, prof002
232 str | isSystemName name = mkFastString (showSDoc (ppr name))
233 | otherwise = occNameFS (getOccName id)
235 mkAllCafsCC :: Module -> CostCentre
236 mkAllCafsCC m = AllCafsCC { cc_mod = m }
240 mkSingletonCCS :: CostCentre -> CostCentreStack
241 mkSingletonCCS cc = pushCCOnCCS cc NoCCS
243 pushCCOnCCS :: CostCentre -> CostCentreStack -> CostCentreStack
246 dupifyCC :: CostCentre -> CostCentre
247 dupifyCC cc = cc {cc_is_dupd = DupdCC}
249 isCafCC, isDupdCC :: CostCentre -> Bool
251 isCafCC (AllCafsCC {}) = True
252 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
255 isDupdCC (NormalCC {cc_is_dupd = DupdCC}) = True
258 isSccCountCostCentre :: CostCentre -> Bool
259 -- Is this a cost-centre which records scc counts
262 isSccCountCostCentre NoCostCentre = panic "isSccCount:NoCostCentre"
264 isSccCountCostCentre cc | isCafCC cc = False
265 | isDupdCC cc = False
268 sccAbleCostCentre :: CostCentre -> Bool
269 -- Is this a cost-centre which can be sccd ?
272 sccAbleCostCentre NoCostCentre = panic "sccAbleCC:NoCostCentre"
274 sccAbleCostCentre cc | isCafCC cc = False
277 ccFromThisModule :: CostCentre -> Module -> Bool
278 ccFromThisModule cc m = cc_mod cc == m
282 instance Eq CostCentre where
283 c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
285 instance Ord CostCentre where
286 compare = cmpCostCentre
288 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
290 cmpCostCentre (AllCafsCC {cc_mod = m1}) (AllCafsCC {cc_mod = m2}) = m1 `compare` m2
292 cmpCostCentre (NormalCC {cc_name = n1, cc_mod = m1, cc_is_caf = c1})
293 (NormalCC {cc_name = n2, cc_mod = m2, cc_is_caf = c2})
294 -- first key is module name, then we use "kinds" (which include
295 -- names) and finally the caf flag
296 = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
298 cmpCostCentre other_1 other_2
300 tag1 = tag_CC other_1
301 tag2 = tag_CC other_2
303 if tag1 <# tag2 then LT else GT
305 tag_CC (NormalCC {}) = _ILIT(1)
306 tag_CC (AllCafsCC {}) = _ILIT(2)
308 -- TODO: swap order of IsCafCC, add deriving Ord
309 cmp_caf :: IsCafCC -> IsCafCC -> Ordering
310 cmp_caf NotCafCC CafCC = LT
311 cmp_caf NotCafCC NotCafCC = EQ
312 cmp_caf CafCC CafCC = EQ
313 cmp_caf CafCC NotCafCC = GT
315 decomposeCCS :: CostCentreStack -> ([CostCentre],CostCentreStack)
316 decomposeCCS (PushCC cc ccs) = (cc:more, ccs')
317 where (more,ccs') = decomposeCCS ccs
318 decomposeCCS ccs = ([],ccs)
321 -----------------------------------------------------------------------------
322 Printing Cost Centre Stacks.
324 The outputable instance for CostCentreStack prints the CCS as a C
327 NOTE: Not all cost centres are suitable for using in a static
328 initializer. In particular, the PushCC forms where the tail is CCCS
329 may only be used in inline C code because they expand to a
330 non-constant C expression.
333 instance Outputable CostCentreStack where
334 ppr NoCCS = ptext (sLit "NO_CCS")
335 ppr CurrentCCS = ptext (sLit "CCCS")
336 ppr OverheadCCS = ptext (sLit "CCS_OVERHEAD")
337 ppr DontCareCCS = ptext (sLit "CCS_DONT_CARE")
338 ppr SubsumedCCS = ptext (sLit "CCS_SUBSUMED")
339 ppr (PushCC cc NoCCS) = ppr cc <> ptext (sLit "_ccs")
340 ppr (PushCC cc ccs) = ptext (sLit "PushCostCentre") <>
341 parens (ppr ccs <> comma <>
342 parens(ptext (sLit "void *")) <> ppr cc)
345 -----------------------------------------------------------------------------
346 Printing Cost Centres.
348 There are several different ways in which we might want to print a
351 - the name of the cost centre, for profiling output (a C string)
352 - the label, i.e. C label for cost centre in .hc file.
353 - the debugging name, for output in -ddump things
354 - the interface name, for printing in _scc_ exprs in iface files.
356 The last 3 are derived from costCentreStr below. The first is given
360 instance Outputable CostCentre where
361 ppr cc = getPprStyle $ \ sty ->
363 then ppCostCentreLbl cc
364 else text (costCentreUserName cc)
366 -- Printing in an interface file or in Core generally
367 pprCostCentreCore :: CostCentre -> SDoc
368 pprCostCentreCore (AllCafsCC {cc_mod = m})
369 = text "__sccC" <+> braces (ppr m)
370 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m,
371 cc_is_caf = caf, cc_is_dupd = dup})
372 = text "__scc" <+> braces (hsep [
379 pp_dup :: IsDupdCC -> SDoc
380 pp_dup DupdCC = char '!'
383 pp_caf :: IsCafCC -> SDoc
384 pp_caf CafCC = text "__C"
387 -- Printing as a C label
388 ppCostCentreLbl :: CostCentre -> SDoc
389 ppCostCentreLbl (NoCostCentre) = text "NONE_cc"
390 ppCostCentreLbl (AllCafsCC {cc_mod = m}) = ppr m <> text "_CAFs_cc"
391 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf})
392 = ppr m <> char '_' <> ftext (zEncodeFS n) <>
393 text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
395 -- This is the name to go in the user-displayed string,
396 -- recorded in the cost centre declaration
397 costCentreUserName :: CostCentre -> String
398 costCentreUserName (NoCostCentre) = "NO_CC"
399 costCentreUserName (AllCafsCC {}) = "CAF"
400 costCentreUserName (NormalCC {cc_name = name, cc_is_caf = is_caf})
401 = case is_caf of { CafCC -> "CAF:"; _ -> "" } ++ unpackFS name