-
Notifications
You must be signed in to change notification settings - Fork 1
/
pid_usage.c
60 lines (49 loc) · 1.73 KB
/
pid_usage.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
#include "headers/process_args.h"
#include "headers/linux_get_process_info.h"
int main(int argc, char **argv) {
// defaults
struct arguments arguments = { 0 };
arguments.pid = 0;
arguments.interval = 1.5;
arguments.repeat = 1;
arguments.use_header = true;
// parse and assign arguments
get_args(argc, argv, &arguments);
pid_t pid = arguments.pid;
double interval = arguments.interval;
unsigned int repeat_times = arguments.repeat;
bool use_header = arguments.use_header;
// declarations
struct process_cpu_usage cpu_usage;
struct timespec last_measured_at;
double last_measured_total_usage;
double cpu_percent;
// First Call
get_process_info(pid, &cpu_usage);
last_measured_total_usage = cpu_usage.total_usage;
last_measured_at = cpu_usage.measured_at;
usleep(0.1 * 1000 * 1000);
// Loop for repeat_times
int i = 0;
while(i < repeat_times) {
// Wait for all iterations except the first
if(i!=0) usleep(interval * 1000 * 1000);
// Calculate CPU Usage
get_process_info(pid, &cpu_usage);
cpu_percent = 100. * (cpu_usage.total_usage - last_measured_total_usage) /
get_time_diff(last_measured_at, cpu_usage.measured_at);
last_measured_total_usage = cpu_usage.total_usage;
last_measured_at = cpu_usage.measured_at;
// Header CSV
if(i == 0)
if(use_header)
printf("CPU %%, Virtual Memory, Resident Memory\n");
// Values CSV
printf("%.1f%%, %zu MiB, %zu MiB\n",
cpu_percent,
cpu_usage.virtual_memory/(1024*1024),
cpu_usage.resident_memory/(1024*1024));
i++;
}
return 0;
}