restore parsing of /proc/meminfo to get memory usage

See issue:
e7f2dd25e8 (commitcomment-9327932)

Unfortunately we can't pull info about cached memory from sysinfo.
Thus we need stick to /proc/meminfo parsing. See code for more comments.
This commit is contained in:
Pawel "l0ner" Soltys 2015-01-18 19:20:55 +01:00
parent ed8ed793bf
commit 7c883b924a

@ -17,6 +17,7 @@
*/ */
#include <sstream> #include <sstream>
#include <fstream>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include "memory.h" #include "memory.h"
@ -25,22 +26,73 @@
std::string mem_string( bool use_colors = false ) std::string mem_string( bool use_colors = false )
{ {
using std::string;
using std::ifstream;
using std::stoi;
std::ostringstream oss; std::ostringstream oss;
struct sysinfo sinfo; string line, substr;
sysinfo(&sinfo); size_t substr_start;
size_t substr_len;
unsigned int total_mem = MEGABYTES(sinfo.totalram); unsigned int total_mem, used_mem;
unsigned int used_mem = total_mem - MEGABYTES(sinfo.freeram);
// we don't need this for now /* Since linux uses some RAM for disk caching, the actuall used ram is lower
//unsigned int unused_mem = MEGABYTES(sinfo.freeram); * 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
* can be freed in any moment. Usually it hapens automatically, when an
* application requests memory.
* In order to calculate the ram that's actually used we need to use the
* following formula:
* total_ram - free_ram - buffered_ram - cached_ram
*
* example data, junk removed, with comments added:
*
* MemTotal: 61768 kB old
* MemFree: 1436 kB old
* MemAvailable ????? kB ??
* MemShared: 0 kB old (now always zero; not calculated)
* Buffers: 1312 kB old
* Cached: 20932 kB old
* SwapTotal: 122580 kB old
* SwapFree: 60352 kB old
*/
ifstream memory_info("/proc/meminfo");
while( getline( memory_info, line ) )
{
substr_start = 0;
substr_len = line.find_first_of( ':' );
substr = line.substr( substr_start, substr_len );
substr_start = line.find_first_not_of( " ", substr_len + 1 );
substr_len = line.find_first_of( 'k' ) - substr_start;
if( substr.compare( "MemTotal" ) == 0 )
{
// get total memory
total_mem = stoi( line.substr( substr_start, substr_len ) );
}
else if( substr.compare( "MemFree" ) == 0 )
{
used_mem = total_mem - stoi( line.substr( substr_start, substr_len ) );
}
else if( substr.compare( "Buffers" ) == 0 ||
substr.compare( "Cached" ) == 0 )
{
used_mem -= stoi( line.substr( substr_start, substr_len ) );
}
}
if( use_colors ) if( use_colors )
{ {
oss << mem_lut[(100 * used_mem) / total_mem]; oss << mem_lut[(100 * used_mem) / total_mem];
} }
oss << used_mem << '/' << total_mem << "MB"; // 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 << KILOBYTES(used_mem) << '/' << KILOBYTES(total_mem) << "MB";
if( use_colors ) if( use_colors )
{ {