blob: de00d986eb05246b25e853c3af524149f1b53e22 [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;
Zhu Yanhai599e72b2011-11-22 09:35:29 +010025 struct mntent *mnt, dummy;
26 char buf[256] = {0};
Jens Axboe6adb38a2009-12-07 08:01:26 +010027 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
Zhu Yanhai599e72b2011-11-22 09:35:29 +010035 while ((mnt = getmntent_r(f, &dummy, buf, sizeof(buf))) != NULL) {
Jens Axboe6adb38a2009-12-07 08:01:26 +010036 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
Vivek Goyal7de87092010-03-31 22:55:15 +020050static void add_cgroup(struct thread_data *td, const char *name,
51 struct flist_head *clist)
Jens Axboe39f22022009-12-04 11:29:12 +010052{
53 struct cgroup_member *cm;
54
55 cm = smalloc(sizeof(*cm));
56 INIT_FLIST_HEAD(&cm->list);
57 cm->root = smalloc_strdup(name);
Vivek Goyal7de87092010-03-31 22:55:15 +020058 if (td->o.cgroup_nodelete)
59 cm->cgroup_nodelete = 1;
Jens Axboe39f22022009-12-04 11:29:12 +010060 fio_mutex_down(lock);
Jens Axboedae53412009-12-04 19:54:18 +010061 flist_add_tail(&cm->list, clist);
Jens Axboe39f22022009-12-04 11:29:12 +010062 fio_mutex_up(lock);
63}
64
Jens Axboedae53412009-12-04 19:54:18 +010065void cgroup_kill(struct flist_head *clist)
Jens Axboe39f22022009-12-04 11:29:12 +010066{
67 struct flist_head *n, *tmp;
68 struct cgroup_member *cm;
69
70 fio_mutex_down(lock);
Jens Axboe39f22022009-12-04 11:29:12 +010071
Jens Axboedae53412009-12-04 19:54:18 +010072 flist_for_each_safe(n, tmp, clist) {
Jens Axboe39f22022009-12-04 11:29:12 +010073 cm = flist_entry(n, struct cgroup_member, list);
Vivek Goyal7de87092010-03-31 22:55:15 +020074 if (!cm->cgroup_nodelete)
75 rmdir(cm->root);
Jens Axboe39f22022009-12-04 11:29:12 +010076 flist_del(&cm->list);
77 sfree(cm->root);
78 sfree(cm);
79 }
80
Jens Axboe39f22022009-12-04 11:29:12 +010081 fio_mutex_up(lock);
82}
Jens Axboea696fa22009-12-04 10:05:02 +010083
Jens Axboe6adb38a2009-12-07 08:01:26 +010084static char *get_cgroup_root(struct thread_data *td, char *mnt)
Jens Axboea696fa22009-12-04 10:05:02 +010085{
86 char *str = malloc(64);
87
88 if (td->o.cgroup)
Jens Axboe6adb38a2009-12-07 08:01:26 +010089 sprintf(str, "%s/%s", mnt, td->o.cgroup);
Jens Axboea696fa22009-12-04 10:05:02 +010090 else
Jens Axboe6adb38a2009-12-07 08:01:26 +010091 sprintf(str, "%s/%s", mnt, td->o.name);
Jens Axboea696fa22009-12-04 10:05:02 +010092
93 return str;
94}
95
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010096static int write_int_to_file(struct thread_data *td, const char *path,
97 const char *filename, unsigned int val,
98 const char *onerr)
Jens Axboea696fa22009-12-04 10:05:02 +010099{
Jens Axboe3858bf92009-12-04 19:26:22 +0100100 char tmp[256];
Jens Axboea696fa22009-12-04 10:05:02 +0100101 FILE *f;
Jens Axboe3858bf92009-12-04 19:26:22 +0100102
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100103 sprintf(tmp, "%s/%s", path, filename);
Jens Axboea696fa22009-12-04 10:05:02 +0100104 f = fopen(tmp, "w");
105 if (!f) {
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100106 td_verror(td, errno, onerr);
Jens Axboea696fa22009-12-04 10:05:02 +0100107 return 1;
108 }
109
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100110 fprintf(f, "%u", val);
Jens Axboea696fa22009-12-04 10:05:02 +0100111 fclose(f);
Jens Axboea696fa22009-12-04 10:05:02 +0100112 return 0;
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100113
Jens Axboe3858bf92009-12-04 19:26:22 +0100114}
115
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100116static int cgroup_write_pid(struct thread_data *td, const char *root)
Jens Axboe3858bf92009-12-04 19:26:22 +0100117{
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100118 unsigned int val = td->pid;
Jens Axboe3858bf92009-12-04 19:26:22 +0100119
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100120 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
Jens Axboea696fa22009-12-04 10:05:02 +0100121}
122
123/*
124 * Move pid to root class
125 */
Jens Axboe6adb38a2009-12-07 08:01:26 +0100126static int cgroup_del_pid(struct thread_data *td, char *mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100127{
Jens Axboe6adb38a2009-12-07 08:01:26 +0100128 return cgroup_write_pid(td, mnt);
Jens Axboea696fa22009-12-04 10:05:02 +0100129}
130
Jens Axboe6adb38a2009-12-07 08:01:26 +0100131int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100132{
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100133 char *root;
Jens Axboea696fa22009-12-04 10:05:02 +0100134
Jens Axboe6adb38a2009-12-07 08:01:26 +0100135 if (!*mnt) {
136 *mnt = find_cgroup_mnt(td);
137 if (!*mnt)
138 return 1;
Jens Axboeddf16fe2009-12-04 10:54:45 +0100139 }
140
Jens Axboea696fa22009-12-04 10:05:02 +0100141 /*
142 * Create container, if it doesn't exist
143 */
Jens Axboe6adb38a2009-12-07 08:01:26 +0100144 root = get_cgroup_root(td, *mnt);
Jens Axboea696fa22009-12-04 10:05:02 +0100145 if (mkdir(root, 0755) < 0) {
146 int __e = errno;
147
148 if (__e != EEXIST) {
149 td_verror(td, __e, "cgroup mkdir");
Jens Axboe6adb38a2009-12-07 08:01:26 +0100150 log_err("fio: path %s\n", root);
Jens Axboe3858bf92009-12-04 19:26:22 +0100151 goto err;
Jens Axboea696fa22009-12-04 10:05:02 +0100152 }
153 } else
Vivek Goyal7de87092010-03-31 22:55:15 +0200154 add_cgroup(td, root, clist);
Jens Axboea696fa22009-12-04 10:05:02 +0100155
Jens Axboe39f22022009-12-04 11:29:12 +0100156 if (td->o.cgroup_weight) {
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100157 if (write_int_to_file(td, root, "blkio.weight",
158 td->o.cgroup_weight,
159 "cgroup open weight"))
Jens Axboe3858bf92009-12-04 19:26:22 +0100160 goto err;
Jens Axboea696fa22009-12-04 10:05:02 +0100161 }
162
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100163 if (!cgroup_write_pid(td, root)) {
164 free(root);
165 return 0;
166 }
Jens Axboea696fa22009-12-04 10:05:02 +0100167
Jens Axboe3858bf92009-12-04 19:26:22 +0100168err:
169 free(root);
170 return 1;
Jens Axboea696fa22009-12-04 10:05:02 +0100171}
172
Jens Axboe6adb38a2009-12-07 08:01:26 +0100173void cgroup_shutdown(struct thread_data *td, char **mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100174{
Jens Axboe6adb38a2009-12-07 08:01:26 +0100175 if (*mnt == NULL)
Jens Axboeddf16fe2009-12-04 10:54:45 +0100176 return;
Jens Axboeed81ee12009-12-05 22:30:04 +0100177 if (!td->o.cgroup_weight && !td->o.cgroup)
Jens Axboea696fa22009-12-04 10:05:02 +0100178 return;
179
Jens Axboe6adb38a2009-12-07 08:01:26 +0100180 cgroup_del_pid(td, *mnt);
Jens Axboe39f22022009-12-04 11:29:12 +0100181}
Jens Axboea696fa22009-12-04 10:05:02 +0100182
Jens Axboe39f22022009-12-04 11:29:12 +0100183static void fio_init cgroup_init(void)
184{
185 lock = fio_mutex_init(1);
186}
187
188static void fio_exit cgroup_exit(void)
189{
190 fio_mutex_remove(lock);
Jens Axboea696fa22009-12-04 10:05:02 +0100191}