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