]>
git.proxmox.com Git - rustc.git/blob - vendor/sysinfo-0.26.7/examples/simple.c
1 // Take a look at the license at the top of the repository in the LICENSE file.
10 void print_process(CProcess process
) {
11 RString exe
= sysinfo_process_get_executable_path(process
);
12 printf("process[%d]: parent: %d,\n"
15 " virtual memory: %ld,\n"
16 " executable path: '%s'\n",
17 sysinfo_process_get_pid(process
),
18 sysinfo_process_get_parent_pid(process
),
19 sysinfo_process_get_cpu_usage(process
),
20 sysinfo_process_get_memory(process
),
21 sysinfo_process_get_virtual_memory(process
),
23 sysinfo_rstring_free(exe
);
26 void check_tasks(CSystem system
) {
28 bool task_loop(pid_t pid
, CProcess process
, void *data
) {
31 print_process(process
);
35 void *sleeping_func(void *data
) {
40 pthread_create(&thread
, NULL
, sleeping_func
, NULL
);
41 sysinfo_refresh_system(system
);
42 CProcess process
= sysinfo_get_process_by_pid(system
, getpid());
43 printf("\n== Task(s) for current process: ==\n");
44 print_process(process
);
45 printf("Got %ld task(s)\n", sysinfo_process_get_tasks(process
, task_loop
, NULL
));
51 bool process_loop(pid_t pid
, CProcess process
, void *data
) {
52 unsigned int *i
= data
;
54 print_process(process
);
60 CSystem system
= sysinfo_init();
61 sysinfo_refresh_all(system
);
62 printf("total memory: %ld\n", sysinfo_get_total_memory(system
));
63 printf("free memory: %ld\n", sysinfo_get_free_memory(system
));
64 printf("used memory: %ld\n", sysinfo_get_used_memory(system
));
65 printf("total swap: %ld\n", sysinfo_get_total_swap(system
));
66 printf("free swap: %ld\n", sysinfo_get_free_swap(system
));
67 printf("used swap: %ld\n", sysinfo_get_used_swap(system
));
68 printf("networks received: %ld\n", sysinfo_get_networks_received(system
));
69 printf("networks transmitted: %ld\n", sysinfo_get_networks_transmitted(system
));
70 unsigned int len
= 0, i
= 0;
72 sysinfo_get_cpus_usage(system
, &len
, &procs
);
74 printf("CPU #%d usage: %f%%\n", i
, procs
[i
]);
81 printf("For a total of %ld processes.\n", sysinfo_get_processes(system
, process_loop
, &i
));
83 // we can now free the CSystem object.
84 sysinfo_destroy(system
);