blob: 34b61ded08d0fa5e22527012c979e31163b1b8ec [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
Jens Axboefba5c5f2013-01-29 22:29:09 +010055 if (!lock)
56 return;
57
Jens Axboe39f22022009-12-04 11:29:12 +010058 cm = smalloc(sizeof(*cm));
Jens Axboefba5c5f2013-01-29 22:29:09 +010059 if (!cm) {
60err:
61 log_err("fio: failed to allocate cgroup member\n");
62 return;
63 }
64
Jens Axboe39f22022009-12-04 11:29:12 +010065 INIT_FLIST_HEAD(&cm->list);
66 cm->root = smalloc_strdup(name);
Jens Axboefba5c5f2013-01-29 22:29:09 +010067 if (!cm->root) {
68 sfree(cm);
69 goto err;
70 }
Vivek Goyal7de87092010-03-31 22:55:15 +020071 if (td->o.cgroup_nodelete)
72 cm->cgroup_nodelete = 1;
Jens Axboe39f22022009-12-04 11:29:12 +010073 fio_mutex_down(lock);
Jens Axboedae53412009-12-04 19:54:18 +010074 flist_add_tail(&cm->list, clist);
Jens Axboe39f22022009-12-04 11:29:12 +010075 fio_mutex_up(lock);
76}
77
Jens Axboedae53412009-12-04 19:54:18 +010078void cgroup_kill(struct flist_head *clist)
Jens Axboe39f22022009-12-04 11:29:12 +010079{
80 struct flist_head *n, *tmp;
81 struct cgroup_member *cm;
82
Jens Axboefba5c5f2013-01-29 22:29:09 +010083 if (!lock)
84 return;
85
Jens Axboe39f22022009-12-04 11:29:12 +010086 fio_mutex_down(lock);
Jens Axboe39f22022009-12-04 11:29:12 +010087
Jens Axboedae53412009-12-04 19:54:18 +010088 flist_for_each_safe(n, tmp, clist) {
Jens Axboe39f22022009-12-04 11:29:12 +010089 cm = flist_entry(n, struct cgroup_member, list);
Vivek Goyal7de87092010-03-31 22:55:15 +020090 if (!cm->cgroup_nodelete)
91 rmdir(cm->root);
Jens Axboe39f22022009-12-04 11:29:12 +010092 flist_del(&cm->list);
93 sfree(cm->root);
94 sfree(cm);
95 }
96
Jens Axboe39f22022009-12-04 11:29:12 +010097 fio_mutex_up(lock);
98}
Jens Axboea696fa22009-12-04 10:05:02 +010099
Jens Axboe6adb38a2009-12-07 08:01:26 +0100100static char *get_cgroup_root(struct thread_data *td, char *mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100101{
102 char *str = malloc(64);
103
104 if (td->o.cgroup)
Bruce Cranb9fd7882012-02-20 17:01:46 +0000105 sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.cgroup);
Jens Axboea696fa22009-12-04 10:05:02 +0100106 else
Bruce Cranb9fd7882012-02-20 17:01:46 +0000107 sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.name);
Jens Axboea696fa22009-12-04 10:05:02 +0100108
109 return str;
110}
111
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100112static int write_int_to_file(struct thread_data *td, const char *path,
113 const char *filename, unsigned int val,
114 const char *onerr)
Jens Axboea696fa22009-12-04 10:05:02 +0100115{
Jens Axboe3858bf92009-12-04 19:26:22 +0100116 char tmp[256];
Jens Axboea696fa22009-12-04 10:05:02 +0100117 FILE *f;
Bruce Cranb9fd7882012-02-20 17:01:46 +0000118
119 sprintf(tmp, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, filename);
Jens Axboea696fa22009-12-04 10:05:02 +0100120 f = fopen(tmp, "w");
121 if (!f) {
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100122 td_verror(td, errno, onerr);
Jens Axboea696fa22009-12-04 10:05:02 +0100123 return 1;
124 }
125
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100126 fprintf(f, "%u", val);
Jens Axboea696fa22009-12-04 10:05:02 +0100127 fclose(f);
Jens Axboea696fa22009-12-04 10:05:02 +0100128 return 0;
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100129
Jens Axboe3858bf92009-12-04 19:26:22 +0100130}
131
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100132static int cgroup_write_pid(struct thread_data *td, const char *root)
Jens Axboe3858bf92009-12-04 19:26:22 +0100133{
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100134 unsigned int val = td->pid;
Jens Axboe3858bf92009-12-04 19:26:22 +0100135
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100136 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
Jens Axboea696fa22009-12-04 10:05:02 +0100137}
138
139/*
140 * Move pid to root class
141 */
Jens Axboe6adb38a2009-12-07 08:01:26 +0100142static int cgroup_del_pid(struct thread_data *td, char *mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100143{
Jens Axboe6adb38a2009-12-07 08:01:26 +0100144 return cgroup_write_pid(td, mnt);
Jens Axboea696fa22009-12-04 10:05:02 +0100145}
146
Jens Axboe6adb38a2009-12-07 08:01:26 +0100147int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100148{
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100149 char *root;
Jens Axboea696fa22009-12-04 10:05:02 +0100150
Jens Axboe6adb38a2009-12-07 08:01:26 +0100151 if (!*mnt) {
152 *mnt = find_cgroup_mnt(td);
153 if (!*mnt)
154 return 1;
Jens Axboeddf16fe2009-12-04 10:54:45 +0100155 }
156
Jens Axboea696fa22009-12-04 10:05:02 +0100157 /*
158 * Create container, if it doesn't exist
159 */
Jens Axboe6adb38a2009-12-07 08:01:26 +0100160 root = get_cgroup_root(td, *mnt);
Jens Axboea696fa22009-12-04 10:05:02 +0100161 if (mkdir(root, 0755) < 0) {
162 int __e = errno;
163
164 if (__e != EEXIST) {
165 td_verror(td, __e, "cgroup mkdir");
Jens Axboe6adb38a2009-12-07 08:01:26 +0100166 log_err("fio: path %s\n", root);
Jens Axboe3858bf92009-12-04 19:26:22 +0100167 goto err;
Jens Axboea696fa22009-12-04 10:05:02 +0100168 }
169 } else
Vivek Goyal7de87092010-03-31 22:55:15 +0200170 add_cgroup(td, root, clist);
Jens Axboea696fa22009-12-04 10:05:02 +0100171
Jens Axboe39f22022009-12-04 11:29:12 +0100172 if (td->o.cgroup_weight) {
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100173 if (write_int_to_file(td, root, "blkio.weight",
174 td->o.cgroup_weight,
175 "cgroup open weight"))
Jens Axboe3858bf92009-12-04 19:26:22 +0100176 goto err;
Jens Axboea696fa22009-12-04 10:05:02 +0100177 }
178
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100179 if (!cgroup_write_pid(td, root)) {
180 free(root);
181 return 0;
182 }
Jens Axboea696fa22009-12-04 10:05:02 +0100183
Jens Axboe3858bf92009-12-04 19:26:22 +0100184err:
185 free(root);
186 return 1;
Jens Axboea696fa22009-12-04 10:05:02 +0100187}
188
Jens Axboe6adb38a2009-12-07 08:01:26 +0100189void cgroup_shutdown(struct thread_data *td, char **mnt)
Jens Axboea696fa22009-12-04 10:05:02 +0100190{
Jens Axboe6adb38a2009-12-07 08:01:26 +0100191 if (*mnt == NULL)
Jens Axboeddf16fe2009-12-04 10:54:45 +0100192 return;
Jens Axboeed81ee12009-12-05 22:30:04 +0100193 if (!td->o.cgroup_weight && !td->o.cgroup)
Jens Axboea696fa22009-12-04 10:05:02 +0100194 return;
195
Jens Axboe6adb38a2009-12-07 08:01:26 +0100196 cgroup_del_pid(td, *mnt);
Jens Axboe39f22022009-12-04 11:29:12 +0100197}
Jens Axboea696fa22009-12-04 10:05:02 +0100198
Jens Axboe39f22022009-12-04 11:29:12 +0100199static void fio_init cgroup_init(void)
200{
Jens Axboe521da522012-08-02 11:21:36 +0200201 lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboefba5c5f2013-01-29 22:29:09 +0100202 if (!lock)
203 log_err("fio: failed to allocate cgroup lock\n");
Jens Axboe39f22022009-12-04 11:29:12 +0100204}
205
206static void fio_exit cgroup_exit(void)
207{
208 fio_mutex_remove(lock);
Jens Axboea696fa22009-12-04 10:05:02 +0100209}