-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
133 lines (107 loc) · 3.25 KB
/
main.cpp
File metadata and controls
133 lines (107 loc) · 3.25 KB
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
#include <iostream>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unistd.h>
#include <chrono>
#include <thread>
#include <string>
#include <cstdlib>
#include "constants.hpp"
using namespace std;
void getCommand(vector<string> arguments);
void getProcesses();
long get_total_cpu_time();
long get_process_cpu_time(pid_t pid);
double get_cpu_usage_percent(pid_t pid, int interval_ms);
void parseCommand(const string& str);
int main() {
cout << Constants::WELCOME_MESSAGE;
string command = "";
// terminal program driven by while loop and leaves on exit
while (command != Constants::EXIT_COMMAND) {
cout << Constants::TERMINAL_PROMPT;
cout << "Type a number: ";
cin >> command;
parseCommand(command);
}
return 0;
}
void parseCommand(const string& str) {
vector<string> arguments;
istringstream iss(str);
string token;
while (iss >> token) {
arguments.push_back(token);
}
getCommand(arguments);
}
//this function will be used to parse the different commands we add
//these conditionals can lead to other functions depending on how much logic the command includes
//any example of this is the conditional for processes, if met this executes the function to show all processes
void getCommand(vector<string> args) {
const string arg0 = args[0];
if (arg0 == Constants::ALL_PROCESSES) {
getProcesses();
} else if (arg0 == Constants::PROCESS_MEMORY && args.size() > 1) {
pid_t pid = static_cast<pid_t>(std::stoi(args[1]));
double d = get_cpu_usage_percent(pid, 100);
printf("%s memory at %f percent", args[1].c_str(), d);
}
}
void getProcesses() {
FILE* pipe = popen("ps -e", "r");
if (!pipe) {
std::cerr << "Error opening pipe" << std::endl;
return;
}
char buffer[256];
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
std::cout << buffer;
}
pclose(pipe);
}
long get_total_cpu_time() {
ifstream file("/proc/stat");
string line;
getline(file, line);
istringstream ss(line);
string cpu;
long total = 0, value;
ss >> cpu;
while (ss >> value) {
total += value;
}
return total;
}
long get_process_cpu_time(pid_t pid) {
string path = "/proc/" + to_string(pid) + "/stat";
ifstream file(path);
string line;
getline(file, line);
istringstream ss(line);
vector<string> fields;
string field;
while (ss >> field) {
fields.push_back(field);
}
if (fields.size() < 17) return -1;
long utime = stol(fields[13]);
long stime = stol(fields[14]);
return utime + stime;
}
double get_cpu_usage_percent(pid_t pid, int interval_ms) {
long clk_tck = sysconf(_SC_CLK_TCK);
long proc_time1 = get_process_cpu_time(pid);
long total_time1 = get_total_cpu_time();
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
long proc_time2 = get_process_cpu_time(pid);
long total_time2 = get_total_cpu_time();
long proc_delta = proc_time2 - proc_time1;
long total_delta = total_time2 - total_time1;
if (total_delta == 0) return 0.0;
double cpu_usage = (double)proc_delta / total_delta * 100.0;
return cpu_usage;
}