forked from martinkersner/gtop
-
Notifications
You must be signed in to change notification settings - Fork 7
/
display.cc
174 lines (136 loc) · 4.1 KB
/
display.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
// Martin Kersner, [email protected]
// 2017/04/23
#include "display.hh"
void display_bars(const int & row, const int & col, const int & val) {
auto b = update_bar_dims(val);
clear_row(row, col);
display_left_bracket(row, col);
display_bars(b.val_bar);
mvprintw(row, col+b.max_bar+1, "%3d%%", val);
display_right_bracket();
refresh();
}
void display_bars(const int & row, const int & col, const int & val, const int & freq) {
auto b = update_bar_dims(val);
clear_row(row, col);
display_left_bracket(row, col);
display_bars(b.val_bar);
mvprintw(row, col+b.max_bar+1, "%3d%%", val);
display_right_bracket();
printw(" %d", freq);
refresh();
}
void display_mem_bars(const int & row, const int & col, const int & val, const int & max_val) {
auto val_norm = int((float(val) / max_val) * 100);
auto b = update_bar_dims(val_norm);
clear_row(row, col);
display_left_bracket(row, col);
display_bars(b.val_bar);
char buffer[MEM_BUFFER_SIZE];
sprintf(buffer, "%2.2fG/%2.2fG", mega2giga(val), mega2giga(max_val));
mvprintw(row, col+b.max_bar-6, buffer);
display_right_bracket();
refresh();
}
// deprecated move to widget
bar update_bar_dims(const int & val) {
bar b;
if (COLS < BAR_MAX_COLS_INT8) {
b.max_bar = int(COLS*BAR_AREA);
b.val_bar = int((val/BAR_MAX_COLS_F32)*b.max_bar);
}
else {
b.max_bar = BAR_MAX_COLS_INT8;
b.val_bar = val;
}
return b;
}
widget update_widget_dims(const int & val) {
widget w;
if (COLS < WIDGET_MAX_COLS_INT8) {
w.max_x = int(COLS*WIDGET_WIDTH);
w.val_x = int((val/WIDGET_MAX_COLS_F32)*w.max_x);
}
else {
w.max_x = BAR_MAX_COLS_INT8;
w.val_x = val;
}
return w;
}
float mega2giga(const int & mega_val) {
return mega_val/float(1000);
}
void clear_row(const int & row, const int & col) {
mvprintw(row, col, std::string(COLS, ' ').c_str());
refresh();
}
void display_bars(const int & size) {
attron(COLOR_PAIR(GREEN_BLACK));
printw(std::string(size, '|').c_str());
attroff(COLOR_PAIR(GREEN_BLACK));
}
void display_left_bracket(const int & row, const int & col) {
attron(A_BOLD);
mvprintw(row, col, "[");
attroff(A_BOLD);
}
void display_right_bracket() {
attron(A_BOLD);
printw("]");
attroff(A_BOLD);
}
void display_cpu_stats(const int & row, const tegrastats & ts) {
int idx = 0;
for (const auto & u : ts.cpu_usage) {
const auto cpu_label = std::string("CPU ") + std::to_string(idx);
attron(COLOR_PAIR(idx+1));
mvprintw(row+idx, 0, cpu_label.c_str());
attroff(COLOR_PAIR(idx+1));
if (ts.version == TX1)
display_bars(row+idx, BAR_OFFSET, u, ts.cpu_freq.at(0));
else if (ts.version == TX2)
display_bars(row+idx, BAR_OFFSET, u, ts.cpu_freq.at(idx));
idx++;
}
}
void display_emc_stats(const int & row, const tegrastats & ts) {
mvprintw(row, 0, "EMC");
display_bars(row, BAR_OFFSET, ts.emc_usage, ts.emc_freq);
}
void display_gpu_stats(const int & row, const tegrastats & ts) {
mvprintw(row, 0, "GPU");
display_bars(row, BAR_OFFSET, ts.gpu_usage, ts.gpu_freq);
}
void display_mem_stats(const int & row, const tegrastats & ts) {
mvprintw(row, 0, "Mem");
display_mem_bars(row, BAR_OFFSET, ts.mem_usage, ts.mem_max);
}
void display_usage_chart(const int & row, const std::vector<std::vector<int>> cpu_usage_buffer) {
int col = cpu_usage_buffer.size();
int idx = 1;
const int max_height = std::min(LINES-row-1, MIN_HEIGHT_USAGE_CHART);
// display scale
mvprintw(row, 0, "100");
mvprintw(row+float(max_height/2), 0, " 50");
mvprintw(row+max_height, 0, " 0");
for (const auto & timepoint : cpu_usage_buffer) {
for (const auto & cpu_usage : timepoint) {
if (cpu_usage == 0) {
idx++;
continue;
}
int tmp_cpu_usage = int((max_height/100.0)*cpu_usage);
attron(COLOR_PAIR(idx));
mvprintw(max_height-tmp_cpu_usage+row, col+3, "*");
attroff(COLOR_PAIR(idx));
idx++;
}
col--;
idx = 1;
}
refresh();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
for (int row = 0; row < LINES; ++row)
mvprintw(row, 0, std::string(COLS, ' ').c_str());
refresh();
}