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 | |
| 105 | static void disk_util_add(int majdev, int mindev, char *path) |
| 106 | { |
| 107 | struct disk_util *du, *__du; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 108 | struct flist_head *entry; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 109 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 110 | dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path); |
| 111 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 112 | du = malloc(sizeof(*du)); |
| 113 | memset(du, 0, sizeof(*du)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 114 | INIT_FLIST_HEAD(&du->list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 115 | sprintf(du->path, "%s/stat", path); |
| 116 | du->name = strdup(basename(path)); |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 117 | du->sysfs_root = path; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 118 | du->major = majdev; |
| 119 | du->minor = mindev; |
| 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 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 124 | dprint(FD_DISKUTIL, "found %s in list\n", __du->name); |
| 125 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 126 | if (!strcmp(du->name, __du->name)) { |
| 127 | free(du->name); |
| 128 | free(du); |
| 129 | return; |
| 130 | } |
| 131 | } |
| 132 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 133 | dprint(FD_DISKUTIL, "add %s to list\n", du->name); |
| 134 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 135 | fio_gettime(&du->time, NULL); |
| 136 | get_io_ticks(du, &du->last_dus); |
| 137 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 138 | flist_add_tail(&du->list, &disk_list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static int check_dev_match(int majdev, int mindev, char *path) |
| 142 | { |
| 143 | int major, minor; |
| 144 | char line[256], *p; |
| 145 | FILE *f; |
| 146 | |
| 147 | f = fopen(path, "r"); |
| 148 | if (!f) { |
| 149 | perror("open path"); |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | p = fgets(line, sizeof(line), f); |
| 154 | if (!p) { |
| 155 | fclose(f); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | if (sscanf(p, "%u:%u", &major, &minor) != 2) { |
| 160 | fclose(f); |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | if (majdev == major && mindev == minor) { |
| 165 | fclose(f); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | fclose(f); |
| 170 | return 1; |
| 171 | } |
| 172 | |
Jens Axboe | ac0be2a | 2008-06-06 12:08:04 +0200 | [diff] [blame] | 173 | 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] | 174 | { |
| 175 | struct dirent *dir; |
| 176 | struct stat st; |
| 177 | int found = 0; |
| 178 | DIR *D; |
| 179 | |
| 180 | D = opendir(path); |
| 181 | if (!D) |
| 182 | return 0; |
| 183 | |
| 184 | while ((dir = readdir(D)) != NULL) { |
| 185 | char full_path[256]; |
| 186 | |
| 187 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 188 | continue; |
| 189 | |
| 190 | sprintf(full_path, "%s/%s", path, dir->d_name); |
| 191 | |
| 192 | if (!strcmp(dir->d_name, "dev")) { |
| 193 | if (!check_dev_match(majdev, mindev, full_path)) { |
| 194 | found = 1; |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
Jens Axboe | ac0be2a | 2008-06-06 12:08:04 +0200 | [diff] [blame] | 199 | if (link_ok) { |
| 200 | if (stat(full_path, &st) == -1) { |
| 201 | perror("stat"); |
| 202 | break; |
| 203 | } |
| 204 | } else { |
| 205 | if (lstat(full_path, &st) == -1) { |
| 206 | perror("stat"); |
| 207 | break; |
| 208 | } |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) |
| 212 | continue; |
| 213 | |
Jens Axboe | ac0be2a | 2008-06-06 12:08:04 +0200 | [diff] [blame] | 214 | found = find_block_dir(majdev, mindev, full_path, 0); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 215 | if (found) { |
| 216 | strcpy(path, full_path); |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | closedir(D); |
| 222 | return found; |
| 223 | } |
| 224 | |
| 225 | static void __init_disk_util(struct thread_data *td, struct fio_file *f) |
| 226 | { |
| 227 | struct stat st; |
| 228 | char foo[PATH_MAX], tmp[PATH_MAX]; |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 229 | struct disk_util *du; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 230 | int mindev, majdev; |
| 231 | char *p; |
| 232 | |
Jens Axboe | 8cc7afa | 2007-04-17 09:06:43 +0200 | [diff] [blame] | 233 | if (!lstat(f->file_name, &st)) { |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 234 | if (S_ISBLK(st.st_mode)) { |
| 235 | majdev = major(st.st_rdev); |
| 236 | mindev = minor(st.st_rdev); |
| 237 | } else if (S_ISCHR(st.st_mode)) { |
| 238 | majdev = major(st.st_rdev); |
| 239 | mindev = minor(st.st_rdev); |
Jens Axboe | 8cc7afa | 2007-04-17 09:06:43 +0200 | [diff] [blame] | 240 | if (fio_lookup_raw(st.st_rdev, &majdev, &mindev)) |
| 241 | return; |
| 242 | } else if (S_ISFIFO(st.st_mode)) |
| 243 | return; |
| 244 | else { |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 245 | majdev = major(st.st_dev); |
| 246 | mindev = minor(st.st_dev); |
| 247 | } |
| 248 | } else { |
| 249 | /* |
| 250 | * must be a file, open "." in that path |
| 251 | */ |
| 252 | strncpy(foo, f->file_name, PATH_MAX - 1); |
| 253 | p = dirname(foo); |
| 254 | if (stat(p, &st)) { |
| 255 | perror("disk util stat"); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | majdev = major(st.st_dev); |
| 260 | mindev = minor(st.st_dev); |
| 261 | } |
| 262 | |
Jens Axboe | cd991b9 | 2008-03-07 13:19:35 +0100 | [diff] [blame] | 263 | dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", f->file_name, |
| 264 | majdev, mindev); |
| 265 | |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 266 | du = disk_util_exists(majdev, mindev); |
| 267 | if (du) { |
| 268 | if (td->o.ioscheduler && !td->sysfs_root) |
| 269 | td->sysfs_root = strdup(du->sysfs_root); |
| 270 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 271 | return; |
Jens Axboe | e11c410 | 2007-04-03 16:33:04 -0700 | [diff] [blame] | 272 | } |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * for an fs without a device, we will repeatedly stat through |
| 276 | * sysfs which can take oodles of time for thousands of files. so |
| 277 | * cache the last lookup and compare with that before going through |
| 278 | * everything again. |
| 279 | */ |
| 280 | if (mindev == last_mindev && majdev == last_majdev) |
| 281 | return; |
| 282 | |
| 283 | last_mindev = mindev; |
| 284 | last_majdev = majdev; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 285 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 286 | sprintf(foo, "/sys/block"); |
Jens Axboe | ac0be2a | 2008-06-06 12:08:04 +0200 | [diff] [blame] | 287 | if (!find_block_dir(majdev, mindev, foo, 1)) |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 288 | return; |
| 289 | |
| 290 | /* |
| 291 | * If there's a ../queue/ directory there, we are inside a partition. |
| 292 | * Check if that is the case and jump back. For loop/md/dm etc we |
| 293 | * are already in the right spot. |
| 294 | */ |
| 295 | sprintf(tmp, "%s/../queue", foo); |
| 296 | if (!stat(tmp, &st)) { |
| 297 | p = dirname(foo); |
| 298 | sprintf(tmp, "%s/queue", p); |
| 299 | if (stat(tmp, &st)) { |
| 300 | log_err("unknown sysfs layout\n"); |
| 301 | return; |
| 302 | } |
| 303 | strncpy(tmp, p, PATH_MAX - 1); |
| 304 | sprintf(foo, "%s", tmp); |
| 305 | } |
| 306 | |
| 307 | if (td->o.ioscheduler && !td->sysfs_root) |
| 308 | td->sysfs_root = strdup(foo); |
| 309 | |
| 310 | disk_util_add(majdev, mindev, foo); |
| 311 | } |
| 312 | |
| 313 | void init_disk_util(struct thread_data *td) |
| 314 | { |
| 315 | struct fio_file *f; |
| 316 | unsigned int i; |
| 317 | |
| 318 | if (!td->o.do_disk_util || |
| 319 | (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL))) |
| 320 | return; |
| 321 | |
| 322 | for_each_file(td, f, i) |
| 323 | __init_disk_util(td, f); |
| 324 | } |
| 325 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 326 | void show_disk_util(void) |
| 327 | { |
| 328 | struct disk_util_stat *dus; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 329 | struct flist_head *entry, *next; |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 330 | struct disk_util *du; |
| 331 | double util; |
| 332 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 333 | if (flist_empty(&disk_list)) |
Jens Axboe | 0a839f3 | 2007-04-26 09:02:34 +0200 | [diff] [blame] | 334 | return; |
| 335 | |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 336 | log_info("\nDisk stats (read/write):\n"); |
| 337 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 338 | flist_for_each(entry, &disk_list) { |
| 339 | du = flist_entry(entry, struct disk_util, list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 340 | dus = &du->dus; |
| 341 | |
| 342 | util = (double) 100 * du->dus.io_ticks / (double) du->msec; |
| 343 | if (util > 100.0) |
| 344 | util = 100.0; |
| 345 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 346 | log_info(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, " |
| 347 | "in_queue=%u, util=%3.2f%%\n", du->name, |
| 348 | dus->ios[0], dus->ios[1], |
| 349 | dus->merges[0], dus->merges[1], |
| 350 | dus->ticks[0], dus->ticks[1], |
| 351 | dus->time_in_queue, util); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | * now free the list |
| 356 | */ |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 357 | flist_for_each_safe(entry, next, &disk_list) { |
| 358 | flist_del(entry); |
| 359 | du = flist_entry(entry, struct disk_util, list); |
Jens Axboe | 9f8f206 | 2007-04-02 15:08:01 +0200 | [diff] [blame] | 360 | free(du->name); |
| 361 | free(du); |
| 362 | } |
| 363 | } |