From 80d70b7a4d5ee9a4ba79deae0e840c65ccaf4131 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Thu, 12 Feb 2015 19:59:22 +0100 Subject: [PATCH] fix for cpu systctl failing on 64 bit OpenBSD On 64bit system KERN_CPTIME systctl gets returned as 64bit uint. On 32bit system it's returned as 32bit uint. This is not documented anywhere (or maybe I've missed it). I've added preprocessor test for 64bit system. --- common/main.cc | 4 ++-- openbsd/cpu.cc | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/common/main.cc b/common/main.cc index a5fa0db..e1bd9ba 100644 --- a/common/main.cc +++ b/common/main.cc @@ -73,7 +73,7 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines, if( graph_lines > 0) { - oss << "["; + oss << " ["; oss << get_graph_by_percentage( unsigned( percentage ), graph_lines ); oss << "]"; } @@ -173,7 +173,7 @@ int main( int argc, char** argv ) return EXIT_FAILURE; } - std::cout << mem_string( use_colors ) << ' ' + std::cout << mem_string( use_colors ) << cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' ' << load_string( use_colors ); diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index 9af4eb5..fefc215 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -42,8 +42,18 @@ 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 size_t size = sizeof( load1 );