From 91594b7a5a7f82695a900d14fa487be66899d1c7 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 16 Feb 2015 18:38:21 +0100 Subject: [PATCH] Use u_long for storing cpu stats instead of detecting architecture As suggested to me by "Jasper Lievisse Adriaanse" in an email: On 2015-02-16 09:02 Jasper Lievisse Adriaanse wrote: > You can actually use 'long' instead of juggling between 64 and 32 bit return > types. I've impemented something similiar for libgtop years ago and never had > any issues when using 'long' for both 64 and 32 platforms. Here's the > refernce: https://git.gnome.org/browse/libgtop/tree/sysdeps/openbsd/cpu.c#n62 This is a better idea than what I've implemented. Also this should resolve eventual occurrence of "unable to get cpu stats" problem on 64bit platforms we do not detect. --- freebsd/cpu.cc | 9 ++------- openbsd/cpu.cc | 14 ++------------ 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/freebsd/cpu.cc b/freebsd/cpu.cc index de865c9..a9f056e 100644 --- a/freebsd/cpu.cc +++ b/freebsd/cpu.cc @@ -28,13 +28,8 @@ float cpu_percentage( unsigned int cpu_usage_delay ) { -#if __x86_64__ || __ppc64__ - u_int64_t load1[CPUSTATES]; - u_int64_t load2[CPUSTATES]; -#else - u_int32_t load1[CPUSTATES]; - u_int32_t load2[CPUSTATES]; -#endif + u_long load1[CPUSTATES]; + u_long load2[CPUSTATES]; GETSYSCTL( "kern.cp_time", load1 ); usleep( cpu_usage_delay ); diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index fefc215..223f51b 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -42,18 +42,8 @@ float cpu_percentage( unsigned int cpu_usage_delay ) { int cpu_ctl[] = { CTL_KERN, KERN_CPTIME }; - // on 64bit systems KERN_CPTIME gets reported as 64bit - // uint. Detect 64bit system and define array to hold the - // stats accordingly. - // NOTE: the following test may need to be extended to cover - // more 64bit platforms. -#if __x86_64__ || __ppc64__ - u_int64_t load1[CPUSTATES]; - u_int64_t load2[CPUSTATES]; -#else - u_int32_t load1[CPUSTATES]; - u_int32_t load2[CPUSTATES]; -#endif + u_long load1[CPUSTATES]; + u_long load2[CPUSTATES]; size_t size = sizeof( load1 );