also count total dispatch stalls in +RTS -as
[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 #include "Rts.h"
19 #include "RtsUtils.h"
20 #include "Stats.h"
21 #include "RtsFlags.h"
22
23
24 struct _papi_events {
25   int event_code;
26   char * event_name;
27 };
28
29 #define PAPI_ADD_EVENT(EVENT) \
30     {                         \
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; \
34         n_papi_events++;                                \
35     }
36
37 /* Report the value of a counter */
38 #define PAPI_REPORT(EVENTSET,EVENT) \
39   { \
40     ullong_format_string(papi_counter(EVENTSET,EVENT),temp,rtsTrue/*commas*/); \
41     statsPrintf("  (" #EVENT ")  : %s\n",temp);                         \
42   }
43
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))
48
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
53  * of doing things.
54  */
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 0x40000054
61 #define FR_DISPATCH_STALLS_BR 0x40000055
62 #define FR_DISPATCH_STALLS_FULL_REORDER 0x40000058
63 #define FR_DISPATCH_STALLS_FULL_RESERVATION 0x40000059
64 #define FR_DISPATCH_STALLS_FULL_LS 0x4000005b
65 #define DC_L2_REFILL_MOES 0x40001e1b
66 #define DC_SYS_REFILL_MOES 0x40001e1c
67
68 /* Number of counted events, computed from size of papi_events */
69 #define N_PAPI_EVENTS n_papi_events
70
71 /* This is bad, it should be in a header */
72 #define BIG_STRING_LEN 512
73
74 /* While PAPI reporting is going on this flag is on */
75 int papi_is_reporting;
76
77 /* Event sets and counter arrays for GC and mutator */
78
79 int MutatorEvents = PAPI_NULL;
80 int GCEvents = PAPI_NULL;
81
82 int papi_error;
83
84 /* Arbitrary, to avoid using malloc */
85 #define MAX_PAPI_EVENTS 10
86
87 int n_papi_events = 0;
88
89
90 /* Events counted during GC and Mutator execution */
91 /* There's a trailing comma, do all C compilers accept that? */
92 static struct _papi_events papi_events[MAX_PAPI_EVENTS];
93 long_long MutatorCounters[MAX_PAPI_EVENTS];
94 long_long GCCounters[MAX_PAPI_EVENTS];
95
96 long_long start_mutator_cycles;
97 long_long start_gc_cycles;
98 long_long mutator_cycles;
99 long_long gc_cycles;
100
101
102
103 /* If you want to add events to count, extend the
104  * init_countable_events and the papi_report function.
105  * Be aware that your processor can count a limited number
106  * of events simultaneously, you can turn on multiplexing
107  * to increase that number, though.
108  */
109 static void
110 init_countable_events(void) 
111 {
112     PAPI_ADD_EVENT(PAPI_TOT_INS);
113     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
114         PAPI_ADD_EVENT(FR_BR);
115         PAPI_ADD_EVENT(FR_BR_MIS);
116         /* Docs are wrong? Opteron does not count indirect branch misses exclusively */
117         PAPI_ADD_EVENT(FR_BR_MISCOMPARE);
118     }
119     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
120         PAPI_ADD_EVENT(FR_DISPATCH_STALLS);
121         PAPI_ADD_EVENT(FR_DISPATCH_STALLS_BR);
122         PAPI_ADD_EVENT(FR_DISPATCH_STALLS_FULL_LS);
123     }
124     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
125         PAPI_ADD_EVENT(PAPI_L1_DCA);
126         PAPI_ADD_EVENT(PAPI_L1_DCM);
127     }
128     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
129         PAPI_ADD_EVENT(PAPI_L2_DCA);
130         PAPI_ADD_EVENT(PAPI_L2_DCM);
131     }
132     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CB_EVENTS) {
133         PAPI_ADD_EVENT(DC_L2_REFILL_MOES);
134         PAPI_ADD_EVENT(DC_SYS_REFILL_MOES);
135         PAPI_ADD_EVENT(FR_BR_MIS);
136     }
137 };
138
139
140 static char temp[BIG_STRING_LEN];
141
142 void
143 papi_mut_cycles()
144 {
145     ullong_format_string(mutator_cycles,temp,rtsTrue/*commas*/); 
146     statsPrintf("  (MUT_CYCLES)  : %s\n",temp);
147 }
148
149 void
150 papi_gc_cycles()
151 {
152     ullong_format_string(gc_cycles,temp,rtsTrue/*commas*/); 
153     statsPrintf("  (GC_CYCLES)  : %s\n",temp);
154 }
155
156 /* This function reports counters for GC and mutator */
157 void
158 papi_report(long_long PapiCounters[])
159 {
160
161     /* I need to improve formatting aesthetics */
162     PAPI_REPORT(PapiCounters,PAPI_TOT_INS);
163
164     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
165         PAPI_REPORT(PapiCounters,FR_BR);
166         PAPI_REPORT(PapiCounters,FR_BR_MIS);
167         PAPI_REPORT_PCT(PapiCounters,FR_BR_MIS,FR_BR);
168         PAPI_REPORT_PCT(PapiCounters,FR_BR_MISCOMPARE,FR_BR);
169     }
170
171     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
172         PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS);
173         PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_BR);
174         //PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_BR,PAPI_TOT_CYC);
175         PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS);
176         //PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS,PAPI_TOT_CYC);
177     }
178
179     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
180         PAPI_REPORT(PapiCounters,PAPI_L1_DCA);
181         PAPI_REPORT(PapiCounters,PAPI_L1_DCM);
182         PAPI_REPORT_PCT(PapiCounters,PAPI_L1_DCM,PAPI_L1_DCA);
183     }
184
185     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
186         PAPI_REPORT(PapiCounters,PAPI_L2_DCA);
187         PAPI_REPORT(PapiCounters,PAPI_L2_DCM);
188         PAPI_REPORT_PCT(PapiCounters,PAPI_L2_DCM,PAPI_L2_DCA);
189     }
190
191     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CB_EVENTS) {
192         PAPI_REPORT(PapiCounters,DC_L2_REFILL_MOES);
193         PAPI_REPORT(PapiCounters,DC_SYS_REFILL_MOES);
194         PAPI_REPORT(PapiCounters,FR_BR_MIS);
195     }
196
197 }
198
199
200
201 void
202 papi_init_eventsets(void)
203 {
204
205     init_countable_events();
206
207     /* One event set for the mutator and another for the GC */
208     PAPI_CHECK( PAPI_create_eventset(&MutatorEvents));
209     PAPI_CHECK( PAPI_create_eventset(&GCEvents));
210
211     /* Both sets contain the same events */
212     papi_add_events(MutatorEvents);
213     papi_add_events(GCEvents);
214
215 }
216
217 /* Extract the value corresponding to an event */
218 long_long
219 papi_counter(long_long values[],int event)
220 {
221   int i;
222   for(i=0;i<N_PAPI_EVENTS;i++) {
223     if(papi_events[i].event_code==event) {
224       return values[i];
225     }
226   }
227   /* Passed a wrong event? */
228   debugBelch("Event %d is not part of event set\n",event);
229   return 0;
230 }
231
232 /* Add the events of papi_events into an event set */
233 void
234 papi_add_events(int EventSet)
235 {
236   int i;
237   for(i=0;i<N_PAPI_EVENTS;i++) {
238     if((papi_error=PAPI_add_event(EventSet,
239                                   papi_events[i].event_code))
240        != PAPI_OK)
241       debugBelch("Failed adding %s to event set with error code %d\n",
242                  papi_events[i].event_name,papi_error);
243   }
244 }
245
246 /* We should be using elapsed cycles
247  * to be consistent with time metric chosen in Stats.c (Elapsed time).
248  * This is an approximation to the cycles that the program spends.
249  * Note that the counters, in contrast, are virtual and user space.
250  */
251 #define PAPI_cycles PAPI_get_virt_cyc
252
253 void
254 papi_start_mutator_count(void)
255 {
256     PAPI_CHECK( PAPI_start(MutatorEvents));
257     start_mutator_cycles = PAPI_cycles();
258 }
259
260 void
261 papi_stop_mutator_count(void)
262 {
263     mutator_cycles += PAPI_cycles() - start_mutator_cycles;
264     PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
265     PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
266 }
267
268 void
269 papi_start_gc_count(void)
270 {
271       PAPI_CHECK( PAPI_start(GCEvents));
272       start_gc_cycles = PAPI_cycles();
273 }
274
275 void
276 papi_stop_gc_count(void)
277 {
278       gc_cycles += PAPI_cycles() - start_gc_cycles;
279       PAPI_CHECK( PAPI_accum(GCEvents,GCCounters));
280       PAPI_CHECK( PAPI_stop(GCEvents,NULL));
281 }
282
283
284 #endif /* USE_PAPI */