From eaca39ba05271bfeb6af9bd7bcaf2aa47bbcc2dc Mon Sep 17 00:00:00 2001 From: Pavel Medvedev Date: Sat, 23 Oct 2021 02:54:58 +0200 Subject: [PATCH 1/2] 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; From 705c1b957d89712884e7b100151f826c5b581a6f Mon Sep 17 00:00:00 2001 From: alexey-milovidov Date: Sat, 23 Oct 2021 05:27:15 +0300 Subject: [PATCH 2/2] Update getMemoryAmount.cpp --- base/base/getMemoryAmount.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/base/getMemoryAmount.cpp b/base/base/getMemoryAmount.cpp index 52a06389432..8240f82fc67 100644 --- a/base/base/getMemoryAmount.cpp +++ b/base/base/getMemoryAmount.cpp @@ -21,7 +21,7 @@ uint64_t getMemoryAmountOrZero() 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 + uint64_t amount = 0; // in case of read error cgroup_limit >> amount; return amount; }