2 * Copyright (C) ARM Limited 2013-2014. All rights reserved.
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.
9 #include "PerfDriver.h"
12 #include <sys/utsname.h>
17 #include "ConfigurationXML.h"
19 #include "DriverSource.h"
22 #include "PerfGroup.h"
23 #include "SessionData.h"
25 #define PERF_DEVICES "/sys/bus/event_source/devices"
27 #define TYPE_DERIVED ~0U
32 // Human readable name
33 const char core_name[32];
34 // gatorfs event and Perf PMU name
35 const char *const pmnc_name;
36 const int pmnc_counters;
40 static const struct gator_cpu gator_cpus[] = {
41 { 0xb36, "ARM1136", "ARM_ARM11", 3 },
42 { 0xb56, "ARM1156", "ARM_ARM11", 3 },
43 { 0xb76, "ARM1176", "ARM_ARM11", 3 },
44 { 0xb02, "ARM11MPCore", "ARM_ARM11MPCore", 3 },
45 { 0xc05, "Cortex-A5", "ARMv7_Cortex_A5", 2 },
46 { 0xc07, "Cortex-A7", "ARMv7_Cortex_A7", 4 },
47 { 0xc08, "Cortex-A8", "ARMv7_Cortex_A8", 4 },
48 { 0xc09, "Cortex-A9", "ARMv7_Cortex_A9", 6 },
49 { 0xc0d, "Cortex-A12", "ARMv7_Cortex_A12", 6 },
50 { 0xc0f, "Cortex-A15", "ARMv7_Cortex_A15", 6 },
51 { 0xc0e, "Cortex-A17", "ARMv7_Cortex_A17", 6 },
52 { 0x00f, "Scorpion", "Scorpion", 4 },
53 { 0x02d, "ScorpionMP", "ScorpionMP", 4 },
54 { 0x049, "KraitSIM", "Krait", 4 },
55 { 0x04d, "Krait", "Krait", 4 },
56 { 0x06f, "Krait S4 Pro", "Krait", 4 },
57 { 0xd03, "Cortex-A53", "ARM_Cortex-A53", 6 },
58 { 0xd07, "Cortex-A57", "ARM_Cortex-A57", 6 },
59 { 0xd0f, "AArch64", "ARM_AArch64", 6 },
62 static const char OLD_PMU_PREFIX[] = "ARMv7 Cortex-";
63 static const char NEW_PMU_PREFIX[] = "ARMv7_Cortex_";
67 PerfCounter(PerfCounter *next, const char *name, uint32_t type, uint64_t config) : mNext(next), mName(name), mType(type), mCount(0), mKey(getEventKey()), mConfig(config), mEnabled(false) {}
72 PerfCounter *getNext() const { return mNext; }
73 const char *getName() const { return mName; }
74 uint32_t getType() const { return mType; }
75 int getCount() const { return mCount; }
76 void setCount(const int count) { mCount = count; }
77 int getKey() const { return mKey; }
78 uint64_t getConfig() const { return mConfig; }
79 void setConfig(const uint64_t config) { mConfig = config; }
80 bool isEnabled() const { return mEnabled; }
81 void setEnabled(const bool enabled) { mEnabled = enabled; }
84 PerfCounter *const mNext;
85 const char *const mName;
93 PerfDriver::PerfDriver() : mCounters(NULL), mIsSetup(false) {
96 PerfDriver::~PerfDriver() {
97 while (mCounters != NULL) {
98 PerfCounter *counter = mCounters;
99 mCounters = counter->getNext();
104 void PerfDriver::addCpuCounters(const char *const counterName, const int type, const int numCounters) {
105 int len = snprintf(NULL, 0, "%s_ccnt", counterName) + 1;
106 char *name = new char[len];
107 snprintf(name, len, "%s_ccnt", counterName);
108 mCounters = new PerfCounter(mCounters, name, type, -1);
110 for (int j = 0; j < numCounters; ++j) {
111 len = snprintf(NULL, 0, "%s_cnt%d", counterName, j) + 1;
112 name = new char[len];
113 snprintf(name, len, "%s_cnt%d", counterName, j);
114 mCounters = new PerfCounter(mCounters, name, type, -1);
118 // From include/generated/uapi/linux/version.h
119 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
121 bool PerfDriver::setup() {
122 // Check the kernel version
123 struct utsname utsname;
124 if (uname(&utsname) != 0) {
125 logg->logMessage("%s(%s:%i): uname failed", __FUNCTION__, __FILE__, __LINE__);
129 int release[3] = { 0, 0, 0 };
131 char *ch = utsname.release;
132 while (*ch >= '0' && *ch <= '9' && part < ARRAY_LENGTH(release)) {
133 release[part] = 10*release[part] + *ch - '0';
142 if (KERNEL_VERSION(release[0], release[1], release[2]) < KERNEL_VERSION(3, 12, 0)) {
143 logg->logMessage("%s(%s:%i): Unsupported kernel version", __FUNCTION__, __FILE__, __LINE__);
147 // Add supported PMUs
148 bool foundCpu = false;
149 DIR *dir = opendir(PERF_DEVICES);
151 logg->logMessage("%s(%s:%i): opendif failed", __FUNCTION__, __FILE__, __LINE__);
155 struct dirent *dirent;
156 while ((dirent = readdir(dir)) != NULL) {
157 for (int i = 0; i < ARRAY_LENGTH(gator_cpus); ++i) {
158 // Do the names match exactly?
159 if (strcmp(dirent->d_name, gator_cpus[i].pmnc_name) != 0 &&
160 // Do these names match but have the old vs new prefix?
161 (strncmp(dirent->d_name, OLD_PMU_PREFIX, sizeof(OLD_PMU_PREFIX) - 1) != 0 ||
162 strncmp(gator_cpus[i].pmnc_name, NEW_PMU_PREFIX, sizeof(NEW_PMU_PREFIX) - 1) != 0 ||
163 strcmp(dirent->d_name + sizeof(OLD_PMU_PREFIX) - 1, gator_cpus[i].pmnc_name + sizeof(NEW_PMU_PREFIX) - 1) != 0)) {
169 snprintf(buf, sizeof(buf), PERF_DEVICES "/%s/type", dirent->d_name);
170 if (DriverSource::readIntDriver(buf, &type) != 0) {
175 addCpuCounters(gator_cpus[i].pmnc_name, type, gator_cpus[i].pmnc_counters);
181 // If no cpu was found based on pmu names, try by cpuid
182 for (int i = 0; i < ARRAY_LENGTH(gator_cpus); ++i) {
183 if (gSessionData->mMaxCpuId != gator_cpus[i].cpuid) {
188 addCpuCounters(gator_cpus[i].pmnc_name, PERF_TYPE_RAW, gator_cpus[i].pmnc_counters);
194 // If all else fails, use the perf architected counters
195 // 9 because that's how many are in events-Perf-Hardware.xml - assume they can all be enabled at once
196 addCpuCounters("Perf_Hardware", PERF_TYPE_HARDWARE, 9);
200 // Add supported software counters
204 id = getTracepointId("irq/softirq_exit", &printb);
206 mCounters = new PerfCounter(mCounters, "Linux_irq_softirq", PERF_TYPE_TRACEPOINT, id);
209 id = getTracepointId("irq/irq_handler_exit", &printb);
211 mCounters = new PerfCounter(mCounters, "Linux_irq_irq", PERF_TYPE_TRACEPOINT, id);
219 id = getTracepointId(SCHED_SWITCH, &printb);
221 mCounters = new PerfCounter(mCounters, "Linux_sched_switch", PERF_TYPE_TRACEPOINT, id);
224 //Linux_meminfo_memused
225 //Linux_meminfo_memfree
226 //Linux_meminfo_bufferram
227 //Linux_power_cpu_freq
228 //Linux_power_cpu_idle
230 mCounters = new PerfCounter(mCounters, "Linux_cpu_wait_contention", TYPE_DERIVED, -1);
238 bool PerfDriver::summary(Buffer *const buffer) {
239 struct utsname utsname;
240 if (uname(&utsname) != 0) {
241 logg->logMessage("%s(%s:%i): uname failed", __FUNCTION__, __FILE__, __LINE__);
246 snprintf(buf, sizeof(buf), "%s %s %s %s %s GNU/Linux", utsname.sysname, utsname.nodename, utsname.release, utsname.version, utsname.machine);
249 if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
250 logg->logMessage("%s(%s:%i): clock_gettime failed", __FUNCTION__, __FILE__, __LINE__);
253 const int64_t timestamp = (int64_t)ts.tv_sec * 1000000000L + ts.tv_nsec;
255 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
256 logg->logMessage("%s(%s:%i): clock_gettime failed", __FUNCTION__, __FILE__, __LINE__);
259 const int64_t uptime = (int64_t)ts.tv_sec * 1000000000L + ts.tv_nsec;
261 buffer->summary(timestamp, uptime, 0, buf);
263 for (int i = 0; i < gSessionData->mCores; ++i) {
265 for (j = 0; j < ARRAY_LENGTH(gator_cpus); ++j) {
266 if (gator_cpus[j].cpuid == gSessionData->mCpuIds[i]) {
270 if (gator_cpus[j].cpuid == gSessionData->mCpuIds[i]) {
271 buffer->coreName(i, gSessionData->mCpuIds[i], gator_cpus[j].core_name);
273 snprintf(buf, sizeof(buf), "Unknown (0x%.3x)", gSessionData->mCpuIds[i]);
274 buffer->coreName(i, gSessionData->mCpuIds[i], buf);
282 PerfCounter *PerfDriver::findCounter(const Counter &counter) const {
283 for (PerfCounter * perfCounter = mCounters; perfCounter != NULL; perfCounter = perfCounter->getNext()) {
284 if (strcmp(perfCounter->getName(), counter.getType()) == 0) {
292 bool PerfDriver::claimCounter(const Counter &counter) const {
293 return findCounter(counter) != NULL;
296 void PerfDriver::resetCounters() {
297 for (PerfCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
298 counter->setEnabled(false);
302 void PerfDriver::setupCounter(Counter &counter) {
303 PerfCounter *const perfCounter = findCounter(counter);
304 if (perfCounter == NULL) {
305 counter.setEnabled(false);
309 // Don't use the config from counters XML if it's not set, ex: software counters
310 if (counter.getEvent() != -1) {
311 perfCounter->setConfig(counter.getEvent());
313 perfCounter->setCount(counter.getCount());
314 perfCounter->setEnabled(true);
315 counter.setKey(perfCounter->getKey());
318 int PerfDriver::writeCounters(mxml_node_t *root) const {
320 for (PerfCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
321 mxml_node_t *node = mxmlNewElement(root, "counter");
322 mxmlElementSetAttr(node, "name", counter->getName());
329 bool PerfDriver::enable(PerfGroup *group, Buffer *const buffer) const {
330 for (PerfCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
331 if (counter->isEnabled() && (counter->getType() != TYPE_DERIVED)) {
332 if (!group->add(buffer, counter->getKey(), counter->getType(), counter->getConfig(), counter->getCount(), 0, 0)) {
333 logg->logMessage("%s(%s:%i): PerfGroup::add failed", __FUNCTION__, __FILE__, __LINE__);
342 long long PerfDriver::getTracepointId(const char *const name, DynBuf *const printb) {
343 if (!printb->printf(EVENTS_PATH "/%s/id", name)) {
344 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
349 if (DriverSource::readInt64Driver(printb->getBuf(), &result) != 0) {
350 logg->logMessage("%s(%s:%i): DriverSource::readInt64Driver failed", __FUNCTION__, __FILE__, __LINE__);