make cgroups version detection more reliable (#17973)
Ilya Mashchenko committed
Jun 20, 2024 at 23:33 UTC
43cb6e87c60e6703af408cf0acb015b600e7d43e
1 file changed
+28
-11
src/collectors/cgroups.plugin/sys_fs_cgroup.c
+28
-11
@@ -131,6 +131,34 @@ static enum cgroups_systemd_setting cgroups_detect_systemd(const char *exec)
131
132
static enum cgroups_type cgroups_try_detect_version()
133
{
134
+ char filename[FILENAME_MAX + 1];
135
+ snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/fs/cgroup");
136
+ struct statfs fsinfo;
137
+
138
+ // https://github.com/systemd/systemd/blob/main/docs/CGROUP_DELEGATION.md#three-different-tree-setups-
139
+ // ├── statfs("/sys/fs/cgroup/")
140
+ // │ └── .f_type
141
+ // │ ├── CGROUP2_SUPER_MAGIC (Unified mode)
142
+ // │ └── TMPFS_MAGIC (Legacy or Hybrid mode)
143
+ // ├── statfs("/sys/fs/cgroup/unified/")
144
+ // │ └── .f_type
145
+ // │ ├── CGROUP2_SUPER_MAGIC (Hybrid mode)
146
+ // │ └── Otherwise, you're in legacy mode
147
+ if (!statfs(filename, &fsinfo)) {
148
+#if defined CGROUP2_SUPER_MAGIC
149
+ if (fsinfo.f_type == CGROUP2_SUPER_MAGIC)
150
+ return CGROUPS_V2;
151
+#endif
152
+#if defined TMPFS_MAGIC
153
+ if (fsinfo.f_type == TMPFS_MAGIC) {
154
+ // either hybrid or legacy
155
+ return CGROUPS_V1;
156
+ }
157
+#endif
158
+ }
159
+
160
+ collector_info("cgroups version: can't detect using statfs (fs type), falling back to heuristics.");
161
+
162
pid_t command_pid;
163
char buf[MAXSIZE_PROC_CMDLINE];
164
enum cgroups_systemd_setting systemd_setting;
@@ -155,17 +183,6 @@ static enum cgroups_type cgroups_try_detect_version()
183
if(!cgroups2_available)
184
return CGROUPS_V1;
185
158
-#if defined CGROUP2_SUPER_MAGIC
159
- // 2. check filesystem type for the default mountpoint
160
- char filename[FILENAME_MAX + 1];
161
- snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/fs/cgroup");
162
- struct statfs fsinfo;
163
- if (!statfs(filename, &fsinfo)) {
164
- if (fsinfo.f_type == CGROUP2_SUPER_MAGIC)
165
- return CGROUPS_V2;
166
- }
167
-#endif
168
-
186
// 3. check systemd compiletime setting
187
if ((systemd_setting = cgroups_detect_systemd("systemd --version")) == SYSTEMD_CGROUP_ERR)
188
systemd_setting = cgroups_detect_systemd(SYSTEMD_CMD_RHEL);