2 * Copyright (C) ARM Limited 2013-2015. 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.
11 #include <sys/types.h>
15 #include "ConfigurationXML.h"
17 #include "DriverSource.h"
19 #include "SessionData.h"
21 // Claim all the counters in /dev/gator/events
22 bool KMod::claimCounter(const Counter &counter) const {
24 snprintf(text, sizeof(text), "/dev/gator/events/%s", counter.getType());
25 return access(text, F_OK) == 0;
28 void KMod::resetCounters() {
32 // Initialize all perf counters in the driver, i.e. set enabled to zero
34 DIR* dir = opendir("/dev/gator/events");
36 while ((ent = readdir(dir)) != NULL) {
37 // skip hidden files, current dir, and parent dir
38 if (ent->d_name[0] == '.')
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);
50 static const char ARM_MALI_MIDGARD[] = "ARM_Mali-Midgard_";
51 static const char ARM_MALI_T[] = "ARM_Mali-T";
53 void KMod::setupCounter(Counter &counter) {
56 snprintf(base, sizeof(base), "/dev/gator/events/%s", counter.getType());
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;
63 snprintf(text, sizeof(text), "%s/enabled", base);
65 if (DriverSource::writeReadDriver(text, &enabled) || !enabled) {
66 counter.setEnabled(false);
71 snprintf(text, sizeof(text), "%s/key", base);
72 DriverSource::readIntDriver(text, &value);
73 counter.setKey(value);
75 snprintf(text, sizeof(text), "%s/cores", base);
76 if (DriverSource::readIntDriver(text, &value) == 0) {
77 counter.setCores(value);
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());
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");
97 int KMod::writeCounters(mxml_node_t *root) const {
101 // counters.xml is simply a file listing of /dev/gator/events
102 DIR* dir = opendir("/dev/gator/events");
108 while ((ent = readdir(dir)) != NULL) {
109 // skip hidden files, current dir, and parent dir
110 if (ent->d_name[0] == '.')
112 counter = mxmlNewElement(root, "counter");
113 mxmlElementSetAttr(counter, "name", ent->d_name);