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 | 7 | #include <sys/stat.h> |
a696fa2a | 8 | #include "fio.h" |
39f22027 | 9 | #include "flist.h" |
a696fa2a | 10 | #include "cgroup.h" |
39f22027 JA |
11 | #include "smalloc.h" |
12 | ||
971caeb1 | 13 | static struct fio_sem *lock; |
39f22027 JA |
14 | |
15 | struct cgroup_member { | |
16 | struct flist_head list; | |
17 | char *root; | |
7de87099 | 18 | unsigned int cgroup_nodelete; |
39f22027 JA |
19 | }; |
20 | ||
c3dc516a | 21 | static struct cgroup_mnt *find_cgroup_mnt(struct thread_data *td) |
6adb38a1 | 22 | { |
c3dc516a | 23 | struct cgroup_mnt *cgroup_mnt = NULL; |
599e72b7 ZY |
24 | struct mntent *mnt, dummy; |
25 | char buf[256] = {0}; | |
6adb38a1 | 26 | FILE *f; |
c3dc516a | 27 | bool cgroup2 = false; |
6adb38a1 JA |
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; | |
c3dc516a JB |
39 | if (!strcmp(mnt->mnt_type, "cgroup2")) { |
40 | cgroup2 = true; | |
41 | break; | |
42 | } | |
6adb38a1 JA |
43 | } |
44 | ||
c3dc516a JB |
45 | if (mnt) { |
46 | cgroup_mnt = smalloc(sizeof(*cgroup_mnt)); | |
47 | if (cgroup_mnt) { | |
48 | cgroup_mnt->path = smalloc_strdup(mnt->mnt_dir); | |
49 | if (!cgroup_mnt->path) { | |
50 | sfree(cgroup_mnt); | |
51 | log_err("fio: could not allocate memory\n"); | |
52 | } else { | |
53 | cgroup_mnt->cgroup2 = cgroup2; | |
54 | } | |
55 | } | |
56 | } else { | |
6adb38a1 | 57 | log_err("fio: cgroup blkio does not appear to be mounted\n"); |
c3dc516a | 58 | } |
6adb38a1 JA |
59 | |
60 | endmntent(f); | |
c3dc516a | 61 | return cgroup_mnt; |
6adb38a1 JA |
62 | } |
63 | ||
7de87099 VG |
64 | static void add_cgroup(struct thread_data *td, const char *name, |
65 | struct flist_head *clist) | |
39f22027 JA |
66 | { |
67 | struct cgroup_member *cm; | |
68 | ||
fba5c5ff JA |
69 | if (!lock) |
70 | return; | |
71 | ||
39f22027 | 72 | cm = smalloc(sizeof(*cm)); |
fba5c5ff JA |
73 | if (!cm) { |
74 | err: | |
75 | log_err("fio: failed to allocate cgroup member\n"); | |
76 | return; | |
77 | } | |
78 | ||
39f22027 JA |
79 | INIT_FLIST_HEAD(&cm->list); |
80 | cm->root = smalloc_strdup(name); | |
fba5c5ff JA |
81 | if (!cm->root) { |
82 | sfree(cm); | |
83 | goto err; | |
84 | } | |
7de87099 VG |
85 | if (td->o.cgroup_nodelete) |
86 | cm->cgroup_nodelete = 1; | |
971caeb1 | 87 | fio_sem_down(lock); |
dae5341c | 88 | flist_add_tail(&cm->list, clist); |
971caeb1 | 89 | fio_sem_up(lock); |
39f22027 JA |
90 | } |
91 | ||
dae5341c | 92 | void cgroup_kill(struct flist_head *clist) |
39f22027 JA |
93 | { |
94 | struct flist_head *n, *tmp; | |
95 | struct cgroup_member *cm; | |
96 | ||
fba5c5ff JA |
97 | if (!lock) |
98 | return; | |
99 | ||
971caeb1 | 100 | fio_sem_down(lock); |
39f22027 | 101 | |
dae5341c | 102 | flist_for_each_safe(n, tmp, clist) { |
39f22027 | 103 | cm = flist_entry(n, struct cgroup_member, list); |
7de87099 VG |
104 | if (!cm->cgroup_nodelete) |
105 | rmdir(cm->root); | |
39f22027 JA |
106 | flist_del(&cm->list); |
107 | sfree(cm->root); | |
108 | sfree(cm); | |
109 | } | |
110 | ||
971caeb1 | 111 | fio_sem_up(lock); |
39f22027 | 112 | } |
a696fa2a | 113 | |
c3dc516a | 114 | static char *get_cgroup_root(struct thread_data *td, struct cgroup_mnt *mnt) |
a696fa2a JA |
115 | { |
116 | char *str = malloc(64); | |
117 | ||
118 | if (td->o.cgroup) | |
c3dc516a | 119 | sprintf(str, "%s/%s", mnt->path, td->o.cgroup); |
a696fa2a | 120 | else |
c3dc516a | 121 | sprintf(str, "%s/%s", mnt->path, td->o.name); |
a696fa2a JA |
122 | |
123 | return str; | |
124 | } | |
125 | ||
8a4d0ff0 JA |
126 | static int write_int_to_file(struct thread_data *td, const char *path, |
127 | const char *filename, unsigned int val, | |
128 | const char *onerr) | |
a696fa2a | 129 | { |
3858bf9e | 130 | char tmp[256]; |
a696fa2a | 131 | FILE *f; |
b9fd788f | 132 | |
22497370 | 133 | sprintf(tmp, "%s/%s", path, filename); |
a696fa2a JA |
134 | f = fopen(tmp, "w"); |
135 | if (!f) { | |
8a4d0ff0 | 136 | td_verror(td, errno, onerr); |
a696fa2a JA |
137 | return 1; |
138 | } | |
139 | ||
8a4d0ff0 | 140 | fprintf(f, "%u", val); |
a696fa2a | 141 | fclose(f); |
a696fa2a | 142 | return 0; |
8a4d0ff0 | 143 | |
a696fa2a JA |
144 | } |
145 | ||
c3dc516a | 146 | static int cgroup_write_pid(struct thread_data *td, char *path, bool cgroup2) |
a696fa2a | 147 | { |
8a4d0ff0 | 148 | unsigned int val = td->pid; |
a696fa2a | 149 | |
c3dc516a JB |
150 | if (cgroup2) |
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"); | |
a696fa2a JA |
154 | } |
155 | ||
3858bf9e JA |
156 | /* |
157 | * Move pid to root class | |
158 | */ | |
c3dc516a | 159 | static int cgroup_del_pid(struct thread_data *td, struct cgroup_mnt *mnt) |
3858bf9e | 160 | { |
c3dc516a | 161 | return cgroup_write_pid(td, mnt->path, mnt->cgroup2); |
3858bf9e | 162 | } |
a696fa2a | 163 | |
c3dc516a | 164 | int cgroup_setup(struct thread_data *td, struct flist_head *clist, struct cgroup_mnt **mnt) |
a696fa2a | 165 | { |
8a4d0ff0 | 166 | char *root; |
a696fa2a | 167 | |
b2e6494c JA |
168 | if (!clist) |
169 | return 1; | |
170 | ||
6adb38a1 JA |
171 | if (!*mnt) { |
172 | *mnt = find_cgroup_mnt(td); | |
173 | if (!*mnt) | |
174 | return 1; | |
ddf16fe4 JA |
175 | } |
176 | ||
a696fa2a JA |
177 | /* |
178 | * Create container, if it doesn't exist | |
179 | */ | |
6adb38a1 | 180 | root = get_cgroup_root(td, *mnt); |
a696fa2a JA |
181 | if (mkdir(root, 0755) < 0) { |
182 | int __e = errno; | |
183 | ||
184 | if (__e != EEXIST) { | |
185 | td_verror(td, __e, "cgroup mkdir"); | |
6adb38a1 | 186 | log_err("fio: path %s\n", root); |
3858bf9e | 187 | goto err; |
a696fa2a JA |
188 | } |
189 | } else | |
7de87099 | 190 | add_cgroup(td, root, clist); |
a696fa2a | 191 | |
39f22027 | 192 | if (td->o.cgroup_weight) { |
c3dc516a JB |
193 | if ((*mnt)->cgroup2) { |
194 | log_err("fio: cgroup weit doesn't work with cgroup2\n"); | |
195 | goto err; | |
196 | } | |
8a4d0ff0 JA |
197 | if (write_int_to_file(td, root, "blkio.weight", |
198 | td->o.cgroup_weight, | |
199 | "cgroup open weight")) | |
3858bf9e | 200 | goto err; |
a696fa2a JA |
201 | } |
202 | ||
c3dc516a | 203 | if (!cgroup_write_pid(td, root, (*mnt)->cgroup2)) { |
8a4d0ff0 JA |
204 | free(root); |
205 | return 0; | |
206 | } | |
a696fa2a | 207 | |
3858bf9e JA |
208 | err: |
209 | free(root); | |
210 | return 1; | |
a696fa2a JA |
211 | } |
212 | ||
c3dc516a | 213 | void cgroup_shutdown(struct thread_data *td, struct cgroup_mnt *mnt) |
a696fa2a | 214 | { |
c3dc516a | 215 | if (mnt == NULL) |
ddf16fe4 | 216 | return; |
ed81ee19 | 217 | if (!td->o.cgroup_weight && !td->o.cgroup) |
c3dc516a | 218 | goto out; |
a696fa2a | 219 | |
c3dc516a JB |
220 | cgroup_del_pid(td, mnt); |
221 | out: | |
222 | if (mnt->path) | |
223 | sfree(mnt->path); | |
224 | sfree(mnt); | |
39f22027 | 225 | } |
a696fa2a | 226 | |
39f22027 JA |
227 | static void fio_init cgroup_init(void) |
228 | { | |
971caeb1 | 229 | lock = fio_sem_init(FIO_SEM_UNLOCKED); |
fba5c5ff JA |
230 | if (!lock) |
231 | log_err("fio: failed to allocate cgroup lock\n"); | |
39f22027 JA |
232 | } |
233 | ||
234 | static void fio_exit cgroup_exit(void) | |
235 | { | |
971caeb1 | 236 | fio_sem_remove(lock); |
a696fa2a | 237 | } |