gator: Version 5.18
[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
15 #include "Buffer.h"
16 #include "Config.h"
17 #include "ConfigurationXML.h"
18 #include "Counter.h"
19 #include "DriverSource.h"
20 #include "DynBuf.h"
21 #include "Logging.h"
22 #include "PerfGroup.h"
23 #include "SessionData.h"
24
25 #define PERF_DEVICES "/sys/bus/event_source/devices"
26
27 #define TYPE_DERIVED ~0U
28
29 // From gator.h
30 struct gator_cpu {
31         const int cpuid;
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;
37 };
38
39 // From gator_main.c
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 },
60 };
61
62 static const char OLD_PMU_PREFIX[] = "ARMv7 Cortex-";
63 static const char NEW_PMU_PREFIX[] = "ARMv7_Cortex_";
64
65 class PerfCounter {
66 public:
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) {}
68         ~PerfCounter() {
69                 delete [] mName;
70         }
71
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; }
82
83 private:
84         PerfCounter *const mNext;
85         const char *const mName;
86         const uint32_t mType;
87         int mCount;
88         const int mKey;
89         uint64_t mConfig;
90         bool mEnabled;
91 };
92
93 PerfDriver::PerfDriver() : mCounters(NULL), mIsSetup(false) {
94 }
95
96 PerfDriver::~PerfDriver() {
97         while (mCounters != NULL) {
98                 PerfCounter *counter = mCounters;
99                 mCounters = counter->getNext();
100                 delete counter;
101         }
102 }
103
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);
109
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);
115         }
116 }
117
118 // From include/generated/uapi/linux/version.h
119 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
120
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__);
126                 return false;
127         }
128
129         int release[3] = { 0, 0, 0 };
130         int part = 0;
131         char *ch = utsname.release;
132         while (*ch >= '0' && *ch <= '9' && part < ARRAY_LENGTH(release)) {
133                 release[part] = 10*release[part] + *ch - '0';
134
135                 ++ch;
136                 if (*ch == '.') {
137                         ++part;
138                         ++ch;
139                 }
140         }
141
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__);
144                 return false;
145         }
146
147         // Add supported PMUs
148         bool foundCpu = false;
149         DIR *dir = opendir(PERF_DEVICES);
150         if (dir == NULL) {
151                 logg->logMessage("%s(%s:%i): opendif failed", __FUNCTION__, __FILE__, __LINE__);
152                 return false;
153         }
154
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)) {
164                                 continue;
165                         }
166
167                         int type;
168                         char buf[256];
169                         snprintf(buf, sizeof(buf), PERF_DEVICES "/%s/type", dirent->d_name);
170                         if (DriverSource::readIntDriver(buf, &type) != 0) {
171                                 continue;
172                         }
173
174                         foundCpu = true;
175                         addCpuCounters(gator_cpus[i].pmnc_name, type, gator_cpus[i].pmnc_counters);
176                 }
177         }
178         closedir(dir);
179
180         if (!foundCpu) {
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) {
184                                 continue;
185                         }
186
187                         foundCpu = true;
188                         addCpuCounters(gator_cpus[i].pmnc_name, PERF_TYPE_RAW, gator_cpus[i].pmnc_counters);
189                 }
190         }
191
192         /*
193         if (!foundCpu) {
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);
197         }
198         */
199
200         // Add supported software counters
201         long long id;
202         DynBuf printb;
203
204         id = getTracepointId("irq/softirq_exit", &printb);
205         if (id >= 0) {
206                 mCounters = new PerfCounter(mCounters, "Linux_irq_softirq", PERF_TYPE_TRACEPOINT, id);
207         }
208
209         id = getTracepointId("irq/irq_handler_exit", &printb);
210         if (id >= 0) {
211                 mCounters = new PerfCounter(mCounters, "Linux_irq_irq", PERF_TYPE_TRACEPOINT, id);
212         }
213
214         //Linux_block_rq_wr
215         //Linux_block_rq_rd
216         //Linux_net_rx
217         //Linux_net_tx
218
219         id = getTracepointId(SCHED_SWITCH, &printb);
220         if (id >= 0) {
221                 mCounters = new PerfCounter(mCounters, "Linux_sched_switch", PERF_TYPE_TRACEPOINT, id);
222         }
223
224         //Linux_meminfo_memused
225         //Linux_meminfo_memfree
226         //Linux_meminfo_bufferram
227         //Linux_power_cpu_freq
228         //Linux_power_cpu_idle
229
230         mCounters = new PerfCounter(mCounters, "Linux_cpu_wait_contention", TYPE_DERIVED, -1);
231
232         //Linux_cpu_wait_io
233
234         mIsSetup = true;
235         return true;
236 }
237
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__);
242                 return false;
243         }
244
245         char buf[512];
246         snprintf(buf, sizeof(buf), "%s %s %s %s %s GNU/Linux", utsname.sysname, utsname.nodename, utsname.release, utsname.version, utsname.machine);
247
248         struct timespec ts;
249         if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
250                 logg->logMessage("%s(%s:%i): clock_gettime failed", __FUNCTION__, __FILE__, __LINE__);
251                 return false;
252         }
253         const int64_t timestamp = (int64_t)ts.tv_sec * 1000000000L + ts.tv_nsec;
254
255         if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
256                 logg->logMessage("%s(%s:%i): clock_gettime failed", __FUNCTION__, __FILE__, __LINE__);
257                 return false;
258         }
259         const int64_t uptime = (int64_t)ts.tv_sec * 1000000000L + ts.tv_nsec;
260
261         buffer->summary(timestamp, uptime, 0, buf);
262
263         for (int i = 0; i < gSessionData->mCores; ++i) {
264                 int j;
265                 for (j = 0; j < ARRAY_LENGTH(gator_cpus); ++j) {
266                         if (gator_cpus[j].cpuid == gSessionData->mCpuIds[i]) {
267                                 break;
268                         }
269                 }
270                 if (gator_cpus[j].cpuid == gSessionData->mCpuIds[i]) {
271                         buffer->coreName(i, gSessionData->mCpuIds[i], gator_cpus[j].core_name);
272                 } else {
273                         snprintf(buf, sizeof(buf), "Unknown (0x%.3x)", gSessionData->mCpuIds[i]);
274                         buffer->coreName(i, gSessionData->mCpuIds[i], buf);
275                 }
276         }
277         buffer->commit(1);
278
279         return true;
280 }
281
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) {
285                         return perfCounter;
286                 }
287         }
288
289         return NULL;
290 }
291
292 bool PerfDriver::claimCounter(const Counter &counter) const {
293         return findCounter(counter) != NULL;
294 }
295
296 void PerfDriver::resetCounters() {
297         for (PerfCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
298                 counter->setEnabled(false);
299         }
300 }
301
302 void PerfDriver::setupCounter(Counter &counter) {
303         PerfCounter *const perfCounter = findCounter(counter);
304         if (perfCounter == NULL) {
305                 counter.setEnabled(false);
306                 return;
307         }
308
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());
312         }
313         perfCounter->setCount(counter.getCount());
314         perfCounter->setEnabled(true);
315         counter.setKey(perfCounter->getKey());
316 }
317
318 int PerfDriver::writeCounters(mxml_node_t *root) const {
319         int count = 0;
320         for (PerfCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
321                 mxml_node_t *node = mxmlNewElement(root, "counter");
322                 mxmlElementSetAttr(node, "name", counter->getName());
323                 ++count;
324         }
325
326         return count;
327 }
328
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__);
334                                 return false;
335                         }
336                 }
337         }
338
339         return true;
340 }
341
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__);
345                 return -1;
346         }
347
348         int64_t result;
349         if (DriverSource::readInt64Driver(printb->getBuf(), &result) != 0) {
350                 logg->logMessage("%s(%s:%i): DriverSource::readInt64Driver failed", __FUNCTION__, __FILE__, __LINE__);
351                 return -1;
352         }
353
354         return result;
355 }