diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/sysinfo/examples/simple.c | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/sysinfo/examples/simple.c')
-rw-r--r-- | vendor/sysinfo/examples/simple.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/vendor/sysinfo/examples/simple.c b/vendor/sysinfo/examples/simple.c new file mode 100644 index 000000000..28a23beef --- /dev/null +++ b/vendor/sysinfo/examples/simple.c @@ -0,0 +1,86 @@ +// Take a look at the license at the top of the repository in the LICENSE file. + +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <unistd.h> +#include <pthread.h> +#include "sysinfo.h" + +void print_process(CProcess process) { + RString exe = sysinfo_process_get_executable_path(process); + printf("process[%d]: parent: %d,\n" + " cpu_usage: %f,\n" + " memory: %ld,\n" + " virtual memory: %ld,\n" + " executable path: '%s'\n", + sysinfo_process_get_pid(process), + sysinfo_process_get_parent_pid(process), + sysinfo_process_get_cpu_usage(process), + sysinfo_process_get_memory(process), + sysinfo_process_get_virtual_memory(process), + exe); + sysinfo_rstring_free(exe); +} + +void check_tasks(CSystem system) { +#ifdef __linux__ + bool task_loop(pid_t pid, CProcess process, void *data) { + (void)data; + printf(" "); + print_process(process); + return true; + } + + void *sleeping_func(void *data) { + sleep(3); + return data; + } + pthread_t thread; + pthread_create(&thread, NULL, sleeping_func, NULL); + sysinfo_refresh_system(system); + CProcess process = sysinfo_get_process_by_pid(system, getpid()); + printf("\n== Task(s) for current process: ==\n"); + print_process(process); + printf("Got %ld task(s)\n", sysinfo_process_get_tasks(process, task_loop, NULL)); +#else + (void)system; +#endif +} + +bool process_loop(pid_t pid, CProcess process, void *data) { + unsigned int *i = data; + + print_process(process); + *i += 1; + return *i < 10; +} + +int main() { + CSystem system = sysinfo_init(); + sysinfo_refresh_all(system); + printf("total memory: %ld\n", sysinfo_get_total_memory(system)); + printf("free memory: %ld\n", sysinfo_get_free_memory(system)); + printf("used memory: %ld\n", sysinfo_get_used_memory(system)); + printf("total swap: %ld\n", sysinfo_get_total_swap(system)); + printf("free swap: %ld\n", sysinfo_get_free_swap(system)); + printf("used swap: %ld\n", sysinfo_get_used_swap(system)); + printf("networks received: %ld\n", sysinfo_get_networks_received(system)); + printf("networks transmitted: %ld\n", sysinfo_get_networks_transmitted(system)); + unsigned int len = 0, i = 0; + float *procs = NULL; + sysinfo_get_cpus_usage(system, &len, &procs); + while (i < len) { + printf("CPU #%d usage: %f%%\n", i, procs[i]); + i += 1; + } + free(procs); + + // processes part + i = 0; + printf("For a total of %ld processes.\n", sysinfo_get_processes(system, process_loop, &i)); + check_tasks(system); + // we can now free the CSystem object. + sysinfo_destroy(system); + return 0; +} |