Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Code related to setting up a blkio cgroup |
| 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 6 | #include <mntent.h> |
Jens Axboe | 4f49b8a | 2010-06-01 14:03:40 +0200 | [diff] [blame] | 7 | #include <sys/stat.h> |
| 8 | #include <sys/types.h> |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 9 | #include "fio.h" |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 10 | #include "flist.h" |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 11 | #include "cgroup.h" |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 12 | #include "smalloc.h" |
| 13 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 14 | static struct fio_mutex *lock; |
| 15 | |
| 16 | struct cgroup_member { |
| 17 | struct flist_head list; |
| 18 | char *root; |
Vivek Goyal | 7de8709 | 2010-03-31 22:55:15 +0200 | [diff] [blame] | 19 | unsigned int cgroup_nodelete; |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 20 | }; |
| 21 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 22 | static char *find_cgroup_mnt(struct thread_data *td) |
| 23 | { |
| 24 | char *mntpoint = NULL; |
Zhu Yanhai | 599e72b | 2011-11-22 09:35:29 +0100 | [diff] [blame] | 25 | struct mntent *mnt, dummy; |
| 26 | char buf[256] = {0}; |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 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 | |
Zhu Yanhai | 599e72b | 2011-11-22 09:35:29 +0100 | [diff] [blame] | 35 | while ((mnt = getmntent_r(f, &dummy, buf, sizeof(buf))) != NULL) { |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 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 | |
Vivek Goyal | 7de8709 | 2010-03-31 22:55:15 +0200 | [diff] [blame] | 50 | static void add_cgroup(struct thread_data *td, const char *name, |
| 51 | struct flist_head *clist) |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 52 | { |
| 53 | struct cgroup_member *cm; |
| 54 | |
Jens Axboe | fba5c5f | 2013-01-29 22:29:09 +0100 | [diff] [blame] | 55 | if (!lock) |
| 56 | return; |
| 57 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 58 | cm = smalloc(sizeof(*cm)); |
Jens Axboe | fba5c5f | 2013-01-29 22:29:09 +0100 | [diff] [blame] | 59 | if (!cm) { |
| 60 | err: |
| 61 | log_err("fio: failed to allocate cgroup member\n"); |
| 62 | return; |
| 63 | } |
| 64 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 65 | INIT_FLIST_HEAD(&cm->list); |
| 66 | cm->root = smalloc_strdup(name); |
Jens Axboe | fba5c5f | 2013-01-29 22:29:09 +0100 | [diff] [blame] | 67 | if (!cm->root) { |
| 68 | sfree(cm); |
| 69 | goto err; |
| 70 | } |
Vivek Goyal | 7de8709 | 2010-03-31 22:55:15 +0200 | [diff] [blame] | 71 | if (td->o.cgroup_nodelete) |
| 72 | cm->cgroup_nodelete = 1; |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 73 | fio_mutex_down(lock); |
Jens Axboe | dae5341 | 2009-12-04 19:54:18 +0100 | [diff] [blame] | 74 | flist_add_tail(&cm->list, clist); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 75 | fio_mutex_up(lock); |
| 76 | } |
| 77 | |
Jens Axboe | dae5341 | 2009-12-04 19:54:18 +0100 | [diff] [blame] | 78 | void cgroup_kill(struct flist_head *clist) |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 79 | { |
| 80 | struct flist_head *n, *tmp; |
| 81 | struct cgroup_member *cm; |
| 82 | |
Jens Axboe | fba5c5f | 2013-01-29 22:29:09 +0100 | [diff] [blame] | 83 | if (!lock) |
| 84 | return; |
| 85 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 86 | fio_mutex_down(lock); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 87 | |
Jens Axboe | dae5341 | 2009-12-04 19:54:18 +0100 | [diff] [blame] | 88 | flist_for_each_safe(n, tmp, clist) { |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 89 | cm = flist_entry(n, struct cgroup_member, list); |
Vivek Goyal | 7de8709 | 2010-03-31 22:55:15 +0200 | [diff] [blame] | 90 | if (!cm->cgroup_nodelete) |
| 91 | rmdir(cm->root); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 92 | flist_del(&cm->list); |
| 93 | sfree(cm->root); |
| 94 | sfree(cm); |
| 95 | } |
| 96 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 97 | fio_mutex_up(lock); |
| 98 | } |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 99 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 100 | static char *get_cgroup_root(struct thread_data *td, char *mnt) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 101 | { |
| 102 | char *str = malloc(64); |
| 103 | |
| 104 | if (td->o.cgroup) |
Bruce Cran | b9fd788 | 2012-02-20 17:01:46 +0000 | [diff] [blame] | 105 | sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.cgroup); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 106 | else |
Bruce Cran | b9fd788 | 2012-02-20 17:01:46 +0000 | [diff] [blame] | 107 | sprintf(str, "%s%s%s", mnt, FIO_OS_PATH_SEPARATOR, td->o.name); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 108 | |
| 109 | return str; |
| 110 | } |
| 111 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 112 | static int write_int_to_file(struct thread_data *td, const char *path, |
| 113 | const char *filename, unsigned int val, |
| 114 | const char *onerr) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 115 | { |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 116 | char tmp[256]; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 117 | FILE *f; |
Bruce Cran | b9fd788 | 2012-02-20 17:01:46 +0000 | [diff] [blame] | 118 | |
| 119 | sprintf(tmp, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, filename); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 120 | f = fopen(tmp, "w"); |
| 121 | if (!f) { |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 122 | td_verror(td, errno, onerr); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 123 | return 1; |
| 124 | } |
| 125 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 126 | fprintf(f, "%u", val); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 127 | fclose(f); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 128 | return 0; |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 129 | |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 132 | static int cgroup_write_pid(struct thread_data *td, const char *root) |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 133 | { |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 134 | unsigned int val = td->pid; |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 135 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 136 | return write_int_to_file(td, root, "tasks", val, "cgroup write pid"); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Move pid to root class |
| 141 | */ |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 142 | static int cgroup_del_pid(struct thread_data *td, char *mnt) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 143 | { |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 144 | return cgroup_write_pid(td, mnt); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 147 | int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 148 | { |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 149 | char *root; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 150 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 151 | if (!*mnt) { |
| 152 | *mnt = find_cgroup_mnt(td); |
| 153 | if (!*mnt) |
| 154 | return 1; |
Jens Axboe | ddf16fe | 2009-12-04 10:54:45 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 157 | /* |
| 158 | * Create container, if it doesn't exist |
| 159 | */ |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 160 | root = get_cgroup_root(td, *mnt); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 161 | if (mkdir(root, 0755) < 0) { |
| 162 | int __e = errno; |
| 163 | |
| 164 | if (__e != EEXIST) { |
| 165 | td_verror(td, __e, "cgroup mkdir"); |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 166 | log_err("fio: path %s\n", root); |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 167 | goto err; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 168 | } |
| 169 | } else |
Vivek Goyal | 7de8709 | 2010-03-31 22:55:15 +0200 | [diff] [blame] | 170 | add_cgroup(td, root, clist); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 171 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 172 | if (td->o.cgroup_weight) { |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 173 | if (write_int_to_file(td, root, "blkio.weight", |
| 174 | td->o.cgroup_weight, |
| 175 | "cgroup open weight")) |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 176 | goto err; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 179 | if (!cgroup_write_pid(td, root)) { |
| 180 | free(root); |
| 181 | return 0; |
| 182 | } |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 183 | |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 184 | err: |
| 185 | free(root); |
| 186 | return 1; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 189 | void cgroup_shutdown(struct thread_data *td, char **mnt) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 190 | { |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 191 | if (*mnt == NULL) |
Jens Axboe | ddf16fe | 2009-12-04 10:54:45 +0100 | [diff] [blame] | 192 | return; |
Jens Axboe | ed81ee1 | 2009-12-05 22:30:04 +0100 | [diff] [blame] | 193 | if (!td->o.cgroup_weight && !td->o.cgroup) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 194 | return; |
| 195 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 196 | cgroup_del_pid(td, *mnt); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 197 | } |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 198 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 199 | static void fio_init cgroup_init(void) |
| 200 | { |
Jens Axboe | 521da52 | 2012-08-02 11:21:36 +0200 | [diff] [blame] | 201 | lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
Jens Axboe | fba5c5f | 2013-01-29 22:29:09 +0100 | [diff] [blame] | 202 | if (!lock) |
| 203 | log_err("fio: failed to allocate cgroup lock\n"); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void fio_exit cgroup_exit(void) |
| 207 | { |
| 208 | fio_mutex_remove(lock); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 209 | } |