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 | ||
55 | cm = smalloc(sizeof(*cm)); | |
56 | INIT_FLIST_HEAD(&cm->list); | |
57 | cm->root = smalloc_strdup(name); | |
7de87099 VG |
58 | if (td->o.cgroup_nodelete) |
59 | cm->cgroup_nodelete = 1; | |
39f22027 | 60 | fio_mutex_down(lock); |
dae5341c | 61 | flist_add_tail(&cm->list, clist); |
39f22027 JA |
62 | fio_mutex_up(lock); |
63 | } | |
64 | ||
dae5341c | 65 | void cgroup_kill(struct flist_head *clist) |
39f22027 JA |
66 | { |
67 | struct flist_head *n, *tmp; | |
68 | struct cgroup_member *cm; | |
69 | ||
70 | fio_mutex_down(lock); | |
39f22027 | 71 | |
dae5341c | 72 | flist_for_each_safe(n, tmp, clist) { |
39f22027 | 73 | cm = flist_entry(n, struct cgroup_member, list); |
7de87099 VG |
74 | if (!cm->cgroup_nodelete) |
75 | rmdir(cm->root); | |
39f22027 JA |
76 | flist_del(&cm->list); |
77 | sfree(cm->root); | |
78 | sfree(cm); | |
79 | } | |
80 | ||
39f22027 JA |
81 | fio_mutex_up(lock); |
82 | } | |
a696fa2a | 83 | |
6adb38a1 | 84 | static char *get_cgroup_root(struct thread_data *td, char *mnt) |
a696fa2a JA |
85 | { |
86 | char *str = malloc(64); | |
87 | ||
88 | if (td->o.cgroup) | |
b9fd788f | 89 | sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.cgroup); |
a696fa2a | 90 | else |
b9fd788f | 91 | sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.name); |
a696fa2a JA |
92 | |
93 | return str; | |
94 | } | |
95 | ||
8a4d0ff0 JA |
96 | static int write_int_to_file(struct thread_data *td, const char *path, |
97 | const char *filename, unsigned int val, | |
98 | const char *onerr) | |
a696fa2a | 99 | { |
3858bf9e | 100 | char tmp[256]; |
a696fa2a | 101 | FILE *f; |
b9fd788f BC |
102 | |
103 | sprintf(tmp, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, filename); | |
a696fa2a JA |
104 | f = fopen(tmp, "w"); |
105 | if (!f) { | |
8a4d0ff0 | 106 | td_verror(td, errno, onerr); |
a696fa2a JA |
107 | return 1; |
108 | } | |
109 | ||
8a4d0ff0 | 110 | fprintf(f, "%u", val); |
a696fa2a | 111 | fclose(f); |
a696fa2a | 112 | return 0; |
8a4d0ff0 | 113 | |
a696fa2a JA |
114 | } |
115 | ||
8a4d0ff0 | 116 | static int cgroup_write_pid(struct thread_data *td, const char *root) |
a696fa2a | 117 | { |
8a4d0ff0 | 118 | unsigned int val = td->pid; |
a696fa2a | 119 | |
8a4d0ff0 | 120 | return write_int_to_file(td, root, "tasks", val, "cgroup write pid"); |
a696fa2a JA |
121 | } |
122 | ||
3858bf9e JA |
123 | /* |
124 | * Move pid to root class | |
125 | */ | |
6adb38a1 | 126 | static int cgroup_del_pid(struct thread_data *td, char *mnt) |
3858bf9e | 127 | { |
6adb38a1 | 128 | return cgroup_write_pid(td, mnt); |
3858bf9e | 129 | } |
a696fa2a | 130 | |
6adb38a1 | 131 | int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt) |
a696fa2a | 132 | { |
8a4d0ff0 | 133 | char *root; |
a696fa2a | 134 | |
6adb38a1 JA |
135 | if (!*mnt) { |
136 | *mnt = find_cgroup_mnt(td); | |
137 | if (!*mnt) | |
138 | return 1; | |
ddf16fe4 JA |
139 | } |
140 | ||
a696fa2a JA |
141 | /* |
142 | * Create container, if it doesn't exist | |
143 | */ | |
6adb38a1 | 144 | root = get_cgroup_root(td, *mnt); |
a696fa2a JA |
145 | if (mkdir(root, 0755) < 0) { |
146 | int __e = errno; | |
147 | ||
148 | if (__e != EEXIST) { | |
149 | td_verror(td, __e, "cgroup mkdir"); | |
6adb38a1 | 150 | log_err("fio: path %s\n", root); |
3858bf9e | 151 | goto err; |
a696fa2a JA |
152 | } |
153 | } else | |
7de87099 | 154 | add_cgroup(td, root, clist); |
a696fa2a | 155 | |
39f22027 | 156 | if (td->o.cgroup_weight) { |
8a4d0ff0 JA |
157 | if (write_int_to_file(td, root, "blkio.weight", |
158 | td->o.cgroup_weight, | |
159 | "cgroup open weight")) | |
3858bf9e | 160 | goto err; |
a696fa2a JA |
161 | } |
162 | ||
8a4d0ff0 JA |
163 | if (!cgroup_write_pid(td, root)) { |
164 | free(root); | |
165 | return 0; | |
166 | } | |
a696fa2a | 167 | |
3858bf9e JA |
168 | err: |
169 | free(root); | |
170 | return 1; | |
a696fa2a JA |
171 | } |
172 | ||
6adb38a1 | 173 | void cgroup_shutdown(struct thread_data *td, char **mnt) |
a696fa2a | 174 | { |
6adb38a1 | 175 | if (*mnt == NULL) |
ddf16fe4 | 176 | return; |
ed81ee19 | 177 | if (!td->o.cgroup_weight && !td->o.cgroup) |
a696fa2a JA |
178 | return; |
179 | ||
6adb38a1 | 180 | cgroup_del_pid(td, *mnt); |
39f22027 | 181 | } |
a696fa2a | 182 | |
39f22027 JA |
183 | static void fio_init cgroup_init(void) |
184 | { | |
185 | lock = fio_mutex_init(1); | |
186 | } | |
187 | ||
188 | static void fio_exit cgroup_exit(void) | |
189 | { | |
190 | fio_mutex_remove(lock); | |
a696fa2a | 191 | } |