2 * Code related to setting up a blkio cgroup
11 static struct fio_mutex *lock;
13 struct cgroup_member {
14 struct flist_head list;
18 static void add_cgroup(const char *name, struct flist_head *clist)
20 struct cgroup_member *cm;
22 cm = smalloc(sizeof(*cm));
23 INIT_FLIST_HEAD(&cm->list);
24 cm->root = smalloc_strdup(name);
27 flist_add_tail(&cm->list, clist);
31 void cgroup_kill(struct flist_head *clist)
33 struct flist_head *n, *tmp;
34 struct cgroup_member *cm;
38 flist_for_each_safe(n, tmp, clist) {
39 cm = flist_entry(n, struct cgroup_member, list);
50 * Check if the given root appears valid
52 static int cgroup_check_fs(struct thread_data *td)
57 sprintf(tmp, "%s/tasks", td->o.cgroup_root);
58 return stat(tmp, &sb);
61 static char *get_cgroup_root(struct thread_data *td)
63 char *str = malloc(64);
66 sprintf(str, "%s/%s", td->o.cgroup_root, td->o.cgroup);
68 sprintf(str, "%s/%s", td->o.cgroup_root, td->o.name);
73 static int cgroup_write_pid(struct thread_data *td, const char *root)
78 sprintf(tmp, "%s/tasks", root);
81 td_verror(td, errno, "cgroup open tasks");
85 fprintf(f, "%d", td->pid);
91 * Add pid to given class
93 static int cgroup_add_pid(struct thread_data *td)
98 root = get_cgroup_root(td);
99 ret = cgroup_write_pid(td, root);
105 * Move pid to root class
107 static int cgroup_del_pid(struct thread_data *td)
109 return cgroup_write_pid(td, td->o.cgroup_root);
112 int cgroup_setup(struct thread_data *td, struct flist_head *clist)
114 char *root, tmp[256];
117 if (cgroup_check_fs(td)) {
118 log_err("fio: blkio cgroup mount point %s not valid\n",
124 * Create container, if it doesn't exist
126 root = get_cgroup_root(td);
127 if (mkdir(root, 0755) < 0) {
131 td_verror(td, __e, "cgroup mkdir");
135 add_cgroup(root, clist);
137 if (td->o.cgroup_weight) {
138 sprintf(tmp, "%s/blkio.weight", root);
141 td_verror(td, errno, "cgroup open weight");
145 fprintf(f, "%d", td->o.cgroup_weight);
151 if (cgroup_add_pid(td))
160 void cgroup_shutdown(struct thread_data *td)
162 if (cgroup_check_fs(td))
164 if (!td->o.cgroup_weight && td->o.cgroup)
170 static void fio_init cgroup_init(void)
172 lock = fio_mutex_init(1);
175 static void fio_exit cgroup_exit(void)
177 fio_mutex_remove(lock);