8000 24.3 backport of #66237 - Fix detection of number of CPUs in containers by Enmk · Pull Request #420 · Altinity/ClickHouse · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions base/base/cgroupsv2.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <base/cgroupsv2.h>
#include <base/cgroupsv2.h>

#include <base/defines.h>

Expand All @@ -23,15 +23,17 @@ bool cgroupsV2MemoryControllerEnabled()
{
#if defined(OS_LINUX)
chassert(cgroupsV2Enabled());
/// According to https://docs.kernel.org/admin-guide/cgroup-v2.html:
/// - file 'cgroup.controllers' defines which controllers *can* be enabled
/// - file 'cgroup.subtree_control' defines which controllers *are* enabled
/// Caveat: nested groups may disable controllers. For simplicity, check only the top-level group.
std::ifstream subtree_control_file(default_cgroups_mount / "cgroup.subtree_control");
if (!subtree_control_file.is_open())
/// According to https://docs.kernel.org/admin-guide/cgroup-v2.html, file "cgroup.controllers" defines which controllers are available
/// for the current + child cgroups. The set of available controllers can be restricted from level to level using file
/// "cgroups.subtree_control". It is therefore sufficient to check the bottom-most nested "cgroup.controllers" file.
auto cgroup_dir = currentCGroupV2Path();
if (cgroup_dir.empty())
return false;
std::ifstream controllers_file(cgroup_dir / "cgroup.controllers");
if (!controllers_file.is_open())
return false;
std::string controllers;
std::getline(subtree_control_file, controllers);
std::getline(controllers_file, controllers);
if (controllers.find("memory") == std::string::npos)
return false;
return true;
Expand All @@ -40,25 +42,27 @@ bool cgroupsV2MemoryControllerEnabled()
#endif
}

std::string cgroupV2OfProcess()
std::filesystem::path currentCGroupV2Path()
{
#if defined(OS_LINUX)
chassert(cgroupsV2Enabled());
/// All PIDs assigned to a cgroup are in /sys/fs/cgroups/{cgroup_name}/cgroup.procs
/// A simpler way to get the membership is:
std::ifstream cgroup_name_file("/proc/self/cgroup");
if (!cgroup_name_file.is_open())
return "";
return {};
/// With cgroups v2, there will be a *single* line with prefix "0::/"
/// (see https://docs.kernel.org/admin-guide/cgroup-v2.html)
std::string cgroup;
std::getline(cgroup_name_file, cgroup);
static const std::string v2_prefix = "0::/";
if (!cgroup.starts_with(v2_prefix))
return "";
return {};

// the 'root' cgroup can have empty path, which is valid
cgroup = cgroup.substr(v2_prefix.length());
return cgroup;
return default_cgroups_mount / cgroup;
#else
return "";
return {};
#endif
}
6 changes: 3 additions & 3 deletions base/base/cgroupsv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bool cgroupsV2Enabled();
/// Assumes that cgroupsV2Enabled() is enabled.
bool cgroupsV2MemoryControllerEnabled();

/// Which cgroup does the process belong to?
/// Returns an empty string if the cgroup cannot be determined.
/// Detects which cgroup the process belong and returns the path to it in sysfs (for cgroups v2).
/// Returns an empty path if the cgroup cannot be determined.
/// Assumes that cgroupsV2Enabled() is enabled.
std::string cgroupV2OfProcess();
std::filesystem::path currentCGroupV2Path();
6 changes: 4 additions & 2 deletions base/base/getMemoryAmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ std::optional<uint64_t> getCgroupsV2MemoryLimit()
if (!cgroupsV2MemoryControllerEnabled())
return {};

std::string cgroup = cgroupV2OfProcess();
auto current_cgroup = cgroup.empty() ? default_cgroups_mount : (default_cgroups_mount / cgroup);
auto current_cgroup = currentCGroupV2Path();

if (current_cgroup.empty())
return {};

/// Open the bottom-most nested memory limit setting file. If there is no such file at the current
/// level, try again at the parent level as memory settings are inherited.
Expand Down
5 changes: 3 additions & 2 deletions src/Common/CgroupsMemoryUsageObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ std::optional<std::string> getCgroupsV2FileName()
if (!cgroupsV2MemoryControllerEnabled())
return {};

String cgroup = cgroupV2OfProcess();
auto current_cgroup = cgroup.empty() ? default_cgroups_mount : (default_cgroups_mount / cgroup);
auto current_cgroup = currentCGroupV2Path();
if (current_cgroup.empty())
return {};

/// Return the bottom-most nested current memory file. If there is no such file at the current
/// level, try again at the parent level as memory settings are inherited.
Expand Down
10 changes: 5 additions & 5 deletions src/Common/getNumberOfPhysicalCPUCores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ uint32_t getCGroupLimitedCPUCores(unsigned default_cpu_count)
/// cgroupsv2
if (cgroupsV2Enabled())
{
/// First, we identify the cgroup the process belongs
std::string cgroup = cgroupV2OfProcess();
if (cgroup.empty())
/// First, we identify the path of the cgroup the process belongs
auto cgroup_path = currentCGroupV2Path();
if (cgroup_path.empty())
return default_cpu_count;

auto current_cgroup = cgroup.empty() ? default_cgroups_mount : (default_cgroups_mount / cgroup);
auto current_cgroup = cgroup_path;

// Looking for cpu.max in directories from the current cgroup to the top level
// It does not stop on the first time since the child could have a greater value than parent
Expand All @@ -62,7 +62,7 @@ uint32_t getCGroupLimitedCPUCores(unsigned default_cpu_count)
}
current_cgroup = current_cgroup.parent_path();
}
current_cgroup = default_cgroups_mount / cgroup;
current_cgroup = cgroup_path;
// Looking for cpuset.cpus.effective in directories from the current cgroup to the top level
while (current_cgroup != default_cgroups_mount.parent_path())
{
Expand Down
0