-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmsr.c
283 lines (227 loc) · 7.18 KB
/
msr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
56
57
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
86
87
88
89
90
91
92
93
94
95
96
97
98
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
125
126
127
128
129
130
131
132
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdio.h>
#include <math.h>
#include "msr.h"
double WRAPAROUND_VALUE;
//factor of F for time_window_limit. It represents these four value.
double F_arr[4] = {1.0, 1.1, 1.2, 1.3};
void
putBitField(uint64_t inField, uint64_t *data, uint64_t width, uint64_t offset)
{
uint64_t mask = ~0;
uint64_t bitMask;
/*The bits to be overwritten are located in the leftmost part.*/
if ((offset+width) == 64)
{
bitMask = (mask<<offset);
} else {
bitMask = (mask<<offset) ^ (mask<<(offset + width));
}
/*Reset the bits that will be overwritten to be 0, and keep other bits the same.*/
*data = ~bitMask & *data;
*data = *data | (inField<<offset);
}
uint64_t
extractBitField(uint64_t inField, uint64_t width, uint64_t offset)
{
uint64_t mask = ~0;
uint64_t bitMask;
uint64_t outField;
if ((offset+width) == 64)
{
bitMask = (mask<<offset);
}
else
{
bitMask = (mask<<offset) ^ (mask<<(offset+width));
}
outField = (inField & bitMask) >> offset;
return outField;
}
uint64_t read_msr(int fd, uint64_t which) {
uint64_t data = 0;
if ( pread(fd, &data, sizeof data, which) != sizeof data ) {
printf("pread error!\n");
}
return data;
}
void write_msr(int fd, uint64_t which, uint64_t limit_info) {
if ( pwrite(fd, &limit_info , sizeof limit_info, which) != sizeof limit_info)
printf("pwrite error!\n");
}
double calc_time_window(uint64_t Y, uint64_t F) {
return _2POW(Y) * F_arr[F] * rapl_unit.time;
}
void
calc_y(uint64_t *Y, double F, jdouble custm_time) {
*Y = log2(custm_time / rapl_unit.time / F);
}
rapl_msr_power_limit_t
get_specs(int fd, uint64_t addr) {
uint64_t msr;
rapl_msr_power_limit_t limit_info;
msr = read_msr(fd, addr);
limit_info.power_limit = rapl_unit.power * extractBitField(msr, 14, 0);
limit_info.time_window_limit = calc_time_window(extractBitField(msr, 5, 17), extractBitField(msr, 2, 22));
limit_info.clamp_enable = extractBitField(msr, 1, 16);
limit_info.limit_enable = extractBitField(msr, 1, 15);
limit_info.lock_enable = extractBitField(msr, 1, 63);
return limit_info;
}
void
set_package_power_limit_enable(int fd, uint64_t setting, uint64_t addr) {
uint64_t msr;
msr = read_msr(fd, addr);
//enable set #1
putBitField(setting, &msr, 1, 15);
//enable set #2
putBitField(setting, &msr, 1, 47);
write_msr(fd, addr, msr);
}
void
set_dram_power_limit_enable(int fd, uint64_t setting, uint64_t addr) {
uint64_t msr;
msr = read_msr(fd, addr);
//enable set
putBitField(setting, &msr, 1, 15);
write_msr(fd, addr, msr);
}
void
set_package_clamp_enable(int fd, uint64_t addr) {
uint64_t msr;
msr = read_msr(fd, addr);
//clamp set #1
putBitField(0, &msr, 1, 16);
//clamp set #2
putBitField(0, &msr, 1, 48);
//putBitField(power_limit, &msr, 15, 32);
write_msr(fd, addr, msr);
}
//This idea is loop four possible sets of Y and F, and in return to get
//the time window, then use the set of Y and F that is smaller than but
//closest to the customized time.
void
convert_optimal_yf_from_time(uint64_t *Y, uint64_t *F, jdouble custm_time) {
uint64_t temp_y;
double time_window = 0.0;
double delta = 0.0;
double smal_delta = 5000000000.0;
int i = 0;
for(i = 0; i < 4; i++) {
calc_y(&temp_y, F_arr[i], custm_time);
time_window = calc_time_window(temp_y, i);
delta = custm_time -time_window;
//printf("Y is: %ld, F is: %d, time window: %f\n", temp_y, i, time_window);
//printf("delta is: %f\n", delta);
if(delta > 0 && delta < smal_delta) {
smal_delta = delta;
*Y = temp_y;
*F = i;
}
}
}
void
set_pkg_time_window_limit(int fd, uint64_t addr, jdouble custm_time) {
uint64_t msr;
uint64_t Y;
uint64_t F;
msr = read_msr(fd, addr);
//Set the customized time window.
convert_optimal_yf_from_time(&Y, &F, custm_time);
//Keep everything else the same.
//#1 time window bits
putBitField(F, &msr, 2, 22);
putBitField(Y, &msr, 5, 17);
//#2 time window bits
putBitField(F, &msr, 2, 54);
putBitField(Y, &msr, 5, 49);
write_msr(fd, addr, msr);
}
void
set_dram_time_window_limit(int fd, uint64_t addr, jdouble custm_time) {
uint64_t msr;
uint64_t Y;
uint64_t F;
msr = read_msr(fd, addr);
//Set the customized time window.
convert_optimal_yf_from_time(&Y, &F, custm_time);
//Keep everything else the same.
//#1 time window bits
putBitField(F, &msr, 2, 22);
putBitField(Y, &msr, 5, 17);
write_msr(fd, addr, msr);
}
void
set_pkg_power_limit(int fd, uint64_t addr, jdouble custm_power) {
uint64_t msr;
msr = read_msr(fd, addr);
//Set the customized power.
uint64_t power_limit = custm_power / rapl_unit.power;
//Keep everything else the same.
putBitField(power_limit, &msr, 15, 0);
putBitField(power_limit, &msr, 15, 32);
write_msr(fd, addr, msr);
}
void
set_dram_power_limit(int fd, uint64_t addr, jdouble custm_power) {
uint64_t msr;
msr = read_msr(fd, addr);
//Set the customized power.
uint64_t power_limit = custm_power / rapl_unit.power;
//Keep everything else the same.
putBitField(power_limit, &msr, 15, 0);
// putBitField(power_limit, &msr, 15, 32);
write_msr(fd, addr, msr);
}
/*Get unit information to be multiplied with */
void get_msr_unit(rapl_msr_unit *unit_obj, uint64_t data) {
uint64_t power_bit = extractBitField(data, 4, 0);
uint64_t energy_bit = extractBitField(data, 5, 8);
uint64_t time_bit = extractBitField(data, 4, 16);
unit_obj->power = (1.0 / _2POW(power_bit));
unit_obj->energy = (1.0 / _2POW(energy_bit));
unit_obj->time = (1.0 / _2POW(time_bit));
}
/*Get wraparound value in order to prevent nagetive value*/
void
get_wraparound_energy(double energy_unit) {
WRAPAROUND_VALUE = 1.0 / energy_unit;
}
void
get_rapl_pkg_parameters(int fd, rapl_msr_unit *unit_obj, rapl_msr_parameter *paras) {
get_rapl_parameters(fd, MSR_PKG_POWER_INFO, (rapl_msr_unit *)unit_obj, (rapl_msr_parameter *)paras);
}
void
get_rapl_dram_parameters(int fd, rapl_msr_unit *unit_obj, rapl_msr_parameter *paras) {
get_rapl_parameters(fd, MSR_DRAM_POWER_INFO, (rapl_msr_unit *)unit_obj, (rapl_msr_parameter *)paras);
}
void
get_rapl_parameters(int fd, uint64_t msr_addr, rapl_msr_unit *unit_obj, rapl_msr_parameter *paras) {
uint64_t thermal_spec_power;
uint64_t max_power;
uint64_t min_power;
uint64_t max_time_window;
uint64_t power_info;
power_info = read_msr(fd, msr_addr);
thermal_spec_power = extractBitField(power_info, 15, 0);
min_power = extractBitField(power_info, 15, 16);
max_power = extractBitField(power_info, 15, 32);
max_time_window = extractBitField(power_info, 6, 48);
paras->thermal_spec_power = unit_obj->power * thermal_spec_power;
paras->min_power = unit_obj->power * min_power;
paras->max_power = unit_obj->power * max_power;
paras->max_time_window = unit_obj->time * max_time_window;
}
void
getPowerSpec(double result[4], rapl_msr_parameter *parameter, int domain) {
int i;
/*Test use*/
/*
printf("thermal specification power is: %f, minimum power limit is: %f, maximum power limit is: %f, maximum time window is: %f\n", parameters[domain].thermal_spec_power, parameters[domain].min_power, parameters[domain].max_power, parameters[domain].max_time_window);
*/
for(i = 0; i < 4; i++) {
result[0] = parameters[domain].thermal_spec_power;
result[1] = parameters[domain].min_power;
result[2] = parameters[domain].max_power;
result[3] = parameters[domain].max_time_window;
}
}