blob: be224a4f2b1f7d453a5704d06e28b23ace61eb25 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
Jon Medhurstb1d07442015-05-08 12:04:18 +01002 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
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 Medhurst15ce78d2014-04-10 09:02:02 +01009#include "ConfigurationXML.h"
10
Jon Medhurstaaf37a32013-06-11 12:10:56 +010011#include <string.h>
12#include <stdlib.h>
13#include <dirent.h>
Jon Medhurst15ce78d2014-04-10 09:02:02 +010014
Jon Medhurstaaf37a32013-06-11 12:10:56 +010015#include "Driver.h"
16#include "Logging.h"
17#include "OlyUtility.h"
18#include "SessionData.h"
19
20static const char* ATTR_COUNTER = "counter";
21static const char* ATTR_REVISION = "revision";
22static const char* ATTR_EVENT = "event";
23static const char* ATTR_COUNT = "count";
Jon Medhurste31266f2014-08-04 15:47:44 +010024static const char* ATTR_CORES = "cores";
Jon Medhurstaaf37a32013-06-11 12:10:56 +010025
26ConfigurationXML::ConfigurationXML() {
27 const char * configuration_xml;
28 unsigned int configuration_xml_len;
29 getDefaultConfigurationXml(configuration_xml, configuration_xml_len);
Jon Medhurste31266f2014-08-04 15:47:44 +010030
Jon Medhurstaaf37a32013-06-11 12:10:56 +010031 char path[PATH_MAX];
32
Jon Medhurstd3698592013-10-10 16:48:56 +010033 getPath(path);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010034 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 Medhurstd3698592013-10-10 16:48:56 +010047 remove();
Jon Medhurstaaf37a32013-06-11 12:10:56 +010048
49 // Free the current configuration and reload
50 free((void*)mConfigurationXML);
51 mConfigurationXML = NULL;
52 continue;
53 }
54
55 break;
56 }
Jon Medhurste31266f2014-08-04 15:47:44 +010057
Jon Medhurstaaf37a32013-06-11 12:10:56 +010058 validate();
59}
60
61ConfigurationXML::~ConfigurationXML() {
62 if (mConfigurationXML) {
63 free((void*)mConfigurationXML);
64 }
65}
66
67int ConfigurationXML::parse(const char* configurationXML) {
68 mxml_node_t *tree, *node;
69 int ret;
70
71 // clear counter overflow
72 gSessionData->mCounterOverflow = 0;
Jon Medhurst15ce78d2014-04-10 09:02:02 +010073 gSessionData->mIsEBS = false;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010074 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 Medhurste31266f2014-08-04 15:47:44 +010086
Jon Medhurstaaf37a32013-06-11 12:10:56 +010087 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
104void 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 Medhurstb1d07442015-05-08 12:04:18 +0100109 logg->logError("Invalid required attribute in configuration.xml:\n counter=\"%s\"\n event=%d\n", counter.getType(), counter.getEvent());
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100110 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 Medhurstb1d07442015-05-08 12:04:18 +0100119 logg->logError("Duplicate performance counter type in configuration.xml: %s", counter.getType());
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100120 handleException();
121 }
122 }
123 }
124 }
125 }
126}
127
128#define CONFIGURATION_REVISION 3
129int ConfigurationXML::configurationsTag(mxml_node_t *node) {
130 const char* revision_string;
Jon Medhurste31266f2014-08-04 15:47:44 +0100131
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100132 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
148void 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 Medhurste31266f2014-08-04 15:47:44 +0100162 if (mxmlElementGetAttr(node, ATTR_CORES)) counter.setCores(strtol(mxmlElementGetAttr(node, ATTR_CORES), NULL, 10));
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100163 if (counter.getCount() > 0) {
164 gSessionData->mIsEBS = true;
165 }
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100166 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 Medhurstb1d07442015-05-08 12:04:18 +0100172 logg->logError("More than one driver has claimed %s:%i", counter.getType(), counter.getEvent());
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100173 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
191void ConfigurationXML::getDefaultConfigurationXml(const char * & xml, unsigned int & len) {
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100192#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 Medhurstaaf37a32013-06-11 12:10:56 +0100195}
Jon Medhurstd3698592013-10-10 16:48:56 +0100196
197void 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
208void ConfigurationXML::remove() {
209 char path[PATH_MAX];
210 getPath(path);
211
212 if (::remove(path) != 0) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100213 logg->logError("Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
Jon Medhurstd3698592013-10-10 16:48:56 +0100214 handleException();
215 }
216 logg->logMessage("Invalid configuration.xml file detected and removed");
217}