Skip to content

Commit

Permalink
Resurrect v1
Browse files Browse the repository at this point in the history
  • Loading branch information
rschu1ze committed Jan 31, 2024
1 parent ea98047 commit b204809
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion base/base/getMemoryAmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,25 @@ uint64_t getMemoryAmountOrZero()
uint64_t memory_amount = num_pages * page_size;

/// Respect the memory limit set by cgroups v2.
/// Cgroups v1 is dead since many years and its limits are not considered for simplicity.
auto limit = getCgroupsV2MemoryLimit("memory.max");

if (limit.has_value() && *limit < memory_amount)
memory_amount = *limit;
else
{
/// Cgroups v1 were replaced by v2 in 2015. The only reason we keep supporting v1 is that the transition to v2
/// has been slow. Caveat : Hierarchical groups as in v2 are not supported for v1, the location of the memory
/// limit (virtual) file is hard-coded.
/// TODO: check at the end of 2024 if we can get rid of v1.
std::ifstream cgroup_limit("/sys/fs/cgroup/memory/memory.limit_in_bytes");
if (cgroup_limit.is_open())
{
uint64_t memory_limit = 0; // in case of read error
cgroup_limit >> memory_limit;
if (memory_limit > 0 && memory_limit < memory_amount)
memory_amount = memory_limit;
}
}

return memory_amount;
}
Expand Down

0 comments on commit b204809

Please sign in to comment.