Fixing reading of /proc/meminfo

This commit is contained in:
Mike Kot 2021-08-06 22:33:21 +03:00
parent 71e5cfe3ca
commit e2cee2576d

View File

@ -779,43 +779,60 @@ void AsynchronousMetrics::update(std::chrono::system_clock::time_point update_ti
uint64_t kb = 0; uint64_t kb = 0;
readText(kb, *meminfo); readText(kb, *meminfo);
if (kb)
if (!kb)
{ {
skipWhitespaceIfAny(*meminfo, true); skipToNextLineOrEOF(*meminfo);
assertString("kB", *meminfo); continue;
}
uint64_t bytes = kb * 1024; skipWhitespaceIfAny(*meminfo, true);
if (name == "MemTotal:") /**
{ * Not all entries in /proc/meminfo contain the kB suffix, e.g.
new_values["OSMemoryTotal"] = bytes; * HugePages_Total: 0
} * HugePages_Free: 0
else if (name == "MemFree:") * We simply skip such entries as they're not needed
{ */
/// We cannot simply name this metric "Free", because it confuses users. if (*meminfo->position() == '\n')
/// See https://www.linuxatemyram.com/ {
/// For convenience we also provide OSMemoryFreePlusCached, that should be somewhat similar to OSMemoryAvailable. skipToNextLineOrEOF(*meminfo);
continue;
}
free_plus_cached_bytes += bytes; assertString("kB", *meminfo);
new_values["OSMemoryFreeWithoutCached"] = bytes;
} uint64_t bytes = kb * 1024;
else if (name == "MemAvailable:")
{ if (name == "MemTotal:")
new_values["OSMemoryAvailable"] = bytes; {
} new_values["OSMemoryTotal"] = bytes;
else if (name == "Buffers:") }
{ else if (name == "MemFree:")
new_values["OSMemoryBuffers"] = bytes; {
} /// We cannot simply name this metric "Free", because it confuses users.
else if (name == "Cached:") /// See https://www.linuxatemyram.com/
{ /// For convenience we also provide OSMemoryFreePlusCached, that should be somewhat similar to OSMemoryAvailable.
free_plus_cached_bytes += bytes;
new_values["OSMemoryCached"] = bytes; free_plus_cached_bytes += bytes;
} new_values["OSMemoryFreeWithoutCached"] = bytes;
else if (name == "SwapCached:") }
{ else if (name == "MemAvailable:")
new_values["OSMemorySwapCached"] = bytes; {
} new_values["OSMemoryAvailable"] = bytes;
}
else if (name == "Buffers:")
{
new_values["OSMemoryBuffers"] = bytes;
}
else if (name == "Cached:")
{
free_plus_cached_bytes += bytes;
new_values["OSMemoryCached"] = bytes;
}
else if (name == "SwapCached:")
{
new_values["OSMemorySwapCached"] = bytes;
} }
skipToNextLineOrEOF(*meminfo); skipToNextLineOrEOF(*meminfo);