-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
116 lines (93 loc) · 3.29 KB
/
main.cpp
File metadata and controls
116 lines (93 loc) · 3.29 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
#include <iostream>
#include <unistd.h>
#include <chrono>
#include <map>
#include <vector>
#include <ncurses.h>
using namespace std;
#include "global_definitions.h"
#include "process_monitor.cpp"
void displayProcessInfo(vector<ProcessInfo>& procInfo, SystemSummary syssum) {
printw("CPU Usage: %6.2f%% | RAM Usage: %6.2f%% (%llu / %llu MB)\n",
syssum.totalCPUUsage,
syssum.totalRAMUsage,
syssum.totalUsedRAM / (1024 * 1024),
syssum.totalRAM / (1024 * 1024));
printw("+--------+-----------------------------+---------+------------+---------+\n");
printw("| PID | Name | CPU%% | Memory(MB) | RAM%% |\n");
printw("+--------+-----------------------------+---------+------------+---------+\n");
for (ProcessInfo p : procInfo) {
printw("| %-6d | %-27s | %6.2f%% | %10.4f | %6.3f%% |\n",
p.pid, p.name.c_str(), p.cpuPercent, p.rssMB, p.ramPercent);
}
move(LINES - 2, 0);
printw("+--------+-----------------------------+---------+------------+---------+\n");
move(LINES - 1, 0);
clrtoeol();
mvprintw(LINES - 1, 0, "Press 'q' to quit and 'h' for help...");
}
void displayHelpPage() {
printw("+------------------- HELP -------------------+\n");
printw("| h: Show this help page |\n");
printw("| v: Display processes unsorted |\n");
printw("| r: Sort processes by RAM percentage |\n");
printw("| c: Sort processes by CPU percentage |\n");
printw("| g: Turn On/Off Group Mode (by Name) |\n");
printw("| q: Quit |\n");
printw("+--------------------------------------------+\n");
move(LINES - 1, 0);
clrtoeol();
mvprintw(LINES - 1, 0, "Press 'q' to quit...");
}
void runProcessMonitor() {
initscr(); // Start ncurses
noecho(); // Do not echo typed chars
cbreak(); // Disable line buffering
keypad(stdscr, TRUE); // Enable special keys
AppState state = VIEW;
bool is_group = false;
SystemSummary syssum = getSystemSummary();
map<pid_t, ProcessStats> prevStats;
vector<ProcessInfo> procInfo;
timeout(1000); // getch() modified to only block for up to 1s
while (true) {
clear();
if (state == VIEW_HELP) {
displayHelpPage();
} else {
getProcessDetails(procInfo, syssum, prevStats);
// Get system summary again
syssum = getSystemSummary();
sortAndGroupProcesses(procInfo, state, is_group);
// Sort and print process information
displayProcessInfo(procInfo, syssum);
}
refresh();
int ch = getch();
switch (ch) {
case 'h':
state = VIEW_HELP;
break;
case 'r':
state = SORT_BY_RAM;
break;
case 'c':
state = SORT_BY_CPU;
break;
case 'v':
state = VIEW;
break;
case 'g':
is_group = !is_group;
break;
default:
break;
}
if (ch == 'q') break;
procInfo.clear(); // Clear vector
}
endwin();
}
int main() {
runProcessMonitor();
}