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