1 /* -----------------------------------------------------------------------------
2 * $Id: StgProf.h,v 1.16 2001/12/12 14:59:41 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 int selected;
40 unsigned long long scc_count;
42 unsigned long time_ticks;
43 unsigned long long mem_alloc;
45 unsigned long inherited_ticks;
46 unsigned long long inherited_alloc;
52 /* -----------------------------------------------------------------------------
53 * The rest is PROFILING only...
54 * ---------------------------------------------------------------------------*/
56 #if defined(PROFILING)
58 /* -----------------------------------------------------------------------------
60 * ---------------------------------------------------------------------------*/
62 #define EMPTY_STACK NULL
63 #define EMPTY_TABLE NULL
65 /* Constants used to set sumbsumed flag on CostCentres */
67 #define CC_IS_CAF 'c' /* 'c' => *is* a CAF cc */
68 #define CC_IS_BORING 'B' /* 'B' => *not* a CAF/sub cc */
71 /* -----------------------------------------------------------------------------
73 * ---------------------------------------------------------------------------*/
75 typedef struct _IndexTable {
78 struct _IndexTable *next;
79 unsigned int back_edge;
83 /* -----------------------------------------------------------------------------
84 Pre-defined cost centres and cost centre stacks
85 -------------------------------------------------------------------------- */
87 extern CostCentreStack *CCCS; /* current CCS */
89 extern CostCentre CC_MAIN[];
90 extern CostCentreStack CCS_MAIN[]; /* Top CCS */
92 extern CostCentre CC_SYSTEM[];
93 extern CostCentreStack CCS_SYSTEM[]; /* RTS costs */
95 extern CostCentre CC_GC[];
96 extern CostCentreStack CCS_GC[]; /* Garbage collector costs */
98 extern CostCentre CC_SUBSUMED[];
99 extern CostCentreStack CCS_SUBSUMED[]; /* Costs are subsumed by caller */
101 extern CostCentre CC_OVERHEAD[];
102 extern CostCentreStack CCS_OVERHEAD[]; /* Profiling overhead */
104 extern CostCentre CC_DONT_CARE[];
105 extern CostCentreStack CCS_DONT_CARE[]; /* shouldn't ever get set */
107 extern unsigned int CC_ID; /* global ids */
108 extern unsigned int CCS_ID;
109 extern unsigned int HP_ID;
111 /* -----------------------------------------------------------------------------
113 * ---------------------------------------------------------------------------*/
115 CostCentreStack *EnterFunCCS ( CostCentreStack *cccs, CostCentreStack *ccsfn );
116 CostCentreStack *PushCostCentre ( CostCentreStack *, CostCentre * );
117 CostCentreStack *AppendCCS ( CostCentreStack *ccs1, CostCentreStack *ccs2 );
119 extern unsigned int entering_PAP;
121 /* -----------------------------------------------------------------------------
124 Cost centres are registered at startup by calling a registering
125 routine in each module. Each module registers its cost centres and
126 calls the registering routine for all imported modules. The RTS calls
127 the registering routine for the module Main. This registering must be
128 done before initialisation since the evaluation required for
129 initialisation may use the cost centres.
131 As the code for each module uses tail calls we use an auxiliary stack
132 (in the heap) to record imported modules still to be registered. At
133 the bottom of the stack is NULL which indicates that
134 @miniInterpretEnd@ should be resumed.
136 @START_REGISTER@ and @END_REGISTER@ are special macros used to
137 delimit the function. @END_REGISTER@ pops the next registering
138 routine off the stack and jumps to it. @REGISTER_CC@ registers a cost
139 centre. @REGISTER_IMPORT@ pushes a modules registering routine onto
142 -------------------------------------------------------------------------- */
144 extern CostCentre *CC_LIST; /* registered CC list */
145 extern CostCentreStack *CCS_LIST; /* registered CCS list */
147 #define REGISTER_CC(cc) \
149 extern CostCentre cc[]; \
150 if ((cc)->link == (CostCentre *)0) { \
151 (cc)->link = CC_LIST; \
153 (cc)->ccID = CC_ID++; \
156 #define REGISTER_CCS(ccs) \
158 extern CostCentreStack ccs[]; \
159 if ((ccs)->prevStack == (CostCentreStack *)0) { \
160 (ccs)->prevStack = CCS_LIST; \
162 (ccs)->ccsID = CCS_ID++; \
165 /* -----------------------------------------------------------------------------
166 * Declaring Cost Centres & Cost Centre Stacks.
167 * -------------------------------------------------------------------------- */
169 # define CC_DECLARE(cc_ident,name,module,caf,is_local) \
170 is_local CostCentre cc_ident[1] \
179 # define CCS_DECLARE(ccs_ident,cc_ident,is_local) \
180 is_local CostCentreStack ccs_ident[1] \
189 inherited_ticks : 0, \
190 inherited_alloc : 0, \
194 # define CC_EXTERN(cc_ident) \
195 extern CostCentre cc_ident[];
197 /* -----------------------------------------------------------------------------
198 * Time / Allocation Macros
199 * ---------------------------------------------------------------------------*/
201 /* eliminate profiling overhead from allocation costs */
202 #define CCS_ALLOC(ccs, size) (ccs)->mem_alloc += ((size)-sizeofW(StgProfHeader))
204 /* For grabbing the cost centre from a closure */
205 #define CCS_HDR(closure) ((StgClosure *)(closure))->header.prof.ccs
207 /* Restore the CCCS from a stack frame.
208 * (addr should always be Sp->header.prof.ccs)
210 #define RESTORE_CCCS(addr) (CCCS = (CostCentreStack *)(addr))
212 /* -----------------------------------------------------------------------------
213 * Pushing a new cost centre (i.e. for scc annotations)
214 * -------------------------------------------------------------------------- */
216 # define SET_CCC_X(cc,do_subcc_count,do_scc_count) \
218 CCCS = PushCostCentre(CCCS,cc); \
219 if (do_scc_count) { CCCS->scc_count++; } \
222 /* We sometimes don't increment the scc_count field, for example when
223 * this scc has been placed by the compiler on an expression it
224 * floated outside the main scc annotation.
227 # define SET_CCC(cc_ident,do_scc_count) \
228 SET_CCC_X(cc_ident,do_scc_count,do_scc_count)
230 # define SET_CCS_TOP(cc_ident) \
231 SET_CCC_X(cc_ident,0,1)
233 /* -----------------------------------------------------------------------------
234 * Allocating new cost centres / cost centre stacks.
235 * -------------------------------------------------------------------------- */
237 #define ASSIGN_CC_ID(ccID) \
243 #define ASSIGN_CCS_ID(ccsID) \
249 #define ASSIGN_HP_ID(hpID) \
255 #define SET_STATS_TO_ZERO(stack) \
257 (stack)->scc_count = 0; \
258 (stack)->time_ticks = 0; \
259 (stack)->mem_alloc = 0; \
262 /* -----------------------------------------------------------------------------
263 * Setting the cost centre when we enter a closure
264 * -------------------------------------------------------------------------- */
266 #if defined(PROFILING_DETAIL_COUNTS)
267 #define CCCS_DETAIL_COUNT(inc_this) ((inc_this)++)
269 #define CCCS_DETAIL_COUNT(inc_this) /*nothing*/
272 /* On entry to top level CAFs we count the scc ...*/
274 #define ENTER_CCS_CAF_X(ccs) \
276 /* set CCCS to ident ccs */ \
277 CCCS = (CostCentreStack *)(ccs); \
278 /* inc scc count of CAF ccs */ \
282 #define ENTER_CCS_CAF(ccs_ident) ENTER_CCS_CAF_X(ccs_ident)
283 #define ENTER_CCS_CAF_CL(closure) ENTER_CCS_CAF_X(CCS_HDR(closure))
285 /* ----------------------------------------------------------------------------
288 * On entering a closure we only count the enter to thunks ...
289 * ------------------------------------------------------------------------- */
291 #define ENTER_CCS_T(ccs) \
293 CCCS = (CostCentreStack *)(ccs); \
294 CCCS_DETAIL_COUNT(CCCS->thunk_count); \
297 #define ENTER_CCS_TCL(closure) ENTER_CCS_T(CCS_HDR(closure))
299 /* -----------------------------------------------------------------------------
300 * Entering a function
302 * Here is our special "hybrid" case when we do *not* set the CCCS.
303 * (a) The closure is a function, not a thunk;
304 * (b) The CCS is CAF-ish.
305 * -------------------------------------------------------------------------- */
307 #define ENTER_CCS_F(stack) \
309 CostCentreStack *ccs = (CostCentreStack *) (stack); \
310 CCCS_DETAIL_COUNT(CCCS->function_count); \
311 CCCS = EnterFunCCS(CCCS,ccs); \
314 #define ENTER_CCS_FCL(closure) ENTER_CCS_F(CCS_HDR(closure))
316 /* Entering a top-level function: costs are subsumed by the caller
318 #define ENTER_CCS_FSUB() \
320 CCCS_DETAIL_COUNT(CCCS->subsumed_fun_count); \
321 CCCS_DETAIL_COUNT(CCCS->function_count); \
325 #define ENTER_CCS_FCAF(stack) \
327 CostCentreStack *ccs = (CostCentreStack *) (stack); \
328 CCCS_DETAIL_COUNT(ccs->caffun_subsumed); \
329 CCCS_DETAIL_COUNT(CCCS->subsumed_caf_count); \
330 CCCS_DETAIL_COUNT(CCCS->function_count); \
334 #define ENTER_CCS_FLOAD(ccs) \
336 CCCS = (CostCentreStack *)(ccs); \
337 CCCS_DETAIL_COUNT(CCCS->function_count); \
340 /* These ENTER_CC_PAP things are only used in the RTS */
342 #define ENTER_CCS_PAP(stack) \
344 ENTER_CCS_F(stack); \
345 entering_PAP = rtsTrue; \
348 #define ENTER_CCS_PAP_CL(closure) \
349 ENTER_CCS_PAP((closure)->header.prof.ccs)
351 /* -----------------------------------------------------------------------------
352 When not profiling, these macros do nothing...
353 -------------------------------------------------------------------------- */
354 #else /* !PROFILING */
356 #define CCS_ALLOC(ccs, amount) doNothing()
357 #define ENTER_CC_PAP_CL(r) doNothing()
358 #define ENTER_CCS_PAP_CL(r) doNothing()
360 #endif /* PROFILING */
362 #endif /* STGPROF_H */