cgroups plugin now reads mountinfo to find the proper place for reading cgroup information #308
Costa Tsaousis (ktsaou) committed
Apr 29, 2016 at 02:54 UTC
8eee2e99d351c05e458e45796b699112713863a4
3 files changed
+62
-17
src/proc_self_mountinfo.c
+34
-13
@@ -26,6 +26,20 @@ struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, un
26
return NULL;
27
}
28
29
+// find the mount info with the given filesystem and mount_source
30
+// in the supplied linked list of mountinfo structures
31
+struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
32
+ struct mountinfo *mi;
33
+ uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
34
+
35
+ for(mi = root; mi ; mi = mi->next)
36
+ if(mi->filesystem_hash == filesystem_hash && mi->mount_source_hash == mount_source_hash
37
+ && !strcmp(mi->filesystem, filesystem) && !strcmp(mi->mount_source, mount_source))
38
+ return mi;
39
+
40
+ return NULL;
41
+}
42
+
43
// free a linked list of mountinfo structures
44
void mountinfo_free(struct mountinfo *mi) {
45
if(unlikely(!mi))
@@ -70,7 +84,6 @@ struct mountinfo *mountinfo_read() {
84
struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
85
86
unsigned long l, lines = procfile_lines(ff);
73
- error("MOUNTINFO: file has %u lines", lines);
87
for(l = 0; l < lines ;l++) {
88
if(procfile_linewords(ff, l) < 5)
89
continue;
@@ -100,9 +113,11 @@ struct mountinfo *mountinfo_read() {
113
114
mi->root = strdup(procfile_lineword(ff, l, w)); w++;
115
if(unlikely(!mi->root)) fatal("Cannot allocate memory");
116
+ mi->root_hash = simple_hash(mi->root);
117
118
mi->mount_point = strdup(procfile_lineword(ff, l, w)); w++;
119
if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
120
+ mi->mount_point_hash = simple_hash(mi->mount_point);
121
122
mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
123
if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
@@ -136,11 +151,15 @@ struct mountinfo *mountinfo_read() {
151
mi->optional_fields = NULL;
152
153
if(likely(*s == '-')) {
154
+ w++;
155
+
156
mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
157
if(!mi->filesystem) fatal("Cannot allocate memory");
158
+ mi->filesystem_hash = simple_hash(mi->filesystem);
159
160
mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
161
if(!mi->mount_source) fatal("Cannot allocate memory");
162
+ mi->mount_source_hash = simple_hash(mi->mount_source);
163
164
mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
165
if(!mi->super_options) fatal("Cannot allocate memory");
@@ -151,18 +170,20 @@ struct mountinfo *mountinfo_read() {
170
mi->super_options = NULL;
171
}
172
154
- //info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",
155
- // mi->id,
156
- // mi->parentid,
157
- // mi->major,
158
- // mi->minor,
159
- // mi->root,
160
- // mi->mount_point,
161
- // mi->mount_options,
162
- // mi->filesystem,
163
- // mi->mount_source,
164
- // mi->super_options
165
- //);
173
+/*
174
+ info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",
175
+ mi->id,
176
+ mi->parentid,
177
+ mi->major,
178
+ mi->minor,
179
+ mi->root,
180
+ mi->mount_point,
181
+ mi->mount_options,
182
+ mi->filesystem,
183
+ mi->mount_source,
184
+ mi->super_options
185
+ );
186
+*/
187
}
188
189
procfile_close(ff);
src/proc_self_mountinfo.h
+10
@@ -10,20 +10,30 @@ struct mountinfo {
10
unsigned long minor;
11
12
char *root; // root: root of the mount within the filesystem.
13
+ uint32_t root_hash;
14
+
15
char *mount_point; // mount point: mount point relative to the process's root.
16
+ uint32_t mount_point_hash;
17
+
18
char *mount_options; // mount options: per-mount options.
19
20
int optional_fields_count;
21
char **optional_fields; // optional fields: zero or more fields of the form "tag[:value]".
22
23
char *filesystem; // filesystem type: name of filesystem in the form "type[.subtype]".
24
+ uint32_t filesystem_hash;
25
+
26
char *mount_source; // mount source: filesystem-specific information or "none".
27
+ uint32_t mount_source_hash;
28
+
29
char *super_options; // super options: per-superblock options.
30
31
struct mountinfo *next;
32
};
33
34
extern struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, unsigned long minor);
35
+extern struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source);
36
+
37
extern void mountinfo_free(struct mountinfo *mi);
38
extern struct mountinfo *mountinfo_read();
39
src/sys_fs_cgroup.c
+18
-4
@@ -16,6 +16,7 @@
16
#include "rrd.h"
17
#include "main.h"
18
#include "popen.h"
19
+#include "proc_self_mountinfo.h"
20
21
// ----------------------------------------------------------------------------
22
// cgroup globals
@@ -42,20 +43,33 @@ void read_cgroup_plugin_configuration() {
43
cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory);
44
cgroup_enable_blkio = config_get_boolean_ondemand("plugin:cgroups", "enable blkio", cgroup_enable_blkio);
45
45
- char filename[FILENAME_MAX + 1];
46
- snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/fs/cgroup/cpuacct");
46
+ char filename[FILENAME_MAX + 1], *s;
47
+ struct mountinfo *mi, *root = mountinfo_read();
48
+
49
+ mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
50
+ if(!mi) s = "/sys/fs/cgroup/cpuacct";
51
+ else s = mi->mount_point;
52
+ snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
53
cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
54
49
- snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/fs/cgroup/blkio");
55
+ mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
56
+ if(!mi) s = "/sys/fs/cgroup/blkio";
57
+ else s = mi->mount_point;
58
+ snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
59
cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
60
52
- snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/fs/cgroup/memory");
61
+ mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
62
+ if(!mi) s = "/sys/fs/cgroup/memory";
63
+ else s = mi->mount_point;
64
+ snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
65
cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
66
67
cgroup_root_max = config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
68
cgroup_max_depth = config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
69
70
cgroup_enable_new_cgroups_detected_at_runtime = config_get_boolean("plugin:cgroups", "enable new cgroups detected at run time", cgroup_enable_new_cgroups_detected_at_runtime);
71
+
72
+ mountinfo_free(root);
73
}
74
75
// ----------------------------------------------------------------------------