-
Notifications
You must be signed in to change notification settings - Fork 22
/
driver.c
401 lines (364 loc) · 10.3 KB
/
driver.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pslib.h"
void test_diskusage() {
DiskUsage du;
printf(" -- disk_usage \n");
disk_usage("/", &du);
printf("/\ntotal: %" PRIu64 "\nused: %" PRIu64 "\nfree: %" PRIu64
"\npercent: %.1f\n\n",
du.total, du.used, du.free, du.percent);
disk_usage("/etc", &du);
printf("/etc\ntotal: %" PRIu64 "\nused: %" PRIu64 "\nfree: %" PRIu64
"\npercent: %.1f\n\n",
du.total, du.used, du.free, du.percent);
}
void test_diskpartitioninfo() {
uint32_t i;
DiskPartitionInfo *phys_dp, *all_dp;
printf(" -- disk_partitions (physical) \n");
phys_dp = disk_partitions(1);
if (!phys_dp) {
printf("Aborting\n");
return;
}
printf("Partitions : %" PRIu32 "\n", phys_dp->nitems);
for (i = 0; i < phys_dp->nitems; i++) {
printf("%s %s %s %s\n", phys_dp->partitions[i].device,
phys_dp->partitions[i].mountpoint, phys_dp->partitions[i].fstype,
phys_dp->partitions[i].opts);
}
free_disk_partition_info(phys_dp);
printf("\n");
printf(" -- disk_partitions (all) \n");
all_dp = disk_partitions(false);
if (!all_dp) {
printf("Aborting\n");
return;
}
printf("Partitions : %" PRIu32 "\n", all_dp->nitems);
for (i = 0; i < all_dp->nitems; i++) {
printf("%s %s %s %s\n", all_dp->partitions[i].device,
all_dp->partitions[i].mountpoint, all_dp->partitions[i].fstype,
all_dp->partitions[i].opts);
}
free_disk_partition_info(all_dp);
printf("\n");
}
void test_diskiocounters() {
DiskIOCounterInfo *d;
DiskIOCounters *dp;
d = disk_io_counters();
if (!d) {
printf("Aborting");
return;
}
printf(" -- disk_io_counters \n");
dp = d->iocounters;
uint32_t i;
for (i = 0; i < d->nitems; i++) {
printf("%s: \tread_count=%" PRIu64 ", write_count=%" PRIu64 ", \n"
"\tread_bytes=%" PRIu64 ", write_bytes=%" PRIu64 ", \n"
"\tread_time=%" PRIu64 ", write_time=%" PRIu64 "\n",
dp->name, dp->reads, dp->writes, dp->readbytes, dp->writebytes,
dp->readtime, dp->writetime);
dp++;
}
free_disk_iocounter_info(d);
printf("\n");
}
void test_netiocounters() {
NetIOCounterInfo *n;
NetIOCounters *dp;
uint32_t i;
n = net_io_counters();
dp = n->iocounters;
printf(" -- net_io_counters (interface count: %" PRIu32 ")\n", n->nitems);
for (i = 0; i < n->nitems; i++) {
printf("%s: bytes_sent=%" PRIu64 " bytes_rec=%" PRIu64
" packets_sen=%" PRIu64 " packets_rec=%" PRIu64 " "
"erri=%" PRIu64 " errou=%" PRIu64 " dropi=%" PRIu64
" dropou=%" PRIu64 " \n",
dp->name, dp->bytes_sent, dp->bytes_recv, dp->packets_sent,
dp->packets_recv, dp->errin, dp->errout, dp->dropin, dp->dropout);
dp++;
}
free_net_iocounter_info(n);
printf("\n");
}
void test_getusers() {
UsersInfo *r;
uint32_t i;
printf(" -- users \n");
r = get_users();
if (!r) {
printf("Failed \n");
return;
}
printf("Total: %" PRIu32 "\n", r->nitems);
printf("Name\tTerminal Host\tStarted\n");
for (i = 0; i < r->nitems; i++) {
printf("%s\t%s\t %s\t%.1f\n", r->users[i].username, r->users[i].tty,
r->users[i].hostname, r->users[i].tstamp);
}
free_users_info(r);
printf("\n");
}
void test_boottime() {
uint32_t t = get_boot_time();
printf(" -- boot_time \n");
if (t == UINT32_MAX) {
printf("Aborting\n");
return;
}
printf("%" PRIu32 "\n\n", t);
}
void test_virtualmeminfo() {
VmemInfo r;
// Empty out to prevent garbage in platform-specific fields
memset(&r, 0, sizeof(VmemInfo));
if (!virtual_memory(&r)) {
printf("Aborting\n");
return;
}
printf(" -- virtual_memory\n");
printf("Total: %" PRIu64 "\n", r.total);
printf("Available: %" PRIu64 "\n", r.available);
printf("Percent: %.1f\n", r.percent);
printf("Used: %" PRIu64 "\n", r.used);
printf("Free: %" PRIu64 "\n", r.free);
printf("Active: %" PRIu64 "\n", r.active);
printf("Inactive: %" PRIu64 "\n", r.inactive);
printf("Buffers: %" PRIu64 "\n", r.buffers);
printf("Cached: %" PRIu64 "\n", r.cached);
printf("Wired: %" PRIu64 "\n", r.wired);
printf("\n");
}
void test_swap() {
SwapMemInfo r;
if (!swap_memory(&r)) {
printf("Aborting\n");
return;
}
printf(" -- swap_memory\n");
printf("Total: %" PRIu64 "\n", r.total);
printf("Used: %" PRIu64 "\n", r.used);
printf("Free: %" PRIu64 "\n", r.free);
printf("Percent: %.1f\n", r.percent);
printf("Sin: %" PRIu64 "\n", r.sin);
printf("Sout: %" PRIu64 "\n", r.sout);
printf("\n");
}
void test_cpu_times() {
CpuTimes *r;
r = cpu_times(false);
if (!r) {
printf("Aborting\n");
return;
}
printf(" -- cpu_times\n");
printf("User: %.1lf;", r->user);
printf(" Nice: %.1lf;", r->nice);
printf(" System: %.1lf;", r->system);
printf(" Idle: %.1lf;", r->idle);
printf(" IOWait: %.1lf;", r->iowait);
printf(" IRQ: %.1lf;", r->irq);
printf(" SoftIRQ: %.1lf;", r->softirq);
printf(" Steal: %.1lf;", r->steal);
printf(" Guest: %.1lf;", r->guest);
printf(" Guest nice: %.1lf\n", r->guest_nice);
printf("\n\n");
free(r);
}
void test_cpu_times_percpu() {
CpuTimes *r, *c;
uint32_t ncpus = cpu_count(true);
c = r = cpu_times(true);
if (!r) {
printf("Aborting\n");
return;
}
printf(" -- cpu_times_percpu\n");
for (uint32_t i = 0; i < ncpus; i++) {
printf("CPU %" PRIu32 " :: ", i + 1);
printf(" Usr: %.1lf;", c->user);
printf(" Nice: %.1lf;", c->nice);
printf(" Sys: %.1lf;", c->system);
printf(" Idle: %.1lf;", c->idle);
printf(" IOWait: %.1lf;", c->iowait);
printf(" IRQ: %.1lf;", c->irq);
printf(" SoftIRQ: %.1lf;", c->softirq);
printf(" Steal: %.1lf;", c->steal);
printf(" Guest: %.1lf;", c->guest);
printf(" Guest nice: %.1lf\n", c->guest_nice);
printf("\n");
c++;
}
printf("\n");
free(r);
}
void test_cpu_util_percent() {
CpuTimes *info;
double *utilisation;
info = cpu_times(false);
if (!info) {
printf("Aborting\n");
return;
}
usleep(100000);
utilisation = cpu_util_percent(false, info);
printf(" -- cpu_util_percent\n");
printf("%.1f\n", *utilisation);
printf("\n");
free(utilisation);
free(info);
}
void test_cpu_util_percent_percpu() {
CpuTimes *info;
double *percentages;
uint32_t ncpus = cpu_count(true);
info = cpu_times(1);
if (!info) {
printf("Aborting\n");
return;
}
usleep(100000);
percentages = cpu_util_percent(1, info);
printf(" -- cpu_util_percent_percpu\n");
for (uint32_t i = 0; i < ncpus; i++) {
printf("Cpu #%" PRIu32 " : %.1f\n", i, percentages[i]);
}
printf("\n");
free(percentages);
free(info);
}
void test_cpu_times_percent() {
CpuTimes *info;
CpuTimes *ret;
info = cpu_times(false);
if (!info) {
printf("Aborting\n");
return;
}
usleep(100000);
ret = cpu_times_percent(false, info); /* Actual percentages since last call */
if (!ret) {
printf("Error while computing utilisation percentage\n");
return;
}
printf(" -- cpu_times_percent\n");
printf("CPU times as percentage of total (0.1 second sample)\n");
printf("Usr: %.1lf;", ret->user);
printf(" Nice: %.1lf;", ret->nice);
printf(" Sys: %.1lf;", ret->system);
printf(" Idle: %.1lf;", ret->idle);
printf(" IOWait: %.1lf;", ret->iowait);
printf(" IRQ: %.1lf;", ret->irq);
printf(" SoftIRQ: %.1lf;", ret->softirq);
printf(" Steal: %.1lf;", ret->steal);
printf(" Guest: %.1lf;", ret->guest);
printf(" Guest nice: %.1lf\n", ret->guest_nice);
printf("\n");
free(info);
free(ret);
}
void test_cpu_times_percent_percpu() {
CpuTimes *info, *last, *r;
uint32_t ncpus = cpu_count(true);
last = cpu_times(true);
if (!last) {
printf("Aborting\n");
return;
}
usleep(100000);
r = info =
cpu_times_percent(true, last); /* Actual percentages since last call */
if (!info) {
printf("Error while computing utilisation percentage\n");
return;
}
printf(" -- cpu_times_percent_percpu\n");
printf("CPU times as percentage of total per CPU (0.1 second sample)\n");
for (uint32_t i = 0; i < ncpus; i++) {
printf("CPU %" PRIu32 " :: ", i + 1);
printf("Usr: %.1lf;", info->user);
printf(" Nice: %.1lf;", info->nice);
printf(" Sys: %.1lf;", info->system);
printf(" Idle: %.1lf;", info->idle);
printf(" IOWait: %.1lf;", info->iowait);
printf(" IRQ: %.1lf;", info->irq);
printf(" SoftIRQ: %.1lf;", info->softirq);
printf(" Steal: %.1lf;", info->steal);
printf(" Guest: %.1lf;", info->guest);
printf(" Guest nice: %.1lf\n", info->guest_nice);
info++;
}
printf("\n");
free(r);
free(last);
}
void test_cpu_count() {
uint32_t logical;
uint32_t physical;
logical = cpu_count(true);
physical = cpu_count(false);
printf(" -- cpu_count \n");
if (logical == UINT32_MAX || physical == UINT32_MAX) {
printf("Aborting\n");
return;
}
printf("Logical : %" PRIu32 "\nPhysical : %" PRIu32 "\n", logical, physical);
printf("\n");
}
void test_pid_exists() {
pid_t pid = getpid();
printf(" -- pid_exists \n");
if (pid_exists(pid))
printf("pid %" PRId32 " exists\n", pid);
else {
printf("pid %" PRId32 " does not exist\n", pid);
}
printf("\n");
}
void test_process() {
pid_t pid = getpid();
Process *process = get_process(pid);
printf(" -- process \n");
printf("pid %" PRId32 "\n", process->pid);
printf("ppid %" PRId32 "\n", process->ppid);
printf("name %s\n", process->name);
printf("exe %s\n", process->exe);
printf("cmdline %s\n", process->cmdline);
printf("Real uid %" PRIu32 "\n", process->uid);
printf("Effective uid %" PRIu32 "\n", process->euid);
printf("Saved uid %" PRIu32 "\n", process->suid);
printf("Real gid %" PRIu32 "\n", process->gid);
printf("Effective gid %" PRIu32 "\n", process->egid);
printf("Saved gid %" PRIu32 "\n", process->sgid);
printf("Username %s\n", process->username);
printf("Terminal %s\n", process->terminal);
printf("\n");
free_process(process);
}
int main(void) {
test_diskusage();
test_diskpartitioninfo();
test_diskiocounters();
test_netiocounters();
test_getusers();
test_boottime();
test_virtualmeminfo();
test_swap();
test_cpu_times();
test_cpu_times_percpu();
test_cpu_util_percent();
test_cpu_util_percent_percpu();
test_cpu_times_percent();
test_cpu_times_percent_percpu();
test_cpu_count();
test_pid_exists();
test_process();
}