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