-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrabage_linux_sys_usage.c
More file actions
235 lines (196 loc) Β· 7.63 KB
/
grabage_linux_sys_usage.c
File metadata and controls
235 lines (196 loc) Β· 7.63 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "ctermui_screen.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define NETWORK_USAGE_RANGE 10
#define MAX_CORES 8
int cpu_usage[MAX_CORES + MAX_CORES * 4] = {0};
void generate_data(float xvalues[], float yvalues[]) {
srand(time(NULL));
for (int i = 0; i < NETWORK_USAGE_RANGE; ++i) {
xvalues[i] = i;
yvalues[i] = 0;
}
}
void getCPUUsage(int cpu_usage_percentage[]) {
FILE *file = fopen("/proc/stat", "r");
if (file == NULL) {
perror("Error opening /proc/stat");
exit(EXIT_FAILURE);
}
char line[256];
while (fgets(line, sizeof(line), file) != NULL) {
if (strncmp(line, "cpu", 3) == 0) {
int core;
sscanf(line, "cpu%d", &core);
if (core >= 0 && core < MAX_CORES) {
int user, nice, sys, idle;
sscanf(line + 3, "%d %d %d %d", &user, &nice, &sys, &idle);
if (user < cpu_usage_percentage[core] ||
nice < cpu_usage_percentage[core + MAX_CORES] ||
sys < cpu_usage_percentage[core + MAX_CORES * 2] ||
idle < cpu_usage_percentage[core + MAX_CORES * 3]) {
cpu_usage_percentage[core] = user;
cpu_usage_percentage[core + MAX_CORES] = nice;
cpu_usage_percentage[core + MAX_CORES * 2] = sys;
cpu_usage_percentage[core + MAX_CORES * 3] = idle;
continue;
}
int total = user + nice + sys + idle;
int diff_total = total - cpu_usage_percentage[core + MAX_CORES * 4];
int diff_idle = idle - cpu_usage_percentage[core + MAX_CORES * 3];
if (diff_total > 0) {
int usage_percentage = (diff_idle * 100) / diff_total;
cpu_usage_percentage[core] =
(usage_percentage > 100) ? 100 : usage_percentage;
cpu_usage_percentage[core + MAX_CORES * 4] = total;
cpu_usage_percentage[core + MAX_CORES] = nice;
cpu_usage_percentage[core + MAX_CORES * 2] = sys;
cpu_usage_percentage[core + MAX_CORES * 3] = idle;
}
}
}
}
fclose(file);
}
void getRamUsage(int *total, int *free) {
FILE *file = fopen("/proc/meminfo", "r");
if (file == NULL) {
perror("Error opening /proc/meminfo");
exit(EXIT_FAILURE);
}
char line[256];
while (fgets(line, sizeof(line), file) != NULL) {
if (strncmp(line, "MemTotal:", 9) == 0) {
sscanf(line + 9, "%d", total);
} else if (strncmp(line, "MemFree:", 8) == 0) {
sscanf(line + 8, "%d", free);
}
}
fclose(file);
}
void getNetworkUsage(const char *interface, float *incoming, float *outgoing) {
FILE *file = fopen("/proc/net/dev", "r");
if (file == NULL) {
perror("Error opening /proc/net/dev");
exit(EXIT_FAILURE);
}
char buffer[256];
fgets(buffer, sizeof(buffer), file);
fgets(buffer, sizeof(buffer), file);
float in, out;
while (fgets(buffer, sizeof(buffer), file) != NULL) {
if (sscanf(buffer, "%*s %f %*s %*s %*s %*s %*s %*s %*s %*s %f", &in,
&out) == 2 &&
strstr(buffer, interface) != NULL) {
*incoming = in;
*outgoing = out;
break;
}
}
*outgoing /= 1024;
fclose(file);
}
void periodic(ctermui_screen_t *screen_p) {
ctermui_screen_t screen = *screen_p;
ctermui_layout_t root = screen->root;
ctermui_layout_t w = ctermui_layout_find(root, "wiget_1_1");
if (w == NULL) {
fprintf(stderr, "layout not found\n");
}
ctermui_component_t c = ctermui_layout_find_component(w, "network");
ScatterPlot *scatter_plot = (ScatterPlot *)c->core_component;
int every = 1;
if (screen->loop_count % every == 0) {
float incoming, outgoing;
getNetworkUsage("wlp1s0", &incoming, &outgoing);
float speed =
(outgoing - scatter_plot->yvalues[NETWORK_USAGE_RANGE - 1]) / 1024;
speed = roundf(speed * 1000) / 1000;
for (int i = 0; i < NETWORK_USAGE_RANGE - 1; i++) {
scatter_plot->xvalues[i] = scatter_plot->xvalues[i + 1];
scatter_plot->yvalues[i] = scatter_plot->yvalues[i + 1];
}
scatter_plot->xvalues[NETWORK_USAGE_RANGE - 1] =
scatter_plot->xvalues[NETWORK_USAGE_RANGE - 2] + 1;
scatter_plot->yvalues[NETWORK_USAGE_RANGE - 1] = speed;
}
if (screen->loop_count % 2) {
ctermui_layout_t cpu_barchart = ctermui_layout_find(root, "wiget_1_2");
ctermui_component_t barchart =
ctermui_layout_find_component(cpu_barchart, "barchart");
BarChart *bar_chart = (BarChart *)barchart->core_component;
getCPUUsage(cpu_usage);
ctermui_barchart_update_values(barchart, cpu_usage, MAX_CORES);
ctermui_layout_t ram_layout = ctermui_layout_find(root, "wiget_2_1_3");
ctermui_component_t ram_usage =
ctermui_layout_find_component(ram_layout, "ram_usage");
ProgressBar *ram_bar = (ProgressBar *)ram_usage->core_component;
int total, free;
getRamUsage(&total, &free);
char usage_string[100];
sprintf(usage_string, "%dMb/%dGb", (free) / (1024),
(total) / (1024 * 1024));
ram_bar->progress = free;
ram_bar->max = total;
strcpy(ram_bar->text, usage_string);
}
ctermui_screen_refresh_layout(screen, screen->root);
}
int main() {
ctermui_screen_t screen = ctermui_screen_init();
ctermui_layout_t root = ctermui_layout_new_root(
CTERMUI_HORIZONTAL, screen->width, screen->height);
ctermui_layout_t wiget_1 =
ctermui_layout_new("left_layout", CTERMUI_VERTICAL, 50);
ctermui_layout_t wiget_2 =
ctermui_layout_new("right_layout", CTERMUI_VERTICAL, 50);
ctermui_layout_t wiget_1_1 =
ctermui_layout_new("wiget_1_1", CTERMUI_HORIZONTAL, 50);
ctermui_layout_t wiget_1_2 =
ctermui_layout_new("wiget_1_2", CTERMUI_HORIZONTAL, 50);
ctermui_layout_t wiget_2_1 =
ctermui_layout_new("wiget_2_1", CTERMUI_VERTICAL, 50);
ctermui_layout_t wiget_2_1_1 = ctermui_layout_new("wiget_2_1_1", LEAF, 25);
ctermui_layout_t wiget_2_1_2 = ctermui_layout_new("wiget_2_1_2", LEAF, 25);
ctermui_layout_t wiget_2_1_3 = ctermui_layout_new("wiget_2_1_3", LEAF, 25);
ctermui_layout_t wiget_2_2 =
ctermui_layout_new("wiget_2_2", CTERMUI_HORIZONTAL, 50);
int cpu_usage[MAX_CORES + MAX_CORES * 4] = {0};
char labels[MAX_CORES][100];
for (int i = 0; i < MAX_CORES; ++i) {
cpu_usage[i] = 0;
sprintf(labels[i], "core %d", i + 1);
}
ctermui_component_t barchart =
ctemrui_new_barchart("barchart", CTERMUI_MAGENTA, CTERMUI_EMPTY, 100,
CTERMUI_HORIZONTAL, cpu_usage, labels, MAX_CORES, 1);
ctermui_layout_add_component(wiget_1_2, barchart);
float x_values[NETWORK_USAGE_RANGE];
float y_values[NETWORK_USAGE_RANGE];
generate_data(x_values, y_values);
ctermui_component_t linechart = ctermui_new_scatter_plot(
"network", x_values, y_values, NETWORK_USAGE_RANGE, CTERMUI_CYAN,
CTERMUI_EMPTY, CTERMUI_RED, 'o', 1);
ctermui_layout_add_component(wiget_1_1, linechart);
int total, free;
getRamUsage(&total, &free);
char usage_string[100];
sprintf(usage_string, "%dMb/%dMb", (total - free) / 1024, total / 1024);
ctermui_component_t ram_usage = ctermui_new_progress_bar(
"ram_usage", CTERMUI_GREEN, CTERMUI_WHITE, total, free, usage_string,
CTERMUI_BLACK, CTERMUI_HORIZONTAL);
ctermui_layout_add_component(wiget_2_1_3, ram_usage);
ctermui_layout_add_child(wiget_1, wiget_1_1);
ctermui_layout_add_child(wiget_1, wiget_1_2);
ctermui_layout_add_child(wiget_2_1, wiget_2_1_1);
ctermui_layout_add_child(wiget_2_1, wiget_2_1_2);
ctermui_layout_add_child(wiget_2_1, wiget_2_1_3);
ctermui_layout_add_child(wiget_2, wiget_2_1);
ctermui_layout_add_child(wiget_2, wiget_2_2);
ctermui_layout_add_child(root, wiget_1);
ctermui_layout_add_child(root, wiget_2);
ctermui_screen_set_layout_root(screen, root);
ctermui_screen_loop_start(screen, periodic, 1 * 1000000);
}