2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CostCentre]{The @CostCentre@ data type}
8 CostCentre(..), CcName, IsDupdCC(..), IsCafCC(..),
9 -- All abstract except to friend: ParseIface.y
12 noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS,
13 noCostCentre, noCCAttached,
14 noCCSAttached, isCurrentCCS, isSubsumedCCS, currentOrSubsumedCCS,
16 mkUserCC, mkAutoCC, mkAllCafsCC,
17 mkSingletonCCS, cafifyCC, dupifyCC,
18 isCafCC, isDupdCC, isEmptyCC, isCafCCS,
23 pprCostCentreDecl, pprCostCentreStackDecl, pprCostCentreCore,
25 cmpCostCentre -- used for removing dups in a list
28 #include "HsVersions.h"
31 import Name ( UserFS, EncodedFS, encodeFS, decode,
34 import Module ( Module, ModuleName, moduleName,
35 pprModuleName, moduleNameUserString
38 import Util ( thenCmp )
41 A Cost Centre Stack is something that can be attached to a closure.
44 - the current cost centre stack (CCCS)
45 - a pre-defined cost centre stack (there are several
46 pre-defined CCSs, see below).
52 | CurrentCCS -- Pinned on a let(rec)-bound
53 -- thunk/function/constructor, this says that the
54 -- cost centre to be attached to the object, when it
55 -- is allocated, is whatever is in the
56 -- current-cost-centre-stack register.
58 | SubsumedCCS -- Cost centre stack for top-level subsumed functions
59 -- (CAFs get an AllCafsCC).
60 -- Its execution costs get subsumed into the caller.
61 -- This guy is *only* ever pinned on static closures,
62 -- and is *never* the cost centre for an SCC construct.
64 | OverheadCCS -- We charge costs due to the profiling-system
65 -- doing its work to "overhead".
67 -- Objects whose CCS is "Overhead"
68 -- have their *allocation* charged to "overhead",
69 -- but have the current CCS put into the object
72 -- For example, if we transform "f g" to "let
73 -- g' = g in f g'" (so that something about
74 -- profiling works better...), then we charge
75 -- the *allocation* of g' to OverheadCCS, but
76 -- we put the cost-centre of the call to f
77 -- (i.e., current CCS) into the g' object. When
78 -- g' is entered, the CCS of the call
81 | DontCareCCS -- We need a CCS to stick in static closures
82 -- (for data), but we *don't* expect them to
83 -- accumulate any costs. But we still need
84 -- the placeholder. This CCS is it.
86 | SingletonCCS CostCentre
87 -- This is primarily for CAF cost centres, which
88 -- are attached to top-level thunks right at the
89 -- end of STG processing, before code generation.
90 -- Hence, a CAF cost centre never appears as the
91 -- argument of an _scc_.
92 -- Also, we generate these singleton CCSs statically
93 -- as part of code generation.
95 deriving (Eq, Ord) -- needed for Ord on CLabel
98 A Cost Centre is the argument of an _scc_ expression.
101 type Group = FAST_STRING -- "Group" that this CC is in; eg directory
104 = NoCostCentre -- Having this constructor avoids having
105 -- to use "Maybe CostCentre" all the time.
108 cc_name :: CcName, -- Name of the cost centre itself
109 cc_mod :: ModuleName, -- Name of module defining this CC.
110 cc_grp :: Group, -- "Group" that this CC is in.
111 cc_is_dupd :: IsDupdCC, -- see below
112 cc_is_caf :: IsCafCC -- see below
116 cc_mod :: ModuleName, -- Name of module defining this CC.
117 cc_grp :: Group -- "Group" that this CC is in
118 -- Again, one "big" CAF cc per module, where all
119 -- CAF costs are attributed unless the user asked for
120 -- per-individual-CAF cost attribution.
123 type CcName = EncodedFS
126 = OriginalCC -- This says how the CC is *used*. Saying that
127 | DupdCC -- it is DupdCC doesn't make it a different
128 -- CC, just that it a sub-expression which has
129 -- been moved ("dupd") into a different scope.
131 -- The point about a dupd SCC is that we don't
132 -- count entries to it, because it's not the
135 -- In the papers, it's called "SCCsub",
136 -- i.e. SCCsub CC == SCC DupdCC,
137 -- but we are trying to avoid confusion between
138 -- "subd" and "subsumed". So we call the former
141 data IsCafCC = CafCC | NotCafCC
144 WILL: Would there be any merit to recording ``I am now using a
145 cost-centre from another module''? I don't know if this would help a
146 user; it might be interesting to us to know how much computation is
147 being moved across module boundaries.
149 SIMON: Maybe later...
154 subsumedCCS = SubsumedCCS
155 currentCCS = CurrentCCS
156 overheadCCS = OverheadCCS
157 dontCareCCS = DontCareCCS
159 noCostCentre = NoCostCentre
162 Predicates on Cost-Centre Stacks
165 noCCSAttached NoCCS = True
166 noCCSAttached _ = False
168 noCCAttached NoCostCentre = True
169 noCCAttached _ = False
171 isCurrentCCS CurrentCCS = True
172 isCurrentCCS _ = False
174 isSubsumedCCS SubsumedCCS = True
175 isSubsumedCCS _ = False
177 isCafCCS (SingletonCCS cc) = isCafCC cc
180 currentOrSubsumedCCS SubsumedCCS = True
181 currentOrSubsumedCCS CurrentCCS = True
182 currentOrSubsumedCCS _ = False
185 Building cost centres
188 mkUserCC :: UserFS -> Module -> Group -> CostCentre
190 mkUserCC cc_name mod group_name
191 = NormalCC { cc_name = encodeFS cc_name,
192 cc_mod = moduleName mod, cc_grp = group_name,
193 cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
196 mkAutoCC :: Id -> Module -> Group -> IsCafCC -> CostCentre
198 mkAutoCC id mod group_name is_caf
199 = NormalCC { cc_name = occNameFS (getOccName id),
200 cc_mod = moduleName mod, cc_grp = group_name,
201 cc_is_dupd = OriginalCC, cc_is_caf = is_caf
204 mkAllCafsCC m g = AllCafsCC { cc_mod = moduleName m, cc_grp = g }
206 mkSingletonCCS :: CostCentre -> CostCentreStack
207 mkSingletonCCS cc = SingletonCCS cc
209 cafifyCC, dupifyCC :: CostCentre -> CostCentre
211 cafifyCC cc@(NormalCC {cc_is_caf = is_caf})
212 = ASSERT(not_a_caf_already is_caf)
213 cc {cc_is_caf = CafCC}
215 not_a_caf_already CafCC = False
216 not_a_caf_already _ = True
217 cafifyCC cc = pprPanic "cafifyCC" (ppr cc)
219 dupifyCC cc = cc {cc_is_dupd = DupdCC}
221 isEmptyCC, isCafCC, isDupdCC :: CostCentre -> Bool
223 isEmptyCC (NoCostCentre) = True
226 isCafCC (AllCafsCC {}) = True
227 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
230 isDupdCC (NormalCC {cc_is_dupd = DupdCC}) = True
233 isSccCountCostCentre :: CostCentre -> Bool
234 -- Is this a cost-centre which records scc counts
237 isSccCountCostCentre NoCostCentre = panic "isSccCount:NoCostCentre"
239 isSccCountCostCentre cc | isCafCC cc = False
240 | isDupdCC cc = False
243 sccAbleCostCentre :: CostCentre -> Bool
244 -- Is this a cost-centre which can be sccd ?
247 sccAbleCostCentre NoCostCentre = panic "sccAbleCC:NoCostCentre"
249 sccAbleCostCentre cc | isCafCC cc = False
252 ccFromThisModule :: CostCentre -> Module -> Bool
253 ccFromThisModule cc m = cc_mod cc == moduleName m
257 instance Eq CostCentre where
258 c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
260 instance Ord CostCentre where
261 compare = cmpCostCentre
263 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
265 cmpCostCentre (AllCafsCC {cc_mod = m1}) (AllCafsCC {cc_mod = m2}) = m1 `compare` m2
267 cmpCostCentre (NormalCC {cc_name = n1, cc_mod = m1, cc_is_caf = c1})
268 (NormalCC {cc_name = n2, cc_mod = m2, cc_is_caf = c2})
269 -- first key is module name, then we use "kinds" (which include
270 -- names) and finally the caf flag
271 = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
273 cmpCostCentre other_1 other_2
275 tag1 = tag_CC other_1
276 tag2 = tag_CC other_2
278 if tag1 _LT_ tag2 then LT else GT
280 tag_CC (NormalCC {}) = (ILIT(1) :: FAST_INT)
281 tag_CC (AllCafsCC {}) = ILIT(2)
283 cmp_caf NotCafCC CafCC = LT
284 cmp_caf NotCafCC NotCafCC = EQ
285 cmp_caf CafCC CafCC = EQ
286 cmp_caf CafCC NotCafCC = GT
289 -----------------------------------------------------------------------------
290 Printing Cost Centre Stacks.
292 There are two ways to print a CCS:
294 - for debugging output (i.e. -ddump-whatever),
298 instance Outputable CostCentreStack where
299 ppr ccs = case ccs of
300 NoCCS -> ptext SLIT("NO_CCS")
301 CurrentCCS -> ptext SLIT("CCCS")
302 OverheadCCS -> ptext SLIT("CCS_OVERHEAD")
303 DontCareCCS -> ptext SLIT("CCS_DONTZuCARE")
304 SubsumedCCS -> ptext SLIT("CCS_SUBSUMED")
305 SingletonCCS cc -> ppr cc <> ptext SLIT("_ccs")
307 pprCostCentreStackDecl :: CostCentreStack -> SDoc
308 pprCostCentreStackDecl ccs@(SingletonCCS cc)
310 is_subsumed = ccSubsumed cc
312 hcat [ ptext SLIT("CCS_DECLARE"), char '(',
313 ppr ccs, comma, -- better be codeStyle
314 ppCostCentreLbl cc, comma,
315 ptext is_subsumed, comma,
316 empty, -- Now always externally visible
320 pprCostCentreStackDecl ccs
321 = pprPanic "pprCostCentreStackDecl: " (ppr ccs)
324 -----------------------------------------------------------------------------
325 Printing Cost Centres.
327 There are several different ways in which we might want to print a
330 - the name of the cost centre, for profiling output (a C string)
331 - the label, i.e. C label for cost centre in .hc file.
332 - the debugging name, for output in -ddump things
333 - the interface name, for printing in _scc_ exprs in iface files.
335 The last 3 are derived from costCentreStr below. The first is given
339 instance Outputable CostCentre where
340 ppr cc = getPprStyle $ \ sty ->
342 then ppCostCentreLbl cc
343 else text (costCentreUserName cc)
345 -- Printing in an interface file or in Core generally
346 pprCostCentreCore (AllCafsCC {cc_mod = m, cc_grp = g})
347 = text "__sccC" <+> braces (pprModuleName m <+> doubleQuotes (ptext g))
348 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m, cc_grp = g,
349 cc_is_caf = caf, cc_is_dupd = dup})
350 = text "__scc" <+> braces (hsep [
353 doubleQuotes (ptext g),
358 pp_dup DupdCC = char '!'
361 pp_caf CafCC = text "__C"
365 -- Printing as a C label
366 ppCostCentreLbl (NoCostCentre) = text "NONE_cc"
367 ppCostCentreLbl (AllCafsCC {cc_mod = m}) = pprModuleName m <> text "_CAFs_cc"
368 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf})
369 = pprModuleName m <> ptext n <>
370 text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
372 -- This is the name to go in the user-displayed string,
373 -- recorded in the cost centre declaration
374 costCentreUserName (NoCostCentre) = "NO_CC"
375 costCentreUserName (AllCafsCC {}) = "CAFs_in_..."
376 costCentreUserName cc@(NormalCC {cc_name = name, cc_is_caf = is_caf})
377 = case is_caf of { CafCC -> "CAF:"; _ -> "" } ++ decode (_UNPK_ name)
380 Cost Centre Declarations
384 pprCostCentreDecl is_local (NoCostCentre)
385 = panic "pprCostCentreDecl: no cost centre!"
387 pprCostCentreDecl is_local cc
390 ptext SLIT("CC_DECLARE"),char '(',
392 doubleQuotes (text (costCentreUserName cc)), comma,
393 doubleQuotes (text (moduleNameUserString mod_name)), comma,
394 doubleQuotes (ptext grp_name), comma,
395 ptext is_subsumed, comma,
396 empty, -- Now always externally visible
399 hcat [ ptext SLIT("CC_EXTERN"),char '(', cc_ident, text ");" ]
401 cc_ident = ppCostCentreLbl cc
404 is_subsumed = ccSubsumed cc
406 ccSubsumed :: CostCentre -> FAST_STRING -- subsumed value
407 ccSubsumed cc | isCafCC cc = SLIT("CC_IS_CAF")
408 | otherwise = SLIT("CC_IS_BORING")