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