1 /* -----------------------------------------------------------------------------
2 * $Id: StgProf.h,v 1.8 2000/03/08 17:48:26 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 CostCentre *CC_LIST; /* registered CC list */
38 extern CostCentreStack *CCS_LIST; /* registered CCS list */
40 #define REGISTER_CC(cc) \
42 extern CostCentre cc[]; \
43 if ((cc)->link == (CostCentre *)0) { \
44 (cc)->link = CC_LIST; \
46 (cc)->ccID = CC_ID++; \
49 #define REGISTER_CCS(ccs) \
51 extern CostCentreStack ccs[]; \
52 if ((ccs)->prevStack == (CostCentreStack *)0) { \
53 (ccs)->prevStack = CCS_LIST; \
55 (ccs)->ccsID = CCS_ID++; \
58 /* -----------------------------------------------------------------------------
59 * Declaring Cost Centres & Cost Centre Stacks.
60 * -------------------------------------------------------------------------- */
62 # define CC_DECLARE(cc_ident,name,module,subsumed,is_local) \
63 is_local CostCentre cc_ident[1] \
72 # define CCS_DECLARE(ccs_ident,cc_ident,subsumed,is_local) \
73 is_local CostCentreStack ccs_ident[1] \
80 sub_cafcc_count : 0, \
87 # define CC_EXTERN(cc_ident) \
88 extern CostCentre cc_ident[];
90 /* -----------------------------------------------------------------------------
91 * Time / Allocation Macros
92 * ---------------------------------------------------------------------------*/
94 #define CCS_TICK(ccs) (ccs)->time_ticks++
96 /* eliminate profiling overhead from allocation costs */
97 #define CCS_ALLOC(ccs, size) (ccs)->mem_alloc += ((size)-sizeofW(StgProfHeader))
99 /* For grabbing the cost centre from a closure */
100 #define CCS_HDR(closure) ((StgClosure *)(closure))->header.prof.ccs
102 /* Restore the CCCS from a stack frame.
103 * (addr should always be Sp->header.prof.ccs)
105 #define RESTORE_CCCS(addr) (CCCS = (CostCentreStack *)(addr))
107 /* -----------------------------------------------------------------------------
108 * Pushing a new cost centre (i.e. for scc annotations)
109 * -------------------------------------------------------------------------- */
111 # define SET_CCC_X(cc,do_subcc_count,do_scc_count) \
113 if (do_subcc_count) { CCCS->sub_scc_count++; } \
114 CCCS = PushCostCentre(CCCS,cc); \
115 if (do_scc_count) { CCCS->scc_count++; } \
118 /* We sometimes don't increment the scc_count field, for example when
119 * this scc has been placed by the compiler on an expression it
120 * floated outside the main scc annotation.
123 # define SET_CCC(cc_ident,do_scc_count) \
124 SET_CCC_X(cc_ident,do_scc_count,do_scc_count)
126 # define SET_CCS_TOP(cc_ident) \
127 SET_CCC_X(cc_ident,0,1)
129 /* -----------------------------------------------------------------------------
130 * Allocating new cost centres / cost centre stacks.
131 * -------------------------------------------------------------------------- */
133 #define ASSIGN_CC_ID(ccID) \
139 #define ASSIGN_CCS_ID(ccsID) \
145 #define ASSIGN_HP_ID(hpID) \
151 #define SET_STATS_TO_ZERO(stack) \
153 (stack)->scc_count = 0; \
154 (stack)->time_ticks = 0; \
155 (stack)->sub_cafcc_count = 0; \
156 (stack)->mem_alloc = 0; \
159 /* -----------------------------------------------------------------------------
160 * Setting the cost centre when we enter a closure
161 * -------------------------------------------------------------------------- */
163 #if defined(PROFILING_DETAIL_COUNTS)
164 #define CCCS_DETAIL_COUNT(inc_this) ((inc_this)++)
166 #define CCCS_DETAIL_COUNT(inc_this) /*nothing*/
169 #define IS_CAF_OR_SUB_CCS(ccs) \
170 /* tests for lower case character */ \
171 ((ccs)->is_subsumed & ' ')
174 /* On entry to top level CAFs we count the scc ...*/
176 #define ENTER_CCS_CAF_X(ccs) \
178 /* inc subcaf count of CCCS */ \
179 CCCS->sub_cafcc_count++; \
180 /* set CCCS to ident ccs */ \
181 CCCS = (CostCentreStack *)(ccs); \
182 /* inc scc count of CAF ccs */ \
186 #define ENTER_CCS_CAF(ccs_ident) ENTER_CCS_CAF_X(ccs_ident)
187 #define ENTER_CCS_CAF_CL(closure) ENTER_CCS_CAF_X(CCS_HDR(closure))
189 /* ----------------------------------------------------------------------------
192 * On entering a closure we only count the enter to thunks ...
193 * ------------------------------------------------------------------------- */
195 #define ENTER_CCS_T(ccs) \
197 CCCS = (CostCentreStack *)(ccs); \
198 CCCS_DETAIL_COUNT(CCCS->thunk_count); \
201 #define ENTER_CCS_TCL(closure) ENTER_CCS_T(CCS_HDR(closure))
203 /* -----------------------------------------------------------------------------
204 * Entering a function
206 * Here is our special "hybrid" case when we do *not* set the CCCS.
207 * (a) The closure is a function, not a thunk;
208 * (b) The CCS is CAF-ish.
209 * -------------------------------------------------------------------------- */
211 #define ENTER_CCS_F(stack) \
213 CostCentreStack *ccs = (CostCentreStack *) (stack); \
214 CCCS_DETAIL_COUNT(CCCS->function_count); \
215 CCCS = EnterFunCCS(CCCS,ccs); \
218 #define ENTER_CCS_FCL(closure) ENTER_CCS_F(CCS_HDR(closure))
220 /* Entering a top-level function: costs are subsumed by the caller
222 #define ENTER_CCS_FSUB() \
224 CCCS_DETAIL_COUNT(CCCS->subsumed_fun_count); \
225 CCCS_DETAIL_COUNT(CCCS->function_count); \
229 #define ENTER_CCS_FCAF(stack) \
231 CostCentreStack *ccs = (CostCentreStack *) (stack); \
232 CCCS_DETAIL_COUNT(ccs->caffun_subsumed); \
233 CCCS_DETAIL_COUNT(CCCS->subsumed_caf_count); \
234 CCCS_DETAIL_COUNT(CCCS->function_count); \
238 #define ENTER_CCS_FLOAD(ccs) \
240 CCCS = (CostCentreStack *)(ccs); \
241 CCCS_DETAIL_COUNT(CCCS->function_count); \
244 /* These ENTER_CC_PAP things are only used in the RTS */
246 #define ENTER_CCS_PAP(stack) \
248 ENTER_CCS_F(stack); \
249 entering_PAP = rtsTrue; \
252 #define ENTER_CCS_PAP_CL(closure) \
253 ENTER_CCS_PAP((closure)->header.prof.ccs)
256 #define STATIC_CCS_REF(ccs) (ccs)
258 #endif /* PROFILING */
260 #endif /* STGPROF_H */