forked from martinkersner/gtop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtop.cc
212 lines (166 loc) · 4.99 KB
/
gtop.cc
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
// Martin Kersner, m.kersner@gmail.com
// 2017/04/21
#include "gtop.hh"
std::mutex m;
std::condition_variable cv;
tegrastats t_stats;
bool processed = false;
bool ready = false;
bool finished = false;
int main() {
if (getuid()) {
std::cout << "gtop requires root privileges!" << std::endl;
exit(1);
}
std::thread t(read_tegrastats);
initscr();
noecho();
timeout(1);
start_color();
init_pair(WHITE_BLACK, COLOR_WHITE, COLOR_BLACK);
init_pair(RED_BLACK, COLOR_RED, COLOR_BLACK);
init_pair(GREEN_BLACK, COLOR_GREEN, COLOR_BLACK);
init_pair(YELLOW_BLACK, COLOR_YELLOW, COLOR_BLACK);
init_pair(BLUE_BLACK, COLOR_BLUE, COLOR_BLACK);
init_pair(MAGENTA_BLACK, COLOR_MAGENTA, COLOR_BLACK);
init_pair(CYAN_BLACK, COLOR_CYAN, COLOR_BLACK);
std::vector<std::vector<int>> cpu_usage_buffer;
while (1) {
{
std::lock_guard<std::mutex> lk(m);
ready = true;
}
cv.notify_one();
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{ return processed; });
processed = false;
// CPU, GPU, MEM STATS
dimensions dim_stats;
display_stats(dim_stats, t_stats);
// CPU USAGE CHART
update_usage_chart(cpu_usage_buffer, t_stats.cpu_usage);
display_usage_chart(10, cpu_usage_buffer);
lk.unlock();
refresh();
if (getch() == 'q') // press 'q' or Ctrl-C to quit
break;
}
{
std::lock_guard<std::mutex> lk(m);
finished = true;
}
t.join();
endwin();
return 0;
}
void read_tegrastats() {
std::array<char, STATS_BUFFER_SIZE> buffer;
#ifdef TEGRASTATS_FAKE
std::shared_ptr<FILE> pipe(popen(TEGRASTATSFAKE_PATH.c_str(), "r"), pclose);
#else
if (!file_exists(TEGRASTATS_PATH)) {
std::cerr << "tegrastats was not found at the expected location "
<< TEGRASTATS_PATH << std::endl;
exit(1); // TODO terminate correctly
}
std::shared_ptr<FILE> pipe(popen(TEGRASTATS_PATH.c_str(), "r"), pclose);
#endif
if (!pipe)
throw std::runtime_error ("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), STATS_BUFFER_SIZE, pipe.get()) != NULL) {
std::unique_lock<std::mutex> lk(m);
// terminate reading tegrastats
if (finished) {
lk.unlock();
break;
}
cv.wait(lk, []{ return ready; });
ready = false;
t_stats = parse_tegrastats(buffer.data());
processed = true;
lk.unlock();
cv.notify_one();
}
}
}
tegrastats parse_tegrastats(const char * buffer) {
tegrastats ts;
auto stats = tokenize(buffer, ' ');
if (stats.size() >= 15)
ts.version = TX1;
else
ts.version = TX2;
get_mem_stats(ts, stats.at(1));
switch (ts.version) {
case TX1:
get_cpu_stats_tx1(ts, stats.at(5));
get_gpu_stats(ts, stats.at(15));
break;
case TX2:
get_cpu_stats_tx2(ts, stats.at(5));
get_gpu_stats(ts, stats.at(13));
break;
case TK1: // TODO
break;
}
return ts;
}
void get_cpu_stats_tx1(tegrastats & ts, const std::string & str) {
auto cpu_stats = tokenize(str, '@');
auto cpu_usage_all = cpu_stats.at(0);
ts.cpu_freq.push_back(std::stoi(cpu_stats.at(1)));
auto cpu_usage = tokenize(cpu_usage_all.substr(1, cpu_usage_all.size()-2), ',');
for (const auto & u: cpu_usage) {
if (u != "off")
ts.cpu_usage.push_back(std::stoi(u.substr(0, u.size()-1)));
else
ts.cpu_usage.push_back(0);
}
}
void get_cpu_stats_tx2(tegrastats & ts, const std::string & str) {
const auto cpu_stats = tokenize(str.substr(1, str.size()-1), ',');
const auto at = std::string("@");
for (const auto & u: cpu_stats) {
if (u != "off") {
std::size_t found = at.find(u);
if (found == std::string::npos) {
auto cpu_info = tokenize(u, at.c_str()[0]);
ts.cpu_usage.push_back(std::stoi(cpu_info.at(0).substr(0, cpu_info.at(0).size()-1)));
ts.cpu_freq.push_back(std::stoi(cpu_info.at(1)));
}
}
else {
ts.cpu_usage.push_back(0);
ts.cpu_freq.push_back(0);
}
}
}
void get_gpu_stats(tegrastats & ts, const std::string & str) {
const auto gpu_stats = tokenize(str, '@');
const auto gpu_usage = gpu_stats.at(0);
ts.gpu_usage = std::stoi(gpu_usage.substr(0, gpu_usage.size()-1));
ts.gpu_freq = std::stoi(gpu_stats.at(1));
}
void get_mem_stats(tegrastats & ts, const std::string & str) {
const auto mem_stats = tokenize(str, '/');
const auto mem_max = mem_stats.at(1);
ts.mem_usage = std::stoi(mem_stats.at(0));
ts.mem_max = std::stoi(mem_max.substr(0, mem_max.size()-2));
}
void display_stats(const dimensions & d, const tegrastats & ts) {
// CPU
display_cpu_stats(0, ts);
// GPU
display_gpu_stats(ts.cpu_usage.size(), ts);
// Memory
display_mem_stats(ts.cpu_usage.size()+1, ts);
}
void update_usage_chart(std::vector<std::vector<int>> & usage_buffer,
const std::vector<int> & usage)
{
widget w = update_widget_dims(0);
if (usage_buffer.size() >= w.max_x)
usage_buffer.erase(usage_buffer.begin());
usage_buffer.push_back(usage);
}