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 write_int_to_file(struct thread_data *td, const char *path,
74 const char *filename, unsigned int val,
80 sprintf(tmp, "%s/%s", path, filename);
83 td_verror(td, errno, onerr);
87 fprintf(f, "%u", val);
93 static int cgroup_write_pid(struct thread_data *td, const char *root)
95 unsigned int val = td->pid;
97 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
101 * Move pid to root class
103 static int cgroup_del_pid(struct thread_data *td)
105 return cgroup_write_pid(td, td->o.cgroup_root);
108 int cgroup_setup(struct thread_data *td, struct flist_head *clist)
112 if (cgroup_check_fs(td)) {
113 log_err("fio: blkio cgroup mount point %s not valid\n",
119 * Create container, if it doesn't exist
121 root = get_cgroup_root(td);
122 if (mkdir(root, 0755) < 0) {
126 td_verror(td, __e, "cgroup mkdir");
130 add_cgroup(root, clist);
132 if (td->o.cgroup_weight) {
133 if (write_int_to_file(td, root, "blkio.weight",
135 "cgroup open weight"))
139 if (!cgroup_write_pid(td, root)) {
149 void cgroup_shutdown(struct thread_data *td)
151 if (cgroup_check_fs(td))
153 if (!td->o.cgroup_weight && td->o.cgroup)
159 static void fio_init cgroup_init(void)
161 lock = fio_mutex_init(1);
164 static void fio_exit cgroup_exit(void)
166 fio_mutex_remove(lock);