From 8a4d0ff0b2d8192ef58e69b7f2a3c976f3bebd5c Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Fri, 4 Dec 2009 22:13:43 +0100 Subject: [PATCH] cgroup cleanups Signed-off-by: Jens Axboe --- cgroup.c | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/cgroup.c b/cgroup.c index 6810ede2..b65b1fb8 100644 --- a/cgroup.c +++ b/cgroup.c @@ -70,35 +70,31 @@ static char *get_cgroup_root(struct thread_data *td) return str; } -static int cgroup_write_pid(struct thread_data *td, const char *root) +static int write_int_to_file(struct thread_data *td, const char *path, + const char *filename, unsigned int val, + const char *onerr) { char tmp[256]; FILE *f; - sprintf(tmp, "%s/tasks", root); + sprintf(tmp, "%s/%s", path, filename); f = fopen(tmp, "w"); if (!f) { - td_verror(td, errno, "cgroup open tasks"); + td_verror(td, errno, onerr); return 1; } - fprintf(f, "%d", td->pid); + fprintf(f, "%u", val); fclose(f); return 0; + } -/* - * Add pid to given class - */ -static int cgroup_add_pid(struct thread_data *td) +static int cgroup_write_pid(struct thread_data *td, const char *root) { - char *root; - int ret; + unsigned int val = td->pid; - root = get_cgroup_root(td); - ret = cgroup_write_pid(td, root); - free(root); - return ret; + return write_int_to_file(td, root, "tasks", val, "cgroup write pid"); } /* @@ -111,8 +107,7 @@ static int cgroup_del_pid(struct thread_data *td) int cgroup_setup(struct thread_data *td, struct flist_head *clist) { - char *root, tmp[256]; - FILE *f; + char *root; if (cgroup_check_fs(td)) { log_err("fio: blkio cgroup mount point %s not valid\n", @@ -135,23 +130,17 @@ int cgroup_setup(struct thread_data *td, struct flist_head *clist) add_cgroup(root, clist); if (td->o.cgroup_weight) { - sprintf(tmp, "%s/blkio.weight", root); - f = fopen(tmp, "w"); - if (!f) { - td_verror(td, errno, "cgroup open weight"); + if (write_int_to_file(td, root, "blkio.weight", + td->o.cgroup_weight, + "cgroup open weight")) goto err; - } - - fprintf(f, "%d", td->o.cgroup_weight); - fclose(f); } - free(root); - - if (cgroup_add_pid(td)) - return 1; + if (!cgroup_write_pid(td, root)) { + free(root); + return 0; + } - return 0; err: free(root); return 1; -- 2.25.1