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

Function cgroupV2PathOfProcess

base/base/cgroupsv2.cpp:30–60  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28}
29
30fs::path cgroupV2PathOfProcess()
31{
32#if defined(OS_LINUX)
33 chassert(cgroupsV2Enabled());
34 /// All PIDs assigned to a cgroup are in /sys/fs/cgroups/{cgroup_name}/cgroup.procs
35 /// A simpler way to get the membership is:
36 std::ifstream cgroup_name_file("/proc/self/cgroup");
37 if (!cgroup_name_file.is_open())
38 return {};
39 /// https://docs.kernel.org/admin-guide/cgroup-v2.html says:
40 /// /proc/$PID/cgroup” lists a process’s cgroup membership. If legacy cgroup is in use in the
41 /// system, this file may contain multiple lines, one for each hierarchy. The entry for cgroup
42 /// v2 is always in the format “0::$PATH”.
43 ///
44 /// So we're basically looking for a (v2) line with prefix "0::/", possibly among lines with
45 /// other prefixes belonging v1. Note: It is valid to have an empty name as 'root' cgroup v2.
46 static const std::string v2_prefix = "0::/";
47 std::string cgroup;
48 while (std::getline(cgroup_name_file, cgroup))
49 {
50 if (cgroup.starts_with(v2_prefix))
51 {
52 cgroup = cgroup.substr(v2_prefix.length());
53 return default_cgroups_mount / cgroup;
54 }
55 }
56 return {};
57#else
58 return {};
59#endif
60}
61
62std::optional<std::string> getCgroupsV2PathContainingFile([[maybe_unused]] std::string_view file_name)
63{

Callers 3

getCgroupsV2MemoryLimitFunction · 0.85
getCGroupLimitedCPUCoresFunction · 0.85

Calls 2

cgroupsV2EnabledFunction · 0.85
lengthMethod · 0.45

Tested by

no test coverage detected