Cosmetic improvements, no change in Papi functionality.
[ghc-hetmet.git] / rts / Papi.c
1
2 /* -----------------------------------------------------------------------------
3  * (c) The GHC Team 2006
4  * 
5  * Initialization and use of the PAPI performance monitoring library
6  *
7  *
8  * For adding events or add your processor counters modify
9  *
10  *   init_countable_events
11  *   papi_report
12  *
13  * ---------------------------------------------------------------------------*/
14
15
16 #ifdef USE_PAPI /* ugly */
17
18 #include "Papi.h"
19 #include "Rts.h"
20 #include "RtsUtils.h"
21 #include "Stats.h"
22 #include "RtsFlags.h"
23
24
25 struct _papi_events {
26   int event_code;
27   char * event_name;
28 };
29
30 #define PAPI_ADD_EVENT(EVENT) \
31     {                         \
32         ASSERT(n_papi_events<MAX_PAPI_EVENTS);     \
33         papi_events[n_papi_events].event_code = EVENT;  \
34         papi_events[n_papi_events].event_name = #EVENT; \
35         n_papi_events++;                                \
36     }
37
38 /* Report the value of a counter */
39 #define PAPI_REPORT(EVENTSET,EVENT) \
40   { \
41     ullong_format_string(papi_counter(EVENTSET,EVENT),temp,rtsTrue/*commas*/); \
42     statsPrintf("  (" #EVENT ")  : %s\n",temp);                         \
43   }
44
45 /* Report the value of a counter as a percentage of another counter */
46 #define PAPI_REPORT_PCT(EVENTSET,EVENT,EVENTTOT) \
47   statsPrintf("  (" #EVENT ") %% of (" #EVENTTOT ") : %.1f%%\n", \
48               papi_counter(EVENTSET,EVENT)*100.0/papi_counter(EVENTSET,EVENTTOT))
49
50 /* Beware, these counters are Opteron specific
51  * I obtained the numbers using the papi_avail
52  * and papi_native_avail utilities.
53  * This is certainly not the official PAPI way
54  * of doing things.
55  */
56 #define FR_BR 0x40000040
57 #define FR_BR_MIS 0x40000041
58 #define FR_BR_MISCOMPARE 0x40000048
59 #define DC_ACCESS 0x40000019
60 #define DC_MISS 0x4000001a
61 #define FR_DISPATCH_STALLS_BR 0x40000055
62 #define FR_DISPATCH_STALLS_FULL_LS 0x4000005b
63
64 /* Number of counted events, computed from size of papi_events */
65 #define N_PAPI_EVENTS n_papi_events
66
67 /* This is bad, it should be in a header */
68 #define BIG_STRING_LEN 512
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
83 int n_papi_events = 0;
84
85
86 /* Events counted during GC and Mutator execution */
87 /* There's a trailing comma, do all C compilers accept that? */
88 static struct _papi_events papi_events[MAX_PAPI_EVENTS];
89 long_long MutatorCounters[MAX_PAPI_EVENTS];
90 long_long GCCounters[MAX_PAPI_EVENTS];
91
92 /* If you want to add events to count, extend the
93  * init_countable_events and the papi_report function.
94  * Be aware that your processor can count a limited number
95  * of events simultaneously, you can turn on multiplexing
96  * to increase that number, though.
97  */
98 static void
99 init_countable_events(void) 
100 {
101     PAPI_ADD_EVENT(PAPI_TOT_CYC);
102     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
103         PAPI_ADD_EVENT(FR_BR);
104         PAPI_ADD_EVENT(FR_BR_MIS);
105         /* Docs are wrong? Opteron does not count indirect branch misses exclusively */
106         PAPI_ADD_EVENT(FR_BR_MISCOMPARE);
107     }
108     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
109         PAPI_ADD_EVENT(FR_DISPATCH_STALLS_BR);
110         PAPI_ADD_EVENT(FR_DISPATCH_STALLS_FULL_LS);
111     }
112     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
113         PAPI_ADD_EVENT(PAPI_L1_DCA);
114         PAPI_ADD_EVENT(PAPI_L1_DCM);
115     }
116     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
117         PAPI_ADD_EVENT(PAPI_L2_DCA);
118         PAPI_ADD_EVENT(PAPI_L2_DCM);
119     }
120 };
121
122 /* This function reports counters for GC and mutator */
123 void
124 papi_report(long_long PapiCounters[])
125 {
126     char temp[BIG_STRING_LEN];
127
128     /* I need to improve formatting aesthetics */
129     PAPI_REPORT(PapiCounters,PAPI_TOT_CYC);
130
131     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
132         PAPI_REPORT(PapiCounters,FR_BR);
133         PAPI_REPORT(PapiCounters,FR_BR_MIS);
134         PAPI_REPORT_PCT(PapiCounters,FR_BR_MIS,FR_BR);
135         PAPI_REPORT_PCT(PapiCounters,FR_BR_MISCOMPARE,FR_BR);
136     }
137
138     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
139         PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_BR);
140         PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_BR,PAPI_TOT_CYC);
141         PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS);
142         PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS,PAPI_TOT_CYC);
143     }
144
145     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
146         PAPI_REPORT(PapiCounters,PAPI_L1_DCA);
147         PAPI_REPORT(PapiCounters,PAPI_L1_DCM);
148         PAPI_REPORT_PCT(PapiCounters,PAPI_L1_DCM,PAPI_L1_DCA);
149     }
150
151     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
152         PAPI_REPORT(PapiCounters,PAPI_L2_DCA);
153         PAPI_REPORT(PapiCounters,PAPI_L2_DCM);
154         PAPI_REPORT_PCT(PapiCounters,PAPI_L2_DCM,PAPI_L2_DCA);
155     }
156
157 }
158
159
160
161 void
162 papi_init_eventsets(void)
163 {
164
165     init_countable_events();
166
167     /* One event set for the mutator and another for the GC */
168     PAPI_CHECK( PAPI_create_eventset(&MutatorEvents));
169     PAPI_CHECK( PAPI_create_eventset(&GCEvents));
170
171     /* Both sets contain the same events */
172     papi_add_events(MutatorEvents);
173     papi_add_events(GCEvents);
174
175 }
176
177 /* Extract the value corresponding to an event */
178 long_long
179 papi_counter(long_long values[],int event)
180 {
181   int i;
182   for(i=0;i<N_PAPI_EVENTS;i++) {
183     if(papi_events[i].event_code==event) {
184       return values[i];
185     }
186   }
187   /* Passed a wrong event? */
188   debugBelch("Event %d is not part of event set\n",event);
189   return 0;
190 }
191
192 /* Add the events of papi_events into an event set */
193 void
194 papi_add_events(int EventSet)
195 {
196   int i;
197   for(i=0;i<N_PAPI_EVENTS;i++) {
198     if((papi_error=PAPI_add_event(EventSet,
199                                   papi_events[i].event_code))
200        != PAPI_OK)
201       debugBelch("Failed adding %s to event set with error code %d\n",
202                  papi_events[i].event_name,papi_error);
203   }
204 }
205
206 void
207 papi_start_mutator_count(void)
208 {
209     PAPI_CHECK( PAPI_start(MutatorEvents));
210 }
211
212 void
213 papi_stop_mutator_count(void)
214 {
215     PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
216     PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
217 }
218
219 void
220 papi_start_gc_count(void)
221 {
222       PAPI_CHECK( PAPI_start(GCEvents));
223 }
224
225 void
226 papi_stop_gc_count(void)
227 {
228       PAPI_CHECK( PAPI_accum(GCEvents,GCCounters));
229       PAPI_CHECK( PAPI_stop(GCEvents,NULL));
230 }
231
232
233 #endif /* USE_PAPI */