Fix memory warning errors

Reviewers: buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1543
This commit is contained in:
Matej Ferencevic 2018-08-16 22:20:44 +02:00
parent 327c3c5d9b
commit 805b86f5e0
2 changed files with 22 additions and 12 deletions

View File

@ -260,12 +260,20 @@ int WithInit(int argc, char **argv,
// Start memory warning logger.
utils::Scheduler mem_log_scheduler;
if (FLAGS_memory_warning_threshold > 0) {
mem_log_scheduler.Run("Memory warning", std::chrono::seconds(3), [] {
auto free_ram_mb = utils::sysinfo::AvailableMem() / 1024;
if (free_ram_mb < FLAGS_memory_warning_threshold)
LOG(WARNING) << "Running out of available RAM, only " << free_ram_mb
<< " MB left.";
});
auto free_ram = utils::sysinfo::AvailableMemoryKilobytes();
if (free_ram) {
mem_log_scheduler.Run("Memory warning", std::chrono::seconds(3), [] {
auto free_ram = utils::sysinfo::AvailableMemoryKilobytes();
if (free_ram && *free_ram / 1024 < FLAGS_memory_warning_threshold)
LOG(WARNING) << "Running out of available RAM, only "
<< *free_ram / 1024 << " MB left.";
});
} else {
// Kernel version for the `MemAvailable` value is from: man procfs
LOG(WARNING) << "You have an older kernel version (<3.14) or the /proc "
"filesystem isn't available so remaining memory warnings "
"won't be available.";
}
}
requests::Init();

View File

@ -1,3 +1,4 @@
#include <experimental/optional>
#include <fstream>
#include <iostream>
#include <limits>
@ -8,24 +9,25 @@ namespace utils::sysinfo {
/**
* Gets the amount of available RAM in kilobytes. If the information is
* unavalable zero is returned.
* unavalable an empty value is returned.
*/
inline auto AvailableMem() {
inline std::experimental::optional<uint64_t> AvailableMemoryKilobytes() {
std::string token;
std::ifstream meminfo("/proc/meminfo");
while (meminfo >> token) {
if (token == "MemAvailable:") {
unsigned long mem;
uint64_t mem = 0;
if (meminfo >> mem) {
return mem;
} else {
return 0UL;
return std::experimental::nullopt;
}
}
meminfo.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
LOG(ERROR) << "Failed to read amount of available memory from /proc/meminfo";
return 0UL;
DLOG(WARNING)
<< "Failed to read amount of available memory from /proc/meminfo";
return std::experimental::nullopt;
}
} // namespace utils::sysinfo