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 <jasper@openbsd.org> 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.
This commit is contained in:
Pawel "l0ner" Soltys 2015-02-16 18:38:21 +01:00
parent 4ca1ab01e4
commit 91594b7a5a
2 changed files with 4 additions and 19 deletions

@ -28,13 +28,8 @@
float cpu_percentage( unsigned int cpu_usage_delay ) float cpu_percentage( unsigned int cpu_usage_delay )
{ {
#if __x86_64__ || __ppc64__ u_long load1[CPUSTATES];
u_int64_t load1[CPUSTATES]; u_long load2[CPUSTATES];
u_int64_t load2[CPUSTATES];
#else
u_int32_t load1[CPUSTATES];
u_int32_t load2[CPUSTATES];
#endif
GETSYSCTL( "kern.cp_time", load1 ); GETSYSCTL( "kern.cp_time", load1 );
usleep( cpu_usage_delay ); usleep( cpu_usage_delay );

@ -42,18 +42,8 @@ float cpu_percentage( unsigned int cpu_usage_delay )
{ {
int cpu_ctl[] = { CTL_KERN, KERN_CPTIME }; int cpu_ctl[] = { CTL_KERN, KERN_CPTIME };
// on 64bit systems KERN_CPTIME gets reported as 64bit u_long load1[CPUSTATES];
// uint. Detect 64bit system and define array to hold the u_long load2[CPUSTATES];
// 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
size_t size = sizeof( load1 ); size_t size = sizeof( load1 );