blob: fe9dc6a7e4f7027919743189dd52c5a9c1905266 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
Jon Medhurst15ce78d2014-04-10 09:02:02 +01002 * Copyright (C) ARM Limited 2013-2014. All rights reserved.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
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
Jon Medhurstd3698592013-10-10 16:48:56 +010015#include "ConfigurationXML.h"
Jon Medhurstaaf37a32013-06-11 12:10:56 +010016#include "Counter.h"
Jon Medhurst15ce78d2014-04-10 09:02:02 +010017#include "DriverSource.h"
Jon Medhurstaaf37a32013-06-11 12:10:56 +010018#include "Logging.h"
Jon Medhurst96b56152014-10-30 18:01:15 +000019#include "SessionData.h"
Jon Medhurstaaf37a32013-06-11 12:10:56 +010020
21// Claim all the counters in /dev/gator/events
22bool 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
28void 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);
Jon Medhurst15ce78d2014-04-10 09:02:02 +010042 DriverSource::writeDriver(text, 0);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010043 snprintf(text, sizeof(text), "%s/count", base);
Jon Medhurst15ce78d2014-04-10 09:02:02 +010044 DriverSource::writeDriver(text, 0);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010045 }
46 closedir(dir);
47 }
48}
49
Jon Medhurst96b56152014-10-30 18:01:15 +000050static const char ARM_MALI_MIDGARD[] = "ARM_Mali-Midgard_";
51static const char ARM_MALI_T[] = "ARM_Mali-T";
52
Jon Medhurstaaf37a32013-06-11 12:10:56 +010053void KMod::setupCounter(Counter &counter) {
54 char base[128];
55 char text[128];
56 snprintf(base, sizeof(base), "/dev/gator/events/%s", counter.getType());
57
Jon Medhurst96b56152014-10-30 18:01:15 +000058 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
Jon Medhurstaaf37a32013-06-11 12:10:56 +010063 snprintf(text, sizeof(text), "%s/enabled", base);
64 int enabled = true;
Jon Medhurst15ce78d2014-04-10 09:02:02 +010065 if (DriverSource::writeReadDriver(text, &enabled) || !enabled) {
Jon Medhurstaaf37a32013-06-11 12:10:56 +010066 counter.setEnabled(false);
67 return;
68 }
69
Jon Medhurste31266f2014-08-04 15:47:44 +010070 int value = 0;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010071 snprintf(text, sizeof(text), "%s/key", base);
Jon Medhurste31266f2014-08-04 15:47:44 +010072 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 }
Jon Medhurstaaf37a32013-06-11 12:10:56 +010079
80 snprintf(text, sizeof(text), "%s/event", base);
Jon Medhurst15ce78d2014-04-10 09:02:02 +010081 DriverSource::writeDriver(text, counter.getEvent());
Jon Medhurstaaf37a32013-06-11 12:10:56 +010082 snprintf(text, sizeof(text), "%s/count", base);
83 if (access(text, F_OK) == 0) {
84 int count = counter.getCount();
Jon Medhurst15ce78d2014-04-10 09:02:02 +010085 if (DriverSource::writeReadDriver(text, &count) && counter.getCount() > 0) {
Jon Medhurstaaf37a32013-06-11 12:10:56 +010086 logg->logError(__FILE__, __LINE__, "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) {
Jon Medhurstd3698592013-10-10 16:48:56 +010091 ConfigurationXML::remove();
92 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. The invalid configuration.xml has been removed.\n");
Jon Medhurstaaf37a32013-06-11 12:10:56 +010093 handleException();
94 }
95}
96
Jon Medhurst15ce78d2014-04-10 09:02:02 +010097int KMod::writeCounters(mxml_node_t *root) const {
Jon Medhurstaaf37a32013-06-11 12:10:56 +010098 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) {
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100104 return 0;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100105 }
106
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100107 int count = 0;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100108 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);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100114 ++count;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100115 }
116 closedir(dir);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100117
118 return count;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100119}