blob: f36683aeacf311561c6ba768565361c547fcd9bf [file] [log] [blame]
Jens Axboea696fa22009-12-04 10:05:02 +01001/*
2 * Code related to setting up a blkio cgroup
3 */
4#include <stdio.h>
5#include <stdlib.h>
Jens Axboe6adb38a2009-12-07 08:01:26 +01006#include <mntent.h>
Jens Axboe4f49b8a2010-06-01 14:03:40 +02007#include <sys/stat.h>
8#include <sys/types.h>
Jens Axboea696fa22009-12-04 10:05:02 +01009#include "fio.h"
Jens Axboe39f22022009-12-04 11:29:12 +010010#include "flist.h"
Jens Axboea696fa22009-12-04 10:05:02 +010011#include "cgroup.h"
Jens Axboe39f22022009-12-04 11:29:12 +010012#include "smalloc.h"
13
Jens Axboe39f22022009-12-04 11:29:12 +010014static struct fio_mutex *lock;
15
16struct cgroup_member {
17 struct flist_head list;
18 char *root;
Vivek Goyal7de87092010-03-31 22:55:15 +020019 unsigned int cgroup_nodelete;
Jens Axboe39f22022009-12-04 11:29:12 +010020};
21
Jens Axboe6adb38a2009-12-07 08:01:26 +010022static 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
Vivek Goyal7de87092010-03-31 22:55:15 +020049static void add_cgroup(struct thread_data *td, const char *name,
50 struct flist_head *clist)
Jens Axboe39f22022009-12-04 11:29:12 +010051{
52 struct cgroup_member *cm;
53
54 cm = smalloc(sizeof(*cm));
55 INIT_FLIST_HEAD(&cm->list);
56 cm->root = smalloc_strdup(name);
Vivek Goyal7de87092010-03-31 22:55:15 +020057 if (td->o.cgroup_nodelete)
58 cm->cgroup_nodelete = 1;
Jens Axboe39f22022009-12-04 11:29:12 +010059 fio_mutex_down(lock);
Jens Axboedae53412009-12-04 19:54:18 +010060 flist_add_tail(&cm->list, clist);
Jens Axboe39f22022009-12-04 11:29:12 +010061 fio_mutex_up(lock);
62}
63
Jens Axboedae53412009-12-04 19:54:18 +010064void cgroup_kill(struct flist_head *clist)
Jens Axboe39f22022009-12-04 11:29:12 +010065{
66 struct flist_head *n, *tmp;
67 struct cgroup_member *cm;
68
69 fio_mutex_down(lock);
Jens Axboe39f22022009-12-04 11:29:12 +010070
Jens Axboedae53412009-12-04 19:54:18 +010071 flist_for_each_safe(n, tmp, clist) {
Jens Axboe39f22022009-12-04 11:29:12 +010072 cm = flist_entry(n, struct cgroup_member, list);
Vivek Goyal7de87092010-03-31 22:55:15 +020073 if (!cm->cgroup_nodelete)
74 rmdir(cm->root);
Jens Axboe39f22022009-12-04 11:29:12 +010075 flist_del(&cm->list);
76 sfree(cm->root);
77 sfree(cm);
78 }
79
Jens Axboe39f22022009-12-04 11:29:12 +010080 fio_mutex_up(lock);
81}
Jens Axboea696fa22009-12-04 10:05:02 +010082
Jens Axboe6adb38a2009-12-07 08:01:26 +010083static char *get_cgroup_root(struct thread_data *td, char *mnt)
Jens Axboea696fa22009-12-04 10:05:02 +010084{
85 char *str = malloc(64);
86
87 if (td->o.cgroup)
Jens Axboe6adb38a2009-12-07 08:01:26 +010088 sprintf(str, "%s/%s", mnt, td->o.cgroup);
Jens Axboea696fa22009-12-04 10:05:02 +010089 else
Jens Axboe6adb38a2009-12-07 08:01:26 +010090 sprintf(str, "%s/%s", mnt, td->o.name);
Jens Axboea696fa22009-12-04 10:05:02 +010091
92 return str;
93}
94
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010095static int write_int_to_file(struct thread_data *td, const char *path,
96 const char *filename, unsigned int val,
97 const char *onerr)
Jens Axboea696fa22009-12-04 10:05:02 +010098{
Jens Axboe3858bf92009-12-04 19:26:22 +010099 char tmp[256];
Jens Axboea696fa22009-12-04 10:05:02 +0100100 FILE *f;
Jens Axboe3858bf92009-12-04 19:26:22 +0100101
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100102 sprintf(tmp, "%s/%s", path, filename);
Jens Axboea696fa22009-12-04 10:05:02 +0100103 f = fopen(tmp, "w");
104 if (!f) {
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100105 td_verror(td, errno, onerr);
Jens Axboea696fa22009-12-04 10:05:02 +0100106 return 1;
107 }
108
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100109 fprintf(f, "%u", val);
Jens Axboea696fa22009-12-04 10:05:02 +0100110 fclose(f);
Jens Axboea696fa22009-12-04 10:05:02 +0100111 return 0;
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100112
Jens Axboe3858bf92009-12-04 19:26:22 +0100113}
114
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100115static int cgroup_write_pid(struct thread_data *td, const char *root)
Jens Axboe3858bf92009-12-04 19:26:22 +0100116{
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100117 unsigned int val = td->pid;
Jens Axboe3858bf92009-12-04 19:26:22 +0100118
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100119 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
Jens Axboea696fa22009-12-04 10:05:02 +0100120}
121
122/*
123 * Move pid to root class
124 */
Jens Axboe6adb38a2009-12-07 08:01:26 +0100125static int cgroup_del_pid(struct thread_data *td, char *mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100126{
Jens Axboe6adb38a2009-12-07 08:01:26 +0100127 return cgroup_write_pid(td, mnt);
Jens Axboea696fa22009-12-04 10:05:02 +0100128}
129
Jens Axboe6adb38a2009-12-07 08:01:26 +0100130int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100131{
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100132 char *root;
Jens Axboea696fa22009-12-04 10:05:02 +0100133
Jens Axboe6adb38a2009-12-07 08:01:26 +0100134 if (!*mnt) {
135 *mnt = find_cgroup_mnt(td);
136 if (!*mnt)
137 return 1;
Jens Axboeddf16fe2009-12-04 10:54:45 +0100138 }
139
Jens Axboea696fa22009-12-04 10:05:02 +0100140 /*
141 * Create container, if it doesn't exist
142 */
Jens Axboe6adb38a2009-12-07 08:01:26 +0100143 root = get_cgroup_root(td, *mnt);
Jens Axboea696fa22009-12-04 10:05:02 +0100144 if (mkdir(root, 0755) < 0) {
145 int __e = errno;
146
147 if (__e != EEXIST) {
148 td_verror(td, __e, "cgroup mkdir");
Jens Axboe6adb38a2009-12-07 08:01:26 +0100149 log_err("fio: path %s\n", root);
Jens Axboe3858bf92009-12-04 19:26:22 +0100150 goto err;
Jens Axboea696fa22009-12-04 10:05:02 +0100151 }
152 } else
Vivek Goyal7de87092010-03-31 22:55:15 +0200153 add_cgroup(td, root, clist);
Jens Axboea696fa22009-12-04 10:05:02 +0100154
Jens Axboe39f22022009-12-04 11:29:12 +0100155 if (td->o.cgroup_weight) {
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100156 if (write_int_to_file(td, root, "blkio.weight",
157 td->o.cgroup_weight,
158 "cgroup open weight"))
Jens Axboe3858bf92009-12-04 19:26:22 +0100159 goto err;
Jens Axboea696fa22009-12-04 10:05:02 +0100160 }
161
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100162 if (!cgroup_write_pid(td, root)) {
163 free(root);
164 return 0;
165 }
Jens Axboea696fa22009-12-04 10:05:02 +0100166
Jens Axboe3858bf92009-12-04 19:26:22 +0100167err:
168 free(root);
169 return 1;
Jens Axboea696fa22009-12-04 10:05:02 +0100170}
171
Jens Axboe6adb38a2009-12-07 08:01:26 +0100172void cgroup_shutdown(struct thread_data *td, char **mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100173{
Jens Axboe6adb38a2009-12-07 08:01:26 +0100174 if (*mnt == NULL)
Jens Axboeddf16fe2009-12-04 10:54:45 +0100175 return;
Jens Axboeed81ee12009-12-05 22:30:04 +0100176 if (!td->o.cgroup_weight && !td->o.cgroup)
Jens Axboea696fa22009-12-04 10:05:02 +0100177 return;
178
Jens Axboe6adb38a2009-12-07 08:01:26 +0100179 cgroup_del_pid(td, *mnt);
Jens Axboe39f22022009-12-04 11:29:12 +0100180}
Jens Axboea696fa22009-12-04 10:05:02 +0100181
Jens Axboe39f22022009-12-04 11:29:12 +0100182static 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);
Jens Axboea696fa22009-12-04 10:05:02 +0100190}