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