From 624bd7a23ba1b7d6ac2b2007033381cacc0a3d18 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Fri, 12 Jan 2024 09:49:53 -0600 Subject: [PATCH] cleanup --- src/macos.c | 18 ++++++++++-------- tests/tests.c | 4 ++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/macos.c b/src/macos.c index 9a79ed6..57cd48e 100644 --- a/src/macos.c +++ b/src/macos.c @@ -2,13 +2,15 @@ #include #include #include -#include #include "lfmacos.h" +#define NS_TO_SECONDS 1000000000.0 +#define NEW_PROCESS_SENTINEL (-1.0) + ProcessData *new_ProcessData() { ProcessData *pd = malloc(sizeof(ProcessData)); - pd->last_total_consumed = -1.0; + pd->last_total_consumed = NEW_PROCESS_SENTINEL; return pd; } @@ -24,18 +26,18 @@ int update_process(pid_t pid, ProcessData *proc) { const double ns_per_tick = (double)info.numer / (double)info.denom; time(&(proc->timestamp)); - proc->total_kernel_time = (taskinfo.pti_total_system * ns_per_tick) / 1000000000.0; - proc->total_user_time = (taskinfo.pti_total_user * ns_per_tick) / 1000000000.0; + proc->total_kernel_time = (taskinfo.pti_total_system * ns_per_tick) / NS_TO_SECONDS; + proc->total_user_time = (taskinfo.pti_total_user * ns_per_tick) / NS_TO_SECONDS; proc->virtual_memory = taskinfo.pti_virtual_size; proc->resident_memory = taskinfo.pti_resident_size; - if (proc->last_total_consumed > -1.0) { - proc->percent_cpu = fabs(100.0 * - (proc->total_user_time + proc->total_kernel_time - proc->last_total_consumed) / - (proc->last_timestamp - proc->timestamp)); + if (proc->last_total_consumed != NEW_PROCESS_SENTINEL) { + time_t t = proc->timestamp - proc->last_timestamp; + proc->percent_cpu = 100.0 * (proc->total_user_time + proc->total_kernel_time - proc->last_total_consumed) / t; } else { proc->percent_cpu = 0.0; } + proc->last_timestamp = proc->timestamp; proc->last_total_consumed = proc->total_kernel_time + proc->total_user_time; diff --git a/tests/tests.c b/tests/tests.c index 60d8c7e..04c2b95 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -179,6 +179,10 @@ void test_math() { printf("Binary: %s\n", s2); printf("Decimal: %d\n", binstr_to_int(s2)); + i = 10; + i = clamp_int(i, 2, 5); + assert(i == 5); + printf("\nGenerate line from 0,0 to 2,5\n"); size_t sz = 0; Point *line = bresenham(0, 0, 2, 5, &sz);