Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <sys/time.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <dirent.h> |
| 7 | #include <libgen.h> |
| 8 | #include <math.h> |
| 9 | |
| 10 | #include "fio.h" |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 11 | #include "smalloc.h" |
Jens Axboe | 7c9b1bc | 2009-06-03 09:25:57 +0200 | [diff] [blame^] | 12 | #include "diskutil.h" |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 13 | |
| 14 | static int last_majdev, last_mindev; |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 15 | static struct disk_util *last_du; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 16 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 17 | static struct flist_head disk_list = FLIST_HEAD_INIT(disk_list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 18 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 19 | static void disk_util_free(struct disk_util *du) |
| 20 | { |
| 21 | if (du == last_du) |
| 22 | last_du = NULL; |
| 23 | |
| 24 | fio_mutex_remove(du->lock); |
| 25 | sfree(du->name); |
| 26 | sfree(du); |
| 27 | } |
| 28 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 29 | static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus) |
| 30 | { |
| 31 | unsigned in_flight; |
| 32 | char line[256]; |
| 33 | FILE *f; |
| 34 | char *p; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 35 | int ret; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 36 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 37 | dprint(FD_DISKUTIL, "open stat file: %s\n", du->path); |
| 38 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 39 | f = fopen(du->path, "r"); |
| 40 | if (!f) |
| 41 | return 1; |
| 42 | |
| 43 | p = fgets(line, sizeof(line), f); |
| 44 | if (!p) { |
| 45 | fclose(f); |
| 46 | return 1; |
| 47 | } |
| 48 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 49 | dprint(FD_DISKUTIL, "%s: %s", du->path, p); |
| 50 | |
| 51 | ret = sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 52 | &dus->merges[0], &dus->sectors[0], |
| 53 | &dus->ticks[0], &dus->ios[1], |
| 54 | &dus->merges[1], &dus->sectors[1], |
| 55 | &dus->ticks[1], &in_flight, |
| 56 | &dus->io_ticks, &dus->time_in_queue); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 57 | fclose(f); |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 58 | dprint(FD_DISKUTIL, "%s: stat read ok? %d\n", du->path, ret == 1); |
| 59 | return ret != 11; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static void update_io_tick_disk(struct disk_util *du) |
| 63 | { |
| 64 | struct disk_util_stat __dus, *dus, *ldus; |
| 65 | struct timeval t; |
| 66 | |
| 67 | if (get_io_ticks(du, &__dus)) |
| 68 | return; |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 69 | if (!du->users) |
| 70 | return; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 71 | |
| 72 | dus = &du->dus; |
| 73 | ldus = &du->last_dus; |
| 74 | |
| 75 | dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]); |
| 76 | dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]); |
| 77 | dus->ios[0] += (__dus.ios[0] - ldus->ios[0]); |
| 78 | dus->ios[1] += (__dus.ios[1] - ldus->ios[1]); |
| 79 | dus->merges[0] += (__dus.merges[0] - ldus->merges[0]); |
| 80 | dus->merges[1] += (__dus.merges[1] - ldus->merges[1]); |
| 81 | dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]); |
| 82 | dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]); |
| 83 | dus->io_ticks += (__dus.io_ticks - ldus->io_ticks); |
| 84 | dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue); |
| 85 | |
| 86 | fio_gettime(&t, NULL); |
| 87 | du->msec += mtime_since(&du->time, &t); |
| 88 | memcpy(&du->time, &t, sizeof(t)); |
| 89 | memcpy(ldus, &__dus, sizeof(__dus)); |
| 90 | } |
| 91 | |
| 92 | void update_io_ticks(void) |
| 93 | { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 94 | struct flist_head *entry; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 95 | struct disk_util *du; |
| 96 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 97 | dprint(FD_DISKUTIL, "update io ticks\n"); |
| 98 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 99 | flist_for_each(entry, &disk_list) { |
| 100 | du = flist_entry(entry, struct disk_util, list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 101 | update_io_tick_disk(du); |
| 102 | } |
| 103 | } |
| 104 | |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 105 | static struct disk_util *disk_util_exists(int major, int minor) |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 106 | { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 107 | struct flist_head *entry; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 108 | struct disk_util *du; |
| 109 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 110 | flist_for_each(entry, &disk_list) { |
| 111 | du = flist_entry(entry, struct disk_util, list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 112 | |
| 113 | if (major == du->major && minor == du->minor) |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 114 | return du; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 117 | return NULL; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 120 | static int get_device_numbers(char *file_name, int *maj, int *min) |
| 121 | { |
| 122 | struct stat st; |
| 123 | int majdev, mindev; |
| 124 | char tempname[PATH_MAX], *p; |
| 125 | |
| 126 | if (!lstat(file_name, &st)) { |
| 127 | if (S_ISBLK(st.st_mode)) { |
| 128 | majdev = major(st.st_rdev); |
| 129 | mindev = minor(st.st_rdev); |
| 130 | } else if (S_ISCHR(st.st_mode)) { |
| 131 | majdev = major(st.st_rdev); |
| 132 | mindev = minor(st.st_rdev); |
| 133 | if (fio_lookup_raw(st.st_rdev, &majdev, &mindev)) |
| 134 | return -1; |
| 135 | } else if (S_ISFIFO(st.st_mode)) |
| 136 | return -1; |
| 137 | else { |
| 138 | majdev = major(st.st_dev); |
| 139 | mindev = minor(st.st_dev); |
| 140 | } |
| 141 | } else { |
| 142 | /* |
| 143 | * must be a file, open "." in that path |
| 144 | */ |
| 145 | strncpy(tempname, file_name, PATH_MAX - 1); |
| 146 | p = dirname(tempname); |
| 147 | if (stat(p, &st)) { |
| 148 | perror("disk util stat"); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | majdev = major(st.st_dev); |
| 153 | mindev = minor(st.st_dev); |
| 154 | } |
| 155 | |
| 156 | *min = mindev; |
| 157 | *maj = majdev; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int read_block_dev_entry(char *path, int *maj, int *min) |
| 163 | { |
| 164 | char line[256], *p; |
| 165 | FILE *f; |
| 166 | |
| 167 | f = fopen(path, "r"); |
| 168 | if (!f) { |
| 169 | perror("open path"); |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | p = fgets(line, sizeof(line), f); |
| 174 | fclose(f); |
| 175 | |
| 176 | if (!p) |
| 177 | return 1; |
| 178 | |
| 179 | if (sscanf(p, "%u:%u", maj, min) != 2) |
| 180 | return 1; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 185 | static struct disk_util *__init_per_file_disk_util(struct thread_data *td, |
| 186 | int majdev, int mindev, char * path); |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 187 | |
| 188 | static void find_add_disk_slaves(struct thread_data *td, char *path, |
| 189 | struct disk_util *masterdu) |
| 190 | { |
| 191 | DIR *dirhandle = NULL; |
| 192 | struct dirent *dirent = NULL; |
| 193 | char slavesdir[PATH_MAX], temppath[PATH_MAX], slavepath[PATH_MAX]; |
| 194 | struct disk_util *slavedu = NULL; |
| 195 | int majdev, mindev; |
| 196 | ssize_t linklen; |
| 197 | |
| 198 | sprintf(slavesdir, "%s/%s",path, "slaves"); |
| 199 | dirhandle = opendir(slavesdir); |
| 200 | if (!dirhandle) |
| 201 | return; |
| 202 | |
| 203 | while ((dirent = readdir(dirhandle)) != NULL) { |
| 204 | if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) |
| 205 | continue; |
| 206 | |
| 207 | sprintf(temppath, "%s/%s", slavesdir, dirent->d_name); |
| 208 | /* Can we always assume that the slaves device entries |
| 209 | * are links to the real directories for the slave |
| 210 | * devices? |
| 211 | */ |
| 212 | if ((linklen = readlink(temppath, slavepath, PATH_MAX-1)) < 0) { |
| 213 | perror("readlink() for slave device."); |
| 214 | return; |
| 215 | } |
| 216 | slavepath[linklen]='\0'; |
| 217 | |
| 218 | sprintf(temppath, "%s/%s/dev", slavesdir, slavepath); |
| 219 | if (read_block_dev_entry(temppath, &majdev, &mindev)) { |
| 220 | perror("Error getting slave device numbers."); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | sprintf(temppath, "%s/%s", slavesdir, slavepath); |
| 225 | __init_per_file_disk_util(td, majdev, mindev, temppath); |
| 226 | slavedu = disk_util_exists(majdev, mindev); |
| 227 | |
| 228 | /* Should probably use an assert here. slavedu should |
| 229 | * always be present at this point. */ |
| 230 | if (slavedu) |
| 231 | flist_add_tail(&slavedu->slavelist, &masterdu->slaves); |
| 232 | } |
| 233 | |
| 234 | closedir(dirhandle); |
| 235 | return; |
| 236 | } |
| 237 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 238 | static struct disk_util *disk_util_add(struct thread_data * td, int majdev, |
| 239 | int mindev, char *path) |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 240 | { |
| 241 | struct disk_util *du, *__du; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 242 | struct flist_head *entry; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 243 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 244 | dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path); |
| 245 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 246 | du = smalloc(sizeof(*du)); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 247 | memset(du, 0, sizeof(*du)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 248 | INIT_FLIST_HEAD(&du->list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 249 | sprintf(du->path, "%s/stat", path); |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 250 | du->name = smalloc_strdup(basename(path)); |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 251 | du->sysfs_root = path; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 252 | du->major = majdev; |
| 253 | du->minor = mindev; |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 254 | INIT_FLIST_HEAD(&du->slavelist); |
| 255 | INIT_FLIST_HEAD(&du->slaves); |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 256 | du->lock = fio_mutex_init(1); |
| 257 | du->users = 0; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 258 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 259 | flist_for_each(entry, &disk_list) { |
| 260 | __du = flist_entry(entry, struct disk_util, list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 261 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 262 | dprint(FD_DISKUTIL, "found %s in list\n", __du->name); |
| 263 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 264 | if (!strcmp(du->name, __du->name)) { |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 265 | disk_util_free(du); |
| 266 | return __du; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 270 | dprint(FD_DISKUTIL, "add %s to list\n", du->name); |
| 271 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 272 | fio_gettime(&du->time, NULL); |
| 273 | get_io_ticks(du, &du->last_dus); |
| 274 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 275 | flist_add_tail(&du->list, &disk_list); |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 276 | find_add_disk_slaves(td, path, du); |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 277 | return du; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 280 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 281 | static int check_dev_match(int majdev, int mindev, char *path) |
| 282 | { |
| 283 | int major, minor; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 284 | |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 285 | if (read_block_dev_entry(path, &major, &minor)) |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 286 | return 1; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 287 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 288 | if (majdev == major && mindev == minor) |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 289 | return 0; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 290 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 291 | return 1; |
| 292 | } |
| 293 | |
Jens Axboe | ac0be2a | 2008-06-06 12:08:04 +0200 | [diff] [blame] | 294 | static int find_block_dir(int majdev, int mindev, char *path, int link_ok) |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 295 | { |
| 296 | struct dirent *dir; |
| 297 | struct stat st; |
| 298 | int found = 0; |
| 299 | DIR *D; |
| 300 | |
| 301 | D = opendir(path); |
| 302 | if (!D) |
| 303 | return 0; |
| 304 | |
| 305 | while ((dir = readdir(D)) != NULL) { |
| 306 | char full_path[256]; |
| 307 | |
| 308 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 309 | continue; |
| 310 | |
| 311 | sprintf(full_path, "%s/%s", path, dir->d_name); |
| 312 | |
| 313 | if (!strcmp(dir->d_name, "dev")) { |
| 314 | if (!check_dev_match(majdev, mindev, full_path)) { |
| 315 | found = 1; |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
Jens Axboe | ac0be2a | 2008-06-06 12:08:04 +0200 | [diff] [blame] | 320 | if (link_ok) { |
| 321 | if (stat(full_path, &st) == -1) { |
| 322 | perror("stat"); |
| 323 | break; |
| 324 | } |
| 325 | } else { |
| 326 | if (lstat(full_path, &st) == -1) { |
| 327 | perror("stat"); |
| 328 | break; |
| 329 | } |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) |
| 333 | continue; |
| 334 | |
Jens Axboe | ac0be2a | 2008-06-06 12:08:04 +0200 | [diff] [blame] | 335 | found = find_block_dir(majdev, mindev, full_path, 0); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 336 | if (found) { |
| 337 | strcpy(path, full_path); |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | closedir(D); |
| 343 | return found; |
| 344 | } |
| 345 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 346 | static struct disk_util *__init_per_file_disk_util(struct thread_data *td, |
| 347 | int majdev, int mindev, |
| 348 | char *path) |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 349 | { |
| 350 | struct stat st; |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 351 | char tmp[PATH_MAX]; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 352 | char *p; |
| 353 | |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 354 | /* |
| 355 | * If there's a ../queue/ directory there, we are inside a partition. |
| 356 | * Check if that is the case and jump back. For loop/md/dm etc we |
| 357 | * are already in the right spot. |
| 358 | */ |
| 359 | sprintf(tmp, "%s/../queue", path); |
| 360 | if (!stat(tmp, &st)) { |
| 361 | p = dirname(path); |
| 362 | sprintf(tmp, "%s/queue", p); |
| 363 | if (stat(tmp, &st)) { |
| 364 | log_err("unknown sysfs layout\n"); |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 365 | return NULL; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 366 | } |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 367 | strncpy(tmp, p, PATH_MAX - 1); |
| 368 | sprintf(path, "%s", tmp); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 371 | if (td->o.ioscheduler && !td->sysfs_root) |
| 372 | td->sysfs_root = strdup(path); |
| 373 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 374 | return disk_util_add(td, majdev, mindev, path); |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 375 | } |
| 376 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 377 | static struct disk_util *init_per_file_disk_util(struct thread_data *td, |
| 378 | char *filename) |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 379 | { |
| 380 | |
| 381 | char foo[PATH_MAX]; |
| 382 | struct disk_util *du; |
| 383 | int mindev, majdev; |
| 384 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 385 | if (get_device_numbers(filename, &majdev, &mindev)) |
| 386 | return NULL; |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 387 | |
| 388 | dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", filename, majdev, |
| 389 | mindev); |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 390 | |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 391 | du = disk_util_exists(majdev, mindev); |
| 392 | if (du) { |
| 393 | if (td->o.ioscheduler && !td->sysfs_root) |
| 394 | td->sysfs_root = strdup(du->sysfs_root); |
| 395 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 396 | return du; |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 397 | } |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 398 | |
| 399 | /* |
| 400 | * for an fs without a device, we will repeatedly stat through |
| 401 | * sysfs which can take oodles of time for thousands of files. so |
| 402 | * cache the last lookup and compare with that before going through |
| 403 | * everything again. |
| 404 | */ |
| 405 | if (mindev == last_mindev && majdev == last_majdev) |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 406 | return last_du; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 407 | |
| 408 | last_mindev = mindev; |
| 409 | last_majdev = majdev; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 410 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 411 | sprintf(foo, "/sys/block"); |
Jens Axboe | ac0be2a | 2008-06-06 12:08:04 +0200 | [diff] [blame] | 412 | if (!find_block_dir(majdev, mindev, foo, 1)) |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 413 | return NULL; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 414 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 415 | return __init_per_file_disk_util(td, majdev, mindev, foo); |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 416 | } |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 417 | |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 418 | static struct disk_util *__init_disk_util(struct thread_data *td, |
| 419 | struct fio_file *f) |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 420 | { |
| 421 | return init_per_file_disk_util(td, f->file_name); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void init_disk_util(struct thread_data *td) |
| 425 | { |
| 426 | struct fio_file *f; |
| 427 | unsigned int i; |
| 428 | |
| 429 | if (!td->o.do_disk_util || |
| 430 | (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL))) |
| 431 | return; |
| 432 | |
| 433 | for_each_file(td, f, i) |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 434 | f->du = __init_disk_util(td, f); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Shehjar Tikoo | 043b74b | 2008-09-30 11:11:35 +1000 | [diff] [blame] | 437 | static void aggregate_slaves_stats(struct disk_util *masterdu) |
| 438 | { |
| 439 | struct disk_util_stat *dus; |
| 440 | struct flist_head *entry; |
| 441 | struct disk_util *slavedu; |
| 442 | double util, max_util = 0; |
| 443 | int slavecount = 0; |
| 444 | |
Jens Axboe | 5e28df3 | 2008-09-30 08:39:17 +0200 | [diff] [blame] | 445 | unsigned merges[2] = { 0, }; |
| 446 | unsigned ticks[2] = { 0, }; |
| 447 | unsigned time_in_queue = { 0, }; |
| 448 | unsigned long long sectors[2] = { 0, }; |
| 449 | unsigned ios[2] = { 0, }; |
Shehjar Tikoo | 043b74b | 2008-09-30 11:11:35 +1000 | [diff] [blame] | 450 | |
| 451 | flist_for_each(entry, &masterdu->slaves) { |
| 452 | slavedu = flist_entry(entry, struct disk_util, slavelist); |
| 453 | dus = &slavedu->dus; |
| 454 | ios[0] += dus->ios[0]; |
| 455 | ios[1] += dus->ios[1]; |
| 456 | merges[0] += dus->merges[0]; |
| 457 | merges[1] += dus->merges[1]; |
| 458 | sectors[0] += dus->sectors[0]; |
| 459 | sectors[1] += dus->sectors[1]; |
| 460 | ticks[0] += dus->ticks[0]; |
| 461 | ticks[1] += dus->ticks[1]; |
| 462 | time_in_queue += dus->time_in_queue; |
| 463 | ++slavecount; |
| 464 | |
| 465 | util = (double) (100 * dus->io_ticks / (double) slavedu->msec); |
| 466 | /* System utilization is the utilization of the |
| 467 | * component with the highest utilization. |
| 468 | */ |
| 469 | if (util > max_util) |
| 470 | max_util = util; |
| 471 | |
| 472 | } |
| 473 | |
| 474 | if (max_util > 100.0) |
| 475 | max_util = 100.0; |
| 476 | |
| 477 | log_info(", aggrios=%u/%u, aggrmerge=%u/%u, aggrticks=%u/%u," |
| 478 | " aggrin_queue=%u, aggrutil=%3.2f%%", |
| 479 | ios[0]/slavecount, ios[1]/slavecount, |
| 480 | merges[0]/slavecount, merges[1]/slavecount, |
| 481 | ticks[0]/slavecount, ticks[1]/slavecount, |
| 482 | time_in_queue/slavecount, max_util); |
| 483 | |
| 484 | } |
| 485 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 486 | void show_disk_util(void) |
| 487 | { |
| 488 | struct disk_util_stat *dus; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 489 | struct flist_head *entry, *next; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 490 | struct disk_util *du; |
| 491 | double util; |
| 492 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 493 | if (flist_empty(&disk_list)) |
Jens Axboe | 0a839f3 | 2007-04-26 09:02:34 +0200 | [diff] [blame] | 494 | return; |
| 495 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 496 | log_info("\nDisk stats (read/write):\n"); |
| 497 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 498 | flist_for_each(entry, &disk_list) { |
| 499 | du = flist_entry(entry, struct disk_util, list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 500 | dus = &du->dus; |
| 501 | |
| 502 | util = (double) 100 * du->dus.io_ticks / (double) du->msec; |
| 503 | if (util > 100.0) |
| 504 | util = 100.0; |
| 505 | |
Shehjar Tikoo | 6742301 | 2008-09-30 10:35:44 +1000 | [diff] [blame] | 506 | /* If this node is the slave of a master device, as |
| 507 | * happens in case of software RAIDs, inward-indent |
| 508 | * this stats line to reflect a master-slave |
| 509 | * relationship. Because the master device gets added |
| 510 | * before the slave devices, we can safely assume that |
| 511 | * the master's stats line has been displayed in a |
| 512 | * previous iteration of this loop. |
| 513 | */ |
| 514 | if(!flist_empty(&du->slavelist)) |
| 515 | log_info(" "); |
| 516 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 517 | log_info(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, " |
Shehjar Tikoo | 043b74b | 2008-09-30 11:11:35 +1000 | [diff] [blame] | 518 | "in_queue=%u, util=%3.2f%%", du->name, |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 519 | dus->ios[0], dus->ios[1], |
| 520 | dus->merges[0], dus->merges[1], |
| 521 | dus->ticks[0], dus->ticks[1], |
| 522 | dus->time_in_queue, util); |
Shehjar Tikoo | 043b74b | 2008-09-30 11:11:35 +1000 | [diff] [blame] | 523 | |
| 524 | /* If the device has slaves, aggregate the stats for |
| 525 | * those slave devices also. |
| 526 | */ |
| 527 | if(!flist_empty(&du->slaves)) |
| 528 | aggregate_slaves_stats(du); |
| 529 | |
| 530 | log_info("\n"); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
| 534 | * now free the list |
| 535 | */ |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 536 | flist_for_each_safe(entry, next, &disk_list) { |
| 537 | flist_del(entry); |
| 538 | du = flist_entry(entry, struct disk_util, list); |
Jens Axboe | c97bd0f | 2009-05-27 13:11:20 +0200 | [diff] [blame] | 539 | disk_util_free(du); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 540 | } |
| 541 | } |