Code Refactoring and NetBSD support.
* Added NetBSD support. * removed per-platform header files: all the supported platforms were using identical header files for cpu, memory and load (except a few differences). Since this was unnecessary code duplication I've gathered what was common among the platforms, removed duplicates and moved what was left to the common/ dir. * implemented get_cpu_count() function across all platforms. We are using it 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. * Unified load_string() across all platforms. Same case as with the header files. We can use the same getloadavg() based logic for getting load averages on all platforms. *BSD and OS X code was already the pretty much identical. Removed duplicated code and made linux adhere to getloadavg() logic.
This commit is contained in:
commit
72b8493a59
@ -59,20 +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" "common/load.cc")
|
||||
else()
|
||||
message(FATAL_ERROR "Cannot be compiled on this system")
|
||||
endif()
|
||||
|
@ -21,12 +21,28 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#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();
|
@ -23,6 +23,7 @@
|
||||
#include <sys/errno.h>
|
||||
#include <cerrno>
|
||||
#include <cstring> // strerror
|
||||
#include <cstdlib> // 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
|
@ -26,6 +26,8 @@
|
||||
#include <cerrno>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <cstdlib> //exit()
|
||||
#include <string.h> //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 )
|
@ -26,7 +26,7 @@
|
||||
#include <cmath> // floorf()
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "getsysctl.h"
|
||||
#include "cpu.h"
|
||||
#include "load.h"
|
||||
#include "luts.h"
|
||||
|
||||
@ -47,18 +47,13 @@ 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<unsigned int>( averages[0] /
|
||||
cpu_count * 0.5f * 100.0f );
|
||||
get_cpu_count() * 0.5f * 100.0f );
|
||||
|
||||
if( load_percent > 100 )
|
||||
{
|
||||
load_percent = 100;
|
||||
}
|
||||
|
||||
ss << load_lut[load_percent];
|
||||
}
|
||||
|
||||
@ -73,7 +68,6 @@ std::string load_string( bool use_colors = false )
|
||||
{
|
||||
ss << "#[fg=default,bg=default]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ss.str();
|
@ -29,28 +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"
|
||||
#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 )
|
||||
|
@ -26,10 +26,18 @@
|
||||
#include "getsysctl.h"
|
||||
#include "cpu.h"
|
||||
|
||||
uint8_t get_cpu_count()
|
||||
{
|
||||
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 )
|
||||
{
|
||||
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 );
|
||||
|
@ -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
|
14
linux/cpu.cc
14
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<float>( stats[0] + stats[1] + stats[2]) /
|
||||
static_cast<float>( stats[0] + stats[1] + stats[2] + stats[3] ) * 100.0;
|
||||
return static_cast<float>(
|
||||
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;
|
||||
}
|
||||
|
||||
|
24
linux/cpu.h
24
linux/cpu.h
@ -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
|
@ -1,70 +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 <unistd.h> // sysconf()?
|
||||
#include <sys/sysinfo.h>
|
||||
#include <linux/kernel.h> // SI_LOAD_SHIFT
|
||||
|
||||
#include "load.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
|
||||
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 );
|
||||
|
||||
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();
|
||||
}
|
26
linux/load.h
26
linux/load.h
@ -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
|
65
netbsd/cpu.cc
Normal file
65
netbsd/cpu.cc
Normal file
@ -0,0 +1,65 @@
|
||||
/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// 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 <sys/types.h>
|
||||
#include <unistd.h> // usleep
|
||||
|
||||
#include "getsysctl.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 )
|
||||
{
|
||||
u_int64_t load1[CP_STATES];
|
||||
u_int64_t load2[CP_STATES];
|
||||
|
||||
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<float>( diff_user + diff_system + diff_nice ) /
|
||||
static_cast<float>( diff_user + diff_system + diff_nice + diff_idle ) *
|
||||
100.0;
|
||||
}
|
64
netbsd/memory.cc
Normal file
64
netbsd/memory.cc
Normal file
@ -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 <sstream>
|
||||
#include <string>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <uvm/uvm_extern.h> // 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();
|
||||
|
||||
}
|
@ -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 );
|
||||
|
||||
|
@ -1,75 +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 )
|
||||
{
|
||||
// may not work
|
||||
uint8_t cpu_count = get_cpu_count();
|
||||
|
||||
unsigned load_percent = static_cast<unsigned int>(
|
||||
averages[0] / 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();
|
||||
}
|
@ -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
osx/cpu.cc
21
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;
|
||||
|
23
osx/cpu.h
23
osx/cpu.h
@ -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
|
89
osx/load.cc
89
osx/load.cc
@ -1,89 +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 "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 )
|
||||
{
|
||||
// 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;
|
||||
// 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 );
|
||||
|
||||
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();
|
||||
}
|
||||
|
25
osx/load.h
25
osx/load.h
@ -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
|
26
osx/memory.h
26
osx/memory.h
@ -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
|
Loading…
Reference in New Issue
Block a user