2 * Code related to setting up a blkio cgroup
12 static struct fio_mutex *lock;
14 struct cgroup_member {
15 struct flist_head list;
17 unsigned int cgroup_nodelete;
20 static char *find_cgroup_mnt(struct thread_data *td)
22 char *mntpoint = NULL;
26 f = setmntent("/proc/mounts", "r");
28 td_verror(td, errno, "setmntent /proc/mounts");
32 while ((mnt = getmntent(f)) != NULL) {
33 if (!strcmp(mnt->mnt_type, "cgroup") &&
34 strstr(mnt->mnt_opts, "blkio"))
39 mntpoint = smalloc_strdup(mnt->mnt_dir);
41 log_err("fio: cgroup blkio does not appear to be mounted\n");
47 static void add_cgroup(struct thread_data *td, const char *name,
48 struct flist_head *clist)
50 struct cgroup_member *cm;
52 cm = smalloc(sizeof(*cm));
53 INIT_FLIST_HEAD(&cm->list);
54 cm->root = smalloc_strdup(name);
55 if (td->o.cgroup_nodelete)
56 cm->cgroup_nodelete = 1;
58 flist_add_tail(&cm->list, clist);
62 void cgroup_kill(struct flist_head *clist)
64 struct flist_head *n, *tmp;
65 struct cgroup_member *cm;
69 flist_for_each_safe(n, tmp, clist) {
70 cm = flist_entry(n, struct cgroup_member, list);
71 if (!cm->cgroup_nodelete)
81 static char *get_cgroup_root(struct thread_data *td, char *mnt)
83 char *str = malloc(64);
86 sprintf(str, "%s/%s", mnt, td->o.cgroup);
88 sprintf(str, "%s/%s", mnt, td->o.name);
93 static int write_int_to_file(struct thread_data *td, const char *path,
94 const char *filename, unsigned int val,
100 sprintf(tmp, "%s/%s", path, filename);
103 td_verror(td, errno, onerr);
107 fprintf(f, "%u", val);
113 static int cgroup_write_pid(struct thread_data *td, const char *root)
115 unsigned int val = td->pid;
117 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
121 * Move pid to root class
123 static int cgroup_del_pid(struct thread_data *td, char *mnt)
125 return cgroup_write_pid(td, mnt);
128 int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
133 *mnt = find_cgroup_mnt(td);
139 * Create container, if it doesn't exist
141 root = get_cgroup_root(td, *mnt);
142 if (mkdir(root, 0755) < 0) {
146 td_verror(td, __e, "cgroup mkdir");
147 log_err("fio: path %s\n", root);
151 add_cgroup(td, root, clist);
153 if (td->o.cgroup_weight) {
154 if (write_int_to_file(td, root, "blkio.weight",
156 "cgroup open weight"))
160 if (!cgroup_write_pid(td, root)) {
170 void cgroup_shutdown(struct thread_data *td, char **mnt)
174 if (!td->o.cgroup_weight && !td->o.cgroup)
177 cgroup_del_pid(td, *mnt);
180 static void fio_init cgroup_init(void)
182 lock = fio_mutex_init(1);
185 static void fio_exit cgroup_exit(void)
187 fio_mutex_remove(lock);