Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) ARM Limited 2010-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 <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <dirent.h> |
| 12 | #include "ConfigurationXML.h" |
| 13 | #include "Driver.h" |
| 14 | #include "Logging.h" |
| 15 | #include "OlyUtility.h" |
| 16 | #include "SessionData.h" |
| 17 | |
| 18 | static const char* ATTR_COUNTER = "counter"; |
| 19 | static const char* ATTR_REVISION = "revision"; |
| 20 | static const char* ATTR_EVENT = "event"; |
| 21 | static const char* ATTR_COUNT = "count"; |
| 22 | |
| 23 | ConfigurationXML::ConfigurationXML() { |
| 24 | const char * configuration_xml; |
| 25 | unsigned int configuration_xml_len; |
| 26 | getDefaultConfigurationXml(configuration_xml, configuration_xml_len); |
| 27 | |
| 28 | char path[PATH_MAX]; |
| 29 | |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame^] | 30 | getPath(path); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 31 | mConfigurationXML = util->readFromDisk(path); |
| 32 | |
| 33 | for (int retryCount = 0; retryCount < 2; ++retryCount) { |
| 34 | if (mConfigurationXML == NULL) { |
| 35 | logg->logMessage("Unable to locate configuration.xml, using default in binary"); |
| 36 | // null-terminate configuration_xml |
| 37 | mConfigurationXML = (char*)malloc(configuration_xml_len + 1); |
| 38 | memcpy(mConfigurationXML, (const void*)configuration_xml, configuration_xml_len); |
| 39 | mConfigurationXML[configuration_xml_len] = 0; |
| 40 | } |
| 41 | |
| 42 | int ret = parse(mConfigurationXML); |
| 43 | if (ret == 1) { |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame^] | 44 | remove(); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 45 | |
| 46 | // Free the current configuration and reload |
| 47 | free((void*)mConfigurationXML); |
| 48 | mConfigurationXML = NULL; |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | validate(); |
| 56 | } |
| 57 | |
| 58 | ConfigurationXML::~ConfigurationXML() { |
| 59 | if (mConfigurationXML) { |
| 60 | free((void*)mConfigurationXML); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | int ConfigurationXML::parse(const char* configurationXML) { |
| 65 | mxml_node_t *tree, *node; |
| 66 | int ret; |
| 67 | |
| 68 | // clear counter overflow |
| 69 | gSessionData->mCounterOverflow = 0; |
| 70 | mIndex = 0; |
| 71 | |
| 72 | // disable all counters prior to parsing the configuration xml |
| 73 | for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) { |
| 74 | gSessionData->mCounters[i].setEnabled(false); |
| 75 | } |
| 76 | |
| 77 | tree = mxmlLoadString(NULL, configurationXML, MXML_NO_CALLBACK); |
| 78 | |
| 79 | node = mxmlGetFirstChild(tree); |
| 80 | while (node && mxmlGetType(node) != MXML_ELEMENT) |
| 81 | node = mxmlWalkNext(node, tree, MXML_NO_DESCEND); |
| 82 | |
| 83 | ret = configurationsTag(node); |
| 84 | |
| 85 | node = mxmlGetFirstChild(node); |
| 86 | while (node) { |
| 87 | if (mxmlGetType(node) != MXML_ELEMENT) { |
| 88 | node = mxmlWalkNext(node, tree, MXML_NO_DESCEND); |
| 89 | continue; |
| 90 | } |
| 91 | configurationTag(node); |
| 92 | node = mxmlWalkNext(node, tree, MXML_NO_DESCEND); |
| 93 | } |
| 94 | |
| 95 | mxmlDelete(tree); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | void ConfigurationXML::validate(void) { |
| 101 | for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) { |
| 102 | const Counter & counter = gSessionData->mCounters[i]; |
| 103 | if (counter.isEnabled()) { |
| 104 | if (strcmp(counter.getType(), "") == 0) { |
| 105 | logg->logError(__FILE__, __LINE__, "Invalid required attribute in configuration.xml:\n counter=\"%s\"\n event=%d\n", counter.getType(), counter.getEvent()); |
| 106 | handleException(); |
| 107 | } |
| 108 | |
| 109 | // iterate through the remaining enabled performance counters |
| 110 | for (int j = i + 1; j < MAX_PERFORMANCE_COUNTERS; j++) { |
| 111 | const Counter & counter2 = gSessionData->mCounters[j]; |
| 112 | if (counter2.isEnabled()) { |
| 113 | // check if the types are the same |
| 114 | if (strcmp(counter.getType(), counter2.getType()) == 0) { |
| 115 | logg->logError(__FILE__, __LINE__, "Duplicate performance counter type in configuration.xml: %s", counter.getType()); |
| 116 | handleException(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #define CONFIGURATION_REVISION 3 |
| 125 | int ConfigurationXML::configurationsTag(mxml_node_t *node) { |
| 126 | const char* revision_string; |
| 127 | |
| 128 | revision_string = mxmlElementGetAttr(node, ATTR_REVISION); |
| 129 | if (!revision_string) { |
| 130 | return 1; //revision issue; |
| 131 | } |
| 132 | |
| 133 | int revision = strtol(revision_string, NULL, 10); |
| 134 | if (revision < CONFIGURATION_REVISION) { |
| 135 | return 1; // revision issue |
| 136 | } |
| 137 | |
| 138 | // A revision >= CONFIGURATION_REVISION is okay |
| 139 | // Greater than can occur when Streamline is newer than gator |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void ConfigurationXML::configurationTag(mxml_node_t *node) { |
| 145 | // handle all other performance counters |
| 146 | if (mIndex >= MAX_PERFORMANCE_COUNTERS) { |
| 147 | mIndex++; |
| 148 | gSessionData->mCounterOverflow = mIndex; |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | // read attributes |
| 153 | Counter & counter = gSessionData->mCounters[mIndex]; |
| 154 | counter.clear(); |
| 155 | if (mxmlElementGetAttr(node, ATTR_COUNTER)) counter.setType(mxmlElementGetAttr(node, ATTR_COUNTER)); |
| 156 | if (mxmlElementGetAttr(node, ATTR_EVENT)) counter.setEvent(strtol(mxmlElementGetAttr(node, ATTR_EVENT), NULL, 16)); |
| 157 | if (mxmlElementGetAttr(node, ATTR_COUNT)) counter.setCount(strtol(mxmlElementGetAttr(node, ATTR_COUNT), NULL, 10)); |
| 158 | counter.setEnabled(true); |
| 159 | |
| 160 | // Associate a driver with each counter |
| 161 | for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) { |
| 162 | if (driver->claimCounter(counter)) { |
| 163 | if (counter.getDriver() != NULL) { |
| 164 | logg->logError(__FILE__, __LINE__, "More than one driver has claimed %s:%i", counter.getType(), counter.getEvent()); |
| 165 | handleException(); |
| 166 | } |
| 167 | counter.setDriver(driver); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // If no driver is associated with the counter, disable it |
| 172 | if (counter.getDriver() == NULL) { |
| 173 | logg->logMessage("No driver has claimed %s:%i", counter.getType(), counter.getEvent()); |
| 174 | counter.setEnabled(false); |
| 175 | } |
| 176 | |
| 177 | if (counter.isEnabled()) { |
| 178 | // update counter index |
| 179 | mIndex++; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void ConfigurationXML::getDefaultConfigurationXml(const char * & xml, unsigned int & len) { |
| 184 | #include "configuration_xml.h" // defines and initializes char configuration_xml[] and int configuration_xml_len |
| 185 | xml = (const char *)configuration_xml; |
| 186 | len = configuration_xml_len; |
| 187 | } |
Jon Medhurst | d369859 | 2013-10-10 16:48:56 +0100 | [diff] [blame^] | 188 | |
| 189 | void ConfigurationXML::getPath(char* path) { |
| 190 | if (gSessionData->mConfigurationXMLPath) { |
| 191 | strncpy(path, gSessionData->mConfigurationXMLPath, PATH_MAX); |
| 192 | } else { |
| 193 | if (util->getApplicationFullPath(path, PATH_MAX) != 0) { |
| 194 | logg->logMessage("Unable to determine the full path of gatord, the cwd will be used"); |
| 195 | } |
| 196 | strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void ConfigurationXML::remove() { |
| 201 | char path[PATH_MAX]; |
| 202 | getPath(path); |
| 203 | |
| 204 | if (::remove(path) != 0) { |
| 205 | logg->logError(__FILE__, __LINE__, "Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk"); |
| 206 | handleException(); |
| 207 | } |
| 208 | logg->logMessage("Invalid configuration.xml file detected and removed"); |
| 209 | } |