Fixed uninitialised FunBind fun_tick field
[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_BR 0x40000055
61 #define FR_DISPATCH_STALLS_FULL_LS 0x4000005b
62
63 /* Number of counted events, computed from size of papi_events */
64 #define N_PAPI_EVENTS n_papi_events
65
66 /* This is bad, it should be in a header */
67 #define BIG_STRING_LEN 512
68
69 /* While PAPI reporting is going on this flag is on */
70 int papi_is_reporting;
71
72 /* Event sets and counter arrays for GC and mutator */
73
74 int MutatorEvents = PAPI_NULL;
75 int GCEvents = PAPI_NULL;
76
77 int papi_error;
78
79 /* Arbitrary, to avoid using malloc */
80 #define MAX_PAPI_EVENTS 10
81
82 int n_papi_events = 0;
83
84
85 /* Events counted during GC and Mutator execution */
86 /* There's a trailing comma, do all C compilers accept that? */
87 static struct _papi_events papi_events[MAX_PAPI_EVENTS];
88 long_long MutatorCounters[MAX_PAPI_EVENTS];
89 long_long GCCounters[MAX_PAPI_EVENTS];
90
91 /* If you want to add events to count, extend the
92  * init_countable_events and the papi_report function.
93  * Be aware that your processor can count a limited number
94  * of events simultaneously, you can turn on multiplexing
95  * to increase that number, though.
96  */
97 static void
98 init_countable_events(void) 
99 {
100     PAPI_ADD_EVENT(PAPI_TOT_CYC);
101     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
102         PAPI_ADD_EVENT(FR_BR);
103         PAPI_ADD_EVENT(FR_BR_MIS);
104         /* Docs are wrong? Opteron does not count indirect branch misses exclusively */
105         PAPI_ADD_EVENT(FR_BR_MISCOMPARE);
106     }
107     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
108         PAPI_ADD_EVENT(FR_DISPATCH_STALLS_BR);
109         PAPI_ADD_EVENT(FR_DISPATCH_STALLS_FULL_LS);
110     }
111     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
112         PAPI_ADD_EVENT(PAPI_L1_DCA);
113         PAPI_ADD_EVENT(PAPI_L1_DCM);
114     }
115     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
116         PAPI_ADD_EVENT(PAPI_L2_DCA);
117         PAPI_ADD_EVENT(PAPI_L2_DCM);
118     }
119 };
120
121 /* This function reports counters for GC and mutator */
122 void
123 papi_report(long_long PapiCounters[])
124 {
125     char temp[BIG_STRING_LEN];
126
127     /* I need to improve formatting aesthetics */
128     PAPI_REPORT(PapiCounters,PAPI_TOT_CYC);
129
130     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_BRANCH) {
131         PAPI_REPORT(PapiCounters,FR_BR);
132         PAPI_REPORT(PapiCounters,FR_BR_MIS);
133         PAPI_REPORT_PCT(PapiCounters,FR_BR_MIS,FR_BR);
134         PAPI_REPORT_PCT(PapiCounters,FR_BR_MISCOMPARE,FR_BR);
135     }
136
137     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_STALLS) {
138         PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_BR);
139         PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_BR,PAPI_TOT_CYC);
140         PAPI_REPORT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS);
141         PAPI_REPORT_PCT(PapiCounters,FR_DISPATCH_STALLS_FULL_LS,PAPI_TOT_CYC);
142     }
143
144     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
145         PAPI_REPORT(PapiCounters,PAPI_L1_DCA);
146         PAPI_REPORT(PapiCounters,PAPI_L1_DCM);
147         PAPI_REPORT_PCT(PapiCounters,PAPI_L1_DCM,PAPI_L1_DCA);
148     }
149
150     if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
151         PAPI_REPORT(PapiCounters,PAPI_L2_DCA);
152         PAPI_REPORT(PapiCounters,PAPI_L2_DCM);
153         PAPI_REPORT_PCT(PapiCounters,PAPI_L2_DCM,PAPI_L2_DCA);
154     }
155
156 }
157
158
159
160 void
161 papi_init_eventsets(void)
162 {
163
164     init_countable_events();
165
166     /* One event set for the mutator and another for the GC */
167     PAPI_CHECK( PAPI_create_eventset(&MutatorEvents));
168     PAPI_CHECK( PAPI_create_eventset(&GCEvents));
169
170     /* Both sets contain the same events */
171     papi_add_events(MutatorEvents);
172     papi_add_events(GCEvents);
173
174 }
175
176 /* Extract the value corresponding to an event */
177 long_long
178 papi_counter(long_long values[],int event)
179 {
180   int i;
181   for(i=0;i<N_PAPI_EVENTS;i++) {
182     if(papi_events[i].event_code==event) {
183       return values[i];
184     }
185   }
186   /* Passed a wrong event? */
187   debugBelch("Event %d is not part of event set\n",event);
188   return 0;
189 }
190
191 /* Add the events of papi_events into an event set */
192 void
193 papi_add_events(int EventSet)
194 {
195   int i;
196   for(i=0;i<N_PAPI_EVENTS;i++) {
197     if((papi_error=PAPI_add_event(EventSet,
198                                   papi_events[i].event_code))
199        != PAPI_OK)
200       debugBelch("Failed adding %s to event set with error code %d\n",
201                  papi_events[i].event_name,papi_error);
202   }
203 }
204
205 void
206 papi_start_mutator_count(void)
207 {
208     PAPI_CHECK( PAPI_start(MutatorEvents));
209 }
210
211 void
212 papi_stop_mutator_count(void)
213 {
214     PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
215     PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
216 }
217
218 void
219 papi_start_gc_count(void)
220 {
221       PAPI_CHECK( PAPI_start(GCEvents));
222 }
223
224 void
225 papi_stop_gc_count(void)
226 {
227       PAPI_CHECK( PAPI_accum(GCEvents,GCCounters));
228       PAPI_CHECK( PAPI_stop(GCEvents,NULL));
229 }
230
231
232 #endif /* USE_PAPI */