1 /* -----------------------------------------------------------------------------
2 * (c) The GHC Team 2006
4 * Initialization and use of the PAPI performance monitoring library
7 * For adding events or add your processor counters modify
9 * init_countable_events
12 * ---------------------------------------------------------------------------*/
15 #ifdef USE_PAPI /* ugly */
29 #define PAPI_ADD_EVENT(EVENT) \
31 ASSERT(n_papi_events<MAX_PAPI_EVENTS); \
32 papi_events[n_papi_events].event_code = EVENT; \
33 papi_events[n_papi_events].event_name = #EVENT; \
37 /* Report the value of a counter */
38 #define PAPI_REPORT(EVENTSET,EVENT) \
40 ullong_format_string(papi_counter(EVENTSET,EVENT),temp,rtsTrue/*commas*/); \
41 statsPrintf(" (" #EVENT ") : %s\n",temp); \
44 /* Report the value of a counter as a percentage of another counter */
45 #define PAPI_REPORT_PCT(EVENTSET,EVENT,EVENTTOT) \
46 statsPrintf(" (" #EVENT ") %% of (" #EVENTTOT ") : %.1f%%\n", \
47 papi_counter(EVENTSET,EVENT)*100.0/papi_counter(EVENTSET,EVENTTOT))
49 /* Beware, these counters are Opteron specific
50 * I obtained the numbers using the papi_avail
51 * and papi_native_avail utilities.
52 * This is certainly not the official PAPI way
55 #define FR_BR 0x40000040
56 #define FR_BR_MIS 0x40000041
57 #define FR_BR_MISCOMPARE 0x40000048
58 #define DC_ACCESS 0x40000019
59 #define DC_MISS 0x4000001a
60 #define FR_DISPATCH_STALLS 0x40000054
61 #define FR_DISPATCH_STALLS_BR 0x40000055
62 #define FR_DISPATCH_STALLS_FULL_REORDER 0x40000058
63 #define FR_DISPATCH_STALLS_FULL_RESERVATION 0x40000059
64 #define FR_DISPATCH_STALLS_FULL_LS 0x4000005b
65 #define DC_L2_REFILL_MOES 0x40001e1b
66 #define DC_SYS_REFILL_MOES 0x40001e1c
68 /* Number of counted events, computed from size of papi_events */
69 #define N_PAPI_EVENTS n_papi_events
71 /* This is bad, it should be in a header */
72 #define BIG_STRING_LEN 512
74 /* While PAPI reporting is going on this flag is on */
75 int papi_is_reporting;
77 /* Event sets and counter arrays for GC and mutator */
79 int MutatorEvents = PAPI_NULL;
80 int GCEvents = PAPI_NULL;
84 /* Arbitrary, to avoid using malloc */
85 #define MAX_PAPI_EVENTS 10
87 int n_papi_events = 0;
90 /* Events counted during GC and Mutator execution */
91 /* There's a trailing comma, do all C compilers accept that? */
92 static struct _papi_events papi_events[MAX_PAPI_EVENTS];
93 long_long MutatorCounters[MAX_PAPI_EVENTS];
94 long_long GCCounters[MAX_PAPI_EVENTS];
96 long_long start_mutator_cycles;
97 long_long start_gc_cycles;
98 long_long mutator_cycles;
103 /* If you want to add events to count, extend the
104 * init_countable_events and the papi_report function.
105 * Be aware that your processor can count a limited number
106 * of events simultaneously, you can turn on multiplexing
107 * to increase that number, though.
110 init_countable_events(void)
112 PAPI_ADD_EVENT(PAPI_TOT_INS);
113 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
114 PAPI_ADD_EVENT(FR_BR);
115 PAPI_ADD_EVENT(FR_BR_MIS);
116 /* Docs are wrong? Opteron does not count indirect branch misses exclusively */
117 PAPI_ADD_EVENT(FR_BR_MISCOMPARE);
119 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
120 PAPI_ADD_EVENT(FR_DISPATCH_STALLS);
121 PAPI_ADD_EVENT(FR_DISPATCH_STALLS_BR);
122 PAPI_ADD_EVENT(FR_DISPATCH_STALLS_FULL_LS);
124 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
125 PAPI_ADD_EVENT(PAPI_L1_DCA);
126 PAPI_ADD_EVENT(PAPI_L1_DCM);
128 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
129 PAPI_ADD_EVENT(PAPI_L2_DCA);
130 PAPI_ADD_EVENT(PAPI_L2_DCM);
132 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CB_EVENTS) {
133 PAPI_ADD_EVENT(DC_L2_REFILL_MOES);
134 PAPI_ADD_EVENT(DC_SYS_REFILL_MOES);
135 PAPI_ADD_EVENT(FR_BR_MIS);
140 static char temp[BIG_STRING_LEN];
145 ullong_format_string(mutator_cycles,temp,rtsTrue/*commas*/);
146 statsPrintf(" (MUT_CYCLES) : %s\n",temp);
152 ullong_format_string(gc_cycles,temp,rtsTrue/*commas*/);
153 statsPrintf(" (GC_CYCLES) : %s\n",temp);
156 /* This function reports counters for GC and mutator */
158 papi_report(long_long PapiCounters[])
161 /* I need to improve formatting aesthetics */
162 PAPI_REPORT(PapiCounters,PAPI_TOT_INS);
164 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
165 PAPI_REPORT(PapiCounters,FR_BR);
166 PAPI_REPORT(PapiCounters,FR_BR_MIS);
167 PAPI_REPORT_PCT(PapiCounters,FR_BR_MIS,FR_BR);
168 PAPI_REPORT_PCT(PapiCounters,FR_BR_MISCOMPARE,FR_BR);
171 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
172 PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS);
173 PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_BR);
174 //PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_BR,PAPI_TOT_CYC);
175 PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS);
176 //PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS,PAPI_TOT_CYC);
179 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
180 PAPI_REPORT(PapiCounters,PAPI_L1_DCA);
181 PAPI_REPORT(PapiCounters,PAPI_L1_DCM);
182 PAPI_REPORT_PCT(PapiCounters,PAPI_L1_DCM,PAPI_L1_DCA);
185 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
186 PAPI_REPORT(PapiCounters,PAPI_L2_DCA);
187 PAPI_REPORT(PapiCounters,PAPI_L2_DCM);
188 PAPI_REPORT_PCT(PapiCounters,PAPI_L2_DCM,PAPI_L2_DCA);
191 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CB_EVENTS) {
192 PAPI_REPORT(PapiCounters,DC_L2_REFILL_MOES);
193 PAPI_REPORT(PapiCounters,DC_SYS_REFILL_MOES);
194 PAPI_REPORT(PapiCounters,FR_BR_MIS);
202 papi_init_eventsets(void)
205 init_countable_events();
207 /* One event set for the mutator and another for the GC */
208 PAPI_CHECK( PAPI_create_eventset(&MutatorEvents));
209 PAPI_CHECK( PAPI_create_eventset(&GCEvents));
211 /* Both sets contain the same events */
212 papi_add_events(MutatorEvents);
213 papi_add_events(GCEvents);
217 /* Extract the value corresponding to an event */
219 papi_counter(long_long values[],int event)
222 for(i=0;i<N_PAPI_EVENTS;i++) {
223 if(papi_events[i].event_code==event) {
227 /* Passed a wrong event? */
228 debugBelch("Event %d is not part of event set\n",event);
232 /* Add the events of papi_events into an event set */
234 papi_add_events(int EventSet)
237 for(i=0;i<N_PAPI_EVENTS;i++) {
238 if((papi_error=PAPI_add_event(EventSet,
239 papi_events[i].event_code))
241 debugBelch("Failed adding %s to event set with error code %d\n",
242 papi_events[i].event_name,papi_error);
246 /* We should be using elapsed cycles
247 * to be consistent with time metric chosen in Stats.c (Elapsed time).
248 * This is an approximation to the cycles that the program spends.
249 * Note that the counters, in contrast, are virtual and user space.
251 #define PAPI_cycles PAPI_get_virt_cyc
254 papi_start_mutator_count(void)
256 PAPI_CHECK( PAPI_start(MutatorEvents));
257 start_mutator_cycles = PAPI_cycles();
261 papi_stop_mutator_count(void)
263 mutator_cycles += PAPI_cycles() - start_mutator_cycles;
264 PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
265 PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
269 papi_start_gc_count(void)
271 PAPI_CHECK( PAPI_start(GCEvents));
272 start_gc_cycles = PAPI_cycles();
276 papi_stop_gc_count(void)
278 gc_cycles += PAPI_cycles() - start_gc_cycles;
279 PAPI_CHECK( PAPI_accum(GCEvents,GCCounters));
280 PAPI_CHECK( PAPI_stop(GCEvents,NULL));
284 #endif /* USE_PAPI */