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.
This commit is contained in:
Pawel "l0ner" Soltys 2015-02-19 19:47:45 +01:00
parent e8bea10056
commit 240752d800
25 changed files with 79 additions and 355 deletions

@ -21,12 +21,28 @@
#include <sys/types.h> #include <sys/types.h>
#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_USER 0
#define CP_NICE 1 #define CP_NICE 1
#define CP_SYS 2 #define CP_SYS 2
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
// *BSD or OSX
#define CP_INTR 3 #define CP_INTR 3
#define CP_IDLE 4 #define CP_IDLE 4
#define CPUSTATES 5 #define CP_STATES 5
#else
//linux
#define CP_IDLE 3
#define CP_STATES 4
#endif
#endif
float cpu_percentage( unsigned ); float cpu_percentage( unsigned );
uint8_t get_cpu_count(); uint8_t get_cpu_count();

@ -29,32 +29,9 @@
// Tmux color lookup tables for the different metrics. // Tmux color lookup tables for the different metrics.
#include "luts.h" #include "luts.h"
#if defined(__APPLE__) && defined(__MACH__) #include "cpu.h"
// Apple osx system #include "memory.h"
#include "osx/cpu.h" #include "load.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
std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines, std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false ) bool use_colors = false )

@ -26,10 +26,18 @@
#include "getsysctl.h" #include "getsysctl.h"
#include "cpu.h" #include "cpu.h"
uint8_t get_cpu_cout()
{
int32_t cpu_count = 0;
GETSYSCTL( "hw.ncpu", cpu_count );
return static_cast<uint8_t>( cpu_count );
}
float cpu_percentage( unsigned int cpu_usage_delay ) float cpu_percentage( unsigned int cpu_usage_delay )
{ {
u_long load1[CPUSTATES]; u_long load1[CP_STATES];
u_long load2[CPUSTATES]; u_long load2[CP_STATES];
GETSYSCTL( "kern.cp_time", load1 ); GETSYSCTL( "kern.cp_time", load1 );
usleep( cpu_usage_delay ); usleep( cpu_usage_delay );

@ -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

@ -27,6 +27,7 @@
#include <sys/types.h> #include <sys/types.h>
#include "getsysctl.h" #include "getsysctl.h"
#include "cpu.h"
#include "load.h" #include "load.h"
#include "luts.h" #include "luts.h"
@ -48,11 +49,9 @@ std::string load_string( bool use_colors = false )
if( use_colors ) if( use_colors )
{ {
// may not work // may not work
int32_t cpu_count = 0;
GETSYSCTL( "hw.ncpu", cpu_count );
unsigned load_percent = static_cast<unsigned int>( averages[0] / unsigned load_percent = static_cast<unsigned int>( averages[0] /
cpu_count * 0.5f * 100.0f ); get_cpu_count() * 0.5f * 100.0f );
if( load_percent > 100 ) if( load_percent > 100 )
{ {

@ -23,6 +23,11 @@
#include "cpu.h" #include "cpu.h"
#include "luts.h" #include "luts.h"
uint8_t get_cpu_count()
{
return sysconf( _SC_NPROCESSORS_ONLN );
}
float cpu_percentage( unsigned cpu_usage_delay ) float cpu_percentage( unsigned cpu_usage_delay )
{ {
std::string line; std::string line;
@ -32,7 +37,7 @@ float cpu_percentage( unsigned cpu_usage_delay )
// cpu stats // cpu stats
// user, nice, system, idle // user, nice, system, idle
// in that order // in that order
unsigned long long stats[4]; unsigned long long stats[CP_STATES];
std::ifstream stat_file( "/proc/stat" ); std::ifstream stat_file( "/proc/stat" );
getline( stat_file, line ); 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]; stats[i] = std::stoll( line.substr( substr_start, substr_len ) ) - stats[i];
} }
return static_cast<float>( stats[0] + stats[1] + stats[2]) / return static_cast<float>(
static_cast<float>( stats[0] + stats[1] + stats[2] + stats[3] ) * 100.0; stats[CP_USER] + stats[CP_NICE] + stats[CP_SYS]
) / static_cast<float>(
stats[CP_USER] + stats[CP_NICE] + stats[CP_SYS] + stats[CP_IDLE]
) * 100.0;
} }

@ -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

@ -18,11 +18,11 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <unistd.h> // sysconf()?
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <linux/kernel.h> // SI_LOAD_SHIFT #include <linux/kernel.h> // SI_LOAD_SHIFT
#include "load.h" #include "load.h"
#include "cpu.h"
#include "luts.h" #include "luts.h"
std::string load_string( bool use_colors = false ) std::string load_string( bool use_colors = false )
@ -37,14 +37,12 @@ std::string load_string( bool use_colors = false )
if( use_colors ) if( use_colors )
{ {
// Likely does not work on BSD, but not tested // Likely does not work on BSD, but not tested
unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN );
float recent_load = sinfo.loads[0] / f; float recent_load = sinfo.loads[0] / f;
// colors range from zero to twice the number of cpu's // colors range from zero to twice the number of cpu's
// for the most recent load metric // for the most recent load metric
unsigned load_percent = static_cast< unsigned int >( 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 ) if( load_percent > 100 )
{ {

@ -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 <string>
std::string load_string( bool );
#endif

@ -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 <string>
std::string mem_string( bool );
#endif

@ -23,13 +23,21 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> // usleep #include <unistd.h> // usleep
#include "../freebsd/getsysctl.h" #include "getsysctl.h"
#include "cpu.h" #include "cpu.h"
uint8_t get_cpu_count()
{
int cpu_count = 0;
GETSYSCTL( "hw.ncpu", cpu_count );
return static_cast<uint8_t>( cpu_count );
}
float cpu_percentage( unsigned int cpu_usage_delay ) float cpu_percentage( unsigned int cpu_usage_delay )
{ {
u_int64_t load1[CPUSTATES]; u_int64_t load1[CP_STATES];
u_int64_t load2[CPUSTATES]; u_int64_t load2[CP_STATES];
GETSYSCTL( "kern.cp_time", load1 ); GETSYSCTL( "kern.cp_time", load1 );
usleep( cpu_usage_delay ); usleep( cpu_usage_delay );

@ -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

@ -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 <string>
std::string mem_string( bool );
#endif

@ -42,8 +42,8 @@ float cpu_percentage( unsigned int cpu_usage_delay )
{ {
int cpu_ctl[] = { CTL_KERN, KERN_CPTIME }; int cpu_ctl[] = { CTL_KERN, KERN_CPTIME };
u_long load1[CPUSTATES]; u_long load1[CP_STATES];
u_long load2[CPUSTATES]; u_long load2[CP_STATES];
size_t size = sizeof( load1 ); size_t size = sizeof( load1 );

@ -43,11 +43,8 @@ std::string load_string( bool use_colors = false )
{ {
if( use_colors ) if( use_colors )
{ {
// may not work
uint8_t cpu_count = get_cpu_count();
unsigned load_percent = static_cast<unsigned int>( unsigned load_percent = static_cast<unsigned int>(
averages[0] / cpu_count * 0.5f * 100.0f); averages[0] / get_cpu_count() * 0.5f * 100.0f);
if( load_percent > 100 ) if( load_percent > 100 )
{ {

@ -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 <string>
std::string load_string( bool );
#endif

@ -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 <string>
std::string mem_string( bool );
#endif

@ -21,6 +21,11 @@
#include "cpu.h" #include "cpu.h"
uint8_t get_cpu_count()
{
return sysconf( _SC_NPROCESSORS_ONLN );
}
// OSX or BSD based system, use BSD APIs instead // OSX or BSD based system, use BSD APIs instead
// See: http://www.opensource.apple.com/source/xnu/xnu-201/osfmk/mach/host_info.h // 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/ // 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(); host_cpu_load_info_data_t load2 = _get_cpu_percentage();
// Current load times // Current load times
unsigned long long current_user = load1.cpu_ticks[CPU_STATE_USER]; unsigned long long current_user = load1.cpu_ticks[CP_USER];
unsigned long long current_system = load1.cpu_ticks[CPU_STATE_SYSTEM]; unsigned long long current_system = load1.cpu_ticks[CP_SYS];
unsigned long long current_nice = load1.cpu_ticks[CPU_STATE_NICE]; unsigned long long current_nice = load1.cpu_ticks[CP_NICE];
unsigned long long current_idle = load1.cpu_ticks[CPU_STATE_IDLE]; unsigned long long current_idle = load1.cpu_ticks[CP_IDLE];
// Next load times // Next load times
unsigned long long next_user = load2.cpu_ticks[CPU_STATE_USER]; unsigned long long next_user = load2.cpu_ticks[CP_USER];
unsigned long long next_system = load2.cpu_ticks[CPU_STATE_SYSTEM]; unsigned long long next_system = load2.cpu_ticks[CP_SYS];
unsigned long long next_nice = load2.cpu_ticks[CPU_STATE_NICE]; unsigned long long next_nice = load2.cpu_ticks[CP_NICE];
unsigned long long next_idle = load2.cpu_ticks[CPU_STATE_IDLE]; unsigned long long next_idle = load2.cpu_ticks[CP_IDLE];
// Difference between the two // Difference between the two
unsigned long long diff_user = next_user - current_user; unsigned long long diff_user = next_user - current_user;
unsigned long long diff_system = next_system - current_system; unsigned long long diff_system = next_system - current_system;

@ -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

@ -25,6 +25,7 @@
#include <stdlib.h> // getloadavg() #include <stdlib.h> // getloadavg()
#include "load.h" #include "load.h"
#include "cpu.h"
#include "luts.h" #include "luts.h"
std::string load_string( bool use_colors = false ) std::string load_string( bool use_colors = false )
@ -57,9 +58,6 @@ std::string load_string( bool use_colors = false )
if( use_colors ) 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 ) ); std::istringstream iss( load_line.substr( 0, 4 ) );
float recent_load; float recent_load;
iss >> 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 // cpu's for the most recent load metric
unsigned load_percent = static_cast< unsigned int >( 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 ) if( load_percent > 100 )
{ {

@ -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 <string>
std::string load_string( bool );
#endif

@ -1,26 +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.
*/
#ifndef MEMORY_H_
#define MEMORY_H_
#include <string>
std::string mem_string( bool );
#endif