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