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 struct cgroup_mnt *find_cgroup_mnt(struct thread_data *td)
23 struct cgroup_mnt *cgroup_mnt = NULL;
24 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"))
39 if (!strcmp(mnt->mnt_type, "cgroup2")) {
46 cgroup_mnt = smalloc(sizeof(*cgroup_mnt));
48 cgroup_mnt->path = smalloc_strdup(mnt->mnt_dir);
49 if (!cgroup_mnt->path) {
51 log_err("fio: could not allocate memory\n");
53 cgroup_mnt->cgroup2 = cgroup2;
57 log_err("fio: cgroup blkio does not appear to be mounted\n");
64 static void add_cgroup(struct thread_data *td, const char *name,
65 struct flist_head *clist)
67 struct cgroup_member *cm;
72 cm = smalloc(sizeof(*cm));
75 log_err("fio: failed to allocate cgroup member\n");
79 INIT_FLIST_HEAD(&cm->list);
80 cm->root = smalloc_strdup(name);
85 if (td->o.cgroup_nodelete)
86 cm->cgroup_nodelete = 1;
88 flist_add_tail(&cm->list, clist);
92 void cgroup_kill(struct flist_head *clist)
94 struct flist_head *n, *tmp;
95 struct cgroup_member *cm;
102 flist_for_each_safe(n, tmp, clist) {
103 cm = flist_entry(n, struct cgroup_member, list);
104 if (!cm->cgroup_nodelete)
106 flist_del(&cm->list);
114 static char *get_cgroup_root(struct thread_data *td, struct cgroup_mnt *mnt)
116 char *str = malloc(64);
119 sprintf(str, "%s/%s", mnt->path, td->o.cgroup);
121 sprintf(str, "%s/%s", mnt->path, td->o.name);
126 static int write_int_to_file(struct thread_data *td, const char *path,
127 const char *filename, unsigned int val,
133 sprintf(tmp, "%s/%s", path, filename);
136 td_verror(td, errno, onerr);
140 fprintf(f, "%u", val);
146 static int cgroup_write_pid(struct thread_data *td, char *path, bool cgroup2)
148 unsigned int val = td->pid;
151 return write_int_to_file(td, path, "cgroup.procs",
152 val, "cgroup write pid");
153 return write_int_to_file(td, path, "tasks", val, "cgroup write pid");
157 * Move pid to root class
159 static int cgroup_del_pid(struct thread_data *td, struct cgroup_mnt *mnt)
161 return cgroup_write_pid(td, mnt->path, mnt->cgroup2);
164 int cgroup_setup(struct thread_data *td, struct flist_head *clist, struct cgroup_mnt **mnt)
172 *mnt = find_cgroup_mnt(td);
178 * Create container, if it doesn't exist
180 root = get_cgroup_root(td, *mnt);
181 if (mkdir(root, 0755) < 0) {
185 td_verror(td, __e, "cgroup mkdir");
186 log_err("fio: path %s\n", root);
190 add_cgroup(td, root, clist);
192 if (td->o.cgroup_weight) {
193 if ((*mnt)->cgroup2) {
194 log_err("fio: cgroup weit doesn't work with cgroup2\n");
197 if (write_int_to_file(td, root, "blkio.weight",
199 "cgroup open weight"))
203 if (!cgroup_write_pid(td, root, (*mnt)->cgroup2)) {
213 void cgroup_shutdown(struct thread_data *td, struct cgroup_mnt *mnt)
217 if (!td->o.cgroup_weight && !td->o.cgroup)
220 cgroup_del_pid(td, mnt);
227 static void fio_init cgroup_init(void)
229 lock = fio_sem_init(FIO_SEM_UNLOCKED);
231 log_err("fio: failed to allocate cgroup lock\n");
234 static void fio_exit cgroup_exit(void)
236 fio_sem_remove(lock);