MSRSensorGroup.cpp 8.06 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
142
143
144
145
146
147
148
149

		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;
					for(unsigned int agg = cpu; agg < _total_number_cpus; agg+=_htAggregation){
						aggregation.value += _sensorBins[agg].isActive() ? _sensorBins[agg].sensors[m]->getLatestValue().value : 0;
					}
					_sensorBins[cpu].sensors[m]->storeReadingGlobal(aggregation);//, 1, MSR_MAXIMUM_SIZE, _htAggregation);
				}
			}
		}

150
	} catch (const std::exception& e) {
151
		LOG(error) << "Sensorgroup " << _groupName << " could not read value: " << e.what();
152
153
154
155
	}
}

int32_t MSRSensorGroup::msr_read(uint64_t msr_number, uint64_t * value, unsigned int cpu){
156
	return pread(_sensorBins[cpu].getFd(), (void *) value, sizeof(uint64_t), msr_number);
157
158
159
}

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

Micha Mueller's avatar
Micha Mueller committed
163
164
165
166
167
168
/**
 * 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.
 */
169
void MSRSensorGroup::program_fixed(){
Micha Mueller's avatar
Micha Mueller committed
170

171
172
173
174
	for (unsigned int cpu=0; cpu < _sensorBins.size(); ++cpu) {
		if(!_sensorBins[cpu].isActive()){ // CPU is not active, so it won't be programmed
			continue;
		}
175
176
		// program core counters

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

181
		msr_read(IA32_CR_FIXED_CTR_CTRL, &ctrl_reg.value, cpu);
182
183
184
185
186
		//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.
187
			LOG(debug) << "CPU" << cpu << " has free running counter, so there will be no fixed counter programming";
188
			continue;
Micha Mueller's avatar
Micha Mueller committed
189
		}
190
		//not all of them (or none) are enabled => we program them again
Micha Mueller's avatar
Micha Mueller committed
191
192

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

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
		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;
211

212
		// program them
213
		msr_write(IA32_CR_FIXED_CTR_CTRL, ctrl_reg.value, cpu);
214

215
		// start counting, enable 3 fixed counters (enable also the programmables counters)
lu43jih's avatar
lu43jih committed
216
		uint64_t value = (1ULL << 0) + (1ULL << 1) + (1ULL << 2) + (1ULL << 3) + (1ULL << 32) + (1ULL << 33) + (1ULL << 34);
217
		//uint64_t value = (1ULL << 32) + (1ULL << 33) + (1ULL << 34);
218
		msr_write(IA32_CR_PERF_GLOBAL_CTRL, value, cpu);
219
220
221
222
	}
}

void MSRSensorGroup::addCpu(unsigned int cpu){
223
224
225
226
	if(cpu + 1 > _sensorBins.size()){
		_sensorBins.resize(cpu + 1);
	}
	_sensorBins[cpu].setActive();
227
}
228

229
230
std::vector<unsigned> MSRSensorGroup::getCpus() {
  std::vector<unsigned> cpus;
231
232
233
234
  for(unsigned int cpu=0; cpu < _sensorBins.size(); ++cpu) {
	  if(_sensorBins[cpu].isActive()){
		  cpus.push_back(cpu);
	  }
235
236
237
238
  }
  return cpus;
}

239
void MSRSensorGroup::printGroupConfig(LOG_LEVEL ll) {
240
241
    std::stringstream ss;
    const char* separator = "";
242
243
244
245
246
    for (unsigned int cpu=0; cpu < _sensorBins.size(); ++cpu) {
    	if(_sensorBins[cpu].isActive()){
    		ss << separator << cpu;
    		separator = ", ";
    	}
247
248
    }

249
    LOG_VAR(ll) << "            CPUs:  " << ss.str();
250
}