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