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