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;
28 f = setmntent("/proc/mounts", "r");
30 td_verror(td, errno, "setmntent /proc/mounts");
34 while ((mnt = getmntent(f)) != 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;
54 cm = smalloc(sizeof(*cm));
55 INIT_FLIST_HEAD(&cm->list);
56 cm->root = smalloc_strdup(name);
57 if (td->o.cgroup_nodelete)
58 cm->cgroup_nodelete = 1;
60 flist_add_tail(&cm->list, clist);
64 void cgroup_kill(struct flist_head *clist)
66 struct flist_head *n, *tmp;
67 struct cgroup_member *cm;
71 flist_for_each_safe(n, tmp, clist) {
72 cm = flist_entry(n, struct cgroup_member, list);
73 if (!cm->cgroup_nodelete)
83 static char *get_cgroup_root(struct thread_data *td, char *mnt)
85 char *str = malloc(64);
88 sprintf(str, "%s/%s", mnt, td->o.cgroup);
90 sprintf(str, "%s/%s", mnt, td->o.name);
95 static int write_int_to_file(struct thread_data *td, const char *path,
96 const char *filename, unsigned int val,
102 sprintf(tmp, "%s/%s", path, filename);
105 td_verror(td, errno, onerr);
109 fprintf(f, "%u", val);
115 static int cgroup_write_pid(struct thread_data *td, const char *root)
117 unsigned int val = td->pid;
119 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
123 * Move pid to root class
125 static int cgroup_del_pid(struct thread_data *td, char *mnt)
127 return cgroup_write_pid(td, mnt);
130 int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
135 *mnt = find_cgroup_mnt(td);
141 * Create container, if it doesn't exist
143 root = get_cgroup_root(td, *mnt);
144 if (mkdir(root, 0755) < 0) {
148 td_verror(td, __e, "cgroup mkdir");
149 log_err("fio: path %s\n", root);
153 add_cgroup(td, root, clist);
155 if (td->o.cgroup_weight) {
156 if (write_int_to_file(td, root, "blkio.weight",
158 "cgroup open weight"))
162 if (!cgroup_write_pid(td, root)) {
172 void cgroup_shutdown(struct thread_data *td, char **mnt)
176 if (!td->o.cgroup_weight && !td->o.cgroup)
179 cgroup_del_pid(td, *mnt);
182 static void fio_init cgroup_init(void)
184 lock = fio_mutex_init(1);
187 static void fio_exit cgroup_exit(void)
189 fio_mutex_remove(lock);