2 * Code related to setting up a blkio cgroup
12 static struct fio_mutex *lock;
14 struct cgroup_member {
15 struct flist_head list;
19 static char *find_cgroup_mnt(struct thread_data *td)
21 char *mntpoint = NULL;
25 f = setmntent("/proc/mounts", "r");
27 td_verror(td, errno, "setmntent /proc/mounts");
31 while ((mnt = getmntent(f)) != NULL) {
32 if (!strcmp(mnt->mnt_type, "cgroup") &&
33 strstr(mnt->mnt_opts, "blkio"))
38 mntpoint = smalloc_strdup(mnt->mnt_dir);
40 log_err("fio: cgroup blkio does not appear to be mounted\n");
46 static void add_cgroup(const char *name, struct flist_head *clist)
48 struct cgroup_member *cm;
50 cm = smalloc(sizeof(*cm));
51 INIT_FLIST_HEAD(&cm->list);
52 cm->root = smalloc_strdup(name);
55 flist_add_tail(&cm->list, clist);
59 void cgroup_kill(struct flist_head *clist)
61 struct flist_head *n, *tmp;
62 struct cgroup_member *cm;
66 flist_for_each_safe(n, tmp, clist) {
67 cm = flist_entry(n, struct cgroup_member, list);
77 static char *get_cgroup_root(struct thread_data *td, char *mnt)
79 char *str = malloc(64);
82 sprintf(str, "%s/%s", mnt, td->o.cgroup);
84 sprintf(str, "%s/%s", mnt, td->o.name);
89 static int write_int_to_file(struct thread_data *td, const char *path,
90 const char *filename, unsigned int val,
96 sprintf(tmp, "%s/%s", path, filename);
99 td_verror(td, errno, onerr);
103 fprintf(f, "%u", val);
109 static int cgroup_write_pid(struct thread_data *td, const char *root)
111 unsigned int val = td->pid;
113 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
117 * Move pid to root class
119 static int cgroup_del_pid(struct thread_data *td, char *mnt)
121 return cgroup_write_pid(td, mnt);
124 int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
129 *mnt = find_cgroup_mnt(td);
135 * Create container, if it doesn't exist
137 root = get_cgroup_root(td, *mnt);
138 if (mkdir(root, 0755) < 0) {
142 td_verror(td, __e, "cgroup mkdir");
143 log_err("fio: path %s\n", root);
147 add_cgroup(root, clist);
149 if (td->o.cgroup_weight) {
150 if (write_int_to_file(td, root, "blkio.weight",
152 "cgroup open weight"))
156 if (!cgroup_write_pid(td, root)) {
166 void cgroup_shutdown(struct thread_data *td, char **mnt)
170 if (!td->o.cgroup_weight && !td->o.cgroup)
173 cgroup_del_pid(td, *mnt);
176 static void fio_init cgroup_init(void)
178 lock = fio_mutex_init(1);
181 static void fio_exit cgroup_exit(void)
183 fio_mutex_remove(lock);