2 * Copyright (C) ARM Limited 2010-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.
9 #include "ConfigurationXML.h"
17 #include "OlyUtility.h"
18 #include "SessionData.h"
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";
24 static const char* ATTR_CORES = "cores";
26 ConfigurationXML::ConfigurationXML() {
27 const char * configuration_xml;
28 unsigned int configuration_xml_len;
29 getDefaultConfigurationXml(configuration_xml, configuration_xml_len);
34 mConfigurationXML = util->readFromDisk(path);
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;
45 int ret = parse(mConfigurationXML);
49 // Free the current configuration and reload
50 free((void*)mConfigurationXML);
51 mConfigurationXML = NULL;
61 ConfigurationXML::~ConfigurationXML() {
62 if (mConfigurationXML) {
63 free((void*)mConfigurationXML);
67 int ConfigurationXML::parse(const char* configurationXML) {
68 mxml_node_t *tree, *node;
71 // clear counter overflow
72 gSessionData->mCounterOverflow = 0;
73 gSessionData->mIsEBS = false;
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);
81 tree = mxmlLoadString(NULL, configurationXML, MXML_NO_CALLBACK);
83 node = mxmlGetFirstChild(tree);
84 while (node && mxmlGetType(node) != MXML_ELEMENT)
85 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
87 ret = configurationsTag(node);
89 node = mxmlGetFirstChild(node);
91 if (mxmlGetType(node) != MXML_ELEMENT) {
92 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
95 configurationTag(node);
96 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
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) {
109 logg->logError(__FILE__, __LINE__, "Invalid required attribute in configuration.xml:\n counter=\"%s\"\n event=%d\n", counter.getType(), counter.getEvent());
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) {
119 logg->logError(__FILE__, __LINE__, "Duplicate performance counter type in configuration.xml: %s", counter.getType());
128 #define CONFIGURATION_REVISION 3
129 int ConfigurationXML::configurationsTag(mxml_node_t *node) {
130 const char* revision_string;
132 revision_string = mxmlElementGetAttr(node, ATTR_REVISION);
133 if (!revision_string) {
134 return 1; //revision issue;
137 int revision = strtol(revision_string, NULL, 10);
138 if (revision < CONFIGURATION_REVISION) {
139 return 1; // revision issue
142 // A revision >= CONFIGURATION_REVISION is okay
143 // Greater than can occur when Streamline is newer than gator
148 void ConfigurationXML::configurationTag(mxml_node_t *node) {
149 // handle all other performance counters
150 if (mIndex >= MAX_PERFORMANCE_COUNTERS) {
152 gSessionData->mCounterOverflow = mIndex;
157 Counter & counter = gSessionData->mCounters[mIndex];
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));
162 if (mxmlElementGetAttr(node, ATTR_CORES)) counter.setCores(strtol(mxmlElementGetAttr(node, ATTR_CORES), NULL, 10));
163 if (counter.getCount() > 0) {
164 gSessionData->mIsEBS = true;
166 counter.setEnabled(true);
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) {
172 logg->logError(__FILE__, __LINE__, "More than one driver has claimed %s:%i", counter.getType(), counter.getEvent());
175 counter.setDriver(driver);
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);
185 if (counter.isEnabled()) {
186 // update counter index
191 void ConfigurationXML::getDefaultConfigurationXml(const char * & xml, unsigned int & len) {
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;
197 void ConfigurationXML::getPath(char* path) {
198 if (gSessionData->mConfigurationXMLPath) {
199 strncpy(path, gSessionData->mConfigurationXMLPath, PATH_MAX);
201 if (util->getApplicationFullPath(path, PATH_MAX) != 0) {
202 logg->logMessage("Unable to determine the full path of gatord, the cwd will be used");
204 strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1);
208 void ConfigurationXML::remove() {
212 if (::remove(path) != 0) {
213 logg->logError(__FILE__, __LINE__, "Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
216 logg->logMessage("Invalid configuration.xml file detected and removed");