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
63 /* Number of counted events, computed from size of papi_events */
64 #define N_PAPI_EVENTS n_papi_events
66 /* This is bad, it should be in a header */
67 #define BIG_STRING_LEN 512
69 /* While PAPI reporting is going on this flag is on */
70 int papi_is_reporting;
72 /* Event sets and counter arrays for GC and mutator */
74 int MutatorEvents = PAPI_NULL;
75 int GCEvents = PAPI_NULL;
79 /* Arbitrary, to avoid using malloc */
80 #define MAX_PAPI_EVENTS 10
82 int n_papi_events = 0;
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];
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.
98 init_countable_events(void)
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);
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);
111 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
112 PAPI_ADD_EVENT(PAPI_L1_DCA);
113 PAPI_ADD_EVENT(PAPI_L1_DCM);
115 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
116 PAPI_ADD_EVENT(PAPI_L2_DCA);
117 PAPI_ADD_EVENT(PAPI_L2_DCM);
121 /* This function reports counters for GC and mutator */
123 papi_report(long_long PapiCounters[])
125 char temp[BIG_STRING_LEN];
127 /* I need to improve formatting aesthetics */
128 PAPI_REPORT(PapiCounters,PAPI_TOT_CYC);
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);
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);
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);
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);
161 papi_init_eventsets(void)
164 init_countable_events();
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));
170 /* Both sets contain the same events */
171 papi_add_events(MutatorEvents);
172 papi_add_events(GCEvents);
176 /* Extract the value corresponding to an event */
178 papi_counter(long_long values[],int event)
181 for(i=0;i<N_PAPI_EVENTS;i++) {
182 if(papi_events[i].event_code==event) {
186 /* Passed a wrong event? */
187 debugBelch("Event %d is not part of event set\n",event);
191 /* Add the events of papi_events into an event set */
193 papi_add_events(int EventSet)
196 for(i=0;i<N_PAPI_EVENTS;i++) {
197 if((papi_error=PAPI_add_event(EventSet,
198 papi_events[i].event_code))
200 debugBelch("Failed adding %s to event set with error code %d\n",
201 papi_events[i].event_name,papi_error);
206 papi_start_mutator_count(void)
208 PAPI_CHECK( PAPI_start(MutatorEvents));
212 papi_stop_mutator_count(void)
214 PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
215 PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
219 papi_start_gc_count(void)
221 PAPI_CHECK( PAPI_start(GCEvents));
225 papi_stop_gc_count(void)
227 PAPI_CHECK( PAPI_accum(GCEvents,GCCounters));
228 PAPI_CHECK( PAPI_stop(GCEvents,NULL));
232 #endif /* USE_PAPI */