-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
141 lines (126 loc) · 3.51 KB
/
config.cpp
File metadata and controls
141 lines (126 loc) · 3.51 KB
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
// SPDX-License-Identifier: GPL-2.0
#include <array>
#include <complex.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <unistd.h>
using namespace std;
enum CONFIG_TYPE {
TSO,
STFILL,
};
bool exit_ = false;
string exec(const char* cmd)
{
array<char, 128> buffer;
string result;
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
string get_config_name(CONFIG_TYPE which)
{
switch (which) {
case TSO:
return "tso_disabled";
break;
case STFILL:
return "stfill";
break;
default:
return "";
break;
}
return "";
}
void show_value(CONFIG_TYPE which)
{
string config_name = get_config_name(which);
string Command = "cat /sys/kernel/loongarch_csr/" + config_name;
try {
string output = exec(Command.c_str());
cout << config_name << ":" << output << endl;
} catch (const runtime_error& e) {
cerr << "get value failed: " << e.what() << endl;
}
}
void set_value(CONFIG_TYPE which, string value)
{
string config_name = get_config_name(which);
// echo 0 > /sys/kernel/loongarch_csr/tso_disabled
string Command = "su -c 'echo " + value + " > /sys/kernel/loongarch_csr/" + config_name + "'";
try {
string output = exec(Command.c_str());
cout << "success." << endl
<< "new value:" << endl;
show_value(which);
} catch (const runtime_error& e) {
cerr << "failed: " << e.what() << endl;
}
}
void print_help()
{
cout << endl
<< "Usage:" << endl;
cout << " - sudo ./config -h" << endl;
cout << " - sudo ./config tso enable" << endl;
cout << " - sudo ./config tso disable" << endl;
cout << " - sudo ./config tso show" << endl;
cout << " - sudo ./config stfill enable" << endl;
cout << " - sudo ./config stfill disable" << endl;
cout << " - sudo ./config stfill show" << endl;
}
int main(int argv, char* argc[])
{
if (argv == 2 && strcmp(argc[1], "-h") == 0) {
print_help();
return 0;
}
if (argv != 3) {
cout << "please input 2 args" << endl;
print_help();
return 0;
}
if (geteuid() != 0) {
cout << "Please rerun the program with 'sudo'." << endl;
print_help();
return 0;
}
string config_which = argc[1];
string config_operation = argc[2];
if (config_which == "tso") {
if (config_operation == "show") {
show_value(TSO);
} else if (config_operation == "enable") {
set_value(TSO, "0");
} else if (config_operation == "disable") {
set_value(TSO, "1");
} else {
cout << "please input 'show', 'disable' or 'enable'." << endl;
}
} else if (config_which == "stfill") {
if (config_operation == "show") {
show_value(STFILL);
} else if (config_operation == "enable") {
set_value(STFILL, "1");
} else if (config_operation == "disable") {
set_value(STFILL, "0");
} else {
cout << "please input 'show', 'disable' or 'enable'." << endl;
}
} else {
cout << "please input correct config name." << endl;
print_help();
return 0;
}
return 0;
}