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.
This commit is contained in:
parent
240752d800
commit
87cc9b1ce8
@ -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()
|
||||
|
@ -26,7 +26,6 @@
|
||||
#include <cmath> // floorf()
|
||||
#include <sys/types.h>
|
||||
|
||||
#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<unsigned int>( 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();
|
@ -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 );
|
||||
|
@ -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 <string>
|
||||
#include <sstream>
|
||||
#include <sys/sysinfo.h>
|
||||
#include <linux/kernel.h> // 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<float>( 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();
|
||||
}
|
@ -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 <sstream>
|
||||
#include <string>
|
||||
#include <stdlib.h> // getloadavg()
|
||||
#include <cmath> // floorf()
|
||||
#include <sys/types.h>
|
||||
|
||||
#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<unsigned int>(
|
||||
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<float> (averages[i] ) * 100 + 0.5 ) / 100;
|
||||
ss << avg << " ";
|
||||
}
|
||||
|
||||
if( use_colors )
|
||||
{
|
||||
ss << "#[fg=default,bg=default]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
87
osx/load.cc
87
osx/load.cc
@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 Matthew McCormick
|
||||
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
|
||||
* 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 <string>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <cmath> // floorf()
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h> // 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<float>( 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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user