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