Refactor #includes and headers
[fio.git] / cgroup.c
CommitLineData
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 13static struct fio_sem *lock;
39f22027
JA
14
15struct cgroup_member {
16 struct flist_head list;
17 char *root;
7de87099 18 unsigned int cgroup_nodelete;
39f22027
JA
19};
20
6adb38a1
JA
21static 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
49static 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) {
59err:
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 77void 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 99static 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
111static 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 131static 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 141static 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 146int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
a696fa2a 147{
8a4d0ff0 148 char *root;
a696fa2a 149
6adb38a1
JA
150 if (!*mnt) {
151 *mnt = find_cgroup_mnt(td);
152 if (!*mnt)
153 return 1;
ddf16fe4
JA
154 }
155
a696fa2a
JA
156 /*
157 * Create container, if it doesn't exist
158 */
6adb38a1 159 root = get_cgroup_root(td, *mnt);
a696fa2a
JA
160 if (mkdir(root, 0755) < 0) {
161 int __e = errno;
162
163 if (__e != EEXIST) {
164 td_verror(td, __e, "cgroup mkdir");
6adb38a1 165 log_err("fio: path %s\n", root);
3858bf9e 166 goto err;
a696fa2a
JA
167 }
168 } else
7de87099 169 add_cgroup(td, root, clist);
a696fa2a 170
39f22027 171 if (td->o.cgroup_weight) {
8a4d0ff0
JA
172 if (write_int_to_file(td, root, "blkio.weight",
173 td->o.cgroup_weight,
174 "cgroup open weight"))
3858bf9e 175 goto err;
a696fa2a
JA
176 }
177
8a4d0ff0
JA
178 if (!cgroup_write_pid(td, root)) {
179 free(root);
180 return 0;
181 }
a696fa2a 182
3858bf9e
JA
183err:
184 free(root);
185 return 1;
a696fa2a
JA
186}
187
6adb38a1 188void cgroup_shutdown(struct thread_data *td, char **mnt)
a696fa2a 189{
6adb38a1 190 if (*mnt == NULL)
ddf16fe4 191 return;
ed81ee19 192 if (!td->o.cgroup_weight && !td->o.cgroup)
a696fa2a
JA
193 return;
194
6adb38a1 195 cgroup_del_pid(td, *mnt);
39f22027 196}
a696fa2a 197
39f22027
JA
198static void fio_init cgroup_init(void)
199{
971caeb1 200 lock = fio_sem_init(FIO_SEM_UNLOCKED);
fba5c5ff
JA
201 if (!lock)
202 log_err("fio: failed to allocate cgroup lock\n");
39f22027
JA
203}
204
205static void fio_exit cgroup_exit(void)
206{
971caeb1 207 fio_sem_remove(lock);
a696fa2a 208}