]> git.proxmox.com Git - rustc.git/blob - vendor/sysinfo-0.26.7/examples/simple.c
New upstream version 1.73.0+dfsg1
[rustc.git] / 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.
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <pthread.h>
8 #include "sysinfo.h"
9
10 void print_process(CProcess process) {
11 RString exe = sysinfo_process_get_executable_path(process);
12 printf("process[%d]: parent: %d,\n"
13 " cpu_usage: %f,\n"
14 " memory: %ld,\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),
22 exe);
23 sysinfo_rstring_free(exe);
24 }
25
26 void check_tasks(CSystem system) {
27 #ifdef __linux__
28 bool task_loop(pid_t pid, CProcess process, void *data) {
29 (void)data;
30 printf(" ");
31 print_process(process);
32 return true;
33 }
34
35 void *sleeping_func(void *data) {
36 sleep(3);
37 return data;
38 }
39 pthread_t thread;
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));
46 #else
47 (void)system;
48 #endif
49 }
50
51 bool process_loop(pid_t pid, CProcess process, void *data) {
52 unsigned int *i = data;
53
54 print_process(process);
55 *i += 1;
56 return *i < 10;
57 }
58
59 int main() {
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;
71 float *procs = NULL;
72 sysinfo_get_cpus_usage(system, &len, &procs);
73 while (i < len) {
74 printf("CPU #%d usage: %f%%\n", i, procs[i]);
75 i += 1;
76 }
77 free(procs);
78
79 // processes part
80 i = 0;
81 printf("For a total of %ld processes.\n", sysinfo_get_processes(system, process_loop, &i));
82 check_tasks(system);
83 // we can now free the CSystem object.
84 sysinfo_destroy(system);
85 return 0;
86 }