From e33924fb7ef7c81f78f82be6126e2ba6a343a4a2 Mon Sep 17 00:00:00 2001 From: Igor Rekun Date: Wed, 8 Dec 2021 06:27:57 +0300 Subject: [PATCH] Condition memory checks on cgroup version. (#3419) * Condition memory checks on cgroup version. * Fix formatting. --- src/common/memory/MemoryUtils.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/common/memory/MemoryUtils.cpp b/src/common/memory/MemoryUtils.cpp index 43477149eff..e2803a15d75 100644 --- a/src/common/memory/MemoryUtils.cpp +++ b/src/common/memory/MemoryUtils.cpp @@ -32,18 +32,25 @@ StatusOr MemoryUtils::hitsHighWatermark() { } double available = 0.0, total = 0.0; if (FLAGS_containerized) { - FileUtils::FileLineIterator iter("/sys/fs/cgroup/memory/memory.stat", &reTotalCache); + bool cgroupsv2 = FileUtils::exist("/sys/fs/cgroup/cgroup.controllers"); + std::string statPath = + cgroupsv2 ? "/sys/fs/cgroup/memory.stat" : "/sys/fs/cgroup/memory/memory.stat"; + FileUtils::FileLineIterator iter(statPath, &reTotalCache); uint64_t cacheSize = 0; for (; iter.valid(); ++iter) { auto& sm = iter.matched(); cacheSize += std::stoul(sm[2].str(), NULL); } - auto limitStatus = MemoryUtils::readSysContents("/sys/fs/cgroup/memory/memory.limit_in_bytes"); + std::string limitPath = + cgroupsv2 ? "/sys/fs/cgroup/memory.max" : "/sys/fs/cgroup/memory/memory.limit_in_bytes"; + auto limitStatus = MemoryUtils::readSysContents(limitPath); NG_RETURN_IF_ERROR(limitStatus); uint64_t limitInBytes = std::move(limitStatus).value(); - auto usageStatus = MemoryUtils::readSysContents("/sys/fs/cgroup/memory/memory.usage_in_bytes"); + std::string usagePath = + cgroupsv2 ? "/sys/fs/cgroup/memory.current" : "/sys/fs/cgroup/memory/memory.usage_in_bytes"; + auto usageStatus = MemoryUtils::readSysContents(usagePath); NG_RETURN_IF_ERROR(usageStatus); uint64_t usageInBytes = std::move(usageStatus).value();