get_memory_size.cpp 2.04 KiB
/* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of simmc-agent
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include "agent/linux/get_memory_size.h"
int get_memory_size() {
std::ifstream meminfo_file;
std::string line;
std::string memsize;
if (open_file("/proc/meminfo", meminfo_file)) {
const std::string key = "MemTotal:";
std::size_t found;
while (getline(meminfo_file, line)) {
found = line.find(key);
if (found != std::string::npos) {
// remove key and 'kB' at the end
std::string value = line.substr(found + key.length(),
line.length() -
(found+key.length()+3));
// remove whitespaces
remove_copy(value.begin(), value.end(),
std::back_inserter(memsize), ' ');
// convert to integer before returning
return std::stoi(memsize);
}
}
// If EOF and no info returned
std::string err = "MemTotal not found in /proc/meminfo.";
throw err;
} else {
std::string err = "Failed to open /proc/meminfo.";
throw err;
}
}