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