Recursively check cgroups.controllers files for memory controllers

This commit is contained in:
Robert Schulze 2024-04-24 10:01:24 +00:00
parent 0ac60137f0
commit 2510fc85b0
No known key found for this signature in database
GPG Key ID: 26703B55FB13728A

View File

@ -23,18 +23,19 @@ 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 enabled for the current cgroup. Check the bottom-most nested "cgroup.controllers"
/// file.
std::string cgroup = cgroupV2OfProcess();
auto cgroup_dir = cgroup.empty() ? default_cgroups_mount : (default_cgroups_mount / cgroup);
std::ifstream controllers_file(cgroup_dir / "cgroup.controllers");
if (!controllers_file.is_open())
return false;
std::string controllers;
std::getline(subtree_control_file, controllers);
if (controllers.find("memory") == std::string::npos)
return false;
std::getline(controllers_file, controllers);
if (controllers.find("memory") != std::string::npos)
return true;
return false;
#else
return false;
#endif