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