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
39 #include "HsVersions.h"
43 import Module ( Module )
47 import Util ( thenCmp )
50 A Cost Centre Stack is something that can be attached to a closure.
53 - the current cost centre stack (CCCS)
54 - a pre-defined cost centre stack (there are several
55 pre-defined CCSs, see below).
61 | CurrentCCS -- Pinned on a let(rec)-bound
62 -- thunk/function/constructor, this says that the
63 -- cost centre to be attached to the object, when it
64 -- is allocated, is whatever is in the
65 -- current-cost-centre-stack register.
67 | SubsumedCCS -- Cost centre stack for top-level subsumed functions
68 -- (CAFs get an AllCafsCC).
69 -- Its execution costs get subsumed into the caller.
70 -- This guy is *only* ever pinned on static closures,
71 -- and is *never* the cost centre for an SCC construct.
73 | OverheadCCS -- We charge costs due to the profiling-system
74 -- doing its work to "overhead".
76 -- Objects whose CCS is "Overhead"
77 -- have their *allocation* charged to "overhead",
78 -- but have the current CCS put into the object
81 -- For example, if we transform "f g" to "let
82 -- g' = g in f g'" (so that something about
83 -- profiling works better...), then we charge
84 -- the *allocation* of g' to OverheadCCS, but
85 -- we put the cost-centre of the call to f
86 -- (i.e., current CCS) into the g' object. When
87 -- g' is entered, the CCS of the call
90 | DontCareCCS -- We need a CCS to stick in static closures
91 -- (for data), but we *don't* expect them to
92 -- accumulate any costs. But we still need
93 -- the placeholder. This CCS is it.
95 | PushCC CostCentre CostCentreStack
96 -- These are used during code generation as the CCSs
97 -- attached to closures. A PushCC never appears as
98 -- the argument to an _scc_.
100 -- The tail (2nd argument) is either NoCCS, indicating
101 -- a staticly allocated CCS, or CurrentCCS indicating
102 -- a dynamically created CCS. We only support
103 -- statically allocated *singleton* CCSs at the
104 -- moment, for the purposes of initialising the CCS
107 deriving (Eq, Ord) -- needed for Ord on CLabel
110 A Cost Centre is the argument of an _scc_ expression.
114 = NoCostCentre -- Having this constructor avoids having
115 -- to use "Maybe CostCentre" all the time.
118 cc_name :: CcName, -- Name of the cost centre itself
119 cc_mod :: Module, -- Name of module defining this CC.
120 cc_is_dupd :: IsDupdCC, -- see below
121 cc_is_caf :: IsCafCC -- see below
125 cc_mod :: Module -- Name of module defining this CC.
128 type CcName = FastString
131 = OriginalCC -- This says how the CC is *used*. Saying that
132 | DupdCC -- it is DupdCC doesn't make it a different
133 -- CC, just that it a sub-expression which has
134 -- been moved ("dupd") into a different scope.
136 -- The point about a dupd SCC is that we don't
137 -- count entries to it, because it's not the
140 -- In the papers, it's called "SCCsub",
141 -- i.e. SCCsub CC == SCC DupdCC,
142 -- but we are trying to avoid confusion between
143 -- "subd" and "subsumed". So we call the former
146 data IsCafCC = CafCC | NotCafCC
148 -- synonym for triple which describes the cost centre info in the generated
149 -- code for a module.
151 = ( [CostCentre] -- local cost-centres that need to be decl'd
152 , [CostCentre] -- "extern" cost-centres
153 , [CostCentreStack] -- pre-defined "singleton" cost centre stacks
157 WILL: Would there be any merit to recording ``I am now using a
158 cost-centre from another module''? I don't know if this would help a
159 user; it might be interesting to us to know how much computation is
160 being moved across module boundaries.
162 SIMON: Maybe later...
165 noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS :: CostCentreStack
168 subsumedCCS = SubsumedCCS
169 currentCCS = CurrentCCS
170 overheadCCS = OverheadCCS
171 dontCareCCS = DontCareCCS
173 noCostCentre :: CostCentre
174 noCostCentre = NoCostCentre
177 Predicates on Cost-Centre Stacks
180 noCCSAttached :: CostCentreStack -> Bool
181 noCCSAttached NoCCS = True
182 noCCSAttached _ = False
184 noCCAttached :: CostCentre -> Bool
185 noCCAttached NoCostCentre = True
186 noCCAttached _ = False
188 isCurrentCCS :: CostCentreStack -> Bool
189 isCurrentCCS CurrentCCS = True
190 isCurrentCCS _ = False
192 isSubsumedCCS :: CostCentreStack -> Bool
193 isSubsumedCCS SubsumedCCS = True
194 isSubsumedCCS _ = False
196 isCafCCS :: CostCentreStack -> Bool
197 isCafCCS (PushCC cc NoCCS) = isCafCC cc
200 isDerivedFromCurrentCCS :: CostCentreStack -> Bool
201 isDerivedFromCurrentCCS CurrentCCS = True
202 isDerivedFromCurrentCCS (PushCC _ ccs) = isDerivedFromCurrentCCS ccs
203 isDerivedFromCurrentCCS _ = False
205 currentOrSubsumedCCS :: CostCentreStack -> Bool
206 currentOrSubsumedCCS SubsumedCCS = True
207 currentOrSubsumedCCS CurrentCCS = True
208 currentOrSubsumedCCS _ = False
210 maybeSingletonCCS :: CostCentreStack -> Maybe CostCentre
211 maybeSingletonCCS (PushCC cc NoCCS) = Just cc
212 maybeSingletonCCS _ = Nothing
215 Building cost centres
218 mkUserCC :: FastString -> Module -> CostCentre
220 = NormalCC { cc_name = cc_name, cc_mod = mod,
221 cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
224 mkAutoCC :: Id -> Module -> IsCafCC -> CostCentre
225 mkAutoCC id mod is_caf
226 = NormalCC { cc_name = str, cc_mod = mod,
227 cc_is_dupd = OriginalCC, cc_is_caf = is_caf
231 -- beware: we might be making an auto CC for a compiler-generated
232 -- thing (like a CAF when -caf-all is on), so include the uniq.
233 -- See bug #249, tests prof001, prof002
234 str | isSystemName name = mkFastString (showSDoc (ppr name))
235 | otherwise = occNameFS (getOccName id)
237 mkAllCafsCC :: Module -> CostCentre
238 mkAllCafsCC m = AllCafsCC { cc_mod = m }
242 mkSingletonCCS :: CostCentre -> CostCentreStack
243 mkSingletonCCS cc = pushCCOnCCS cc NoCCS
245 pushCCOnCCS :: CostCentre -> CostCentreStack -> CostCentreStack
248 dupifyCC :: CostCentre -> CostCentre
249 dupifyCC cc = cc {cc_is_dupd = DupdCC}
251 isCafCC, isDupdCC :: CostCentre -> Bool
253 isCafCC (AllCafsCC {}) = True
254 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
257 isDupdCC (NormalCC {cc_is_dupd = DupdCC}) = True
260 isSccCountCostCentre :: CostCentre -> Bool
261 -- Is this a cost-centre which records scc counts
264 isSccCountCostCentre NoCostCentre = panic "isSccCount:NoCostCentre"
266 isSccCountCostCentre cc | isCafCC cc = False
267 | isDupdCC cc = False
270 sccAbleCostCentre :: CostCentre -> Bool
271 -- Is this a cost-centre which can be sccd ?
274 sccAbleCostCentre NoCostCentre = panic "sccAbleCC:NoCostCentre"
276 sccAbleCostCentre cc | isCafCC cc = False
279 ccFromThisModule :: CostCentre -> Module -> Bool
280 ccFromThisModule cc m = cc_mod cc == m
284 instance Eq CostCentre where
285 c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
287 instance Ord CostCentre where
288 compare = cmpCostCentre
290 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
292 cmpCostCentre (AllCafsCC {cc_mod = m1}) (AllCafsCC {cc_mod = m2}) = m1 `compare` m2
294 cmpCostCentre (NormalCC {cc_name = n1, cc_mod = m1, cc_is_caf = c1})
295 (NormalCC {cc_name = n2, cc_mod = m2, cc_is_caf = c2})
296 -- first key is module name, then we use "kinds" (which include
297 -- names) and finally the caf flag
298 = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
300 cmpCostCentre other_1 other_2
302 tag1 = tag_CC other_1
303 tag2 = tag_CC other_2
305 if tag1 <# tag2 then LT else GT
307 tag_CC (NormalCC {}) = _ILIT(1)
308 tag_CC (AllCafsCC {}) = _ILIT(2)
310 -- TODO: swap order of IsCafCC, add deriving Ord
311 cmp_caf :: IsCafCC -> IsCafCC -> Ordering
312 cmp_caf NotCafCC CafCC = LT
313 cmp_caf NotCafCC NotCafCC = EQ
314 cmp_caf CafCC CafCC = EQ
315 cmp_caf CafCC NotCafCC = GT
317 decomposeCCS :: CostCentreStack -> ([CostCentre],CostCentreStack)
318 decomposeCCS (PushCC cc ccs) = (cc:more, ccs')
319 where (more,ccs') = decomposeCCS ccs
320 decomposeCCS ccs = ([],ccs)
323 -----------------------------------------------------------------------------
324 Printing Cost Centre Stacks.
326 The outputable instance for CostCentreStack prints the CCS as a C
329 NOTE: Not all cost centres are suitable for using in a static
330 initializer. In particular, the PushCC forms where the tail is CCCS
331 may only be used in inline C code because they expand to a
332 non-constant C expression.
335 instance Outputable CostCentreStack where
336 ppr NoCCS = ptext SLIT("NO_CCS")
337 ppr CurrentCCS = ptext SLIT("CCCS")
338 ppr OverheadCCS = ptext SLIT("CCS_OVERHEAD")
339 ppr DontCareCCS = ptext SLIT("CCS_DONT_CARE")
340 ppr SubsumedCCS = ptext SLIT("CCS_SUBSUMED")
341 ppr (PushCC cc NoCCS) = ppr cc <> ptext SLIT("_ccs")
342 ppr (PushCC cc ccs) = ptext SLIT("PushCostCentre") <>
343 parens (ppr ccs <> comma <>
344 parens(ptext SLIT("void *")) <> ppr cc)
347 -----------------------------------------------------------------------------
348 Printing Cost Centres.
350 There are several different ways in which we might want to print a
353 - the name of the cost centre, for profiling output (a C string)
354 - the label, i.e. C label for cost centre in .hc file.
355 - the debugging name, for output in -ddump things
356 - the interface name, for printing in _scc_ exprs in iface files.
358 The last 3 are derived from costCentreStr below. The first is given
362 instance Outputable CostCentre where
363 ppr cc = getPprStyle $ \ sty ->
365 then ppCostCentreLbl cc
366 else text (costCentreUserName cc)
368 -- Printing in an interface file or in Core generally
369 pprCostCentreCore :: CostCentre -> SDoc
370 pprCostCentreCore (AllCafsCC {cc_mod = m})
371 = text "__sccC" <+> braces (ppr m)
372 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m,
373 cc_is_caf = caf, cc_is_dupd = dup})
374 = text "__scc" <+> braces (hsep [
381 pp_dup :: IsDupdCC -> SDoc
382 pp_dup DupdCC = char '!'
385 pp_caf :: IsCafCC -> SDoc
386 pp_caf CafCC = text "__C"
389 -- Printing as a C label
390 ppCostCentreLbl :: CostCentre -> SDoc
391 ppCostCentreLbl (NoCostCentre) = text "NONE_cc"
392 ppCostCentreLbl (AllCafsCC {cc_mod = m}) = ppr m <> text "_CAFs_cc"
393 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf})
394 = ppr m <> char '_' <> ftext (zEncodeFS n) <>
395 text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
397 -- This is the name to go in the user-displayed string,
398 -- recorded in the cost centre declaration
399 costCentreUserName :: CostCentre -> String
400 costCentreUserName (NoCostCentre) = "NO_CC"
401 costCentreUserName (AllCafsCC {}) = "CAF"
402 costCentreUserName (NormalCC {cc_name = name, cc_is_caf = is_caf})
403 = case is_caf of { CafCC -> "CAF:"; _ -> "" } ++ unpackFS name