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