Merge pull request #62903 from rschu1ze/cgroup-no-subtreecontrol

Improve detection of cgroups v2 support for memory controllers
This commit is contained in:
Robert Schulze 2024-05-02 08:17:38 +00:00 committed by GitHub
commit 57dfde9759
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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