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