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