-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetPowerLimits.cpp
71 lines (60 loc) · 2.02 KB
/
SetPowerLimits.cpp
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
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#define RAPL_BASE_DIRECTORY "/sys/class/powercap/intel-rapl/intel-rapl:0/"
#define LONG_LIMIT "constraint_0_power_limit_uw"
#define SHORT_LIMIT "constraint_1_power_limit_uw"
#define LONG_WINDOW "constraint_0_time_window_us"
#define SHORT_WINDOW "constraint_1_time_window_us"
void writeLimitToFile (std::string fileName, int limit){
std::ofstream outfile (fileName.c_str(), std::ios::out | std::ios::trunc);
if (outfile.is_open()){
outfile << limit;
} else {
std::cerr << "cannot write the limit to file\n";
std::cerr << "file not open\n";
}
outfile.close();
}
int readLimitFromFile (std::string fileName){
std::ifstream limitFile (fileName.c_str());
std::string line;
int limit = -1;
if (limitFile.is_open()){
while ( getline (limitFile, line) ){
limit = atoi(line.c_str());
}
limitFile.close();
} else {
std::cerr << "cannot read the limit file\n";
std::cerr << "file not open\n";
}
return limit;
}
int main (int argc, char *argv[]) {
if(argc < 3){
printf("Usage: ./SetPowerLimit longLimit shortLimit\n");
exit(EXIT_FAILURE);
}
std::string raplDir = RAPL_BASE_DIRECTORY;
std::string pl0dir = raplDir + LONG_LIMIT;
std::string pl1dir = raplDir + SHORT_LIMIT;
int powerLimit0 = atoi(argv[1])*1000000;
int powerLimit1 = atoi(argv[2])*1000000;
writeLimitToFile (pl0dir, powerLimit0);
if (readLimitFromFile(pl0dir) != powerLimit0) {
std::cerr << "Limit was not overwritten succesfully.\n"
<< "HINT: Check dmesg if it is not locked by BIOS.\n";
}
writeLimitToFile (pl1dir, powerLimit1);
if (readLimitFromFile(pl1dir) != powerLimit1) {
std::cerr << "Limit was not overwritten succesfully.\n"
<< "HINT: Check dmesg if it is not locked by BIOS.\n";
}
return 0;
}