1 /**
2 * Copyright (C) ARM Limited 2013. 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 */
9 #include "KMod.h"
11 #include <sys/types.h>
12 #include <dirent.h>
13 #include <unistd.h>
15 #include "Collector.h"
16 #include "Counter.h"
17 #include "Logging.h"
19 // Claim all the counters in /dev/gator/events
20 bool KMod::claimCounter(const Counter &counter) const {
21 char text[128];
22 snprintf(text, sizeof(text), "/dev/gator/events/%s", counter.getType());
23 return access(text, F_OK) == 0;
24 }
26 void KMod::resetCounters() {
27 char base[128];
28 char text[128];
30 // Initialize all perf counters in the driver, i.e. set enabled to zero
31 struct dirent *ent;
32 DIR* dir = opendir("/dev/gator/events");
33 if (dir) {
34 while ((ent = readdir(dir)) != NULL) {
35 // skip hidden files, current dir, and parent dir
36 if (ent->d_name[0] == '.')
37 continue;
38 snprintf(base, sizeof(base), "/dev/gator/events/%s", ent->d_name);
39 snprintf(text, sizeof(text), "%s/enabled", base);
40 Collector::writeDriver(text, 0);
41 snprintf(text, sizeof(text), "%s/count", base);
42 Collector::writeDriver(text, 0);
43 }
44 closedir(dir);
45 }
46 }
48 void KMod::setupCounter(Counter &counter) {
49 char base[128];
50 char text[128];
51 snprintf(base, sizeof(base), "/dev/gator/events/%s", counter.getType());
53 snprintf(text, sizeof(text), "%s/enabled", base);
54 int enabled = true;
55 if (Collector::writeReadDriver(text, &enabled) || !enabled) {
56 counter.setEnabled(false);
57 return;
58 }
60 snprintf(text, sizeof(text), "%s/key", base);
61 int key = 0;
62 Collector::readIntDriver(text, &key);
63 counter.setKey(key);
65 snprintf(text, sizeof(text), "%s/event", base);
66 Collector::writeDriver(text, counter.getEvent());
67 snprintf(text, sizeof(text), "%s/count", base);
68 if (access(text, F_OK) == 0) {
69 int count = counter.getCount();
70 if (Collector::writeReadDriver(text, &count) && counter.getCount() > 0) {
71 logg->logError(__FILE__, __LINE__, "Cannot enable EBS for %s:%i with a count of %d\n", counter.getType(), counter.getEvent(), counter.getCount());
72 handleException();
73 }
74 counter.setCount(count);
75 } else if (counter.getCount() > 0) {
76 logg->logError(__FILE__, __LINE__, "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\n");
77 handleException();
78 }
79 }
81 void KMod::writeCounters(mxml_node_t *root) const {
82 struct dirent *ent;
83 mxml_node_t *counter;
85 // counters.xml is simply a file listing of /dev/gator/events
86 DIR* dir = opendir("/dev/gator/events");
87 if (dir == NULL) {
88 logg->logError(__FILE__, __LINE__, "Cannot create counters.xml since unable to read /dev/gator/events");
89 handleException();
90 }
92 while ((ent = readdir(dir)) != NULL) {
93 // skip hidden files, current dir, and parent dir
94 if (ent->d_name[0] == '.')
95 continue;
96 counter = mxmlNewElement(root, "counter");
97 mxmlElementSetAttr(counter, "name", ent->d_name);
98 }
99 closedir(dir);
100 }