Merge pull request #32 from thewtex/mem-mode-all-platforms

Mem mode all platforms
This commit is contained in:
Matt McCormick 2016-02-15 00:00:11 -05:00
commit be0b914c49
11 changed files with 152 additions and 145 deletions

4
.gitattributes vendored Normal file

@ -0,0 +1,4 @@
*.cc whitespace=tab-in-indent,no-lf-at-eof,trailing-whitespace
*.h whitespace=tab-in-indent,no-lf-at-eof,trailing-whitespace
*.rst whitespace=tab-in-indent,no-lf-at-eof,trailing-whitespace
*.txt whitespace=tab-in-indent,no-lf-at-eof,trailing-whitespace

@ -80,7 +80,7 @@ else()
endif() endif()
# set common source files # 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 # add binary tree so we find version.h
include_directories("${PROJECT_BINARY_DIR}") include_directories("${PROJECT_BINARY_DIR}")

@ -16,6 +16,9 @@
* limitations under the License. * limitations under the License.
*/ */
#ifndef CONVERSIONS_H_
#define CONVERSIONS_H_
enum enum
{ {
BYTES = 0, BYTES = 0,
@ -33,3 +36,5 @@ inline T convert_unit( T num, int to, int from = BYTES)
} }
return num; return num;
} }
#endif

@ -85,6 +85,8 @@ void print_help()
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n" << "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
<< "-g <value>, --graph-lines <value>\n" << "-g <value>, --graph-lines <value>\n"
<< "\tSet how many lines should be drawn in a graph. Default: 10\n" << "\tSet how many lines should be drawn in a graph. Default: 10\n"
<< "-m <value>, --mem-mode <value>\n"
<< "\tSet memory display mode. 0: Default, 1: Free memory, 2: Usage percent.\n"
<< endl; << endl;
} }
@ -93,7 +95,7 @@ int main( int argc, char** argv )
unsigned cpu_usage_delay = 990000; unsigned cpu_usage_delay = 990000;
short graph_lines = 10; // max 32767 should be enough short graph_lines = 10; // max 32767 should be enough
bool use_colors = false; bool use_colors = false;
int mem_mode = MEMORY_MODE_DEFAULT; MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
static struct option long_options[] = static struct option long_options[] =
{ {
@ -105,6 +107,7 @@ int main( int argc, char** argv )
{ "colors", no_argument, NULL, 'c' }, { "colors", no_argument, NULL, 'c' },
{ "interval", required_argument, NULL, 'i' }, { "interval", required_argument, NULL, 'i' },
{ "graph-lines", required_argument, NULL, 'g' }, { "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' },
{ 0, 0, 0, 0 } // used to handle unknown long options { 0, 0, 0, 0 } // used to handle unknown long options
}; };
@ -143,7 +146,7 @@ int main( int argc, char** argv )
std::cerr << "Memory mode argument must be zero or greater.\n"; std::cerr << "Memory mode argument must be zero or greater.\n";
return EXIT_FAILURE; return EXIT_FAILURE;
} }
mem_mode = atoi( optarg ); mem_mode = static_cast< MEMORY_MODE >( atoi( optarg ) );
break; break;
case '?': case '?':
// getopt_long prints error message automatically // getopt_long prints error message automatically
@ -163,7 +166,9 @@ int main( int argc, char** argv )
return EXIT_FAILURE; 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 ) << cpu_string( cpu_usage_delay, graph_lines, use_colors )
<< load_string( use_colors ); << load_string( use_colors );

78
common/memory.cc Normal file

@ -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 <sstream>
#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<float>( 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();
}

@ -16,18 +16,39 @@
* limitations under the License. * limitations under the License.
*/ */
enum #ifndef MEMORY_H_
#define MEMORY_H_
#include <string>
/** 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, MEMORY_MODE_DEFAULT,
MEMORY_MODE_FREE_MEMORY, MEMORY_MODE_FREE_MEMORY,
MEMORY_MODE_USAGE_PERCENTAGE MEMORY_MODE_USAGE_PERCENTAGE
}; };
#ifndef MEMORY_H_ std::string mem_string( const MemoryStatus & mem_status,
#define MEMORY_H_ MEMORY_MODE mode = MEMORY_MODE_DEFAULT,
bool use_colors = false );
#include <string>
std::string mem_string( bool, int );
#endif #endif

@ -26,10 +26,9 @@
#include "getsysctl.h" #include "getsysctl.h"
#include "memory.h" #include "memory.h"
#include "luts.h"
#include "conversions.h" #include "conversions.h"
std::string mem_string( bool use_colors = false, int mode = 0 ) void mem_status( MemoryStatus & status )
{ {
// These values are in bytes // These values are in bytes
//u_int total; //u_int total;
@ -40,7 +39,6 @@ std::string mem_string( bool use_colors = false, int mode = 0 )
u_int active; u_int active;
u_int page_size; u_int page_size;
u_int page_count; u_int page_count;
std::ostringstream oss;
// Get total physical memory, page size, and some other needed info // Get total physical memory, page size, and some other needed info
// instead of using realmem which reports quantity of ram installed on // instead of using realmem which reports quantity of ram installed on
@ -67,18 +65,6 @@ std::string mem_string( bool use_colors = false, int mode = 0 )
// Used memory on FreeBSD is active + wired. // Used memory on FreeBSD is active + wired.
u_int used = ( active + wired ) * page_size; u_int used = ( active + wired ) * page_size;
if( use_colors ) 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);
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();
} }

@ -21,29 +21,24 @@
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include "memory.h" #include "memory.h"
#include "luts.h"
#include "conversions.h" #include "conversions.h"
std::string mem_string( bool use_colors = false, int mode = 0 ) void mem_status( MemoryStatus & status )
{ {
using std::string; std::string line;
using std::ifstream; std::string substr;
using std::stoi;
std::ostringstream oss;
string line, substr;
size_t substr_start; size_t substr_start;
size_t substr_len; 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 /* 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 * than what sysinfo(), top or free reports. htop reports the usage in a
* correct way. The memory used for caching doesn't count as used, since it * correct way. The memory used for caching doesn't count as used, since it
* can be freed in any moment. Usually it hapens automatically, when an * can be freed in any moment. Usually it hapens automatically, when an
* application requests memory. * application requests memory.
* In order to calculate the ram that's actually used we need to use the * In order to calculate the ram that's actually used we need to use the
* following formula: * following formula:
* total_ram - free_ram - buffered_ram - cached_ram * total_ram - free_ram - buffered_ram - cached_ram
* *
@ -59,9 +54,9 @@ std::string mem_string( bool use_colors = false, int mode = 0 )
* SwapFree: 60352 kB old * 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_start = 0;
substr_len = line.find_first_of( ':' ); substr_len = line.find_first_of( ':' );
@ -84,22 +79,10 @@ std::string mem_string( bool use_colors = false, int mode = 0 )
} }
} }
if( use_colors ) // we want megabytes on output, but since the values already come as
{
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 we need to divide them by 1024 only once, thus we use
// KILOBYTES // KILOBYTES
oss << convert_unit(used_mem, MEGABYTES, KILOBYTES) << '/' status.used_mem = convert_unit(static_cast< float >( used_mem ), MEGABYTES, KILOBYTES);
<< convert_unit(total_mem, MEGABYTES, KILOBYTES) << "MB"; status.total_mem = convert_unit(static_cast< float >( total_mem ), MEGABYTES, KILOBYTES);
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
} }

@ -24,14 +24,11 @@
#include <uvm/uvm_extern.h> // uvmexp struct #include <uvm/uvm_extern.h> // uvmexp struct
#include "error.h" #include "error.h"
#include "luts.h"
#include "conversions.h" #include "conversions.h"
#include "memory.h" #include "memory.h"
std::string mem_string( bool use_colors = false, int mode = 0 ) void mem_status( MemoryStatus & status )
{ {
std::ostringstream oss;
// get vm memory stats // get vm memory stats
static int vm_totalmem[] = { CTL_VM, VM_UVMEXP2 }; static int vm_totalmem[] = { CTL_VM, VM_UVMEXP2 };
struct uvmexp_sysctl mem; struct uvmexp_sysctl mem;
@ -42,23 +39,10 @@ std::string mem_string( bool use_colors = false, int mode = 0 )
} }
int64_t total_mem = ( mem.npages << mem.pageshift ); int64_t total_mem = ( mem.npages << mem.pageshift );
int64_t used_mem = int64_t used_mem =
( mem.active + mem.wired - mem.filepages ) << mem.pageshift; ( 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 // add 1 to used which gets lost somewhere along conversions
oss << convert_unit( used_mem, MEGABYTES ) status.used_mem = convert_unit(static_cast< float >( used_mem ), MEGABYTES );
<< '/' << convert_unit( total_mem, MEGABYTES ) << "MB"; status.total_mem = convert_unit(static_cast< float >( total_mem ), MEGABYTES );
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
} }

@ -37,7 +37,6 @@
#include "error.h" #include "error.h"
#include "memory.h" #include "memory.h"
#include "luts.h"
#include "conversions.h" #include "conversions.h"
static int pageshift; static int pageshift;
@ -47,10 +46,8 @@ static int pageshift;
#endif #endif
#define pagesh(size) ((size) << pageshift) #define pagesh(size) ((size) << pageshift)
std::string mem_string( bool use_colors = false, int mode = 0 ) void mem_status( MemoryStatus & status )
{ {
std::ostringstream oss;
// These values are in bytes // These values are in bytes
int64_t total_mem = 0; int64_t total_mem = 0;
int64_t used_mem = 0; int64_t used_mem = 0;
@ -106,18 +103,6 @@ std::string mem_string( bool use_colors = false, int mode = 0 )
// calculate total memory // calculate total memory
total_mem = (uint64_t) pagesh( uvmexp.npages ) << LOG1024; total_mem = (uint64_t) pagesh( uvmexp.npages ) << LOG1024;
if( use_colors ) status.used_mem = convert_unit(static_cast< float >( used_mem ), MEGABYTES );
{ status.total_mem = convert_unit(static_cast< float >( total_mem ), MEGABYTES );
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();
} }

@ -22,13 +22,10 @@
#include <sys/sysctl.h> // for sysctl #include <sys/sysctl.h> // for sysctl
#include "memory.h" #include "memory.h"
#include "luts.h"
#include "conversions.h" #include "conversions.h"
std::string mem_string( bool use_colors, int mode ) void mem_status( MemoryStatus & status )
{ {
std::ostringstream oss;
// These values are in bytes // These values are in bytes
u_int64_t total_mem; u_int64_t total_mem;
float used_mem; float used_mem;
@ -48,8 +45,8 @@ std::string mem_string( bool use_colors, int mode )
mach_port_t mach_port = mach_host_self(); mach_port_t mach_port = mach_host_self();
mach_msg_type_number_t count = sizeof( vm_stats ) / sizeof( natural_t ); mach_msg_type_number_t count = sizeof( vm_stats ) / sizeof( natural_t );
if( KERN_SUCCESS == host_page_size( mach_port, &page_size ) && if( KERN_SUCCESS == host_page_size( mach_port, &page_size ) &&
KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO, KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO,
( host_info_t )&vm_stats, &count ) ( host_info_t )&vm_stats, &count )
) )
{ {
//unused_mem = static_cast<u_int64_t>( vm_stats.free_count * page_size ); //unused_mem = static_cast<u_int64_t>( vm_stats.free_count * page_size );
@ -58,47 +55,6 @@ std::string mem_string( bool use_colors, int mode )
( vm_stats.active_count + vm_stats.wire_count ) * page_size); ( vm_stats.active_count + vm_stats.wire_count ) * page_size);
} }
if( use_colors ) status.used_mem = convert_unit(static_cast< float >( used_mem ), MEGABYTES );
{ status.total_mem = convert_unit(static_cast< float >( total_mem ), MEGABYTES );
oss << mem_lut[( 100 * static_cast<u_int64_t>(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<float>( 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();
} }