From c282341ed8cbf8091dcdecc507f5a114a3087677 Mon Sep 17 00:00:00 2001 From: Al 'netstar' Poole Date: Tue, 11 Apr 2017 16:34:05 -0500 Subject: [PATCH] Sysinfo: Make memusage and cpumonitor work better on BSD. This commits D4749 --- .../sysinfo/cpumonitor/cpumonitor_sysctl.c | 54 ++++++++++++------- .../sysinfo/memusage/memusage_sysctl.c | 23 ++++++-- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c b/src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c index 8c3a53c74..55ffd39b0 100644 --- a/src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c +++ b/src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c @@ -50,17 +50,23 @@ _cpumonitor_sysctl_getusage(Instance *inst) for (j = 0; j < CPU_STATES; j++) total += cpu[j]; - double used = total - (cpu[4]); // + cpu[2] + cpu[1] + cpu[0]); - double ratio = (float) total / 100.0; + core = eina_list_nth(inst->cfg->cpumonitor.cores, i); - double percent = ((double)used / (double)ratio); + int diff_total = total - core->total; + int diff_idle = cpu[4] - core->idle; + + if (diff_total == 0) diff_total = 1; + + double ratio = diff_total / 100.0; + long used = diff_total - diff_idle; + + int percent = used / ratio; if (percent > 100) percent = 100; else if (percent < 0) percent = 0; - core = eina_list_nth(inst->cfg->cpumonitor.cores, i); - core->percent = (int) percent; + core->percent = percent; core->total = total; core->idle = cpu[4]; @@ -86,17 +92,23 @@ _cpumonitor_sysctl_getusage(Instance *inst) for (j = 0; j < CPU_STATES; j++) total += cpu_times[j]; - //user", "nice", "system", "interrupt", "idle", NULL - long used = total - cpu_times[0] + cpu_times[1] + cpu_times[2] + cpu_times[3]; - double ratio = (double) total / 100.0; - double percent = 100.0 - ((double) used / (double) ratio); + long idle = cpu_times[4]; + + int diff_total = total - inst->cfg->cpumonitor.total; + int diff_idle = idle - inst->cfg->cpumonitor.idle; + + if (diff_total == 0) diff_total = 1; + + double ratio = diff_total / 100.0; + long used = diff_total - diff_idle; + int percent = used / ratio; if (percent > 100) percent = 100; else if (percent < 0) percent = 0; inst->cfg->cpumonitor.total = total; - inst->cfg->cpumonitor.idle = cpu_times[3]; + inst->cfg->cpumonitor.idle = idle; // cpu_times[3]; inst->cfg->cpumonitor.percent = (int) percent; } else if (ncpu > 1) @@ -114,25 +126,27 @@ _cpumonitor_sysctl_getusage(Instance *inst) for (j = 0; j < CPU_STATES - 1; j++) total += cpu_times[j]; - long used = total - (cpu_times[4]); - double ratio = (float) total / 100.0; - int percent = used / ratio; - if (percent > 100) - percent = 100; - else if (percent < 0) - percent = 0; core = eina_list_nth(inst->cfg->cpumonitor.cores, i); - core->percent = (int) percent; + int diff_total = total - core->total; + int diff_idle = cpu_times[4] - core->idle; + core->total = total; core->idle = cpu_times[4]; + if (diff_total == 0) diff_total = 1; + double ratio = diff_total / 100; + long used = diff_total - diff_idle; + int percent = used / ratio; + + core->percent = percent; + percent_all += (int) percent; total_all += total; idle_all += core->idle; } - inst->cfg->cpumonitor.total = total_all / ncpu; - inst->cfg->cpumonitor.idle = idle_all / ncpu; + inst->cfg->cpumonitor.total = (total_all / ncpu); + inst->cfg->cpumonitor.idle = (idle_all / ncpu); inst->cfg->cpumonitor.percent = (percent_all / ncpu) + (percent_all % ncpu); } #endif diff --git a/src/modules/sysinfo/memusage/memusage_sysctl.c b/src/modules/sysinfo/memusage/memusage_sysctl.c index 1694861d5..db2c7e587 100644 --- a/src/modules/sysinfo/memusage/memusage_sysctl.c +++ b/src/modules/sysinfo/memusage/memusage_sysctl.c @@ -28,6 +28,13 @@ _sysctlfromname(const char *name, void *mib, int depth, size_t *len) } #endif +#if defined(__OpenBSD__) +void _memsize_bytes_to_kb(unsigned long *bytes) +{ + *bytes = (unsigned int) *bytes >> 10; +} +#endif + void _memusage_sysctl_getusage(unsigned long *mem_total, unsigned long *mem_used, unsigned long *mem_cached, @@ -139,9 +146,17 @@ swap_out: if (swdev) free(swdev); *mem_total /= 1024; - *mem_cached = (uvmexp.pagesize * bcstats.numbufpages) / 1024; - *mem_used = (uvmexp.active * uvmexp.pagesize) / 1024 ; - *mem_buffers = (uvmexp.pagesize * (uvmexp.npages - uvmexp.free)) / 1024; - *mem_shared = (uvmexp.pagesize * uvmexp.wired) / 1024; + + *mem_cached = (uvmexp.pagesize * bcstats.numbufpages); + _memsize_bytes_to_kb(mem_cached); + + *mem_used = (uvmexp.active * uvmexp.pagesize); + _memsize_bytes_to_kb(mem_used); + + *mem_buffers = (uvmexp.pagesize * (uvmexp.npages - uvmexp.free)); + _memsize_bytes_to_kb(mem_buffers); + + *mem_shared = (uvmexp.pagesize * uvmexp.wired); + _memsize_bytes_to_kb(mem_shared); #endif }