blob: ac97a077d266fad9d33fbf711d34a44f98b3fc89 [file] [log] [blame]
Jon Medhurst15ce78d2014-04-10 09:02:02 +01001/**
2 * Copyright (C) ARM Limited 2013-2014. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include "PerfDriver.h"
10
11#include <dirent.h>
12#include <sys/utsname.h>
13#include <time.h>
Jon Medhurste31266f2014-08-04 15:47:44 +010014#include <unistd.h>
Jon Medhurst15ce78d2014-04-10 09:02:02 +010015
16#include "Buffer.h"
17#include "Config.h"
18#include "ConfigurationXML.h"
19#include "Counter.h"
20#include "DriverSource.h"
21#include "DynBuf.h"
22#include "Logging.h"
23#include "PerfGroup.h"
24#include "SessionData.h"
25
26#define PERF_DEVICES "/sys/bus/event_source/devices"
27
28#define TYPE_DERIVED ~0U
29
30// From gator.h
31struct gator_cpu {
32 const int cpuid;
33 // Human readable name
Jon Medhurste31266f2014-08-04 15:47:44 +010034 const char *const core_name;
Jon Medhurst15ce78d2014-04-10 09:02:02 +010035 // gatorfs event and Perf PMU name
36 const char *const pmnc_name;
37 const int pmnc_counters;
38};
39
40// From gator_main.c
41static const struct gator_cpu gator_cpus[] = {
42 { 0xb36, "ARM1136", "ARM_ARM11", 3 },
43 { 0xb56, "ARM1156", "ARM_ARM11", 3 },
44 { 0xb76, "ARM1176", "ARM_ARM11", 3 },
45 { 0xb02, "ARM11MPCore", "ARM_ARM11MPCore", 3 },
46 { 0xc05, "Cortex-A5", "ARMv7_Cortex_A5", 2 },
47 { 0xc07, "Cortex-A7", "ARMv7_Cortex_A7", 4 },
48 { 0xc08, "Cortex-A8", "ARMv7_Cortex_A8", 4 },
49 { 0xc09, "Cortex-A9", "ARMv7_Cortex_A9", 6 },
50 { 0xc0d, "Cortex-A12", "ARMv7_Cortex_A12", 6 },
51 { 0xc0f, "Cortex-A15", "ARMv7_Cortex_A15", 6 },
52 { 0xc0e, "Cortex-A17", "ARMv7_Cortex_A17", 6 },
53 { 0x00f, "Scorpion", "Scorpion", 4 },
54 { 0x02d, "ScorpionMP", "ScorpionMP", 4 },
55 { 0x049, "KraitSIM", "Krait", 4 },
56 { 0x04d, "Krait", "Krait", 4 },
57 { 0x06f, "Krait S4 Pro", "Krait", 4 },
58 { 0xd03, "Cortex-A53", "ARM_Cortex-A53", 6 },
59 { 0xd07, "Cortex-A57", "ARM_Cortex-A57", 6 },
60 { 0xd0f, "AArch64", "ARM_AArch64", 6 },
61};
62
63static const char OLD_PMU_PREFIX[] = "ARMv7 Cortex-";
64static const char NEW_PMU_PREFIX[] = "ARMv7_Cortex_";
65
Jon Medhurste31266f2014-08-04 15:47:44 +010066struct uncore_counter {
67 // gatorfs event and Perf PMU name
68 const char *const name;
69 const int count;
70};
71
72static const struct uncore_counter uncore_counters[] = {
73 { "CCI_400", 4 },
74 { "CCI_400-r1", 4 },
75};
76
Jon Medhurst15ce78d2014-04-10 09:02:02 +010077class PerfCounter {
78public:
Jon Medhurste31266f2014-08-04 15:47:44 +010079 PerfCounter(PerfCounter *next, const char *name, uint32_t type, uint64_t config, bool perCpu) : mNext(next), mName(name), mType(type), mCount(0), mKey(getEventKey()), mConfig(config), mEnabled(false), mPerCpu(perCpu) {}
Jon Medhurst15ce78d2014-04-10 09:02:02 +010080 ~PerfCounter() {
81 delete [] mName;
82 }
83
84 PerfCounter *getNext() const { return mNext; }
85 const char *getName() const { return mName; }
86 uint32_t getType() const { return mType; }
87 int getCount() const { return mCount; }
88 void setCount(const int count) { mCount = count; }
89 int getKey() const { return mKey; }
90 uint64_t getConfig() const { return mConfig; }
91 void setConfig(const uint64_t config) { mConfig = config; }
92 bool isEnabled() const { return mEnabled; }
93 void setEnabled(const bool enabled) { mEnabled = enabled; }
Jon Medhurste31266f2014-08-04 15:47:44 +010094 bool isPerCpu() const { return mPerCpu; }
Jon Medhurst15ce78d2014-04-10 09:02:02 +010095
96private:
97 PerfCounter *const mNext;
98 const char *const mName;
99 const uint32_t mType;
100 int mCount;
101 const int mKey;
102 uint64_t mConfig;
Jon Medhurste31266f2014-08-04 15:47:44 +0100103 int mEnabled : 1,
104 mPerCpu : 1;
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100105};
106
Jon Medhurste31266f2014-08-04 15:47:44 +0100107PerfDriver::PerfDriver() : mCounters(NULL), mIsSetup(false), mLegacySupport(false) {
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100108}
109
110PerfDriver::~PerfDriver() {
111 while (mCounters != NULL) {
112 PerfCounter *counter = mCounters;
113 mCounters = counter->getNext();
114 delete counter;
115 }
116}
117
118void PerfDriver::addCpuCounters(const char *const counterName, const int type, const int numCounters) {
119 int len = snprintf(NULL, 0, "%s_ccnt", counterName) + 1;
120 char *name = new char[len];
121 snprintf(name, len, "%s_ccnt", counterName);
Jon Medhurste31266f2014-08-04 15:47:44 +0100122 mCounters = new PerfCounter(mCounters, name, type, -1, true);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100123
124 for (int j = 0; j < numCounters; ++j) {
125 len = snprintf(NULL, 0, "%s_cnt%d", counterName, j) + 1;
126 name = new char[len];
127 snprintf(name, len, "%s_cnt%d", counterName, j);
Jon Medhurste31266f2014-08-04 15:47:44 +0100128 mCounters = new PerfCounter(mCounters, name, type, -1, true);
129 }
130}
131
132void PerfDriver::addUncoreCounters(const char *const counterName, const int type, const int numCounters) {
133 int len = snprintf(NULL, 0, "%s_ccnt", counterName) + 1;
134 char *name = new char[len];
135 snprintf(name, len, "%s_ccnt", counterName);
136 mCounters = new PerfCounter(mCounters, name, type, -1, false);
137
138 for (int j = 0; j < numCounters; ++j) {
139 len = snprintf(NULL, 0, "%s_cnt%d", counterName, j) + 1;
140 name = new char[len];
141 snprintf(name, len, "%s_cnt%d", counterName, j);
142 mCounters = new PerfCounter(mCounters, name, type, -1, false);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100143 }
144}
145
146// From include/generated/uapi/linux/version.h
147#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
148
149bool PerfDriver::setup() {
150 // Check the kernel version
151 struct utsname utsname;
152 if (uname(&utsname) != 0) {
153 logg->logMessage("%s(%s:%i): uname failed", __FUNCTION__, __FILE__, __LINE__);
154 return false;
155 }
156
157 int release[3] = { 0, 0, 0 };
158 int part = 0;
159 char *ch = utsname.release;
160 while (*ch >= '0' && *ch <= '9' && part < ARRAY_LENGTH(release)) {
161 release[part] = 10*release[part] + *ch - '0';
162
163 ++ch;
164 if (*ch == '.') {
165 ++part;
166 ++ch;
167 }
168 }
169
Jon Medhurste31266f2014-08-04 15:47:44 +0100170 if (KERNEL_VERSION(release[0], release[1], release[2]) < KERNEL_VERSION(3, 4, 0)) {
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100171 logg->logMessage("%s(%s:%i): Unsupported kernel version", __FUNCTION__, __FILE__, __LINE__);
172 return false;
173 }
Jon Medhurste31266f2014-08-04 15:47:44 +0100174 mLegacySupport = KERNEL_VERSION(release[0], release[1], release[2]) < KERNEL_VERSION(3, 12, 0);
175
176 if (access(EVENTS_PATH, R_OK) != 0) {
177 logg->logMessage("%s(%s:%i): " EVENTS_PATH " does not exist, is CONFIG_TRACING enabled?", __FUNCTION__, __FILE__, __LINE__);
178 return false;
179 }
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100180
181 // Add supported PMUs
182 bool foundCpu = false;
183 DIR *dir = opendir(PERF_DEVICES);
184 if (dir == NULL) {
185 logg->logMessage("%s(%s:%i): opendif failed", __FUNCTION__, __FILE__, __LINE__);
186 return false;
187 }
188
189 struct dirent *dirent;
190 while ((dirent = readdir(dir)) != NULL) {
191 for (int i = 0; i < ARRAY_LENGTH(gator_cpus); ++i) {
192 // Do the names match exactly?
193 if (strcmp(dirent->d_name, gator_cpus[i].pmnc_name) != 0 &&
194 // Do these names match but have the old vs new prefix?
195 (strncmp(dirent->d_name, OLD_PMU_PREFIX, sizeof(OLD_PMU_PREFIX) - 1) != 0 ||
196 strncmp(gator_cpus[i].pmnc_name, NEW_PMU_PREFIX, sizeof(NEW_PMU_PREFIX) - 1) != 0 ||
197 strcmp(dirent->d_name + sizeof(OLD_PMU_PREFIX) - 1, gator_cpus[i].pmnc_name + sizeof(NEW_PMU_PREFIX) - 1) != 0)) {
198 continue;
199 }
200
201 int type;
202 char buf[256];
203 snprintf(buf, sizeof(buf), PERF_DEVICES "/%s/type", dirent->d_name);
204 if (DriverSource::readIntDriver(buf, &type) != 0) {
205 continue;
206 }
207
208 foundCpu = true;
209 addCpuCounters(gator_cpus[i].pmnc_name, type, gator_cpus[i].pmnc_counters);
210 }
Jon Medhurste31266f2014-08-04 15:47:44 +0100211
212 for (int i = 0; i < ARRAY_LENGTH(uncore_counters); ++i) {
213 if (strcmp(dirent->d_name, uncore_counters[i].name) != 0) {
214 continue;
215 }
216
217 int type;
218 char buf[256];
219 snprintf(buf, sizeof(buf), PERF_DEVICES "/%s/type", dirent->d_name);
220 if (DriverSource::readIntDriver(buf, &type) != 0) {
221 continue;
222 }
223
224 addUncoreCounters(uncore_counters[i].name, type, uncore_counters[i].count);
225 }
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100226 }
227 closedir(dir);
228
229 if (!foundCpu) {
230 // If no cpu was found based on pmu names, try by cpuid
231 for (int i = 0; i < ARRAY_LENGTH(gator_cpus); ++i) {
232 if (gSessionData->mMaxCpuId != gator_cpus[i].cpuid) {
233 continue;
234 }
235
236 foundCpu = true;
237 addCpuCounters(gator_cpus[i].pmnc_name, PERF_TYPE_RAW, gator_cpus[i].pmnc_counters);
238 }
239 }
240
241 /*
242 if (!foundCpu) {
243 // If all else fails, use the perf architected counters
244 // 9 because that's how many are in events-Perf-Hardware.xml - assume they can all be enabled at once
245 addCpuCounters("Perf_Hardware", PERF_TYPE_HARDWARE, 9);
246 }
247 */
248
249 // Add supported software counters
250 long long id;
251 DynBuf printb;
252
253 id = getTracepointId("irq/softirq_exit", &printb);
254 if (id >= 0) {
Jon Medhurste31266f2014-08-04 15:47:44 +0100255 mCounters = new PerfCounter(mCounters, "Linux_irq_softirq", PERF_TYPE_TRACEPOINT, id, true);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100256 }
257
258 id = getTracepointId("irq/irq_handler_exit", &printb);
259 if (id >= 0) {
Jon Medhurste31266f2014-08-04 15:47:44 +0100260 mCounters = new PerfCounter(mCounters, "Linux_irq_irq", PERF_TYPE_TRACEPOINT, id, true);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100261 }
262
263 //Linux_block_rq_wr
264 //Linux_block_rq_rd
265 //Linux_net_rx
266 //Linux_net_tx
267
268 id = getTracepointId(SCHED_SWITCH, &printb);
269 if (id >= 0) {
Jon Medhurste31266f2014-08-04 15:47:44 +0100270 mCounters = new PerfCounter(mCounters, "Linux_sched_switch", PERF_TYPE_TRACEPOINT, id, true);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100271 }
272
273 //Linux_meminfo_memused
274 //Linux_meminfo_memfree
275 //Linux_meminfo_bufferram
276 //Linux_power_cpu_freq
277 //Linux_power_cpu_idle
278
Jon Medhurste31266f2014-08-04 15:47:44 +0100279 mCounters = new PerfCounter(mCounters, "Linux_cpu_wait_contention", TYPE_DERIVED, -1, false);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100280
281 //Linux_cpu_wait_io
282
283 mIsSetup = true;
284 return true;
285}
286
287bool PerfDriver::summary(Buffer *const buffer) {
288 struct utsname utsname;
289 if (uname(&utsname) != 0) {
290 logg->logMessage("%s(%s:%i): uname failed", __FUNCTION__, __FILE__, __LINE__);
291 return false;
292 }
293
294 char buf[512];
295 snprintf(buf, sizeof(buf), "%s %s %s %s %s GNU/Linux", utsname.sysname, utsname.nodename, utsname.release, utsname.version, utsname.machine);
296
297 struct timespec ts;
298 if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
299 logg->logMessage("%s(%s:%i): clock_gettime failed", __FUNCTION__, __FILE__, __LINE__);
300 return false;
301 }
302 const int64_t timestamp = (int64_t)ts.tv_sec * 1000000000L + ts.tv_nsec;
303
Jon Medhurste31266f2014-08-04 15:47:44 +0100304 const int64_t uptime = getTime();
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100305
306 buffer->summary(timestamp, uptime, 0, buf);
307
308 for (int i = 0; i < gSessionData->mCores; ++i) {
Jon Medhurste31266f2014-08-04 15:47:44 +0100309 // Don't send information on a cpu we know nothing about
310 if (gSessionData->mCpuIds[i] == -1) {
311 continue;
312 }
313
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100314 int j;
315 for (j = 0; j < ARRAY_LENGTH(gator_cpus); ++j) {
316 if (gator_cpus[j].cpuid == gSessionData->mCpuIds[i]) {
317 break;
318 }
319 }
320 if (gator_cpus[j].cpuid == gSessionData->mCpuIds[i]) {
321 buffer->coreName(i, gSessionData->mCpuIds[i], gator_cpus[j].core_name);
322 } else {
Jon Medhurste31266f2014-08-04 15:47:44 +0100323 if (gSessionData->mCpuIds[i] == -1) {
324 snprintf(buf, sizeof(buf), "Unknown");
325 } else {
326 snprintf(buf, sizeof(buf), "Unknown (0x%.3x)", gSessionData->mCpuIds[i]);
327 }
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100328 buffer->coreName(i, gSessionData->mCpuIds[i], buf);
329 }
330 }
331 buffer->commit(1);
332
333 return true;
334}
335
336PerfCounter *PerfDriver::findCounter(const Counter &counter) const {
337 for (PerfCounter * perfCounter = mCounters; perfCounter != NULL; perfCounter = perfCounter->getNext()) {
338 if (strcmp(perfCounter->getName(), counter.getType()) == 0) {
339 return perfCounter;
340 }
341 }
342
343 return NULL;
344}
345
346bool PerfDriver::claimCounter(const Counter &counter) const {
347 return findCounter(counter) != NULL;
348}
349
350void PerfDriver::resetCounters() {
351 for (PerfCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
352 counter->setEnabled(false);
353 }
354}
355
356void PerfDriver::setupCounter(Counter &counter) {
357 PerfCounter *const perfCounter = findCounter(counter);
358 if (perfCounter == NULL) {
359 counter.setEnabled(false);
360 return;
361 }
362
363 // Don't use the config from counters XML if it's not set, ex: software counters
364 if (counter.getEvent() != -1) {
365 perfCounter->setConfig(counter.getEvent());
366 }
367 perfCounter->setCount(counter.getCount());
368 perfCounter->setEnabled(true);
369 counter.setKey(perfCounter->getKey());
370}
371
372int PerfDriver::writeCounters(mxml_node_t *root) const {
373 int count = 0;
374 for (PerfCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
375 mxml_node_t *node = mxmlNewElement(root, "counter");
376 mxmlElementSetAttr(node, "name", counter->getName());
377 ++count;
378 }
379
380 return count;
381}
382
Jon Medhurste31266f2014-08-04 15:47:44 +0100383bool PerfDriver::enable(PerfGroup *const group, Buffer *const buffer) const {
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100384 for (PerfCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
385 if (counter->isEnabled() && (counter->getType() != TYPE_DERIVED)) {
Jon Medhurste31266f2014-08-04 15:47:44 +0100386 if (!group->add(buffer, counter->getKey(), counter->getType(), counter->getConfig(), counter->getCount(), counter->getCount() > 0 ? PERF_SAMPLE_TID | PERF_SAMPLE_IP : 0, counter->isPerCpu() ? PERF_GROUP_PER_CPU : 0)) {
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100387 logg->logMessage("%s(%s:%i): PerfGroup::add failed", __FUNCTION__, __FILE__, __LINE__);
388 return false;
389 }
390 }
391 }
392
393 return true;
394}
395
396long long PerfDriver::getTracepointId(const char *const name, DynBuf *const printb) {
397 if (!printb->printf(EVENTS_PATH "/%s/id", name)) {
398 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
399 return -1;
400 }
401
402 int64_t result;
403 if (DriverSource::readInt64Driver(printb->getBuf(), &result) != 0) {
404 logg->logMessage("%s(%s:%i): DriverSource::readInt64Driver failed", __FUNCTION__, __FILE__, __LINE__);
405 return -1;
406 }
407
408 return result;
409}