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 | |
| 55 | cm = smalloc(sizeof(*cm)); |
| 56 | INIT_FLIST_HEAD(&cm->list); |
| 57 | cm->root = smalloc_strdup(name); |
Vivek Goyal | 7de8709 | 2010-03-31 22:55:15 +0200 | [diff] [blame] | 58 | if (td->o.cgroup_nodelete) |
| 59 | cm->cgroup_nodelete = 1; |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 60 | fio_mutex_down(lock); |
Jens Axboe | dae5341 | 2009-12-04 19:54:18 +0100 | [diff] [blame] | 61 | flist_add_tail(&cm->list, clist); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 62 | fio_mutex_up(lock); |
| 63 | } |
| 64 | |
Jens Axboe | dae5341 | 2009-12-04 19:54:18 +0100 | [diff] [blame] | 65 | void cgroup_kill(struct flist_head *clist) |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 66 | { |
| 67 | struct flist_head *n, *tmp; |
| 68 | struct cgroup_member *cm; |
| 69 | |
| 70 | fio_mutex_down(lock); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 71 | |
Jens Axboe | dae5341 | 2009-12-04 19:54:18 +0100 | [diff] [blame] | 72 | flist_for_each_safe(n, tmp, clist) { |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 73 | cm = flist_entry(n, struct cgroup_member, list); |
Vivek Goyal | 7de8709 | 2010-03-31 22:55:15 +0200 | [diff] [blame] | 74 | if (!cm->cgroup_nodelete) |
| 75 | rmdir(cm->root); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 76 | flist_del(&cm->list); |
| 77 | sfree(cm->root); |
| 78 | sfree(cm); |
| 79 | } |
| 80 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 81 | fio_mutex_up(lock); |
| 82 | } |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 83 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 84 | static char *get_cgroup_root(struct thread_data *td, char *mnt) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 85 | { |
| 86 | char *str = malloc(64); |
| 87 | |
| 88 | if (td->o.cgroup) |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 89 | sprintf(str, "%s/%s", mnt, td->o.cgroup); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 90 | else |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 91 | sprintf(str, "%s/%s", mnt, td->o.name); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 92 | |
| 93 | return str; |
| 94 | } |
| 95 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 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) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 99 | { |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 100 | char tmp[256]; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 101 | FILE *f; |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 102 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 103 | sprintf(tmp, "%s/%s", path, filename); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 104 | f = fopen(tmp, "w"); |
| 105 | if (!f) { |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 106 | td_verror(td, errno, onerr); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 107 | return 1; |
| 108 | } |
| 109 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 110 | fprintf(f, "%u", val); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 111 | fclose(f); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 112 | return 0; |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 113 | |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 116 | static int cgroup_write_pid(struct thread_data *td, const char *root) |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 117 | { |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 118 | unsigned int val = td->pid; |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 119 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 120 | return write_int_to_file(td, root, "tasks", val, "cgroup write pid"); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Move pid to root class |
| 125 | */ |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 126 | static int cgroup_del_pid(struct thread_data *td, char *mnt) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 127 | { |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 128 | return cgroup_write_pid(td, mnt); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 131 | 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] | 132 | { |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 133 | char *root; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 134 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 135 | if (!*mnt) { |
| 136 | *mnt = find_cgroup_mnt(td); |
| 137 | if (!*mnt) |
| 138 | return 1; |
Jens Axboe | ddf16fe | 2009-12-04 10:54:45 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 141 | /* |
| 142 | * Create container, if it doesn't exist |
| 143 | */ |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 144 | root = get_cgroup_root(td, *mnt); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 145 | if (mkdir(root, 0755) < 0) { |
| 146 | int __e = errno; |
| 147 | |
| 148 | if (__e != EEXIST) { |
| 149 | td_verror(td, __e, "cgroup mkdir"); |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 150 | log_err("fio: path %s\n", root); |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 151 | goto err; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 152 | } |
| 153 | } else |
Vivek Goyal | 7de8709 | 2010-03-31 22:55:15 +0200 | [diff] [blame] | 154 | add_cgroup(td, root, clist); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 155 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 156 | if (td->o.cgroup_weight) { |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 157 | if (write_int_to_file(td, root, "blkio.weight", |
| 158 | td->o.cgroup_weight, |
| 159 | "cgroup open weight")) |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 160 | goto err; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Jens Axboe | 8a4d0ff | 2009-12-04 22:13:43 +0100 | [diff] [blame] | 163 | if (!cgroup_write_pid(td, root)) { |
| 164 | free(root); |
| 165 | return 0; |
| 166 | } |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 167 | |
Jens Axboe | 3858bf9 | 2009-12-04 19:26:22 +0100 | [diff] [blame] | 168 | err: |
| 169 | free(root); |
| 170 | return 1; |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 173 | void cgroup_shutdown(struct thread_data *td, char **mnt) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 174 | { |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 175 | if (*mnt == NULL) |
Jens Axboe | ddf16fe | 2009-12-04 10:54:45 +0100 | [diff] [blame] | 176 | return; |
Jens Axboe | ed81ee1 | 2009-12-05 22:30:04 +0100 | [diff] [blame] | 177 | if (!td->o.cgroup_weight && !td->o.cgroup) |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 178 | return; |
| 179 | |
Jens Axboe | 6adb38a | 2009-12-07 08:01:26 +0100 | [diff] [blame] | 180 | cgroup_del_pid(td, *mnt); |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 181 | } |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 182 | |
Jens Axboe | 39f2202 | 2009-12-04 11:29:12 +0100 | [diff] [blame] | 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); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame] | 191 | } |