1 /* -----------------------------------------------------------------------------
2 * $Id: StgProf.h,v 1.13 2001/10/18 13:46:47 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 /* -----------------------------------------------------------------------------
59 * ---------------------------------------------------------------------------*/
61 #define EMPTY_STACK NULL
62 #define EMPTY_TABLE NULL
64 /* Constants used to set sumbsumed flag on CostCentres */
66 #define CC_IS_CAF 'c' /* 'c' => *is* a CAF cc */
67 #define CC_IS_BORING 'B' /* 'B' => *not* a CAF/sub cc */
70 /* -----------------------------------------------------------------------------
72 * ---------------------------------------------------------------------------*/
74 typedef struct _IndexTable {
77 struct _IndexTable *next;
78 unsigned int back_edge;
82 /* -----------------------------------------------------------------------------
83 Pre-defined cost centres and cost centre stacks
84 -------------------------------------------------------------------------- */
86 extern CostCentreStack *CCCS; /* current CCS */
88 extern CostCentre CC_MAIN[];
89 extern CostCentreStack CCS_MAIN[]; /* Top CCS */
91 extern CostCentre CC_SYSTEM[];
92 extern CostCentreStack CCS_SYSTEM[]; /* RTS costs */
94 extern CostCentre CC_GC[];
95 extern CostCentreStack CCS_GC[]; /* Garbage collector costs */
97 extern CostCentre CC_SUBSUMED[];
98 extern CostCentreStack CCS_SUBSUMED[]; /* Costs are subsumed by caller */
100 extern CostCentre CC_OVERHEAD[];
101 extern CostCentreStack CCS_OVERHEAD[]; /* Profiling overhead */
103 extern CostCentre CC_DONT_CARE[];
104 extern CostCentreStack CCS_DONT_CARE[]; /* shouldn't ever get set */
106 extern unsigned int CC_ID; /* global ids */
107 extern unsigned int CCS_ID;
108 extern unsigned int HP_ID;
110 /* -----------------------------------------------------------------------------
112 * ---------------------------------------------------------------------------*/
114 CostCentreStack *EnterFunCCS ( CostCentreStack *cccs, CostCentreStack *ccsfn );
115 CostCentreStack *PushCostCentre ( CostCentreStack *, CostCentre * );
116 CostCentreStack *AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 );
118 extern unsigned int entering_PAP;
120 /* -----------------------------------------------------------------------------
123 Cost centres are registered at startup by calling a registering
124 routine in each module. Each module registers its cost centres and
125 calls the registering routine for all imported modules. The RTS calls
126 the registering routine for the module Main. This registering must be
127 done before initialisation since the evaluation required for
128 initialisation may use the cost centres.
130 As the code for each module uses tail calls we use an auxiliary stack
131 (in the heap) to record imported modules still to be registered. At
132 the bottom of the stack is NULL which indicates that
133 @miniInterpretEnd@ should be resumed.
135 @START_REGISTER@ and @END_REGISTER@ are special macros used to
136 delimit the function. @END_REGISTER@ pops the next registering
137 routine off the stack and jumps to it. @REGISTER_CC@ registers a cost
138 centre. @REGISTER_IMPORT@ pushes a modules registering routine onto
141 -------------------------------------------------------------------------- */
143 extern CostCentre *CC_LIST; /* registered CC list */
144 extern CostCentreStack *CCS_LIST; /* registered CCS list */
146 #define REGISTER_CC(cc) \
148 extern CostCentre cc[]; \
149 if ((cc)->link == (CostCentre *)0) { \
150 (cc)->link = CC_LIST; \
152 (cc)->ccID = CC_ID++; \
155 #define REGISTER_CCS(ccs) \
157 extern CostCentreStack ccs[]; \
158 if ((ccs)->prevStack == (CostCentreStack *)0) { \
159 (ccs)->prevStack = CCS_LIST; \
161 (ccs)->ccsID = CCS_ID++; \
164 /* -----------------------------------------------------------------------------
165 * Declaring Cost Centres & Cost Centre Stacks.
166 * -------------------------------------------------------------------------- */
168 # define CC_DECLARE(cc_ident,name,module,caf,is_local) \
169 is_local CostCentre cc_ident[1] \
178 # define CCS_DECLARE(ccs_ident,cc_ident,is_local) \
179 is_local CostCentreStack ccs_ident[1] \
188 inherited_ticks : 0, \
189 inherited_alloc : 0, \
193 # define CC_EXTERN(cc_ident) \
194 extern CostCentre cc_ident[];
196 /* -----------------------------------------------------------------------------
197 * Time / Allocation Macros
198 * ---------------------------------------------------------------------------*/
200 #define CCS_TICK(ccs) (ccs)->time_ticks++
202 /* eliminate profiling overhead from allocation costs */
203 #define CCS_ALLOC(ccs, size) (ccs)->mem_alloc += ((size)-sizeofW(StgProfHeader))
205 /* For grabbing the cost centre from a closure */
206 #define CCS_HDR(closure) ((StgClosure *)(closure))->header.prof.ccs
208 /* Restore the CCCS from a stack frame.
209 * (addr should always be Sp->header.prof.ccs)
211 #define RESTORE_CCCS(addr) (CCCS = (CostCentreStack *)(addr))
213 /* -----------------------------------------------------------------------------
214 * Pushing a new cost centre (i.e. for scc annotations)
215 * -------------------------------------------------------------------------- */
217 # define SET_CCC_X(cc,do_subcc_count,do_scc_count) \
219 CCCS = PushCostCentre(CCCS,cc); \
220 if (do_scc_count) { CCCS->scc_count++; } \
223 /* We sometimes don't increment the scc_count field, for example when
224 * this scc has been placed by the compiler on an expression it
225 * floated outside the main scc annotation.
228 # define SET_CCC(cc_ident,do_scc_count) \
229 SET_CCC_X(cc_ident,do_scc_count,do_scc_count)
231 # define SET_CCS_TOP(cc_ident) \
232 SET_CCC_X(cc_ident,0,1)
234 /* -----------------------------------------------------------------------------
235 * Allocating new cost centres / cost centre stacks.
236 * -------------------------------------------------------------------------- */
238 #define ASSIGN_CC_ID(ccID) \
244 #define ASSIGN_CCS_ID(ccsID) \
250 #define ASSIGN_HP_ID(hpID) \
256 #define SET_STATS_TO_ZERO(stack) \
258 (stack)->scc_count = 0; \
259 (stack)->time_ticks = 0; \
260 (stack)->mem_alloc = 0; \
263 /* -----------------------------------------------------------------------------
264 * Setting the cost centre when we enter a closure
265 * -------------------------------------------------------------------------- */
267 #if defined(PROFILING_DETAIL_COUNTS)
268 #define CCCS_DETAIL_COUNT(inc_this) ((inc_this)++)
270 #define CCCS_DETAIL_COUNT(inc_this) /*nothing*/
273 /* On entry to top level CAFs we count the scc ...*/
275 #define ENTER_CCS_CAF_X(ccs) \
277 /* set CCCS to ident ccs */ \
278 CCCS = (CostCentreStack *)(ccs); \
279 /* inc scc count of CAF ccs */ \
283 #define ENTER_CCS_CAF(ccs_ident) ENTER_CCS_CAF_X(ccs_ident)
284 #define ENTER_CCS_CAF_CL(closure) ENTER_CCS_CAF_X(CCS_HDR(closure))
286 /* ----------------------------------------------------------------------------
289 * On entering a closure we only count the enter to thunks ...
290 * ------------------------------------------------------------------------- */
292 #define ENTER_CCS_T(ccs) \
294 CCCS = (CostCentreStack *)(ccs); \
295 CCCS_DETAIL_COUNT(CCCS->thunk_count); \
298 #define ENTER_CCS_TCL(closure) ENTER_CCS_T(CCS_HDR(closure))
300 /* -----------------------------------------------------------------------------
301 * Entering a function
303 * Here is our special "hybrid" case when we do *not* set the CCCS.
304 * (a) The closure is a function, not a thunk;
305 * (b) The CCS is CAF-ish.
306 * -------------------------------------------------------------------------- */
308 #define ENTER_CCS_F(stack) \
310 CostCentreStack *ccs = (CostCentreStack *) (stack); \
311 CCCS_DETAIL_COUNT(CCCS->function_count); \
312 CCCS = EnterFunCCS(CCCS,ccs); \
315 #define ENTER_CCS_FCL(closure) ENTER_CCS_F(CCS_HDR(closure))
317 /* Entering a top-level function: costs are subsumed by the caller
319 #define ENTER_CCS_FSUB() \
321 CCCS_DETAIL_COUNT(CCCS->subsumed_fun_count); \
322 CCCS_DETAIL_COUNT(CCCS->function_count); \
326 #define ENTER_CCS_FCAF(stack) \
328 CostCentreStack *ccs = (CostCentreStack *) (stack); \
329 CCCS_DETAIL_COUNT(ccs->caffun_subsumed); \
330 CCCS_DETAIL_COUNT(CCCS->subsumed_caf_count); \
331 CCCS_DETAIL_COUNT(CCCS->function_count); \
335 #define ENTER_CCS_FLOAD(ccs) \
337 CCCS = (CostCentreStack *)(ccs); \
338 CCCS_DETAIL_COUNT(CCCS->function_count); \
341 /* These ENTER_CC_PAP things are only used in the RTS */
343 #define ENTER_CCS_PAP(stack) \
345 ENTER_CCS_F(stack); \
346 entering_PAP = rtsTrue; \
349 #define ENTER_CCS_PAP_CL(closure) \
350 ENTER_CCS_PAP((closure)->header.prof.ccs)
353 #define STATIC_CCS_REF(ccs) (ccs)
355 /* -----------------------------------------------------------------------------
356 When not profiling, these macros do nothing...
357 -------------------------------------------------------------------------- */
358 #else /* !PROFILING */
360 #define CCS_ALLOC(ccs, amount) doNothing()
361 #define ENTER_CC_PAP_CL(r) doNothing()
362 #define ENTER_CCS_PAP_CL(r) doNothing()
364 #endif /* PROFILING */
366 #endif /* STGPROF_H */