2 * Code related to setting up a blkio cgroup
11 static struct flist_head *cgroup_list;
12 static struct fio_mutex *lock;
14 struct cgroup_member {
15 struct flist_head list;
19 static void add_cgroup(const char *name)
21 struct cgroup_member *cm;
23 cm = smalloc(sizeof(*cm));
24 INIT_FLIST_HEAD(&cm->list);
25 cm->root = smalloc_strdup(name);
30 cgroup_list = smalloc(sizeof(struct flist_head));
31 INIT_FLIST_HEAD(cgroup_list);
34 flist_add_tail(&cm->list, cgroup_list);
38 void cgroup_kill(void)
40 struct flist_head *n, *tmp;
41 struct cgroup_member *cm;
47 flist_for_each_safe(n, tmp, cgroup_list) {
48 cm = flist_entry(n, struct cgroup_member, list);
62 * Check if the given root appears valid
64 static int cgroup_check_fs(struct thread_data *td)
69 sprintf(tmp, "%s/tasks", td->o.cgroup_root);
70 return stat(tmp, &sb);
73 static char *get_cgroup_root(struct thread_data *td)
75 char *str = malloc(64);
78 sprintf(str, "%s/%s", td->o.cgroup_root, td->o.cgroup);
80 sprintf(str, "%s/%s", td->o.cgroup_root, td->o.name);
86 * Add pid to given class
88 static int cgroup_add_pid(struct thread_data *td)
93 root = get_cgroup_root(td);
94 sprintf(tmp, "%s/tasks", root);
97 td_verror(td, errno, "cgroup open tasks");
101 fprintf(f, "%d", td->pid);
108 * Move pid to root class
110 static int cgroup_del_pid(struct thread_data *td)
115 sprintf(tmp, "%s/tasks", td->o.cgroup_root);
118 td_verror(td, errno, "cgroup open tasks");
122 fprintf(f, "%d", td->pid);
128 int cgroup_setup(struct thread_data *td)
130 char *root, tmp[256];
133 if (cgroup_check_fs(td)) {
134 log_err("fio: blkio cgroup mount point %s not valid\n",
140 * Create container, if it doesn't exist
142 root = get_cgroup_root(td);
143 if (mkdir(root, 0755) < 0) {
147 td_verror(td, __e, "cgroup mkdir");
153 if (td->o.cgroup_weight) {
154 sprintf(tmp, "%s/blkio.weight", root);
157 td_verror(td, errno, "cgroup open weight");
161 fprintf(f, "%d", td->o.cgroup_weight);
167 if (cgroup_add_pid(td))
173 void cgroup_shutdown(struct thread_data *td)
175 if (cgroup_check_fs(td))
177 if (!td->o.cgroup_weight && td->o.cgroup)
184 static void fio_init cgroup_init(void)
186 lock = fio_mutex_init(1);
189 static void fio_exit cgroup_exit(void)
191 fio_mutex_remove(lock);