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;
58 cm = smalloc(sizeof(*cm));
61 log_err("fio: failed to allocate cgroup member\n");
65 INIT_FLIST_HEAD(&cm->list);
66 cm->root = smalloc_strdup(name);
71 if (td->o.cgroup_nodelete)
72 cm->cgroup_nodelete = 1;
74 flist_add_tail(&cm->list, clist);
78 void cgroup_kill(struct flist_head *clist)
80 struct flist_head *n, *tmp;
81 struct cgroup_member *cm;
88 flist_for_each_safe(n, tmp, clist) {
89 cm = flist_entry(n, struct cgroup_member, list);
90 if (!cm->cgroup_nodelete)
100 static char *get_cgroup_root(struct thread_data *td, char *mnt)
102 char *str = malloc(64);
105 sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.cgroup);
107 sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.name);
112 static int write_int_to_file(struct thread_data *td, const char *path,
113 const char *filename, unsigned int val,
119 sprintf(tmp, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, filename);
122 td_verror(td, errno, onerr);
126 fprintf(f, "%u", val);
132 static int cgroup_write_pid(struct thread_data *td, const char *root)
134 unsigned int val = td->pid;
136 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
140 * Move pid to root class
142 static int cgroup_del_pid(struct thread_data *td, char *mnt)
144 return cgroup_write_pid(td, mnt);
147 int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
152 *mnt = find_cgroup_mnt(td);
158 * Create container, if it doesn't exist
160 root = get_cgroup_root(td, *mnt);
161 if (mkdir(root, 0755) < 0) {
165 td_verror(td, __e, "cgroup mkdir");
166 log_err("fio: path %s\n", root);
170 add_cgroup(td, root, clist);
172 if (td->o.cgroup_weight) {
173 if (write_int_to_file(td, root, "blkio.weight",
175 "cgroup open weight"))
179 if (!cgroup_write_pid(td, root)) {
189 void cgroup_shutdown(struct thread_data *td, char **mnt)
193 if (!td->o.cgroup_weight && !td->o.cgroup)
196 cgroup_del_pid(td, *mnt);
199 static void fio_init cgroup_init(void)
201 lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
203 log_err("fio: failed to allocate cgroup lock\n");
206 static void fio_exit cgroup_exit(void)
208 fio_mutex_remove(lock);