PerfeventConfigurator.cpp 9.41 KB
Newer Older
1
/*
2
 * PerfeventConfigurator.cpp
3
4
5
6
7
 *
 *  Created on: 13.12.2017
 *      Author: Micha Mueller
 */

8
#include "PerfeventConfigurator.h"
9

10
#include <iostream>
11
12
13
14
#include <sstream>
#include <unistd.h>
#include <iomanip>
#include <sys/sysinfo.h>
15
16
17
18
#include <linux/perf_event.h>

using namespace std;

19
PerfeventConfigurator::PerfeventConfigurator() {
20
21
22
	_groupName = "group";
	_baseName = "counter";

23
	//set up enum-maps to map string from cfgFile to an enum value defined in linux/perf_event.h
24
25
26
27
28
29
	_enumType["PERF_TYPE_HARDWARE"] 	= PERF_TYPE_HARDWARE;
	_enumType["PERF_TYPE_SOFTWARE"] 	= PERF_TYPE_SOFTWARE;
	_enumType["PERF_TYPE_TRACEPOINT"] 	= PERF_TYPE_TRACEPOINT;
	_enumType["PERF_TYPE_HW_CACHE"] 	= PERF_TYPE_HW_CACHE;
	_enumType["PERF_TYPE_RAW"] 			= PERF_TYPE_RAW;
	_enumType["PERF_TYPE_BREAKPOINT"] 	= PERF_TYPE_BREAKPOINT;
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	//if type==PERF_TYPE_HARDWARE
	_enumConfig["PERF_COUNT_HW_CPU_CYCLES"] 		 = PERF_COUNT_HW_CPU_CYCLES;
	_enumConfig["PERF_COUNT_HW_INSTRUCTIONS"] 		 = PERF_COUNT_HW_INSTRUCTIONS;
	_enumConfig["PERF_COUNT_HW_CACHE_REFERENCES"] 	 = PERF_COUNT_HW_CACHE_REFERENCES;
	_enumConfig["PERF_COUNT_HW_CACHE_MISSES"] 		 = PERF_COUNT_HW_CACHE_MISSES;
	_enumConfig["PERF_COUNT_HW_BRANCH_INSTRUCTIONS"] = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
	_enumConfig["PERF_COUNT_HW_BRANCH_MISSES"] 		 = PERF_COUNT_HW_BRANCH_MISSES;
	_enumConfig["PERF_COUNT_HW_BUS_CYCLES"] 			 = PERF_COUNT_HW_BUS_CYCLES;
	_enumConfig["PERF_COUNT_HW_STALLED_CYCLES_FRONTEND"] = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND;
	_enumConfig["PERF_COUNT_HW_STALLED_CYCLES_BACKEND"]	 = PERF_COUNT_HW_STALLED_CYCLES_BACKEND;
	_enumConfig["PERF_COUNT_HW_REF_CPU_CYCLES"] 		 = PERF_COUNT_HW_REF_CPU_CYCLES;

	//if type==PERF_TYPE_SOFTWARE
	_enumConfig["PERF_COUNT_SW_CPU_CLOCK"] 		  = PERF_COUNT_SW_CPU_CLOCK;
	_enumConfig["PERF_COUNT_SW_TASK_CLOCK"] 	  = PERF_COUNT_SW_TASK_CLOCK;
	_enumConfig["PERF_COUNT_SW_PAGE_FAULTS"] 	  = PERF_COUNT_SW_PAGE_FAULTS;
	_enumConfig["PERF_COUNT_SW_CONTEXT_SWITCHES"] = PERF_COUNT_SW_CONTEXT_SWITCHES;
	_enumConfig["PERF_COUNT_SW_CPU_MIGRATIONS"]   = PERF_COUNT_SW_CPU_MIGRATIONS;
	_enumConfig["PERF_COUNT_SW_PAGE_FAULTS_MIN"]  = PERF_COUNT_SW_PAGE_FAULTS_MIN;
	_enumConfig["PERF_COUNT_SW_PAGE_FAULTS_MAJ"]  = PERF_COUNT_SW_PAGE_FAULTS_MAJ;
	_enumConfig["PERF_COUNT_SW_ALIGNMENT_FAULTS"] = PERF_COUNT_SW_ALIGNMENT_FAULTS;
	_enumConfig["PERF_COUNT_SW_EMULATION_FAULTS"] = PERF_COUNT_SW_EMULATION_FAULTS;
	_enumConfig["PERF_COUNT_SW_DUMMY"] 			  = PERF_COUNT_SW_DUMMY;

	//TODO set up map for rest of config enum
56
}
57

58
PerfeventConfigurator::~PerfeventConfigurator() {}
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
void PerfeventConfigurator::sensorBase(PerfSensorBase& s, CFG_VAL config) {
	/*
	 * Custom code, as perf-event is an extra special plugin
	 */
	BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
		if (boost::iequals(val.first, "type")) {
			enumMap_t::iterator it = _enumType.find(val.second.data());
			if(it != _enumType.end()) {
				s.setType(it->second);
				//LOG(debug) << "  Type:      " << val.second.data() << " (= " << s.getType() << ")";
			} else {
				LOG(warning) << "  Type \"" << val.second.data() << "\" not known.";
			}
		} else if (boost::iequals(val.first, "config")) {
			if (s.getType() == PERF_TYPE_BREAKPOINT) {
				//leave config zero
			} else if (s.getType() == PERF_TYPE_RAW) {
				//read in custom hex-value
				unsigned long config = stoul(val.second.data(), 0, 16);
				s.setConfig(config);
				LOG(debug) << "  Config:    Raw value: " << s.getConfig();
			} else {
				enumMap_t::iterator it = _enumConfig.find(val.second.data());
				if(it != _enumConfig.end()) {
					s.setConfig(it->second);
					//LOG(debug) << "  Config:    " << val.second.data() << " (= " << s.getConfig() << ")";
				} else {
					LOG(warning) << "  Config \"" << val.second.data() << "\" not known.";
88
				}
89
90
91
			}
		}
	}
92
}
93

94
95
96
97
98
void PerfeventConfigurator::sensorGroup(PerfSensorGroup& s, CFG_VAL config) {
	ADD {
		//no group attributes currently
	}
}
99

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
bool PerfeventConfigurator::readConfig(std::string cfgPath) {
	/*
	 * Custom code, as perf-event is an extra special plugin
	 */
	_cfgPath = cfgPath;

	boost::property_tree::iptree cfg;
	boost::property_tree::read_info(cfgPath, cfg);

	//read global variables (if present overwrite those from global.conf)
	readGlobal(cfg);

	//read groups and templates for groups
	BOOST_FOREACH(boost::property_tree::iptree::value_type &val, cfg) {
		if (boost::iequals(val.first, "template_" + _groupName)) {
			LOG(debug) << "Template " << _groupName << " \"" << val.second.data() << "\"";
			if (!val.second.empty()) {
				PerfSensorGroup* group = new PerfSensorGroup(val.second.data());
				if (readSensorGroup(*group, val.second)) {
					//check if cpus-list is given for this template group
					boost::optional<boost::property_tree::iptree&> cpus = val.second.get_child_optional("cpus");
					if(cpus) {
						LOG(debug) << "Reading CPUs for \"" << val.second.data() << "\"";
						std::set<int> cpuVec = parseCpuString(cpus.get().data());
						_templateCpus.insert(templateCpuMap_t::value_type(val.second.data(), cpuVec));
125
					}
126

127
128
129
					auto ret = _templateSensorGroups.insert(std::pair<std::string, PerfSensorGroup*>(val.second.data(), group));
					if(!ret.second) {
						LOG(warning) << "Template " << _groupName << " " << val.second.data() << " already exists! Omitting...";
130
					}
131
				} else {
132
					LOG(warning) << "Template " << _groupName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
133
				}
134
			}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
		} else if (boost::iequals(val.first, _groupName)) {
			LOG(debug) << _groupName << " \"" << val.second.data() << "\"";
			if (!val.second.empty()) {
				PerfSensorGroup group(val.second.data());
				if (readSensorGroup(group, val.second)) {
					//now for the cpus...
					//initialize set with cpuIDs
					//default cpuSet: contains all cpuIDs
					std::set<int> cpuSet;
					for (int i = 0; i < get_nprocs(); i++) {
						cpuSet.insert(i);
					}
					//check if (differing) cpus-list is given; if so, overwrite default cpuVec
					boost::optional<boost::property_tree::iptree&> cpus = val.second.get_child_optional("cpus");
					if (cpus) { //cpu list given
						cpuSet = parseCpuString(cpus.get().data());
					} else { //cpu list not given, but perhaps template counter has one
						boost::optional<boost::property_tree::iptree&> def = val.second.get_child_optional("default");
						if (def) {
							templateCpuMap_t::iterator itC = _templateCpus.find(def.get().data());
							if(itC != _templateCpus.end()) {
								cpuSet = itC->second;
							}
						}
					}
160

161
162
163
164
					if (group.getMqttPart().size() == 0) {
						LOG(warning) << _groupName << " \"" << val.second.data() << "\" has no mqttPart entry set. This is required as a place holder for the CPU id!";
					}
					
165
					//customize perfCounterGroup for every CPU
166
167
168
169
170
					if(!cpuSet.empty()) {
						//first create groupLeader
						std::set<int>::iterator it = cpuSet.begin();
						PerfSensorGroup* leaderSG = new PerfSensorGroup(group);
						leaderSG->setSensorGroupLeader(true);
171
						std::stringstream mqttPart;
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
						mqttPart << std::setfill ('0') << std::setw(group.getMqttPart().size()) << std::hex << *it << "/";

						leaderSG->setGroupName(leaderSG->getGroupName() + std::to_string(*it));
						leaderSG->setCpuId(*it);
						leaderSG->setMqttPart(mqttPart.str());
						storeSensorGroup(leaderSG);
						it++;

						//create fellow groups
						for (; it != cpuSet.end(); ++it) {
							PerfSensorGroup* perfSG = new PerfSensorGroup(group);
							std::stringstream mqttPart;
							mqttPart << std::setfill ('0') << std::setw(group.getMqttPart().size()) << std::hex << *it << "/";

							perfSG->setGroupName(perfSG->getGroupName() + std::to_string(*it));
							perfSG->setCpuId(*it);
							perfSG->setMqttPart(mqttPart.str());
							storeSensorGroup(perfSG);
							leaderSG->pushBackGroup(perfSG);
						}
192
					}
193
194
				} else {
					LOG(warning) << _groupName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
195
196
197
198
				}
			}
		}
	}
199
200
201
202
	//read of config finished. Now we build the mqtt-topic for every sensor
	for(auto g : _sensorGroups) {
		for(auto s : g->getSensors()) {
			s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt());
203
		}
204
	}
205
206
	//we do not need them anymore
	_templateCpus.clear();
207
	return true;
208
}
209

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
std::set<int> PerfeventConfigurator::parseCpuString(const std::string& cpuString) {
	std::set<int> cpus;
	int maxCpu = get_nprocs();

	std::vector<std::string> subStrings;

	std::stringstream ssComma(cpuString);
	std::string item;
	while (std::getline(ssComma, item, ',')) {
		subStrings.push_back(item);
	}

	for (auto s : subStrings) {
		if (s.find('-') != std::string::npos) { //range of values (e.g. 1-5) specified
			std::stringstream ssHyphen(s);
			std::string min, max;
			std::getline(ssHyphen, min, '-');
			std::getline(ssHyphen, max);

			try {
				int minVal = stoi(min);
				int maxVal = stoi(max);

				for (int i = minVal; i <= maxVal; i++) {
					if (i >= 0 && i < maxCpu) {
						cpus.insert(i);
					}
				}
			} catch (const std::exception& e) {
239
				LOG(debug) << "Could not parse values \"" << min << "-" << max << "\"";
240
241
242
243
244
245
246
247
			}
		} else { //single value
			try {
				int val = stoi(s);
				if (val >= 0 && val < maxCpu) {
					cpus.insert(val);
				}
			} catch (const std::exception& e) {
248
				LOG(debug) << "Could not parse value \"" << s << "\"";
249
250
251
252
253
			}
		}
	}

	if (cpus.empty()) {
254
		LOG(warning) << "  CPUs could not be parsed!";
255
	} else {
256
257
		std::stringstream sstream;
		sstream << "  CPUS: ";
258
		for (auto i : cpus) {
259
			sstream << i << ", ";
260
		}
261
262
263
264
		std::string msg = sstream.str();
		msg.pop_back();
		msg.pop_back();
		LOG(debug) << msg;
265
266
267
	}
	return cpus;
}