9
9
#include < iomanip>
10
10
#include < math.h>
11
11
#include " center.cpp"
12
+ #include " cpu.cpp"
12
13
13
14
using namespace std ;
14
15
15
- vector<double > cpu;
16
16
vector<double > gpu;
17
17
vector<double > drive;
18
18
19
19
int col_width = 5 ;
20
20
21
- void render (const vector< double > & cpu, const vector<double > &gpu, const vector<double > &drive, const int &height)
21
+ void render (CPU cpu, const vector<double > &gpu, const vector<double > &drive, const int &height)
22
22
{
23
23
ostringstream rendered;
24
+ int title_height = 1 ;
25
+ int footer_height = 1 ;
26
+ int graph_height = height - title_height - footer_height;
24
27
25
28
string cpu_header = " CPU Temps" ;
26
29
int section_padding_right = 1 ;
27
- int section_width = col_width * cpu.size () + section_padding_right;
30
+ vector<double > cpu_temps = cpu.getTemps ();
31
+ int section_width = col_width * cpu_temps.size () + section_padding_right;
28
32
29
33
rendered << " |" << setw (section_width) << centered (cpu_header) << " |\n " ;
30
- for (int i = 10 ; i > 0 ; i--)
34
+ for (int i = graph_height ; i > 0 ; i--)
31
35
{
32
36
rendered << " | " ;
33
- for (int j = 0 ; j < cpu .size (); j++)
37
+ for (int j = 0 ; j < cpu_temps .size (); j++)
34
38
{
35
- if (cpu [j] >= i * 10 )
39
+ if (cpu_temps [j] >= i * 10 )
36
40
{
37
41
rendered << setw (col_width) << centered (" ==" );
38
42
}
@@ -45,74 +49,33 @@ void render(const vector<double> &cpu, const vector<double> &gpu, const vector<d
45
49
}
46
50
47
51
rendered << " | " ;
48
- for (int i = 0 ; i < cpu .size (); i++) {
52
+ for (int i = 0 ; i < cpu_temps .size (); i++) {
49
53
ostringstream temp;
50
- temp << cpu .at (i);
54
+ temp << cpu_temps .at (i);
51
55
rendered << setw (col_width) << centered (temp.str ());
52
56
}
53
57
rendered << " |\n " ;
54
58
55
59
cout << rendered.str ();
56
60
}
57
61
58
- string probe_cpu () {
59
- char buffer[128 ];
60
- string result = " " ;
61
- FILE *pipe = popen (" sensors" , " r" );
62
- if (!pipe )
63
- printf (" popen() failed!" );
64
- try
65
- {
66
- while (!feof (pipe ))
67
- {
68
- if (fgets (buffer, 128 , pipe ) != NULL )
69
- result += buffer;
70
- }
71
- }
72
- catch (...)
73
- {
74
- pclose (pipe );
75
- throw ;
76
- }
77
- pclose (pipe );
78
- return result;
62
+ string probeGpu () {
63
+ return execCommand (" nvidia-smi --id=0 --query-gpu=temperature.gpu --format=csv" );
79
64
}
80
65
81
- vector<string> split (const string &s, char delim) {
82
- stringstream ss (s);
83
- string item;
84
- vector<string> tokens;
85
- while (getline (ss, item, delim)) {
86
- tokens.push_back (item);
87
- }
88
- return tokens;
89
- }
90
-
91
- vector<double > parse_cpu_temp (const string &cpu_temp) {
92
- vector<string> lines = split (cpu_temp.c_str (), ' \n ' );
93
- vector<double > temps;
94
- regex core_temp (" Core[^+|-]*([^°]*).*" );
95
- smatch matches;
96
- for (int i = 0 ; i < lines.size (); i++) {
97
- regex_match (lines[i], matches, core_temp);
98
- if (matches.size () == 2 ) {
99
- double temp = stod (matches.str (1 ));
100
- temps.push_back (temp);
101
- }
102
- }
103
- return temps;
66
+ vector<double > parseGpuTemp (const string &gpu_temp) {
104
67
}
105
68
106
- vector<double > get_cpu_temp () {
107
- string cpu_temp = probe_cpu ();
108
- return parse_cpu_temp (cpu_temp );
69
+ vector<double > getGpuTemp () {
70
+ string gpu_temp = probeGpu ();
71
+ return parseGpuTemp (gpu_temp );
109
72
}
110
73
111
- void update_cpu_temp () {
112
- cpu = get_cpu_temp ();
74
+ void updateGpuTemp () {
75
+ gpu = getGpuTemp ();
113
76
}
114
77
115
- void clear_screen (const int &height) {
78
+ void clearScreen (const int &height) {
116
79
for (int i = 0 ; i < height; i++) {
117
80
cout << " \033 [A\033 [2K" ;
118
81
}
@@ -121,13 +84,17 @@ void clear_screen(const int &height) {
121
84
int main ()
122
85
{
123
86
int height = 12 ;
87
+
88
+ CPU cpu {};
89
+ // GPU gpu {};
124
90
while (true ) {
125
- update_cpu_temp ();
126
- gpu = {29.0 };
91
+ cpu.update ();
92
+ // gpu.update();
93
+ updateGpuTemp ();
127
94
drive = {26.2 };
128
95
render (cpu, gpu, drive, height);
129
96
this_thread::sleep_for (chrono::seconds (1 ));
130
- clear_screen (height);
97
+ clearScreen (height);
131
98
}
132
99
return 0 ;
133
100
}
0 commit comments