fix haddock submodule pointer
[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 /* The posix symbols get defined in a header included from papi.h.
19  * undefind them here to allow redefinition in PosixSource.h */
20 #undef _POSIX_SOURCE
21 #undef _POSIX_C_SOURCE
22 #undef _XOPEN_SOURCE
23
24 #include "PosixSource.h"
25 #include "Rts.h"
26
27 #include "RtsUtils.h"
28 #include "Stats.h"
29 #include "Papi.h"
30
31 // used to protect the aggregated counters
32 #ifdef THREADED_RTS
33 static Mutex papi_counter_mutex;
34 #endif
35
36 struct _papi_events {
37   int event_code;
38   const char * event_name;
39 };
40
41 /* Beware, these counters are Opteron specific
42  * I obtained the numbers using the papi_avail
43  * and papi_native_avail utilities.
44  * This is certainly not the official PAPI way
45  * of doing things.
46  */
47 #define FR_BR 0x40000040
48 #define FR_BR_MIS 0x40000041
49 #define FR_BR_MISCOMPARE 0x40000048
50 #define DC_ACCESS 0x40000019
51 #define DC_MISS 0x4000001a
52 #define FR_DISPATCH_STALLS 0x40000054
53 #define FR_DISPATCH_STALLS_BR 0x40000055
54 #define FR_DISPATCH_STALLS_FULL_REORDER 0x40000058
55 #define FR_DISPATCH_STALLS_FULL_RESERVATION 0x40000059
56 #define FR_DISPATCH_STALLS_FULL_LS 0x4000005b
57 #define DC_L2_REFILL_MOES 0x40001e1b
58 #define DC_SYS_REFILL_MOES 0x40001e1c
59
60 /* This is bad, it should be in a header */
61 #define BIG_STRING_LEN 512
62
63
64 #define PAPI_CHECK(CALL) \
65   if((papi_error=(CALL)) != PAPI_OK) { \
66    debugBelch("PAPI function failed in module %s at line %d with error code %d\n", \
67               __FILE__,__LINE__,papi_error);                            \
68   }
69
70 /* While PAPI reporting is going on this flag is on */
71 int papi_is_reporting;
72
73 /* Event sets and counter arrays for GC and mutator */
74
75 int MutatorEvents = PAPI_NULL;
76 int GCEvents = PAPI_NULL;
77
78 int papi_error;
79
80 /* Arbitrary, to avoid using malloc */
81 #define MAX_PAPI_EVENTS 10
82 static char papiNativeEventNames[MAX_PAPI_EVENTS][PAPI_MAX_STR_LEN];
83
84 static nat n_papi_events = 0;
85
86
87 /* Events counted during GC and Mutator execution */
88 /* There's a trailing comma, do all C compilers accept that? */
89 static struct _papi_events papi_events[MAX_PAPI_EVENTS];
90 long_long MutatorCounters[MAX_PAPI_EVENTS];
91 long_long GC0Counters[MAX_PAPI_EVENTS];
92 long_long GC1Counters[MAX_PAPI_EVENTS];
93
94 long_long start_mutator_cycles;
95 long_long mutator_cycles = 0;
96 long_long start_gc_cycles;
97 long_long gc0_cycles = 0;
98 long_long gc1_cycles = 0;
99
100
101
102 static long_long papi_counter(long_long values[],int event);
103 static void papi_add_events(int EventSet);
104
105 static nat max_hardware_counters = 2;
106
107 /* If you want to add events to count, extend the
108  * init_countable_events and the papi_report function.
109  * Be aware that your processor can count a limited number
110  * of events simultaneously, you can turn on multiplexing
111  * to increase that number, though.
112  */
113 static void papi_add_event(const char *name, int code)
114 {
115     if (n_papi_events >= max_hardware_counters) {
116         errorBelch("too many PAPI events for this CPU (max: %d)", 
117                    max_hardware_counters);
118         stg_exit(EXIT_FAILURE);
119     }
120     papi_events[n_papi_events].event_code = code;
121     papi_events[n_papi_events].event_name = name;
122     n_papi_events++;
123 }    
124
125 static void
126 init_countable_events(void) 
127 {
128     max_hardware_counters = PAPI_num_counters();
129
130 #define PAPI_ADD_EVENT(EVENT) papi_add_event(#EVENT,EVENT)
131
132     if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
133         PAPI_ADD_EVENT(FR_BR);
134         PAPI_ADD_EVENT(FR_BR_MIS);
135         /* Docs are wrong? Opteron does not count indirect branch misses exclusively */
136         PAPI_ADD_EVENT(FR_BR_MISCOMPARE);
137     } else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
138         PAPI_ADD_EVENT(FR_DISPATCH_STALLS);
139         PAPI_ADD_EVENT(FR_DISPATCH_STALLS_BR);
140         PAPI_ADD_EVENT(FR_DISPATCH_STALLS_FULL_LS);
141     } else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
142         PAPI_ADD_EVENT(PAPI_L1_DCA);
143         PAPI_ADD_EVENT(PAPI_L1_DCM);
144     } else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
145         PAPI_ADD_EVENT(PAPI_L2_DCA);
146         PAPI_ADD_EVENT(PAPI_L2_DCM);
147     } else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CB_EVENTS) {
148         PAPI_ADD_EVENT(DC_L2_REFILL_MOES);
149         PAPI_ADD_EVENT(DC_SYS_REFILL_MOES);
150         PAPI_ADD_EVENT(FR_BR_MIS);
151     } else if (RtsFlags.PapiFlags.eventType==PAPI_USER_EVENTS) {
152         nat i;
153         char *name;
154         char *asciiEventCode;
155         int code;
156         for (i = 0; i < RtsFlags.PapiFlags.numUserEvents; i++) {
157           if(RtsFlags.PapiFlags.userEventsKind[i] == PAPI_PRESET_EVENT_KIND) {
158             name = RtsFlags.PapiFlags.userEvents[i];
159             PAPI_CHECK(PAPI_event_name_to_code(name, &code))
160           }
161           else { // PAPI_NATIVE_EVENT_KIND
162             asciiEventCode = RtsFlags.PapiFlags.userEvents[i];
163             name = papiNativeEventNames[i];
164             code = strtol(asciiEventCode, NULL, 16 /* hex number expected */);
165             PAPI_CHECK(PAPI_event_code_to_name(code, name))
166           }
167           papi_add_event(name, code);
168         }
169     } else {
170         // PAPI_ADD_EVENT(PAPI_L1_DCA); // L1 data cache accesses
171         // PAPI_ADD_EVENT(PAPI_L1_ICR); // L1 instruction cache reads
172         // PAPI_ADD_EVENT(PAPI_L1_ICM); // L1 instruction cache misses
173         // PAPI_ADD_EVENT(PAPI_L1_STM); // L1 store misses
174         // PAPI_ADD_EVENT(PAPI_L1_DCM); // L1 data cache misses
175         // PAPI_ADD_EVENT(PAPI_L1_LDM); // L1 load misses
176         // PAPI_ADD_EVENT(PAPI_L2_TCM); // L2 cache misses
177         // PAPI_ADD_EVENT(PAPI_L2_STM); // L2 store misses
178         // PAPI_ADD_EVENT(PAPI_L2_DCW); // L2 data cache writes
179         // PAPI_ADD_EVENT(PAPI_L2_DCR); // L2 data cache reads
180         // PAPI_ADD_EVENT(PAPI_L2_TCW); // L2 cache writes
181         // PAPI_ADD_EVENT(PAPI_L2_TCR); // L2 cache reads
182         // PAPI_ADD_EVENT(PAPI_CA_CLN); // exclusive access to clean cache line
183         // PAPI_ADD_EVENT(PAPI_TLB_DM); // TLB misses
184         PAPI_ADD_EVENT(PAPI_TOT_INS); // Total instructions
185         PAPI_ADD_EVENT(PAPI_TOT_CYC); // Total instructions
186         // PAPI_ADD_EVENT(PAPI_CA_SHR); // exclusive access to shared cache line
187         // PAPI_ADD_EVENT(PAPI_RES_STL); // Cycles stalled on any resource
188         
189     }
190
191     // We might also consider:
192     //  PAPI_BR_MSP     Conditional branch instructions mispredicted
193     //  PAPI_RES_STL    Cycles stalled on any resource
194 };
195
196
197 static void
198 papi_report_event(const char *name, StgWord64 value)
199 {
200     static char temp[BIG_STRING_LEN];
201     showStgWord64(value,temp,rtsTrue/*commas*/); 
202     statsPrintf("  %15s  %15s\n", name, temp);
203 }
204
205 /* This function reports counters for GC and mutator */
206 static void
207 papi_report(long_long counters[])
208 {
209     nat i;
210
211 /* Report the value of a counter as a percentage of another counter */
212 #define PAPI_REPORT_PCT(EVENTSET,EVENT,EVENTTOT) \
213     statsPrintf("   " #EVENT " %% of " #EVENTTOT " : %.1f%%\n",      \
214          papi_counter(EVENTSET,EVENT)*100.0/papi_counter(EVENTSET,EVENTTOT))
215
216     for (i = 0; i < n_papi_events; i++)
217     {
218         papi_report_event(papi_events[i].event_name, counters[i]);
219     }
220
221     if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
222         PAPI_REPORT_PCT(counters,FR_BR_MIS,FR_BR);
223         PAPI_REPORT_PCT(counters,FR_BR_MISCOMPARE,FR_BR);
224     }
225
226     else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
227         PAPI_REPORT_PCT(counters,PAPI_L1_DCM,PAPI_L1_DCA);
228     }
229
230     else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
231         PAPI_REPORT_PCT(counters,PAPI_L2_DCM,PAPI_L2_DCA);
232     }
233 }
234
235 void
236 papi_stats_report (void)
237 {
238     statsPrintf("  Mutator CPU counters\n");
239     papi_report_event("CYCLES", mutator_cycles);
240     papi_report(MutatorCounters);
241     
242     statsPrintf("\n  GC(0) CPU counters\n");
243     papi_report_event("CYCLES", gc0_cycles);
244     papi_report(GC0Counters);
245
246     statsPrintf("\n  GC(1) CPU counters\n");
247     papi_report_event("CYCLES", gc1_cycles);
248     papi_report(GC1Counters);
249 }
250     
251 void
252 papi_init_eventset (int *event_set)
253 {
254     PAPI_register_thread();
255     PAPI_CHECK( PAPI_create_eventset(event_set));
256     papi_add_events(*event_set);
257 }
258
259 void
260 papi_init (void)
261 {
262     /* Initialise the performance tracking library */
263     int ver;
264     if ((ver = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) {
265         if (ver > 0) {
266             errorBelch("PAPI_library_init: wrong version: %x", ver);
267             stg_exit(EXIT_FAILURE);
268         } else {
269             sysErrorBelch("PAPI_library_init");
270             stg_exit(EXIT_FAILURE);
271         }
272     }
273
274 #ifdef THREADED_RTS
275     {
276         int err;
277         if ((err = PAPI_thread_init(osThreadId)) < 0) {
278             barf("PAPI_thread_init: %d",err);
279         }
280
281         initMutex(&papi_counter_mutex);
282     }
283 #endif
284
285     init_countable_events();
286
287     papi_init_eventset(&MutatorEvents);
288     papi_init_eventset(&GCEvents);
289 }
290
291 /* Extract the value corresponding to an event */
292 static long_long
293 papi_counter(long_long values[],int event)
294 {
295   nat i;
296   for(i=0;i<n_papi_events;i++) {
297     if(papi_events[i].event_code==event) {
298       return values[i];
299     }
300   }
301   /* Passed a wrong event? */
302   debugBelch("Event %d is not part of event set\n",event);
303   return 0;
304 }
305
306 /* Add the events of papi_events into an event set */
307 static void
308 papi_add_events(int EventSet)
309 {
310   nat i;
311   for(i=0;i<n_papi_events;i++) {
312     if((papi_error=PAPI_add_event(EventSet,
313                                   papi_events[i].event_code))
314        != PAPI_OK)
315       debugBelch("Failed adding %s to event set with error code %d\n",
316                  papi_events[i].event_name,papi_error);
317   }
318 }
319
320 /* We should be using elapsed cycles
321  * to be consistent with time metric chosen in Stats.c (Elapsed time).
322  * This is an approximation to the cycles that the program spends.
323  * Note that the counters, in contrast, are virtual and user space.
324  */
325 #define PAPI_cycles PAPI_get_virt_cyc
326
327 void
328 papi_start_mutator_count(void)
329 {
330     ACQUIRE_LOCK(&papi_counter_mutex);
331     PAPI_CHECK( PAPI_start(MutatorEvents));
332     start_mutator_cycles = PAPI_cycles();
333     RELEASE_LOCK(&papi_counter_mutex);
334 }
335
336 void
337 papi_stop_mutator_count(void)
338 {
339     ACQUIRE_LOCK(&papi_counter_mutex);
340     mutator_cycles += PAPI_cycles() - start_mutator_cycles;
341     PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
342     PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
343     RELEASE_LOCK(&papi_counter_mutex);
344 }
345
346 void
347 papi_start_gc_count(void)
348 {
349     ACQUIRE_LOCK(&papi_counter_mutex);
350     PAPI_CHECK( PAPI_start(GCEvents));
351     start_gc_cycles = PAPI_cycles();
352     RELEASE_LOCK(&papi_counter_mutex);
353 }
354
355 void
356 papi_stop_gc0_count(void)
357 {
358     ACQUIRE_LOCK(&papi_counter_mutex);
359     PAPI_CHECK( PAPI_accum(GCEvents,GC0Counters));
360     PAPI_CHECK( PAPI_stop(GCEvents,NULL));
361     gc0_cycles += PAPI_cycles() - start_gc_cycles;
362     RELEASE_LOCK(&papi_counter_mutex);
363 }
364
365
366 void
367 papi_stop_gc1_count(void)
368 {
369     ACQUIRE_LOCK(&papi_counter_mutex);
370     PAPI_CHECK( PAPI_accum(GCEvents,GC1Counters));
371     PAPI_CHECK( PAPI_stop(GCEvents,NULL));
372     gc1_cycles += PAPI_cycles() - start_gc_cycles;
373     RELEASE_LOCK(&papi_counter_mutex);
374 }
375
376
377 void
378 papi_thread_start_gc1_count(int event_set)
379 {
380     ACQUIRE_LOCK(&papi_counter_mutex);
381     PAPI_CHECK( PAPI_start(event_set));
382     RELEASE_LOCK(&papi_counter_mutex);
383 }
384
385 void
386 papi_thread_stop_gc1_count(int event_set)
387 {
388     ACQUIRE_LOCK(&papi_counter_mutex);
389     PAPI_CHECK( PAPI_accum(event_set,GC1Counters));
390     PAPI_CHECK( PAPI_stop(event_set,NULL));
391     RELEASE_LOCK(&papi_counter_mutex);
392 }
393
394 #endif /* USE_PAPI */