gator: Merge gator version 5.23.1
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / tools / gator / daemon / ConfigurationXML.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-2015. 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 "ConfigurationXML.h"
10
11 #include <string.h>
12 #include <stdlib.h>
13 #include <dirent.h>
14
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";
24 static const char ATTR_CORES[]              = "cores";
25
26 ConfigurationXML::ConfigurationXML() {
27         if (gSessionData.mCountersError != NULL) {
28                 free(gSessionData.mCountersError);
29                 gSessionData.mCountersError = NULL;
30         }
31
32         const char * configuration_xml;
33         unsigned int configuration_xml_len;
34         getDefaultConfigurationXml(configuration_xml, configuration_xml_len);
35
36         char path[PATH_MAX];
37
38         getPath(path);
39         mConfigurationXML = readFromDisk(path);
40
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;
48                 }
49
50                 int ret = parse(mConfigurationXML);
51                 if (ret == 1) {
52                         remove();
53
54                         // Free the current configuration and reload
55                         free((void*)mConfigurationXML);
56                         mConfigurationXML = NULL;
57                         continue;
58                 }
59
60                 break;
61         }
62
63         validate();
64 }
65
66 ConfigurationXML::~ConfigurationXML() {
67         if (mConfigurationXML) {
68                 free((void*)mConfigurationXML);
69         }
70 }
71
72 int ConfigurationXML::parse(const char* configurationXML) {
73         mxml_node_t *tree, *node;
74         int ret;
75
76         gSessionData.mIsEBS = false;
77         mIndex = 0;
78
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);
82         }
83
84         tree = mxmlLoadString(NULL, configurationXML, MXML_NO_CALLBACK);
85
86         node = mxmlGetFirstChild(tree);
87         while (node && mxmlGetType(node) != MXML_ELEMENT)
88                 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
89
90         ret = configurationsTag(node);
91
92         node = mxmlGetFirstChild(node);
93         while (node) {
94                 if (mxmlGetType(node) != MXML_ELEMENT) {
95                         node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
96                         continue;
97                 }
98                 configurationTag(node);
99                 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
100         }
101
102         mxmlDelete(tree);
103
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");
107                         handleException();
108                 }
109         }
110         gSessionData.mCcnDriver.validateCounters();
111
112         return ret;
113 }
114
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");
122                                         handleException();
123                                 }
124                                 return;
125                         }
126
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");
135                                                         handleException();
136                                                 }
137                                                 return;
138                                         }
139                                 }
140                         }
141                 }
142         }
143 }
144
145 #define CONFIGURATION_REVISION 3
146 int ConfigurationXML::configurationsTag(mxml_node_t *node) {
147         const char* revision_string;
148
149         revision_string = mxmlElementGetAttr(node, ATTR_REVISION);
150         if (!revision_string) {
151                 return 1; //revision issue;
152         }
153
154         int revision = strtol(revision_string, NULL, 10);
155         if (revision < CONFIGURATION_REVISION) {
156                 return 1; // revision issue
157         }
158
159         // A revision >= CONFIGURATION_REVISION is okay
160         // Greater than can occur when Streamline is newer than gator
161
162         return 0;
163 }
164
165 void ConfigurationXML::configurationTag(mxml_node_t *node) {
166         // handle all other performance counters
167         if (mIndex >= MAX_PERFORMANCE_COUNTERS) {
168                 mIndex++;
169                 return;
170         }
171
172         // read attributes
173         Counter & counter = gSessionData.mCounters[mIndex];
174         counter.clear();
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;
181         }
182         counter.setEnabled(true);
183
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());
189                                 handleException();
190                         }
191                         counter.setDriver(driver);
192                 }
193         }
194
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);
199         }
200
201         if (counter.isEnabled()) {
202                 // update counter index
203                 mIndex++;
204         }
205 }
206
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;
211 }
212
213 void ConfigurationXML::getPath(char* path) {
214         if (gSessionData.mConfigurationXMLPath) {
215                 strncpy(path, gSessionData.mConfigurationXMLPath, PATH_MAX);
216         } else {
217                 if (getApplicationFullPath(path, PATH_MAX) != 0) {
218                         logg.logMessage("Unable to determine the full path of gatord, the cwd will be used");
219                 }
220                 strncat(path, "configuration.xml", PATH_MAX - strlen(path) - 1);
221         }
222 }
223
224 void ConfigurationXML::remove() {
225         char path[PATH_MAX];
226         getPath(path);
227
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");
230                 handleException();
231         }
232         logg.logMessage("Invalid configuration.xml file detected and removed");
233 }