Skip to content

Commit d584426

Browse files
author
Jesse Martin
committed
extract cpu class
1 parent f996f62 commit d584426

File tree

3 files changed

+115
-61
lines changed

3 files changed

+115
-61
lines changed

command.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
string execCommand(const string &command) {
7+
char buffer[128];
8+
string result = "";
9+
FILE *pipe = popen(command.c_str(), "r");
10+
if (!pipe) {
11+
printf("popen() failed!");
12+
}
13+
try
14+
{
15+
while (!feof(pipe))
16+
{
17+
if (fgets(buffer, 128, pipe) != NULL)
18+
result += buffer;
19+
}
20+
}
21+
catch (...)
22+
{
23+
pclose(pipe);
24+
throw;
25+
}
26+
pclose(pipe);
27+
return result;
28+
}

cpu.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <stdio.h>
2+
#include "command.cpp"
3+
4+
using namespace std;
5+
6+
string probeCpu() {
7+
return execCommand("sensors");
8+
}
9+
10+
vector<string> split(const string &s, char delim) {
11+
stringstream ss(s);
12+
string item;
13+
vector<string> tokens;
14+
while (getline(ss, item, delim)) {
15+
tokens.push_back(item);
16+
}
17+
return tokens;
18+
}
19+
20+
vector<double> parseCpuTemp(const string &cpu_temp) {
21+
vector<string> lines = split(cpu_temp.c_str(), '\n');
22+
vector<double> temps;
23+
regex core_temp ("Core[^+|-]*([^°]*).*");
24+
smatch matches;
25+
for (int i = 0; i < lines.size(); i++) {
26+
regex_match(lines[i], matches, core_temp);
27+
if (matches.size() == 2) {
28+
double temp = stod(matches.str(1));
29+
temps.push_back(temp);
30+
}
31+
}
32+
return temps;
33+
}
34+
35+
vector<double> getCpuTemp() {
36+
string cpu_temp = probeCpu();
37+
return parseCpuTemp(cpu_temp);
38+
}
39+
40+
class CPU
41+
{
42+
private:
43+
vector<double> temps;
44+
45+
public:
46+
CPU()
47+
{
48+
}
49+
50+
void update()
51+
{
52+
temps = getCpuTemp();
53+
}
54+
55+
vector<double> getTemps() {
56+
return temps;
57+
}
58+
};
59+

temps.cpp

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,34 @@
99
#include <iomanip>
1010
#include <math.h>
1111
#include "center.cpp"
12+
#include "cpu.cpp"
1213

1314
using namespace std;
1415

15-
vector<double> cpu;
1616
vector<double> gpu;
1717
vector<double> drive;
1818

1919
int col_width = 5;
2020

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)
2222
{
2323
ostringstream rendered;
24+
int title_height = 1;
25+
int footer_height = 1;
26+
int graph_height = height - title_height - footer_height;
2427

2528
string cpu_header = "CPU Temps";
2629
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;
2832

2933
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--)
3135
{
3236
rendered << "| ";
33-
for (int j = 0; j < cpu.size(); j++)
37+
for (int j = 0; j < cpu_temps.size(); j++)
3438
{
35-
if (cpu[j] >= i * 10)
39+
if (cpu_temps[j] >= i * 10)
3640
{
3741
rendered << setw(col_width) << centered("==");
3842
}
@@ -45,74 +49,33 @@ void render(const vector<double> &cpu, const vector<double> &gpu, const vector<d
4549
}
4650

4751
rendered << "| ";
48-
for (int i = 0; i < cpu.size(); i++) {
52+
for (int i = 0; i < cpu_temps.size(); i++) {
4953
ostringstream temp;
50-
temp << cpu.at(i);
54+
temp << cpu_temps.at(i);
5155
rendered << setw(col_width) << centered(temp.str());
5256
}
5357
rendered << "|\n";
5458

5559
cout << rendered.str();
5660
}
5761

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");
7964
}
8065

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) {
10467
}
10568

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);
10972
}
11073

111-
void update_cpu_temp() {
112-
cpu = get_cpu_temp();
74+
void updateGpuTemp() {
75+
gpu = getGpuTemp();
11376
}
11477

115-
void clear_screen(const int &height) {
78+
void clearScreen(const int &height) {
11679
for (int i = 0; i < height; i++) {
11780
cout << "\033[A\033[2K";
11881
}
@@ -121,13 +84,17 @@ void clear_screen(const int &height) {
12184
int main()
12285
{
12386
int height = 12;
87+
88+
CPU cpu {};
89+
// GPU gpu {};
12490
while(true) {
125-
update_cpu_temp();
126-
gpu = {29.0};
91+
cpu.update();
92+
// gpu.update();
93+
updateGpuTemp();
12794
drive = {26.2};
12895
render(cpu, gpu, drive, height);
12996
this_thread::sleep_for(chrono::seconds(1));
130-
clear_screen(height);
97+
clearScreen(height);
13198
}
13299
return 0;
133100
}

0 commit comments

Comments
 (0)