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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/**
* Copyright (C) ARM Limited 2013-2016. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include "PerfGroup.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "Buffer.h"
#include "DynBuf.h"
#include "Logging.h"
#include "Monitor.h"
#include "PerfBuffer.h"
#include "SessionData.h"
static const int schedSwitchKey = getEventKey();
#define DEFAULT_PEA_ARGS(pea, additionalSampleType) \
pea.size = sizeof(pea); \
/* Emit time, read_format below, group leader id, and raw tracepoint info */ \
pea.sample_type = (gSessionData.mPerf.getLegacySupport() \
? PERF_SAMPLE_TID | PERF_SAMPLE_IP | PERF_SAMPLE_ID \
: PERF_SAMPLE_IDENTIFIER ) | PERF_SAMPLE_TIME | additionalSampleType; \
/* Emit emit value in group format */ \
pea.read_format = PERF_FORMAT_ID | PERF_FORMAT_GROUP; \
/* start out disabled */ \
pea.disabled = 1; \
/* have a sampling interrupt happen when we cross the wakeup_watermark boundary */ \
pea.watermark = 1; \
/* Be conservative in flush size as only one buffer set is monitored */ \
pea.wakeup_watermark = BUF_SIZE / 2; \
/* Use the monotonic raw clock if possible */ \
pea.use_clockid = gSessionData.mPerf.getClockidSupport() ? 1 : 0; \
pea.clockid = gSessionData.mPerf.getClockidSupport() ? CLOCK_MONOTONIC_RAW : 0
static int sys_perf_event_open(struct perf_event_attr *const attr, const pid_t pid, const int cpu, const int group_fd, const unsigned long flags) {
int fd = syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
if (fd < 0) {
return -1;
}
int fdf = fcntl(fd, F_GETFD);
if ((fdf == -1) || (fcntl(fd, F_SETFD, fdf | FD_CLOEXEC) != 0)) {
close(fd);
return -1;
}
return fd;
}
PerfGroup::PerfGroup(PerfBuffer *const pb) : mPb(pb), mSchedSwitchId(-1) {
memset(&mAttrs, 0, sizeof(mAttrs));
memset(&mFlags, 0, sizeof(mFlags));
memset(&mClusters, 0, sizeof(mClusters));
memset(&mKeys, -1, sizeof(mKeys));
memset(&mFds, -1, sizeof(mFds));
memset(&mLeaders, -1, sizeof(mLeaders));
}
PerfGroup::~PerfGroup() {
for (int pos = ARRAY_LENGTH(mFds) - 1; pos >= 0; --pos) {
if (mFds[pos] >= 0) {
close(mFds[pos]);
}
}
}
int PerfGroup::doAdd(const uint64_t currTime, Buffer *const buffer, const int key, const __u32 type, const __u64 config, const __u64 sample, const __u64 sampleType, const int flags, const GatorCpu *const cluster) {
int i;
for (i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
if (mKeys[i] < 0) {
break;
}
}
if (i >= ARRAY_LENGTH(mKeys)) {
logg.logMessage("Too many counters");
return -1;
}
DEFAULT_PEA_ARGS(mAttrs[i], sampleType);
mAttrs[i].type = type;
mAttrs[i].config = config;
mAttrs[i].sample_period = sample;
// always be on the CPU but only a group leader can be pinned
mAttrs[i].pinned = (flags & PERF_GROUP_LEADER ? 1 : 0);
mAttrs[i].mmap = (flags & PERF_GROUP_MMAP ? 1 : 0);
mAttrs[i].comm = (flags & PERF_GROUP_COMM ? 1 : 0);
mAttrs[i].freq = (flags & PERF_GROUP_FREQ ? 1 : 0);
mAttrs[i].task = (flags & PERF_GROUP_TASK ? 1 : 0);
mAttrs[i].sample_id_all = (flags & PERF_GROUP_SAMPLE_ID_ALL ? 1 : 0);
mFlags[i] = flags;
mClusters[i] = cluster;
mKeys[i] = key;
buffer->marshalPea(currTime, &mAttrs[i], key);
return i;
}
/* Counters from different hardware PMUs need to be in different
* groups. Software counters can be in the same group as the CPU and
* should be marked as PERF_GROUP_CPU. The big and little clusters can
* be in the same group as only one or the other will be available on
* a given CPU.
*/
int PerfGroup::getEffectiveType(const int type, const int flags) {
const int effectiveType = flags & PERF_GROUP_CPU ? (int)PERF_TYPE_HARDWARE : type;
if (effectiveType >= ARRAY_LENGTH(mLeaders)) {
logg.logError("perf type is too large, please increase the size of PerfGroup::mLeaders");
handleException();
}
return effectiveType;
}
bool PerfGroup::createCpuGroup(const uint64_t currTime, Buffer *const buffer) {
if (mSchedSwitchId < 0) {
DynBuf b;
mSchedSwitchId = PerfDriver::getTracepointId(SCHED_SWITCH, &b);
if (mSchedSwitchId < 0) {
logg.logMessage("Unable to read sched_switch id");
return false;
}
}
mLeaders[PERF_TYPE_HARDWARE] = doAdd(currTime, buffer, schedSwitchKey, PERF_TYPE_TRACEPOINT, mSchedSwitchId, 1, PERF_SAMPLE_READ | PERF_SAMPLE_RAW, PERF_GROUP_MMAP | PERF_GROUP_COMM | PERF_GROUP_TASK | PERF_GROUP_SAMPLE_ID_ALL | PERF_GROUP_PER_CPU | PERF_GROUP_LEADER | PERF_GROUP_CPU | PERF_GROUP_ALL_CLUSTERS, NULL);
if (mLeaders[PERF_TYPE_HARDWARE] < 0) {
return false;
}
if (gSessionData.mSampleRate > 0 && !gSessionData.mIsEBS && doAdd(currTime, buffer, INT_MAX-PERF_TYPE_HARDWARE, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK, 1000000000UL / gSessionData.mSampleRate, PERF_SAMPLE_TID | PERF_SAMPLE_IP | PERF_SAMPLE_READ, PERF_GROUP_PER_CPU | PERF_GROUP_CPU | PERF_GROUP_ALL_CLUSTERS, NULL) < 0) {
return false;
}
return true;
}
bool PerfGroup::add(const uint64_t currTime, Buffer *const buffer, const int key, const __u32 type, const __u64 config, const __u64 sample, const __u64 sampleType, const int flags, const GatorCpu *const cluster) {
const int effectiveType = getEffectiveType(type, flags);
// Does a group exist for this already?
if (!(flags & PERF_GROUP_LEADER) && mLeaders[effectiveType] < 0) {
// Create it
if (effectiveType == PERF_TYPE_HARDWARE) {
if (!createCpuGroup(currTime, buffer)) {
return false;
}
} else {
// Non-CPU PMUs are sampled every 100ms for Sample Rate: None and EBS, otherwise they would never be sampled
const uint64_t timeout = gSessionData.mSampleRate > 0 && !gSessionData.mIsEBS ? 1000000000UL / gSessionData.mSampleRate : 100000000UL;
mLeaders[effectiveType] = doAdd(currTime, buffer, INT_MAX-effectiveType, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK, timeout, PERF_SAMPLE_READ, PERF_GROUP_LEADER, NULL);
if (mLeaders[effectiveType] < 0) {
return false;
}
}
}
if (!(flags & PERF_GROUP_LEADER) && effectiveType != PERF_TYPE_HARDWARE && (flags & PERF_GROUP_PER_CPU)) {
logg.logError("'uncore' counters are not permitted to be per-cpu");
handleException();
}
return doAdd(currTime, buffer, key, type, config, sample, sampleType, flags, cluster) >= 0;
}
int PerfGroup::prepareCPU(const int cpu, Monitor *const monitor) {
logg.logMessage("Onlining cpu %i", cpu);
for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
if (mKeys[i] < 0) {
continue;
}
if ((cpu != 0) && !(mFlags[i] & PERF_GROUP_PER_CPU)) {
continue;
}
if ((mFlags[i] & PERF_GROUP_PER_CPU) && !(mFlags[i] & PERF_GROUP_ALL_CLUSTERS) && gSessionData.mSharedData->mClusters[gSessionData.mSharedData->mClusterIds[cpu]] != mClusters[i]) {
continue;
}
const int offset = i * gSessionData.mCores + cpu;
if (mFds[offset] >= 0) {
logg.logMessage("cpu already online or not correctly cleaned up");
return PG_FAILURE;
}
logg.logMessage("perf_event_open cpu: %i type: %i config: %lli sample: %lli sample_type: 0x%llx pinned: %lli mmap: %lli comm: %lli freq: %lli task: %lli sample_id_all: %lli", cpu, mAttrs[i].type, mAttrs[i].config, mAttrs[i].sample_period, mAttrs[i].sample_type, mAttrs[i].pinned, mAttrs[i].mmap, mAttrs[i].comm, mAttrs[i].freq, mAttrs[i].task, mAttrs[i].sample_id_all);
mFds[offset] = sys_perf_event_open(&mAttrs[i], -1, cpu, mAttrs[i].pinned ? -1 : mFds[mLeaders[getEffectiveType(mAttrs[i].type, mFlags[i])] * gSessionData.mCores + cpu], mAttrs[i].pinned ? 0 : PERF_FLAG_FD_OUTPUT);
if (mFds[offset] < 0) {
logg.logMessage("failed %s", strerror(errno));
if (errno == ENODEV) {
// The core is offline
return PG_CPU_OFFLINE;
}
if (errno == ENOENT) {
// This event doesn't apply to this CPU but should apply to a different one, ex bL
continue;
}
logg.logMessage("perf_event_open failed");
return PG_FAILURE;
}
if (!mPb->useFd(cpu, mFds[offset])) {
logg.logMessage("PerfBuffer::useFd failed");
return PG_FAILURE;
}
if (!monitor->add(mFds[offset])) {
logg.logMessage("Monitor::add failed");
return PG_FAILURE;
}
}
return PG_SUCCESS;
}
static bool readAndSend(const uint64_t currTime, Buffer *const buffer, const int fd, const int keyCount, const int *const keys) {
char buf[1024];
ssize_t bytes = read(fd, buf, sizeof(buf));
if (bytes < 0) {
logg.logMessage("read failed");
return false;
}
buffer->marshalKeysOld(currTime, keyCount, keys, bytes, buf);
return true;
}
int PerfGroup::onlineCPU(const uint64_t currTime, const int cpu, const bool enable, Buffer *const buffer) {
bool addedEvents = false;
if (!gSessionData.mPerf.getLegacySupport()) {
int idCount = 0;
int coreKeys[ARRAY_LENGTH(mKeys)];
__u64 ids[ARRAY_LENGTH(mKeys)];
for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
const int fd = mFds[cpu + i * gSessionData.mCores];
if (fd < 0) {
continue;
}
coreKeys[idCount] = mKeys[i];
if (ioctl(fd, PERF_EVENT_IOC_ID, &ids[idCount]) != 0 &&
// Workaround for running 32-bit gatord on 64-bit systems, kernel patch in the works
ioctl(fd, (PERF_EVENT_IOC_ID & ~IOCSIZE_MASK) | (8 << _IOC_SIZESHIFT), &ids[idCount]) != 0) {
logg.logMessage("ioctl failed");
return 0;
}
++idCount;
addedEvents = true;
}
buffer->marshalKeys(currTime, idCount, ids, coreKeys);
} else {
int idCounts[ARRAY_LENGTH(mLeaders)] = { 0 };
int coreKeys[ARRAY_LENGTH(mLeaders)][ARRAY_LENGTH(mKeys)];
for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
const int fd = mFds[cpu + i * gSessionData.mCores];
if (fd < 0) {
continue;
}
const int effectiveType = getEffectiveType(mAttrs[i].type, mFlags[i]);
if (mAttrs[i].pinned && mLeaders[effectiveType] != i) {
if (!readAndSend(currTime, buffer, fd, 1, mKeys + i)) {
return 0;
}
} else {
coreKeys[effectiveType][idCounts[effectiveType]] = mKeys[i];
++idCounts[effectiveType];
addedEvents = true;
}
}
for (int i = 0; i < ARRAY_LENGTH(mLeaders); ++i) {
if (idCounts[i] > 0 && !readAndSend(currTime, buffer, mFds[mLeaders[i] * gSessionData.mCores + cpu], idCounts[i], coreKeys[i])) {
return 0;
}
}
}
if (enable) {
for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
int offset = i * gSessionData.mCores + cpu;
if (mFds[offset] >= 0 && ioctl(mFds[offset], PERF_EVENT_IOC_ENABLE, 0) != 0) {
logg.logMessage("ioctl failed");
return 0;
}
}
}
if (!addedEvents) {
logg.logMessage("no events came online");
}
return 1;
}
bool PerfGroup::offlineCPU(const int cpu) {
logg.logMessage("Offlining cpu %i", cpu);
for (int i = ARRAY_LENGTH(mKeys) - 1; i >= 0; --i) {
int offset = i * gSessionData.mCores + cpu;
if (mFds[offset] >= 0 && ioctl(mFds[offset], PERF_EVENT_IOC_DISABLE, 0) != 0) {
logg.logMessage("ioctl failed");
return false;
}
}
// Mark the buffer so that it will be released next time it's read
mPb->discard(cpu);
for (int i = ARRAY_LENGTH(mKeys) - 1; i >= 0; --i) {
if (mKeys[i] < 0) {
continue;
}
int offset = i * gSessionData.mCores + cpu;
if (mFds[offset] >= 0) {
close(mFds[offset]);
mFds[offset] = -1;
}
}
return true;
}
void PerfGroup::start() {
char buf[1<<10];
for (int pos = 0; pos < ARRAY_LENGTH(mFds); ++pos) {
if (mFds[pos] >= 0 && ioctl(mFds[pos], PERF_EVENT_IOC_ENABLE, 0) != 0) {
logg.logError("Unable to enable a perf event");
handleException();
}
}
// Try reading from all the group leaders to ensure that the event isn't disabled
for (int pos = 0; pos < ARRAY_LENGTH(mAttrs); ++pos) {
if (mAttrs[pos].pinned) {
for (int cpu = 0; cpu < gSessionData.mCores; ++cpu) {
int fd = mFds[pos * gSessionData.mCores + cpu];
if (fd >= 0 && read(fd, buf, sizeof(buf)) <= 0) {
logg.logError("Unable to read all perf groups, perhaps too many events were enabled");
handleException();
}
}
}
}
}
void PerfGroup::stop() {
for (int pos = ARRAY_LENGTH(mFds) - 1; pos >= 0; --pos) {
if (mFds[pos] >= 0) {
ioctl(mFds[pos], PERF_EVENT_IOC_DISABLE, 0);
}
}
}
|