1 /* -----------------------------------------------------------------------------
2 * $Id: StgProf.h,v 1.11 2001/10/03 13:57:42 simonmar Exp $
4 * (c) The GHC Team, 1998
6 * Macros for profiling operations in STG code
7 * ---------------------------------------------------------------------------*/
12 /* -----------------------------------------------------------------------------
14 * ---------------------------------------------------------------------------*/
15 typedef struct _CostCentre {
21 /* used for accumulating costs at the end of the run... */
22 unsigned long time_ticks;
23 unsigned long long mem_alloc;
27 struct _CostCentre *link;
31 typedef struct _CostCentreStack {
35 struct _CostCentreStack *prevStack;
36 struct _IndexTable *indexTable;
38 unsigned long long scc_count;
40 unsigned long time_ticks;
41 unsigned long long mem_alloc;
42 unsigned long mem_resid;
44 unsigned long inherited_ticks;
45 unsigned long long inherited_alloc;
51 /* -----------------------------------------------------------------------------
52 * The rest is PROFILING only...
53 * ---------------------------------------------------------------------------*/
55 #if !defined(PROFILING)
57 #define CCS_ALLOC(ccs, amount) doNothing()
58 #define ENTER_CC_PAP_CL(r) doNothing()
59 #define ENTER_CCS_PAP_CL(r) doNothing()
61 #else /* PROFILING... */
63 /* -----------------------------------------------------------------------------
65 * ---------------------------------------------------------------------------*/
67 #define EMPTY_STACK NULL
68 #define EMPTY_TABLE NULL
70 /* Constants used to set sumbsumed flag on CostCentres */
72 #define CC_IS_CAF 'c' /* 'c' => *is* a CAF cc */
73 #define CC_IS_BORING 'B' /* 'B' => *not* a CAF/sub cc */
75 /* -----------------------------------------------------------------------------
77 * ---------------------------------------------------------------------------*/
82 typedef struct _IndexTable {
85 struct _IndexTable *next;
86 unsigned int back_edge;
90 /* -----------------------------------------------------------------------------
91 Pre-defined cost centres and cost centre stacks
92 -------------------------------------------------------------------------- */
94 extern CostCentreStack *CCCS; /* current CCS */
96 extern CostCentre CC_MAIN[];
97 extern CostCentreStack CCS_MAIN[]; /* Top CCS */
99 extern CostCentre CC_SYSTEM[];
100 extern CostCentreStack CCS_SYSTEM[]; /* RTS costs */
102 extern CostCentre CC_GC[];
103 extern CostCentreStack CCS_GC[]; /* Garbage collector costs */
105 extern CostCentre CC_SUBSUMED[];
106 extern CostCentreStack CCS_SUBSUMED[]; /* Costs are subsumed by caller */
108 extern CostCentre CC_OVERHEAD[];
109 extern CostCentreStack CCS_OVERHEAD[]; /* Profiling overhead */
111 extern CostCentre CC_DONT_CARE[];
112 extern CostCentreStack CCS_DONT_CARE[]; /* shouldn't ever get set */
114 extern unsigned int CC_ID; /* global ids */
115 extern unsigned int CCS_ID;
116 extern unsigned int HP_ID;
118 /* -----------------------------------------------------------------------------
120 * ---------------------------------------------------------------------------*/
122 CostCentreStack *EnterFunCCS ( CostCentreStack *cccs, CostCentreStack *ccsfn );
123 CostCentreStack *PushCostCentre ( CostCentreStack *, CostCentre * );
124 CostCentreStack *AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 );
126 extern unsigned int entering_PAP;
128 /* -----------------------------------------------------------------------------
131 Cost centres are registered at startup by calling a registering
132 routine in each module. Each module registers its cost centres and
133 calls the registering routine for all imported modules. The RTS calls
134 the registering routine for the module Main. This registering must be
135 done before initialisation since the evaluation required for
136 initialisation may use the cost centres.
138 As the code for each module uses tail calls we use an auxiliary stack
139 (in the heap) to record imported modules still to be registered. At
140 the bottom of the stack is NULL which indicates that
141 @miniInterpretEnd@ should be resumed.
143 @START_REGISTER@ and @END_REGISTER@ are special macros used to
144 delimit the function. @END_REGISTER@ pops the next registering
145 routine off the stack and jumps to it. @REGISTER_CC@ registers a cost
146 centre. @REGISTER_IMPORT@ pushes a modules registering routine onto
149 -------------------------------------------------------------------------- */
151 extern CostCentre *CC_LIST; /* registered CC list */
152 extern CostCentreStack *CCS_LIST; /* registered CCS list */
154 #define REGISTER_CC(cc) \
156 extern CostCentre cc[]; \
157 if ((cc)->link == (CostCentre *)0) { \
158 (cc)->link = CC_LIST; \
160 (cc)->ccID = CC_ID++; \
163 #define REGISTER_CCS(ccs) \
165 extern CostCentreStack ccs[]; \
166 if ((ccs)->prevStack == (CostCentreStack *)0) { \
167 (ccs)->prevStack = CCS_LIST; \
169 (ccs)->ccsID = CCS_ID++; \
172 /* -----------------------------------------------------------------------------
173 * Declaring Cost Centres & Cost Centre Stacks.
174 * -------------------------------------------------------------------------- */
176 # define CC_DECLARE(cc_ident,name,module,caf,is_local) \
177 is_local CostCentre cc_ident[1] \
186 # define CCS_DECLARE(ccs_ident,cc_ident,is_local) \
187 is_local CostCentreStack ccs_ident[1] \
196 inherited_ticks : 0, \
197 inherited_alloc : 0, \
201 # define CC_EXTERN(cc_ident) \
202 extern CostCentre cc_ident[];
204 /* -----------------------------------------------------------------------------
205 * Time / Allocation Macros
206 * ---------------------------------------------------------------------------*/
208 #define CCS_TICK(ccs) (ccs)->time_ticks++
210 /* eliminate profiling overhead from allocation costs */
211 #define CCS_ALLOC(ccs, size) (ccs)->mem_alloc += ((size)-sizeofW(StgProfHeader))
213 /* For grabbing the cost centre from a closure */
214 #define CCS_HDR(closure) ((StgClosure *)(closure))->header.prof.ccs
216 /* Restore the CCCS from a stack frame.
217 * (addr should always be Sp->header.prof.ccs)
219 #define RESTORE_CCCS(addr) (CCCS = (CostCentreStack *)(addr))
221 /* -----------------------------------------------------------------------------
222 * Pushing a new cost centre (i.e. for scc annotations)
223 * -------------------------------------------------------------------------- */
225 # define SET_CCC_X(cc,do_subcc_count,do_scc_count) \
227 CCCS = PushCostCentre(CCCS,cc); \
228 if (do_scc_count) { CCCS->scc_count++; } \
231 /* We sometimes don't increment the scc_count field, for example when
232 * this scc has been placed by the compiler on an expression it
233 * floated outside the main scc annotation.
236 # define SET_CCC(cc_ident,do_scc_count) \
237 SET_CCC_X(cc_ident,do_scc_count,do_scc_count)
239 # define SET_CCS_TOP(cc_ident) \
240 SET_CCC_X(cc_ident,0,1)
242 /* -----------------------------------------------------------------------------
243 * Allocating new cost centres / cost centre stacks.
244 * -------------------------------------------------------------------------- */
246 #define ASSIGN_CC_ID(ccID) \
252 #define ASSIGN_CCS_ID(ccsID) \
258 #define ASSIGN_HP_ID(hpID) \
264 #define SET_STATS_TO_ZERO(stack) \
266 (stack)->scc_count = 0; \
267 (stack)->time_ticks = 0; \
268 (stack)->mem_alloc = 0; \
271 /* -----------------------------------------------------------------------------
272 * Setting the cost centre when we enter a closure
273 * -------------------------------------------------------------------------- */
275 #if defined(PROFILING_DETAIL_COUNTS)
276 #define CCCS_DETAIL_COUNT(inc_this) ((inc_this)++)
278 #define CCCS_DETAIL_COUNT(inc_this) /*nothing*/
281 /* On entry to top level CAFs we count the scc ...*/
283 #define ENTER_CCS_CAF_X(ccs) \
285 /* set CCCS to ident ccs */ \
286 CCCS = (CostCentreStack *)(ccs); \
287 /* inc scc count of CAF ccs */ \
291 #define ENTER_CCS_CAF(ccs_ident) ENTER_CCS_CAF_X(ccs_ident)
292 #define ENTER_CCS_CAF_CL(closure) ENTER_CCS_CAF_X(CCS_HDR(closure))
294 /* ----------------------------------------------------------------------------
297 * On entering a closure we only count the enter to thunks ...
298 * ------------------------------------------------------------------------- */
300 #define ENTER_CCS_T(ccs) \
302 CCCS = (CostCentreStack *)(ccs); \
303 CCCS_DETAIL_COUNT(CCCS->thunk_count); \
306 #define ENTER_CCS_TCL(closure) ENTER_CCS_T(CCS_HDR(closure))
308 /* -----------------------------------------------------------------------------
309 * Entering a function
311 * Here is our special "hybrid" case when we do *not* set the CCCS.
312 * (a) The closure is a function, not a thunk;
313 * (b) The CCS is CAF-ish.
314 * -------------------------------------------------------------------------- */
316 #define ENTER_CCS_F(stack) \
318 CostCentreStack *ccs = (CostCentreStack *) (stack); \
319 CCCS_DETAIL_COUNT(CCCS->function_count); \
320 CCCS = EnterFunCCS(CCCS,ccs); \
323 #define ENTER_CCS_FCL(closure) ENTER_CCS_F(CCS_HDR(closure))
325 /* Entering a top-level function: costs are subsumed by the caller
327 #define ENTER_CCS_FSUB() \
329 CCCS_DETAIL_COUNT(CCCS->subsumed_fun_count); \
330 CCCS_DETAIL_COUNT(CCCS->function_count); \
334 #define ENTER_CCS_FCAF(stack) \
336 CostCentreStack *ccs = (CostCentreStack *) (stack); \
337 CCCS_DETAIL_COUNT(ccs->caffun_subsumed); \
338 CCCS_DETAIL_COUNT(CCCS->subsumed_caf_count); \
339 CCCS_DETAIL_COUNT(CCCS->function_count); \
343 #define ENTER_CCS_FLOAD(ccs) \
345 CCCS = (CostCentreStack *)(ccs); \
346 CCCS_DETAIL_COUNT(CCCS->function_count); \
349 /* These ENTER_CC_PAP things are only used in the RTS */
351 #define ENTER_CCS_PAP(stack) \
353 ENTER_CCS_F(stack); \
354 entering_PAP = rtsTrue; \
357 #define ENTER_CCS_PAP_CL(closure) \
358 ENTER_CCS_PAP((closure)->header.prof.ccs)
361 #define STATIC_CCS_REF(ccs) (ccs)
363 #endif /* PROFILING */
365 #endif /* STGPROF_H */