From 87cc9b1ce8ae973940862f8f56e90b14066caa3c Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Thu, 19 Feb 2015 20:25:43 +0100 Subject: [PATCH] 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(); -} -