gator: Version 5.20
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / PerfDriver.cpp
1 /**
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>
14 #include <unistd.h>
15
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 #include "Setup.h"
26
27 #define PERF_DEVICES "/sys/bus/event_source/devices"
28
29 #define TYPE_DERIVED ~0U
30
31 // From gator.h
32 struct gator_cpu {
33         const int cpuid;
34         // Human readable name
35         const char *const core_name;
36         // gatorfs event and Perf PMU name
37         const char *const pmnc_name;
38         const int pmnc_counters;
39 };
40
41 // From gator_main.c
42 static const struct gator_cpu gator_cpus[] = {
43         { 0xb36, "ARM1136",      "ARM_ARM11",        3 },
44         { 0xb56, "ARM1156",      "ARM_ARM11",        3 },
45         { 0xb76, "ARM1176",      "ARM_ARM11",        3 },
46         { 0xb02, "ARM11MPCore",  "ARM_ARM11MPCore",  3 },
47         { 0xc05, "Cortex-A5",    "ARMv7_Cortex_A5",  2 },
48         { 0xc07, "Cortex-A7",    "ARMv7_Cortex_A7",  4 },
49         { 0xc08, "Cortex-A8",    "ARMv7_Cortex_A8",  4 },
50         { 0xc09, "Cortex-A9",    "ARMv7_Cortex_A9",  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
63 static const char OLD_PMU_PREFIX[] = "ARMv7 Cortex-";
64 static const char NEW_PMU_PREFIX[] = "ARMv7_Cortex_";
65
66 struct uncore_counter {
67         // Perf PMU name
68         const char *const perfName;
69         // gatorfs event name
70         const char *const gatorName;
71         const int count;
72 };
73
74 static const struct uncore_counter uncore_counters[] = {
75         { "CCI_400", "CCI_400", 4 },
76         { "CCI_400-r1", "CCI_400-r1", 4 },
77         { "ccn", "ARM_CCN_5XX", 8 },
78 };
79
80 class PerfCounter : public DriverCounter {
81 public:
82         PerfCounter(DriverCounter *next, const char *name, uint32_t type, uint64_t config, bool perCpu) : DriverCounter(next, name), mType(type), mCount(0), mConfig(config), mPerCpu(perCpu) {}
83
84         ~PerfCounter() {
85         }
86
87         uint32_t getType() const { return mType; }
88         int getCount() const { return mCount; }
89         void setCount(const int count) { mCount = count; }
90         uint64_t getConfig() const { return mConfig; }
91         void setConfig(const uint64_t config) { mConfig = config; }
92         bool isPerCpu() const { return mPerCpu; }
93
94 private:
95         const uint32_t mType;
96         int mCount;
97         uint64_t mConfig;
98         bool mPerCpu;
99 };
100
101 PerfDriver::PerfDriver() : mIsSetup(false), mLegacySupport(false) {
102 }
103
104 PerfDriver::~PerfDriver() {
105 }
106
107 void PerfDriver::addCpuCounters(const char *const counterName, const int type, const int numCounters) {
108         int len = snprintf(NULL, 0, "%s_ccnt", counterName) + 1;
109         char *name = new char[len];
110         snprintf(name, len, "%s_ccnt", counterName);
111         setCounters(new PerfCounter(getCounters(), name, type, -1, true));
112
113         for (int j = 0; j < numCounters; ++j) {
114                 len = snprintf(NULL, 0, "%s_cnt%d", counterName, j) + 1;
115                 name = new char[len];
116                 snprintf(name, len, "%s_cnt%d", counterName, j);
117                 setCounters(new PerfCounter(getCounters(), name, type, -1, true));
118         }
119 }
120
121 void PerfDriver::addUncoreCounters(const char *const counterName, const int type, const int numCounters) {
122         int len = snprintf(NULL, 0, "%s_ccnt", counterName) + 1;
123         char *name = new char[len];
124         snprintf(name, len, "%s_ccnt", counterName);
125         setCounters(new PerfCounter(getCounters(), name, type, -1, false));
126
127         for (int j = 0; j < numCounters; ++j) {
128                 len = snprintf(NULL, 0, "%s_cnt%d", counterName, j) + 1;
129                 name = new char[len];
130                 snprintf(name, len, "%s_cnt%d", counterName, j);
131                 setCounters(new PerfCounter(getCounters(), name, type, -1, false));
132         }
133 }
134
135 bool PerfDriver::setup() {
136         // Check the kernel version
137         int release[3];
138         if (!getLinuxVersion(release)) {
139                 logg->logMessage("%s(%s:%i): getLinuxVersion failed", __FUNCTION__, __FILE__, __LINE__);
140                 return false;
141         }
142
143         if (KERNEL_VERSION(release[0], release[1], release[2]) < KERNEL_VERSION(3, 4, 0)) {
144                 logg->logMessage("%s(%s:%i): Unsupported kernel version", __FUNCTION__, __FILE__, __LINE__);
145                 return false;
146         }
147         mLegacySupport = KERNEL_VERSION(release[0], release[1], release[2]) < KERNEL_VERSION(3, 12, 0);
148
149         if (access(EVENTS_PATH, R_OK) != 0) {
150                 logg->logMessage("%s(%s:%i): " EVENTS_PATH " does not exist, is CONFIG_TRACING and CONFIG_CONTEXT_SWITCH_TRACER enabled?", __FUNCTION__, __FILE__, __LINE__);
151                 return false;
152         }
153
154         // Add supported PMUs
155         bool foundCpu = false;
156         DIR *dir = opendir(PERF_DEVICES);
157         if (dir == NULL) {
158                 logg->logMessage("%s(%s:%i): opendif failed", __FUNCTION__, __FILE__, __LINE__);
159                 return false;
160         }
161
162         struct dirent *dirent;
163         while ((dirent = readdir(dir)) != NULL) {
164                 for (int i = 0; i < ARRAY_LENGTH(gator_cpus); ++i) {
165                         const struct gator_cpu *const gator_cpu = &gator_cpus[i];
166
167                         // Do the names match exactly?
168                         if (strcasecmp(gator_cpu->pmnc_name, dirent->d_name) != 0 &&
169                             // Do these names match but have the old vs new prefix?
170                             ((strncasecmp(dirent->d_name, OLD_PMU_PREFIX, sizeof(OLD_PMU_PREFIX) - 1) != 0 ||
171                               strncasecmp(gator_cpu->pmnc_name, NEW_PMU_PREFIX, sizeof(NEW_PMU_PREFIX) - 1) != 0 ||
172                               strcasecmp(dirent->d_name + sizeof(OLD_PMU_PREFIX) - 1, gator_cpu->pmnc_name + sizeof(NEW_PMU_PREFIX) - 1) != 0))) {
173                                 continue;
174                         }
175
176                         int type;
177                         char buf[256];
178                         snprintf(buf, sizeof(buf), PERF_DEVICES "/%s/type", dirent->d_name);
179                         if (DriverSource::readIntDriver(buf, &type) != 0) {
180                                 continue;
181                         }
182
183                         foundCpu = true;
184                         logg->logMessage("Adding cpu counters for %s", gator_cpu->pmnc_name);
185                         addCpuCounters(gator_cpu->pmnc_name, type, gator_cpu->pmnc_counters);
186                 }
187
188                 for (int i = 0; i < ARRAY_LENGTH(uncore_counters); ++i) {
189                         if (strcmp(dirent->d_name, uncore_counters[i].perfName) != 0) {
190                                 continue;
191                         }
192
193                         int type;
194                         char buf[256];
195                         snprintf(buf, sizeof(buf), PERF_DEVICES "/%s/type", dirent->d_name);
196                         if (DriverSource::readIntDriver(buf, &type) != 0) {
197                                 continue;
198                         }
199
200                         logg->logMessage("Adding uncore counters for %s", uncore_counters[i].gatorName);
201                         addUncoreCounters(uncore_counters[i].gatorName, type, uncore_counters[i].count);
202                 }
203         }
204         closedir(dir);
205
206         if (!foundCpu) {
207                 // If no cpu was found based on pmu names, try by cpuid
208                 for (int i = 0; i < ARRAY_LENGTH(gator_cpus); ++i) {
209                         if (gSessionData->mMaxCpuId != gator_cpus[i].cpuid) {
210                                 continue;
211                         }
212
213                         foundCpu = true;
214                         logg->logMessage("Adding cpu counters (based on cpuid) for %s", gator_cpus[i].pmnc_name);
215                         addCpuCounters(gator_cpus[i].pmnc_name, PERF_TYPE_RAW, gator_cpus[i].pmnc_counters);
216                 }
217         }
218
219         /*
220         if (!foundCpu) {
221                 // If all else fails, use the perf architected counters
222                 // 9 because that's how many are in events-Perf-Hardware.xml - assume they can all be enabled at once
223                 addCpuCounters("Perf_Hardware", PERF_TYPE_HARDWARE, 9);
224         }
225         */
226
227         // Add supported software counters
228         long long id;
229         DynBuf printb;
230
231         id = getTracepointId("irq/softirq_exit", &printb);
232         if (id >= 0) {
233                 setCounters(new PerfCounter(getCounters(), "Linux_irq_softirq", PERF_TYPE_TRACEPOINT, id, true));
234         }
235
236         id = getTracepointId("irq/irq_handler_exit", &printb);
237         if (id >= 0) {
238                 setCounters(new PerfCounter(getCounters(), "Linux_irq_irq", PERF_TYPE_TRACEPOINT, id, true));
239         }
240
241         id = getTracepointId(SCHED_SWITCH, &printb);
242         if (id >= 0) {
243                 setCounters(new PerfCounter(getCounters(), "Linux_sched_switch", PERF_TYPE_TRACEPOINT, id, true));
244         }
245
246         setCounters(new PerfCounter(getCounters(), "Linux_cpu_wait_contention", TYPE_DERIVED, -1, false));
247
248         //Linux_cpu_wait_io
249
250         mIsSetup = true;
251         return true;
252 }
253
254 bool PerfDriver::summary(Buffer *const buffer) {
255         struct utsname utsname;
256         if (uname(&utsname) != 0) {
257                 logg->logMessage("%s(%s:%i): uname failed", __FUNCTION__, __FILE__, __LINE__);
258                 return false;
259         }
260
261         char buf[512];
262         snprintf(buf, sizeof(buf), "%s %s %s %s %s GNU/Linux", utsname.sysname, utsname.nodename, utsname.release, utsname.version, utsname.machine);
263
264         struct timespec ts;
265         if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
266                 logg->logMessage("%s(%s:%i): clock_gettime failed", __FUNCTION__, __FILE__, __LINE__);
267                 return false;
268         }
269         const int64_t timestamp = (int64_t)ts.tv_sec * NS_PER_S + ts.tv_nsec;
270
271         const uint64_t monotonicStarted = getTime();
272         gSessionData->mMonotonicStarted = monotonicStarted;
273
274         buffer->summary(monotonicStarted, timestamp, monotonicStarted, monotonicStarted, buf);
275
276         for (int i = 0; i < gSessionData->mCores; ++i) {
277                 coreName(monotonicStarted, buffer, i);
278         }
279         buffer->commit(monotonicStarted);
280
281         return true;
282 }
283
284 void PerfDriver::coreName(const uint32_t startTime, Buffer *const buffer, const int cpu) {
285         // Don't send information on a cpu we know nothing about
286         if (gSessionData->mCpuIds[cpu] == -1) {
287                 return;
288         }
289
290         int j;
291         for (j = 0; j < ARRAY_LENGTH(gator_cpus); ++j) {
292                 if (gator_cpus[j].cpuid == gSessionData->mCpuIds[cpu]) {
293                         break;
294                 }
295         }
296         if (gator_cpus[j].cpuid == gSessionData->mCpuIds[cpu]) {
297                 buffer->coreName(startTime, cpu, gSessionData->mCpuIds[cpu], gator_cpus[j].core_name);
298         } else {
299                 char buf[32];
300                 if (gSessionData->mCpuIds[cpu] == -1) {
301                         snprintf(buf, sizeof(buf), "Unknown");
302                 } else {
303                         snprintf(buf, sizeof(buf), "Unknown (0x%.3x)", gSessionData->mCpuIds[cpu]);
304                 }
305                 buffer->coreName(startTime, cpu, gSessionData->mCpuIds[cpu], buf);
306         }
307 }
308
309 void PerfDriver::setupCounter(Counter &counter) {
310         PerfCounter *const perfCounter = static_cast<PerfCounter *>(findCounter(counter));
311         if (perfCounter == NULL) {
312                 counter.setEnabled(false);
313                 return;
314         }
315
316         // Don't use the config from counters XML if it's not set, ex: software counters
317         if (counter.getEvent() != -1) {
318                 perfCounter->setConfig(counter.getEvent());
319         }
320         perfCounter->setCount(counter.getCount());
321         perfCounter->setEnabled(true);
322         counter.setKey(perfCounter->getKey());
323 }
324
325 bool PerfDriver::enable(const uint64_t currTime, PerfGroup *const group, Buffer *const buffer) const {
326         for (PerfCounter *counter = static_cast<PerfCounter *>(getCounters()); counter != NULL; counter = static_cast<PerfCounter *>(counter->getNext())) {
327                 if (counter->isEnabled() && (counter->getType() != TYPE_DERIVED)) {
328                         if (!group->add(currTime, 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)) {
329                                 logg->logMessage("%s(%s:%i): PerfGroup::add failed", __FUNCTION__, __FILE__, __LINE__);
330                                 return false;
331                         }
332                 }
333         }
334
335         return true;
336 }
337
338 long long PerfDriver::getTracepointId(const char *const name, DynBuf *const printb) {
339         if (!printb->printf(EVENTS_PATH "/%s/id", name)) {
340                 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
341                 return -1;
342         }
343
344         int64_t result;
345         if (DriverSource::readInt64Driver(printb->getBuf(), &result) != 0) {
346                 logg->logMessage("%s(%s:%i): DriverSource::readInt64Driver failed", __FUNCTION__, __FILE__, __LINE__);
347                 return -1;
348         }
349
350         return result;
351 }