]> arthur.barton.de Git - netdata.git/commitdiff
cgroups plugin now reads mountinfo to find the proper place for reading cgroup inform...
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Thu, 28 Apr 2016 23:54:34 +0000 (02:54 +0300)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Thu, 28 Apr 2016 23:54:34 +0000 (02:54 +0300)
src/proc_self_mountinfo.c
src/proc_self_mountinfo.h
src/sys_fs_cgroup.c

index 4337a2b9398352c7cb01263049bc36a67c032d06..eb87d433224f143aba4e27b950f1c66b2503b159 100644 (file)
@@ -26,6 +26,20 @@ struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, un
        return NULL;
 }
 
+// find the mount info with the given filesystem and mount_source
+// in the supplied linked list of mountinfo structures
+struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source) {
+       struct mountinfo *mi;
+       uint32_t filesystem_hash = simple_hash(filesystem), mount_source_hash = simple_hash(mount_source);
+
+       for(mi = root; mi ; mi = mi->next)
+               if(mi->filesystem_hash == filesystem_hash && mi->mount_source_hash == mount_source_hash
+                               && !strcmp(mi->filesystem, filesystem) && !strcmp(mi->mount_source, mount_source))
+                       return mi;
+
+       return NULL;
+}
+
 // free a linked list of mountinfo structures
 void mountinfo_free(struct mountinfo *mi) {
        if(unlikely(!mi))
@@ -70,7 +84,6 @@ struct mountinfo *mountinfo_read() {
        struct mountinfo *root = NULL, *last = NULL, *mi = NULL;
 
        unsigned long l, lines = procfile_lines(ff);
-       error("MOUNTINFO: file has %u lines", lines);
        for(l = 0; l < lines ;l++) {
                if(procfile_linewords(ff, l) < 5)
                        continue;
@@ -100,9 +113,11 @@ struct mountinfo *mountinfo_read() {
 
                mi->root = strdup(procfile_lineword(ff, l, w)); w++;
                if(unlikely(!mi->root)) fatal("Cannot allocate memory");
+               mi->root_hash = simple_hash(mi->root);
 
                mi->mount_point = strdup(procfile_lineword(ff, l, w)); w++;
                if(unlikely(!mi->mount_point)) fatal("Cannot allocate memory");
+               mi->mount_point_hash = simple_hash(mi->mount_point);
 
                mi->mount_options = strdup(procfile_lineword(ff, l, w)); w++;
                if(unlikely(!mi->mount_options)) fatal("Cannot allocate memory");
@@ -136,11 +151,15 @@ struct mountinfo *mountinfo_read() {
                        mi->optional_fields = NULL;
 
                if(likely(*s == '-')) {
+                       w++;
+
                        mi->filesystem = strdup(procfile_lineword(ff, l, w)); w++;
                        if(!mi->filesystem) fatal("Cannot allocate memory");
+                       mi->filesystem_hash = simple_hash(mi->filesystem);
 
                        mi->mount_source = strdup(procfile_lineword(ff, l, w)); w++;
                        if(!mi->mount_source) fatal("Cannot allocate memory");
+                       mi->mount_source_hash = simple_hash(mi->mount_source);
 
                        mi->super_options = strdup(procfile_lineword(ff, l, w)); w++;
                        if(!mi->super_options) fatal("Cannot allocate memory");
@@ -151,18 +170,20 @@ struct mountinfo *mountinfo_read() {
                        mi->super_options = NULL;
                }
 
-               //info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",
-               //     mi->id,
-               //     mi->parentid,
-               //     mi->major,
-               //     mi->minor,
-               //     mi->root,
-               //     mi->mount_point,
-               //     mi->mount_options,
-               //     mi->filesystem,
-               //     mi->mount_source,
-               //     mi->super_options
-               //);
+/*
+               info("MOUNTINFO: %u %u %u:%u root '%s', mount point '%s', mount options '%s', filesystem '%s', mount source '%s', super options '%s'",
+                    mi->id,
+                    mi->parentid,
+                    mi->major,
+                    mi->minor,
+                    mi->root,
+                    mi->mount_point,
+                    mi->mount_options,
+                    mi->filesystem,
+                    mi->mount_source,
+                    mi->super_options
+               );
+*/
        }
 
        procfile_close(ff);
index c215ad60bdfa7303e6f244ba0eda2ffd53dee357..1e83a357ee1b27cbecb689d58880e1ab6fc9e05d 100644 (file)
@@ -10,20 +10,30 @@ struct mountinfo {
        unsigned long minor;
 
        char *root;             // root: root of the mount within the filesystem.
+       uint32_t root_hash;
+
        char *mount_point;      // mount point: mount point relative to the process's root.
+       uint32_t mount_point_hash;
+
        char *mount_options;    // mount options: per-mount options.
 
        int optional_fields_count;
        char **optional_fields; // optional fields: zero or more fields of the form "tag[:value]".
 
        char *filesystem;       // filesystem type: name of filesystem in the form "type[.subtype]".
+       uint32_t filesystem_hash;
+
        char *mount_source;     // mount source: filesystem-specific information or "none".
+       uint32_t mount_source_hash;
+
        char *super_options;    // super options: per-superblock options.
 
        struct mountinfo *next;
 };
 
 extern struct mountinfo *mountinfo_find(struct mountinfo *root, unsigned long major, unsigned long minor);
+extern struct mountinfo *mountinfo_find_by_filesystem_mount_source(struct mountinfo *root, const char *filesystem, const char *mount_source);
+
 extern void mountinfo_free(struct mountinfo *mi);
 extern struct mountinfo *mountinfo_read();
 
index b09af5246a8cf583bf3143bf23e1afe967aefc71..fa044d72b905de6f26abe513bbc897b72204e49c 100644 (file)
@@ -16,6 +16,7 @@
 #include "rrd.h"
 #include "main.h"
 #include "popen.h"
+#include "proc_self_mountinfo.h"
 
 // ----------------------------------------------------------------------------
 // cgroup globals
@@ -42,20 +43,33 @@ void read_cgroup_plugin_configuration() {
        cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory);
        cgroup_enable_blkio = config_get_boolean_ondemand("plugin:cgroups", "enable blkio", cgroup_enable_blkio);
 
-       char filename[FILENAME_MAX + 1];
-       snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/fs/cgroup/cpuacct");
+       char filename[FILENAME_MAX + 1], *s;
+       struct mountinfo *mi, *root = mountinfo_read();
+
+       mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
+       if(!mi) s = "/sys/fs/cgroup/cpuacct";
+       else s = mi->mount_point;
+       snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
        cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
 
-       snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/fs/cgroup/blkio");
+       mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
+       if(!mi) s = "/sys/fs/cgroup/blkio";
+       else s = mi->mount_point;
+       snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
        cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
 
-       snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/fs/cgroup/memory");
+       mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
+       if(!mi) s = "/sys/fs/cgroup/memory";
+       else s = mi->mount_point;
+       snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
        cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
 
        cgroup_root_max = config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
        cgroup_max_depth = config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
 
        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);
+
+       mountinfo_free(root);
 }
 
 // ----------------------------------------------------------------------------