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 */
18 /* The posix symbols get defined in a header included from papi.h.
19 * undefind them here to allow redefinition in PosixSource.h */
21 #undef _POSIX_C_SOURCE
24 #include "PosixSource.h"
31 // used to protect the aggregated counters
33 static Mutex papi_counter_mutex;
38 const char * event_name;
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
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
60 /* This is bad, it should be in a header */
61 #define BIG_STRING_LEN 512
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); \
70 /* While PAPI reporting is going on this flag is on */
71 int papi_is_reporting;
73 /* Event sets and counter arrays for GC and mutator */
75 int MutatorEvents = PAPI_NULL;
76 int GCEvents = PAPI_NULL;
80 /* Arbitrary, to avoid using malloc */
81 #define MAX_PAPI_EVENTS 10
82 static char papiNativeEventNames[MAX_PAPI_EVENTS][PAPI_MAX_STR_LEN];
84 static nat n_papi_events = 0;
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];
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;
102 static long_long papi_counter(long_long values[],int event);
103 static void papi_add_events(int EventSet);
105 static nat max_hardware_counters = 2;
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.
113 static void papi_add_event(const char *name, int code)
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);
120 papi_events[n_papi_events].event_code = code;
121 papi_events[n_papi_events].event_name = name;
126 init_countable_events(void)
128 max_hardware_counters = PAPI_num_counters();
130 #define PAPI_ADD_EVENT(EVENT) papi_add_event(#EVENT,EVENT)
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) {
154 char *asciiEventCode;
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))
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))
167 papi_add_event(name, code);
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
191 // We might also consider:
192 // PAPI_BR_MSP Conditional branch instructions mispredicted
193 // PAPI_RES_STL Cycles stalled on any resource
198 papi_report_event(const char *name, StgWord64 value)
200 static char temp[BIG_STRING_LEN];
201 showStgWord64(value,temp,rtsTrue/*commas*/);
202 statsPrintf(" %15s %15s\n", name, temp);
205 /* This function reports counters for GC and mutator */
207 papi_report(long_long counters[])
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))
216 for (i = 0; i < n_papi_events; i++)
218 papi_report_event(papi_events[i].event_name, counters[i]);
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);
226 else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
227 PAPI_REPORT_PCT(counters,PAPI_L1_DCM,PAPI_L1_DCA);
230 else if (RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
231 PAPI_REPORT_PCT(counters,PAPI_L2_DCM,PAPI_L2_DCA);
236 papi_stats_report (void)
238 statsPrintf(" Mutator CPU counters\n");
239 papi_report_event("CYCLES", mutator_cycles);
240 papi_report(MutatorCounters);
242 statsPrintf("\n GC(0) CPU counters\n");
243 papi_report_event("CYCLES", gc0_cycles);
244 papi_report(GC0Counters);
246 statsPrintf("\n GC(1) CPU counters\n");
247 papi_report_event("CYCLES", gc1_cycles);
248 papi_report(GC1Counters);
252 papi_init_eventset (int *event_set)
254 PAPI_register_thread();
255 PAPI_CHECK( PAPI_create_eventset(event_set));
256 papi_add_events(*event_set);
262 /* Initialise the performance tracking library */
264 if ((ver = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) {
266 errorBelch("PAPI_library_init: wrong version: %x", ver);
267 stg_exit(EXIT_FAILURE);
269 sysErrorBelch("PAPI_library_init");
270 stg_exit(EXIT_FAILURE);
277 if ((err = PAPI_thread_init(osThreadId)) < 0) {
278 barf("PAPI_thread_init: %d",err);
281 initMutex(&papi_counter_mutex);
285 init_countable_events();
287 papi_init_eventset(&MutatorEvents);
288 papi_init_eventset(&GCEvents);
291 /* Extract the value corresponding to an event */
293 papi_counter(long_long values[],int event)
296 for(i=0;i<n_papi_events;i++) {
297 if(papi_events[i].event_code==event) {
301 /* Passed a wrong event? */
302 debugBelch("Event %d is not part of event set\n",event);
306 /* Add the events of papi_events into an event set */
308 papi_add_events(int EventSet)
311 for(i=0;i<n_papi_events;i++) {
312 if((papi_error=PAPI_add_event(EventSet,
313 papi_events[i].event_code))
315 debugBelch("Failed adding %s to event set with error code %d\n",
316 papi_events[i].event_name,papi_error);
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.
325 #define PAPI_cycles PAPI_get_virt_cyc
328 papi_start_mutator_count(void)
330 ACQUIRE_LOCK(&papi_counter_mutex);
331 PAPI_CHECK( PAPI_start(MutatorEvents));
332 start_mutator_cycles = PAPI_cycles();
333 RELEASE_LOCK(&papi_counter_mutex);
337 papi_stop_mutator_count(void)
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);
347 papi_start_gc_count(void)
349 ACQUIRE_LOCK(&papi_counter_mutex);
350 PAPI_CHECK( PAPI_start(GCEvents));
351 start_gc_cycles = PAPI_cycles();
352 RELEASE_LOCK(&papi_counter_mutex);
356 papi_stop_gc0_count(void)
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);
367 papi_stop_gc1_count(void)
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);
378 papi_thread_start_gc1_count(int event_set)
380 ACQUIRE_LOCK(&papi_counter_mutex);
381 PAPI_CHECK( PAPI_start(event_set));
382 RELEASE_LOCK(&papi_counter_mutex);
386 papi_thread_stop_gc1_count(int event_set)
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);
394 #endif /* USE_PAPI */