Merge branch 'linux-3.10.y' of git://git.kernel.org/pub/scm/linux/kernel/git/stable...
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / KMod.cpp
1 /**
2  * Copyright (C) ARM Limited 2013-2015. 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 "KMod.h"
10
11 #include <sys/types.h>
12 #include <dirent.h>
13 #include <unistd.h>
14
15 #include "ConfigurationXML.h"
16 #include "Counter.h"
17 #include "DriverSource.h"
18 #include "Logging.h"
19 #include "SessionData.h"
20
21 // Claim all the counters in /dev/gator/events
22 bool KMod::claimCounter(const Counter &counter) const {
23         char text[128];
24         snprintf(text, sizeof(text), "/dev/gator/events/%s", counter.getType());
25         return access(text, F_OK) == 0;
26 }
27
28 void KMod::resetCounters() {
29         char base[128];
30         char text[128];
31
32         // Initialize all perf counters in the driver, i.e. set enabled to zero
33         struct dirent *ent;
34         DIR* dir = opendir("/dev/gator/events");
35         if (dir) {
36                 while ((ent = readdir(dir)) != NULL) {
37                         // skip hidden files, current dir, and parent dir
38                         if (ent->d_name[0] == '.')
39                                 continue;
40                         snprintf(base, sizeof(base), "/dev/gator/events/%s", ent->d_name);
41                         snprintf(text, sizeof(text), "%s/enabled", base);
42                         DriverSource::writeDriver(text, 0);
43                         snprintf(text, sizeof(text), "%s/count", base);
44                         DriverSource::writeDriver(text, 0);
45                 }
46                 closedir(dir);
47         }
48 }
49
50 static const char ARM_MALI_MIDGARD[] = "ARM_Mali-Midgard_";
51 static const char ARM_MALI_T[] = "ARM_Mali-T";
52
53 void KMod::setupCounter(Counter &counter) {
54         char base[128];
55         char text[128];
56         snprintf(base, sizeof(base), "/dev/gator/events/%s", counter.getType());
57
58         if ((strncmp(counter.getType(), ARM_MALI_MIDGARD, sizeof(ARM_MALI_MIDGARD) - 1) == 0 ||
59              strncmp(counter.getType(), ARM_MALI_T, sizeof(ARM_MALI_T) - 1) == 0)) {
60                 mIsMaliCapture = true;
61         }
62
63         snprintf(text, sizeof(text), "%s/enabled", base);
64         int enabled = true;
65         if (DriverSource::writeReadDriver(text, &enabled) || !enabled) {
66                 counter.setEnabled(false);
67                 return;
68         }
69
70         int value = 0;
71         snprintf(text, sizeof(text), "%s/key", base);
72         DriverSource::readIntDriver(text, &value);
73         counter.setKey(value);
74
75         snprintf(text, sizeof(text), "%s/cores", base);
76         if (DriverSource::readIntDriver(text, &value) == 0) {
77                 counter.setCores(value);
78         }
79
80         snprintf(text, sizeof(text), "%s/event", base);
81         DriverSource::writeDriver(text, counter.getEvent());
82         snprintf(text, sizeof(text), "%s/count", base);
83         if (access(text, F_OK) == 0) {
84                 int count = counter.getCount();
85                 if (DriverSource::writeReadDriver(text, &count) && counter.getCount() > 0) {
86                         logg->logError("Cannot enable EBS for %s:%i with a count of %d\n", counter.getType(), counter.getEvent(), counter.getCount());
87                         handleException();
88                 }
89                 counter.setCount(count);
90         } else if (counter.getCount() > 0) {
91                 ConfigurationXML::remove();
92                 logg->logError("Event Based Sampling is only supported with kernel versions 3.0.0 and higher with CONFIG_PERF_EVENTS=y, and CONFIG_HW_PERF_EVENTS=y. The invalid configuration.xml has been removed.\n");
93                 handleException();
94         }
95 }
96
97 int KMod::writeCounters(mxml_node_t *root) const {
98         struct dirent *ent;
99         mxml_node_t *counter;
100
101         // counters.xml is simply a file listing of /dev/gator/events
102         DIR* dir = opendir("/dev/gator/events");
103         if (dir == NULL) {
104                 return 0;
105         }
106
107         int count = 0;
108         while ((ent = readdir(dir)) != NULL) {
109                 // skip hidden files, current dir, and parent dir
110                 if (ent->d_name[0] == '.')
111                         continue;
112                 counter = mxmlNewElement(root, "counter");
113                 mxmlElementSetAttr(counter, "name", ent->d_name);
114                 ++count;
115         }
116         closedir(dir);
117
118         return count;
119 }