2 * Copyright (C) ARM Limited 2014. 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.
14 #include <sys/types.h>
17 #include "DriverSource.h"
20 class FSCounter : public DriverCounter {
22 FSCounter(DriverCounter *next, char *name, char *path, const char *regex);
25 const char *getPath() const { return mPath; }
34 // Intentionally unimplemented
35 FSCounter(const FSCounter &);
36 FSCounter &operator=(const FSCounter &);
39 FSCounter::FSCounter(DriverCounter *next, char *name, char *path, const char *regex) : DriverCounter(next, name), mPath(path), mUseRegex(regex != NULL) {
41 int result = regcomp(&mReg, regex, REG_EXTENDED);
44 regerror(result, &mReg, buf, sizeof(buf));
45 logg->logError(__FILE__, __LINE__, "Invalid regex '%s': %s", regex, buf);
51 FSCounter::~FSCounter() {
58 int64_t FSCounter::read() {
63 const int fd = open(mPath, O_RDONLY | O_CLOEXEC);
67 while (pos < sizeof(buf) - 1) {
68 const ssize_t bytes = ::read(fd, buf + pos, sizeof(buf) - pos - 1);
71 } else if (bytes == 0) {
80 int result = regexec(&mReg, buf, 2, match, 0);
82 regerror(result, &mReg, buf, sizeof(buf));
83 logg->logError(__FILE__, __LINE__, "Parsing %s failed: %s", mPath, buf);
87 if (match[1].rm_so < 0) {
88 logg->logError(__FILE__, __LINE__, "Parsing %s failed", mPath);
93 value = strtoll(buf + match[1].rm_so, NULL, 0);
95 logg->logError(__FILE__, __LINE__, "Parsing %s failed: %s", mPath, strerror(errno));
99 if (DriverSource::readInt64Driver(mPath, &value) != 0) {
106 logg->logError(__FILE__, __LINE__, "Unable to read %s", mPath);
110 FSDriver::FSDriver() {
113 FSDriver::~FSDriver() {
116 void FSDriver::readEvents(mxml_node_t *const xml) {
117 mxml_node_t *node = xml;
119 node = mxmlFindElement(node, xml, "event", NULL, NULL, MXML_DESCEND);
123 const char *counter = mxmlElementGetAttr(node, "counter");
124 if (counter == NULL) {
128 if (counter[0] == '/') {
129 logg->logError(__FILE__, __LINE__, "Old style filesystem counter (%s) detected, please create a new unique counter value and move the filename into the path attribute, see events-Filesystem.xml for examples", counter);
133 if (strncmp(counter, "filesystem_", 11) != 0) {
137 const char *path = mxmlElementGetAttr(node, "path");
139 logg->logError(__FILE__, __LINE__, "The filesystem counter %s is missing the required path attribute", counter);
142 const char *regex = mxmlElementGetAttr(node, "regex");
143 setCounters(new FSCounter(getCounters(), strdup(counter), strdup(path), regex));
147 int FSDriver::writeCounters(mxml_node_t *root) const {
149 for (FSCounter *counter = static_cast<FSCounter *>(getCounters()); counter != NULL; counter = static_cast<FSCounter *>(counter->getNext())) {
150 if (access(counter->getPath(), R_OK) == 0) {
151 mxml_node_t *node = mxmlNewElement(root, "counter");
152 mxmlElementSetAttr(node, "name", counter->getName());