-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPUModule.cpp
124 lines (100 loc) · 3.21 KB
/
CPUModule.cpp
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* CPUModule.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kdenisov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/03 16:03:32 by kdenisov #+# #+# */
/* Updated: 2019/11/14 11:12:25 by kdenisov ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <sys/sysctl.h>
#include "includes/CPUModule.hpp"
CPUModule::CPUModule() {
}
CPUModule::CPUModule(int pos, int gpos) : _pos(pos), _gpos(gpos) {
this->_module = "CPU";
char info[100];
size_t size = 100;
sysctlbyname("machdep.cpu.brand_string", &info, &size, NULL, 0);
this->_info = info;
refresh();
}
CPUModule::~CPUModule() {
}
CPUModule::CPUModule(CPUModule const & src) {
*this = src;
}
CPUModule & CPUModule::operator=(CPUModule const & rfs) {
this->_module = rfs._module;
this->_info = rfs._info;
this->_usage = rfs._usage;
this->_stat = rfs._stat;
this->_pos = rfs._pos;
this->_gpos = rfs._gpos;
return (*this);
}
void CPUModule::render(IMonitorDisplay *d) {
d->render(this);
}
void CPUModule::refresh() {
system("top -l 1 | grep \"^CPU usage:\" | awk '{print $3}' > ./logs/cpulog");
std::ifstream ifs("./logs/cpulog");
try {
if (!ifs.is_open())
throw std::exception();
}
catch(std::exception& e) {
std::cerr << e.what() << "(" << getName() << ")" << ": Failed on openning file" << std::endl;
exit(1);
}
std::string buff;
std::getline(ifs, buff);
this->_usage = "Usage: " + buff;
buff.erase(buff.length() - 1, buff.length());
float usage;
try {
usage = static_cast<float>(std::stof(buff));
}
catch(std::exception& e) {
std::cerr << e.what() << std::endl;
}
this->_stat.push_back(usage);
ifs.close();
}
std::string CPUModule::getName() const {
return (this->_module);
}
int CPUModule::getPos() const {
return (this->_pos);
}
int CPUModule::getGPos() const {
return (this->_gpos);
}
std::string CPUModule::getInfo() const {
return (this->_info);
}
std::string CPUModule::getUsage() const {
return (this->_usage);
}
std::list<float> CPUModule::getLastUsage(unsigned long n) {
std::list<float> chart;
if (this->_stat.size() <= n) {
for (std::vector<float>::iterator it = this->_stat.begin(); it != this->_stat.end(); it++) {
chart.push_back(*it);
}
return (chart);
}
for (unsigned long i = this->_stat.size() - n; i < this->_stat.size(); i++) {
chart.push_back(this->_stat.at(i));
}
return (chart);
}
int CPUModule::getSize(std::string const name) const {
return (name.length());
}