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