Add support for collecting PAPI native events
[ghc-hetmet.git] / rts / Papi.c
1 /* -----------------------------------------------------------------------------
2  * (c) The GHC Team 2006
3  * 
4  * Initialization and use of the PAPI performance monitoring library
5  *
6  *
7  * For adding events or add your processor counters modify
8  *
9  *   init_countable_events
10  *   papi_report
11  *
12  * ---------------------------------------------------------------------------*/
13
14
15 #ifdef USE_PAPI /* ugly */
16
17 #include <papi.h>
18
19 #include "PosixSource.h"
20 #include "Rts.h"
21
22 #include "RtsUtils.h"
23 #include "Stats.h"
24 #include "Papi.h"
25
26 // used to protect the aggregated counters
27 #ifdef THREADED_RTS
28 static Mutex papi_counter_mutex;
29 #endif
30
31 struct _papi_events {
32   int event_code;
33   const char * event_name;
34 };
35
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
40  * of doing things.
41  */
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
54
55 /* This is bad, it should be in a header */
56 #define BIG_STRING_LEN 512
57
58
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);                            \
63   }
64
65 /* While PAPI reporting is going on this flag is on */
66 int papi_is_reporting;
67
68 /* Event sets and counter arrays for GC and mutator */
69
70 int MutatorEvents = PAPI_NULL;
71 int GCEvents = PAPI_NULL;
72
73 int papi_error;
74
75 /* Arbitrary, to avoid using malloc */
76 #define MAX_PAPI_EVENTS 10
77 static char papiNativeEventNames[MAX_PAPI_EVENTS][PAPI_MAX_STR_LEN];
78
79 static nat n_papi_events = 0;
80
81
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];
88
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;
94
95
96
97 static long_long papi_counter(long_long values[],int event);
98 static void papi_add_events(int EventSet);
99
100 static nat max_hardware_counters = 2;
101
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.
107  */
108 static void papi_add_event(const char *name, int code)
109 {
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);
114     }
115     papi_events[n_papi_events].event_code = code;
116     papi_events[n_papi_events].event_name = name;
117     n_papi_events++;
118 }    
119
120 static void
121 init_countable_events(void) 
122 {
123     max_hardware_counters = PAPI_num_counters();
124
125 #define PAPI_ADD_EVENT(EVENT) papi_add_event(#EVENT,EVENT)
126
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) {
147         nat i;
148         char *name;
149         char *asciiEventCode;
150         int code;
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))
155           }
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))
161           }
162           papi_add_event(name, code);
163         }
164     } else {
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
183         
184     }
185
186     // We might also consider:
187     //  PAPI_BR_MSP     Conditional branch instructions mispredicted
188     //  PAPI_RES_STL    Cycles stalled on any resource
189 };
190
191
192 static void
193 papi_report_event(const char *name, StgWord64 value)
194 {
195     static char temp[BIG_STRING_LEN];
196     showStgWord64(value,temp,rtsTrue/*commas*/); 
197     statsPrintf("  %15s  %15s\n", name, temp);
198 }
199
200 /* This function reports counters for GC and mutator */
201 static void
202 papi_report(long_long counters[])
203 {
204     nat i;
205
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))
210
211     for (i = 0; i < n_papi_events; i++)
212     {
213         papi_report_event(papi_events[i].event_name, counters[i]);
214     }
215
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);
219     }
220
221     else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
222         PAPI_REPORT_PCT(counters,PAPI_L1_DCM,PAPI_L1_DCA);
223     }
224
225     else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
226         PAPI_REPORT_PCT(counters,PAPI_L2_DCM,PAPI_L2_DCA);
227     }
228 }
229
230 void
231 papi_stats_report (void)
232 {
233     statsPrintf("  Mutator CPU counters\n");
234     papi_report_event("CYCLES", mutator_cycles);
235     papi_report(MutatorCounters);
236     
237     statsPrintf("\n  GC(0) CPU counters\n");
238     papi_report_event("CYCLES", gc0_cycles);
239     papi_report(GC0Counters);
240
241     statsPrintf("\n  GC(1) CPU counters\n");
242     papi_report_event("CYCLES", gc1_cycles);
243     papi_report(GC1Counters);
244 }
245     
246 void
247 papi_init_eventset (int *event_set)
248 {
249     PAPI_register_thread();
250     PAPI_CHECK( PAPI_create_eventset(event_set));
251     papi_add_events(*event_set);
252 }
253
254 void
255 papi_init (void)
256 {
257     /* Initialise the performance tracking library */
258     int ver;
259     if ((ver = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) {
260         if (ver > 0) {
261             errorBelch("PAPI_library_init: wrong version: %x", ver);
262             stg_exit(EXIT_FAILURE);
263         } else {
264             sysErrorBelch("PAPI_library_init");
265             stg_exit(EXIT_FAILURE);
266         }
267     }
268
269 #ifdef THREADED_RTS
270     {
271         int err;
272         if ((err = PAPI_thread_init(osThreadId)) < 0) {
273             barf("PAPI_thread_init: %d",err);
274         }
275
276         initMutex(&papi_counter_mutex);
277     }
278 #endif
279
280     init_countable_events();
281
282     papi_init_eventset(&MutatorEvents);
283     papi_init_eventset(&GCEvents);
284 }
285
286 /* Extract the value corresponding to an event */
287 static long_long
288 papi_counter(long_long values[],int event)
289 {
290   nat i;
291   for(i=0;i<n_papi_events;i++) {
292     if(papi_events[i].event_code==event) {
293       return values[i];
294     }
295   }
296   /* Passed a wrong event? */
297   debugBelch("Event %d is not part of event set\n",event);
298   return 0;
299 }
300
301 /* Add the events of papi_events into an event set */
302 static void
303 papi_add_events(int EventSet)
304 {
305   nat i;
306   for(i=0;i<n_papi_events;i++) {
307     if((papi_error=PAPI_add_event(EventSet,
308                                   papi_events[i].event_code))
309        != PAPI_OK)
310       debugBelch("Failed adding %s to event set with error code %d\n",
311                  papi_events[i].event_name,papi_error);
312   }
313 }
314
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.
319  */
320 #define PAPI_cycles PAPI_get_virt_cyc
321
322 void
323 papi_start_mutator_count(void)
324 {
325     ACQUIRE_LOCK(&papi_counter_mutex);
326     PAPI_CHECK( PAPI_start(MutatorEvents));
327     start_mutator_cycles = PAPI_cycles();
328     RELEASE_LOCK(&papi_counter_mutex);
329 }
330
331 void
332 papi_stop_mutator_count(void)
333 {
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);
339 }
340
341 void
342 papi_start_gc_count(void)
343 {
344     ACQUIRE_LOCK(&papi_counter_mutex);
345     PAPI_CHECK( PAPI_start(GCEvents));
346     start_gc_cycles = PAPI_cycles();
347     RELEASE_LOCK(&papi_counter_mutex);
348 }
349
350 void
351 papi_stop_gc0_count(void)
352 {
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);
358 }
359
360
361 void
362 papi_stop_gc1_count(void)
363 {
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);
369 }
370
371
372 void
373 papi_thread_start_gc1_count(int event_set)
374 {
375     ACQUIRE_LOCK(&papi_counter_mutex);
376     PAPI_CHECK( PAPI_start(event_set));
377     RELEASE_LOCK(&papi_counter_mutex);
378 }
379
380 void
381 papi_thread_stop_gc1_count(int event_set)
382 {
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);
387 }
388
389 #endif /* USE_PAPI */