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