Autodetect cgroup blkio mount point
authorJens Axboe <jens.axboe@oracle.com>
Mon, 7 Dec 2009 07:01:26 +0000 (08:01 +0100)
committerJens Axboe <jens.axboe@oracle.com>
Mon, 7 Dec 2009 07:01:26 +0000 (08:01 +0100)
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
HOWTO
cgroup.c
cgroup.h
fio.1
fio.c
fio.h
options.c

diff --git a/HOWTO b/HOWTO
index 7a7d14ea0816080d794d25f4d899603e2eee57a8..0b40f758bbd3604f5fa9c0572d4296c753072dea 100644 (file)
--- a/HOWTO
+++ b/HOWTO
@@ -1004,18 +1004,13 @@ continue_on_error=bool  Normally fio will exit the job on the first observed
                given in the stats is the first error that was hit during the
                run.
 
-cgroup_root=str        Root of the mounted blkio cgroup file systems. This is a Linux
-               specific IO controller. If your system doesn't have it mounted,
-               you can do so with:
+cgroup=str     Add job to this control group. If it doesn't exist, it will
+               be created. The system must have a mounted cgroup blkio
+               mount point for this to work. If your system doesn't have it
+               mounted, you can do so with:
 
                # mount -t cgroup -o blkio none /cgroup
 
-               The cgroup_root defaults to /cgroup, if mounted elsewhere
-               please specify this option.
-
-cgroup=str     Add job to this control group. If it doesn't exist, it will
-               be created.
-
 cgroup_weight=int      Set the weight of the cgroup to this value. See
                the documentation that comes with the kernel, allowed values
                are in the range of 100..1000.
index 29d89dd3e316c4ec8c6dfc385488129beb6f193a..14bbc57bd0862a46cac3ea0fbd9162288c77341f 100644 (file)
--- a/cgroup.c
+++ b/cgroup.c
@@ -3,6 +3,7 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <mntent.h>
 #include "fio.h"
 #include "flist.h"
 #include "cgroup.h"
@@ -15,6 +16,33 @@ struct cgroup_member {
        char *root;
 };
 
+static char *find_cgroup_mnt(struct thread_data *td)
+{
+       char *mntpoint = NULL;
+       struct mntent *mnt;
+       FILE *f;
+
+       f = setmntent("/proc/mounts", "r");
+       if (!f) {
+               td_verror(td, errno, "setmntent /proc/mounts");
+               return NULL;
+       }
+
+       while ((mnt = getmntent(f)) != NULL) {
+               if (!strcmp(mnt->mnt_type, "cgroup") &&
+                   strstr(mnt->mnt_opts, "blkio"))
+                       break;
+       }
+
+       if (mnt)
+               mntpoint = smalloc_strdup(mnt->mnt_dir);
+       else
+               log_err("fio: cgroup blkio does not appear to be mounted\n");
+
+       endmntent(f);
+       return mntpoint;
+}
+
 static void add_cgroup(const char *name, struct flist_head *clist)
 {
        struct cgroup_member *cm;
@@ -46,26 +74,14 @@ void cgroup_kill(struct flist_head *clist)
        fio_mutex_up(lock);
 }
 
-/*
- * Check if the given root appears valid
- */
-static int cgroup_check_fs(struct thread_data *td)
-{
-       struct stat sb;
-       char tmp[256];
-
-       sprintf(tmp, "%s/tasks", td->o.cgroup_root);
-       return stat(tmp, &sb);
-}
-
-static char *get_cgroup_root(struct thread_data *td)
+static char *get_cgroup_root(struct thread_data *td, char *mnt)
 {
        char *str = malloc(64);
 
        if (td->o.cgroup)
-               sprintf(str, "%s/%s", td->o.cgroup_root, td->o.cgroup);
+               sprintf(str, "%s/%s", mnt, td->o.cgroup);
        else
-               sprintf(str, "%s/%s", td->o.cgroup_root, td->o.name);
+               sprintf(str, "%s/%s", mnt, td->o.name);
 
        return str;
 }
@@ -100,30 +116,31 @@ static int cgroup_write_pid(struct thread_data *td, const char *root)
 /*
  * Move pid to root class
  */
-static int cgroup_del_pid(struct thread_data *td)
+static int cgroup_del_pid(struct thread_data *td, char *mnt)
 {
-       return cgroup_write_pid(td, td->o.cgroup_root);
+       return cgroup_write_pid(td, mnt);
 }
 
-int cgroup_setup(struct thread_data *td, struct flist_head *clist)
+int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
 {
        char *root;
 
-       if (cgroup_check_fs(td)) {
-               log_err("fio: blkio cgroup mount point %s not valid\n",
-                                                       td->o.cgroup_root);
-               return 1;
+       if (!*mnt) {
+               *mnt = find_cgroup_mnt(td);
+               if (!*mnt)
+                       return 1;
        }
 
        /*
         * Create container, if it doesn't exist
         */
-       root = get_cgroup_root(td);
+       root = get_cgroup_root(td, *mnt);
        if (mkdir(root, 0755) < 0) {
                int __e = errno;
 
                if (__e != EEXIST) {
                        td_verror(td, __e, "cgroup mkdir");
+                       log_err("fio: path %s\n", root);
                        goto err;
                }
        } else
@@ -146,14 +163,14 @@ err:
        return 1;
 }
 
-void cgroup_shutdown(struct thread_data *td)
+void cgroup_shutdown(struct thread_data *td, char **mnt)
 {
-       if (cgroup_check_fs(td))
+       if (*mnt == NULL)
                return;
        if (!td->o.cgroup_weight && !td->o.cgroup)
                return;
 
-       cgroup_del_pid(td);
+       cgroup_del_pid(td, *mnt);
 }
 
 static void fio_init cgroup_init(void)
index b246ef8607d044f3f15e3a51ac9f8be7b6eefde3..f7ad210dca003d0d72b240701b3ff848386231b2 100644 (file)
--- a/cgroup.h
+++ b/cgroup.h
@@ -3,20 +3,21 @@
 
 #ifdef FIO_HAVE_CGROUPS
 
-int cgroup_setup(struct thread_data *td, struct flist_head *list);
-void cgroup_shutdown(struct thread_data *td);
+int cgroup_setup(struct thread_data *, struct flist_head *, char **);
+void cgroup_shutdown(struct thread_data *, char **);
 
 void cgroup_kill(struct flist_head *list);
 
 #else
 
-static inline int cgroup_setup(struct thread_data *td, struct flist_head *list);
+static inline int cgroup_setup(struct thread_data *td, struct flist_head *list,
+                              char **mnt)
 {
        td_verror(td, EINVAL, "cgroup_setup");
        return 1;
 }
 
-static inline void cgroup_shutdown(struct thread_data *td)
+static inline void cgroup_shutdown(struct thread_data *td, char **mnt)
 {
 }
 
diff --git a/fio.1 b/fio.1
index 648b4e9c6d73fb3c040aed3167e8d925a98e1e17..2e0a283d527b9dffeca64221fcbf359e3a892670 100644 (file)
--- a/fio.1
+++ b/fio.1
@@ -725,18 +725,12 @@ entering the kernel with a gettimeofday() call. The CPU set aside for doing
 these time calls will be excluded from other uses. Fio will manually clear it
 from the CPU mask of other jobs.
 .TP
-.BI cgroup_root \fR=\fPstr
-Root of the mounted blkio cgroup file systems. This is a Linux
-specific IO controller. If your system doesn't have it mounted,
-you can do so with:
-
-# mount -t cgroup -o blkio none /cgroup
-
-The cgroup_root defaults to /cgroup, if mounted elsewhere please specify this
-option.
-.TP
 .BI cgroup \fR=\fPstr
 Add job to this control group. If it doesn't exist, it will be created.
+The system must have a mounted cgroup blkio mount point for this to work. If
+your system doesn't have it mounted, you can do so with:
+
+# mount -t cgroup -o blkio none /cgroup
 .TP
 .BI cgroup_weight \fR=\fPint
 Set the weight of the cgroup to this value. See the documentation that comes
diff --git a/fio.c b/fio.c
index bcc130bbde49297c2576b0081de0767289515931..7d4c96a2b8a5045cda742f4be7842deb66529637 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -62,6 +62,7 @@ static int exit_value;
 static struct itimerval itimer;
 static pthread_t gtod_thread;
 static struct flist_head *cgroup_list;
+static char *cgroup_mnt;
 
 struct io_log *agg_io_log[2];
 
@@ -1077,7 +1078,7 @@ static void *thread_main(void *data)
                }
        }
 
-       if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list))
+       if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
                goto err;
 
        if (nice(td->o.nice) == -1) {
@@ -1209,7 +1210,7 @@ err:
        close_and_free_files(td);
        close_ioengine(td);
        cleanup_io_u(td);
-       cgroup_shutdown(td);
+       cgroup_shutdown(td, &cgroup_mnt);
 
        if (td->o.cpumask_set) {
                int ret = fio_cpuset_exit(&td->o.cpumask);
@@ -1674,6 +1675,8 @@ int main(int argc, char *argv[])
 
        cgroup_kill(cgroup_list);
        sfree(cgroup_list);
+       if (cgroup_mnt)
+               sfree(cgroup_mnt);
 
        fio_mutex_remove(startup_mutex);
        fio_mutex_remove(writeout_mutex);
diff --git a/fio.h b/fio.h
index 17aeaadae07cedf53cd137ed23475bf2b622e86d..b1af7b1bb8935ddfe1ac384f844a91f6c14e7eb5 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -275,7 +275,6 @@ struct thread_options {
        /*
         * blkio cgroup support
         */
-       char *cgroup_root;
        char *cgroup;
        unsigned int cgroup_weight;
 };
index 77cfd424ed2b9e2de6506f6961a182d89a1614d4..16908a07710a5e525ba666aa82fa4235c3f6c3c0 100644 (file)
--- a/options.c
+++ b/options.c
@@ -1726,13 +1726,6 @@ static struct fio_option options[] = {
                },
                .help   = "Select a specific builtin performance test",
        },
-       {
-               .name   = "cgroup_root",
-               .type   = FIO_OPT_STR_STORE,
-               .off1   = td_var_offset(cgroup_root),
-               .help   = "Root of mounted blkio cgroup",
-               .def    = "/cgroup",
-       },
        {
                .name   = "cgroup",
                .type   = FIO_OPT_STR_STORE,