2 * Code related to setting up a blkio cgroup
13 static struct fio_sem *lock;
15 struct cgroup_member {
16 struct flist_head list;
18 unsigned int cgroup_nodelete;
21 static char *find_cgroup_mnt(struct thread_data *td)
23 char *mntpoint = NULL;
24 struct mntent *mnt, dummy;
28 f = setmntent("/proc/mounts", "r");
30 td_verror(td, errno, "setmntent /proc/mounts");
34 while ((mnt = getmntent_r(f, &dummy, buf, sizeof(buf))) != NULL) {
35 if (!strcmp(mnt->mnt_type, "cgroup") &&
36 strstr(mnt->mnt_opts, "blkio"))
41 mntpoint = smalloc_strdup(mnt->mnt_dir);
43 log_err("fio: cgroup blkio does not appear to be mounted\n");
49 static void add_cgroup(struct thread_data *td, const char *name,
50 struct flist_head *clist)
52 struct cgroup_member *cm;
57 cm = smalloc(sizeof(*cm));
60 log_err("fio: failed to allocate cgroup member\n");
64 INIT_FLIST_HEAD(&cm->list);
65 cm->root = smalloc_strdup(name);
70 if (td->o.cgroup_nodelete)
71 cm->cgroup_nodelete = 1;
73 flist_add_tail(&cm->list, clist);
77 void cgroup_kill(struct flist_head *clist)
79 struct flist_head *n, *tmp;
80 struct cgroup_member *cm;
87 flist_for_each_safe(n, tmp, clist) {
88 cm = flist_entry(n, struct cgroup_member, list);
89 if (!cm->cgroup_nodelete)
99 static char *get_cgroup_root(struct thread_data *td, char *mnt)
101 char *str = malloc(64);
104 sprintf(str, "%s/%s", mnt, td->o.cgroup);
106 sprintf(str, "%s/%s", mnt, td->o.name);
111 static int write_int_to_file(struct thread_data *td, const char *path,
112 const char *filename, unsigned int val,
118 sprintf(tmp, "%s/%s", path, filename);
121 td_verror(td, errno, onerr);
125 fprintf(f, "%u", val);
131 static int cgroup_write_pid(struct thread_data *td, const char *root)
133 unsigned int val = td->pid;
135 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
139 * Move pid to root class
141 static int cgroup_del_pid(struct thread_data *td, char *mnt)
143 return cgroup_write_pid(td, mnt);
146 int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
154 *mnt = find_cgroup_mnt(td);
160 * Create container, if it doesn't exist
162 root = get_cgroup_root(td, *mnt);
163 if (mkdir(root, 0755) < 0) {
167 td_verror(td, __e, "cgroup mkdir");
168 log_err("fio: path %s\n", root);
172 add_cgroup(td, root, clist);
174 if (td->o.cgroup_weight) {
175 if (write_int_to_file(td, root, "blkio.weight",
177 "cgroup open weight"))
181 if (!cgroup_write_pid(td, root)) {
191 void cgroup_shutdown(struct thread_data *td, char **mnt)
195 if (!td->o.cgroup_weight && !td->o.cgroup)
198 cgroup_del_pid(td, *mnt);
201 static void fio_init cgroup_init(void)
203 lock = fio_sem_init(FIO_SEM_UNLOCKED);
205 log_err("fio: failed to allocate cgroup lock\n");
208 static void fio_exit cgroup_exit(void)
210 fio_sem_remove(lock);