-
Notifications
You must be signed in to change notification settings - Fork 16
/
perfCheck.c
337 lines (269 loc) · 8.86 KB
/
perfCheck.c
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* perfCheck.c
*
* Created on: Apr 19, 2017
* Author: kenan
*
* Manual for perf_event_open()
* If PERF_FORMAT_GROUP was specified to allow reading all events in a
* group at once:
*
* struct read_format {
* u64 nr; * The number of events *
* u64 time_enabled; * if PERF_FORMAT_TOTAL_TIME_ENABLED *
* u64 time_running; * if PERF_FORMAT_TOTAL_TIME_RUNNING *
* struct {
* u64 value; * The value of the event *
* u64 id; * if PERF_FORMAT_ID *
* } values[nr];
* };
*
* If PERF_FORMAT_GROUP was not specified:
*
* struct read_format {
* u64 value; * The value of the event *
* u64 time_enabled; * if PERF_FORMAT_TOTAL_TIME_ENABLED *
* u64 time_running; * if PERF_FORMAT_TOTAL_TIME_RUNNING *
* u64 id; * if PERF_FORMAT_ID *
* };
*
*/
#include <stdio.h>
#include <jni.h>
#include <stdlib.h>
#include <errno.h>
#include <perfmon/pfmlib_perf_event.h>
#include <stdbool.h>
#include <string.h>
#include <err.h>
static int enabled = 0;
static int isGrouped = 0;
static int *perf_event_fds;
static perf_event_attr_t *perf_event_attrs;
static size_t dataLength;
static int buffer_size;
static int num_events;
//Value from previous read
static long previousValue = 0;
/**
* Set all counter values in an event group to be read with one read or not
*/
void setGroup(bool isSet) {
isGrouped = isSet;
}
JNIEXPORT void JNICALL Java_PerfCheckUtils_perfInit(JNIEnv * env, jclass jcls, jint numEvents, jint isSet) {
setGroup(isSet);
num_events = numEvents;
int i;
int ret = pfm_initialize();
if (ret != PFM_SUCCESS) {
errx(1, "cannot initialize library: %s", pfm_strerror(ret));
}
perf_event_fds = (int *) calloc(numEvents, sizeof(int));
if (!perf_event_fds) {
errx(1, "error allocating perf_event_fds");
}
if (isGrouped) {
buffer_size = 3 + (2 * numEvents);
} else {
buffer_size = 3;
}
dataLength = buffer_size * sizeof(long);
perf_event_attrs = (perf_event_attr_t *) calloc(numEvents, sizeof(perf_event_attr_t));
if (!perf_event_attrs) {
errx(1, "error allocating perf_event_attrs");
}
for (i = 0; i < numEvents; i++) {
perf_event_attrs[i].size = sizeof(perf_event_attr_t);
}
enabled = 1;
}
JNIEXPORT void JNICALL Java_PerfCheckUtils_groupPerfEventsCheck(JNIEnv * env, jclass jcls, jstring name) {
char *eventName;
char *event[num_events];
int id = 0;
int i = 0;
eventName = (*env)->GetStringUTFChars( env, name , NULL );
while((event[id] = strsep(&eventName, ",")) != NULL) ++id;
for(i = 0; i < id; i++) {
perf_event_attr_t *attr = (perf_event_attrs + i);
int ret = pfm_get_perf_event_encoding(event[i], PFM_PLM3, attr, NULL,NULL);
if (ret != PFM_SUCCESS) {
errx(1, "cannot get encoding %d '%s': %s\n", i, event[i],
pfm_strerror(ret));
}
attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
//Enable event group to be read with one read
attr->read_format |= PERF_FORMAT_GROUP | PERF_FORMAT_ID;
//Inherit does not work for some combinations of read_format
//values, such as PERF_FORMAT_GROUP.
attr->inherit = 0;
attr->disabled = 1;
//Kenan: edit for group reading
if (i == 0) {
// Open perf event for group leader, and set its own group leader fd to be -1.
perf_event_fds[i] = perf_event_open(attr, 0, -1, -1, 0);
} else {
//Open perf event for the rest members in this group.
perf_event_fds[i] = perf_event_open(attr, 0, -1, perf_event_fds[0], 0);
}
if (perf_event_fds[i] == -1) {
errx(1, "cannot open library perf_event_open: %d '%s'", i, event[i]);
}
}
}
JNIEXPORT void JNICALL Java_PerfCheckUtils_singlePerfEventCheck(JNIEnv * env, jclass jcls, jstring name) {
char *eventName;
char *event[num_events];
int i = 0;
int id = 0;
eventName = (*env)->GetStringUTFChars( env, name , NULL );
while((event[id] = strsep(&eventName, ",")) != NULL) ++id;
for(i = 0; i < id; i++) {
perf_event_attr_t *attr = (perf_event_attrs + i);
//Helper library to configure perf
int ret = pfm_get_perf_event_encoding(event[i], PFM_PLM3, attr, NULL,NULL);
if (ret != PFM_SUCCESS) {
errx(1, "cannot get encoding %d '%s': %s\n", i, event[i],
pfm_strerror(ret));
}
attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
attr->disabled = 1;
attr->inherit = 1;
//Read perf counter values
perf_event_fds[i] = perf_event_open(attr, 0, -1, -1, 0);
if (perf_event_fds[i] == -1) {
errx(1, "cannot open library perf_event_open: %d '%s'", i, event[i]);
}
}
}
JNIEXPORT void JNICALL Java_PerfCheckUtils_perfEnable(JNIEnv * env, jclass jcls) {
if (enabled) {
if (prctl(PR_TASK_PERF_EVENTS_ENABLE)) {
errx(1, "cannot enable perf in prctl(PR_TASK_PERF_EVENTS_ENABLE)");
}
}
}
JNIEXPORT void JNICALL Java_PerfCheckUtils_perfDisable(JNIEnv * env, jclass jcls) {
if (!enabled) {
if (prctl(PR_TASK_PERF_EVENTS_DISABLE)) {
errx(1, "cannot disable perf in prctl(PR_TASK_PERF_EVENTS_DISABLE)");
}
}
}
JNIEXPORT void JNICALL Java_PerfCheckUtils_perfSingleRead(JNIEnv * env, jclass jcls, jint id, jlongArray buffer) {
// size_t dataLength = 3 * sizeof(long long);
long *values;
values = (*env)->GetLongArrayElements(env, buffer, NULL);
int ret = read(perf_event_fds[id], values, dataLength);
if (ret < 0) {
errx(1, "Failed to open: %s", strerror(errno));
}
if (ret != dataLength) {
errx(1, "Failed to read");
}
(*env)->ReleaseLongArrayElements(env, buffer, values, 0);
}
JNIEXPORT void JNICALL Java_PerfCheckUtils_perfMultRead(JNIEnv * env, jclass jcls, jlongArray buffer) {
long *values;
values = (*env)->GetLongArrayElements(env, buffer, NULL);
int ret = read(perf_event_fds[0], values, dataLength);
if (ret < 0) {
err(1, "Failed to open: %s", strerror(errno));
}
if (ret != dataLength) {
errx(1, "Failed to read");
}
(*env)->ReleaseLongArrayElements(env, buffer, values, 0);
}
JNIEXPORT jlongArray JNICALL Java_PerfCheckUtils_processMultiValue(JNIEnv * env, jclass jcls, jlongArray buffer) {
int RAW_COUNT = 0;
int TIME_ENABLED = 1;
int TIME_RUNNING = 2;
bool overflowed = false;
int dataWasScaled = 0;
long *readBuffer;
readBuffer = (*env)->GetLongArrayElements(env, buffer, NULL);
long * values = malloc(num_events * sizeof(long));
jlongArray result = (*env)->NewLongArray(env, num_events);
int i;
if (!readBuffer[TIME_ENABLED] || !readBuffer[TIME_RUNNING]) {
printf("Time enabled: %ld, time running: %ld\n", readBuffer[TIME_ENABLED],
readBuffer[TIME_RUNNING]);
}
if (readBuffer[TIME_ENABLED] != readBuffer[TIME_RUNNING]) {
if (readBuffer[TIME_ENABLED] == 0) {
dataWasScaled = 0;
} else {
dataWasScaled = readBuffer[TIME_ENABLED] / readBuffer[TIME_RUNNING];
}
for (i = 0; i < num_events; i++) {
readBuffer[3 + i * 2] *= dataWasScaled;
values[i] = readBuffer[3 + i * 2];
}
(*env)->SetLongArrayRegion(env, result, 0, num_events, values);
free(values);
return result;
}
//Return event counters only
for (i = 0; i < num_events; i++) {
values[i] = readBuffer[3 + i * 2];
}
(*env)->SetLongArrayRegion(env, result, 0, num_events, values);
free(values);
return result;
}
JNIEXPORT jlong JNICALL Java_PerfCheckUtils_processSingleValue(JNIEnv * env, jclass jcls, jlongArray buffer) {
int RAW_COUNT = 0;
int TIME_ENABLED = 1;
int TIME_RUNNING = 2;
bool overflowed = false;
int dataWasScaled = 0;
long *readBuffer;
readBuffer = (*env)->GetLongArrayElements(env, buffer, NULL);
if (readBuffer[TIME_ENABLED] == 0) {
// Counter never run (assume contention)
// contended = true;
errx(1, "Event counter contended!\n");
}
if (readBuffer[RAW_COUNT] == 0) {
errx(1, "Event counter is 0\n");
}
//When multiplexing is happening
if (readBuffer[TIME_ENABLED] != readBuffer[TIME_RUNNING]) {
// scaled = true;
dataWasScaled = 1;
double scaleFactor;
if (readBuffer[TIME_RUNNING] == 0) {
scaleFactor = 0;
} else {
scaleFactor = readBuffer[TIME_ENABLED] / readBuffer[TIME_RUNNING];
}
readBuffer[RAW_COUNT] = (long) (readBuffer[RAW_COUNT] * scaleFactor);
}
if (readBuffer[RAW_COUNT] < previousValue) {
//TODO: figure out the maximum of PMU counters can hold
//and do readBuffer[RAW_COUNT] += MAXIMUM
errx(1, "overflow is happening");
}
previousValue = readBuffer[RAW_COUNT];
return (jlong)readBuffer[RAW_COUNT];
}
int main() {
long long readBuffer[3];
perfInit(2, false);
singlePerfEventCheck(0, "cache-misses");
singlePerfEventCheck(1, "cache-references");
perfEnable();
printf("before sleep\n");
perfSingleRead(0, readBuffer);
printf("counter value 1 is: %d\n", processSingleValue(readBuffer));
perfSingleRead(1, readBuffer);
printf("counter value 2 is: %d\n", processSingleValue(readBuffer));
sleep(10);
printf("After sleep\n");
perfSingleRead(0, readBuffer);
printf("counter value 1 is: %d\n", processSingleValue(readBuffer));
perfSingleRead(1, readBuffer);
printf("counter value 2 is: %d\n", processSingleValue(readBuffer));
}