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 */
19 #include "PosixSource.h"
26 // used to protect the aggregated counters
28 static Mutex papi_counter_mutex;
33 const char * event_name;
36 /* Beware, these counters are Opteron specific
37 * I obtained the numbers using the papi_avail
38 * and papi_native_avail utilities.
39 * This is certainly not the official PAPI way
42 #define FR_BR 0x40000040
43 #define FR_BR_MIS 0x40000041
44 #define FR_BR_MISCOMPARE 0x40000048
45 #define DC_ACCESS 0x40000019
46 #define DC_MISS 0x4000001a
47 #define FR_DISPATCH_STALLS 0x40000054
48 #define FR_DISPATCH_STALLS_BR 0x40000055
49 #define FR_DISPATCH_STALLS_FULL_REORDER 0x40000058
50 #define FR_DISPATCH_STALLS_FULL_RESERVATION 0x40000059
51 #define FR_DISPATCH_STALLS_FULL_LS 0x4000005b
52 #define DC_L2_REFILL_MOES 0x40001e1b
53 #define DC_SYS_REFILL_MOES 0x40001e1c
55 /* This is bad, it should be in a header */
56 #define BIG_STRING_LEN 512
59 #define PAPI_CHECK(CALL) \
60 if((papi_error=(CALL)) != PAPI_OK) { \
61 debugBelch("PAPI function failed in module %s at line %d with error code %d\n", \
62 __FILE__,__LINE__,papi_error); \
65 /* While PAPI reporting is going on this flag is on */
66 int papi_is_reporting;
68 /* Event sets and counter arrays for GC and mutator */
70 int MutatorEvents = PAPI_NULL;
71 int GCEvents = PAPI_NULL;
75 /* Arbitrary, to avoid using malloc */
76 #define MAX_PAPI_EVENTS 10
77 static char papiNativeEventNames[MAX_PAPI_EVENTS][PAPI_MAX_STR_LEN];
79 static nat n_papi_events = 0;
82 /* Events counted during GC and Mutator execution */
83 /* There's a trailing comma, do all C compilers accept that? */
84 static struct _papi_events papi_events[MAX_PAPI_EVENTS];
85 long_long MutatorCounters[MAX_PAPI_EVENTS];
86 long_long GC0Counters[MAX_PAPI_EVENTS];
87 long_long GC1Counters[MAX_PAPI_EVENTS];
89 long_long start_mutator_cycles;
90 long_long mutator_cycles = 0;
91 long_long start_gc_cycles;
92 long_long gc0_cycles = 0;
93 long_long gc1_cycles = 0;
97 static long_long papi_counter(long_long values[],int event);
98 static void papi_add_events(int EventSet);
100 static nat max_hardware_counters = 2;
102 /* If you want to add events to count, extend the
103 * init_countable_events and the papi_report function.
104 * Be aware that your processor can count a limited number
105 * of events simultaneously, you can turn on multiplexing
106 * to increase that number, though.
108 static void papi_add_event(const char *name, int code)
110 if (n_papi_events >= max_hardware_counters) {
111 errorBelch("too many PAPI events for this CPU (max: %d)",
112 max_hardware_counters);
113 stg_exit(EXIT_FAILURE);
115 papi_events[n_papi_events].event_code = code;
116 papi_events[n_papi_events].event_name = name;
121 init_countable_events(void)
123 max_hardware_counters = PAPI_num_counters();
125 #define PAPI_ADD_EVENT(EVENT) papi_add_event(#EVENT,EVENT)
127 if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
128 PAPI_ADD_EVENT(FR_BR);
129 PAPI_ADD_EVENT(FR_BR_MIS);
130 /* Docs are wrong? Opteron does not count indirect branch misses exclusively */
131 PAPI_ADD_EVENT(FR_BR_MISCOMPARE);
132 } else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
133 PAPI_ADD_EVENT(FR_DISPATCH_STALLS);
134 PAPI_ADD_EVENT(FR_DISPATCH_STALLS_BR);
135 PAPI_ADD_EVENT(FR_DISPATCH_STALLS_FULL_LS);
136 } else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
137 PAPI_ADD_EVENT(PAPI_L1_DCA);
138 PAPI_ADD_EVENT(PAPI_L1_DCM);
139 } else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
140 PAPI_ADD_EVENT(PAPI_L2_DCA);
141 PAPI_ADD_EVENT(PAPI_L2_DCM);
142 } else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CB_EVENTS) {
143 PAPI_ADD_EVENT(DC_L2_REFILL_MOES);
144 PAPI_ADD_EVENT(DC_SYS_REFILL_MOES);
145 PAPI_ADD_EVENT(FR_BR_MIS);
146 } else if (RtsFlags.PapiFlags.eventType==PAPI_USER_EVENTS) {
149 char *asciiEventCode;
151 for (i = 0; i < RtsFlags.PapiFlags.numUserEvents; i++) {
152 if(RtsFlags.PapiFlags.userEventsKind[i] == PAPI_PRESET_EVENT_KIND) {
153 name = RtsFlags.PapiFlags.userEvents[i];
154 PAPI_CHECK(PAPI_event_name_to_code(name, &code))
156 else { // PAPI_NATIVE_EVENT_KIND
157 asciiEventCode = RtsFlags.PapiFlags.userEvents[i];
158 name = papiNativeEventNames[i];
159 code = strtol(asciiEventCode, NULL, 16 /* hex number expected */);
160 PAPI_CHECK(PAPI_event_code_to_name(code, name))
162 papi_add_event(name, code);
165 // PAPI_ADD_EVENT(PAPI_L1_DCA); // L1 data cache accesses
166 // PAPI_ADD_EVENT(PAPI_L1_ICR); // L1 instruction cache reads
167 // PAPI_ADD_EVENT(PAPI_L1_ICM); // L1 instruction cache misses
168 // PAPI_ADD_EVENT(PAPI_L1_STM); // L1 store misses
169 // PAPI_ADD_EVENT(PAPI_L1_DCM); // L1 data cache misses
170 // PAPI_ADD_EVENT(PAPI_L1_LDM); // L1 load misses
171 // PAPI_ADD_EVENT(PAPI_L2_TCM); // L2 cache misses
172 // PAPI_ADD_EVENT(PAPI_L2_STM); // L2 store misses
173 // PAPI_ADD_EVENT(PAPI_L2_DCW); // L2 data cache writes
174 // PAPI_ADD_EVENT(PAPI_L2_DCR); // L2 data cache reads
175 // PAPI_ADD_EVENT(PAPI_L2_TCW); // L2 cache writes
176 // PAPI_ADD_EVENT(PAPI_L2_TCR); // L2 cache reads
177 // PAPI_ADD_EVENT(PAPI_CA_CLN); // exclusive access to clean cache line
178 // PAPI_ADD_EVENT(PAPI_TLB_DM); // TLB misses
179 PAPI_ADD_EVENT(PAPI_TOT_INS); // Total instructions
180 PAPI_ADD_EVENT(PAPI_TOT_CYC); // Total instructions
181 // PAPI_ADD_EVENT(PAPI_CA_SHR); // exclusive access to shared cache line
182 // PAPI_ADD_EVENT(PAPI_RES_STL); // Cycles stalled on any resource
186 // We might also consider:
187 // PAPI_BR_MSP Conditional branch instructions mispredicted
188 // PAPI_RES_STL Cycles stalled on any resource
193 papi_report_event(const char *name, StgWord64 value)
195 static char temp[BIG_STRING_LEN];
196 showStgWord64(value,temp,rtsTrue/*commas*/);
197 statsPrintf(" %15s %15s\n", name, temp);
200 /* This function reports counters for GC and mutator */
202 papi_report(long_long counters[])
206 /* Report the value of a counter as a percentage of another counter */
207 #define PAPI_REPORT_PCT(EVENTSET,EVENT,EVENTTOT) \
208 statsPrintf(" " #EVENT " %% of " #EVENTTOT " : %.1f%%\n", \
209 papi_counter(EVENTSET,EVENT)*100.0/papi_counter(EVENTSET,EVENTTOT))
211 for (i = 0; i < n_papi_events; i++)
213 papi_report_event(papi_events[i].event_name, counters[i]);
216 if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
217 PAPI_REPORT_PCT(counters,FR_BR_MIS,FR_BR);
218 PAPI_REPORT_PCT(counters,FR_BR_MISCOMPARE,FR_BR);
221 else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
222 PAPI_REPORT_PCT(counters,PAPI_L1_DCM,PAPI_L1_DCA);
225 else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
226 PAPI_REPORT_PCT(counters,PAPI_L2_DCM,PAPI_L2_DCA);
231 papi_stats_report (void)
233 statsPrintf(" Mutator CPU counters\n");
234 papi_report_event("CYCLES", mutator_cycles);
235 papi_report(MutatorCounters);
237 statsPrintf("\n GC(0) CPU counters\n");
238 papi_report_event("CYCLES", gc0_cycles);
239 papi_report(GC0Counters);
241 statsPrintf("\n GC(1) CPU counters\n");
242 papi_report_event("CYCLES", gc1_cycles);
243 papi_report(GC1Counters);
247 papi_init_eventset (int *event_set)
249 PAPI_register_thread();
250 PAPI_CHECK( PAPI_create_eventset(event_set));
251 papi_add_events(*event_set);
257 /* Initialise the performance tracking library */
259 if ((ver = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) {
261 errorBelch("PAPI_library_init: wrong version: %x", ver);
262 stg_exit(EXIT_FAILURE);
264 sysErrorBelch("PAPI_library_init");
265 stg_exit(EXIT_FAILURE);
272 if ((err = PAPI_thread_init(osThreadId)) < 0) {
273 barf("PAPI_thread_init: %d",err);
276 initMutex(&papi_counter_mutex);
280 init_countable_events();
282 papi_init_eventset(&MutatorEvents);
283 papi_init_eventset(&GCEvents);
286 /* Extract the value corresponding to an event */
288 papi_counter(long_long values[],int event)
291 for(i=0;i<n_papi_events;i++) {
292 if(papi_events[i].event_code==event) {
296 /* Passed a wrong event? */
297 debugBelch("Event %d is not part of event set\n",event);
301 /* Add the events of papi_events into an event set */
303 papi_add_events(int EventSet)
306 for(i=0;i<n_papi_events;i++) {
307 if((papi_error=PAPI_add_event(EventSet,
308 papi_events[i].event_code))
310 debugBelch("Failed adding %s to event set with error code %d\n",
311 papi_events[i].event_name,papi_error);
315 /* We should be using elapsed cycles
316 * to be consistent with time metric chosen in Stats.c (Elapsed time).
317 * This is an approximation to the cycles that the program spends.
318 * Note that the counters, in contrast, are virtual and user space.
320 #define PAPI_cycles PAPI_get_virt_cyc
323 papi_start_mutator_count(void)
325 ACQUIRE_LOCK(&papi_counter_mutex);
326 PAPI_CHECK( PAPI_start(MutatorEvents));
327 start_mutator_cycles = PAPI_cycles();
328 RELEASE_LOCK(&papi_counter_mutex);
332 papi_stop_mutator_count(void)
334 ACQUIRE_LOCK(&papi_counter_mutex);
335 mutator_cycles += PAPI_cycles() - start_mutator_cycles;
336 PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
337 PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
338 RELEASE_LOCK(&papi_counter_mutex);
342 papi_start_gc_count(void)
344 ACQUIRE_LOCK(&papi_counter_mutex);
345 PAPI_CHECK( PAPI_start(GCEvents));
346 start_gc_cycles = PAPI_cycles();
347 RELEASE_LOCK(&papi_counter_mutex);
351 papi_stop_gc0_count(void)
353 ACQUIRE_LOCK(&papi_counter_mutex);
354 PAPI_CHECK( PAPI_accum(GCEvents,GC0Counters));
355 PAPI_CHECK( PAPI_stop(GCEvents,NULL));
356 gc0_cycles += PAPI_cycles() - start_gc_cycles;
357 RELEASE_LOCK(&papi_counter_mutex);
362 papi_stop_gc1_count(void)
364 ACQUIRE_LOCK(&papi_counter_mutex);
365 PAPI_CHECK( PAPI_accum(GCEvents,GC1Counters));
366 PAPI_CHECK( PAPI_stop(GCEvents,NULL));
367 gc1_cycles += PAPI_cycles() - start_gc_cycles;
368 RELEASE_LOCK(&papi_counter_mutex);
373 papi_thread_start_gc1_count(int event_set)
375 ACQUIRE_LOCK(&papi_counter_mutex);
376 PAPI_CHECK( PAPI_start(event_set));
377 RELEASE_LOCK(&papi_counter_mutex);
381 papi_thread_stop_gc1_count(int event_set)
383 ACQUIRE_LOCK(&papi_counter_mutex);
384 PAPI_CHECK( PAPI_accum(event_set,GC1Counters));
385 PAPI_CHECK( PAPI_stop(event_set,NULL));
386 RELEASE_LOCK(&papi_counter_mutex);
389 #endif /* USE_PAPI */