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