code syntax style, vim modelines, license

* Use std::sting instead of char[] in the graph drawing functions. This is the
  only change to the code. Rest is just styling.
* Corrected whole code to follow Allman/GNU coding style, with 2 spaces for each
  indentation step.
* Added license headers to all code files
* Added vim modelines to all files. They sit on the first line and enable the
  following settings: 2 space indentation, tab expansion to spaces, line at
  80th column, automatic line breaking on "\ !@*+-;:,./?" characters, automatic
  line break if line exceeds 80 colums. This should keep the code nice and tidy.
This commit is contained in:
Pawel "l0ner" Soltys 2015-01-18 14:31:04 +01:00
parent d5506ac797
commit 51f85ba032
25 changed files with 830 additions and 415 deletions

@ -1,6 +1,24 @@
# 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.
#
cmake_minimum_required(VERSION 2.6)
if(COMMAND cmake_policy)
cmake_policy(VERSION 2.6)
cmake_policy(VERSION 2.6)
endif(COMMAND cmake_policy)
### General Package stuff
@ -12,38 +30,38 @@ set(VERSION_PATCH 0)
# generate header file to handle version
configure_file(
"${PROJECT_SOURCE_DIR}/version.h.in"
"${PROJECT_SOURCE_DIR}/version.h"
)
"${PROJECT_SOURCE_DIR}/version.h.in"
"${PROJECT_SOURCE_DIR}/version.h"
)
# set buold type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
endif(NOT CMAKE_BUILD_TYPE)
# detect system type
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
message("Linux detected")
set(METER_SOURCES "linux/memory.cc" "linux/cpu.cc" "linux/load.cc")
message("Linux detected")
set(METER_SOURCES "linux/memory.cc" "linux/cpu.cc" "linux/load.cc")
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
# Mac OS X source setting will go here
message( "Darwin detected")
set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "osx/load.cc")
# Mac OS X source setting will go here
message( "Darwin detected")
set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "osx/load.cc")
elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
# FreeBSD STUFF HERE
message("FreeBSD detected")
message( WARNING "FreeBSD is still experimental!" )
set( METER_SOURCES "bsd/memory_freebsd.cc" "bsd/cpu.cc" "bsd/load.cc" )
# FreeBSD STUFF HERE
message("FreeBSD detected")
message( WARNING "FreeBSD is still experimental!" )
set( METER_SOURCES "bsd/memory_freebsd.cc" "bsd/cpu.cc" "bsd/load.cc" )
elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
# OpenBSD Stuff Here
message( "OpenBSD detected")
message( FATAL_ERROR "OpenBSD is not supported! See bsd/openBSD.txt for more
info" )
set( METER_SOURCES "bsd/memory_openbsd.cc" "bsd/cpu.cc" "bsd/load.cc" )
# OpenBSD Stuff Here
message( "OpenBSD detected")
message( FATAL_ERROR "OpenBSD is not supported! See bsd/openBSD.txt for more
info" )
set( METER_SOURCES "bsd/memory_openbsd.cc" "bsd/cpu.cc" "bsd/load.cc" )
else()
message( FATAL_ERROR "Cannot be compiled on this system" )
message( FATAL_ERROR "Cannot be compiled on this system" )
endif()
# set common source files
@ -52,13 +70,13 @@ SET( COMMON_SOURCES "tmux-mem-cpu-load.cpp" "graph.cc" "argParse/argParse.cc" )
# compiler flags
if (CMAKE_COMPILER_IS_GNUCC)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE GCC_VERSION)
OUTPUT_VARIABLE GCC_VERSION)
if (GCC_VERSION VERSION_EQUAL 4.6)
set( GCC_COVERAGE_COMPILE_FLAGS "-std=c++0x " )
set( GCC_COVERAGE_COMPILE_FLAGS "-std=c++0x " )
elseif (GCC_VERSION GREATER 4.6)
set( GCC_COVERAGE_COMPILE_FLAGS "-std=c++11 " )
set( GCC_COVERAGE_COMPILE_FLAGS "-std=c++11 " )
else()
message( FATAL_ERROR "You need gcc version >= 4.6")
message( FATAL_ERROR "You need gcc version >= 4.6")
endif()
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set( GCC_COVERAGE_COMPILE_FLAGS "-std=c++11 " )
@ -74,8 +92,8 @@ include_directories("${PROJECT_BINARY_DIR}" )
add_executable(tmux-mem-cpu-load ${COMMON_SOURCES} ${METER_SOURCES})
install(TARGETS tmux-mem-cpu-load
RUNTIME DESTINATION bin
)
RUNTIME DESTINATION bin
)
include( CTest )
if( BUILD_TESTING )

@ -1,5 +1,7 @@
/*
* 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.
@ -12,8 +14,8 @@
* 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.
* */
// This file was Authored by Justin Crawford <Justasic@gmail.com>
*/
// 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
@ -39,20 +41,23 @@
#define CPUSTATES 5
#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
static inline void getsysctl(const char *name, void *ptr, size_t len) {
static inline void getsysctl( const char *name, void *ptr, size_t len )
{
size_t nlen = len;
if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
std::cerr << "sysctl(" << name << "...) failed: " << strerror(errno)
<< std::endl;
exit(23);
if( sysctlbyname( name, ptr, &nlen, NULL, 0 ) == -1 )
{
std::cerr << "sysctl(" << name << "...) failed: " << strerror( errno )
<< std::endl;
exit( 23 );
}
if (nlen != len) {
std::cerr << "sysctl(" << name << "...) expected "
<< static_cast<unsigned long>(len) << " bytes, got "
<< static_cast<unsigned long>(nlen) << " bytes\n";
//exit(23);
if( nlen != len )
{
std::cerr << "sysctl(" << name << "...) expected "
<< static_cast<unsigned long>( len ) << " bytes, got "
<< static_cast<unsigned long>( nlen ) << " bytes\n";
//exit( 23 );
}
}

@ -1,5 +1,8 @@
/*
/* 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.
@ -12,8 +15,8 @@
* 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.
* */
// This file was Authored by Justin Crawford <Justasic@gmail.com>
*/
// 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
@ -23,32 +26,32 @@
#include "common.h"
#include "cpu.h"
float cpu_percentage(unsigned int cpu_usage_delay)
float cpu_percentage( unsigned int cpu_usage_delay )
{
int32_t load1[CPUSTATES];
int32_t load2[CPUSTATES];
GETSYSCTL("kern.cp_time", load1);
usleep(cpu_usage_delay);
GETSYSCTL("kern.cp_time", load2);
int32_t load1[CPUSTATES];
int32_t load2[CPUSTATES];
// 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;
GETSYSCTL( "kern.cp_time", load1 );
usleep( cpu_usage_delay );
GETSYSCTL( "kern.cp_time", load2 );
return static_cast<float>(diff_user + diff_system + diff_nice) /
static_cast<float>(diff_user + diff_system + diff_nice + diff_idle) *
100.0;
// 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;
}

@ -1,6 +1,24 @@
/* 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);
float cpu_percentage( unsigned );
#endif

@ -1,5 +1,8 @@
/*
/* 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.
@ -12,11 +15,10 @@
* 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.
* */
// This file was Authored by Justin Crawford <Justasic@gmail.com>
// Based on: https://github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c
// Based on: Apple.cpp for load_string/mem_string and apple's documentation
*/
// 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 <sstream>
#include <string>
@ -29,38 +31,48 @@
#include "../luts.h"
// Load Averages
std::string load_string( bool use_colors = false ) {
std::string load_string( bool use_colors = false )
{
std::stringstream ss;
// Only get 3 load averages
int nelem = 3;
double averages[3];
// Get only 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
int32_t cpu_count = 0;
GETSYSCTL("hw.ncpu", cpu_count);
if( getloadavg( averages, nelem ) < 0 )
{
ss << "0.00 0.00 0.00"; // couldn't get averages.
}
else
{
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);
unsigned load_percent = static_cast<unsigned int>( averages[0] /
cpu_count * 0.5f * 100.0f );
if( load_percent > 100 )
load_percent = 100;
if( load_percent > 100 )
{
load_percent = 100;
}
ss << load_lut[load_percent];
}
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 << " ";
}
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]";
if( use_colors )
{
ss << "#[fg=default,bg=default]";
}
}

@ -1,3 +1,21 @@
/* 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_

@ -1,3 +1,21 @@
/* 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_

@ -1,5 +1,7 @@
/*
/* 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.
@ -12,11 +14,10 @@
* 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.
* */
// This file was Authored by Justin Crawford <Justasic@gmail.com>
// Based on: https://github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c
// Based on: Apple.cpp for load_string/mem_string and apple's documentation
*/
// 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 <sstream>
#include <string>
@ -26,7 +27,8 @@
#include "memory.h"
#include "../luts.h"
std::string mem_string( bool use_colors = false ) {
std::string mem_string( bool use_colors = false )
{
// These values are in bytes
int32_t total_mem = 0;
int64_t used_mem = 0;
@ -40,28 +42,32 @@ std::string mem_string( bool use_colors = false ) {
std::ostringstream oss;
// Get total physical memory, page size, and some other needed info
GETSYSCTL("hw.realmem", total_mem);
GETSYSCTL("hw.pagesize", page_size);
GETSYSCTL( "hw.realmem", total_mem );
GETSYSCTL( "hw.pagesize", page_size );
GETSYSCTL("vm.stats.vm.v_free_count", free_mem);
GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_mem);
GETSYSCTL("vm.stats.vm.v_cache_count", cache_mem);
GETSYSCTL("vm.stats.vm.v_wire_count", wired_mem);
GETSYSCTL("vm.stats.vm.v_active_count", active_mem);
GETSYSCTL( "vm.stats.vm.v_free_count", free_mem );
GETSYSCTL( "vm.stats.vm.v_inactive_count", inactive_mem );
GETSYSCTL( "vm.stats.vm.v_cache_count", cache_mem );
GETSYSCTL( "vm.stats.vm.v_wire_count", wired_mem );
GETSYSCTL( "vm.stats.vm.v_active_count", active_mem );
// Get all memory which can be allocated
//unused_mem = (cache_mem + free_mem) * page_size;
used_mem = (
static_cast<int64_t>(active_mem) + static_cast<int64_t>(inactive_mem) +
static_cast<int64_t>(wired_mem)) * static_cast<int64_t>(page_size);
used_mem = ( static_cast<int64_t>( active_mem ) +
static_cast<int64_t>( inactive_mem ) +
static_cast<int64_t>( wired_mem ) ) * static_cast<int64_t>( page_size );
if( use_colors )
oss << mem_lut[(100 * used_mem) / total_mem];
{
oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB";
oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB";
if( use_colors )
oss << "#[fg=default,bg=default]";
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
}

@ -1,5 +1,8 @@
/*
/* 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.
@ -12,9 +15,9 @@
* 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.
* */
// This file was Authored by Justin Crawford <Justasic@gmail.com>
// Based on: https://github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c
*/
// 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
#error ToDo: OpenBSD. This file is incomplete and likely will not compile if you remove this error (it is here to tell you it's unfinished)
@ -27,42 +30,47 @@
#include "memory.h"
#include "../luts.h"
std::string mem_string( bool use_colors = false ) {
// These values are in bytes
int64_t total_mem = 0;
int64_t used_mem = 0;
int64_t unused_mem = 0;
int32_t inactive_mem = 0;
int32_t active_mem = 0;
int32_t free_mem = 0;
int32_t wired_mem = 0;
int32_t page_size = 0;
int32_t cache_mem = 0;
std::ostringstream oss;
std::string mem_string( bool use_colors = false )
{
// These values are in bytes
int64_t total_mem = 0;
int64_t used_mem = 0;
int64_t unused_mem = 0;
int32_t inactive_mem = 0;
int32_t active_mem = 0;
int32_t free_mem = 0;
int32_t wired_mem = 0;
int32_t page_size = 0;
int32_t cache_mem = 0;
std::ostringstream oss;
// Get total physical memory, page size, and some other needed info
GETSYSCTL("hw.realmem", total_mem);
GETSYSCTL("hw.pagesize", page_size);
// Get total physical memory, page size, and some other needed info
GETSYSCTL( "hw.realmem", total_mem );
GETSYSCTL( "hw.pagesize", page_size );
GETSYSCTL("vm.stats.vm.v_free_count", free_mem);
GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_mem);
GETSYSCTL("vm.stats.vm.v_cache_count", cache_mem);
GETSYSCTL("vm.stats.vm.v_wire_count", wired_mem);
GETSYSCTL( "vm.stats.vm.v_free_count", free_mem );
GETSYSCTL( "vm.stats.vm.v_inactive_count", inactive_mem );
GETSYSCTL( "vm.stats.vm.v_cache_count", cache_mem );
GETSYSCTL( "vm.stats.vm.v_wire_count", wired_mem );
// Get all memory which can be allocated
//unused_mem = (inactive_mem + cache_mem + free_mem) * page_size;
used_mem = (
static_cast<int64_t>(active_mem) + static_cast<int64_t>(wired_mem) +
static_cast<int64_t>(inactive_mem)) * static_cast<int64_t>(page_size);
// Get all memory which can be allocated
//unused_mem = (inactive_mem + cache_mem + free_mem) * page_size;
used_mem = (
static_cast<int64_t>( active_mem ) + static_cast<int64_t>( wired_mem ) +
static_cast<int64_t>( inactive_mem ) ) * static_cast<int64_t>( page_size );
if( use_colors )
oss << mem_lut[(100 * used_mem) / total_mem];
if( use_colors )
{
oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB";
oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB";
if( use_colors )
oss << "#[fg=default,bg=default]";
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
return oss.str();
}

@ -1,42 +1,60 @@
/* 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 <cstring>
#include "graph.h"
std::string get_graph_by_percentage(unsigned value, unsigned len)
std::string get_graph_by_percentage( unsigned value, unsigned len )
{
unsigned step = 0;
std::string bars;
unsigned bar_count = (static_cast<float>(value) / 99.9 * len);
unsigned bar_count = ( static_cast<float>(value) / 99.9 * len );
for(step; step < bar_count; step++)
for( step; step < bar_count; step++ )
{
bars.append("|");
bars.append( "|" );
}
for(step; step < len; step++)
for( step; step < len; step++ )
{
bars.append(" ");
bars.append( " " );
}
return bars;
}
std::string get_graph_by_value(unsigned value, unsigned max, unsigned len)
std::string get_graph_by_value( unsigned value, unsigned max, unsigned len )
{
unsigned step = 0;
std::string bars;
unsigned bar_count = (static_cast<float>(value / (max - 0.1)) * len);
unsigned bar_count = ( static_cast<float>( value / ( max - 0.1 ) ) * len );
for(step; step < bar_count; step++)
for( step; step < bar_count; step++ )
{
bars.append("|");
bars.append( "|" );
}
for(step; step < len; step++)
for( step; step < len; step++ )
{
bars.append(" ");
bars.append( " " );
}
return bars;

22
graph.h

@ -1,9 +1,27 @@
/* 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 GRAPH_H_
#define GRAPH_H_
#include <string>
std::string get_graph_by_percentage(unsigned, unsigned len = 10);
std::string get_graph_by_value(unsigned, unsigned, unsigned len = 10);
std::string get_graph_by_percentage( unsigned, unsigned len = 10 );
std::string get_graph_by_value( unsigned, unsigned, unsigned len = 10 );
#endif

@ -1,3 +1,21 @@
/* 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 <fstream>
#include <unistd.h> // usleep
@ -5,47 +23,48 @@
#include "cpu.h"
#include "../luts.h"
float cpu_percentage( unsigned cpu_usage_delay ) {
std::string line;
size_t substrStart = 0;
size_t substrLen;
float cpu_percentage( unsigned cpu_usage_delay )
{
std::string line;
size_t substr_start = 0;
size_t substr_len;
// cpu stats
// user, nice, system, idle
// in that order
unsigned long long stats[4];
// cpu stats
// user, nice, system, idle
// in that order
unsigned long long stats[4];
std::ifstream stat_file("/proc/stat");
getline(stat_file, line);
stat_file.close();
std::ifstream stat_file( "/proc/stat" );
getline( stat_file, line );
stat_file.close();
// skip "cpu"
substrLen = line.find_first_of(" ", 3);
// parse cpu line
for(unsigned i=0; i < 4; i++) {
substrStart = line.find_first_not_of(" ", substrLen);
substrLen = line.find_first_of (" ", substrStart);
stats[i] = std::stoll(
line.substr(substrStart, substrLen));
}
// skip "cpu"
substr_len = line.find_first_of( " ", 3 );
// parse cpu line
for( unsigned i=0; i < 4; i++ )
{
substr_start = line.find_first_not_of( " ", substr_len );
substr_len = line.find_first_of( " ", substr_start );
stats[i] = std::stoll( line.substr( substr_start, substr_len ) );
}
usleep( cpu_usage_delay );
usleep( cpu_usage_delay );
stat_file.open("/proc/stat");
getline(stat_file, line);
stat_file.close();
stat_file.open( "/proc/stat" );
getline( stat_file, line );
stat_file.close();
// skip "cpu"
substrLen = line.find_first_of(" ", 3);
// parse cpu line
for(unsigned i=0; i < 4; i++) {
substrStart = line.find_first_not_of(" ", substrLen);
substrLen = line.find_first_of (" ", substrStart);
stats[i] = std::stoll(
line.substr(substrStart, substrLen)) - stats[i];
}
// skip "cpu"
substr_len = line.find_first_of( " ", 3 );
// parse cpu line
for( unsigned i=0; i < 4; i++ )
{
substr_start = line.find_first_not_of( " ", substr_len );
substr_len = line.find_first_of ( " ", substr_start );
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[0] + stats[1] + stats[2]) /
static_cast<float>( stats[0] + stats[1] + stats[2] + stats[3] ) * 100.0;
}

@ -1,6 +1,24 @@
/* 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);
float cpu_percentage( unsigned );
#endif

@ -1,3 +1,21 @@
/* 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()?
@ -7,40 +25,46 @@
#include "load.h"
#include "../luts.h"
std::string load_string( bool use_colors = false ) {
std::ostringstream oss;
std::string load_string( bool use_colors = false )
{
std::ostringstream oss;
float f = static_cast<float>(1 << SI_LOAD_SHIFT);
float f = static_cast<float>( 1 << SI_LOAD_SHIFT );
struct sysinfo sinfo;
sysinfo(&sinfo);
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 );
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;
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]";
// 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 );
return oss.str();
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,3 +1,21 @@
/* 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_

@ -1,30 +1,51 @@
/* 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 <sys/sysinfo.h>
#include "memory.h"
#include "../luts.h"
std::string mem_string( bool use_colors = false ) {
std::ostringstream oss;
std::string mem_string( bool use_colors = false )
{
std::ostringstream oss;
struct sysinfo sinfo;
sysinfo(&sinfo);
struct sysinfo sinfo;
sysinfo(&sinfo);
unsigned int total_mem = sinfo.totalram / 1014;
unsigned int used_mem = total_mem - sinfo.freeram / 1024;
// we don't need this for now
//unsigned int unused_mem = sinfo.freeram / 1024;
if( use_colors ) {
oss << mem_lut[(100 * used_mem) / total_mem];
}
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
if( use_colors ) {
oss << "#[fg=default,bg=default]";
}
unsigned int total_mem = sinfo.totalram / 1014;
unsigned int used_mem = total_mem - sinfo.freeram / 1024;
// we don't need this for now
//unsigned int unused_mem = sinfo.freeram / 1024;
return oss.str();
if( use_colors )
{
oss << mem_lut[(100 * used_mem) / total_mem];
}
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
}

@ -1,3 +1,21 @@
/* 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_

@ -1,3 +1,21 @@
/*
* 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 <mach/mach.h>
#include <unistd.h> // usleep()
@ -6,48 +24,52 @@
// 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/
//
host_cpu_load_info_data_t _get_cpu_percentage() {
kern_return_t error;
mach_msg_type_number_t count;
host_cpu_load_info_data_t r_load;
mach_port_t mach_port;
count = HOST_CPU_LOAD_INFO_COUNT;
mach_port = mach_host_self();
error = host_statistics(mach_port, HOST_CPU_LOAD_INFO, (
host_info_t)&r_load, &count);
if (error != KERN_SUCCESS)
return host_cpu_load_info_data_t();
host_cpu_load_info_data_t _get_cpu_percentage()
{
kern_return_t error;
mach_msg_type_number_t count;
host_cpu_load_info_data_t r_load;
mach_port_t mach_port;
return r_load;
count = HOST_CPU_LOAD_INFO_COUNT;
mach_port = mach_host_self();
error = host_statistics(mach_port, HOST_CPU_LOAD_INFO,
( host_info_t )&r_load, &count );
if ( error != KERN_SUCCESS )
{
return host_cpu_load_info_data_t();
}
return r_load;
}
float cpu_percentage( unsigned int cpu_usage_delay ) {
// Get the load times from the XNU kernel
host_cpu_load_info_data_t load1 = _get_cpu_percentage();
usleep(cpu_usage_delay);
host_cpu_load_info_data_t load2 = _get_cpu_percentage();
float cpu_percentage( unsigned int cpu_usage_delay )
{
// Get the load times from the XNU kernel
host_cpu_load_info_data_t load1 = _get_cpu_percentage();
usleep( 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];
// 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];
// 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;
// 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];
// 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];
// 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;
return static_cast<float>( diff_user + diff_system + diff_nice ) /
static_cast<float>( diff_user + diff_system + diff_nice + diff_idle ) *
100.0;
}

@ -1,3 +1,20 @@
/*
* 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_

@ -1,3 +1,21 @@
/*
* 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>
@ -9,53 +27,63 @@
#include "load.h"
#include "../luts.h"
std::string load_string( bool use_colors = false ) {
std::ostringstream oss;
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
int nelem = 3;
double averages[3];
// 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 << " ";
}
// 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( "" );
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 );
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];
}
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
oss << load_line.substr( 0, 14 );
if( use_colors )
oss << "#[fg=default,bg=default]";
unsigned load_percent = static_cast< unsigned int >(
recent_load / number_of_cpus * 0.5f * 100.0f );
return oss.str();
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();
}

@ -1,3 +1,20 @@
/*
* 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_

@ -1,3 +1,21 @@
/*
* 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 <mach/mach.h>
@ -6,51 +24,55 @@
#include "memory.h"
#include "../luts.h"
std::string mem_string( bool use_colors ) {
std::ostringstream oss;
// These values are in bytes
int64_t total_mem;
int64_t used_mem;
int64_t unused_mem;
vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics_data_t vm_stats;
std::string mem_string( bool use_colors )
{
std::ostringstream oss;
// Get total physical memory
int mib[2];
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
size_t length = sizeof(int64_t);
sysctl(mib, 2, &total_mem, &length, NULL, 0);
// These values are in bytes
int64_t total_mem;
int64_t used_mem;
int64_t unused_mem;
mach_port = mach_host_self();
count = sizeof(vm_stats) / sizeof(natural_t);
if (KERN_SUCCESS == host_page_size( mach_port, &page_size) &&
KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO,
(host_info_t)&vm_stats, &count)) {
unused_mem = (int64_t)vm_stats.free_count * (int64_t)page_size;
vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics_data_t vm_stats;
used_mem = (
(int64_t)vm_stats.active_count +
(int64_t)vm_stats.inactive_count +
(int64_t)vm_stats.wire_count) *
(int64_t)page_size;
}
// Get total physical memory
int mib[2];
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
size_t length = sizeof( int64_t );
sysctl( mib, 2, &total_mem, &length, NULL, 0 );
// To kilobytes
used_mem /= 1024;
total_mem /= 1024;
mach_port = mach_host_self();
count = sizeof( vm_stats ) / sizeof( natural_t );
if( KERN_SUCCESS == host_page_size( mach_port, &page_size ) &&
KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO,
( host_info_t )&vm_stats, &count ) )
{
unused_mem = ( int64_t )vm_stats.free_count * ( int64_t )page_size;
if( use_colors )
oss << mem_lut[(100 * used_mem) / total_mem];
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
if( use_colors )
oss << "#[fg=default,bg=default]";
used_mem = ( ( int64_t )vm_stats.active_count +
( int64_t )vm_stats.inactive_count + ( int64_t )vm_stats.wire_count
) * ( int64_t )page_size;
}
return oss.str();
// To kilobytes
used_mem /= 1024;
total_mem /= 1024;
if( use_colors )
{
oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
}

@ -1,3 +1,21 @@
/*
* 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_

@ -1,4 +1,5 @@
/*
/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,7 +13,7 @@
* 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 <cstring>
#include <iostream>
@ -20,13 +21,14 @@
#include <sstream>
#include <string>
#include <cstdlib> // EXIT_SUCCESS
#include "argParse/argParse.h"
#include "version.h"
#include "graph.h"
// Tmux color lookup tables for the different metrics.
#include "luts.h"
#include "version.h"
#if defined(__APPLE__) && defined(__MACH__)
// Apple osx system
#include "osx/cpu.h"
@ -45,10 +47,9 @@
#include "linux/load.h"
#endif
#include "graph.h"
std::string cpu_string(unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false) {
std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false )
{
float percentage;
@ -61,23 +62,29 @@ std::string cpu_string(unsigned int cpu_usage_delay, unsigned int graph_lines,
percentage = cpu_percentage( cpu_usage_delay );
if( use_colors )
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
{
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
}
if( graph_lines > 0) {
oss << "[";
oss << getGraphByPercentage( unsigned(percentage), graph_lines );
oss << "]";
if( graph_lines > 0)
{
oss << "[";
oss << get_graph_by_percentage( unsigned( percentage ), graph_lines );
oss << "]";
}
oss.width( 5 );
oss << percentage;
oss << "%";
if( use_colors )
oss << "#[fg=default,bg=default]";
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
}
int main(int argc, char** argv) {
int main( int argc, char** argv )
{
using namespace ArgvParse;
unsigned cpu_usage_delay = 990000;
@ -89,53 +96,59 @@ int main(int argc, char** argv) {
// ugly, I know
std::string intro = "tmux-mem-cpu-load v";
intro += std::to_string(VERSION_MAJOR) + "." + std::to_string(VERSION_MINOR);
intro += "." + std::to_string(VERSION_PATCH) + "\n";
intro += std::to_string( VERSION_MAJOR ) + ".";
intro += std::to_string( VERSION_MINOR ) + ".";
intro += std::to_string( VERSION_PATCH ) + "\n";
intro += "Usage: tmux-mem-cpu-load [OPTIONS]";
arg.setIntroduction(intro);
arg.setIntroduction( intro );
arg.setHelpOption("h", "help", "Prints this help message");
arg.setHelpOption( "h", "help", "Prints this help message" );
// define actual options
arg.defineOption("colors", "Use tmux colors in output",
ArgvParser::NoAttribute);
arg.defineOption("i", "interval", "set tmux status refresh interval in "
"seconds. Default: 1 second", ArgvParser::RequiresValue);
arg.defineOption("g", "graph-lines", "Set how many lines should be drawn in "
"a graph. Default: 10", ArgvParser::RequiresValue);
arg.defineOption( "colors", "Use tmux colors in output",
ArgvParser::NoAttribute );
arg.defineOption( "i", "interval", "set tmux status refresh interval in "
"seconds. Default: 1 second", ArgvParser::RequiresValue );
arg.defineOption( "g", "graph-lines", "Set how many lines should be drawn in "
"a graph. Default: 10", ArgvParser::RequiresValue );
int result = arg.parse(argc, argv);
int result = arg.parse( argc, argv );
if (result != ArgvParser::Success) {
std::cerr << arg.parseErrorDescription(result);
return EXIT_FAILURE;
if( result != ArgvParser::Success )
{
std::cerr << arg.parseErrorDescription( result );
return EXIT_FAILURE;
}
// mangle arguments
if (arg.foundOption("colors"))
use_colors = true;
if( arg.foundOption( "colors" ) )
use_colors = true;
if (arg.foundOption("interval")) {
int delay = std::stoi(arg.optionValue("interval"));
if (delay < 1) {
std::cerr << "Status interval argument must be one or greater.\n";
return EXIT_FAILURE;
}
cpu_usage_delay = delay * 1000000 - 10000;
if( arg.foundOption( "interval" ) )
{
int delay = std::stoi( arg.optionValue( "interval" ) );
if( delay < 1 )
{
std::cerr << "Status interval argument must be one or greater.\n";
return EXIT_FAILURE;
}
cpu_usage_delay = delay * 1000000 - 10000;
}
if (arg.foundOption("graph-lines")) {
graph_lines = std::stoi(arg.optionValue("graph-lines"));
if( graph_lines < 0 ) {
std::cerr << "Graph lines argument must be zero or greater.\n";
return EXIT_FAILURE;
}
if( arg.foundOption( "graph-lines" ) )
{
graph_lines = std::stoi( arg.optionValue( "graph-lines" ) );
if( graph_lines < 0 )
{
std::cerr << "Graph lines argument must be zero or greater.\n";
return EXIT_FAILURE;
}
}
std::cout << mem_string( use_colors ) << ' '
<< cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' '
<< load_string( use_colors );
<< cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' '
<< load_string( use_colors );
return EXIT_SUCCESS;
}

@ -1,3 +1,21 @@
/* 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.
*/
// the configured options and settings for sysstat
#define VERSION_MAJOR @VERSION_MAJOR@
#define VERSION_MINOR @VERSION_MINOR@