From 42275e094d65afe36402dec234398b413e79299c Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Wed, 10 Feb 2016 17:22:34 -0500 Subject: [PATCH] Memory mode output for platforms other than osx. Refactor status string generation into a common function. --- CMakeLists.txt | 2 +- common/main.cc | 4 ++- common/memory.cc | 78 +++++++++++++++++++++++++++++++++++++++++++++++ common/memory.h | 24 +++++++++++++-- freebsd/memory.cc | 20 ++---------- linux/memory.cc | 35 ++++++--------------- netbsd/memory.cc | 24 +++------------ openbsd/memory.cc | 21 ++----------- osx/memory.cc | 50 ++---------------------------- 9 files changed, 126 insertions(+), 132 deletions(-) create mode 100644 common/memory.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c2aafc..9109314 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,7 +80,7 @@ else() endif() # set common source files -set(COMMON_SOURCES "common/main.cc" "common/graph.cc") +set(COMMON_SOURCES "common/main.cc" "common/memory.cc" "common/graph.cc") # add binary tree so we find version.h include_directories("${PROJECT_BINARY_DIR}") diff --git a/common/main.cc b/common/main.cc index 24c64e1..a4a7b3a 100644 --- a/common/main.cc +++ b/common/main.cc @@ -163,7 +163,9 @@ int main( int argc, char** argv ) return EXIT_FAILURE; } - std::cout << mem_string( use_colors, mem_mode ) + MemoryStatus memory_status; + mem_status( memory_status ); + std::cout << mem_string( memory_status, mem_mode, use_colors ) << cpu_string( cpu_usage_delay, graph_lines, use_colors ) << load_string( use_colors ); diff --git a/common/memory.cc b/common/memory.cc new file mode 100644 index 0000000..23a4ea5 --- /dev/null +++ b/common/memory.cc @@ -0,0 +1,78 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "memory.h" +#include "luts.h" +#include "conversions.h" + +std::string mem_string( const MemoryStatus & mem_status, + MEMORY_MODE mode, + bool use_colors ) +{ + std::ostringstream oss; + // Change the percision for floats, for a pretty output + oss.precision( 2 ); + oss.setf( std::ios::fixed | std::ios::right ); + + if( use_colors ) + { + oss << mem_lut[static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem)]; + } + + switch( mode ) + { + case MEMORY_MODE_FREE_MEMORY: // Show free memory in MB or GB + { + const float free_mem = mem_status.total_mem - mem_status.used_mem; + const float free_mem_in_gigabytes = convert_unit( free_mem, GIGABYTES, MEGABYTES ); + + // if free memory is less than 1 GB, use MB instead + if( free_mem_in_gigabytes < 1.0f ) + { + oss << free_mem << "MB"; + } + else + { + oss << free_mem_in_gigabytes << "GB"; + } + break; + } + case MEMORY_MODE_USAGE_PERCENTAGE: + { + // Calculate the percentage of used memory + const float percentage_mem = mem_status.used_mem / + static_cast( mem_status.total_mem ) * 100.0; + + oss << percentage_mem << '%'; + break; + } + default: // Default mode, just show the used/total memory in MB + oss << static_cast< unsigned int >( mem_status.used_mem ) << '/' + << static_cast< unsigned int >( mem_status.used_mem ) << "MB"; + } + + if( use_colors ) + { + oss << "#[fg=default,bg=default]"; + } + + return oss.str(); +} + diff --git a/common/memory.h b/common/memory.h index e3d6164..8fac41a 100644 --- a/common/memory.h +++ b/common/memory.h @@ -21,6 +21,25 @@ #include +/** Memory status in megabytes */ +struct MemoryStatus +{ + float used_mem; + float total_mem; +}; + +/** Get the current memory status */ +void mem_status( MemoryStatus & status ); + + +/** Memory status string output mode. + * + * Examples: + * + * MEMORY_MODE_DEFAULT: 11156/16003MB + * MEMORY_MODE_FREE_MEMORY: + * MEMORY_MODE_USAGE_PERCENTAGE: + */ enum MEMORY_MODE { MEMORY_MODE_DEFAULT, @@ -28,7 +47,8 @@ enum MEMORY_MODE MEMORY_MODE_USAGE_PERCENTAGE }; -std::string mem_string( bool, - MEMORY_MODE mode = MEMORY_MODE_DEFAULT ); +std::string mem_string( const MemoryStatus & mem_status, + MEMORY_MODE mode = MEMORY_MODE_DEFAULT, + bool use_colors = false ); #endif diff --git a/freebsd/memory.cc b/freebsd/memory.cc index 41ca9aa..5500032 100644 --- a/freebsd/memory.cc +++ b/freebsd/memory.cc @@ -26,10 +26,9 @@ #include "getsysctl.h" #include "memory.h" -#include "luts.h" #include "conversions.h" -std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) +void mem_status( MemoryStatus & status ) { // These values are in bytes //u_int total; @@ -40,7 +39,6 @@ std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) u_int active; u_int page_size; u_int page_count; - std::ostringstream oss; // Get total physical memory, page size, and some other needed info // instead of using realmem which reports quantity of ram installed on @@ -67,18 +65,6 @@ std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) // Used memory on FreeBSD is active + wired. u_int used = ( active + wired ) * page_size; - if( use_colors ) - { - oss << mem_lut[ ( 100 * used ) / ( page_count * page_size ) ]; - } - - oss << convert_unit( used, MEGABYTES ) << '/' - << convert_unit( page_count * (page_size >> 10), MEGABYTES, KILOBYTES) << "MB"; - - if( use_colors ) - { - oss << "#[fg=default,bg=default]"; - } - - return oss.str(); + status.used_mem = convert_unit( static_cast< float >( used ), MEGABYTES ); + status.total_mem = convert_unit( static_cast< float >( page_count * (page_size >> 10) ), MEGABYTES, KILOBYTES); } diff --git a/linux/memory.cc b/linux/memory.cc index dfd4f4e..f6ac406 100644 --- a/linux/memory.cc +++ b/linux/memory.cc @@ -21,22 +21,17 @@ #include #include "memory.h" -#include "luts.h" #include "conversions.h" -std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) +void mem_status( MemoryStatus & status ) { - using std::string; - using std::ifstream; - using std::stoi; - - std::ostringstream oss; - - string line, substr; + std::string line; + std::string substr; size_t substr_start; size_t substr_len; - unsigned int total_mem, used_mem; + unsigned int total_mem; + unsigned int used_mem; /* Since linux uses some RAM for disk caching, the actuall used ram is lower * than what sysinfo(), top or free reports. htop reports the usage in a @@ -59,9 +54,9 @@ std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) * SwapFree: 60352 kB old */ - ifstream memory_info("/proc/meminfo"); + std::ifstream memory_info("/proc/meminfo"); - while( getline( memory_info, line ) ) + while( std::getline( memory_info, line ) ) { substr_start = 0; substr_len = line.find_first_of( ':' ); @@ -84,22 +79,10 @@ std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) } } - if( use_colors ) - { - oss << mem_lut[(100 * used_mem) / total_mem]; - } - // we want megabytes on output, but since the values already come as // kilobytes we need to divide them by 1024 only once, thus we use // KILOBYTES - oss << convert_unit(used_mem, MEGABYTES, KILOBYTES) << '/' - << convert_unit(total_mem, MEGABYTES, KILOBYTES) << "MB"; - - if( use_colors ) - { - oss << "#[fg=default,bg=default]"; - } - - return oss.str(); + status.used_mem = convert_unit(static_cast< float >( used_mem ), MEGABYTES, KILOBYTES); + status.total_mem = convert_unit(static_cast< float >( total_mem ), MEGABYTES, KILOBYTES); } diff --git a/netbsd/memory.cc b/netbsd/memory.cc index 40c291f..d064583 100644 --- a/netbsd/memory.cc +++ b/netbsd/memory.cc @@ -24,14 +24,11 @@ #include // uvmexp struct #include "error.h" -#include "luts.h" #include "conversions.h" #include "memory.h" -std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) +void mem_status( MemoryStatus & status ) { - std::ostringstream oss; - // get vm memory stats static int vm_totalmem[] = { CTL_VM, VM_UVMEXP2 }; struct uvmexp_sysctl mem; @@ -42,23 +39,10 @@ std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) } int64_t total_mem = ( mem.npages << mem.pageshift ); - int64_t used_mem = + 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(); - + status.used_mem = convert_unit(static_cast< float >( used_mem ), MEGABYTES ); + status.total_mem = convert_unit(static_cast< float >( total_mem ), MEGABYTES ); } diff --git a/openbsd/memory.cc b/openbsd/memory.cc index 7e521b2..709e228 100644 --- a/openbsd/memory.cc +++ b/openbsd/memory.cc @@ -37,7 +37,6 @@ #include "error.h" #include "memory.h" -#include "luts.h" #include "conversions.h" static int pageshift; @@ -47,10 +46,8 @@ static int pageshift; #endif #define pagesh(size) ((size) << pageshift) -std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) +void mem_status( MemoryStatus & status ) { - std::ostringstream oss; - // These values are in bytes int64_t total_mem = 0; int64_t used_mem = 0; @@ -106,18 +103,6 @@ std::string mem_string( bool use_colors = false, MEMORY_MODE mode ) // calculate total memory total_mem = (uint64_t) pagesh( uvmexp.npages ) << LOG1024; - if( use_colors ) - { - oss << mem_lut[( 100 * used_mem ) / total_mem]; - } - - oss << convert_unit( used_mem, MEGABYTES ) - << '/' << convert_unit( total_mem, MEGABYTES ) << "MB"; - - if( use_colors ) - { - oss << "#[fg=default,bg=default]"; - } - - return oss.str(); + status.used_mem = convert_unit(static_cast< float >( used_mem ), MEGABYTES ); + status.total_mem = convert_unit(static_cast< float >( total_mem ), MEGABYTES ); } diff --git a/osx/memory.cc b/osx/memory.cc index ba8761b..fb61f60 100644 --- a/osx/memory.cc +++ b/osx/memory.cc @@ -22,13 +22,10 @@ #include // for sysctl #include "memory.h" -#include "luts.h" #include "conversions.h" -std::string mem_string( bool use_colors, MEMORY_MODE mode ) +void mem_status( MemoryStatus & status ) { - std::ostringstream oss; - // These values are in bytes u_int64_t total_mem; float used_mem; @@ -58,47 +55,6 @@ std::string mem_string( bool use_colors, MEMORY_MODE mode ) ( vm_stats.active_count + vm_stats.wire_count ) * page_size); } - if( use_colors ) - { - oss << mem_lut[( 100 * static_cast(used_mem) ) / total_mem]; - } - - // Change the percision for floats, for a pretty output - oss.precision( 2 ); - oss.setf( std::ios::fixed | std::ios::right ); - - switch( mode ) - { - case MEMORY_MODE_FREE_MEMORY: // Show free memory in MB or GB - free_mem = total_mem - used_mem; - free_mem_in_gigabytes = convert_unit( free_mem, GIGABYTES ); - - // if free memory is less than 1 GB, use MB instead - if( free_mem_in_gigabytes < 1 ) - { - oss << convert_unit( free_mem, MEGABYTES ) << "MB"; - } - else - { - oss << free_mem_in_gigabytes << "GB"; - } - break; - case MEMORY_MODE_USAGE_PERCENTAGE: - // Calculate the percentage of used memory - percentage_mem = used_mem / - static_cast( total_mem ) * 100.0; - - oss << percentage_mem << '%'; - break; - default: // Default mode, just show the used/total memory in MB - oss << convert_unit( used_mem, MEGABYTES ) << '/' - << convert_unit( total_mem, MEGABYTES ) << "MB"; - } - - if( use_colors ) - { - oss << "#[fg=default,bg=default]"; - } - - return oss.str(); + status.used_mem = convert_unit(static_cast< float >( used_mem ), MEGABYTES ); + status.total_mem = convert_unit(static_cast< float >( total_mem ), MEGABYTES ); }