[project @ 2000-04-20 15:34:17 by simonmar]
[ghc-hetmet.git] / ghc / compiler / profiling / CostCentre.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[CostCentre]{The @CostCentre@ data type}
5
6 \begin{code}
7 module CostCentre (
8         CostCentre(..), CcName, IsDupdCC(..), IsCafCC(..),
9                 -- All abstract except to friend: ParseIface.y
10
11         CostCentreStack,
12         noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS,
13         noCostCentre, noCCAttached,
14         noCCSAttached, isCurrentCCS,  isSubsumedCCS, currentOrSubsumedCCS,
15
16         mkUserCC, mkAutoCC, mkAllCafsCC, 
17         mkSingletonCCS, cafifyCC, dupifyCC,
18         isCafCC, isDupdCC, isEmptyCC, isCafCCS,
19         isSccCountCostCentre,
20         sccAbleCostCentre,
21         ccFromThisModule,
22
23         pprCostCentreDecl, pprCostCentreStackDecl, pprCostCentreCore,
24
25         cmpCostCentre   -- used for removing dups in a list
26     ) where
27
28 #include "HsVersions.h"
29
30 import Var              ( Id )
31 import Name             ( UserFS, EncodedFS, encodeFS, decode,
32                           getOccName, occNameFS
33                         )
34 import Module           ( Module, ModuleName, moduleName,
35                           pprModuleName, moduleNameUserString
36                         )
37 import Outputable       
38 import CStrings         ( pprStringInCStyle )
39 import Util             ( thenCmp )
40 \end{code}
41
42 A Cost Centre Stack is something that can be attached to a closure.
43 This is either:
44         
45         - the current cost centre stack (CCCS)
46         - a pre-defined cost centre stack (there are several
47           pre-defined CCSs, see below).
48
49 \begin{code}
50 data CostCentreStack
51   = NoCCS
52
53   | CurrentCCS          -- Pinned on a let(rec)-bound 
54                         -- thunk/function/constructor, this says that the 
55                         -- cost centre to be attached to the object, when it 
56                         -- is allocated, is whatever is in the 
57                         -- current-cost-centre-stack register.
58
59   | SubsumedCCS         -- Cost centre stack for top-level subsumed functions
60                         -- (CAFs get an AllCafsCC).
61                         -- Its execution costs get subsumed into the caller.
62                         -- This guy is *only* ever pinned on static closures,
63                         -- and is *never* the cost centre for an SCC construct.
64
65   | OverheadCCS         -- We charge costs due to the profiling-system
66                         -- doing its work to "overhead".
67                         --
68                         -- Objects whose CCS is "Overhead"
69                         -- have their *allocation* charged to "overhead",
70                         -- but have the current CCS put into the object
71                         -- itself.
72
73                         -- For example, if we transform "f g" to "let
74                         -- g' = g in f g'" (so that something about
75                         -- profiling works better...), then we charge
76                         -- the *allocation* of g' to OverheadCCS, but
77                         -- we put the cost-centre of the call to f
78                         -- (i.e., current CCS) into the g' object.  When
79                         -- g' is entered, the CCS of the call
80                         -- to f will be set.
81
82   | DontCareCCS         -- We need a CCS to stick in static closures
83                         -- (for data), but we *don't* expect them to
84                         -- accumulate any costs.  But we still need
85                         -- the placeholder.  This CCS is it.
86
87   | SingletonCCS CostCentre
88                         -- This is primarily for CAF cost centres, which
89                         -- are attached to top-level thunks right at the
90                         -- end of STG processing, before code generation.
91                         -- Hence, a CAF cost centre never appears as the
92                         -- argument of an _scc_.
93                         -- Also, we generate these singleton CCSs statically
94                         -- as part of code generation.
95
96   deriving (Eq, Ord)    -- needed for Ord on CLabel
97 \end{code}
98
99 A Cost Centre is the argument of an _scc_ expression.
100  
101 \begin{code}
102 data CostCentre
103   = NoCostCentre        -- Having this constructor avoids having
104                         -- to use "Maybe CostCentre" all the time.
105
106   | NormalCC {  
107                 cc_name :: CcName,      -- Name of the cost centre itself
108                 cc_mod  :: ModuleName,  -- Name of module defining this CC.
109                 cc_is_dupd :: IsDupdCC, -- see below
110                 cc_is_caf  :: IsCafCC   -- see below
111     }
112
113   | AllCafsCC { 
114                 cc_mod  :: ModuleName   -- Name of module defining this CC.
115     }
116
117 type CcName = EncodedFS
118
119 data IsDupdCC
120   = OriginalCC  -- This says how the CC is *used*.  Saying that
121   | DupdCC              -- it is DupdCC doesn't make it a different
122                         -- CC, just that it a sub-expression which has
123                         -- been moved ("dupd") into a different scope.
124                         --
125                         -- The point about a dupd SCC is that we don't
126                         -- count entries to it, because it's not the
127                         -- "original" one.
128                         --
129                         -- In the papers, it's called "SCCsub",
130                         --  i.e. SCCsub CC == SCC DupdCC,
131                         -- but we are trying to avoid confusion between
132                         -- "subd" and "subsumed".  So we call the former
133                         -- "dupd".
134
135 data IsCafCC = CafCC | NotCafCC
136 \end{code}
137
138 WILL: Would there be any merit to recording ``I am now using a
139 cost-centre from another module''?  I don't know if this would help a
140 user; it might be interesting to us to know how much computation is
141 being moved across module boundaries.
142
143 SIMON: Maybe later...
144
145 \begin{code}
146
147 noCCS                   = NoCCS
148 subsumedCCS             = SubsumedCCS
149 currentCCS              = CurrentCCS
150 overheadCCS             = OverheadCCS
151 dontCareCCS             = DontCareCCS
152
153 noCostCentre            = NoCostCentre
154 \end{code}
155
156 Predicates on Cost-Centre Stacks
157
158 \begin{code}
159 noCCSAttached NoCCS                     = True
160 noCCSAttached _                         = False
161
162 noCCAttached NoCostCentre               = True
163 noCCAttached _                          = False
164
165 isCurrentCCS CurrentCCS                 = True
166 isCurrentCCS _                          = False
167
168 isSubsumedCCS SubsumedCCS               = True
169 isSubsumedCCS _                         = False
170
171 isCafCCS (SingletonCCS cc)              = isCafCC cc
172 isCafCCS _                              = False
173
174 currentOrSubsumedCCS SubsumedCCS        = True
175 currentOrSubsumedCCS CurrentCCS         = True
176 currentOrSubsumedCCS _                  = False
177 \end{code}
178
179 Building cost centres
180
181 \begin{code}
182 mkUserCC :: UserFS -> Module -> CostCentre
183
184 mkUserCC cc_name mod
185   = NormalCC { cc_name = encodeFS cc_name, cc_mod =  moduleName mod,
186                cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
187     }
188
189 mkAutoCC :: Id -> Module -> IsCafCC -> CostCentre
190
191 mkAutoCC id mod is_caf
192   = NormalCC { cc_name = occNameFS (getOccName id), cc_mod =  moduleName mod,
193                cc_is_dupd = OriginalCC, cc_is_caf = is_caf
194     }
195
196 mkAllCafsCC m = AllCafsCC  { cc_mod = moduleName m }
197
198 mkSingletonCCS :: CostCentre -> CostCentreStack
199 mkSingletonCCS cc = SingletonCCS cc
200
201 cafifyCC, dupifyCC  :: CostCentre -> CostCentre
202
203 cafifyCC cc@(NormalCC {cc_is_caf = is_caf})
204   = ASSERT(not_a_caf_already is_caf)
205     cc {cc_is_caf = CafCC}
206   where
207     not_a_caf_already CafCC = False
208     not_a_caf_already _       = True
209 cafifyCC cc = pprPanic "cafifyCC" (ppr cc)
210
211 dupifyCC cc = cc {cc_is_dupd = DupdCC}
212
213 isEmptyCC, isCafCC, isDupdCC :: CostCentre -> Bool
214
215 isEmptyCC (NoCostCentre)                = True
216 isEmptyCC _                             = False
217
218 isCafCC (AllCafsCC {})                   = True
219 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
220 isCafCC _                                = False
221
222 isDupdCC (NormalCC   {cc_is_dupd = DupdCC}) = True
223 isDupdCC _                                   = False
224
225 isSccCountCostCentre :: CostCentre -> Bool
226   -- Is this a cost-centre which records scc counts
227
228 #if DEBUG
229 isSccCountCostCentre NoCostCentre  = panic "isSccCount:NoCostCentre"
230 #endif
231 isSccCountCostCentre cc | isCafCC cc  = False
232                         | isDupdCC cc = False
233                         | otherwise   = True
234
235 sccAbleCostCentre :: CostCentre -> Bool
236   -- Is this a cost-centre which can be sccd ?
237
238 #if DEBUG
239 sccAbleCostCentre NoCostCentre  = panic "sccAbleCC:NoCostCentre"
240 #endif
241 sccAbleCostCentre cc | isCafCC cc = False
242                      | otherwise  = True
243
244 ccFromThisModule :: CostCentre -> Module -> Bool
245 ccFromThisModule cc m = cc_mod cc == moduleName m
246 \end{code}
247
248 \begin{code}
249 instance Eq CostCentre where
250         c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
251
252 instance Ord CostCentre where
253         compare = cmpCostCentre
254
255 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
256
257 cmpCostCentre (AllCafsCC  {cc_mod = m1}) (AllCafsCC  {cc_mod = m2}) = m1 `compare` m2
258
259 cmpCostCentre (NormalCC {cc_name = n1, cc_mod =  m1, cc_is_caf = c1}) 
260               (NormalCC {cc_name = n2, cc_mod =  m2, cc_is_caf = c2}) 
261     -- first key is module name, then we use "kinds" (which include
262     -- names) and finally the caf flag
263   = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
264
265 cmpCostCentre other_1 other_2
266   = let
267         tag1 = tag_CC other_1
268         tag2 = tag_CC other_2
269     in
270     if tag1 _LT_ tag2 then LT else GT
271   where
272     tag_CC (NormalCC   {}) = (ILIT(1) :: FAST_INT)
273     tag_CC (AllCafsCC  {}) = ILIT(2)
274
275 cmp_caf NotCafCC CafCC     = LT
276 cmp_caf NotCafCC NotCafCC  = EQ
277 cmp_caf CafCC    CafCC     = EQ
278 cmp_caf CafCC    NotCafCC  = GT
279 \end{code}
280
281 -----------------------------------------------------------------------------
282 Printing Cost Centre Stacks.
283
284 There are two ways to print a CCS:
285
286         - for debugging output (i.e. -ddump-whatever),
287         - as a C label
288
289 \begin{code}
290 instance Outputable CostCentreStack where
291   ppr ccs = case ccs of
292                 NoCCS           -> ptext SLIT("NO_CCS")
293                 CurrentCCS      -> ptext SLIT("CCCS")
294                 OverheadCCS     -> ptext SLIT("CCS_OVERHEAD")
295                 DontCareCCS     -> ptext SLIT("CCS_DONT_CARE")
296                 SubsumedCCS     -> ptext SLIT("CCS_SUBSUMED")
297                 SingletonCCS cc -> ppr cc <> ptext SLIT("_ccs")
298
299 pprCostCentreStackDecl :: CostCentreStack -> SDoc
300 pprCostCentreStackDecl ccs@(SingletonCCS cc)
301   = hcat [ ptext SLIT("CCS_DECLARE"), char '(',
302            ppr ccs,             comma,  -- better be codeStyle
303            ppCostCentreLbl cc,  comma,
304            empty,       -- Now always externally visible
305            text ");"
306          ]
307
308 pprCostCentreStackDecl ccs 
309   = pprPanic "pprCostCentreStackDecl: " (ppr ccs)
310 \end{code}
311
312 -----------------------------------------------------------------------------
313 Printing Cost Centres.
314
315 There are several different ways in which we might want to print a
316 cost centre:
317
318         - the name of the cost centre, for profiling output (a C string)
319         - the label, i.e. C label for cost centre in .hc file.
320         - the debugging name, for output in -ddump things
321         - the interface name, for printing in _scc_ exprs in iface files.
322
323 The last 3 are derived from costCentreStr below.  The first is given
324 by costCentreName.
325
326 \begin{code}
327 instance Outputable CostCentre where
328   ppr cc = getPprStyle $ \ sty ->
329            if codeStyle sty
330            then ppCostCentreLbl cc
331            else text (costCentreUserName cc)
332
333 -- Printing in an interface file or in Core generally
334 pprCostCentreCore (AllCafsCC {cc_mod = m})
335   = text "__sccC" <+> braces (pprModuleName m)
336 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m,
337                              cc_is_caf = caf, cc_is_dupd = dup})
338   = text "__scc" <+> braces (hsep [
339         ptext n,
340         pprModuleName m,        
341         pp_dup dup,
342         pp_caf caf
343     ])
344
345 pp_dup DupdCC = char '!'
346 pp_dup other   = empty
347
348 pp_caf CafCC = text "__C"
349 pp_caf other   = empty
350
351
352 -- Printing as a C label
353 ppCostCentreLbl (NoCostCentre)            = text "NONE_cc"
354 ppCostCentreLbl (AllCafsCC  {cc_mod = m}) = pprModuleName m <> text "_CAFs_cc"
355 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf}) 
356   = pprModuleName m <> ptext n <> 
357         text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
358
359 -- This is the name to go in the user-displayed string, 
360 -- recorded in the cost centre declaration
361 costCentreUserName (NoCostCentre)  = "NO_CC"
362 costCentreUserName (AllCafsCC {})  = "CAF"
363 costCentreUserName cc@(NormalCC {cc_name = name, cc_is_caf = is_caf})
364   =  case is_caf of { CafCC -> "CAF:";   _ -> "" } ++ decode (_UNPK_ name)
365 \end{code}
366
367 Cost Centre Declarations
368
369 \begin{code}
370 #ifdef DEBUG
371 pprCostCentreDecl is_local (NoCostCentre)
372   = panic "pprCostCentreDecl: no cost centre!"
373 #endif
374 pprCostCentreDecl is_local cc
375   = if is_local then
376         hcat [
377             ptext SLIT("CC_DECLARE"),char '(',
378             cc_ident,                                                   comma,
379             pprStringInCStyle (costCentreUserName cc),                  comma,
380             pprStringInCStyle (moduleNameUserString mod_name),          comma,
381             ptext is_subsumed,                                          comma,
382             empty,      -- Now always externally visible
383             text ");"]
384     else
385         hcat [ ptext SLIT("CC_EXTERN"),char '(', cc_ident, text ");" ]
386   where
387     cc_ident    = ppCostCentreLbl cc
388     mod_name    = cc_mod cc
389     is_subsumed = ccSubsumed cc
390
391 ccSubsumed :: CostCentre -> FAST_STRING         -- subsumed value
392 ccSubsumed cc | isCafCC  cc = SLIT("CC_IS_CAF")
393               | otherwise   = SLIT("CC_IS_BORING")
394 \end{code}