From 2c7c06c4defd5f56d9b71225917f28397766aa12 Mon Sep 17 00:00:00 2001 From: Amirsaman Memaripour Date: Tue, 18 Jan 2022 20:09:46 +0000 Subject: SERVER-60412 Support using cgroups v2 to inquire about the memory limit (cherry picked from commit 6bc31230f0cd0de66f02268c5ce0920c4f27effe) --- src/mongo/util/processinfo_linux.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/mongo/util/processinfo_linux.cpp b/src/mongo/util/processinfo_linux.cpp index fd2b1378a84..d9b97174c18 100644 --- a/src/mongo/util/processinfo_linux.cpp +++ b/src/mongo/util/processinfo_linux.cpp @@ -56,6 +56,7 @@ #include #include +#include "mongo/base/parse_number.h" #include "mongo/util/file.h" #include "mongo/util/log.h" #include "mongo/util/static_immortal.h" @@ -557,12 +558,16 @@ public: * return the actual memory we'll have available to the process. */ static unsigned long long getMemorySizeLimit() { - unsigned long long systemMemBytes = getSystemMemorySize(); - unsigned long long cgroupMemBytes = 0; - std::string cgmemlimit = readLineFromFile("/sys/fs/cgroup/memory/memory.limit_in_bytes"); - if (!cgmemlimit.empty() && - mongo::parseNumberFromString(cgmemlimit, &cgroupMemBytes).isOK()) { - return std::min(systemMemBytes, cgroupMemBytes); + const unsigned long long systemMemBytes = getSystemMemorySize(); + for (const char* file : { + "/sys/fs/cgroup/memory.max", // cgroups v2 + "/sys/fs/cgroup/memory/memory.limit_in_bytes" // cgroups v1 + }) { + unsigned long long groupMemBytes = 0; + std::string groupLimit = readLineFromFile(file); + if (!groupLimit.empty() && parseNumberFromString(groupLimit, &groupMemBytes).isOK()) { + return std::min(systemMemBytes, groupMemBytes); + } } return systemMemBytes; } -- cgit v1.2.1