1 /* -----------------------------------------------------------------------------
2 * $Id: StgProf.h,v 1.7 1999/09/15 13:45:14 simonmar Exp $
4 * (c) The GHC Team, 1998
6 * Macros for profiling operations in STG code
7 * ---------------------------------------------------------------------------*/
12 #if defined(PROFILING)
14 /* -----------------------------------------------------------------------------
17 Cost centres are registered at startup by calling a registering
18 routine in each module. Each module registers its cost centres and
19 calls the registering routine for all imported modules. The RTS calls
20 the registering routine for the module Main. This registering must be
21 done before initialisation since the evaluation required for
22 initialisation may use the cost centres.
24 As the code for each module uses tail calls we use an auxiliary stack
25 (in the heap) to record imported modules still to be registered. At
26 the bottom of the stack is NULL which indicates that
27 @miniInterpretEnd@ should be resumed.
29 @START_REGISTER@ and @END_REGISTER@ are special macros used to
30 delimit the function. @END_REGISTER@ pops the next registering
31 routine off the stack and jumps to it. @REGISTER_CC@ registers a cost
32 centre. @REGISTER_IMPORT@ pushes a modules registering routine onto
35 -------------------------------------------------------------------------- */
37 extern F_ *register_stack;
39 extern CostCentre *CC_LIST; /* registered CC list */
40 extern CostCentreStack *CCS_LIST; /* registered CCS list */
42 # define PUSH_REGISTER_STACK(reg_function) \
43 *(register_stack++) = (F_)reg_function
45 # define POP_REGISTER_STACK() \
48 # define START_REGISTER_CCS(reg_mod_name) \
49 static int _module_registered = 0; \
52 if (! _module_registered) { \
53 _module_registered = 1
55 # define REGISTER_IMPORT(reg_mod_name) \
56 do { EF_(reg_mod_name); \
57 PUSH_REGISTER_STACK(reg_mod_name) ; \
60 # define END_REGISTER_CCS() \
62 JMP_(POP_REGISTER_STACK()); \
65 #define REGISTER_CC(cc) \
67 extern CostCentre cc[]; \
68 if ((cc)->link == (CostCentre *)0) { \
69 (cc)->link = CC_LIST; \
71 (cc)->ccID = CC_ID++; \
74 #define REGISTER_CCS(ccs) \
76 extern CostCentreStack ccs[]; \
77 if ((ccs)->prevStack == (CostCentreStack *)0) { \
78 (ccs)->prevStack = CCS_LIST; \
80 (ccs)->ccsID = CCS_ID++; \
83 /* -----------------------------------------------------------------------------
84 * Declaring Cost Centres & Cost Centre Stacks.
85 * -------------------------------------------------------------------------- */
87 # define CC_DECLARE(cc_ident,name,module,group,subsumed,is_local) \
88 is_local CostCentre cc_ident[1] \
98 # define CCS_DECLARE(ccs_ident,cc_ident,subsumed,is_local) \
99 is_local CostCentreStack ccs_ident[1] \
106 sub_cafcc_count : 0, \
113 # define CC_EXTERN(cc_ident) \
114 extern CostCentre cc_ident[];
116 /* -----------------------------------------------------------------------------
117 * Time / Allocation Macros
118 * ---------------------------------------------------------------------------*/
120 #define CCS_TICK(ccs) (ccs)->time_ticks++
122 /* eliminate profiling overhead from allocation costs */
123 #define CCS_ALLOC(ccs, size) (ccs)->mem_alloc += ((size)-sizeofW(StgProfHeader))
125 /* For grabbing the cost centre from a closure */
126 #define CCS_HDR(closure) ((StgClosure *)(closure))->header.prof.ccs
128 /* Restore the CCCS from a stack frame.
129 * (addr should always be Sp->header.prof.ccs)
131 #define RESTORE_CCCS(addr) (CCCS = (CostCentreStack *)(addr))
133 /* -----------------------------------------------------------------------------
134 * Pushing a new cost centre (i.e. for scc annotations)
135 * -------------------------------------------------------------------------- */
137 # define SET_CCC_X(cc,do_subcc_count,do_scc_count) \
139 if (do_subcc_count) { CCCS->sub_scc_count++; } \
140 CCCS = PushCostCentre(CCCS,cc); \
141 if (do_scc_count) { CCCS->scc_count++; } \
144 /* We sometimes don't increment the scc_count field, for example when
145 * this scc has been placed by the compiler on an expression it
146 * floated outside the main scc annotation.
149 # define SET_CCC(cc_ident,do_scc_count) \
150 SET_CCC_X(cc_ident,do_scc_count,do_scc_count)
152 # define SET_CCS_TOP(cc_ident) \
153 SET_CCC_X(cc_ident,0,1)
155 /* -----------------------------------------------------------------------------
156 * Allocating new cost centres / cost centre stacks.
157 * -------------------------------------------------------------------------- */
159 #define ASSIGN_CC_ID(ccID) \
165 #define ASSIGN_CCS_ID(ccsID) \
171 #define ASSIGN_HP_ID(hpID) \
177 #define SET_STATS_TO_ZERO(stack) \
179 (stack)->scc_count = 0; \
180 (stack)->time_ticks = 0; \
181 (stack)->sub_cafcc_count = 0; \
182 (stack)->mem_alloc = 0; \
185 /* -----------------------------------------------------------------------------
186 * Setting the cost centre when we enter a closure
187 * -------------------------------------------------------------------------- */
189 #if defined(PROFILING_DETAIL_COUNTS)
190 #define CCCS_DETAIL_COUNT(inc_this) ((inc_this)++)
192 #define CCCS_DETAIL_COUNT(inc_this) /*nothing*/
195 #define IS_CAF_OR_SUB_CCS(ccs) \
196 /* tests for lower case character */ \
197 ((ccs)->is_subsumed & ' ')
200 /* On entry to top level CAFs we count the scc ...*/
202 #define ENTER_CCS_CAF_X(ccs) \
204 /* inc subcaf count of CCCS */ \
205 CCCS->sub_cafcc_count++; \
206 /* set CCCS to ident ccs */ \
207 CCCS = (CostCentreStack *)(ccs); \
208 /* inc scc count of CAF ccs */ \
212 #define ENTER_CCS_CAF(ccs_ident) ENTER_CCS_CAF_X(ccs_ident)
213 #define ENTER_CCS_CAF_CL(closure) ENTER_CCS_CAF_X(CCS_HDR(closure))
215 /* ----------------------------------------------------------------------------
218 * On entering a closure we only count the enter to thunks ...
219 * ------------------------------------------------------------------------- */
221 #define ENTER_CCS_T(ccs) \
223 CCCS = (CostCentreStack *)(ccs); \
224 CCCS_DETAIL_COUNT(CCCS->thunk_count); \
227 #define ENTER_CCS_TCL(closure) ENTER_CCS_T(CCS_HDR(closure))
229 /* -----------------------------------------------------------------------------
230 * Entering a function
232 * Here is our special "hybrid" case when we do *not* set the CCCS.
233 * (a) The closure is a function, not a thunk;
234 * (b) The CCS is CAF-ish.
235 * -------------------------------------------------------------------------- */
237 #define ENTER_CCS_F(stack) \
239 CostCentreStack *ccs = (CostCentreStack *) (stack); \
240 CCCS_DETAIL_COUNT(CCCS->function_count); \
241 CCCS = EnterFunCCS(CCCS,ccs); \
244 #define ENTER_CCS_FCL(closure) ENTER_CCS_F(CCS_HDR(closure))
246 /* Entering a top-level function: costs are subsumed by the caller
248 #define ENTER_CCS_FSUB() \
250 CCCS_DETAIL_COUNT(CCCS->subsumed_fun_count); \
251 CCCS_DETAIL_COUNT(CCCS->function_count); \
255 #define ENTER_CCS_FCAF(stack) \
257 CostCentreStack *ccs = (CostCentreStack *) (stack); \
258 CCCS_DETAIL_COUNT(ccs->caffun_subsumed); \
259 CCCS_DETAIL_COUNT(CCCS->subsumed_caf_count); \
260 CCCS_DETAIL_COUNT(CCCS->function_count); \
264 #define ENTER_CCS_FLOAD(ccs) \
266 CCCS = (CostCentreStack *)(ccs); \
267 CCCS_DETAIL_COUNT(CCCS->function_count); \
270 /* These ENTER_CC_PAP things are only used in the RTS */
272 #define ENTER_CCS_PAP(stack) \
274 ENTER_CCS_F(stack); \
275 entering_PAP = rtsTrue; \
278 #define ENTER_CCS_PAP_CL(closure) \
279 ENTER_CCS_PAP((closure)->header.prof.ccs)
282 #define STATIC_CCS_REF(ccs) (ccs)
284 #endif /* PROFILING */
286 #endif /* STGPROF_H */