MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / getMemoryAmountOrZero

Function getMemoryAmountOrZero

base/base/getMemoryAmount.cpp:49–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47}
48
49uint64_t getMemoryAmountOrZero()
50{
51 int64_t num_pages = sysconf(_SC_PHYS_PAGES);
52 if (num_pages <= 0)
53 return 0;
54
55 int64_t page_size = getPageSize();
56 if (page_size <= 0)
57 return 0;
58
59 uint64_t memory_amount = num_pages * page_size;
60
61 if (auto total_numa_memory = DB::getNumaNodesTotalMemory(); total_numa_memory.has_value())
62 memory_amount = *total_numa_memory;
63
64 /// Respect the memory limit set by cgroups v2.
65 auto limit_v2 = getCgroupsV2MemoryLimit();
66 if (limit_v2.has_value() && *limit_v2 < memory_amount)
67 memory_amount = *limit_v2;
68 else
69 {
70 /// Cgroups v1 were replaced by v2 in 2015. The only reason we keep supporting v1 is that the transition to v2
71 /// has been slow. Caveat : Hierarchical groups as in v2 are not supported for v1, the location of the memory
72 /// limit (virtual) file is hard-coded.
73 /// TODO: check at the end of 2024 if we can get rid of v1.
74 std::ifstream limit_file_v1("/sys/fs/cgroup/memory/memory.limit_in_bytes");
75 if (limit_file_v1.is_open())
76 {
77 uint64_t limit_v1 = {};
78 if (limit_file_v1 >> limit_v1)
79 memory_amount = std::min(memory_amount, limit_v1);
80 }
81 }
82
83 return memory_amount;
84}
85
86
87uint64_t getMemoryAmount()

Callers 2

getMemoryAmountFunction · 0.85
sendErrorMethod · 0.85

Calls 4

getPageSizeFunction · 0.85
getNumaNodesTotalMemoryFunction · 0.85
getCgroupsV2MemoryLimitFunction · 0.85
minFunction · 0.50

Tested by

no test coverage detected