From 4cf0dad920a35e0d794f3b6f009b3e8303484a06 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 12 May 2023 17:08:39 +0200 Subject: [PATCH 1/2] use the correct CP_* definitions and number of CPUSTATES on OpenBSD --- common/cpu.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/common/cpu.h b/common/cpu.h index 233b3be..a0b5c25 100644 --- a/common/cpu.h +++ b/common/cpu.h @@ -28,12 +28,15 @@ #define CP_IDLE 2 #define CP_NICE 3 #define CP_STATES 4 +#elif defined(__OpenBSD__) + #include + #define CP_STATES CPUSTATES #else #define CP_USER 0 #define CP_NICE 1 #define CP_SYS 2 - #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) + #if defined(__FreeBSD__) || defined(__NetBSD__) // *BSD or OSX #define CP_INTR 3 #define CP_IDLE 4 From 525c6a28b214aaaf2c9c187cd9d6caac9eaa1ec7 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 12 May 2023 17:08:52 +0200 Subject: [PATCH 2/2] cast the return value of get_cpu_count() and fix cpu usage calculation --- openbsd/cpu.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index 4407e8c..2352640 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -24,7 +24,7 @@ #include "error.h" #include "cpu.h" -uint8_t get_cpu_count() +uint32_t get_cpu_count() { int cpu_count = 1; // default to 1 int mib[2] = { CTL_HW, HW_NCPUONLINE }; @@ -35,7 +35,7 @@ uint8_t get_cpu_count() error( "sysctl: error getting cpu count" ); } - return cpu_count; + return static_cast( cpu_count ); } float cpu_percentage( unsigned int cpu_usage_delay ) @@ -55,6 +55,8 @@ float cpu_percentage( unsigned int cpu_usage_delay ) usleep( cpu_usage_delay ); + size = sizeof( load2 ); + // update cpu times if( sysctl( cpu_ctl, 2, &load2, &size, NULL, 0 ) < 0 ) {