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_BR 0x40000055
61 #define FR_DISPATCH_STALLS_FULL_LS 0x4000005b
62 #define DC_L2_REFILL_MOES 0x40001e1b
63 #define DC_SYS_REFILL_MOES 0x40001e1c
65 /* Number of counted events, computed from size of papi_events */
66 #define N_PAPI_EVENTS n_papi_events
68 /* This is bad, it should be in a header */
69 #define BIG_STRING_LEN 512
71 /* While PAPI reporting is going on this flag is on */
72 int papi_is_reporting;
74 /* Event sets and counter arrays for GC and mutator */
76 int MutatorEvents = PAPI_NULL;
77 int GCEvents = PAPI_NULL;
81 /* Arbitrary, to avoid using malloc */
82 #define MAX_PAPI_EVENTS 10
84 int 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 GCCounters[MAX_PAPI_EVENTS];
93 long_long start_mutator_cycles;
94 long_long start_gc_cycles;
95 long_long mutator_cycles;
100 /* If you want to add events to count, extend the
101 * init_countable_events and the papi_report function.
102 * Be aware that your processor can count a limited number
103 * of events simultaneously, you can turn on multiplexing
104 * to increase that number, though.
107 init_countable_events(void)
109 PAPI_ADD_EVENT(PAPI_TOT_INS);
110 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
111 PAPI_ADD_EVENT(FR_BR);
112 PAPI_ADD_EVENT(FR_BR_MIS);
113 /* Docs are wrong? Opteron does not count indirect branch misses exclusively */
114 PAPI_ADD_EVENT(FR_BR_MISCOMPARE);
116 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
117 PAPI_ADD_EVENT(FR_DISPATCH_STALLS_BR);
118 PAPI_ADD_EVENT(FR_DISPATCH_STALLS_FULL_LS);
120 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
121 PAPI_ADD_EVENT(PAPI_L1_DCA);
122 PAPI_ADD_EVENT(PAPI_L1_DCM);
124 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
125 PAPI_ADD_EVENT(PAPI_L2_DCA);
126 PAPI_ADD_EVENT(PAPI_L2_DCM);
128 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CB_EVENTS) {
129 PAPI_ADD_EVENT(DC_L2_REFILL_MOES);
130 PAPI_ADD_EVENT(DC_SYS_REFILL_MOES);
131 PAPI_ADD_EVENT(FR_BR_MIS);
136 static char temp[BIG_STRING_LEN];
141 ullong_format_string(mutator_cycles,temp,rtsTrue/*commas*/);
142 statsPrintf(" (MUT_CYCLES) : %s\n",temp);
148 ullong_format_string(gc_cycles,temp,rtsTrue/*commas*/);
149 statsPrintf(" (GC_CYCLES) : %s\n",temp);
152 /* This function reports counters for GC and mutator */
154 papi_report(long_long PapiCounters[])
157 /* I need to improve formatting aesthetics */
158 PAPI_REPORT(PapiCounters,PAPI_TOT_INS);
160 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
161 PAPI_REPORT(PapiCounters,FR_BR);
162 PAPI_REPORT(PapiCounters,FR_BR_MIS);
163 PAPI_REPORT_PCT(PapiCounters,FR_BR_MIS,FR_BR);
164 PAPI_REPORT_PCT(PapiCounters,FR_BR_MISCOMPARE,FR_BR);
167 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
168 PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_BR);
169 //PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_BR,PAPI_TOT_CYC);
170 PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS);
171 //PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS,PAPI_TOT_CYC);
174 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
175 PAPI_REPORT(PapiCounters,PAPI_L1_DCA);
176 PAPI_REPORT(PapiCounters,PAPI_L1_DCM);
177 PAPI_REPORT_PCT(PapiCounters,PAPI_L1_DCM,PAPI_L1_DCA);
180 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
181 PAPI_REPORT(PapiCounters,PAPI_L2_DCA);
182 PAPI_REPORT(PapiCounters,PAPI_L2_DCM);
183 PAPI_REPORT_PCT(PapiCounters,PAPI_L2_DCM,PAPI_L2_DCA);
186 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CB_EVENTS) {
187 PAPI_REPORT(PapiCounters,DC_L2_REFILL_MOES);
188 PAPI_REPORT(PapiCounters,DC_SYS_REFILL_MOES);
189 PAPI_REPORT(PapiCounters,FR_BR_MIS);
197 papi_init_eventsets(void)
200 init_countable_events();
202 /* One event set for the mutator and another for the GC */
203 PAPI_CHECK( PAPI_create_eventset(&MutatorEvents));
204 PAPI_CHECK( PAPI_create_eventset(&GCEvents));
206 /* Both sets contain the same events */
207 papi_add_events(MutatorEvents);
208 papi_add_events(GCEvents);
212 /* Extract the value corresponding to an event */
214 papi_counter(long_long values[],int event)
217 for(i=0;i<N_PAPI_EVENTS;i++) {
218 if(papi_events[i].event_code==event) {
222 /* Passed a wrong event? */
223 debugBelch("Event %d is not part of event set\n",event);
227 /* Add the events of papi_events into an event set */
229 papi_add_events(int EventSet)
232 for(i=0;i<N_PAPI_EVENTS;i++) {
233 if((papi_error=PAPI_add_event(EventSet,
234 papi_events[i].event_code))
236 debugBelch("Failed adding %s to event set with error code %d\n",
237 papi_events[i].event_name,papi_error);
241 /* We should be using elapsed cycles
242 * to be consistent with time metric chosen in Stats.c (Elapsed time).
243 * This is an approximation to the cycles that the program spends.
244 * Note that the counters, in contrast, are virtual and user space.
246 #define PAPI_cycles PAPI_get_virt_cyc
249 papi_start_mutator_count(void)
251 PAPI_CHECK( PAPI_start(MutatorEvents));
252 start_mutator_cycles = PAPI_cycles();
256 papi_stop_mutator_count(void)
258 mutator_cycles += PAPI_cycles() - start_mutator_cycles;
259 PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
260 PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
264 papi_start_gc_count(void)
266 PAPI_CHECK( PAPI_start(GCEvents));
267 start_gc_cycles = PAPI_cycles();
271 papi_stop_gc_count(void)
273 gc_cycles += PAPI_cycles() - start_gc_cycles;
274 PAPI_CHECK( PAPI_accum(GCEvents,GCCounters));
275 PAPI_CHECK( PAPI_stop(GCEvents,NULL));
279 #endif /* USE_PAPI */