2 * Copyright (C) ARM Limited 2010-2015. 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 if (gSessionData.mCountersError != NULL) {
28 free(gSessionData.mCountersError);
29 gSessionData.mCountersError = NULL;
32 const char * configuration_xml;
33 unsigned int configuration_xml_len;
34 getDefaultConfigurationXml(configuration_xml, configuration_xml_len);
39 mConfigurationXML = readFromDisk(path);
41 for (int retryCount = 0; retryCount < 2; ++retryCount) {
42 if (mConfigurationXML == NULL) {
43 logg.logMessage("Unable to locate configuration.xml, using default in binary");
44 // null-terminate configuration_xml
45 mConfigurationXML = (char*)malloc(configuration_xml_len + 1);
46 memcpy(mConfigurationXML, (const void*)configuration_xml, configuration_xml_len);
47 mConfigurationXML[configuration_xml_len] = 0;
50 int ret = parse(mConfigurationXML);
54 // Free the current configuration and reload
55 free((void*)mConfigurationXML);
56 mConfigurationXML = NULL;
66 ConfigurationXML::~ConfigurationXML() {
67 if (mConfigurationXML) {
68 free((void*)mConfigurationXML);
72 int ConfigurationXML::parse(const char* configurationXML) {
73 mxml_node_t *tree, *node;
76 gSessionData.mIsEBS = false;
79 // disable all counters prior to parsing the configuration xml
80 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
81 gSessionData.mCounters[i].setEnabled(false);
84 tree = mxmlLoadString(NULL, configurationXML, MXML_NO_CALLBACK);
86 node = mxmlGetFirstChild(tree);
87 while (node && mxmlGetType(node) != MXML_ELEMENT)
88 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
90 ret = configurationsTag(node);
92 node = mxmlGetFirstChild(node);
94 if (mxmlGetType(node) != MXML_ELEMENT) {
95 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
98 configurationTag(node);
99 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
104 if (gSessionData.mCountersError == NULL && mIndex > MAX_PERFORMANCE_COUNTERS) {
105 if (asprintf(&gSessionData.mCountersError, "Only %i performance counters are permitted, %i are selected", MAX_PERFORMANCE_COUNTERS, mIndex) <= 0) {
106 logg.logError("asprintf failed");
110 gSessionData.mCcnDriver.validateCounters();
115 void ConfigurationXML::validate(void) {
116 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
117 const Counter & counter = gSessionData.mCounters[i];
118 if (counter.isEnabled()) {
119 if (strcmp(counter.getType(), "") == 0) {
120 if (gSessionData.mCountersError == NULL && asprintf(&gSessionData.mCountersError, "Invalid required attribute in configuration.xml:\n counter=\"%s\"\n event=%d", counter.getType(), counter.getEvent()) <= 0) {
121 logg.logError("asprintf failed");
127 // iterate through the remaining enabled performance counters
128 for (int j = i + 1; j < MAX_PERFORMANCE_COUNTERS; j++) {
129 const Counter & counter2 = gSessionData.mCounters[j];
130 if (counter2.isEnabled()) {
131 // check if the types are the same
132 if (strcmp(counter.getType(), counter2.getType()) == 0) {
133 if (gSessionData.mCountersError == NULL && asprintf(&gSessionData.mCountersError, "Duplicate performance counter type in configuration.xml: %s", counter.getType()) <= 0) {
134 logg.logError("asprintf failed");
145 #define CONFIGURATION_REVISION 3
146 int ConfigurationXML::configurationsTag(mxml_node_t *node) {
147 const char* revision_string;
149 revision_string = mxmlElementGetAttr(node, ATTR_REVISION);
150 if (!revision_string) {
151 return 1; //revision issue;
154 int revision = strtol(revision_string, NULL, 10);
155 if (revision < CONFIGURATION_REVISION) {
156 return 1; // revision issue
159 // A revision >= CONFIGURATION_REVISION is okay
160 // Greater than can occur when Streamline is newer than gator
165 void ConfigurationXML::configurationTag(mxml_node_t *node) {
166 // handle all other performance counters
167 if (mIndex >= MAX_PERFORMANCE_COUNTERS) {
173 Counter & counter = gSessionData.mCounters[mIndex];
175 if (mxmlElementGetAttr(node, ATTR_COUNTER)) counter.setType(mxmlElementGetAttr(node, ATTR_COUNTER));
176 if (mxmlElementGetAttr(node, ATTR_EVENT)) counter.setEvent(strtol(mxmlElementGetAttr(node, ATTR_EVENT), NULL, 16));
177 if (mxmlElementGetAttr(node, ATTR_COUNT)) counter.setCount(strtol(mxmlElementGetAttr(node, ATTR_COUNT), NULL, 10));
178 if (mxmlElementGetAttr(node, ATTR_CORES)) counter.setCores(strtol(mxmlElementGetAttr(node, ATTR_CORES), NULL, 10));
179 if (counter.getCount() > 0) {
180 gSessionData.mIsEBS = true;
182 counter.setEnabled(true);
184 // Associate a driver with each counter
185 for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) {
186 if (driver->claimCounter(counter)) {
187 if (counter.getDriver() != NULL) {
188 logg.logError("More than one driver has claimed %s:%i", counter.getType(), counter.getEvent());
191 counter.setDriver(driver);
195 // If no driver is associated with the counter, disable it
196 if (counter.getDriver() == NULL) {
197 logg.logMessage("No driver has claimed %s:%i", counter.getType(), counter.getEvent());
198 counter.setEnabled(false);
201 if (counter.isEnabled()) {
202 // update counter index
207 void ConfigurationXML::getDefaultConfigurationXml(const char * & xml, unsigned int & len) {
208 #include "defaults_xml.h" // defines and initializes char defaults_xml[] and int defaults_xml_len
209 xml = (const char *)defaults_xml;
210 len = defaults_xml_len;
213 void ConfigurationXML::getPath(char* path) {
214 if (gSessionData.mConfigurationXMLPath) {
215 strncpy(path, gSessionData.mConfigurationXMLPath, PATH_MAX);
217 if (getApplicationFullPath(path, PATH_MAX) != 0) {
218 logg.logMessage("Unable to determine the full path of gatord, the cwd will be used");
220 strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1);
224 void ConfigurationXML::remove() {
228 if (::remove(path) != 0) {
229 logg.logError("Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
232 logg.logMessage("Invalid configuration.xml file detected and removed");