Commit | Line | Data |
---|---|---|
a696fa2a JA |
1 | /* |
2 | * Code related to setting up a blkio cgroup | |
3 | */ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6adb38a1 | 6 | #include <mntent.h> |
4f49b8a7 JA |
7 | #include <sys/stat.h> |
8 | #include <sys/types.h> | |
a696fa2a | 9 | #include "fio.h" |
39f22027 | 10 | #include "flist.h" |
a696fa2a | 11 | #include "cgroup.h" |
39f22027 JA |
12 | #include "smalloc.h" |
13 | ||
39f22027 JA |
14 | static struct fio_mutex *lock; |
15 | ||
16 | struct cgroup_member { | |
17 | struct flist_head list; | |
18 | char *root; | |
7de87099 | 19 | unsigned int cgroup_nodelete; |
39f22027 JA |
20 | }; |
21 | ||
6adb38a1 JA |
22 | static char *find_cgroup_mnt(struct thread_data *td) |
23 | { | |
24 | char *mntpoint = NULL; | |
599e72b7 ZY |
25 | struct mntent *mnt, dummy; |
26 | char buf[256] = {0}; | |
6adb38a1 JA |
27 | FILE *f; |
28 | ||
29 | f = setmntent("/proc/mounts", "r"); | |
30 | if (!f) { | |
31 | td_verror(td, errno, "setmntent /proc/mounts"); | |
32 | return NULL; | |
33 | } | |
34 | ||
599e72b7 | 35 | while ((mnt = getmntent_r(f, &dummy, buf, sizeof(buf))) != NULL) { |
6adb38a1 JA |
36 | if (!strcmp(mnt->mnt_type, "cgroup") && |
37 | strstr(mnt->mnt_opts, "blkio")) | |
38 | break; | |
39 | } | |
40 | ||
41 | if (mnt) | |
42 | mntpoint = smalloc_strdup(mnt->mnt_dir); | |
43 | else | |
44 | log_err("fio: cgroup blkio does not appear to be mounted\n"); | |
45 | ||
46 | endmntent(f); | |
47 | return mntpoint; | |
48 | } | |
49 | ||
7de87099 VG |
50 | static void add_cgroup(struct thread_data *td, const char *name, |
51 | struct flist_head *clist) | |
39f22027 JA |
52 | { |
53 | struct cgroup_member *cm; | |
54 | ||
fba5c5ff JA |
55 | if (!lock) |
56 | return; | |
57 | ||
39f22027 | 58 | cm = smalloc(sizeof(*cm)); |
fba5c5ff JA |
59 | if (!cm) { |
60 | err: | |
61 | log_err("fio: failed to allocate cgroup member\n"); | |
62 | return; | |
63 | } | |
64 | ||
39f22027 JA |
65 | INIT_FLIST_HEAD(&cm->list); |
66 | cm->root = smalloc_strdup(name); | |
fba5c5ff JA |
67 | if (!cm->root) { |
68 | sfree(cm); | |
69 | goto err; | |
70 | } | |
7de87099 VG |
71 | if (td->o.cgroup_nodelete) |
72 | cm->cgroup_nodelete = 1; | |
39f22027 | 73 | fio_mutex_down(lock); |
dae5341c | 74 | flist_add_tail(&cm->list, clist); |
39f22027 JA |
75 | fio_mutex_up(lock); |
76 | } | |
77 | ||
dae5341c | 78 | void cgroup_kill(struct flist_head *clist) |
39f22027 JA |
79 | { |
80 | struct flist_head *n, *tmp; | |
81 | struct cgroup_member *cm; | |
82 | ||
fba5c5ff JA |
83 | if (!lock) |
84 | return; | |
85 | ||
39f22027 | 86 | fio_mutex_down(lock); |
39f22027 | 87 | |
dae5341c | 88 | flist_for_each_safe(n, tmp, clist) { |
39f22027 | 89 | cm = flist_entry(n, struct cgroup_member, list); |
7de87099 VG |
90 | if (!cm->cgroup_nodelete) |
91 | rmdir(cm->root); | |
39f22027 JA |
92 | flist_del(&cm->list); |
93 | sfree(cm->root); | |
94 | sfree(cm); | |
95 | } | |
96 | ||
39f22027 JA |
97 | fio_mutex_up(lock); |
98 | } | |
a696fa2a | 99 | |
6adb38a1 | 100 | static char *get_cgroup_root(struct thread_data *td, char *mnt) |
a696fa2a JA |
101 | { |
102 | char *str = malloc(64); | |
103 | ||
104 | if (td->o.cgroup) | |
22497370 | 105 | sprintf(str, "%s/%s", mnt, td->o.cgroup); |
a696fa2a | 106 | else |
22497370 | 107 | sprintf(str, "%s/%s", mnt, td->o.name); |
a696fa2a JA |
108 | |
109 | return str; | |
110 | } | |
111 | ||
8a4d0ff0 JA |
112 | static int write_int_to_file(struct thread_data *td, const char *path, |
113 | const char *filename, unsigned int val, | |
114 | const char *onerr) | |
a696fa2a | 115 | { |
3858bf9e | 116 | char tmp[256]; |
a696fa2a | 117 | FILE *f; |
b9fd788f | 118 | |
22497370 | 119 | sprintf(tmp, "%s/%s", path, filename); |
a696fa2a JA |
120 | f = fopen(tmp, "w"); |
121 | if (!f) { | |
8a4d0ff0 | 122 | td_verror(td, errno, onerr); |
a696fa2a JA |
123 | return 1; |
124 | } | |
125 | ||
8a4d0ff0 | 126 | fprintf(f, "%u", val); |
a696fa2a | 127 | fclose(f); |
a696fa2a | 128 | return 0; |
8a4d0ff0 | 129 | |
a696fa2a JA |
130 | } |
131 | ||
8a4d0ff0 | 132 | static int cgroup_write_pid(struct thread_data *td, const char *root) |
a696fa2a | 133 | { |
8a4d0ff0 | 134 | unsigned int val = td->pid; |
a696fa2a | 135 | |
8a4d0ff0 | 136 | return write_int_to_file(td, root, "tasks", val, "cgroup write pid"); |
a696fa2a JA |
137 | } |
138 | ||
3858bf9e JA |
139 | /* |
140 | * Move pid to root class | |
141 | */ | |
6adb38a1 | 142 | static int cgroup_del_pid(struct thread_data *td, char *mnt) |
3858bf9e | 143 | { |
6adb38a1 | 144 | return cgroup_write_pid(td, mnt); |
3858bf9e | 145 | } |
a696fa2a | 146 | |
6adb38a1 | 147 | int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt) |
a696fa2a | 148 | { |
8a4d0ff0 | 149 | char *root; |
a696fa2a | 150 | |
6adb38a1 JA |
151 | if (!*mnt) { |
152 | *mnt = find_cgroup_mnt(td); | |
153 | if (!*mnt) | |
154 | return 1; | |
ddf16fe4 JA |
155 | } |
156 | ||
a696fa2a JA |
157 | /* |
158 | * Create container, if it doesn't exist | |
159 | */ | |
6adb38a1 | 160 | root = get_cgroup_root(td, *mnt); |
a696fa2a JA |
161 | if (mkdir(root, 0755) < 0) { |
162 | int __e = errno; | |
163 | ||
164 | if (__e != EEXIST) { | |
165 | td_verror(td, __e, "cgroup mkdir"); | |
6adb38a1 | 166 | log_err("fio: path %s\n", root); |
3858bf9e | 167 | goto err; |
a696fa2a JA |
168 | } |
169 | } else | |
7de87099 | 170 | add_cgroup(td, root, clist); |
a696fa2a | 171 | |
39f22027 | 172 | if (td->o.cgroup_weight) { |
8a4d0ff0 JA |
173 | if (write_int_to_file(td, root, "blkio.weight", |
174 | td->o.cgroup_weight, | |
175 | "cgroup open weight")) | |
3858bf9e | 176 | goto err; |
a696fa2a JA |
177 | } |
178 | ||
8a4d0ff0 JA |
179 | if (!cgroup_write_pid(td, root)) { |
180 | free(root); | |
181 | return 0; | |
182 | } | |
a696fa2a | 183 | |
3858bf9e JA |
184 | err: |
185 | free(root); | |
186 | return 1; | |
a696fa2a JA |
187 | } |
188 | ||
6adb38a1 | 189 | void cgroup_shutdown(struct thread_data *td, char **mnt) |
a696fa2a | 190 | { |
6adb38a1 | 191 | if (*mnt == NULL) |
ddf16fe4 | 192 | return; |
ed81ee19 | 193 | if (!td->o.cgroup_weight && !td->o.cgroup) |
a696fa2a JA |
194 | return; |
195 | ||
6adb38a1 | 196 | cgroup_del_pid(td, *mnt); |
39f22027 | 197 | } |
a696fa2a | 198 | |
39f22027 JA |
199 | static void fio_init cgroup_init(void) |
200 | { | |
521da527 | 201 | lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
fba5c5ff JA |
202 | if (!lock) |
203 | log_err("fio: failed to allocate cgroup lock\n"); | |
39f22027 JA |
204 | } |
205 | ||
206 | static void fio_exit cgroup_exit(void) | |
207 | { | |
208 | fio_mutex_remove(lock); | |
a696fa2a | 209 | } |