365d422ddb96dd97f5e592f0256d22050b28d8dc
1 /**
2 * Copyright (C) ARM Limited 2010-2011. 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 */
9 typedef unsigned long long uint64_t;
10 typedef long long int64_t;
11 #include <string.h>
12 #include <stdlib.h>
13 #include <dirent.h>
14 #include "ConfigurationXML.h"
15 #include "Logging.h"
16 #include "Collector.h"
17 #include "OlyUtility.h"
18 #include "SessionData.h"
20 extern void handleException();
21 extern Collector* collector;
23 static const char* ATTR_COUNTER = "counter";
24 static const char* ATTR_VERSION = "version";
25 static const char* ATTR_TITLE = "title";
26 static const char* ATTR_NAME = "name";
27 static const char* ATTR_EVENT = "event";
28 static const char* ATTR_COLOR = "color";
29 static const char* ATTR_COUNT = "count";
30 static const char* ATTR_OPERATION = "operation";
31 static const char* ATTR_PER_CPU = "per_cpu";
32 static const char* ATTR_DESCRIPTION = "description";
33 static const char* ATTR_EBS = "event_based_sampling";
35 ConfigurationXML::ConfigurationXML() {
36 #include "configuration_xml.h"
37 char path[PATH_MAX];
38 index = 0;
40 if (util->getApplicationFullPath(path, sizeof(path)) != 0) {
41 logg->logMessage("Unable to determine the full path of gatord, the cwd will be used");
42 }
43 strcat(path, "configuration.xml");
44 mConfigurationXML = util->readFromDisk(path);
46 if (mConfigurationXML == NULL) {
47 logg->logMessage("Unable to locate configuration.xml, using default in binary");
48 // null-terminate configuration_xml
49 mConfigurationXML = (char*)malloc(configuration_xml_len + 1);
50 memcpy(mConfigurationXML, (const void*)configuration_xml, configuration_xml_len);
51 mConfigurationXML[configuration_xml_len] = 0;
52 }
54 gSessionData.initializeCounters();
56 int ret = parse(mConfigurationXML);
57 if (ret == 1) {
58 // remove configuration.xml on disk to use the default
59 if (remove(path) != 0) {
60 logg->logError(__FILE__, __LINE__, "Invalid configuration.xml file detected and unable to delete it. To resolve, delete configuration.xml on disk");
61 handleException();
62 }
63 } else if (ret < 0 || isValid() == false) {
64 logg->logError(__FILE__, __LINE__, "Parsing of the configuration.xml file failed. Please verify configuration.xml on the target filesystem is valid or delete it to use the default.");
65 handleException();
66 }
68 collector->enablePerfCounters();
69 }
71 ConfigurationXML::~ConfigurationXML() {
72 if (mConfigurationXML) {
73 free((void*)mConfigurationXML);
74 }
75 }
77 int ConfigurationXML::parse(const char* configurationXML) {
78 int ret = 0;
79 XMLReader reader(configurationXML);
80 char * tag = reader.nextTag();
81 while(tag != 0 && ret == 0) {
82 if (strcmp(tag, "configurations") == 0) {
83 ret = configurationsTag(&reader);
84 } else if (strcmp(tag, "configuration") == 0) {
85 ret = configurationTag(&reader);
86 }
87 tag = reader.nextTag();
88 }
90 return ret;
91 }
93 bool ConfigurationXML::isValid(void) {
94 for (int i = 0; i < MAX_PERFORMANCE_COUNTERS; i++) {
95 if (gSessionData.mPerfCounterEnabled[i]) {
96 if (strcmp(gSessionData.mPerfCounterType[i], "") == 0 ||
97 strcmp(gSessionData.mPerfCounterTitle[i], "") == 0 ||
98 strcmp(gSessionData.mPerfCounterName[i], "") == 0) {
99 logg->logMessage("Invalid required attribute\n counter=\"%s\"\n title=\"%s\"\n name=\"%s\"\n event=%d\n", gSessionData.mPerfCounterType[i], gSessionData.mPerfCounterTitle[i], gSessionData.mPerfCounterName[i], gSessionData.mPerfCounterEvent[i]);
100 return false; // failure
101 }
103 // iterate through the remaining enabled performance counters
104 for (int j = i + 1; j < MAX_PERFORMANCE_COUNTERS; j++) {
105 if (gSessionData.mPerfCounterEnabled[j]) {
106 // check if the type or device are the same
107 if (strcmp(gSessionData.mPerfCounterType[i], gSessionData.mPerfCounterType[j]) == 0) {
108 logg->logMessage("Duplicate performance counter type: %s", gSessionData.mPerfCounterType[i]);
109 return false; // failure
110 }
111 }
112 }
113 }
114 }
116 return true; // success
117 }
119 #define CONFIGURATION_VERSION 1
120 int ConfigurationXML::configurationsTag(XMLReader *in) {
121 int version = in->getAttributeAsInteger(ATTR_VERSION, 0);
122 if (version != CONFIGURATION_VERSION) {
123 logg->logMessage("Incompatible configuration.xml version (%d) detected. The version needs to be %d.", version, CONFIGURATION_VERSION);
124 return 1; // version issue
125 }
126 return 0;
127 }
129 int ConfigurationXML::configurationTag(XMLReader* in) {
130 // handle all other performance counters
131 if (index >= MAX_PERFORMANCE_COUNTERS) {
132 logg->logMessage("Invalid performance counter index: %d", index);
133 return -1; // failure
134 }
136 // read attributes
137 in->getAttribute(ATTR_COUNTER, gSessionData.mPerfCounterType[index], sizeof(gSessionData.mPerfCounterType[index]), "");
138 in->getAttribute(ATTR_TITLE, gSessionData.mPerfCounterTitle[index], sizeof(gSessionData.mPerfCounterTitle[index]), "");
139 in->getAttribute(ATTR_NAME, gSessionData.mPerfCounterName[index], sizeof(gSessionData.mPerfCounterName[index]), "");
140 in->getAttribute(ATTR_DESCRIPTION, gSessionData.mPerfCounterDescription[index], sizeof(gSessionData.mPerfCounterDescription[index]), "");
141 gSessionData.mPerfCounterEvent[index] = in->getAttributeAsInteger(ATTR_EVENT, 0);
142 gSessionData.mPerfCounterCount[index] = in->getAttributeAsInteger(ATTR_COUNT, 0);
143 gSessionData.mPerfCounterColor[index] = in->getAttributeAsInteger(ATTR_COLOR, 0);
144 gSessionData.mPerfCounterPerCPU[index] = in->getAttributeAsBoolean(ATTR_PER_CPU, false);
145 gSessionData.mPerfCounterEBSCapable[index] = in->getAttributeAsBoolean(ATTR_EBS, false);
146 in->getAttribute(ATTR_OPERATION, gSessionData.mPerfCounterOperation[index], sizeof(gSessionData.mPerfCounterOperation[index]), "");
147 gSessionData.mPerfCounterEnabled[index] = true;
149 // update counter index
150 index++;
152 return 0; // success
153 }