[project @ 2004-11-26 16:19:45 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         CollectedCCs,
13         noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS,
14         noCostCentre, noCCAttached,
15         noCCSAttached, isCurrentCCS,  isSubsumedCCS, currentOrSubsumedCCS,
16         isDerivedFromCurrentCCS, maybeSingletonCCS,
17         decomposeCCS,
18
19         mkUserCC, mkAutoCC, mkAllCafsCC, 
20         mkSingletonCCS, dupifyCC, pushCCOnCCS,
21         isCafCCS, isCafCC,
22         isSccCountCostCentre,
23         sccAbleCostCentre,
24         ccFromThisModule,
25
26         pprCostCentreCore,
27         costCentreUserName,
28
29         cmpCostCentre   -- used for removing dups in a list
30     ) where
31
32 #include "HsVersions.h"
33
34 import Var              ( Id )
35 import Name             ( UserFS, EncodedFS, encodeFS, decode,
36                           getOccName, occNameFS
37                         )
38 import Module           ( Module )
39 import Outputable       
40 import FastTypes
41 import FastString
42 import Util             ( thenCmp )
43 \end{code}
44
45 A Cost Centre Stack is something that can be attached to a closure.
46 This is either:
47         
48         - the current cost centre stack (CCCS)
49         - a pre-defined cost centre stack (there are several
50           pre-defined CCSs, see below).
51
52 \begin{code}
53 data CostCentreStack
54   = NoCCS
55
56   | CurrentCCS          -- Pinned on a let(rec)-bound 
57                         -- thunk/function/constructor, this says that the 
58                         -- cost centre to be attached to the object, when it 
59                         -- is allocated, is whatever is in the 
60                         -- current-cost-centre-stack register.
61
62   | SubsumedCCS         -- Cost centre stack for top-level subsumed functions
63                         -- (CAFs get an AllCafsCC).
64                         -- Its execution costs get subsumed into the caller.
65                         -- This guy is *only* ever pinned on static closures,
66                         -- and is *never* the cost centre for an SCC construct.
67
68   | OverheadCCS         -- We charge costs due to the profiling-system
69                         -- doing its work to "overhead".
70                         --
71                         -- Objects whose CCS is "Overhead"
72                         -- have their *allocation* charged to "overhead",
73                         -- but have the current CCS put into the object
74                         -- itself.
75
76                         -- For example, if we transform "f g" to "let
77                         -- g' = g in f g'" (so that something about
78                         -- profiling works better...), then we charge
79                         -- the *allocation* of g' to OverheadCCS, but
80                         -- we put the cost-centre of the call to f
81                         -- (i.e., current CCS) into the g' object.  When
82                         -- g' is entered, the CCS of the call
83                         -- to f will be set.
84
85   | DontCareCCS         -- We need a CCS to stick in static closures
86                         -- (for data), but we *don't* expect them to
87                         -- accumulate any costs.  But we still need
88                         -- the placeholder.  This CCS is it.
89
90   | PushCC CostCentre CostCentreStack
91                 -- These are used during code generation as the CCSs
92                 -- attached to closures.  A PushCC never appears as
93                 -- the argument to an _scc_.
94                 --
95                 -- The tail (2nd argument) is either NoCCS, indicating
96                 -- a staticly allocated CCS, or CurrentCCS indicating
97                 -- a dynamically created CCS.  We only support
98                 -- statically allocated *singleton* CCSs at the
99                 -- moment, for the purposes of initialising the CCS
100                 -- field of a CAF.
101
102   deriving (Eq, Ord)    -- needed for Ord on CLabel
103 \end{code}
104
105 A Cost Centre is the argument of an _scc_ expression.
106  
107 \begin{code}
108 data CostCentre
109   = NoCostCentre        -- Having this constructor avoids having
110                         -- to use "Maybe CostCentre" all the time.
111
112   | NormalCC {  
113                 cc_name :: CcName,      -- Name of the cost centre itself
114                 cc_mod  :: Module,      -- Name of module defining this CC.
115                 cc_is_dupd :: IsDupdCC, -- see below
116                 cc_is_caf  :: IsCafCC   -- see below
117     }
118
119   | AllCafsCC { 
120                 cc_mod  :: Module       -- Name of module defining this CC.
121     }
122
123 type CcName = EncodedFS
124
125 data IsDupdCC
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.
130                         --
131                         -- The point about a dupd SCC is that we don't
132                         -- count entries to it, because it's not the
133                         -- "original" one.
134                         --
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
139                         -- "dupd".
140
141 data IsCafCC = CafCC | NotCafCC
142
143 -- synonym for triple which describes the cost centre info in the generated
144 -- code for a module.
145 type CollectedCCs
146   = ( [CostCentre]       -- local cost-centres that need to be decl'd
147     , [CostCentre]       -- "extern" cost-centres
148     , [CostCentreStack]  -- pre-defined "singleton" cost centre stacks
149     )
150 \end{code}
151
152 WILL: Would there be any merit to recording ``I am now using a
153 cost-centre from another module''?  I don't know if this would help a
154 user; it might be interesting to us to know how much computation is
155 being moved across module boundaries.
156
157 SIMON: Maybe later...
158
159 \begin{code}
160
161 noCCS                   = NoCCS
162 subsumedCCS             = SubsumedCCS
163 currentCCS              = CurrentCCS
164 overheadCCS             = OverheadCCS
165 dontCareCCS             = DontCareCCS
166
167 noCostCentre            = NoCostCentre
168 \end{code}
169
170 Predicates on Cost-Centre Stacks
171
172 \begin{code}
173 noCCSAttached NoCCS                     = True
174 noCCSAttached _                         = False
175
176 noCCAttached NoCostCentre               = True
177 noCCAttached _                          = False
178
179 isCurrentCCS CurrentCCS                 = True
180 isCurrentCCS _                          = False
181
182 isSubsumedCCS SubsumedCCS               = True
183 isSubsumedCCS _                         = False
184
185 isCafCCS (PushCC cc NoCCS)              = isCafCC cc
186 isCafCCS _                              = False
187
188 isDerivedFromCurrentCCS CurrentCCS      = True
189 isDerivedFromCurrentCCS (PushCC _ ccs)  = isDerivedFromCurrentCCS ccs
190 isDerivedFromCurrentCCS _               = False
191
192 currentOrSubsumedCCS SubsumedCCS        = True
193 currentOrSubsumedCCS CurrentCCS         = True
194 currentOrSubsumedCCS _                  = False
195
196 maybeSingletonCCS (PushCC cc NoCCS)     = Just cc
197 maybeSingletonCCS _                     = Nothing
198 \end{code}
199
200 Building cost centres
201
202 \begin{code}
203 mkUserCC :: UserFS -> Module -> CostCentre
204 mkUserCC cc_name mod
205   = NormalCC { cc_name = encodeFS cc_name, cc_mod =  mod,
206                cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
207     }
208
209 mkAutoCC :: Id -> Module -> IsCafCC -> CostCentre
210 mkAutoCC id mod is_caf
211   = NormalCC { cc_name = occNameFS (getOccName id), cc_mod =  mod,
212                cc_is_dupd = OriginalCC, cc_is_caf = is_caf
213     }
214
215 mkAllCafsCC m = AllCafsCC  { cc_mod = m }
216
217
218
219 mkSingletonCCS :: CostCentre -> CostCentreStack
220 mkSingletonCCS cc = pushCCOnCCS cc NoCCS
221
222 pushCCOnCCS :: CostCentre -> CostCentreStack -> CostCentreStack
223 pushCCOnCCS = PushCC
224
225 dupifyCC cc = cc {cc_is_dupd = DupdCC}
226
227 isCafCC, isDupdCC :: CostCentre -> Bool
228
229 isCafCC (AllCafsCC {})                   = True
230 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
231 isCafCC _                                = False
232
233 isDupdCC (NormalCC   {cc_is_dupd = DupdCC}) = True
234 isDupdCC _                                   = False
235
236 isSccCountCostCentre :: CostCentre -> Bool
237   -- Is this a cost-centre which records scc counts
238
239 #if DEBUG
240 isSccCountCostCentre NoCostCentre  = panic "isSccCount:NoCostCentre"
241 #endif
242 isSccCountCostCentre cc | isCafCC cc  = False
243                         | isDupdCC cc = False
244                         | otherwise   = True
245
246 sccAbleCostCentre :: CostCentre -> Bool
247   -- Is this a cost-centre which can be sccd ?
248
249 #if DEBUG
250 sccAbleCostCentre NoCostCentre  = panic "sccAbleCC:NoCostCentre"
251 #endif
252 sccAbleCostCentre cc | isCafCC cc = False
253                      | otherwise  = True
254
255 ccFromThisModule :: CostCentre -> Module -> Bool
256 ccFromThisModule cc m = cc_mod cc == m
257 \end{code}
258
259 \begin{code}
260 instance Eq CostCentre where
261         c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
262
263 instance Ord CostCentre where
264         compare = cmpCostCentre
265
266 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
267
268 cmpCostCentre (AllCafsCC  {cc_mod = m1}) (AllCafsCC  {cc_mod = m2}) = m1 `compare` m2
269
270 cmpCostCentre (NormalCC {cc_name = n1, cc_mod =  m1, cc_is_caf = c1}) 
271               (NormalCC {cc_name = n2, cc_mod =  m2, cc_is_caf = c2}) 
272     -- first key is module name, then we use "kinds" (which include
273     -- names) and finally the caf flag
274   = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
275
276 cmpCostCentre other_1 other_2
277   = let
278         tag1 = tag_CC other_1
279         tag2 = tag_CC other_2
280     in
281     if tag1 <# tag2 then LT else GT
282   where
283     tag_CC (NormalCC   {}) = (_ILIT 1 :: FastInt)
284     tag_CC (AllCafsCC  {}) = _ILIT 2
285
286 cmp_caf NotCafCC CafCC     = LT
287 cmp_caf NotCafCC NotCafCC  = EQ
288 cmp_caf CafCC    CafCC     = EQ
289 cmp_caf CafCC    NotCafCC  = GT
290
291 decomposeCCS :: CostCentreStack -> ([CostCentre],CostCentreStack)
292 decomposeCCS (PushCC cc ccs) = (cc:more, ccs') 
293   where (more,ccs') = decomposeCCS ccs
294 decomposeCCS ccs = ([],ccs)
295 \end{code}
296
297 -----------------------------------------------------------------------------
298 Printing Cost Centre Stacks.
299
300 The outputable instance for CostCentreStack prints the CCS as a C
301 expression.
302
303 NOTE: Not all cost centres are suitable for using in a static
304 initializer.  In particular, the PushCC forms where the tail is CCCS
305 may only be used in inline C code because they expand to a
306 non-constant C expression.
307
308 \begin{code}
309 instance Outputable CostCentreStack where
310   ppr NoCCS             = ptext SLIT("NO_CCS")
311   ppr CurrentCCS        = ptext SLIT("CCCS")
312   ppr OverheadCCS       = ptext SLIT("CCS_OVERHEAD")
313   ppr DontCareCCS       = ptext SLIT("CCS_DONT_CARE")
314   ppr SubsumedCCS       = ptext SLIT("CCS_SUBSUMED")
315   ppr (PushCC cc NoCCS) = ppr cc <> ptext SLIT("_ccs")
316   ppr (PushCC cc ccs)   = ptext SLIT("PushCostCentre") <> 
317                            parens (ppr ccs <> comma <> 
318                            parens(ptext SLIT("void *")) <> ppr cc)
319 \end{code}
320
321 -----------------------------------------------------------------------------
322 Printing Cost Centres.
323
324 There are several different ways in which we might want to print a
325 cost centre:
326
327         - the name of the cost centre, for profiling output (a C string)
328         - the label, i.e. C label for cost centre in .hc file.
329         - the debugging name, for output in -ddump things
330         - the interface name, for printing in _scc_ exprs in iface files.
331
332 The last 3 are derived from costCentreStr below.  The first is given
333 by costCentreName.
334
335 \begin{code}
336 instance Outputable CostCentre where
337   ppr cc = getPprStyle $ \ sty ->
338            if codeStyle sty
339            then ppCostCentreLbl cc
340            else text (costCentreUserName cc)
341
342 -- Printing in an interface file or in Core generally
343 pprCostCentreCore (AllCafsCC {cc_mod = m})
344   = text "__sccC" <+> braces (ppr m)
345 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m,
346                              cc_is_caf = caf, cc_is_dupd = dup})
347   = text "__scc" <+> braces (hsep [
348         ftext n,
349         ppr m,  
350         pp_dup dup,
351         pp_caf caf
352     ])
353
354 pp_dup DupdCC = char '!'
355 pp_dup other   = empty
356
357 pp_caf CafCC = text "__C"
358 pp_caf other   = empty
359
360
361 -- Printing as a C label
362 ppCostCentreLbl (NoCostCentre)            = text "NONE_cc"
363 ppCostCentreLbl (AllCafsCC  {cc_mod = m}) = ppr m <> text "_CAFs_cc"
364 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf}) 
365   = ppr m <> ftext n <> 
366         text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
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 {})  = "CAF"
372 costCentreUserName cc@(NormalCC {cc_name = name, cc_is_caf = is_caf})
373   =  case is_caf of { CafCC -> "CAF:";   _ -> "" } ++ decode (unpackFS name)
374 \end{code}