MSRSensorGroup.cpp 8.23 KB
Newer Older
1
2
3
//================================================================================
// Name        : MSRSensorGroup.cpp
// Author      : Carla Guillen
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright   : Leibniz Supercomputing Centre
// Description : Source file for MSR sensor group class.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2019-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include "MSRSensorGroup.h"

#include <boost/log/core/record.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include <boost/parameter/keyword.hpp>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <atomic>
#include <exception>
#include <utility>

43
#include "logging.h"
44
45
46
47
#include "timestamp.h"
#include "Types.h"
#include <sstream>
#include <iomanip>
48
#include <thread>
49

50
51
52
const uint64_t MSR_MAXIMUM_SIZE = 281474976710656; //2^48
MSRSensorGroup::MSRSensorGroup(const std::string& name) : SensorGroupTemplate(name), _htAggregation(0) {
	_total_number_cpus = std::thread::hardware_concurrency();
53
54
55
56
57
}

MSRSensorGroup::~MSRSensorGroup() {
}

58
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
void MSRSensorGroup::groupInBins(){
	for(auto s : _sensors) {
		_sensorBins[s->getCpu()].addMsrSensorBin(s);
	}
	//sanity check: all bins should have the same number of sensors
	if (_sensorBins.size() == 0) {
	    LOG(error) << "Sensorgroup " << _groupName << " failed to sort sensors!";
	    return;
	}
	size_t binSensorSize = _sensorBins.front().sensors.size();
	for (auto& b : _sensorBins) {
	    if (b.sensors.size() != binSensorSize) {
	        LOG(error) << "Sensorgroup " << _groupName << " sensor number missmatch!";
            return;
	    }
	}

	//sort bins, so that the sensor ordering is equal in every bin (useful in case of hyper-threading aggregation
	for (auto& b : _sensorBins) {
        std::sort(b.sensors.begin(), b.sensors.end(), [](const S_Ptr& lhs, const S_Ptr& rhs)
            {
                   return lhs->getMetric() < rhs->getMetric();
            });
	}

	_sensorBins.shrink_to_fit();
}

86
bool MSRSensorGroup::execOnStart() {
87
88
89
90
91
92
93
	for (unsigned int cpu = 0; cpu < _sensorBins.size(); ++cpu) {
		if(!_sensorBins[cpu].isActive()){
			continue;
		}
		const std::size_t BUF_LEN=200;
                char path[BUF_LEN];
		snprintf(path, BUF_LEN, "/dev/cpu/%d/msr", cpu);
94
95
		int handle = open(path, O_RDWR);
		if (handle < 0) {   // try msr_safe
96
			snprintf(path, BUF_LEN, "/dev/cpu/%d/msr_safe", cpu);
97
98
99
100
101
102
			handle = open(path, O_RDWR);
		}
		if (handle < 0){
			LOG(error) << "Can't open msr device " << path;
			continue;
		}
103
		_sensorBins[cpu].setFd(handle);
104
105
	}

lu43jih's avatar
lu43jih committed
106
107
	program_fixed();

108
	return true;
109
110
}

111
void MSRSensorGroup::execOnStop() {
lu43jih's avatar
lu43jih committed
112
    //close file descriptors and leave counters running freely
113
114
115
116
117
    for (unsigned int cpu=0; cpu < _sensorBins.size(); ++cpu) {
    	if(_sensorBins[cpu].isActive()){
    		close(_sensorBins[cpu].getFd());
    		_sensorBins[cpu].setFd(-1);
    	}
Micha Mueller's avatar
Micha Mueller committed
118
    }
119
120
121
122
123
124
125
126
}

void MSRSensorGroup::read() {
	ureading_t reading;
	reading.timestamp = getTimestamp();

	try {
		for(auto s : _sensors) {
lu43jih's avatar
lu43jih committed
127
128
			auto ret_val = msr_read(s->getMetric(), &reading.value, s->getCpu());
			if(ret_val != -1){
129
				s->storeReading(reading, 1, MSR_MAXIMUM_SIZE, !_htAggregation); //1 is no correction...
130
#ifdef DEBUG
lu43jih's avatar
lu43jih committed
131
				LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
132
#endif
lu43jih's avatar
lu43jih committed
133
			}
134
		}
135
136
137
138
139
140
141

		if(_htAggregation){
			for(unsigned int cpu = 0; cpu < _htAggregation; ++cpu) {
				for(unsigned int m = 0; m < _sensorBins[cpu].sensors.size(); ++m){
					reading_t aggregation;
					aggregation.value = 0;
					aggregation.timestamp = reading.timestamp;
142
					unsigned int mincpu = _total_number_cpus;
143
					for(unsigned int agg = cpu; agg < _total_number_cpus; agg+=_htAggregation){
144
145
146
147
148
149
150
151
152
						if(_sensorBins[agg].isActive()){
							aggregation.value += _sensorBins[agg].sensors[m]->getLatestValue().value;
							if(agg < mincpu){
								mincpu = agg;
							}
						}
					}
					if(mincpu != _total_number_cpus){
						_sensorBins[mincpu].sensors[m]->storeReadingGlobal(aggregation);//, 1, MSR_MAXIMUM_SIZE, _htAggregation);
153
154
155
156
157
					}
				}
			}
		}

158
	} catch (const std::exception& e) {
159
		LOG(error) << "Sensorgroup " << _groupName << " could not read value: " << e.what();
160
161
162
163
	}
}

int32_t MSRSensorGroup::msr_read(uint64_t msr_number, uint64_t * value, unsigned int cpu){
164
	return pread(_sensorBins[cpu].getFd(), (void *) value, sizeof(uint64_t), msr_number);
165
166
167
}

int32_t MSRSensorGroup::msr_write(uint64_t msr_number, uint64_t value, unsigned int cpu){
168
	return pwrite(_sensorBins[cpu].getFd(), (const void *) &value, sizeof(uint64_t), msr_number);
169
170
}

Micha Mueller's avatar
Micha Mueller committed
171
172
173
174
175
176
/**
 * Program the fixed MSR as required for this plugin.
 *
 * @return  True if counters programmed successfully, false otherwise, e.g.
 *          because the counters are already in use.
 */
177
void MSRSensorGroup::program_fixed(){
Micha Mueller's avatar
Micha Mueller committed
178

179
180
181
182
	for (unsigned int cpu=0; cpu < _sensorBins.size(); ++cpu) {
		if(!_sensorBins[cpu].isActive()){ // CPU is not active, so it won't be programmed
			continue;
		}
183
184
		// program core counters

Micha Mueller's avatar
Micha Mueller committed
185
186
	    //we do not want to interrupt other services already doing measurements with MSRs
        //therefore check if any fixed counter is currently enabled
187
		struct FixedEventControlRegister ctrl_reg;
Micha Mueller's avatar
Micha Mueller committed
188

189
		msr_read(IA32_CR_FIXED_CTR_CTRL, &ctrl_reg.value, cpu);
190
191
192
193
194
		//are they all enabled?
		if (ctrl_reg.fields.os0 && ctrl_reg.fields.usr0 && ctrl_reg.fields.os1
						&& ctrl_reg.fields.usr1 && ctrl_reg.fields.os2
						&& ctrl_reg.fields.usr2) {
			//yes! Free running counters were set by someone else => we don't need to program them, just read them.
195
			LOG(debug) << "CPU" << cpu << " has free running counter, so there will be no fixed counter programming";
196
			continue;
Micha Mueller's avatar
Micha Mueller committed
197
		}
198
		//not all of them (or none) are enabled => we program them again
Micha Mueller's avatar
Micha Mueller committed
199
200

		// disable counters while programming
201
        msr_write(IA32_CR_PERF_GLOBAL_CTRL, 0, cpu);
Micha Mueller's avatar
Micha Mueller committed
202

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
		ctrl_reg.fields.os0 = 1;
		ctrl_reg.fields.usr0 = 1;
		ctrl_reg.fields.any_thread0 = 0;
		ctrl_reg.fields.enable_pmi0 = 0;

		ctrl_reg.fields.os1 = 1;
		ctrl_reg.fields.usr1 = 1;
		ctrl_reg.fields.any_thread1 = 0;
		ctrl_reg.fields.enable_pmi1 = 0;

		ctrl_reg.fields.os2 = 1;
		ctrl_reg.fields.usr2 = 1;
		ctrl_reg.fields.any_thread2 = 0;
		ctrl_reg.fields.enable_pmi2 = 0;

		ctrl_reg.fields.reserved1 = 0;
219

220
		// program them
221
		msr_write(IA32_CR_FIXED_CTR_CTRL, ctrl_reg.value, cpu);
222

223
		// start counting, enable 3 fixed counters (enable also the programmables counters)
lu43jih's avatar
lu43jih committed
224
		uint64_t value = (1ULL << 0) + (1ULL << 1) + (1ULL << 2) + (1ULL << 3) + (1ULL << 32) + (1ULL << 33) + (1ULL << 34);
225
		//uint64_t value = (1ULL << 32) + (1ULL << 33) + (1ULL << 34);
226
		msr_write(IA32_CR_PERF_GLOBAL_CTRL, value, cpu);
227
228
229
230
	}
}

void MSRSensorGroup::addCpu(unsigned int cpu){
231
232
233
234
	if(cpu + 1 > _sensorBins.size()){
		_sensorBins.resize(cpu + 1);
	}
	_sensorBins[cpu].setActive();
235
}
236

237
238
std::vector<unsigned> MSRSensorGroup::getCpus() {
  std::vector<unsigned> cpus;
239
240
241
242
  for(unsigned int cpu=0; cpu < _sensorBins.size(); ++cpu) {
	  if(_sensorBins[cpu].isActive()){
		  cpus.push_back(cpu);
	  }
243
244
245
246
  }
  return cpus;
}

247
void MSRSensorGroup::printGroupConfig(LOG_LEVEL ll) {
248
249
    std::stringstream ss;
    const char* separator = "";
250
251
252
253
254
    for (unsigned int cpu=0; cpu < _sensorBins.size(); ++cpu) {
    	if(_sensorBins[cpu].isActive()){
    		ss << separator << cpu;
    		separator = ", ";
    	}
255
256
    }

257
    LOG_VAR(ll) << "            CPUs:  " << ss.str();
258
}