From eaca39ba05271bfeb6af9bd7bcaf2aa47bbcc2dc Mon Sep 17 00:00:00 2001 From: Pavel Medvedev Date: Sat, 23 Oct 2021 02:54:58 +0200 Subject: [PATCH] use cgroup memory limit in getMemoryAmountOrZero Try to read the memory amount from /sys/fs/cgroup/memory/memory.limit_in_bytes See issue # --- base/base/getMemoryAmount.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/base/base/getMemoryAmount.cpp b/base/base/getMemoryAmount.cpp index 59ad10335ed..52a06389432 100644 --- a/base/base/getMemoryAmount.cpp +++ b/base/base/getMemoryAmount.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -15,6 +16,17 @@ */ uint64_t getMemoryAmountOrZero() { +#if defined(OS_LINUX) + // Try to lookup at the Cgroup limit + std::ifstream cgroup_limit("/sys/fs/cgroup/memory/memory.limit_in_bytes"); + if (cgroup_limit.is_open()) + { + uint64 amount = 0; // in case of read error + cgroup_limit >> amount; + return amount; + } +#endif + int64_t num_pages = sysconf(_SC_PHYS_PAGES); if (num_pages <= 0) return 0;