| 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 | |
| 34 | #include "blktrace.h" |
| 35 | #include "rbtree.h" |
| 36 | |
| 37 | #define SECONDS(x) ((unsigned long long)(x) / 1000000000) |
| 38 | #define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000) |
| 39 | #define DOUBLE_TO_NANO_ULL(d) ((unsigned long long)((d) * 1000000000)) |
| 40 | |
| 41 | #define MINORBITS 20 |
| 42 | #define MINORMASK ((1U << MINORBITS) - 1) |
| 43 | #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS)) |
| 44 | #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK)) |
| 45 | |
| 46 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 47 | |
| 48 | struct io_stats { |
| 49 | unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites; |
| 50 | unsigned long ireads, iwrites; |
| 51 | unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb; |
| 52 | unsigned long long iread_kb, iwrite_kb; |
| 53 | }; |
| 54 | |
| 55 | struct per_cpu_info { |
| 56 | int cpu; |
| 57 | int nelems; |
| 58 | |
| 59 | int fd; |
| 60 | char fname[128]; |
| 61 | |
| 62 | struct io_stats io_stats; |
| 63 | }; |
| 64 | |
| 65 | struct per_dev_info { |
| 66 | dev_t id; |
| 67 | char *name; |
| 68 | |
| 69 | int backwards; |
| 70 | unsigned long long events; |
| 71 | unsigned long long last_reported_time; |
| 72 | struct io_stats io_stats; |
| 73 | |
| 74 | int ncpus; |
| 75 | struct per_cpu_info *cpus; |
| 76 | }; |
| 77 | |
| 78 | struct per_process_info { |
| 79 | char name[16]; |
| 80 | __u32 pid; |
| 81 | struct io_stats io_stats; |
| 82 | struct per_process_info *hash_next, *list_next; |
| 83 | |
| 84 | /* |
| 85 | * individual io stats |
| 86 | */ |
| 87 | unsigned long long longest_allocation_wait[2]; |
| 88 | unsigned long long longest_dispatch_wait[2]; |
| 89 | unsigned long long longest_completion_wait[2]; |
| 90 | }; |
| 91 | |
| 92 | #define PPI_HASH_SHIFT (8) |
| 93 | static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT]; |
| 94 | static struct per_process_info *ppi_list; |
| 95 | |
| 96 | #define S_OPTS "i:o:b:stqw:" |
| 97 | static struct option l_opts[] = { |
| 98 | { |
| 99 | .name = "input", |
| 100 | .has_arg = 1, |
| 101 | .flag = NULL, |
| 102 | .val = 'i' |
| 103 | }, |
| 104 | { |
| 105 | .name = "output", |
| 106 | .has_arg = 1, |
| 107 | .flag = NULL, |
| 108 | .val = 'o' |
| 109 | }, |
| 110 | { |
| 111 | .name = "batch", |
| 112 | .has_arg = 1, |
| 113 | .flag = NULL, |
| 114 | .val = 'b' |
| 115 | }, |
| 116 | { |
| 117 | .name = "per program stats", |
| 118 | .has_arg = 0, |
| 119 | .flag = NULL, |
| 120 | .val = 's' |
| 121 | }, |
| 122 | { |
| 123 | .name = "track ios", |
| 124 | .has_arg = 0, |
| 125 | .flag = NULL, |
| 126 | .val = 't' |
| 127 | }, |
| 128 | { |
| 129 | .name = "quiet", |
| 130 | .has_arg = 0, |
| 131 | .flag = NULL, |
| 132 | .val = 'q' |
| 133 | }, |
| 134 | { |
| 135 | .name = "stopwatch", |
| 136 | .has_arg = 1, |
| 137 | .flag = NULL, |
| 138 | .val = 'w' |
| 139 | }, |
| 140 | { |
| 141 | .name = NULL, |
| 142 | .has_arg = 0, |
| 143 | .flag = NULL, |
| 144 | .val = 0 |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | static struct rb_root rb_sort_root; |
| 149 | static struct rb_root rb_track_root; |
| 150 | |
| 151 | /* |
| 152 | * for sorting the displayed output |
| 153 | */ |
| 154 | struct trace { |
| 155 | struct blk_io_trace *bit; |
| 156 | struct rb_node rb_node; |
| 157 | }; |
| 158 | |
| 159 | /* |
| 160 | * for tracking individual ios |
| 161 | */ |
| 162 | struct io_track { |
| 163 | struct rb_node rb_node; |
| 164 | |
| 165 | dev_t device; |
| 166 | __u64 sector; |
| 167 | __u32 pid; |
| 168 | unsigned long long allocation_time; |
| 169 | unsigned long long queue_time; |
| 170 | unsigned long long dispatch_time; |
| 171 | unsigned long long completion_time; |
| 172 | }; |
| 173 | |
| 174 | static int ndevices; |
| 175 | static struct per_dev_info *devices; |
| 176 | static char *get_dev_name(struct per_dev_info *, char *, int); |
| 177 | |
| 178 | static FILE *ofp; |
| 179 | static char *output_name; |
| 180 | |
| 181 | static unsigned long long genesis_time; |
| 182 | static unsigned long long stopwatch_start; /* start from zero by default */ |
| 183 | static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */ |
| 184 | |
| 185 | static int per_process_stats; |
| 186 | static int track_ios; |
| 187 | |
| 188 | #define RB_BATCH_DEFAULT (1024) |
| 189 | static int rb_batch = RB_BATCH_DEFAULT; |
| 190 | |
| 191 | static int pipeline; |
| 192 | |
| 193 | #define is_done() (*(volatile int *)(&done)) |
| 194 | static volatile int done; |
| 195 | |
| 196 | static inline unsigned long hash_long(unsigned long val) |
| 197 | { |
| 198 | #if __WORDSIZE == 32 |
| 199 | val *= 0x9e370001UL; |
| 200 | #elif __WORDSIZE == 64 |
| 201 | val *= 0x9e37fffffffc0001UL; |
| 202 | #else |
| 203 | #error unknown word size |
| 204 | #endif |
| 205 | |
| 206 | return val >> (__WORDSIZE - PPI_HASH_SHIFT); |
| 207 | } |
| 208 | |
| 209 | static inline void add_process_to_hash(struct per_process_info *ppi) |
| 210 | { |
| 211 | const int hash_idx = hash_long(ppi->pid); |
| 212 | |
| 213 | ppi->hash_next = ppi_hash[hash_idx]; |
| 214 | ppi_hash[hash_idx] = ppi; |
| 215 | } |
| 216 | |
| 217 | static inline void add_process_to_list(struct per_process_info *ppi) |
| 218 | { |
| 219 | ppi->list_next = ppi_list; |
| 220 | ppi_list = ppi; |
| 221 | } |
| 222 | |
| 223 | static struct per_process_info *find_process_by_pid(__u32 pid) |
| 224 | { |
| 225 | const int hash_idx = hash_long(pid); |
| 226 | struct per_process_info *ppi; |
| 227 | |
| 228 | ppi = ppi_hash[hash_idx]; |
| 229 | while (ppi) { |
| 230 | if (ppi->pid == pid) |
| 231 | return ppi; |
| 232 | |
| 233 | ppi = ppi->hash_next; |
| 234 | } |
| 235 | |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | static inline int trace_rb_insert(struct trace *t) |
| 240 | { |
| 241 | struct rb_node **p = &rb_sort_root.rb_node; |
| 242 | struct rb_node *parent = NULL; |
| 243 | struct trace *__t; |
| 244 | |
| 245 | if (genesis_time == 0 || t->bit->time < genesis_time) |
| 246 | genesis_time = t->bit->time; |
| 247 | |
| 248 | while (*p) { |
| 249 | parent = *p; |
| 250 | __t = rb_entry(parent, struct trace, rb_node); |
| 251 | |
| 252 | if (t->bit->time < __t->bit->time) |
| 253 | p = &(*p)->rb_left; |
| 254 | else if (t->bit->time > __t->bit->time) |
| 255 | p = &(*p)->rb_right; |
| 256 | else if (t->bit->device < __t->bit->device) |
| 257 | p = &(*p)->rb_left; |
| 258 | else if (t->bit->device > __t->bit->device) |
| 259 | p = &(*p)->rb_right; |
| 260 | else if (t->bit->sequence < __t->bit->sequence) |
| 261 | p = &(*p)->rb_left; |
| 262 | else if (t->bit->sequence > __t->bit->sequence) |
| 263 | p = &(*p)->rb_right; |
| 264 | else if (t->bit->device == __t->bit->device) { |
| 265 | fprintf(stderr, |
| 266 | "sequence alias (%d) on device %d,%d!\n", |
| 267 | t->bit->sequence, |
| 268 | MAJOR(t->bit->device), MINOR(t->bit->device)); |
| 269 | return 1; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | rb_link_node(&t->rb_node, parent, p); |
| 274 | rb_insert_color(&t->rb_node, &rb_sort_root); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static inline int track_rb_insert(struct io_track *iot) |
| 279 | { |
| 280 | struct rb_node **p = &rb_track_root.rb_node; |
| 281 | struct rb_node *parent = NULL; |
| 282 | struct io_track *__iot; |
| 283 | |
| 284 | while (*p) { |
| 285 | parent = *p; |
| 286 | |
| 287 | __iot = rb_entry(parent, struct io_track, rb_node); |
| 288 | |
| 289 | if (iot->device < __iot->device) |
| 290 | p = &(*p)->rb_left; |
| 291 | else if (iot->device > __iot->device) |
| 292 | p = &(*p)->rb_right; |
| 293 | else if (iot->sector < __iot->sector) |
| 294 | p = &(*p)->rb_left; |
| 295 | else if (iot->sector > __iot->sector) |
| 296 | p = &(*p)->rb_right; |
| 297 | else { |
| 298 | fprintf(stderr, |
| 299 | "sector alias (%llu) on device %d,%d!\n", |
| 300 | iot->sector, |
| 301 | MAJOR(iot->device), MINOR(iot->device)); |
| 302 | return 1; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | rb_link_node(&iot->rb_node, parent, p); |
| 307 | rb_insert_color(&iot->rb_node, &rb_track_root); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static struct io_track *__find_track(dev_t device, __u64 sector) |
| 312 | { |
| 313 | struct rb_node **p = &rb_track_root.rb_node; |
| 314 | struct rb_node *parent = NULL; |
| 315 | struct io_track *__iot; |
| 316 | |
| 317 | while (*p) { |
| 318 | parent = *p; |
| 319 | |
| 320 | __iot = rb_entry(parent, struct io_track, rb_node); |
| 321 | |
| 322 | if (device < __iot->device) |
| 323 | p = &(*p)->rb_left; |
| 324 | else if (device > __iot->device) |
| 325 | p = &(*p)->rb_right; |
| 326 | else if (sector < __iot->sector) |
| 327 | p = &(*p)->rb_left; |
| 328 | else if (sector > __iot->sector) |
| 329 | p = &(*p)->rb_right; |
| 330 | else |
| 331 | return __iot; |
| 332 | } |
| 333 | |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector) |
| 338 | { |
| 339 | struct io_track *iot; |
| 340 | |
| 341 | iot = __find_track(device, sector); |
| 342 | if (!iot) { |
| 343 | iot = malloc(sizeof(*iot)); |
| 344 | iot->pid = pid; |
| 345 | iot->device = device; |
| 346 | iot->sector = sector; |
| 347 | track_rb_insert(iot); |
| 348 | } |
| 349 | |
| 350 | return iot; |
| 351 | } |
| 352 | |
| 353 | static void log_track_merge(struct blk_io_trace *t) |
| 354 | { |
| 355 | struct io_track *iot; |
| 356 | |
| 357 | if (!track_ios) |
| 358 | return; |
| 359 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) |
| 360 | return; |
| 361 | |
| 362 | iot = __find_track(t->device, t->sector - (t->bytes >> 10)); |
| 363 | if (!iot) { |
| 364 | fprintf(stderr, "Trying to merge on non-existing request\n"); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | rb_erase(&iot->rb_node, &rb_track_root); |
| 369 | iot->sector -= t->bytes >> 10; |
| 370 | track_rb_insert(iot); |
| 371 | } |
| 372 | |
| 373 | static void log_track_getrq(struct blk_io_trace *t) |
| 374 | { |
| 375 | struct io_track *iot; |
| 376 | |
| 377 | if (!track_ios) |
| 378 | return; |
| 379 | |
| 380 | iot = find_track(t->pid, t->device, t->sector); |
| 381 | iot->allocation_time = t->time; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* |
| 386 | * return time between rq allocation and queue |
| 387 | */ |
| 388 | static unsigned long long log_track_queue(struct blk_io_trace *t) |
| 389 | { |
| 390 | unsigned long long elapsed; |
| 391 | struct io_track *iot; |
| 392 | |
| 393 | if (!track_ios) |
| 394 | return -1; |
| 395 | |
| 396 | iot = find_track(t->pid, t->device, t->sector); |
| 397 | iot->queue_time = t->time; |
| 398 | elapsed = iot->queue_time - iot->allocation_time; |
| 399 | |
| 400 | if (per_process_stats) { |
| 401 | struct per_process_info *ppi = find_process_by_pid(iot->pid); |
| 402 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 403 | |
| 404 | if (ppi && elapsed > ppi->longest_allocation_wait[w]) |
| 405 | ppi->longest_allocation_wait[w] = elapsed; |
| 406 | } |
| 407 | |
| 408 | return elapsed; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * return time between queue and issue |
| 413 | */ |
| 414 | static unsigned long long log_track_issue(struct blk_io_trace *t) |
| 415 | { |
| 416 | unsigned long long elapsed; |
| 417 | struct io_track *iot; |
| 418 | |
| 419 | if (!track_ios) |
| 420 | return -1; |
| 421 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) |
| 422 | return -1; |
| 423 | |
| 424 | iot = __find_track(t->device, t->sector); |
| 425 | if (!iot) { |
| 426 | fprintf(stderr, "Trying to issue on non-existing request\n"); |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | iot->dispatch_time = t->time; |
| 431 | elapsed = iot->dispatch_time - iot->queue_time; |
| 432 | |
| 433 | if (per_process_stats) { |
| 434 | struct per_process_info *ppi = find_process_by_pid(iot->pid); |
| 435 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 436 | |
| 437 | if (ppi && elapsed > ppi->longest_dispatch_wait[w]) |
| 438 | ppi->longest_dispatch_wait[w] = elapsed; |
| 439 | } |
| 440 | |
| 441 | return elapsed; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * return time between dispatch and complete |
| 446 | */ |
| 447 | static unsigned long long log_track_complete(struct blk_io_trace *t) |
| 448 | { |
| 449 | unsigned long long elapsed; |
| 450 | struct io_track *iot; |
| 451 | |
| 452 | if (!track_ios) |
| 453 | return -1; |
| 454 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) |
| 455 | return -1; |
| 456 | |
| 457 | iot = __find_track(t->device, t->sector); |
| 458 | if (!iot) { |
| 459 | fprintf(stderr, "Trying to dispatch on non-existing request\n"); |
| 460 | return -1; |
| 461 | } |
| 462 | |
| 463 | iot->completion_time = t->time; |
| 464 | elapsed = iot->completion_time - iot->dispatch_time; |
| 465 | |
| 466 | if (per_process_stats) { |
| 467 | struct per_process_info *ppi = find_process_by_pid(iot->pid); |
| 468 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 469 | |
| 470 | if (ppi && elapsed > ppi->longest_completion_wait[w]) |
| 471 | ppi->longest_completion_wait[w] = elapsed; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * kill the trace, we don't need it after completion |
| 476 | */ |
| 477 | rb_erase(&iot->rb_node, &rb_track_root); |
| 478 | free(iot); |
| 479 | |
| 480 | return elapsed; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | static struct io_stats *find_process_io_stats(__u32 pid, char *name) |
| 485 | { |
| 486 | struct per_process_info *ppi = find_process_by_pid(pid); |
| 487 | |
| 488 | if (!ppi) { |
| 489 | ppi = malloc(sizeof(*ppi)); |
| 490 | memset(ppi, 0, sizeof(*ppi)); |
| 491 | strncpy(ppi->name, name, sizeof(ppi->name)); |
| 492 | ppi->pid = pid; |
| 493 | add_process_to_hash(ppi); |
| 494 | add_process_to_list(ppi); |
| 495 | } |
| 496 | |
| 497 | return &ppi->io_stats; |
| 498 | } |
| 499 | |
| 500 | |
| 501 | static void resize_cpu_info(struct per_dev_info *pdi, int cpu) |
| 502 | { |
| 503 | struct per_cpu_info *cpus = pdi->cpus; |
| 504 | int ncpus = pdi->ncpus; |
| 505 | int new_count = cpu + 1; |
| 506 | int new_space, size; |
| 507 | char *new_start; |
| 508 | |
| 509 | size = new_count * sizeof(struct per_cpu_info); |
| 510 | cpus = realloc(cpus, size); |
| 511 | if (!cpus) { |
| 512 | char name[20]; |
| 513 | fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n", |
| 514 | get_dev_name(pdi, name, sizeof(name)), size); |
| 515 | exit(1); |
| 516 | } |
| 517 | |
| 518 | new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info)); |
| 519 | new_space = (new_count - ncpus) * sizeof(struct per_cpu_info); |
| 520 | memset(new_start, 0, new_space); |
| 521 | |
| 522 | pdi->ncpus = new_count; |
| 523 | pdi->cpus = cpus; |
| 524 | } |
| 525 | |
| 526 | static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu) |
| 527 | { |
| 528 | if (cpu >= pdi->ncpus) |
| 529 | resize_cpu_info(pdi, cpu); |
| 530 | return &pdi->cpus[cpu]; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | static int resize_devices(char *name) |
| 535 | { |
| 536 | int size = (ndevices + 1) * sizeof(struct per_dev_info); |
| 537 | |
| 538 | devices = realloc(devices, size); |
| 539 | if (!devices) { |
| 540 | fprintf(stderr, "Out of memory, device %s (%d)\n", name, size); |
| 541 | return 1; |
| 542 | } |
| 543 | memset(&devices[ndevices], 0, sizeof(struct per_dev_info)); |
| 544 | devices[ndevices].name = name; |
| 545 | ndevices++; |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static struct per_dev_info *get_dev_info(dev_t id, int create) |
| 550 | { |
| 551 | int i; |
| 552 | |
| 553 | for (i = 0; i < ndevices; i++) |
| 554 | if (devices[i].id == id) |
| 555 | return &devices[i]; |
| 556 | if (!create) |
| 557 | return NULL; |
| 558 | if (resize_devices(NULL) != 0) |
| 559 | return NULL; |
| 560 | return &devices[ndevices-1]; |
| 561 | } |
| 562 | |
| 563 | static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size) |
| 564 | { |
| 565 | if (pdi->name) |
| 566 | snprintf(buffer, size, "%s", pdi->name); |
| 567 | else |
| 568 | snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id)); |
| 569 | return buffer; |
| 570 | } |
| 571 | |
| 572 | |
| 573 | static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit) |
| 574 | { |
| 575 | unsigned long long this = bit->time; |
| 576 | unsigned long long last = pdi->last_reported_time; |
| 577 | |
| 578 | pdi->backwards = (this < last) ? 'B' : ' '; |
| 579 | pdi->last_reported_time = this; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t, |
| 584 | int rw) |
| 585 | { |
| 586 | if (rw) { |
| 587 | ios->mwrites++; |
| 588 | ios->qwrite_kb += t->bytes >> 10; |
| 589 | } else { |
| 590 | ios->mreads++; |
| 591 | ios->qread_kb += t->bytes >> 10; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 596 | int rw) |
| 597 | { |
| 598 | __account_m(&pci->io_stats, t, rw); |
| 599 | |
| 600 | if (per_process_stats) { |
| 601 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 602 | |
| 603 | __account_m(ios, t, rw); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t, |
| 608 | int rw) |
| 609 | { |
| 610 | if (rw) { |
| 611 | ios->qwrites++; |
| 612 | ios->qwrite_kb += t->bytes >> 10; |
| 613 | } else { |
| 614 | ios->qreads++; |
| 615 | ios->qread_kb += t->bytes >> 10; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 620 | int rw) |
| 621 | { |
| 622 | __account_q(&pci->io_stats, t, rw); |
| 623 | |
| 624 | if (per_process_stats) { |
| 625 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 626 | |
| 627 | __account_q(ios, t, rw); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes) |
| 632 | { |
| 633 | if (rw) { |
| 634 | ios->cwrites++; |
| 635 | ios->cwrite_kb += bytes >> 10; |
| 636 | } else { |
| 637 | ios->creads++; |
| 638 | ios->cread_kb += bytes >> 10; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 643 | int rw, int bytes) |
| 644 | { |
| 645 | __account_c(&pci->io_stats, rw, bytes); |
| 646 | |
| 647 | if (per_process_stats) { |
| 648 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 649 | |
| 650 | __account_c(ios, rw, bytes); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes) |
| 655 | { |
| 656 | if (rw) { |
| 657 | ios->iwrites++; |
| 658 | ios->iwrite_kb += bytes >> 10; |
| 659 | } else { |
| 660 | ios->ireads++; |
| 661 | ios->iread_kb += bytes >> 10; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 666 | int rw) |
| 667 | { |
| 668 | __account_i(&pci->io_stats, rw, t->bytes); |
| 669 | |
| 670 | if (per_process_stats) { |
| 671 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); |
| 672 | |
| 673 | __account_i(ios, rw, t->bytes); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static void output(struct per_cpu_info *pci, char *s) |
| 678 | { |
| 679 | fprintf(ofp, "%s", s); |
| 680 | } |
| 681 | |
| 682 | static char hstring[256]; |
| 683 | static char tstring[256]; |
| 684 | |
| 685 | static inline char *setup_header(struct per_cpu_info *pci, |
| 686 | struct blk_io_trace *t, char act) |
| 687 | { |
| 688 | int w = t->action & BLK_TC_ACT(BLK_TC_WRITE); |
| 689 | int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER); |
| 690 | int s = t->action & BLK_TC_ACT(BLK_TC_SYNC); |
| 691 | char rwbs[4]; |
| 692 | int i = 0; |
| 693 | |
| 694 | if (w) |
| 695 | rwbs[i++] = 'W'; |
| 696 | else |
| 697 | rwbs[i++] = 'R'; |
| 698 | if (b) |
| 699 | rwbs[i++] = 'B'; |
| 700 | if (s) |
| 701 | rwbs[i++] = 'S'; |
| 702 | |
| 703 | rwbs[i] = '\0'; |
| 704 | |
| 705 | sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %c %3s", |
| 706 | MAJOR(t->device), MINOR(t->device), pci->cpu, |
| 707 | (unsigned long)t->sequence, SECONDS(t->time), |
| 708 | NANO_SECONDS(t->time), t->pid, act, rwbs); |
| 709 | |
| 710 | return hstring; |
| 711 | } |
| 712 | |
| 713 | static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 714 | char act) |
| 715 | { |
| 716 | unsigned long long elapsed = log_track_complete(t); |
| 717 | |
| 718 | if (elapsed != -1ULL) { |
| 719 | unsigned long usec = elapsed / 1000; |
| 720 | |
| 721 | sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n", |
| 722 | setup_header(pci, t, act), |
| 723 | (unsigned long long)t->sector, t->bytes >> 9, |
| 724 | usec, t->error); |
| 725 | } else { |
| 726 | sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act), |
| 727 | (unsigned long long)t->sector, t->bytes >> 9, t->error); |
| 728 | } |
| 729 | |
| 730 | output(pci, tstring); |
| 731 | } |
| 732 | |
| 733 | static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 734 | char act) |
| 735 | { |
| 736 | unsigned long long elapsed = log_track_queue(t); |
| 737 | |
| 738 | if (elapsed != -1ULL) { |
| 739 | unsigned long usec = elapsed / 1000; |
| 740 | |
| 741 | sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n", |
| 742 | setup_header(pci, t, act), |
| 743 | (unsigned long long)t->sector, t->bytes >> 9, |
| 744 | usec, t->comm); |
| 745 | } else { |
| 746 | sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act), |
| 747 | (unsigned long long)t->sector, t->bytes >> 9, t->comm); |
| 748 | } |
| 749 | output(pci, tstring); |
| 750 | } |
| 751 | |
| 752 | static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 753 | char act) |
| 754 | { |
| 755 | unsigned long long elapsed = log_track_issue(t); |
| 756 | |
| 757 | if (elapsed != -1ULL) { |
| 758 | double usec = (double) elapsed / 1000; |
| 759 | |
| 760 | sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n", |
| 761 | setup_header(pci, t, act), |
| 762 | (unsigned long long)t->sector, t->bytes >> 9, |
| 763 | usec, t->comm); |
| 764 | } else { |
| 765 | sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act), |
| 766 | (unsigned long long)t->sector, t->bytes >> 9, t->comm); |
| 767 | } |
| 768 | |
| 769 | output(pci, tstring); |
| 770 | } |
| 771 | |
| 772 | static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 773 | char act) |
| 774 | { |
| 775 | log_track_merge(t); |
| 776 | |
| 777 | sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act), |
| 778 | (unsigned long long)t->sector, t->bytes >> 9, t->comm); |
| 779 | output(pci, tstring); |
| 780 | } |
| 781 | |
| 782 | static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 783 | char act) |
| 784 | { |
| 785 | sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act), |
| 786 | (unsigned long long)t->sector, t->bytes >> 9, t->comm); |
| 787 | output(pci, tstring); |
| 788 | } |
| 789 | |
| 790 | static int log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 791 | char act) |
| 792 | { |
| 793 | __u64 *depth; |
| 794 | int len; |
| 795 | |
| 796 | len = sprintf(tstring,"%s ", setup_header(pci, t, act)); |
| 797 | depth = (__u64 *) t + sizeof(*t); |
| 798 | sprintf(tstring + len, "%u\n", (unsigned int) be64_to_cpu(*depth)); |
| 799 | output(pci, tstring); |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char act) |
| 805 | { |
| 806 | unsigned char *buf; |
| 807 | int i; |
| 808 | |
| 809 | sprintf(tstring,"%s ", setup_header(pci, t, act)); |
| 810 | output(pci, tstring); |
| 811 | |
| 812 | buf = (unsigned char *) t + sizeof(*t); |
| 813 | for (i = 0; i < t->pdu_len; i++) { |
| 814 | sprintf(tstring,"%02x ", buf[i]); |
| 815 | output(pci, tstring); |
| 816 | } |
| 817 | |
| 818 | if (act == 'C') { |
| 819 | sprintf(tstring,"[%d]\n", t->error); |
| 820 | output(pci, tstring); |
| 821 | } else { |
| 822 | sprintf(tstring,"[%s]\n", t->comm); |
| 823 | output(pci, tstring); |
| 824 | } |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci) |
| 829 | { |
| 830 | int ret = 0; |
| 831 | |
| 832 | switch (t->action & 0xffff) { |
| 833 | case __BLK_TA_QUEUE: |
| 834 | log_generic(pci, t, 'Q'); |
| 835 | break; |
| 836 | case __BLK_TA_GETRQ: |
| 837 | log_generic(pci, t, 'G'); |
| 838 | break; |
| 839 | case __BLK_TA_SLEEPRQ: |
| 840 | log_generic(pci, t, 'S'); |
| 841 | break; |
| 842 | case __BLK_TA_REQUEUE: |
| 843 | log_generic(pci, t, 'R'); |
| 844 | break; |
| 845 | case __BLK_TA_ISSUE: |
| 846 | ret = log_pc(pci, t, 'D'); |
| 847 | break; |
| 848 | case __BLK_TA_COMPLETE: |
| 849 | log_pc(pci, t, 'C'); |
| 850 | break; |
| 851 | default: |
| 852 | fprintf(stderr, "Bad pc action %x\n", t->action); |
| 853 | ret = 1; |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci) |
| 861 | { |
| 862 | int w = t->action & BLK_TC_ACT(BLK_TC_WRITE); |
| 863 | int act = t->action & 0xffff; |
| 864 | |
| 865 | switch (act) { |
| 866 | case __BLK_TA_QUEUE: |
| 867 | account_q(t, pci, w); |
| 868 | log_queue(pci, t, 'Q'); |
| 869 | break; |
| 870 | case __BLK_TA_BACKMERGE: |
| 871 | account_m(t, pci, w); |
| 872 | log_merge(pci, t, 'M'); |
| 873 | break; |
| 874 | case __BLK_TA_FRONTMERGE: |
| 875 | account_m(t, pci, w); |
| 876 | log_merge(pci, t, 'F'); |
| 877 | break; |
| 878 | case __BLK_TA_GETRQ: |
| 879 | log_track_getrq(t); |
| 880 | log_generic(pci, t, 'G'); |
| 881 | break; |
| 882 | case __BLK_TA_SLEEPRQ: |
| 883 | log_generic(pci, t, 'S'); |
| 884 | break; |
| 885 | case __BLK_TA_REQUEUE: |
| 886 | account_c(t, pci, w, -t->bytes); |
| 887 | log_queue(pci, t, 'R'); |
| 888 | break; |
| 889 | case __BLK_TA_ISSUE: |
| 890 | account_i(t, pci, w); |
| 891 | log_issue(pci, t, 'D'); |
| 892 | break; |
| 893 | case __BLK_TA_COMPLETE: |
| 894 | account_c(t, pci, w, t->bytes); |
| 895 | log_complete(pci, t, 'C'); |
| 896 | break; |
| 897 | case __BLK_TA_PLUG: |
| 898 | log_generic(pci, t, 'P'); |
| 899 | break; |
| 900 | case __BLK_TA_UNPLUG: |
| 901 | log_unplug(pci, t, 'U'); |
| 902 | break; |
| 903 | default: |
| 904 | fprintf(stderr, "Bad fs action %x\n", t->action); |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 910 | struct per_dev_info *pdi) |
| 911 | { |
| 912 | int ret = 0; |
| 913 | |
| 914 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) |
| 915 | ret = dump_trace_pc(t, pci); |
| 916 | else |
| 917 | dump_trace_fs(t, pci); |
| 918 | |
| 919 | pdi->events++; |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | static void dump_io_stats(struct io_stats *ios, char *msg) |
| 924 | { |
| 925 | fprintf(ofp, "%s\n", msg); |
| 926 | |
| 927 | fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb); |
| 928 | fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb); |
| 929 | |
| 930 | fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb); |
| 931 | fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb); |
| 932 | fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb); |
| 933 | fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb); |
| 934 | fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' '); |
| 935 | |
| 936 | fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites); |
| 937 | } |
| 938 | |
| 939 | static void dump_wait_stats(struct per_process_info *ppi) |
| 940 | { |
| 941 | unsigned long rawait = ppi->longest_allocation_wait[0] / 1000; |
| 942 | unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000; |
| 943 | unsigned long rcwait = ppi->longest_completion_wait[0] / 1000; |
| 944 | unsigned long wawait = ppi->longest_allocation_wait[1] / 1000; |
| 945 | unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000; |
| 946 | unsigned long wcwait = ppi->longest_completion_wait[1] / 1000; |
| 947 | |
| 948 | fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' '); |
| 949 | fprintf(ofp, " Allocation wait: %'8lu\n", wawait); |
| 950 | fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' '); |
| 951 | fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait); |
| 952 | fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' '); |
| 953 | fprintf(ofp, " Completion wait: %'8lu\n", wcwait); |
| 954 | } |
| 955 | |
| 956 | static void show_process_stats(void) |
| 957 | { |
| 958 | struct per_process_info *ppi; |
| 959 | |
| 960 | ppi = ppi_list; |
| 961 | while (ppi) { |
| 962 | dump_io_stats(&ppi->io_stats, ppi->name); |
| 963 | dump_wait_stats(ppi); |
| 964 | ppi = ppi->list_next; |
| 965 | } |
| 966 | |
| 967 | fprintf(ofp, "\n"); |
| 968 | } |
| 969 | |
| 970 | static void show_device_and_cpu_stats(void) |
| 971 | { |
| 972 | struct per_dev_info *pdi; |
| 973 | struct per_cpu_info *pci; |
| 974 | struct io_stats total, *ios; |
| 975 | int i, j, pci_events; |
| 976 | char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3]; |
| 977 | char name[32]; |
| 978 | |
| 979 | for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { |
| 980 | |
| 981 | memset(&total, 0, sizeof(total)); |
| 982 | pci_events = 0; |
| 983 | |
| 984 | if (i > 0) |
| 985 | fprintf(ofp, "\n"); |
| 986 | |
| 987 | for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) { |
| 988 | if (!pci->nelems) |
| 989 | continue; |
| 990 | |
| 991 | ios = &pci->io_stats; |
| 992 | total.qreads += ios->qreads; |
| 993 | total.qwrites += ios->qwrites; |
| 994 | total.creads += ios->creads; |
| 995 | total.cwrites += ios->cwrites; |
| 996 | total.mreads += ios->mreads; |
| 997 | total.mwrites += ios->mwrites; |
| 998 | total.ireads += ios->ireads; |
| 999 | total.iwrites += ios->iwrites; |
| 1000 | total.qread_kb += ios->qread_kb; |
| 1001 | total.qwrite_kb += ios->qwrite_kb; |
| 1002 | total.cread_kb += ios->cread_kb; |
| 1003 | total.cwrite_kb += ios->cwrite_kb; |
| 1004 | total.iread_kb += ios->iread_kb; |
| 1005 | total.iwrite_kb += ios->iwrite_kb; |
| 1006 | |
| 1007 | snprintf(line, sizeof(line) - 1, "CPU%d (%s):", |
| 1008 | j, get_dev_name(pdi, name, sizeof(name))); |
| 1009 | dump_io_stats(ios, line); |
| 1010 | pci_events++; |
| 1011 | } |
| 1012 | |
| 1013 | if (pci_events > 1) { |
| 1014 | fprintf(ofp, "\n"); |
| 1015 | snprintf(line, sizeof(line) - 1, "Total (%s):", |
| 1016 | get_dev_name(pdi, name, sizeof(name))); |
| 1017 | dump_io_stats(&total, line); |
| 1018 | } |
| 1019 | |
| 1020 | fprintf(ofp, "Events (%s): %'Lu\n", |
| 1021 | get_dev_name(pdi, line, sizeof(line)), pdi->events); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | static struct blk_io_trace *find_trace(void *p, unsigned long offset, int nr) |
| 1026 | { |
| 1027 | unsigned long max_offset = min(offset,nr * sizeof(struct blk_io_trace)); |
| 1028 | unsigned long off; |
| 1029 | struct blk_io_trace *bit; |
| 1030 | __u32 magic; |
| 1031 | |
| 1032 | for (off = 0; off < max_offset; off++) { |
| 1033 | bit = p + off; |
| 1034 | |
| 1035 | magic = be32_to_cpu(bit->magic); |
| 1036 | if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) |
| 1037 | return bit; |
| 1038 | } |
| 1039 | |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | |
| 1043 | static int sort_entries(void *traces, unsigned long offset, int nr, |
| 1044 | struct per_dev_info *fpdi, struct per_cpu_info *fpci) |
| 1045 | { |
| 1046 | struct per_dev_info *pdi; |
| 1047 | struct per_cpu_info *pci; |
| 1048 | struct blk_io_trace *bit; |
| 1049 | struct trace *t; |
| 1050 | void *start = traces; |
| 1051 | |
| 1052 | while (traces - start <= offset - sizeof(*bit)) { |
| 1053 | if (!nr) |
| 1054 | break; |
| 1055 | |
| 1056 | bit = find_trace(traces, offset - (traces - start), nr); |
| 1057 | if (!bit) |
| 1058 | break; |
| 1059 | |
| 1060 | t = malloc(sizeof(*t)); |
| 1061 | if (!t) { |
| 1062 | fprintf(stderr, "Out of memory, seq %d on dev %d,%d\n", |
| 1063 | bit->sequence, |
| 1064 | MAJOR(bit->device), MINOR(bit->device)); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | t->bit = bit; |
| 1068 | memset(&t->rb_node, 0, sizeof(t->rb_node)); |
| 1069 | |
| 1070 | trace_to_cpu(bit); |
| 1071 | |
| 1072 | if (verify_trace(bit)) { |
| 1073 | free(t); |
| 1074 | break; |
| 1075 | } |
| 1076 | |
| 1077 | pdi = fpdi ? fpdi : get_dev_info(bit->device, 1); |
| 1078 | pdi->id = bit->device; |
| 1079 | pci = fpci ? fpci : get_cpu_info(pdi, bit->cpu); |
| 1080 | pci->cpu = bit->cpu; |
| 1081 | pci->nelems++; |
| 1082 | |
| 1083 | if (trace_rb_insert(t)) { |
| 1084 | free(t); |
| 1085 | return -1; |
| 1086 | } |
| 1087 | |
| 1088 | traces += sizeof(*bit) + bit->pdu_len; |
| 1089 | nr--; |
| 1090 | } |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | static void free_entries_rb(void) |
| 1096 | { |
| 1097 | struct rb_node *n; |
| 1098 | |
| 1099 | while ((n = rb_first(&rb_sort_root)) != NULL) { |
| 1100 | struct trace *t = rb_entry(n, struct trace, rb_node); |
| 1101 | |
| 1102 | rb_erase(&t->rb_node, &rb_sort_root); |
| 1103 | free(t); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | static void show_entries_rb(void) |
| 1108 | { |
| 1109 | struct per_dev_info *pdi; |
| 1110 | struct blk_io_trace *bit; |
| 1111 | struct rb_node *n; |
| 1112 | struct trace *t; |
| 1113 | int cpu; |
| 1114 | |
| 1115 | n = rb_first(&rb_sort_root); |
| 1116 | if (!n) |
| 1117 | return; |
| 1118 | |
| 1119 | do { |
| 1120 | t = rb_entry(n, struct trace, rb_node); |
| 1121 | bit = t->bit; |
| 1122 | |
| 1123 | pdi = get_dev_info(bit->device, 0); |
| 1124 | if (!pdi) { |
| 1125 | fprintf(stderr, "Unknown device ID? (%d,%d)\n", |
| 1126 | MAJOR(bit->device), MINOR(bit->device)); |
| 1127 | break; |
| 1128 | } |
| 1129 | cpu = bit->cpu; |
| 1130 | if (cpu > pdi->ncpus) { |
| 1131 | fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n", |
| 1132 | cpu, MAJOR(bit->device), MINOR(bit->device)); |
| 1133 | break; |
| 1134 | } |
| 1135 | |
| 1136 | bit->time -= genesis_time; |
| 1137 | if (bit->time < stopwatch_start) |
| 1138 | continue; |
| 1139 | if (bit->time >= stopwatch_end) |
| 1140 | break; |
| 1141 | |
| 1142 | check_time(pdi, bit); |
| 1143 | |
| 1144 | if (dump_trace(bit, &pdi->cpus[cpu], pdi)) |
| 1145 | break; |
| 1146 | |
| 1147 | } while ((n = rb_next(n)) != NULL); |
| 1148 | } |
| 1149 | |
| 1150 | static int read_data(int fd, void *buffer, int bytes, int block) |
| 1151 | { |
| 1152 | int ret, bytes_left, fl; |
| 1153 | void *p; |
| 1154 | |
| 1155 | fl = fcntl(fd, F_GETFL); |
| 1156 | |
| 1157 | if (!block) |
| 1158 | fcntl(fd, F_SETFL, fl | O_NONBLOCK); |
| 1159 | else |
| 1160 | fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); |
| 1161 | |
| 1162 | bytes_left = bytes; |
| 1163 | p = buffer; |
| 1164 | while (bytes_left > 0) { |
| 1165 | ret = read(fd, p, bytes_left); |
| 1166 | if (!ret) |
| 1167 | return 1; |
| 1168 | else if (ret < 0) { |
| 1169 | if (errno != EAGAIN) |
| 1170 | perror("read"); |
| 1171 | return -1; |
| 1172 | } else { |
| 1173 | p += ret; |
| 1174 | bytes_left -= ret; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | static int do_file(void) |
| 1182 | { |
| 1183 | struct per_dev_info *pdi; |
| 1184 | int i, j, nfiles = 0; |
| 1185 | |
| 1186 | for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { |
| 1187 | for (j = 0;; j++, nfiles++) { |
| 1188 | struct per_cpu_info *pci; |
| 1189 | struct stat st; |
| 1190 | void *tb; |
| 1191 | |
| 1192 | pci = get_cpu_info(pdi, j); |
| 1193 | pci->cpu = j; |
| 1194 | |
| 1195 | snprintf(pci->fname, sizeof(pci->fname)-1, |
| 1196 | "%s_out.%d", pdi->name, j); |
| 1197 | if (stat(pci->fname, &st) < 0) |
| 1198 | break; |
| 1199 | if (!st.st_size) |
| 1200 | continue; |
| 1201 | |
| 1202 | printf("Processing %s\n", pci->fname); |
| 1203 | |
| 1204 | tb = malloc(st.st_size); |
| 1205 | if (!tb) { |
| 1206 | fprintf(stderr, "Out of memory, skip file %s\n", |
| 1207 | pci->fname); |
| 1208 | continue; |
| 1209 | } |
| 1210 | |
| 1211 | pci->fd = open(pci->fname, O_RDONLY); |
| 1212 | if (pci->fd < 0) { |
| 1213 | perror(pci->fname); |
| 1214 | free(tb); |
| 1215 | continue; |
| 1216 | } |
| 1217 | |
| 1218 | if (read_data(pci->fd, tb, st.st_size, 1)) { |
| 1219 | close(pci->fd); |
| 1220 | free(tb); |
| 1221 | continue; |
| 1222 | } |
| 1223 | |
| 1224 | if (sort_entries(tb, st.st_size, ~0U, pdi, pci) == -1) { |
| 1225 | close(pci->fd); |
| 1226 | free(tb); |
| 1227 | continue; |
| 1228 | } |
| 1229 | |
| 1230 | printf("Completed %s (CPU%d %d, entries)\n", |
| 1231 | pci->fname, j, pci->nelems); |
| 1232 | close(pci->fd); |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | if (!nfiles) { |
| 1237 | fprintf(stderr, "No files found\n"); |
| 1238 | return 1; |
| 1239 | } |
| 1240 | |
| 1241 | show_entries_rb(); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static void resize_buffer(void **buffer, long *size, long offset) |
| 1246 | { |
| 1247 | long old_size = *size; |
| 1248 | |
| 1249 | if (*size == 0) |
| 1250 | *size = 64 * sizeof(struct blk_io_trace); |
| 1251 | |
| 1252 | *size *= 2; |
| 1253 | *buffer = realloc(*buffer, *size); |
| 1254 | |
| 1255 | if (old_size) |
| 1256 | memset(*buffer + offset, 0, *size - old_size); |
| 1257 | } |
| 1258 | |
| 1259 | static int read_sort_events(int fd, void **buffer, long *max_offset) |
| 1260 | { |
| 1261 | long offset; |
| 1262 | int events; |
| 1263 | |
| 1264 | events = offset = 0; |
| 1265 | do { |
| 1266 | struct blk_io_trace *t; |
| 1267 | int pdu_len; |
| 1268 | __u32 magic; |
| 1269 | |
| 1270 | if (*max_offset - offset < sizeof(*t)) |
| 1271 | resize_buffer(buffer, max_offset, offset); |
| 1272 | |
| 1273 | if (read_data(fd, *buffer + offset, sizeof(*t), !events)) |
| 1274 | break; |
| 1275 | |
| 1276 | t = *buffer + offset; |
| 1277 | offset += sizeof(*t); |
| 1278 | |
| 1279 | magic = be32_to_cpu(t->magic); |
| 1280 | if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
| 1281 | fprintf(stderr, "Bad magic %x\n", magic); |
| 1282 | break; |
| 1283 | } |
| 1284 | |
| 1285 | pdu_len = be16_to_cpu(t->pdu_len); |
| 1286 | if (pdu_len) { |
| 1287 | if (*max_offset - offset <= pdu_len) |
| 1288 | resize_buffer(buffer, max_offset, offset); |
| 1289 | |
| 1290 | if (read_data(fd, *buffer + offset, pdu_len, 1)) |
| 1291 | break; |
| 1292 | |
| 1293 | offset += pdu_len; |
| 1294 | } |
| 1295 | |
| 1296 | events++; |
| 1297 | } while (!is_done() && events < rb_batch); |
| 1298 | |
| 1299 | return events; |
| 1300 | } |
| 1301 | |
| 1302 | static int do_stdin(void) |
| 1303 | { |
| 1304 | int fd; |
| 1305 | void *ptr = NULL; |
| 1306 | long max_offset; |
| 1307 | |
| 1308 | fd = dup(STDIN_FILENO); |
| 1309 | max_offset = 0; |
| 1310 | do { |
| 1311 | int events; |
| 1312 | |
| 1313 | events = read_sort_events(fd, &ptr, &max_offset); |
| 1314 | if (!events) |
| 1315 | break; |
| 1316 | |
| 1317 | if (sort_entries(ptr, ~0UL, events, NULL, NULL) == -1) |
| 1318 | break; |
| 1319 | |
| 1320 | show_entries_rb(); |
| 1321 | free_entries_rb(); |
| 1322 | } while (1); |
| 1323 | |
| 1324 | if (ptr) |
| 1325 | free(ptr); |
| 1326 | |
| 1327 | close(fd); |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | static void flush_output(void) |
| 1332 | { |
| 1333 | fflush(ofp); |
| 1334 | } |
| 1335 | |
| 1336 | static void handle_sigint(int sig) |
| 1337 | { |
| 1338 | done = 1; |
| 1339 | flush_output(); |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * Extract start and duration times from a string, allowing |
| 1344 | * us to specify a time interval of interest within a trace. |
| 1345 | * Format: "duration" (start is zero) or "start:duration". |
| 1346 | */ |
| 1347 | static int find_stopwatch_interval(char *string) |
| 1348 | { |
| 1349 | double value; |
| 1350 | char *sp; |
| 1351 | |
| 1352 | value = strtod(string, &sp); |
| 1353 | if (sp == string) { |
| 1354 | fprintf(stderr,"Invalid stopwatch timer: %s\n", string); |
| 1355 | return 1; |
| 1356 | } |
| 1357 | if (*sp == ':') { |
| 1358 | stopwatch_start = DOUBLE_TO_NANO_ULL(value); |
| 1359 | string = sp + 1; |
| 1360 | value = strtod(string, &sp); |
| 1361 | if (sp == string || *sp != '\0') { |
| 1362 | fprintf(stderr,"Invalid stopwatch duration time: %s\n", |
| 1363 | string); |
| 1364 | return 1; |
| 1365 | } |
| 1366 | } else if (*sp != '\0') { |
| 1367 | fprintf(stderr,"Invalid stopwatch start timer: %s\n", string); |
| 1368 | return 1; |
| 1369 | } |
| 1370 | stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value); |
| 1371 | return 0; |
| 1372 | } |
| 1373 | |
| 1374 | static void usage(char *prog) |
| 1375 | { |
| 1376 | fprintf(stderr, "Usage: %s " |
| 1377 | "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n", |
| 1378 | prog); |
| 1379 | } |
| 1380 | |
| 1381 | int main(int argc, char *argv[]) |
| 1382 | { |
| 1383 | char *ofp_buffer; |
| 1384 | int c, ret, mode; |
| 1385 | int per_device_and_cpu_stats = 1; |
| 1386 | |
| 1387 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) { |
| 1388 | switch (c) { |
| 1389 | case 'i': |
| 1390 | if (!strcmp(optarg, "-") && !pipeline) |
| 1391 | pipeline = 1; |
| 1392 | else if (resize_devices(optarg) != 0) |
| 1393 | return 1; |
| 1394 | break; |
| 1395 | case 'o': |
| 1396 | output_name = optarg; |
| 1397 | break; |
| 1398 | case 'b': |
| 1399 | rb_batch = atoi(optarg); |
| 1400 | if (rb_batch <= 0) |
| 1401 | rb_batch = RB_BATCH_DEFAULT; |
| 1402 | break; |
| 1403 | case 's': |
| 1404 | per_process_stats = 1; |
| 1405 | break; |
| 1406 | case 't': |
| 1407 | track_ios = 1; |
| 1408 | break; |
| 1409 | case 'q': |
| 1410 | per_device_and_cpu_stats = 0; |
| 1411 | break; |
| 1412 | case 'w': |
| 1413 | if (find_stopwatch_interval(optarg) != 0) |
| 1414 | return 1; |
| 1415 | break; |
| 1416 | default: |
| 1417 | usage(argv[0]); |
| 1418 | return 1; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | while (optind < argc) { |
| 1423 | if (!strcmp(argv[optind], "-") && !pipeline) |
| 1424 | pipeline = 1; |
| 1425 | else if (resize_devices(argv[optind]) != 0) |
| 1426 | return 1; |
| 1427 | optind++; |
| 1428 | } |
| 1429 | |
| 1430 | if (!pipeline && !ndevices) { |
| 1431 | usage(argv[0]); |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
| 1435 | memset(&rb_sort_root, 0, sizeof(rb_sort_root)); |
| 1436 | memset(&rb_track_root, 0, sizeof(rb_track_root)); |
| 1437 | |
| 1438 | signal(SIGINT, handle_sigint); |
| 1439 | signal(SIGHUP, handle_sigint); |
| 1440 | signal(SIGTERM, handle_sigint); |
| 1441 | |
| 1442 | setlocale(LC_NUMERIC, "en_US"); |
| 1443 | |
| 1444 | if (!output_name) { |
| 1445 | ofp = fdopen(STDOUT_FILENO, "w"); |
| 1446 | mode = _IOLBF; |
| 1447 | } else { |
| 1448 | char ofname[128]; |
| 1449 | |
| 1450 | snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name); |
| 1451 | ofp = fopen(ofname, "w"); |
| 1452 | mode = _IOFBF; |
| 1453 | } |
| 1454 | |
| 1455 | if (!ofp) { |
| 1456 | perror("fopen"); |
| 1457 | return 1; |
| 1458 | } |
| 1459 | |
| 1460 | ofp_buffer = malloc(4096); |
| 1461 | if (setvbuf(ofp, ofp_buffer, mode, 4096)) { |
| 1462 | perror("setvbuf"); |
| 1463 | return 1; |
| 1464 | } |
| 1465 | |
| 1466 | if (pipeline) |
| 1467 | ret = do_stdin(); |
| 1468 | else |
| 1469 | ret = do_file(); |
| 1470 | |
| 1471 | if (per_process_stats) |
| 1472 | show_process_stats(); |
| 1473 | |
| 1474 | if (per_device_and_cpu_stats) |
| 1475 | show_device_and_cpu_stats(); |
| 1476 | |
| 1477 | flush_output(); |
| 1478 | return ret; |
| 1479 | } |