| 1 | /* |
| 2 | * block queue tracing parse application |
| 3 | * |
| 4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <getopt.h> |
| 29 | #include <errno.h> |
| 30 | #include <signal.h> |
| 31 | #include <locale.h> |
| 32 | #include <limits.h> |
| 33 | #include <ctype.h> |
| 34 | |
| 35 | #include "blktrace.h" |
| 36 | #include "rbtree.h" |
| 37 | |
| 38 | #define SECONDS(x) ((unsigned long long)(x) / 1000000000) |
| 39 | #define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000) |
| 40 | #define DOUBLE_TO_NANO_ULL(d) ((unsigned long long)((d) * 1000000000)) |
| 41 | |
| 42 | #define MINORBITS 20 |
| 43 | #define MINORMASK ((1U << MINORBITS) - 1) |
| 44 | #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS)) |
| 45 | #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK)) |
| 46 | |
| 47 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 48 | |
| 49 | #define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d " |
| 50 | |
| 51 | struct io_stats { |
| 52 | unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites; |
| 53 | unsigned long ireads, iwrites; |
| 54 | unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb; |
| 55 | unsigned long long iread_kb, iwrite_kb; |
| 56 | unsigned long io_unplugs, timer_unplugs; |
| 57 | }; |
| 58 | |
| 59 | struct per_cpu_info { |
| 60 | int cpu; |
| 61 | int nelems; |
| 62 | |
| 63 | int fd; |
| 64 | char fname[128]; |
| 65 | |
| 66 | struct io_stats io_stats; |
| 67 | }; |
| 68 | |
| 69 | struct per_dev_info { |
| 70 | dev_t id; |
| 71 | char *name; |
| 72 | |
| 73 | int backwards; |
| 74 | unsigned long long events; |
| 75 | unsigned long long last_reported_time; |
| 76 | struct io_stats io_stats; |
| 77 | unsigned long last_sequence; |
| 78 | unsigned long skips; |
| 79 | |
| 80 | int ncpus; |
| 81 | struct per_cpu_info *cpus; |
| 82 | }; |
| 83 | |
| 84 | struct per_process_info { |
| 85 | char name[16]; |
| 86 | __u32 pid; |
| 87 | struct io_stats io_stats; |
| 88 | struct per_process_info *hash_next, *list_next; |
| 89 | |
| 90 | /* |
| 91 | * individual io stats |
| 92 | */ |
| 93 | unsigned long long longest_allocation_wait[2]; |
| 94 | unsigned long long longest_dispatch_wait[2]; |
| 95 | unsigned long long longest_completion_wait[2]; |
| 96 | }; |
| 97 | |
| 98 | #define PPI_HASH_SHIFT (8) |
| 99 | static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT]; |
| 100 | static struct per_process_info *ppi_list; |
| 101 | static int ppi_list_entries; |
| 102 | |
| 103 | #define S_OPTS "i:o:b:stqw:f:F:" |
| 104 | static struct option l_opts[] = { |
| 105 | { |
| 106 | .name = "input", |
| 107 | .has_arg = required_argument, |
| 108 | .flag = NULL, |
| 109 | .val = 'i' |
| 110 | }, |
| 111 | { |
| 112 | .name = "output", |
| 113 | .has_arg = required_argument, |
| 114 | .flag = NULL, |
| 115 | .val = 'o' |
| 116 | }, |
| 117 | { |
| 118 | .name = "batch", |
| 119 | .has_arg = required_argument, |
| 120 | .flag = NULL, |
| 121 | .val = 'b' |
| 122 | }, |
| 123 | { |
| 124 | .name = "per program stats", |
| 125 | .has_arg = no_argument, |
| 126 | .flag = NULL, |
| 127 | .val = 's' |
| 128 | }, |
| 129 | { |
| 130 | .name = "track ios", |
| 131 | .has_arg = no_argument, |
| 132 | .flag = NULL, |
| 133 | .val = 't' |
| 134 | }, |
| 135 | { |
| 136 | .name = "quiet", |
| 137 | .has_arg = no_argument, |
| 138 | .flag = NULL, |
| 139 | .val = 'q' |
| 140 | }, |
| 141 | { |
| 142 | .name = "stopwatch", |
| 143 | .has_arg = required_argument, |
| 144 | .flag = NULL, |
| 145 | .val = 'w' |
| 146 | }, |
| 147 | { |
| 148 | .name = "format", |
| 149 | .has_arg = required_argument, |
| 150 | .flag = NULL, |
| 151 | .val = 'f' |
| 152 | }, |
| 153 | { |
| 154 | .name = "format-spec", |
| 155 | .has_arg = required_argument, |
| 156 | .flag = NULL, |
| 157 | .val = 'F' |
| 158 | }, |
| 159 | }; |
| 160 | |
| 161 | /* |
| 162 | * for sorting the displayed output |
| 163 | */ |
| 164 | struct trace { |
| 165 | struct blk_io_trace *bit; |
| 166 | struct rb_node rb_node; |
| 167 | struct trace *next; |
| 168 | int skipped; |
| 169 | }; |
| 170 | |
| 171 | static struct rb_root rb_sort_root; |
| 172 | static struct rb_root rb_track_root; |
| 173 | |
| 174 | static struct trace *trace_list; |
| 175 | |
| 176 | /* |
| 177 | * for tracking individual ios |
| 178 | */ |
| 179 | struct io_track { |
| 180 | struct rb_node rb_node; |
| 181 | |
| 182 | dev_t device; |
| 183 | __u64 sector; |
| 184 | __u32 pid; |
| 185 | unsigned long long allocation_time; |
| 186 | unsigned long long queue_time; |
| 187 | unsigned long long dispatch_time; |
| 188 | unsigned long long completion_time; |
| 189 | }; |
| 190 | |
| 191 | static int ndevices; |
| 192 | static struct per_dev_info *devices; |
| 193 | static char *get_dev_name(struct per_dev_info *, char *, int); |
| 194 | |
| 195 | static FILE *ofp; |
| 196 | static char *output_name; |
| 197 | |
| 198 | static unsigned long long genesis_time; |
| 199 | static unsigned long long stopwatch_start; /* start from zero by default */ |
| 200 | static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */ |
| 201 | |
| 202 | static int per_process_stats; |
| 203 | static int track_ios; |
| 204 | |
| 205 | #define RB_BATCH_DEFAULT (1024) |
| 206 | static int rb_batch = RB_BATCH_DEFAULT; |
| 207 | |
| 208 | static int pipeline; |
| 209 | |
| 210 | #define is_done() (*(volatile int *)(&done)) |
| 211 | static volatile int done; |
| 212 | |
| 213 | static inline unsigned long hash_long(unsigned long val) |
| 214 | { |
| 215 | #if __WORDSIZE == 32 |
| 216 | val *= 0x9e370001UL; |
| 217 | #elif __WORDSIZE == 64 |
| 218 | val *= 0x9e37fffffffc0001UL; |
| 219 | #else |
| 220 | #error unknown word size |
| 221 | #endif |
| 222 | |
| 223 | return val >> (__WORDSIZE - PPI_HASH_SHIFT); |
| 224 | } |
| 225 | |
| 226 | static inline void add_process_to_hash(struct per_process_info *ppi) |
| 227 | { |
| 228 | const int hash_idx = hash_long(ppi->pid); |
| 229 | |
| 230 | ppi->hash_next = ppi_hash[hash_idx]; |
| 231 | ppi_hash[hash_idx] = ppi; |
| 232 | } |
| 233 | |
| 234 | static inline void add_process_to_list(struct per_process_info *ppi) |
| 235 | { |
| 236 | ppi->list_next = ppi_list; |
| 237 | ppi_list = ppi; |
| 238 | ppi_list_entries++; |
| 239 | } |
| 240 | |
| 241 | static struct per_process_info *find_process_by_pid(__u32 pid) |
| 242 | { |
| 243 | const int hash_idx = hash_long(pid); |
| 244 | struct per_process_info *ppi; |
| 245 | |
| 246 | ppi = ppi_hash[hash_idx]; |
| 247 | while (ppi) { |
| 248 | if (ppi->pid == pid) |
| 249 | return ppi; |
| 250 | |
| 251 | ppi = ppi->hash_next; |
| 252 | } |
| 253 | |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | static inline int trace_rb_insert(struct trace *t) |
| 258 | { |
| 259 | struct rb_node **p = &rb_sort_root.rb_node; |
| 260 | struct rb_node *parent = NULL; |
| 261 | struct trace *__t; |
| 262 | |
| 263 | if (genesis_time == 0 || t->bit->time < genesis_time) |
| 264 | genesis_time = t->bit->time; |
| 265 | |
| 266 | while (*p) { |
| 267 | parent = *p; |
| 268 | __t = rb_entry(parent, struct trace, rb_node); |
| 269 | |
| 270 | if (t->bit->time < __t->bit->time) |
| 271 | p = &(*p)->rb_left; |
| 272 | else if (t->bit->time > __t->bit->time) |
| 273 | p = &(*p)->rb_right; |
| 274 | else if (t->bit->device < __t->bit->device) |
| 275 | p = &(*p)->rb_left; |
| 276 | else if (t->bit->device > __t->bit->device) |
| 277 | p = &(*p)->rb_right; |
| 278 | else if (t->bit->sequence < __t->bit->sequence) |
| 279 | p = &(*p)->rb_left; |
| 280 | else if (t->bit->sequence > __t->bit->sequence) |
| 281 | p = &(*p)->rb_right; |
| 282 | else if (t->bit->device == __t->bit->device) { |
| 283 | fprintf(stderr, |
| 284 | "sequence alias (%d) on device %d,%d!\n", |
| 285 | t->bit->sequence, |
| 286 | MAJOR(t->bit->device), MINOR(t->bit->device)); |
| 287 | return 1; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | rb_link_node(&t->rb_node, parent, p); |
| 292 | rb_insert_color(&t->rb_node, &rb_sort_root); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static inline int track_rb_insert(struct io_track *iot) |
| 297 | { |
| 298 | struct rb_node **p = &rb_track_root.rb_node; |
| 299 | struct rb_node *parent = NULL; |
| 300 | struct io_track *__iot; |
| 301 | |
| 302 | while (*p) { |
| 303 | parent = *p; |
| 304 | |
| 305 | __iot = rb_entry(parent, struct io_track, rb_node); |
| 306 | |
| 307 | if (iot->device < __iot->device) |
| 308 | p = &(*p)->rb_left; |
| 309 | else if (iot->device > __iot->device) |
| 310 | p = &(*p)->rb_right; |
| 311 | else if (iot->sector < __iot->sector) |
| 312 | p = &(*p)->rb_left; |
| 313 | else if (iot->sector > __iot->sector) |
| 314 | p = &(*p)->rb_right; |
| 315 | else { |
| 316 | fprintf(stderr, |
| 317 | "sector alias (%Lu) on device %d,%d!\n", |
| 318 | (unsigned long long) iot->sector, |
| 319 | MAJOR(iot->device), MINOR(iot->device)); |
| 320 | return 1; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | rb_link_node(&iot->rb_node, parent, p); |
| 325 | rb_insert_color(&iot->rb_node, &rb_track_root); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static struct io_track *__find_track(dev_t device, __u64 sector) |
| 330 | { |
| 331 | struct rb_node **p = &rb_track_root.rb_node; |
| 332 | struct rb_node *parent = NULL; |
| 333 | struct io_track *__iot; |
| 334 | |
| 335 | while (*p) { |
| 336 | parent = *p; |
| 337 | |
| 338 | __iot = rb_entry(parent, struct io_track, rb_node); |
| 339 | |
| 340 | if (device < __iot->device) |
| 341 | p = &(*p)->rb_left; |
| 342 | else if (device > __iot->device) |
| 343 | p = &(*p)->rb_right; |
| 344 | else if (sector < __iot->sector) |
| 345 | p = &(*p)->rb_left; |
| 346 | else if (sector > __iot->sector) |
| 347 | p = &(*p)->rb_right; |
| 348 | else |
| 349 | return __iot; |
| 350 | } |
| 351 | |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector) |
| 356 | { |
| 357 | struct io_track *iot; |
| 358 | |
| 359 | iot = __find_track(device, sector); |
| 360 | if (!iot) { |
| 361 | iot = malloc(sizeof(*iot)); |
| 362 | iot->pid = pid; |
| 363 | iot->device = device; |
| 364 | iot->sector = sector; |
| 365 | track_rb_insert(iot); |
| 366 | } |
| 367 | |
| 368 | return iot; |
| 369 | } |
| 370 | |
| 371 | static void log_track_frontmerge(struct blk_io_trace *t) |
| 372 | { |
| 373 | struct io_track *iot; |
| 374 | |
| 375 | if (!track_ios) |
| 376 | return; |
| 377 | |
| 378 | iot = __find_track(t->device, t->sector + (t->bytes >> 9)); |
| 379 | if (!iot) { |
| 380 | fprintf(stderr, "failed to find mergeable event\n"); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | rb_erase(&iot->rb_node, &rb_track_root); |
| 385 | iot->sector -= t->bytes >> 9; |
| 386 | track_rb_insert(iot); |
| 387 | } |
| 388 | |
| 389 | static void log_track_getrq(struct blk_io_trace *t) |
| 390 | { |
| 391 | struct io_track *iot; |
| 392 | |
| 393 | if (!track_ios) |
| 394 | return; |
| 395 | |
| 396 | iot = find_track(t->pid, t->device, t->sector); |
| 397 | iot->allocation_time = t->time; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | /* |
| 402 | * return time between rq allocation and queue |
| 403 | */ |
| 404 | static unsigned long long log_track_queue(struct blk_io_trace *t) |
| 405 | { |
| 406 | unsigned long long elapsed; |
| 407 | struct io_track *iot; |
| 408 | |
| 409 | if (!track_ios) |
| 410 | return -1; |
| 411 | |
| 412 | iot = find_track(t->pid, t->device, t->sector); |
| 413 | iot->queue_time = t->time; |
| 414 | elapsed = iot->queue_time - iot->allocation_time; |
| 415 | |
| 416 | if (per_process_stats) { |
| 417 | struct per_process_info *ppi = find_process_by_pid(iot->pid); |
| 418 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 419 | |
| 420 | if (ppi && elapsed > ppi->longest_allocation_wait[w]) |
| 421 | ppi->longest_allocation_wait[w] = elapsed; |
| 422 | } |
| 423 | |
| 424 | return elapsed; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * return time between queue and issue |
| 429 | */ |
| 430 | static unsigned long long log_track_issue(struct blk_io_trace *t) |
| 431 | { |
| 432 | unsigned long long elapsed; |
| 433 | struct io_track *iot; |
| 434 | |
| 435 | if (!track_ios) |
| 436 | return -1; |
| 437 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) |
| 438 | return -1; |
| 439 | |
| 440 | iot = __find_track(t->device, t->sector); |
| 441 | if (!iot) { |
| 442 | fprintf(stderr, "failed to find issue event\n"); |
| 443 | return -1; |
| 444 | } |
| 445 | |
| 446 | iot->dispatch_time = t->time; |
| 447 | elapsed = iot->dispatch_time - iot->queue_time; |
| 448 | |
| 449 | if (per_process_stats) { |
| 450 | struct per_process_info *ppi = find_process_by_pid(iot->pid); |
| 451 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 452 | |
| 453 | if (ppi && elapsed > ppi->longest_dispatch_wait[w]) |
| 454 | ppi->longest_dispatch_wait[w] = elapsed; |
| 455 | } |
| 456 | |
| 457 | return elapsed; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * return time between dispatch and complete |
| 462 | */ |
| 463 | static unsigned long long log_track_complete(struct blk_io_trace *t) |
| 464 | { |
| 465 | unsigned long long elapsed; |
| 466 | struct io_track *iot; |
| 467 | |
| 468 | if (!track_ios) |
| 469 | return -1; |
| 470 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) |
| 471 | return -1; |
| 472 | |
| 473 | iot = __find_track(t->device, t->sector); |
| 474 | if (!iot) { |
| 475 | fprintf(stderr, "failed to find complete event\n"); |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | iot->completion_time = t->time; |
| 480 | elapsed = iot->completion_time - iot->dispatch_time; |
| 481 | |
| 482 | if (per_process_stats) { |
| 483 | struct per_process_info *ppi = find_process_by_pid(iot->pid); |
| 484 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 485 | |
| 486 | if (ppi && elapsed > ppi->longest_completion_wait[w]) |
| 487 | ppi->longest_completion_wait[w] = elapsed; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * kill the trace, we don't need it after completion |
| 492 | */ |
| 493 | rb_erase(&iot->rb_node, &rb_track_root); |
| 494 | free(iot); |
| 495 | |
| 496 | return elapsed; |
| 497 | } |
| 498 | |
| 499 | |
| 500 | static struct io_stats *find_process_io_stats(__u32 pid, char *name) |
| 501 | { |
| 502 | struct per_process_info *ppi = find_process_by_pid(pid); |
| 503 | |
| 504 | if (!ppi) { |
| 505 | ppi = malloc(sizeof(*ppi)); |
| 506 | memset(ppi, 0, sizeof(*ppi)); |
| 507 | strncpy(ppi->name, name, sizeof(ppi->name)); |
| 508 | ppi->pid = pid; |
| 509 | add_process_to_hash(ppi); |
| 510 | add_process_to_list(ppi); |
| 511 | } |
| 512 | |
| 513 | return &ppi->io_stats; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | static void resize_cpu_info(struct per_dev_info *pdi, int cpu) |
| 518 | { |
| 519 | struct per_cpu_info *cpus = pdi->cpus; |
| 520 | int ncpus = pdi->ncpus; |
| 521 | int new_count = cpu + 1; |
| 522 | int new_space, size; |
| 523 | char *new_start; |
| 524 | |
| 525 | size = new_count * sizeof(struct per_cpu_info); |
| 526 | cpus = realloc(cpus, size); |
| 527 | if (!cpus) { |
| 528 | char name[20]; |
| 529 | fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n", |
| 530 | get_dev_name(pdi, name, sizeof(name)), size); |
| 531 | exit(1); |
| 532 | } |
| 533 | |
| 534 | new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info)); |
| 535 | new_space = (new_count - ncpus) * sizeof(struct per_cpu_info); |
| 536 | memset(new_start, 0, new_space); |
| 537 | |
| 538 | pdi->ncpus = new_count; |
| 539 | pdi->cpus = cpus; |
| 540 | } |
| 541 | |
| 542 | static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu) |
| 543 | { |
| 544 | struct per_cpu_info *pci; |
| 545 | |
| 546 | if (cpu >= pdi->ncpus) |
| 547 | resize_cpu_info(pdi, cpu); |
| 548 | |
| 549 | pci = &pdi->cpus[cpu]; |
| 550 | pci->cpu = cpu; |
| 551 | return pci; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | static int resize_devices(char *name) |
| 556 | { |
| 557 | int size = (ndevices + 1) * sizeof(struct per_dev_info); |
| 558 | |
| 559 | devices = realloc(devices, size); |
| 560 | if (!devices) { |
| 561 | fprintf(stderr, "Out of memory, device %s (%d)\n", name, size); |
| 562 | return 1; |
| 563 | } |
| 564 | memset(&devices[ndevices], 0, sizeof(struct per_dev_info)); |
| 565 | devices[ndevices].name = name; |
| 566 | ndevices++; |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | static struct per_dev_info *get_dev_info(dev_t id) |
| 571 | { |
| 572 | struct per_dev_info *pdi; |
| 573 | int i; |
| 574 | |
| 575 | for (i = 0; i < ndevices; i++) |
| 576 | if (devices[i].id == id) |
| 577 | return &devices[i]; |
| 578 | |
| 579 | if (resize_devices(NULL) != 0) |
| 580 | return NULL; |
| 581 | |
| 582 | pdi = &devices[ndevices - 1]; |
| 583 | pdi->id = id; |
| 584 | return pdi; |
| 585 | } |
| 586 | |
| 587 | static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size) |
| 588 | { |
| 589 | if (pdi->name) |
| 590 | snprintf(buffer, size, "%s", pdi->name); |
| 591 | else |
| 592 | snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id)); |
| 593 | return buffer; |
| 594 | } |
| 595 | |
| 596 | static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit) |
| 597 | { |
| 598 | unsigned long long this = bit->time; |
| 599 | unsigned long long last = pdi->last_reported_time; |
| 600 | |
| 601 | pdi->backwards = (this < last) ? 'B' : ' '; |
| 602 | pdi->last_reported_time = this; |
| 603 | } |
| 604 | |
| 605 | static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t, |
| 606 | int rw) |
| 607 | { |
| 608 | if (rw) { |
| 609 | ios->mwrites++; |
| 610 | ios->qwrite_kb += t->bytes >> 10; |
| 611 | } else { |
| 612 | ios->mreads++; |
| 613 | ios->qread_kb += t->bytes >> 10; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 618 | int rw) |
| 619 | { |
| 620 | __account_m(&pci->io_stats, t, rw); |
| 621 | |
| 622 | if (per_process_stats) { |
| 623 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 624 | |
| 625 | __account_m(ios, t, rw); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t, |
| 630 | int rw) |
| 631 | { |
| 632 | if (rw) { |
| 633 | ios->qwrites++; |
| 634 | ios->qwrite_kb += t->bytes >> 10; |
| 635 | } else { |
| 636 | ios->qreads++; |
| 637 | ios->qread_kb += t->bytes >> 10; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 642 | int rw) |
| 643 | { |
| 644 | __account_q(&pci->io_stats, t, rw); |
| 645 | |
| 646 | if (per_process_stats) { |
| 647 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 648 | |
| 649 | __account_q(ios, t, rw); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes) |
| 654 | { |
| 655 | if (rw) { |
| 656 | ios->cwrites++; |
| 657 | ios->cwrite_kb += bytes >> 10; |
| 658 | } else { |
| 659 | ios->creads++; |
| 660 | ios->cread_kb += bytes >> 10; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 665 | int rw, int bytes) |
| 666 | { |
| 667 | __account_c(&pci->io_stats, rw, bytes); |
| 668 | |
| 669 | if (per_process_stats) { |
| 670 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 671 | |
| 672 | __account_c(ios, rw, bytes); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes) |
| 677 | { |
| 678 | if (rw) { |
| 679 | ios->iwrites++; |
| 680 | ios->iwrite_kb += bytes >> 10; |
| 681 | } else { |
| 682 | ios->ireads++; |
| 683 | ios->iread_kb += bytes >> 10; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 688 | int rw) |
| 689 | { |
| 690 | __account_i(&pci->io_stats, rw, t->bytes); |
| 691 | |
| 692 | if (per_process_stats) { |
| 693 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 694 | |
| 695 | __account_i(ios, rw, t->bytes); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | static inline void __account_unplug(struct io_stats *ios, int timer) |
| 700 | { |
| 701 | if (timer) |
| 702 | ios->timer_unplugs++; |
| 703 | else |
| 704 | ios->io_unplugs++; |
| 705 | } |
| 706 | |
| 707 | static inline void account_unplug(struct blk_io_trace *t, |
| 708 | struct per_cpu_info *pci, int timer) |
| 709 | { |
| 710 | __account_unplug(&pci->io_stats, timer); |
| 711 | |
| 712 | if (per_process_stats) { |
| 713 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 714 | |
| 715 | __account_unplug(ios, timer); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | #define VALID_SPECS "BCDFGMPQRSTU" |
| 720 | static char *override_format[256]; |
| 721 | static inline int valid_spec(int spec) |
| 722 | { |
| 723 | return strchr(VALID_SPECS, spec) != NULL; |
| 724 | } |
| 725 | |
| 726 | static void set_all_format_specs(char *optarg) |
| 727 | { |
| 728 | char *p; |
| 729 | |
| 730 | for (p = VALID_SPECS; *p; p++) |
| 731 | if (override_format[(int)(*p)] == NULL) |
| 732 | override_format[(int)(*p)] = strdup(optarg); |
| 733 | } |
| 734 | |
| 735 | static int add_format_spec(char *optarg) |
| 736 | { |
| 737 | int spec = optarg[0]; |
| 738 | |
| 739 | if (!valid_spec(spec)) { |
| 740 | fprintf(stderr,"Bad format specifier %c\n", spec); |
| 741 | return 1; |
| 742 | } |
| 743 | if (optarg[1] != ',') { |
| 744 | fprintf(stderr,"Bad format specifier - need ',' %s\n", optarg); |
| 745 | return 1; |
| 746 | } |
| 747 | optarg += 2; |
| 748 | if (*optarg == '\0') { |
| 749 | fprintf(stderr,"Bad format specifier - need fmt %s\n", optarg); |
| 750 | return 1; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Set both merges (front and back) |
| 755 | */ |
| 756 | if (spec == 'M') { |
| 757 | override_format['B'] = strdup(optarg); |
| 758 | override_format['M'] = strdup(optarg); |
| 759 | } else |
| 760 | override_format[spec] = strdup(optarg); |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static void print_field(char *act, struct per_cpu_info *pci, |
| 766 | struct blk_io_trace *t, unsigned long long elapsed, |
| 767 | int pdu_len, unsigned char *pdu_buf, char field, |
| 768 | int minus, int has_w, int width) |
| 769 | { |
| 770 | char format[64]; |
| 771 | |
| 772 | if (has_w) { |
| 773 | if (minus) |
| 774 | sprintf(format, "%%-%d", width); |
| 775 | else |
| 776 | sprintf(format, "%%%d", width); |
| 777 | } else |
| 778 | sprintf(format, "%%"); |
| 779 | |
| 780 | switch (field) { |
| 781 | case 'a': |
| 782 | fprintf(ofp, strcat(format, "s"), act); |
| 783 | break; |
| 784 | case 'c': |
| 785 | fprintf(ofp, strcat(format, "d"), pci->cpu); |
| 786 | break; |
| 787 | case 'C': |
| 788 | fprintf(ofp, strcat(format, "s"), t->comm); |
| 789 | break; |
| 790 | case 'd': { |
| 791 | char rwbs[4]; |
| 792 | int i = 0; |
| 793 | int w = t->action & BLK_TC_ACT(BLK_TC_WRITE); |
| 794 | int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER); |
| 795 | int s = t->action & BLK_TC_ACT(BLK_TC_SYNC); |
| 796 | if (w) |
| 797 | rwbs[i++] = 'W'; |
| 798 | else |
| 799 | rwbs[i++] = 'R'; |
| 800 | if (b) |
| 801 | rwbs[i++] = 'B'; |
| 802 | if (s) |
| 803 | rwbs[i++] = 'S'; |
| 804 | rwbs[i] = '\0'; |
| 805 | fprintf(ofp, strcat(format, "s"), rwbs); |
| 806 | break; |
| 807 | } |
| 808 | case 'D': /* format width ignored */ |
| 809 | fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device)); |
| 810 | break; |
| 811 | case 'e': |
| 812 | fprintf(ofp, strcat(format, "d"), t->error); |
| 813 | break; |
| 814 | case 'M': |
| 815 | fprintf(ofp, strcat(format, "d"), MAJOR(t->device)); |
| 816 | break; |
| 817 | case 'm': |
| 818 | fprintf(ofp, strcat(format, "d"), MINOR(t->device)); |
| 819 | break; |
| 820 | case 'n': |
| 821 | fprintf(ofp, strcat(format, "u"), t->bytes >> 9); |
| 822 | break; |
| 823 | case 'p': |
| 824 | fprintf(ofp, strcat(format, "u"), t->pid); |
| 825 | break; |
| 826 | case 'P': /* format width ignored */ |
| 827 | if ((pdu_len > 0) && (pdu_buf != NULL)) { |
| 828 | int i; |
| 829 | unsigned char *p = pdu_buf; |
| 830 | for (i = 0; i < pdu_len; i++) |
| 831 | fprintf(ofp, "%02x ", *p++); |
| 832 | } |
| 833 | break; |
| 834 | case 's': |
| 835 | fprintf(ofp, strcat(format, "ld"), t->sequence); |
| 836 | break; |
| 837 | case 'S': |
| 838 | fprintf(ofp, strcat(format, "lu"), t->sector); |
| 839 | break; |
| 840 | case 't': |
| 841 | sprintf(format, "%%0%dlu", has_w ? width : 9); |
| 842 | fprintf(ofp, format, NANO_SECONDS(t->time)); |
| 843 | break; |
| 844 | case 'T': |
| 845 | fprintf(ofp, strcat(format, "d"), SECONDS(t->time)); |
| 846 | break; |
| 847 | case 'u': |
| 848 | if (elapsed == -1ULL) { |
| 849 | fprintf(stderr, "Expecting elapsed value\n"); |
| 850 | exit(1); |
| 851 | } |
| 852 | fprintf(ofp, strcat(format, "llu"), elapsed / 1000); |
| 853 | break; |
| 854 | case 'U': { |
| 855 | __u64 *depth = (__u64 *) ((char *) t + sizeof(*t)); |
| 856 | fprintf(ofp, strcat(format, "u"), |
| 857 | (unsigned int) be64_to_cpu(*depth)); |
| 858 | break; |
| 859 | } |
| 860 | default: |
| 861 | fprintf(ofp,strcat(format, "c"), field); |
| 862 | break; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | static char *parse_field(char *act, struct per_cpu_info *pci, |
| 867 | struct blk_io_trace *t, unsigned long long elapsed, |
| 868 | int pdu_len, unsigned char *pdu_buf, |
| 869 | char *master_format) |
| 870 | { |
| 871 | int minus = 0; |
| 872 | int has_w = 0; |
| 873 | int width = 0; |
| 874 | char *p = master_format; |
| 875 | |
| 876 | if (*p == '-') { |
| 877 | minus = 1; |
| 878 | p++; |
| 879 | } |
| 880 | if (isdigit(*p)) { |
| 881 | has_w = 1; |
| 882 | do { |
| 883 | width = (width * 10) + (*p++ - '0'); |
| 884 | } while ((*p) && (isdigit(*p))); |
| 885 | } |
| 886 | if (*p) { |
| 887 | print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++, |
| 888 | minus, has_w, width); |
| 889 | } |
| 890 | return p; |
| 891 | } |
| 892 | |
| 893 | static char *fmt_select(int fmt_spec, struct blk_io_trace *t, |
| 894 | unsigned long long elapsed) |
| 895 | { |
| 896 | char *fmt; |
| 897 | char scratch_format[1024]; |
| 898 | |
| 899 | if (override_format[fmt_spec] != NULL) |
| 900 | return override_format[fmt_spec]; |
| 901 | |
| 902 | switch (fmt_spec) { |
| 903 | case 'C': /* Complete */ |
| 904 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) { |
| 905 | strcpy(scratch_format, HEADER); |
| 906 | strcat(scratch_format, "%P"); |
| 907 | } else { |
| 908 | strcpy(scratch_format, HEADER "%S + %n "); |
| 909 | if (elapsed != -1ULL) |
| 910 | strcat(scratch_format, "(%8u) "); |
| 911 | } |
| 912 | strcat(scratch_format, "[%e]\n"); |
| 913 | fmt = scratch_format; |
| 914 | break; |
| 915 | |
| 916 | case 'D': /* Issue */ |
| 917 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) { |
| 918 | strcpy(scratch_format, HEADER); |
| 919 | strcat(scratch_format, "%P"); |
| 920 | } else { |
| 921 | strcpy(scratch_format, HEADER "%S + %n "); |
| 922 | if (elapsed != -1ULL) |
| 923 | strcat(scratch_format, "(%8u) "); |
| 924 | } |
| 925 | strcat(scratch_format,"[%C]\n"); |
| 926 | fmt = scratch_format; |
| 927 | break; |
| 928 | |
| 929 | case 'Q': /* Queue */ |
| 930 | strcpy(scratch_format, HEADER "%S + %n "); |
| 931 | if (elapsed != -1ULL) |
| 932 | strcat(scratch_format, "(%8u) "); |
| 933 | strcat(scratch_format,"[%C]\n"); |
| 934 | fmt = scratch_format; |
| 935 | break; |
| 936 | |
| 937 | case 'B': /* Back merge */ |
| 938 | case 'F': /* Front merge */ |
| 939 | case 'M': /* Front or back merge */ |
| 940 | fmt = HEADER "%S + %n [%C]\n"; |
| 941 | break; |
| 942 | |
| 943 | case 'P': /* Plug */ |
| 944 | fmt = HEADER "[%C]\n"; |
| 945 | break; |
| 946 | |
| 947 | case 'G': /* Get request */ |
| 948 | case 'S': /* Sleep request */ |
| 949 | fmt = HEADER "%S + %n [%C]\n"; |
| 950 | break; |
| 951 | |
| 952 | case 'U': /* Unplug IO */ |
| 953 | case 'T': /* Unplug timer */ |
| 954 | fmt = HEADER "[%C] %U\n"; |
| 955 | break; |
| 956 | |
| 957 | default: |
| 958 | fprintf(stderr,"FATAL: Invalid format spec %c\n", fmt_spec); |
| 959 | exit(1); |
| 960 | /*NOTREACHED*/ |
| 961 | } |
| 962 | |
| 963 | return fmt; |
| 964 | } |
| 965 | |
| 966 | static void process_fmt(char *act, struct per_cpu_info *pci, |
| 967 | struct blk_io_trace *t, unsigned long long elapsed, |
| 968 | int pdu_len, unsigned char *pdu_buf) |
| 969 | { |
| 970 | char *p = fmt_select(act[0], t, elapsed); |
| 971 | |
| 972 | while (*p) { |
| 973 | switch (*p) { |
| 974 | case '%': /* Field specifier */ |
| 975 | p++; |
| 976 | if (*p == '%') |
| 977 | fprintf(ofp, "%c", *p++); |
| 978 | else if (!*p) |
| 979 | fprintf(ofp, "%c", '%'); |
| 980 | else |
| 981 | p = parse_field(act, pci, t, elapsed, |
| 982 | pdu_len, pdu_buf, p); |
| 983 | break; |
| 984 | case '\\': { /* escape */ |
| 985 | switch (p[1]) { |
| 986 | case 'b': fprintf(ofp, "\b"); break; |
| 987 | case 'n': fprintf(ofp, "\n"); break; |
| 988 | case 'r': fprintf(ofp, "\r"); break; |
| 989 | case 't': fprintf(ofp, "\t"); break; |
| 990 | default: |
| 991 | fprintf(stderr, |
| 992 | "Invalid escape char in format %c\n", |
| 993 | p[1]); |
| 994 | exit(1); |
| 995 | /*NOTREACHED*/ |
| 996 | } |
| 997 | p += 2; |
| 998 | break; |
| 999 | } |
| 1000 | default: |
| 1001 | fprintf(ofp, "%c", *p++); |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1008 | char *act) |
| 1009 | { |
| 1010 | process_fmt(act, pci, t, log_track_complete(t), 0, NULL); |
| 1011 | } |
| 1012 | |
| 1013 | static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1014 | char *act) |
| 1015 | { |
| 1016 | process_fmt(act, pci, t, log_track_queue(t), 0, NULL); |
| 1017 | } |
| 1018 | |
| 1019 | static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1020 | char *act) |
| 1021 | { |
| 1022 | process_fmt(act, pci, t, log_track_issue(t), 0, NULL); |
| 1023 | } |
| 1024 | |
| 1025 | static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1026 | char *act) |
| 1027 | { |
| 1028 | if (act[0] == 'F') |
| 1029 | log_track_frontmerge(t); |
| 1030 | |
| 1031 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1032 | } |
| 1033 | |
| 1034 | static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1035 | char *act) |
| 1036 | { |
| 1037 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1038 | } |
| 1039 | |
| 1040 | static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1041 | char *act) |
| 1042 | { |
| 1043 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1044 | } |
| 1045 | |
| 1046 | static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1047 | char *act) |
| 1048 | { |
| 1049 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1050 | } |
| 1051 | |
| 1052 | static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) |
| 1053 | { |
| 1054 | unsigned char *buf = (unsigned char *) t + sizeof(*t); |
| 1055 | |
| 1056 | process_fmt(act, pci, t, -1ULL, t->pdu_len, buf); |
| 1057 | } |
| 1058 | |
| 1059 | static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci) |
| 1060 | { |
| 1061 | int ret = 0; |
| 1062 | |
| 1063 | switch (t->action & 0xffff) { |
| 1064 | case __BLK_TA_QUEUE: |
| 1065 | log_generic(pci, t, "Q"); |
| 1066 | break; |
| 1067 | case __BLK_TA_GETRQ: |
| 1068 | log_generic(pci, t, "G"); |
| 1069 | break; |
| 1070 | case __BLK_TA_SLEEPRQ: |
| 1071 | log_generic(pci, t, "S"); |
| 1072 | break; |
| 1073 | case __BLK_TA_REQUEUE: |
| 1074 | log_generic(pci, t, "R"); |
| 1075 | break; |
| 1076 | case __BLK_TA_ISSUE: |
| 1077 | log_pc(pci, t, "D"); |
| 1078 | break; |
| 1079 | case __BLK_TA_COMPLETE: |
| 1080 | log_pc(pci, t, "C"); |
| 1081 | break; |
| 1082 | default: |
| 1083 | fprintf(stderr, "Bad pc action %x\n", t->action); |
| 1084 | ret = 1; |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci) |
| 1092 | { |
| 1093 | int w = t->action & BLK_TC_ACT(BLK_TC_WRITE); |
| 1094 | int act = t->action & 0xffff; |
| 1095 | |
| 1096 | switch (act) { |
| 1097 | case __BLK_TA_QUEUE: |
| 1098 | account_q(t, pci, w); |
| 1099 | log_queue(pci, t, "Q"); |
| 1100 | break; |
| 1101 | case __BLK_TA_BACKMERGE: |
| 1102 | account_m(t, pci, w); |
| 1103 | log_merge(pci, t, "M"); |
| 1104 | break; |
| 1105 | case __BLK_TA_FRONTMERGE: |
| 1106 | account_m(t, pci, w); |
| 1107 | log_merge(pci, t, "F"); |
| 1108 | break; |
| 1109 | case __BLK_TA_GETRQ: |
| 1110 | log_track_getrq(t); |
| 1111 | log_generic(pci, t, "G"); |
| 1112 | break; |
| 1113 | case __BLK_TA_SLEEPRQ: |
| 1114 | log_generic(pci, t, "S"); |
| 1115 | break; |
| 1116 | case __BLK_TA_REQUEUE: |
| 1117 | account_c(t, pci, w, -t->bytes); |
| 1118 | log_queue(pci, t, "R"); |
| 1119 | break; |
| 1120 | case __BLK_TA_ISSUE: |
| 1121 | account_i(t, pci, w); |
| 1122 | log_issue(pci, t, "D"); |
| 1123 | break; |
| 1124 | case __BLK_TA_COMPLETE: |
| 1125 | account_c(t, pci, w, t->bytes); |
| 1126 | log_complete(pci, t, "C"); |
| 1127 | break; |
| 1128 | case __BLK_TA_PLUG: |
| 1129 | log_action(pci, t, "P"); |
| 1130 | break; |
| 1131 | case __BLK_TA_UNPLUG_IO: |
| 1132 | account_unplug(t, pci, 0); |
| 1133 | log_unplug(pci, t, "U"); |
| 1134 | break; |
| 1135 | case __BLK_TA_UNPLUG_TIMER: |
| 1136 | account_unplug(t, pci, 1); |
| 1137 | log_unplug(pci, t, "UT"); |
| 1138 | break; |
| 1139 | default: |
| 1140 | fprintf(stderr, "Bad fs action %x\n", t->action); |
| 1141 | break; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 1146 | struct per_dev_info *pdi) |
| 1147 | { |
| 1148 | int ret = 0; |
| 1149 | |
| 1150 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) |
| 1151 | ret = dump_trace_pc(t, pci); |
| 1152 | else |
| 1153 | dump_trace_fs(t, pci); |
| 1154 | |
| 1155 | pdi->events++; |
| 1156 | return ret; |
| 1157 | } |
| 1158 | |
| 1159 | static void dump_io_stats(struct io_stats *ios, char *msg) |
| 1160 | { |
| 1161 | fprintf(ofp, "%s\n", msg); |
| 1162 | |
| 1163 | fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb); |
| 1164 | fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb); |
| 1165 | |
| 1166 | fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb); |
| 1167 | fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb); |
| 1168 | fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb); |
| 1169 | fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb); |
| 1170 | fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' '); |
| 1171 | fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites); |
| 1172 | fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' '); |
| 1173 | fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs); |
| 1174 | } |
| 1175 | |
| 1176 | static void dump_wait_stats(struct per_process_info *ppi) |
| 1177 | { |
| 1178 | unsigned long rawait = ppi->longest_allocation_wait[0] / 1000; |
| 1179 | unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000; |
| 1180 | unsigned long rcwait = ppi->longest_completion_wait[0] / 1000; |
| 1181 | unsigned long wawait = ppi->longest_allocation_wait[1] / 1000; |
| 1182 | unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000; |
| 1183 | unsigned long wcwait = ppi->longest_completion_wait[1] / 1000; |
| 1184 | |
| 1185 | fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' '); |
| 1186 | fprintf(ofp, " Allocation wait: %'8lu\n", wawait); |
| 1187 | fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' '); |
| 1188 | fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait); |
| 1189 | fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' '); |
| 1190 | fprintf(ofp, " Completion wait: %'8lu\n", wcwait); |
| 1191 | } |
| 1192 | |
| 1193 | static int ppi_name_compare(const void *p1, const void *p2) |
| 1194 | { |
| 1195 | struct per_process_info *ppi1 = *((struct per_process_info **) p1); |
| 1196 | struct per_process_info *ppi2 = *((struct per_process_info **) p2); |
| 1197 | int res; |
| 1198 | |
| 1199 | res = strverscmp(ppi1->name, ppi2->name); |
| 1200 | if (!res) |
| 1201 | res = ppi1->pid > ppi2->pid; |
| 1202 | |
| 1203 | return res; |
| 1204 | } |
| 1205 | |
| 1206 | static void sort_process_list(void) |
| 1207 | { |
| 1208 | struct per_process_info **ppis; |
| 1209 | struct per_process_info *ppi; |
| 1210 | int i = 0; |
| 1211 | |
| 1212 | ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *)); |
| 1213 | |
| 1214 | ppi = ppi_list; |
| 1215 | while (ppi) { |
| 1216 | ppis[i++] = ppi; |
| 1217 | ppi = ppi->list_next; |
| 1218 | } |
| 1219 | |
| 1220 | qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare); |
| 1221 | |
| 1222 | i = ppi_list_entries - 1; |
| 1223 | ppi_list = NULL; |
| 1224 | while (i >= 0) { |
| 1225 | ppi = ppis[i]; |
| 1226 | |
| 1227 | ppi->list_next = ppi_list; |
| 1228 | ppi_list = ppi; |
| 1229 | i--; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | static void show_process_stats(void) |
| 1234 | { |
| 1235 | struct per_process_info *ppi; |
| 1236 | |
| 1237 | sort_process_list(); |
| 1238 | |
| 1239 | ppi = ppi_list; |
| 1240 | while (ppi) { |
| 1241 | char name[64]; |
| 1242 | |
| 1243 | snprintf(name, sizeof(name)-1, "%s (%u)", ppi->name, ppi->pid); |
| 1244 | dump_io_stats(&ppi->io_stats, name); |
| 1245 | dump_wait_stats(ppi); |
| 1246 | ppi = ppi->list_next; |
| 1247 | } |
| 1248 | |
| 1249 | fprintf(ofp, "\n"); |
| 1250 | } |
| 1251 | |
| 1252 | static void show_device_and_cpu_stats(void) |
| 1253 | { |
| 1254 | struct per_dev_info *pdi; |
| 1255 | struct per_cpu_info *pci; |
| 1256 | struct io_stats total, *ios; |
| 1257 | int i, j, pci_events; |
| 1258 | char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3]; |
| 1259 | char name[32]; |
| 1260 | |
| 1261 | for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { |
| 1262 | |
| 1263 | memset(&total, 0, sizeof(total)); |
| 1264 | pci_events = 0; |
| 1265 | |
| 1266 | if (i > 0) |
| 1267 | fprintf(ofp, "\n"); |
| 1268 | |
| 1269 | for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) { |
| 1270 | if (!pci->nelems) |
| 1271 | continue; |
| 1272 | |
| 1273 | ios = &pci->io_stats; |
| 1274 | total.qreads += ios->qreads; |
| 1275 | total.qwrites += ios->qwrites; |
| 1276 | total.creads += ios->creads; |
| 1277 | total.cwrites += ios->cwrites; |
| 1278 | total.mreads += ios->mreads; |
| 1279 | total.mwrites += ios->mwrites; |
| 1280 | total.ireads += ios->ireads; |
| 1281 | total.iwrites += ios->iwrites; |
| 1282 | total.qread_kb += ios->qread_kb; |
| 1283 | total.qwrite_kb += ios->qwrite_kb; |
| 1284 | total.cread_kb += ios->cread_kb; |
| 1285 | total.cwrite_kb += ios->cwrite_kb; |
| 1286 | total.iread_kb += ios->iread_kb; |
| 1287 | total.iwrite_kb += ios->iwrite_kb; |
| 1288 | total.timer_unplugs += ios->timer_unplugs; |
| 1289 | total.io_unplugs += ios->io_unplugs; |
| 1290 | |
| 1291 | snprintf(line, sizeof(line) - 1, "CPU%d (%s):", |
| 1292 | j, get_dev_name(pdi, name, sizeof(name))); |
| 1293 | dump_io_stats(ios, line); |
| 1294 | pci_events++; |
| 1295 | } |
| 1296 | |
| 1297 | if (pci_events > 1) { |
| 1298 | fprintf(ofp, "\n"); |
| 1299 | snprintf(line, sizeof(line) - 1, "Total (%s):", |
| 1300 | get_dev_name(pdi, name, sizeof(name))); |
| 1301 | dump_io_stats(&total, line); |
| 1302 | } |
| 1303 | |
| 1304 | fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n", |
| 1305 | get_dev_name(pdi, line, sizeof(line)), pdi->events, |
| 1306 | pdi->skips); |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | static struct blk_io_trace *find_trace(void *p, unsigned long offset) |
| 1311 | { |
| 1312 | unsigned long max_offset = offset; |
| 1313 | unsigned long off; |
| 1314 | struct blk_io_trace *bit; |
| 1315 | __u32 magic; |
| 1316 | |
| 1317 | for (off = 0; off < max_offset; off++) { |
| 1318 | bit = p + off; |
| 1319 | |
| 1320 | magic = be32_to_cpu(bit->magic); |
| 1321 | if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) |
| 1322 | return bit; |
| 1323 | } |
| 1324 | |
| 1325 | return NULL; |
| 1326 | } |
| 1327 | |
| 1328 | static int sort_entries(void) |
| 1329 | { |
| 1330 | struct per_dev_info *pdi; |
| 1331 | struct per_cpu_info *pci; |
| 1332 | struct blk_io_trace *bit; |
| 1333 | struct trace *t; |
| 1334 | int nr = 0; |
| 1335 | |
| 1336 | while ((t = trace_list) != NULL) { |
| 1337 | |
| 1338 | trace_list = t->next; |
| 1339 | bit = t->bit; |
| 1340 | |
| 1341 | memset(&t->rb_node, 0, sizeof(t->rb_node)); |
| 1342 | |
| 1343 | if (verify_trace(bit)) |
| 1344 | break; |
| 1345 | if (trace_rb_insert(t)) |
| 1346 | return -1; |
| 1347 | |
| 1348 | pdi = get_dev_info(bit->device); |
| 1349 | pci = get_cpu_info(pdi, bit->cpu); |
| 1350 | pci->nelems++; |
| 1351 | |
| 1352 | nr++; |
| 1353 | } |
| 1354 | |
| 1355 | return nr; |
| 1356 | } |
| 1357 | |
| 1358 | static void show_entries_rb(int piped) |
| 1359 | { |
| 1360 | struct per_dev_info *pdi; |
| 1361 | struct blk_io_trace *bit; |
| 1362 | struct rb_node *n; |
| 1363 | struct trace *t; |
| 1364 | int cpu; |
| 1365 | |
| 1366 | while ((n = rb_first(&rb_sort_root)) != NULL) { |
| 1367 | |
| 1368 | t = rb_entry(n, struct trace, rb_node); |
| 1369 | bit = t->bit; |
| 1370 | |
| 1371 | pdi = get_dev_info(bit->device); |
| 1372 | if (!pdi) { |
| 1373 | fprintf(stderr, "Unknown device ID? (%d,%d)\n", |
| 1374 | MAJOR(bit->device), MINOR(bit->device)); |
| 1375 | break; |
| 1376 | } |
| 1377 | cpu = bit->cpu; |
| 1378 | if (cpu > pdi->ncpus) { |
| 1379 | fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n", |
| 1380 | cpu, MAJOR(bit->device), MINOR(bit->device)); |
| 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * back off displaying more info if we are out of sync |
| 1386 | * on SMP systems. to prevent stalling on lost events, |
| 1387 | * only allow an event to us a few times |
| 1388 | */ |
| 1389 | if (bit->sequence != (pdi->last_sequence + 1)) { |
| 1390 | if (piped && t->skipped < 5) { |
| 1391 | t->skipped++; |
| 1392 | break; |
| 1393 | } else { |
| 1394 | fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence); |
| 1395 | pdi->skips++; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | pdi->last_sequence = bit->sequence; |
| 1400 | |
| 1401 | bit->time -= genesis_time; |
| 1402 | if (bit->time < stopwatch_start) |
| 1403 | continue; |
| 1404 | if (bit->time >= stopwatch_end) |
| 1405 | break; |
| 1406 | |
| 1407 | check_time(pdi, bit); |
| 1408 | |
| 1409 | if (dump_trace(bit, &pdi->cpus[cpu], pdi)) |
| 1410 | break; |
| 1411 | |
| 1412 | rb_erase(&t->rb_node, &rb_sort_root); |
| 1413 | |
| 1414 | if (piped) { |
| 1415 | free(bit); |
| 1416 | free(t); |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | static int read_data(int fd, void *buffer, int bytes, int block) |
| 1422 | { |
| 1423 | int ret, bytes_left, fl; |
| 1424 | void *p; |
| 1425 | |
| 1426 | fl = fcntl(fd, F_GETFL); |
| 1427 | |
| 1428 | if (!block) |
| 1429 | fcntl(fd, F_SETFL, fl | O_NONBLOCK); |
| 1430 | else |
| 1431 | fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); |
| 1432 | |
| 1433 | bytes_left = bytes; |
| 1434 | p = buffer; |
| 1435 | while (bytes_left > 0) { |
| 1436 | ret = read(fd, p, bytes_left); |
| 1437 | if (!ret) |
| 1438 | return 1; |
| 1439 | else if (ret < 0) { |
| 1440 | if (errno != EAGAIN) |
| 1441 | perror("read"); |
| 1442 | return -1; |
| 1443 | } else { |
| 1444 | p += ret; |
| 1445 | bytes_left -= ret; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * Find the traces in 'tb' and add them to the list for sorting and |
| 1454 | * displaying |
| 1455 | */ |
| 1456 | static int find_entries(void *tb, unsigned long size) |
| 1457 | { |
| 1458 | struct blk_io_trace *bit; |
| 1459 | struct trace *t; |
| 1460 | void *start = tb; |
| 1461 | |
| 1462 | while (tb - start <= size - sizeof(*bit)) { |
| 1463 | bit = find_trace(tb, size - (tb - start)); |
| 1464 | if (!bit) |
| 1465 | break; |
| 1466 | |
| 1467 | t = malloc(sizeof(*t)); |
| 1468 | memset(t, 0, sizeof(*t)); |
| 1469 | t->bit = bit; |
| 1470 | |
| 1471 | trace_to_cpu(bit); |
| 1472 | t->next = trace_list; |
| 1473 | trace_list = t; |
| 1474 | |
| 1475 | tb += sizeof(*bit) + bit->pdu_len; |
| 1476 | } |
| 1477 | |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | static int do_file(void) |
| 1482 | { |
| 1483 | int i, j, nfiles = 0, nelems; |
| 1484 | |
| 1485 | for (i = 0; i < ndevices; i++) { |
| 1486 | for (j = 0;; j++, nfiles++) { |
| 1487 | struct per_dev_info *pdi; |
| 1488 | struct per_cpu_info *pci; |
| 1489 | struct stat st; |
| 1490 | void *tb; |
| 1491 | |
| 1492 | pdi = &devices[i]; |
| 1493 | pci = get_cpu_info(pdi, j); |
| 1494 | pci->cpu = j; |
| 1495 | |
| 1496 | snprintf(pci->fname, sizeof(pci->fname)-1, |
| 1497 | "%s.%d", pdi->name, j); |
| 1498 | if (stat(pci->fname, &st) < 0) |
| 1499 | break; |
| 1500 | if (!st.st_size) |
| 1501 | continue; |
| 1502 | |
| 1503 | printf("Processing %s\n", pci->fname); |
| 1504 | |
| 1505 | tb = malloc(st.st_size); |
| 1506 | if (!tb) { |
| 1507 | fprintf(stderr, "Out of memory, skip file %s\n", |
| 1508 | pci->fname); |
| 1509 | continue; |
| 1510 | } |
| 1511 | |
| 1512 | pci->fd = open(pci->fname, O_RDONLY); |
| 1513 | if (pci->fd < 0) { |
| 1514 | perror(pci->fname); |
| 1515 | free(tb); |
| 1516 | continue; |
| 1517 | } |
| 1518 | |
| 1519 | if (read_data(pci->fd, tb, st.st_size, 1)) { |
| 1520 | close(pci->fd); |
| 1521 | free(tb); |
| 1522 | continue; |
| 1523 | } |
| 1524 | |
| 1525 | if (find_entries(tb, st.st_size)) { |
| 1526 | close(pci->fd); |
| 1527 | free(tb); |
| 1528 | } |
| 1529 | |
| 1530 | nelems = sort_entries(); |
| 1531 | if (nelems == -1) { |
| 1532 | close(pci->fd); |
| 1533 | free(tb); |
| 1534 | continue; |
| 1535 | } |
| 1536 | |
| 1537 | printf("Completed %s (CPU%d %d, entries)\n", |
| 1538 | pci->fname, j, nelems); |
| 1539 | close(pci->fd); |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | if (!nfiles) { |
| 1544 | fprintf(stderr, "No files found\n"); |
| 1545 | return 1; |
| 1546 | } |
| 1547 | |
| 1548 | show_entries_rb(0); |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | static int read_sort_events(int fd) |
| 1553 | { |
| 1554 | int events = 0; |
| 1555 | |
| 1556 | do { |
| 1557 | struct blk_io_trace *bit; |
| 1558 | struct trace *t; |
| 1559 | int pdu_len; |
| 1560 | __u32 magic; |
| 1561 | |
| 1562 | bit = malloc(sizeof(*bit)); |
| 1563 | |
| 1564 | if (read_data(fd, bit, sizeof(*bit), !events)) |
| 1565 | break; |
| 1566 | |
| 1567 | magic = be32_to_cpu(bit->magic); |
| 1568 | if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
| 1569 | fprintf(stderr, "Bad magic %x\n", magic); |
| 1570 | break; |
| 1571 | } |
| 1572 | |
| 1573 | pdu_len = be16_to_cpu(bit->pdu_len); |
| 1574 | if (pdu_len) { |
| 1575 | void *ptr = realloc(bit, sizeof(*bit) + pdu_len); |
| 1576 | |
| 1577 | if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1)) |
| 1578 | break; |
| 1579 | |
| 1580 | bit = ptr; |
| 1581 | } |
| 1582 | |
| 1583 | t = malloc(sizeof(*t)); |
| 1584 | memset(t, 0, sizeof(*t)); |
| 1585 | t->bit = bit; |
| 1586 | |
| 1587 | trace_to_cpu(bit); |
| 1588 | t->next = trace_list; |
| 1589 | trace_list = t; |
| 1590 | |
| 1591 | events++; |
| 1592 | } while (!is_done() && events < rb_batch); |
| 1593 | |
| 1594 | return events; |
| 1595 | } |
| 1596 | |
| 1597 | static int do_stdin(void) |
| 1598 | { |
| 1599 | int fd; |
| 1600 | |
| 1601 | fd = dup(STDIN_FILENO); |
| 1602 | do { |
| 1603 | int events; |
| 1604 | |
| 1605 | events = read_sort_events(fd); |
| 1606 | if (!events) |
| 1607 | break; |
| 1608 | |
| 1609 | if (sort_entries() == -1) |
| 1610 | break; |
| 1611 | |
| 1612 | show_entries_rb(1); |
| 1613 | } while (1); |
| 1614 | |
| 1615 | close(fd); |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | static void flush_output(void) |
| 1620 | { |
| 1621 | fflush(ofp); |
| 1622 | } |
| 1623 | |
| 1624 | static void handle_sigint(int sig) |
| 1625 | { |
| 1626 | done = 1; |
| 1627 | flush_output(); |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * Extract start and duration times from a string, allowing |
| 1632 | * us to specify a time interval of interest within a trace. |
| 1633 | * Format: "duration" (start is zero) or "start:duration". |
| 1634 | */ |
| 1635 | static int find_stopwatch_interval(char *string) |
| 1636 | { |
| 1637 | double value; |
| 1638 | char *sp; |
| 1639 | |
| 1640 | value = strtod(string, &sp); |
| 1641 | if (sp == string) { |
| 1642 | fprintf(stderr,"Invalid stopwatch timer: %s\n", string); |
| 1643 | return 1; |
| 1644 | } |
| 1645 | if (*sp == ':') { |
| 1646 | stopwatch_start = DOUBLE_TO_NANO_ULL(value); |
| 1647 | string = sp + 1; |
| 1648 | value = strtod(string, &sp); |
| 1649 | if (sp == string || *sp != '\0') { |
| 1650 | fprintf(stderr,"Invalid stopwatch duration time: %s\n", |
| 1651 | string); |
| 1652 | return 1; |
| 1653 | } |
| 1654 | } else if (*sp != '\0') { |
| 1655 | fprintf(stderr,"Invalid stopwatch start timer: %s\n", string); |
| 1656 | return 1; |
| 1657 | } |
| 1658 | stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value); |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | static void usage(char *prog) |
| 1663 | { |
| 1664 | fprintf(stderr, "Usage: %s " |
| 1665 | "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n", |
| 1666 | prog); |
| 1667 | } |
| 1668 | |
| 1669 | int main(int argc, char *argv[]) |
| 1670 | { |
| 1671 | char *ofp_buffer; |
| 1672 | int c, ret, mode; |
| 1673 | int per_device_and_cpu_stats = 1; |
| 1674 | |
| 1675 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) { |
| 1676 | switch (c) { |
| 1677 | case 'i': |
| 1678 | if (!strcmp(optarg, "-") && !pipeline) |
| 1679 | pipeline = 1; |
| 1680 | else if (resize_devices(optarg) != 0) |
| 1681 | return 1; |
| 1682 | break; |
| 1683 | case 'o': |
| 1684 | output_name = optarg; |
| 1685 | break; |
| 1686 | case 'b': |
| 1687 | rb_batch = atoi(optarg); |
| 1688 | if (rb_batch <= 0) |
| 1689 | rb_batch = RB_BATCH_DEFAULT; |
| 1690 | break; |
| 1691 | case 's': |
| 1692 | per_process_stats = 1; |
| 1693 | break; |
| 1694 | case 't': |
| 1695 | track_ios = 1; |
| 1696 | break; |
| 1697 | case 'q': |
| 1698 | per_device_and_cpu_stats = 0; |
| 1699 | break; |
| 1700 | case 'w': |
| 1701 | if (find_stopwatch_interval(optarg) != 0) |
| 1702 | return 1; |
| 1703 | break; |
| 1704 | case 'f': |
| 1705 | set_all_format_specs(optarg); |
| 1706 | break; |
| 1707 | case 'F': |
| 1708 | if (add_format_spec(optarg) != 0) |
| 1709 | return 1; |
| 1710 | break; |
| 1711 | default: |
| 1712 | usage(argv[0]); |
| 1713 | return 1; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | while (optind < argc) { |
| 1718 | if (!strcmp(argv[optind], "-") && !pipeline) |
| 1719 | pipeline = 1; |
| 1720 | else if (resize_devices(argv[optind]) != 0) |
| 1721 | return 1; |
| 1722 | optind++; |
| 1723 | } |
| 1724 | |
| 1725 | if (!pipeline && !ndevices) { |
| 1726 | usage(argv[0]); |
| 1727 | return 1; |
| 1728 | } |
| 1729 | |
| 1730 | memset(&rb_sort_root, 0, sizeof(rb_sort_root)); |
| 1731 | memset(&rb_track_root, 0, sizeof(rb_track_root)); |
| 1732 | |
| 1733 | signal(SIGINT, handle_sigint); |
| 1734 | signal(SIGHUP, handle_sigint); |
| 1735 | signal(SIGTERM, handle_sigint); |
| 1736 | |
| 1737 | setlocale(LC_NUMERIC, "en_US"); |
| 1738 | |
| 1739 | if (!output_name) { |
| 1740 | ofp = fdopen(STDOUT_FILENO, "w"); |
| 1741 | mode = _IOLBF; |
| 1742 | } else { |
| 1743 | char ofname[128]; |
| 1744 | |
| 1745 | snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name); |
| 1746 | ofp = fopen(ofname, "w"); |
| 1747 | mode = _IOFBF; |
| 1748 | } |
| 1749 | |
| 1750 | if (!ofp) { |
| 1751 | perror("fopen"); |
| 1752 | return 1; |
| 1753 | } |
| 1754 | |
| 1755 | ofp_buffer = malloc(4096); |
| 1756 | if (setvbuf(ofp, ofp_buffer, mode, 4096)) { |
| 1757 | perror("setvbuf"); |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
| 1761 | if (pipeline) |
| 1762 | ret = do_stdin(); |
| 1763 | else |
| 1764 | ret = do_file(); |
| 1765 | |
| 1766 | if (per_process_stats) |
| 1767 | show_process_stats(); |
| 1768 | |
| 1769 | if (per_device_and_cpu_stats) |
| 1770 | show_device_and_cpu_stats(); |
| 1771 | |
| 1772 | flush_output(); |
| 1773 | return ret; |
| 1774 | } |