tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / tools / gator / daemon / ConfigurationXML.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-2014. 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         const char * configuration_xml;
28         unsigned int configuration_xml_len;
29         getDefaultConfigurationXml(configuration_xml, configuration_xml_len);
30
31         char path[PATH_MAX];
32
33         getPath(path);
34         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) {
47                         remove();
48
49                         // Free the current configuration and reload
50                         free((void*)mConfigurationXML);
51                         mConfigurationXML = NULL;
52                         continue;
53                 }
54
55                 break;
56         }
57
58         validate();
59 }
60
61 ConfigurationXML::~ConfigurationXML() {
62         if (mConfigurationXML) {
63                 free((void*)mConfigurationXML);
64         }
65 }
66
67 int ConfigurationXML::parse(const char* configurationXML) {
68         mxml_node_t *tree, *node;
69         int ret;
70
71         // clear counter overflow
72         gSessionData->mCounterOverflow = 0;
73         gSessionData->mIsEBS = false;
74         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);
86
87         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
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());
110                                 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) {
119                                                 logg->logError(__FILE__, __LINE__, "Duplicate performance counter type in configuration.xml: %s", counter.getType());
120                                                 handleException();
121                                         }
122                                 }
123                         }
124                 }
125         }
126 }
127
128 #define CONFIGURATION_REVISION 3
129 int ConfigurationXML::configurationsTag(mxml_node_t *node) {
130         const char* revision_string;
131
132         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
148 void 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));
162         if (mxmlElementGetAttr(node, ATTR_CORES)) counter.setCores(strtol(mxmlElementGetAttr(node, ATTR_CORES), NULL, 10));
163         if (counter.getCount() > 0) {
164                 gSessionData->mIsEBS = true;
165         }
166         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) {
172                                 logg->logError(__FILE__, __LINE__, "More than one driver has claimed %s:%i", counter.getType(), counter.getEvent());
173                                 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
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;
195 }
196
197 void 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
208 void ConfigurationXML::remove() {
209         char path[PATH_MAX];
210         getPath(path);
211
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");
214                 handleException();
215         }
216         logg->logMessage("Invalid configuration.xml file detected and removed");
217 }