From 3efd9f9031b4585ffac0c557b43d1fdfac893868 Mon Sep 17 00:00:00 2001 From: Igor Rekun Date: Mon, 6 Dec 2021 19:18:01 +0300 Subject: [PATCH 1/2] Condition memory checks on cgroup version. --- src/common/memory/MemoryUtils.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/common/memory/MemoryUtils.cpp b/src/common/memory/MemoryUtils.cpp index 43477149eff..85fe3d38dde 100644 --- a/src/common/memory/MemoryUtils.cpp +++ b/src/common/memory/MemoryUtils.cpp @@ -32,18 +32,22 @@ 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(); From e09a8e54f115d4c69bd45276b7bec95213cffb9f Mon Sep 17 00:00:00 2001 From: Igor Rekun Date: Tue, 7 Dec 2021 10:48:57 +0300 Subject: [PATCH 2/2] Fix formatting. --- src/common/memory/MemoryUtils.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/common/memory/MemoryUtils.cpp b/src/common/memory/MemoryUtils.cpp index 85fe3d38dde..e2803a15d75 100644 --- a/src/common/memory/MemoryUtils.cpp +++ b/src/common/memory/MemoryUtils.cpp @@ -33,7 +33,8 @@ StatusOr MemoryUtils::hitsHighWatermark() { double available = 0.0, total = 0.0; if (FLAGS_containerized) { bool cgroupsv2 = FileUtils::exist("/sys/fs/cgroup/cgroup.controllers"); - std::string statPath = cgroupsv2 ? "/sys/fs/cgroup/memory.stat" : "/sys/fs/cgroup/memory/memory.stat"; + 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) { @@ -41,12 +42,14 @@ StatusOr MemoryUtils::hitsHighWatermark() { cacheSize += std::stoul(sm[2].str(), NULL); } - std::string limitPath = cgroupsv2 ? "/sys/fs/cgroup/memory.max" : "/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(); - std::string usagePath = cgroupsv2 ? "/sys/fs/cgroup/memory.current" : "/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();