Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright (C) ARM Limited 2014. 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 "FSDriver.h" |
| 10 | |
| 11 | #include <fcntl.h> |
| 12 | #include <regex.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include "Buffer.h" |
| 18 | #include "Counter.h" |
| 19 | #include "DriverSource.h" |
| 20 | #include "Logging.h" |
| 21 | #include "SessionData.h" |
| 22 | |
| 23 | class FSCounter { |
| 24 | public: |
| 25 | FSCounter(FSCounter *next, char *name, const char *regex); |
| 26 | ~FSCounter(); |
| 27 | |
| 28 | FSCounter *getNext() const { return next; } |
| 29 | int getKey() const { return key; } |
| 30 | bool isEnabled() const { return enabled; } |
| 31 | void setEnabled(const bool enabled) { this->enabled = enabled; } |
| 32 | const char *getName() const { return name; } |
| 33 | int64_t read(); |
| 34 | |
| 35 | private: |
| 36 | FSCounter *const next; |
| 37 | regex_t reg; |
| 38 | char *name; |
| 39 | const int key; |
| 40 | int enabled : 1, |
| 41 | useRegex : 1; |
| 42 | |
| 43 | // Intentionally unimplemented |
| 44 | FSCounter(const FSCounter &); |
| 45 | FSCounter &operator=(const FSCounter &); |
| 46 | }; |
| 47 | |
| 48 | FSCounter::FSCounter(FSCounter *next, char *name, const char *regex) : next(next), name(name), key(getEventKey()), enabled(false), useRegex(regex != NULL) { |
| 49 | if (useRegex) { |
| 50 | int result = regcomp(®, regex, REG_EXTENDED); |
| 51 | if (result != 0) { |
| 52 | char buf[128]; |
| 53 | regerror(result, ®, buf, sizeof(buf)); |
| 54 | logg->logError(__FILE__, __LINE__, "Invalid regex '%s': %s", regex, buf); |
| 55 | handleException(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | FSCounter::~FSCounter() { |
| 61 | free(name); |
| 62 | if (useRegex) { |
| 63 | regfree(®); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | int64_t FSCounter::read() { |
| 68 | int64_t value; |
| 69 | if (useRegex) { |
| 70 | char buf[4096]; |
| 71 | size_t pos = 0; |
| 72 | const int fd = open(name, O_RDONLY); |
| 73 | if (fd < 0) { |
| 74 | goto fail; |
| 75 | } |
| 76 | while (pos < sizeof(buf) - 1) { |
| 77 | const ssize_t bytes = ::read(fd, buf + pos, sizeof(buf) - pos - 1); |
| 78 | if (bytes < 0) { |
| 79 | goto fail; |
| 80 | } else if (bytes == 0) { |
| 81 | break; |
| 82 | } |
| 83 | pos += bytes; |
| 84 | } |
| 85 | close(fd); |
| 86 | buf[pos] = '\0'; |
| 87 | |
| 88 | regmatch_t match[2]; |
| 89 | int result = regexec(®, buf, 2, match, 0); |
| 90 | if (result != 0) { |
| 91 | regerror(result, ®, buf, sizeof(buf)); |
| 92 | logg->logError(__FILE__, __LINE__, "Parsing %s failed: %s", name, buf); |
| 93 | handleException(); |
| 94 | } |
| 95 | |
| 96 | if (match[1].rm_so < 0) { |
| 97 | logg->logError(__FILE__, __LINE__, "Parsing %s failed", name); |
| 98 | handleException(); |
| 99 | } |
| 100 | char *endptr; |
| 101 | errno = 0; |
| 102 | value = strtoll(buf + match[1].rm_so, &endptr, 0); |
| 103 | if (errno != 0) { |
| 104 | logg->logError(__FILE__, __LINE__, "Parsing %s failed: %s", name, strerror(errno)); |
| 105 | handleException(); |
| 106 | } |
| 107 | } else { |
| 108 | if (DriverSource::readInt64Driver(name, &value) != 0) { |
| 109 | goto fail; |
| 110 | } |
| 111 | } |
| 112 | return value; |
| 113 | |
| 114 | fail: |
| 115 | logg->logError(__FILE__, __LINE__, "Unable to read %s", name); |
| 116 | handleException(); |
| 117 | } |
| 118 | |
| 119 | FSDriver::FSDriver() : counters(NULL) { |
| 120 | } |
| 121 | |
| 122 | FSDriver::~FSDriver() { |
| 123 | while (counters != NULL) { |
| 124 | FSCounter * counter = counters; |
| 125 | counters = counter->getNext(); |
| 126 | delete counter; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void FSDriver::setup(mxml_node_t *const xml) { |
| 131 | // fs driver does not currently work with perf |
| 132 | if (gSessionData->perf.isSetup()) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | mxml_node_t *node = xml; |
| 137 | while (true) { |
| 138 | node = mxmlFindElement(node, xml, "event", NULL, NULL, MXML_DESCEND); |
| 139 | if (node == NULL) { |
| 140 | break; |
| 141 | } |
| 142 | const char *counter = mxmlElementGetAttr(node, "counter"); |
| 143 | if ((counter != NULL) && (counter[0] == '/')) { |
| 144 | const char *regex = mxmlElementGetAttr(node, "regex"); |
| 145 | counters = new FSCounter(counters, strdup(counter), regex); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | FSCounter *FSDriver::findCounter(const Counter &counter) const { |
| 151 | for (FSCounter * fsCounter = counters; fsCounter != NULL; fsCounter = fsCounter->getNext()) { |
| 152 | if (strcmp(fsCounter->getName(), counter.getType()) == 0) { |
| 153 | return fsCounter; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | bool FSDriver::claimCounter(const Counter &counter) const { |
| 161 | return findCounter(counter) != NULL; |
| 162 | } |
| 163 | |
| 164 | bool FSDriver::countersEnabled() const { |
| 165 | for (FSCounter *counter = counters; counter != NULL; counter = counter->getNext()) { |
| 166 | if (counter->isEnabled()) { |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | void FSDriver::resetCounters() { |
| 174 | for (FSCounter * counter = counters; counter != NULL; counter = counter->getNext()) { |
| 175 | counter->setEnabled(false); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void FSDriver::setupCounter(Counter &counter) { |
| 180 | FSCounter *const fsCounter = findCounter(counter); |
| 181 | if (fsCounter == NULL) { |
| 182 | counter.setEnabled(false); |
| 183 | return; |
| 184 | } |
| 185 | fsCounter->setEnabled(true); |
| 186 | counter.setKey(fsCounter->getKey()); |
| 187 | } |
| 188 | |
| 189 | int FSDriver::writeCounters(mxml_node_t *root) const { |
| 190 | int count = 0; |
| 191 | for (FSCounter * counter = counters; counter != NULL; counter = counter->getNext()) { |
| 192 | if (access(counter->getName(), R_OK) == 0) { |
| 193 | mxml_node_t *node = mxmlNewElement(root, "counter"); |
| 194 | mxmlElementSetAttr(node, "name", counter->getName()); |
| 195 | ++count; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return count; |
| 200 | } |
| 201 | |
| 202 | void FSDriver::start() { |
| 203 | } |
| 204 | |
| 205 | void FSDriver::read(Buffer * const buffer) { |
| 206 | for (FSCounter * counter = counters; counter != NULL; counter = counter->getNext()) { |
| 207 | if (!counter->isEnabled()) { |
| 208 | continue; |
| 209 | } |
| 210 | buffer->event(counter->getKey(), counter->read()); |
| 211 | } |
| 212 | } |