2 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2006
5 * Initialization and use of the PAPI performance monitoring library
8 * For adding events or add your processor counters modify
10 * init_countable_events
13 * ---------------------------------------------------------------------------*/
16 #ifdef USE_PAPI /* ugly */
30 #define PAPI_ADD_EVENT(EVENT) \
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; \
38 /* Report the value of a counter */
39 #define PAPI_REPORT(EVENTSET,EVENT) \
41 ullong_format_string(papi_counter(EVENTSET,EVENT),temp,rtsTrue/*commas*/); \
42 statsPrintf(" (" #EVENT ") : %s\n",temp); \
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))
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
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
64 /* Number of counted events, computed from size of papi_events */
65 #define N_PAPI_EVENTS n_papi_events
67 /* This is bad, it should be in a header */
68 #define BIG_STRING_LEN 512
70 /* While PAPI reporting is going on this flag is on */
71 int papi_is_reporting;
73 /* Event sets and counter arrays for GC and mutator */
75 int MutatorEvents = PAPI_NULL;
76 int GCEvents = PAPI_NULL;
80 /* Arbitrary, to avoid using malloc */
81 #define MAX_PAPI_EVENTS 10
83 int n_papi_events = 0;
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];
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.
99 init_countable_events(void)
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);
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);
112 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L1) {
113 PAPI_ADD_EVENT(PAPI_L1_DCA);
114 PAPI_ADD_EVENT(PAPI_L1_DCM);
116 if(RtsFlags.PapiFlags.eventType==PAPI_FLAG_CACHE_L2) {
117 PAPI_ADD_EVENT(PAPI_L2_DCA);
118 PAPI_ADD_EVENT(PAPI_L2_DCM);
122 /* This function reports counters for GC and mutator */
124 papi_report(long_long PapiCounters[])
126 char temp[BIG_STRING_LEN];
128 /* I need to improve formatting aesthetics */
129 PAPI_REPORT(PapiCounters,PAPI_TOT_CYC);
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);
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);
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);
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);
162 papi_init_eventsets(void)
165 init_countable_events();
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));
171 /* Both sets contain the same events */
172 papi_add_events(MutatorEvents);
173 papi_add_events(GCEvents);
177 /* Extract the value corresponding to an event */
179 papi_counter(long_long values[],int event)
182 for(i=0;i<N_PAPI_EVENTS;i++) {
183 if(papi_events[i].event_code==event) {
187 /* Passed a wrong event? */
188 debugBelch("Event %d is not part of event set\n",event);
192 /* Add the events of papi_events into an event set */
194 papi_add_events(int EventSet)
197 for(i=0;i<N_PAPI_EVENTS;i++) {
198 if((papi_error=PAPI_add_event(EventSet,
199 papi_events[i].event_code))
201 debugBelch("Failed adding %s to event set with error code %d\n",
202 papi_events[i].event_name,papi_error);
207 papi_start_mutator_count(void)
209 PAPI_CHECK( PAPI_start(MutatorEvents));
213 papi_stop_mutator_count(void)
215 PAPI_CHECK( PAPI_accum(MutatorEvents,MutatorCounters));
216 PAPI_CHECK( PAPI_stop(MutatorEvents,NULL));
220 papi_start_gc_count(void)
222 PAPI_CHECK( PAPI_start(GCEvents));
226 papi_stop_gc_count(void)
228 PAPI_CHECK( PAPI_accum(GCEvents,GCCounters));
229 PAPI_CHECK( PAPI_stop(GCEvents,NULL));
233 #endif /* USE_PAPI */