2 * Code related to setting up a blkio cgroup
14 static struct fio_mutex *lock;
16 struct cgroup_member {
17 struct flist_head list;
19 unsigned int cgroup_nodelete;
22 static char *find_cgroup_mnt(struct thread_data *td)
24 char *mntpoint = NULL;
25 struct mntent *mnt, dummy;
29 f = setmntent("/proc/mounts", "r");
31 td_verror(td, errno, "setmntent /proc/mounts");
35 while ((mnt = getmntent_r(f, &dummy, buf, sizeof(buf))) != NULL) {
36 if (!strcmp(mnt->mnt_type, "cgroup") &&
37 strstr(mnt->mnt_opts, "blkio"))
42 mntpoint = smalloc_strdup(mnt->mnt_dir);
44 log_err("fio: cgroup blkio does not appear to be mounted\n");
50 static void add_cgroup(struct thread_data *td, const char *name,
51 struct flist_head *clist)
53 struct cgroup_member *cm;
55 cm = smalloc(sizeof(*cm));
56 INIT_FLIST_HEAD(&cm->list);
57 cm->root = smalloc_strdup(name);
58 if (td->o.cgroup_nodelete)
59 cm->cgroup_nodelete = 1;
61 flist_add_tail(&cm->list, clist);
65 void cgroup_kill(struct flist_head *clist)
67 struct flist_head *n, *tmp;
68 struct cgroup_member *cm;
72 flist_for_each_safe(n, tmp, clist) {
73 cm = flist_entry(n, struct cgroup_member, list);
74 if (!cm->cgroup_nodelete)
84 static char *get_cgroup_root(struct thread_data *td, char *mnt)
86 char *str = malloc(64);
89 sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.cgroup);
91 sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.name);
96 static int write_int_to_file(struct thread_data *td, const char *path,
97 const char *filename, unsigned int val,
103 sprintf(tmp, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, filename);
106 td_verror(td, errno, onerr);
110 fprintf(f, "%u", val);
116 static int cgroup_write_pid(struct thread_data *td, const char *root)
118 unsigned int val = td->pid;
120 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
124 * Move pid to root class
126 static int cgroup_del_pid(struct thread_data *td, char *mnt)
128 return cgroup_write_pid(td, mnt);
131 int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
136 *mnt = find_cgroup_mnt(td);
142 * Create container, if it doesn't exist
144 root = get_cgroup_root(td, *mnt);
145 if (mkdir(root, 0755) < 0) {
149 td_verror(td, __e, "cgroup mkdir");
150 log_err("fio: path %s\n", root);
154 add_cgroup(td, root, clist);
156 if (td->o.cgroup_weight) {
157 if (write_int_to_file(td, root, "blkio.weight",
159 "cgroup open weight"))
163 if (!cgroup_write_pid(td, root)) {
173 void cgroup_shutdown(struct thread_data *td, char **mnt)
177 if (!td->o.cgroup_weight && !td->o.cgroup)
180 cgroup_del_pid(td, *mnt);
183 static void fio_init cgroup_init(void)
185 lock = fio_mutex_init(1);
188 static void fio_exit cgroup_exit(void)
190 fio_mutex_remove(lock);