use sysinfo() to get ram stats instead of parsing meminfo file.
Idea pulled from seculabird gihub user.
see:
880f8b1967
This commit is contained in:
parent
b02d0000e9
commit
e7f2dd25e8
@ -1,6 +1,5 @@
|
|||||||
#include <string>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <fstream>
|
#include <sys/sysinfo.h>
|
||||||
|
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "../luts.h"
|
#include "../luts.h"
|
||||||
@ -8,40 +7,13 @@
|
|||||||
std::string mem_string( bool use_colors = false ) {
|
std::string mem_string( bool use_colors = false ) {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
|
|
||||||
unsigned int total_mem;
|
struct sysinfo sinfo;
|
||||||
unsigned int used_mem;
|
sysinfo(&sinfo);
|
||||||
unsigned int unused_mem;
|
|
||||||
|
|
||||||
size_t substrStart;
|
unsigned int total_mem = sinfo.totalram / 1014;
|
||||||
size_t substrLen;
|
unsigned int used_mem = total_mem - sinfo.freeram / 1024;
|
||||||
|
// we don't need this for now
|
||||||
std::ifstream meminfo_file( "/proc/meminfo" );
|
//unsigned int unused_mem = sinfo.freeram / 1024;
|
||||||
|
|
||||||
std::string mem_line;
|
|
||||||
|
|
||||||
getline( meminfo_file, mem_line );
|
|
||||||
substrStart = mem_line.find_first_of( ':' ) + 1;
|
|
||||||
substrLen = mem_line.find_first_of( 'k' );
|
|
||||||
total_mem = stoi(mem_line.substr(substrStart, substrLen));
|
|
||||||
|
|
||||||
used_mem = total_mem;
|
|
||||||
|
|
||||||
for( unsigned int i = 0; i < 3; i++ ) {
|
|
||||||
getline( meminfo_file, mem_line );
|
|
||||||
// accomodate MemAvailable potentially being in lines 2-4 of
|
|
||||||
// /proc/meminfo. do this in a way to not break the original logic of the
|
|
||||||
// loop
|
|
||||||
if( mem_line.find("MemAvailable") == 0 )
|
|
||||||
i--;
|
|
||||||
else {
|
|
||||||
substrStart = mem_line.find_first_of( ':' ) + 1;
|
|
||||||
substrLen = mem_line.find_first_of( 'k' );
|
|
||||||
unused_mem = stoi(mem_line.substr(substrStart, substrLen));
|
|
||||||
used_mem -= unused_mem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
meminfo_file.close();
|
|
||||||
|
|
||||||
if( use_colors ) {
|
if( use_colors ) {
|
||||||
oss << mem_lut[(100 * used_mem) / total_mem];
|
oss << mem_lut[(100 * used_mem) / total_mem];
|
||||||
|
Loading…
Reference in New Issue
Block a user