From 91594b7a5a7f82695a900d14fa487be66899d1c7 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 16 Feb 2015 18:38:21 +0100 Subject: [PATCH 1/4] 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 ); From 1512a0bda07e292cb30b1a75149ac8d199794f49 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 16 Feb 2015 23:29:27 +0100 Subject: [PATCH 2/4] NetBSD port. NetBSD port shares with FreeBSD: - load_string() - getsysctl() With OpenBSD: - error() --- CMakeLists.txt | 2 ++ {openbsd => common}/error.h | 3 +- common/main.cc | 36 +++++++++++---------- freebsd/getsysctl.h | 4 ++- netbsd/cpu.cc | 57 +++++++++++++++++++++++++++++++++ netbsd/cpu.h | 31 ++++++++++++++++++ netbsd/memory.cc | 64 +++++++++++++++++++++++++++++++++++++ netbsd/memory.h | 26 +++++++++++++++ 8 files changed, 205 insertions(+), 18 deletions(-) rename {openbsd => common}/error.h (94%) create mode 100644 netbsd/cpu.cc create mode 100644 netbsd/cpu.h create mode 100644 netbsd/memory.cc create mode 100644 netbsd/memory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b49801..372229e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,8 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") if(CMAKE_SYSTEM_VERSION VERSION_LESS 5.7) add_definitions(-DOPENBSD_WORKAROUND=1) endif() +elseif(CMAKE_SYSTEM_NAME MATCHES "NetBSD") + set(METER_SOURCES "netbsd/memory.cc" "netbsd/cpu.cc" "freebsd/load.cc") else() message(FATAL_ERROR "Cannot be compiled on this system") endif() diff --git a/openbsd/error.h b/common/error.h similarity index 94% rename from openbsd/error.h rename to common/error.h index b4c6a7e..f771c41 100644 --- a/openbsd/error.h +++ b/common/error.h @@ -23,6 +23,7 @@ #include #include #include // strerror +#include // exit() inline void error( const char * error ) { @@ -30,7 +31,7 @@ inline void error( const char * error ) using std::endl; cerr << error << ": " << strerror( errno ) << endl; - exit( 23 ); + exit( EXIT_FAILURE ); } #endif diff --git a/common/main.cc b/common/main.cc index e1bd9ba..33c5d8f 100644 --- a/common/main.cc +++ b/common/main.cc @@ -31,25 +31,29 @@ #if defined(__APPLE__) && defined(__MACH__) // Apple osx system - #include "osx/cpu.h" - #include "osx/memory.h" - #include "osx/load.h" + #include "osx/cpu.h" + #include "osx/memory.h" + #include "osx/load.h" #elif defined(__FreeBSD__) || defined(__NetBSD__) // BSD system - #define BSD_BASED 1 - #include "freebsd/cpu.h" - #include "freebsd/load.h" - #include "freebsd/memory.h" -#elif defined(__OpenBSD) - #define BSD_BASED 1 - #include "freebsd/cpu.h" - #include "freebsd/load.h" - #include "freebsd/memory.h" + #define BSD_BASED 1 + #include "freebsd/cpu.h" + #include "freebsd/load.h" + #include "freebsd/memory.h" +#elif defined(__OpenBSD__) + #define BSD_BASED 1 + #include "freebsd/cpu.h" + #include "freebsd/load.h" + #include "freebsd/memory.h" +#elif defined(__NetBSD__) + #include "freebsd/cpu.h" + #include "freebsd/load.h" + #include "netbsd/memory.h" #else - // assume linux system - #include "linux/cpu.h" - #include "linux/memory.h" - #include "linux/load.h" + // assume linux system + #include "linux/cpu.h" + #include "linux/memory.h" + #include "linux/load.h" #endif std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines, diff --git a/freebsd/getsysctl.h b/freebsd/getsysctl.h index aecd004..e97f9ee 100644 --- a/freebsd/getsysctl.h +++ b/freebsd/getsysctl.h @@ -26,6 +26,8 @@ #include #include #include +#include //exit() +#include //strerror() #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) static inline void getsysctl( const char *name, void *ptr, size_t len ) @@ -36,7 +38,7 @@ static inline void getsysctl( const char *name, void *ptr, size_t len ) { std::cerr << "sysctl(" << name << "...) failed: " << strerror( errno ) << std::endl; - exit( 23 ); + exit( EXIT_FAILURE ); } if( nlen != len ) diff --git a/netbsd/cpu.cc b/netbsd/cpu.cc new file mode 100644 index 0000000..2afab53 --- /dev/null +++ b/netbsd/cpu.cc @@ -0,0 +1,57 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2013 Justin Crawford + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Based on: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c +// Based on: Apple.cpp for load_string/mem_string and apple's documentation + +#include +#include // usleep + +#include "../freebsd/getsysctl.h" +#include "cpu.h" + +float cpu_percentage( unsigned int cpu_usage_delay ) +{ + u_int64_t load1[CPUSTATES]; + u_int64_t load2[CPUSTATES]; + + GETSYSCTL( "kern.cp_time", load1 ); + usleep( cpu_usage_delay ); + GETSYSCTL( "kern.cp_time", load2 ); + + // Current load times + unsigned long long current_user = load1[CP_USER]; + unsigned long long current_system = load1[CP_SYS]; + unsigned long long current_nice = load1[CP_NICE]; + unsigned long long current_idle = load1[CP_IDLE]; + // Next load times + unsigned long long next_user = load2[CP_USER]; + unsigned long long next_system = load2[CP_SYS]; + unsigned long long next_nice = load2[CP_NICE]; + unsigned long long next_idle = load2[CP_IDLE]; + // Difference between the two + unsigned long long diff_user = next_user - current_user; + unsigned long long diff_system = next_system - current_system; + unsigned long long diff_nice = next_nice - current_nice; + unsigned long long diff_idle = next_idle - current_idle; + + return static_cast( diff_user + diff_system + diff_nice ) / + static_cast( diff_user + diff_system + diff_nice + diff_idle ) * + 100.0; +} diff --git a/netbsd/cpu.h b/netbsd/cpu.h new file mode 100644 index 0000000..3840650 --- /dev/null +++ b/netbsd/cpu.h @@ -0,0 +1,31 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CPU_H_ +#define CPU_H_ + +#define CP_USER 0 +#define CP_NICE 1 +#define CP_SYS 2 +#define CP_INTR 3 +#define CP_IDLE 4 +#define CPUSTATES 5 + +float cpu_percentage( unsigned ); + +#endif diff --git a/netbsd/memory.cc b/netbsd/memory.cc new file mode 100644 index 0000000..d27bb44 --- /dev/null +++ b/netbsd/memory.cc @@ -0,0 +1,64 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include // uvmexp struct + +#include "error.h" +#include "luts.h" +#include "conversions.h" +#include "memory.h" + +std::string mem_string( bool use_colors = false ) +{ + std::ostringstream oss; + + // get vm memory stats + static int vm_totalmem[] = { CTL_VM, VM_UVMEXP2 }; + struct uvmexp_sysctl mem; + size_t size = sizeof( mem ); + if( sysctl( vm_totalmem, 2, &mem, &size, NULL, 0 ) < 0 ) + { + error( "memory: error getting vm memory stats" ); + } + + int64_t total_mem = ( mem.npages << mem.pageshift ); + int64_t used_mem = + ( mem.active + mem.wired - mem.filepages ) << mem.pageshift; + + if( use_colors ) + { + oss << mem_lut[( 100 * used_mem ) / total_mem]; + } + + // add 1 to used which gets lost somewhere along conversions + oss << convert_unit( used_mem, MEGABYTES ) + << '/' << convert_unit( total_mem, MEGABYTES ) << "MB"; + + if( use_colors ) + { + oss << "#[fg=default,bg=default]"; + } + + return oss.str(); + +} diff --git a/netbsd/memory.h b/netbsd/memory.h new file mode 100644 index 0000000..3a123bb --- /dev/null +++ b/netbsd/memory.h @@ -0,0 +1,26 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MEMORY_H_ +#define MEMORY_H_ + +#include + +std::string mem_string( bool ); + +#endif From 240752d800d98c4a3dd9a7048ea1d7d1b0c5f600 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Thu, 19 Feb 2015 19:47:45 +0100 Subject: [PATCH 3/4] Code Refactornig Since the headers for cpu, memory and load functions are virtually the same for all platforms, I've decided to move them into common/ dir and do some refacotring: * removed per-platform header files * implemented get_cpu_count() function across all platforms. We are using it cpu on every platform, yet not on every one this was implemented as a separate function. * removed platform detection through preprocessor from main: we don't need this there anymore, since the headers are common for all platforms. CMake will handle setting of correct source files for us now. * Unified used defines for CPU states across all platforms and made linux use them. Added some platform detection to cpu.h in order to set them correctly across the platforms. * moved getsysctl.h to common/ dir, since it's used on Net and Free BSD, and thus become a common include. --- {openbsd => common}/cpu.h | 28 ++++++++++++++++++++++------ {freebsd => common}/getsysctl.h | 0 {freebsd => common}/load.h | 0 common/main.cc | 29 +++-------------------------- {freebsd => common}/memory.h | 0 freebsd/cpu.cc | 12 ++++++++++-- freebsd/cpu.h | 31 ------------------------------- freebsd/load.cc | 5 ++--- linux/cpu.cc | 14 +++++++++++--- linux/cpu.h | 24 ------------------------ linux/load.cc | 6 ++---- linux/load.h | 26 -------------------------- linux/memory.h | 26 -------------------------- netbsd/cpu.cc | 14 +++++++++++--- netbsd/cpu.h | 31 ------------------------------- netbsd/memory.h | 26 -------------------------- openbsd/cpu.cc | 4 ++-- openbsd/load.cc | 5 +---- openbsd/load.h | 26 -------------------------- openbsd/memory.h | 26 -------------------------- osx/cpu.cc | 21 +++++++++++++-------- osx/cpu.h | 23 ----------------------- osx/load.cc | 6 ++---- osx/load.h | 25 ------------------------- osx/memory.h | 26 -------------------------- 25 files changed, 79 insertions(+), 355 deletions(-) rename {openbsd => common}/cpu.h (64%) rename {freebsd => common}/getsysctl.h (100%) rename {freebsd => common}/load.h (100%) rename {freebsd => common}/memory.h (100%) delete mode 100644 freebsd/cpu.h delete mode 100644 linux/cpu.h delete mode 100644 linux/load.h delete mode 100644 linux/memory.h delete mode 100644 netbsd/cpu.h delete mode 100644 netbsd/memory.h delete mode 100644 openbsd/load.h delete mode 100644 openbsd/memory.h delete mode 100644 osx/cpu.h delete mode 100644 osx/load.h delete mode 100644 osx/memory.h diff --git a/openbsd/cpu.h b/common/cpu.h similarity index 64% rename from openbsd/cpu.h rename to common/cpu.h index 5bb6e87..4fe0b98 100644 --- a/openbsd/cpu.h +++ b/common/cpu.h @@ -21,12 +21,28 @@ #include -#define CP_USER 0 -#define CP_NICE 1 -#define CP_SYS 2 -#define CP_INTR 3 -#define CP_IDLE 4 -#define CPUSTATES 5 +#if defined(__APPLE__) && defined(__MACH__) + #define CP_USER 0 + #define CP_SYS 1 + #define CP_IDLE 2 + #define CP_NICE 3 + #define CP_STATES 4 +#else + #define CP_USER 0 + #define CP_NICE 1 + #define CP_SYS 2 + + #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) + // *BSD or OSX + #define CP_INTR 3 + #define CP_IDLE 4 + #define CP_STATES 5 + #else + //linux + #define CP_IDLE 3 + #define CP_STATES 4 + #endif +#endif float cpu_percentage( unsigned ); uint8_t get_cpu_count(); diff --git a/freebsd/getsysctl.h b/common/getsysctl.h similarity index 100% rename from freebsd/getsysctl.h rename to common/getsysctl.h diff --git a/freebsd/load.h b/common/load.h similarity index 100% rename from freebsd/load.h rename to common/load.h diff --git a/common/main.cc b/common/main.cc index 33c5d8f..f9c3264 100644 --- a/common/main.cc +++ b/common/main.cc @@ -29,32 +29,9 @@ // Tmux color lookup tables for the different metrics. #include "luts.h" -#if defined(__APPLE__) && defined(__MACH__) - // Apple osx system - #include "osx/cpu.h" - #include "osx/memory.h" - #include "osx/load.h" -#elif defined(__FreeBSD__) || defined(__NetBSD__) - // BSD system - #define BSD_BASED 1 - #include "freebsd/cpu.h" - #include "freebsd/load.h" - #include "freebsd/memory.h" -#elif defined(__OpenBSD__) - #define BSD_BASED 1 - #include "freebsd/cpu.h" - #include "freebsd/load.h" - #include "freebsd/memory.h" -#elif defined(__NetBSD__) - #include "freebsd/cpu.h" - #include "freebsd/load.h" - #include "netbsd/memory.h" -#else - // assume linux system - #include "linux/cpu.h" - #include "linux/memory.h" - #include "linux/load.h" -#endif +#include "cpu.h" +#include "memory.h" +#include "load.h" std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines, bool use_colors = false ) diff --git a/freebsd/memory.h b/common/memory.h similarity index 100% rename from freebsd/memory.h rename to common/memory.h diff --git a/freebsd/cpu.cc b/freebsd/cpu.cc index a9f056e..f6a4e62 100644 --- a/freebsd/cpu.cc +++ b/freebsd/cpu.cc @@ -26,10 +26,18 @@ #include "getsysctl.h" #include "cpu.h" +uint8_t get_cpu_cout() +{ + int32_t cpu_count = 0; + GETSYSCTL( "hw.ncpu", cpu_count ); + + return static_cast( cpu_count ); +} + float cpu_percentage( unsigned int cpu_usage_delay ) { - u_long load1[CPUSTATES]; - u_long load2[CPUSTATES]; + u_long load1[CP_STATES]; + u_long load2[CP_STATES]; GETSYSCTL( "kern.cp_time", load1 ); usleep( cpu_usage_delay ); diff --git a/freebsd/cpu.h b/freebsd/cpu.h deleted file mode 100644 index 3840650..0000000 --- a/freebsd/cpu.h +++ /dev/null @@ -1,31 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CPU_H_ -#define CPU_H_ - -#define CP_USER 0 -#define CP_NICE 1 -#define CP_SYS 2 -#define CP_INTR 3 -#define CP_IDLE 4 -#define CPUSTATES 5 - -float cpu_percentage( unsigned ); - -#endif diff --git a/freebsd/load.cc b/freebsd/load.cc index 3a8517e..60a3597 100644 --- a/freebsd/load.cc +++ b/freebsd/load.cc @@ -27,6 +27,7 @@ #include #include "getsysctl.h" +#include "cpu.h" #include "load.h" #include "luts.h" @@ -48,11 +49,9 @@ std::string load_string( bool use_colors = false ) if( use_colors ) { // may not work - int32_t cpu_count = 0; - GETSYSCTL( "hw.ncpu", cpu_count ); unsigned load_percent = static_cast( averages[0] / - cpu_count * 0.5f * 100.0f ); + get_cpu_count() * 0.5f * 100.0f ); if( load_percent > 100 ) { diff --git a/linux/cpu.cc b/linux/cpu.cc index c105bac..2fcc4c9 100644 --- a/linux/cpu.cc +++ b/linux/cpu.cc @@ -23,6 +23,11 @@ #include "cpu.h" #include "luts.h" +uint8_t get_cpu_count() +{ + return sysconf( _SC_NPROCESSORS_ONLN ); +} + float cpu_percentage( unsigned cpu_usage_delay ) { std::string line; @@ -32,7 +37,7 @@ float cpu_percentage( unsigned cpu_usage_delay ) // cpu stats // user, nice, system, idle // in that order - unsigned long long stats[4]; + unsigned long long stats[CP_STATES]; std::ifstream stat_file( "/proc/stat" ); getline( stat_file, line ); @@ -64,7 +69,10 @@ float cpu_percentage( unsigned cpu_usage_delay ) stats[i] = std::stoll( line.substr( substr_start, substr_len ) ) - stats[i]; } - return static_cast( stats[0] + stats[1] + stats[2]) / - static_cast( stats[0] + stats[1] + stats[2] + stats[3] ) * 100.0; + return static_cast( + stats[CP_USER] + stats[CP_NICE] + stats[CP_SYS] + ) / static_cast( + stats[CP_USER] + stats[CP_NICE] + stats[CP_SYS] + stats[CP_IDLE] + ) * 100.0; } diff --git a/linux/cpu.h b/linux/cpu.h deleted file mode 100644 index ebc0e8e..0000000 --- a/linux/cpu.h +++ /dev/null @@ -1,24 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CPU_H_ -#define CPU_H_ - -float cpu_percentage( unsigned ); - -#endif diff --git a/linux/load.cc b/linux/load.cc index a93c1fa..1414e04 100644 --- a/linux/load.cc +++ b/linux/load.cc @@ -18,11 +18,11 @@ #include #include -#include // sysconf()? #include #include // SI_LOAD_SHIFT #include "load.h" +#include "cpu.h" #include "luts.h" std::string load_string( bool use_colors = false ) @@ -37,14 +37,12 @@ std::string load_string( bool use_colors = false ) if( use_colors ) { // Likely does not work on BSD, but not tested - unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN ); - float recent_load = sinfo.loads[0] / f; // colors range from zero to twice the number of cpu's // for the most recent load metric unsigned load_percent = static_cast< unsigned int >( - recent_load / number_of_cpus * 0.5f * 100.0f ); + recent_load / get_cpu_count() * 0.5f * 100.0f ); if( load_percent > 100 ) { diff --git a/linux/load.h b/linux/load.h deleted file mode 100644 index f1bbc58..0000000 --- a/linux/load.h +++ /dev/null @@ -1,26 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LOAD_H_ -#define LOAD_H_ - -#include - -std::string load_string( bool ); - -#endif diff --git a/linux/memory.h b/linux/memory.h deleted file mode 100644 index 3a123bb..0000000 --- a/linux/memory.h +++ /dev/null @@ -1,26 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MEMORY_H_ -#define MEMORY_H_ - -#include - -std::string mem_string( bool ); - -#endif diff --git a/netbsd/cpu.cc b/netbsd/cpu.cc index 2afab53..cbee70e 100644 --- a/netbsd/cpu.cc +++ b/netbsd/cpu.cc @@ -23,13 +23,21 @@ #include #include // usleep -#include "../freebsd/getsysctl.h" +#include "getsysctl.h" #include "cpu.h" +uint8_t get_cpu_count() +{ + int cpu_count = 0; + GETSYSCTL( "hw.ncpu", cpu_count ); + + return static_cast( cpu_count ); +} + float cpu_percentage( unsigned int cpu_usage_delay ) { - u_int64_t load1[CPUSTATES]; - u_int64_t load2[CPUSTATES]; + u_int64_t load1[CP_STATES]; + u_int64_t load2[CP_STATES]; GETSYSCTL( "kern.cp_time", load1 ); usleep( cpu_usage_delay ); diff --git a/netbsd/cpu.h b/netbsd/cpu.h deleted file mode 100644 index 3840650..0000000 --- a/netbsd/cpu.h +++ /dev/null @@ -1,31 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CPU_H_ -#define CPU_H_ - -#define CP_USER 0 -#define CP_NICE 1 -#define CP_SYS 2 -#define CP_INTR 3 -#define CP_IDLE 4 -#define CPUSTATES 5 - -float cpu_percentage( unsigned ); - -#endif diff --git a/netbsd/memory.h b/netbsd/memory.h deleted file mode 100644 index 3a123bb..0000000 --- a/netbsd/memory.h +++ /dev/null @@ -1,26 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MEMORY_H_ -#define MEMORY_H_ - -#include - -std::string mem_string( bool ); - -#endif diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index 223f51b..e016c27 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -42,8 +42,8 @@ float cpu_percentage( unsigned int cpu_usage_delay ) { int cpu_ctl[] = { CTL_KERN, KERN_CPTIME }; - u_long load1[CPUSTATES]; - u_long load2[CPUSTATES]; + u_long load1[CP_STATES]; + u_long load2[CP_STATES]; size_t size = sizeof( load1 ); diff --git a/openbsd/load.cc b/openbsd/load.cc index 7c4a138..4aacab9 100644 --- a/openbsd/load.cc +++ b/openbsd/load.cc @@ -43,11 +43,8 @@ std::string load_string( bool use_colors = false ) { if( use_colors ) { - // may not work - uint8_t cpu_count = get_cpu_count(); - unsigned load_percent = static_cast( - averages[0] / cpu_count * 0.5f * 100.0f); + averages[0] / get_cpu_count() * 0.5f * 100.0f); if( load_percent > 100 ) { diff --git a/openbsd/load.h b/openbsd/load.h deleted file mode 100644 index f1bbc58..0000000 --- a/openbsd/load.h +++ /dev/null @@ -1,26 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LOAD_H_ -#define LOAD_H_ - -#include - -std::string load_string( bool ); - -#endif diff --git a/openbsd/memory.h b/openbsd/memory.h deleted file mode 100644 index 3a123bb..0000000 --- a/openbsd/memory.h +++ /dev/null @@ -1,26 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MEMORY_H_ -#define MEMORY_H_ - -#include - -std::string mem_string( bool ); - -#endif diff --git a/osx/cpu.cc b/osx/cpu.cc index f38fbe4..d287756 100644 --- a/osx/cpu.cc +++ b/osx/cpu.cc @@ -21,6 +21,11 @@ #include "cpu.h" +uint8_t get_cpu_count() +{ + return sysconf( _SC_NPROCESSORS_ONLN ); +} + // OSX or BSD based system, use BSD APIs instead // See: http://www.opensource.apple.com/source/xnu/xnu-201/osfmk/mach/host_info.h // and: http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/ @@ -53,15 +58,15 @@ float cpu_percentage( unsigned int cpu_usage_delay ) host_cpu_load_info_data_t load2 = _get_cpu_percentage(); // Current load times - unsigned long long current_user = load1.cpu_ticks[CPU_STATE_USER]; - unsigned long long current_system = load1.cpu_ticks[CPU_STATE_SYSTEM]; - unsigned long long current_nice = load1.cpu_ticks[CPU_STATE_NICE]; - unsigned long long current_idle = load1.cpu_ticks[CPU_STATE_IDLE]; + unsigned long long current_user = load1.cpu_ticks[CP_USER]; + unsigned long long current_system = load1.cpu_ticks[CP_SYS]; + unsigned long long current_nice = load1.cpu_ticks[CP_NICE]; + unsigned long long current_idle = load1.cpu_ticks[CP_IDLE]; // Next load times - unsigned long long next_user = load2.cpu_ticks[CPU_STATE_USER]; - unsigned long long next_system = load2.cpu_ticks[CPU_STATE_SYSTEM]; - unsigned long long next_nice = load2.cpu_ticks[CPU_STATE_NICE]; - unsigned long long next_idle = load2.cpu_ticks[CPU_STATE_IDLE]; + unsigned long long next_user = load2.cpu_ticks[CP_USER]; + unsigned long long next_system = load2.cpu_ticks[CP_SYS]; + unsigned long long next_nice = load2.cpu_ticks[CP_NICE]; + unsigned long long next_idle = load2.cpu_ticks[CP_IDLE]; // Difference between the two unsigned long long diff_user = next_user - current_user; unsigned long long diff_system = next_system - current_system; diff --git a/osx/cpu.h b/osx/cpu.h deleted file mode 100644 index 1652c03..0000000 --- a/osx/cpu.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CPU_H_ -#define CPU_H_ - -float cpu_percentage ( unsigned ); - -#endif diff --git a/osx/load.cc b/osx/load.cc index cf4a816..11cd4ed 100644 --- a/osx/load.cc +++ b/osx/load.cc @@ -25,6 +25,7 @@ #include // getloadavg() #include "load.h" +#include "cpu.h" #include "luts.h" std::string load_string( bool use_colors = false ) @@ -57,9 +58,6 @@ std::string load_string( bool use_colors = false ) if( use_colors ) { - // Likely does not work on BSD, but not tested - unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN ); - std::istringstream iss( load_line.substr( 0, 4 ) ); float recent_load; iss >> recent_load; @@ -67,7 +65,7 @@ std::string load_string( bool use_colors = false ) // cpu's for the most recent load metric unsigned load_percent = static_cast< unsigned int >( - recent_load / number_of_cpus * 0.5f * 100.0f ); + recent_load / get_cpu_count() * 0.5f * 100.0f ); if( load_percent > 100 ) { diff --git a/osx/load.h b/osx/load.h deleted file mode 100644 index 16cd762..0000000 --- a/osx/load.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LOAD_H_ -#define LOAD_H_ - -#include - -std::string load_string( bool ); - -#endif diff --git a/osx/memory.h b/osx/memory.h deleted file mode 100644 index 3d89aa3..0000000 --- a/osx/memory.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2012 Matthew McCormick - * Copyright 2013 Justin Crawford - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MEMORY_H_ -#define MEMORY_H_ - -#include - -std::string mem_string( bool ); - -#endif From 87cc9b1ce8ae973940862f8f56e90b14066caa3c Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Thu, 19 Feb 2015 20:25:43 +0100 Subject: [PATCH 4/4] Code Refactoring part 2: unified load_string() function on all platforms All the platforms were using identical logic based on getloadavg() function to get the load avg stats (except linux, which was using sinfo struct, but can use getloadavg() function). I've noticed this while working on NetBSD port. Also: fixed a typo on freebsd. --- CMakeLists.txt | 10 ++--- {freebsd => common}/load.cc | 5 --- freebsd/cpu.cc | 2 +- linux/load.cc | 68 ----------------------------- openbsd/load.cc | 72 ------------------------------ osx/load.cc | 87 ------------------------------------- 6 files changed, 6 insertions(+), 238 deletions(-) rename {freebsd => common}/load.cc (97%) delete mode 100644 linux/load.cc delete mode 100644 openbsd/load.cc delete mode 100644 osx/load.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 372229e..cc6e949 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,22 +59,22 @@ endif(NOT CMAKE_BUILD_TYPE) # detect system type if(CMAKE_SYSTEM_NAME MATCHES "Linux") message(STATUS "Linux detected") - set(METER_SOURCES "linux/memory.cc" "linux/cpu.cc" "linux/load.cc") + set(METER_SOURCES "linux/memory.cc" "linux/cpu.cc" "common/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") message(STATUS "Darwin detected") - set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "osx/load.cc") + set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "common/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") message(STATUS "FreeBSD detected") - set(METER_SOURCES "freebsd/memory.cc" "freebsd/cpu.cc" "freebsd/load.cc") + set(METER_SOURCES "freebsd/memory.cc" "freebsd/cpu.cc" "common/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") # OpenBSD Stuff Here message(STATUS "OpenBSD detected") - set(METER_SOURCES "openbsd/memory.cc" "openbsd/cpu.cc" "openbsd/load.cc") + set(METER_SOURCES "openbsd/memory.cc" "openbsd/cpu.cc" "common/load.cc") if(CMAKE_SYSTEM_VERSION VERSION_LESS 5.7) add_definitions(-DOPENBSD_WORKAROUND=1) endif() elseif(CMAKE_SYSTEM_NAME MATCHES "NetBSD") - set(METER_SOURCES "netbsd/memory.cc" "netbsd/cpu.cc" "freebsd/load.cc") + set(METER_SOURCES "netbsd/memory.cc" "netbsd/cpu.cc" "common/load.cc") else() message(FATAL_ERROR "Cannot be compiled on this system") endif() diff --git a/freebsd/load.cc b/common/load.cc similarity index 97% rename from freebsd/load.cc rename to common/load.cc index 60a3597..6dc3fe7 100644 --- a/freebsd/load.cc +++ b/common/load.cc @@ -26,7 +26,6 @@ #include // floorf() #include -#include "getsysctl.h" #include "cpu.h" #include "load.h" #include "luts.h" @@ -48,8 +47,6 @@ std::string load_string( bool use_colors = false ) { if( use_colors ) { - // may not work - unsigned load_percent = static_cast( averages[0] / get_cpu_count() * 0.5f * 100.0f ); @@ -57,7 +54,6 @@ std::string load_string( bool use_colors = false ) { load_percent = 100; } - ss << load_lut[load_percent]; } @@ -72,7 +68,6 @@ std::string load_string( bool use_colors = false ) { ss << "#[fg=default,bg=default]"; } - } return ss.str(); diff --git a/freebsd/cpu.cc b/freebsd/cpu.cc index f6a4e62..b85d402 100644 --- a/freebsd/cpu.cc +++ b/freebsd/cpu.cc @@ -26,7 +26,7 @@ #include "getsysctl.h" #include "cpu.h" -uint8_t get_cpu_cout() +uint8_t get_cpu_count() { int32_t cpu_count = 0; GETSYSCTL( "hw.ncpu", cpu_count ); diff --git a/linux/load.cc b/linux/load.cc deleted file mode 100644 index 1414e04..0000000 --- a/linux/load.cc +++ /dev/null @@ -1,68 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include // SI_LOAD_SHIFT - -#include "load.h" -#include "cpu.h" -#include "luts.h" - -std::string load_string( bool use_colors = false ) -{ - std::ostringstream oss; - - float f = static_cast( 1 << SI_LOAD_SHIFT ); - - struct sysinfo sinfo; - sysinfo( &sinfo ); - - if( use_colors ) - { - // Likely does not work on BSD, but not tested - float recent_load = sinfo.loads[0] / f; - - // colors range from zero to twice the number of cpu's - // for the most recent load metric - unsigned load_percent = static_cast< unsigned int >( - recent_load / get_cpu_count() * 0.5f * 100.0f ); - - if( load_percent > 100 ) - { - load_percent = 100; - } - - oss << load_lut[load_percent]; - } - - // set precision so we get results like "0.22" - oss.setf( std::ios::fixed ); - oss.precision( 2 ); - - oss << sinfo.loads[0] / f << " " << sinfo.loads[1] / f << " " - << sinfo.loads[2] / f; - - if( use_colors ) - { - oss << "#[fg=default,bg=default]"; - } - - return oss.str(); -} diff --git a/openbsd/load.cc b/openbsd/load.cc deleted file mode 100644 index 4aacab9..0000000 --- a/openbsd/load.cc +++ /dev/null @@ -1,72 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include // getloadavg() -#include // floorf() -#include - -#include "cpu.h" -#include "load.h" -#include "luts.h" - -// Load Averages -std::string load_string( bool use_colors = false ) -{ - std::stringstream ss; - // Only get 3 load averages - const int nelem = 3; - double averages[nelem]; - // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c - - if( getloadavg( averages, nelem ) < 0) - { - ss << "0.00 0.00 0.00"; // couldn't get averages. - } - else - { - if( use_colors ) - { - unsigned load_percent = static_cast( - averages[0] / get_cpu_count() * 0.5f * 100.0f); - - if( load_percent > 100 ) - { - load_percent = 100; - } - - ss << load_lut[load_percent]; - } - - for( int i = 0; i < nelem; ++i ) - { - // Round to nearest, make sure this is only a 0.00 value not a 0.0000 - float avg = floorf( static_cast (averages[i] ) * 100 + 0.5 ) / 100; - ss << avg << " "; - } - - if( use_colors ) - { - ss << "#[fg=default,bg=default]"; - } - - } - - return ss.str(); -} diff --git a/osx/load.cc b/osx/load.cc deleted file mode 100644 index 11cd4ed..0000000 --- a/osx/load.cc +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2012 Matthew McCormick - * Copyright 2013 Justin Crawford - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include // floorf() - -#include -#include // getloadavg() - -#include "load.h" -#include "cpu.h" -#include "luts.h" - -std::string load_string( bool use_colors = false ) -{ - std::ostringstream oss; - - // Both apple and BSD style systems have these api calls - // Only get 3 load averages - const int nelem = 3; - double averages[nelem]; - // based on: - // http://www.opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c - if( getloadavg( averages, nelem ) < 0 ) - { - oss << "0.00 0.00 0.00"; // couldn't get averages. - } - else - { - for( int i = 0; i < nelem; ++i ) - { - // Round to nearest, make sure this is - // only a 0.00 value not a 0.0000 - float avg = floorf( static_cast( averages[i] ) * 100 + 0.5 ) / 100; - oss << avg << " "; - } - } - - std::string load_line( oss.str() ); - oss.str( "" ); - - if( use_colors ) - { - std::istringstream iss( load_line.substr( 0, 4 ) ); - float recent_load; - iss >> recent_load; - // colors range from zero to twice the number of - // cpu's for the most recent load metric - - unsigned load_percent = static_cast< unsigned int >( - recent_load / get_cpu_count() * 0.5f * 100.0f ); - - if( load_percent > 100 ) - { - load_percent = 100; - } - - oss << load_lut[load_percent]; - } - - oss << load_line.substr( 0, 14 ); - - if( use_colors ) - { - oss << "#[fg=default,bg=default]"; - } - - return oss.str(); -} -