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