Memory mode output for platforms other than osx.
Refactor status string generation into a common function.
This commit is contained in:
parent
fb8e3c6ad0
commit
42275e094d
@ -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}")
|
||||||
|
@ -163,7 +163,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
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();
|
||||||
|
}
|
||||||
|
|
@ -21,6 +21,25 @@
|
|||||||
|
|
||||||
#include <string>
|
#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
|
enum MEMORY_MODE
|
||||||
{
|
{
|
||||||
MEMORY_MODE_DEFAULT,
|
MEMORY_MODE_DEFAULT,
|
||||||
@ -28,7 +47,8 @@ enum MEMORY_MODE
|
|||||||
MEMORY_MODE_USAGE_PERCENTAGE
|
MEMORY_MODE_USAGE_PERCENTAGE
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string mem_string( bool,
|
std::string mem_string( const MemoryStatus & mem_status,
|
||||||
MEMORY_MODE mode = MEMORY_MODE_DEFAULT );
|
MEMORY_MODE mode = MEMORY_MODE_DEFAULT,
|
||||||
|
bool use_colors = false );
|
||||||
|
|
||||||
#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, MEMORY_MODE mode )
|
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, MEMORY_MODE mode )
|
|||||||
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, MEMORY_MODE mode )
|
|||||||
// 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,22 +21,17 @@
|
|||||||
#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, MEMORY_MODE mode )
|
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
|
||||||
@ -59,9 +54,9 @@ std::string mem_string( bool use_colors = false, MEMORY_MODE mode )
|
|||||||
* 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, 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
|
// 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, MEMORY_MODE mode )
|
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;
|
||||||
@ -45,20 +42,7 @@ std::string mem_string( bool use_colors = false, MEMORY_MODE mode )
|
|||||||
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, MEMORY_MODE mode )
|
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, MEMORY_MODE mode )
|
|||||||
// 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, MEMORY_MODE 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;
|
||||||
@ -58,47 +55,6 @@ std::string mem_string( bool use_colors, MEMORY_MODE 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();
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user