| 1 | /* |
| 2 | * block queue tracing parse application |
| 3 | * |
| 4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> |
| 5 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <unistd.h> |
| 25 | #include <stdio.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <getopt.h> |
| 30 | #include <errno.h> |
| 31 | #include <signal.h> |
| 32 | #include <locale.h> |
| 33 | #include <libgen.h> |
| 34 | |
| 35 | #include "blktrace.h" |
| 36 | #include "rbtree.h" |
| 37 | #include "jhash.h" |
| 38 | |
| 39 | static char blkparse_version[] = "0.99.3"; |
| 40 | |
| 41 | struct skip_info { |
| 42 | unsigned long start, end; |
| 43 | struct skip_info *prev, *next; |
| 44 | }; |
| 45 | |
| 46 | struct per_dev_info { |
| 47 | dev_t dev; |
| 48 | char *name; |
| 49 | |
| 50 | int backwards; |
| 51 | unsigned long long events; |
| 52 | unsigned long long first_reported_time; |
| 53 | unsigned long long last_reported_time; |
| 54 | unsigned long long last_read_time; |
| 55 | struct io_stats io_stats; |
| 56 | unsigned long skips; |
| 57 | unsigned long long seq_skips; |
| 58 | unsigned int max_depth[2]; |
| 59 | unsigned int cur_depth[2]; |
| 60 | |
| 61 | struct rb_root rb_track; |
| 62 | |
| 63 | int nfiles; |
| 64 | int ncpus; |
| 65 | |
| 66 | unsigned long *cpu_map; |
| 67 | unsigned int cpu_map_max; |
| 68 | |
| 69 | struct per_cpu_info *cpus; |
| 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * some duplicated effort here, we can unify this hash and the ppi hash later |
| 74 | */ |
| 75 | struct process_pid_map { |
| 76 | pid_t pid; |
| 77 | char comm[16]; |
| 78 | struct process_pid_map *hash_next, *list_next; |
| 79 | }; |
| 80 | |
| 81 | #define PPM_HASH_SHIFT (8) |
| 82 | #define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT) |
| 83 | #define PPM_HASH_MASK (PPM_HASH_SIZE - 1) |
| 84 | static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE]; |
| 85 | |
| 86 | struct per_process_info { |
| 87 | struct process_pid_map *ppm; |
| 88 | struct io_stats io_stats; |
| 89 | struct per_process_info *hash_next, *list_next; |
| 90 | int more_than_one; |
| 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 | #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT) |
| 102 | #define PPI_HASH_MASK (PPI_HASH_SIZE - 1) |
| 103 | static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE]; |
| 104 | static struct per_process_info *ppi_list; |
| 105 | static int ppi_list_entries; |
| 106 | |
| 107 | #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vV" |
| 108 | static struct option l_opts[] = { |
| 109 | { |
| 110 | .name = "act-mask", |
| 111 | .has_arg = required_argument, |
| 112 | .flag = NULL, |
| 113 | .val = 'a' |
| 114 | }, |
| 115 | { |
| 116 | .name = "set-mask", |
| 117 | .has_arg = required_argument, |
| 118 | .flag = NULL, |
| 119 | .val = 'A' |
| 120 | }, |
| 121 | { |
| 122 | .name = "batch", |
| 123 | .has_arg = required_argument, |
| 124 | .flag = NULL, |
| 125 | .val = 'b' |
| 126 | }, |
| 127 | { |
| 128 | .name = "input-directory", |
| 129 | .has_arg = required_argument, |
| 130 | .flag = NULL, |
| 131 | .val = 'D' |
| 132 | }, |
| 133 | { |
| 134 | .name = "dump-binary", |
| 135 | .has_arg = required_argument, |
| 136 | .flag = NULL, |
| 137 | .val = 'd' |
| 138 | }, |
| 139 | { |
| 140 | .name = "format", |
| 141 | .has_arg = required_argument, |
| 142 | .flag = NULL, |
| 143 | .val = 'f' |
| 144 | }, |
| 145 | { |
| 146 | .name = "format-spec", |
| 147 | .has_arg = required_argument, |
| 148 | .flag = NULL, |
| 149 | .val = 'F' |
| 150 | }, |
| 151 | { |
| 152 | .name = "hash-by-name", |
| 153 | .has_arg = no_argument, |
| 154 | .flag = NULL, |
| 155 | .val = 'h' |
| 156 | }, |
| 157 | { |
| 158 | .name = "input", |
| 159 | .has_arg = required_argument, |
| 160 | .flag = NULL, |
| 161 | .val = 'i' |
| 162 | }, |
| 163 | { |
| 164 | .name = "output", |
| 165 | .has_arg = required_argument, |
| 166 | .flag = NULL, |
| 167 | .val = 'o' |
| 168 | }, |
| 169 | { |
| 170 | .name = "no-text-output", |
| 171 | .has_arg = no_argument, |
| 172 | .flag = NULL, |
| 173 | .val = 'O' |
| 174 | }, |
| 175 | { |
| 176 | .name = "quiet", |
| 177 | .has_arg = no_argument, |
| 178 | .flag = NULL, |
| 179 | .val = 'q' |
| 180 | }, |
| 181 | { |
| 182 | .name = "per-program-stats", |
| 183 | .has_arg = no_argument, |
| 184 | .flag = NULL, |
| 185 | .val = 's' |
| 186 | }, |
| 187 | { |
| 188 | .name = "track-ios", |
| 189 | .has_arg = no_argument, |
| 190 | .flag = NULL, |
| 191 | .val = 't' |
| 192 | }, |
| 193 | { |
| 194 | .name = "stopwatch", |
| 195 | .has_arg = required_argument, |
| 196 | .flag = NULL, |
| 197 | .val = 'w' |
| 198 | }, |
| 199 | { |
| 200 | .name = "verbose", |
| 201 | .has_arg = no_argument, |
| 202 | .flag = NULL, |
| 203 | .val = 'v' |
| 204 | }, |
| 205 | { |
| 206 | .name = "version", |
| 207 | .has_arg = no_argument, |
| 208 | .flag = NULL, |
| 209 | .val = 'V' |
| 210 | }, |
| 211 | { |
| 212 | .name = NULL, |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | /* |
| 217 | * for sorting the displayed output |
| 218 | */ |
| 219 | struct trace { |
| 220 | struct blk_io_trace *bit; |
| 221 | struct rb_node rb_node; |
| 222 | struct trace *next; |
| 223 | unsigned long read_sequence; |
| 224 | }; |
| 225 | |
| 226 | static struct rb_root rb_sort_root; |
| 227 | static unsigned long rb_sort_entries; |
| 228 | |
| 229 | static struct trace *trace_list; |
| 230 | |
| 231 | /* |
| 232 | * allocation cache |
| 233 | */ |
| 234 | static struct blk_io_trace *bit_alloc_list; |
| 235 | static struct trace *t_alloc_list; |
| 236 | |
| 237 | /* |
| 238 | * for tracking individual ios |
| 239 | */ |
| 240 | struct io_track { |
| 241 | struct rb_node rb_node; |
| 242 | |
| 243 | struct process_pid_map *ppm; |
| 244 | __u64 sector; |
| 245 | unsigned long long allocation_time; |
| 246 | unsigned long long queue_time; |
| 247 | unsigned long long dispatch_time; |
| 248 | unsigned long long completion_time; |
| 249 | }; |
| 250 | |
| 251 | static int ndevices; |
| 252 | static struct per_dev_info *devices; |
| 253 | static char *get_dev_name(struct per_dev_info *, char *, int); |
| 254 | static int trace_rb_insert_last(struct per_dev_info *, struct trace *); |
| 255 | |
| 256 | FILE *ofp = NULL; |
| 257 | static char *output_name; |
| 258 | static char *input_dir; |
| 259 | |
| 260 | static unsigned long long genesis_time; |
| 261 | static unsigned long long last_allowed_time; |
| 262 | static unsigned long long stopwatch_start; /* start from zero by default */ |
| 263 | static unsigned long long stopwatch_end = -1ULL; /* "infinity" */ |
| 264 | static unsigned long read_sequence; |
| 265 | |
| 266 | static int per_process_stats; |
| 267 | static int per_device_and_cpu_stats = 1; |
| 268 | static int track_ios; |
| 269 | static int ppi_hash_by_pid = 1; |
| 270 | static int verbose; |
| 271 | static unsigned int act_mask = -1U; |
| 272 | static int stats_printed; |
| 273 | int data_is_native = -1; |
| 274 | |
| 275 | static FILE *dump_fp; |
| 276 | static char *dump_binary; |
| 277 | |
| 278 | static unsigned int t_alloc_cache; |
| 279 | static unsigned int bit_alloc_cache; |
| 280 | |
| 281 | #define RB_BATCH_DEFAULT (512) |
| 282 | static unsigned int rb_batch = RB_BATCH_DEFAULT; |
| 283 | |
| 284 | static int pipeline; |
| 285 | static char *pipename; |
| 286 | |
| 287 | static int text_output = 1; |
| 288 | |
| 289 | #define is_done() (*(volatile int *)(&done)) |
| 290 | static volatile int done; |
| 291 | |
| 292 | struct timespec abs_start_time; |
| 293 | static unsigned long long start_timestamp; |
| 294 | |
| 295 | #define JHASH_RANDOM (0x3af5f2ee) |
| 296 | |
| 297 | #define CPUS_PER_LONG (8 * sizeof(unsigned long)) |
| 298 | #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG) |
| 299 | #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1)) |
| 300 | |
| 301 | static void output_binary(void *buf, int len) |
| 302 | { |
| 303 | if (dump_binary) { |
| 304 | size_t n = fwrite(buf, len, 1, dump_fp); |
| 305 | if (n != 1) { |
| 306 | perror(dump_binary); |
| 307 | fclose(dump_fp); |
| 308 | dump_binary = NULL; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static void resize_cpu_info(struct per_dev_info *pdi, int cpu) |
| 314 | { |
| 315 | struct per_cpu_info *cpus = pdi->cpus; |
| 316 | int ncpus = pdi->ncpus; |
| 317 | int new_count = cpu + 1; |
| 318 | int new_space, size; |
| 319 | char *new_start; |
| 320 | |
| 321 | size = new_count * sizeof(struct per_cpu_info); |
| 322 | cpus = realloc(cpus, size); |
| 323 | if (!cpus) { |
| 324 | char name[20]; |
| 325 | fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n", |
| 326 | get_dev_name(pdi, name, sizeof(name)), size); |
| 327 | exit(1); |
| 328 | } |
| 329 | |
| 330 | new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info)); |
| 331 | new_space = (new_count - ncpus) * sizeof(struct per_cpu_info); |
| 332 | memset(new_start, 0, new_space); |
| 333 | |
| 334 | pdi->ncpus = new_count; |
| 335 | pdi->cpus = cpus; |
| 336 | |
| 337 | for (new_count = 0; new_count < pdi->ncpus; new_count++) { |
| 338 | struct per_cpu_info *pci = &pdi->cpus[new_count]; |
| 339 | |
| 340 | if (!pci->fd) { |
| 341 | pci->fd = -1; |
| 342 | memset(&pci->rb_last, 0, sizeof(pci->rb_last)); |
| 343 | pci->rb_last_entries = 0; |
| 344 | pci->last_sequence = -1; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu) |
| 350 | { |
| 351 | struct per_cpu_info *pci; |
| 352 | |
| 353 | if (cpu >= pdi->ncpus) |
| 354 | resize_cpu_info(pdi, cpu); |
| 355 | |
| 356 | pci = &pdi->cpus[cpu]; |
| 357 | pci->cpu = cpu; |
| 358 | return pci; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | static int resize_devices(char *name) |
| 363 | { |
| 364 | int size = (ndevices + 1) * sizeof(struct per_dev_info); |
| 365 | |
| 366 | devices = realloc(devices, size); |
| 367 | if (!devices) { |
| 368 | fprintf(stderr, "Out of memory, device %s (%d)\n", name, size); |
| 369 | return 1; |
| 370 | } |
| 371 | memset(&devices[ndevices], 0, sizeof(struct per_dev_info)); |
| 372 | devices[ndevices].name = name; |
| 373 | ndevices++; |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static struct per_dev_info *get_dev_info(dev_t dev) |
| 378 | { |
| 379 | struct per_dev_info *pdi; |
| 380 | int i; |
| 381 | |
| 382 | for (i = 0; i < ndevices; i++) { |
| 383 | if (!devices[i].dev) |
| 384 | devices[i].dev = dev; |
| 385 | if (devices[i].dev == dev) |
| 386 | return &devices[i]; |
| 387 | } |
| 388 | |
| 389 | if (resize_devices(NULL)) |
| 390 | return NULL; |
| 391 | |
| 392 | pdi = &devices[ndevices - 1]; |
| 393 | pdi->dev = dev; |
| 394 | pdi->first_reported_time = 0; |
| 395 | pdi->last_read_time = 0; |
| 396 | |
| 397 | return pdi; |
| 398 | } |
| 399 | |
| 400 | static void insert_skip(struct per_cpu_info *pci, unsigned long start, |
| 401 | unsigned long end) |
| 402 | { |
| 403 | struct skip_info *sip; |
| 404 | |
| 405 | for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) { |
| 406 | if (end == (sip->start - 1)) { |
| 407 | sip->start = start; |
| 408 | return; |
| 409 | } else if (start == (sip->end + 1)) { |
| 410 | sip->end = end; |
| 411 | return; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | sip = malloc(sizeof(struct skip_info)); |
| 416 | sip->start = start; |
| 417 | sip->end = end; |
| 418 | sip->prev = sip->next = NULL; |
| 419 | if (pci->skips_tail == NULL) |
| 420 | pci->skips_head = pci->skips_tail = sip; |
| 421 | else { |
| 422 | sip->prev = pci->skips_tail; |
| 423 | pci->skips_tail->next = sip; |
| 424 | pci->skips_tail = sip; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip) |
| 429 | { |
| 430 | if (sip->prev == NULL) { |
| 431 | if (sip->next == NULL) |
| 432 | pci->skips_head = pci->skips_tail = NULL; |
| 433 | else { |
| 434 | pci->skips_head = sip->next; |
| 435 | sip->next->prev = NULL; |
| 436 | } |
| 437 | } else if (sip->next == NULL) { |
| 438 | pci->skips_tail = sip->prev; |
| 439 | sip->prev->next = NULL; |
| 440 | } else { |
| 441 | sip->prev->next = sip->next; |
| 442 | sip->next->prev = sip->prev; |
| 443 | } |
| 444 | |
| 445 | sip->prev = sip->next = NULL; |
| 446 | free(sip); |
| 447 | } |
| 448 | |
| 449 | #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end)) |
| 450 | static int check_current_skips(struct per_cpu_info *pci, unsigned long seq) |
| 451 | { |
| 452 | struct skip_info *sip; |
| 453 | |
| 454 | for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) { |
| 455 | if (IN_SKIP(sip, seq)) { |
| 456 | if (sip->start == seq) { |
| 457 | if (sip->end == seq) |
| 458 | remove_sip(pci, sip); |
| 459 | else |
| 460 | sip->start += 1; |
| 461 | } else if (sip->end == seq) |
| 462 | sip->end -= 1; |
| 463 | else { |
| 464 | sip->end = seq - 1; |
| 465 | insert_skip(pci, seq + 1, sip->end); |
| 466 | } |
| 467 | return 1; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static void collect_pdi_skips(struct per_dev_info *pdi) |
| 475 | { |
| 476 | struct skip_info *sip; |
| 477 | int cpu; |
| 478 | |
| 479 | pdi->skips = 0; |
| 480 | pdi->seq_skips = 0; |
| 481 | |
| 482 | for (cpu = 0; cpu < pdi->ncpus; cpu++) { |
| 483 | struct per_cpu_info *pci = &pdi->cpus[cpu]; |
| 484 | |
| 485 | for (sip = pci->skips_head; sip != NULL; sip = sip->next) { |
| 486 | pdi->skips++; |
| 487 | pdi->seq_skips += (sip->end - sip->start + 1); |
| 488 | if (verbose) |
| 489 | fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n", |
| 490 | MAJOR(pdi->dev), MINOR(pdi->dev), |
| 491 | sip->start, sip->end); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu) |
| 497 | { |
| 498 | if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) { |
| 499 | int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1); |
| 500 | unsigned long *map = malloc(new_max / sizeof(long)); |
| 501 | |
| 502 | memset(map, 0, new_max / sizeof(long)); |
| 503 | |
| 504 | if (pdi->cpu_map) { |
| 505 | memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long)); |
| 506 | free(pdi->cpu_map); |
| 507 | } |
| 508 | |
| 509 | pdi->cpu_map = map; |
| 510 | pdi->cpu_map_max = new_max; |
| 511 | } |
| 512 | |
| 513 | pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu)); |
| 514 | } |
| 515 | |
| 516 | static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu) |
| 517 | { |
| 518 | pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu)); |
| 519 | } |
| 520 | |
| 521 | static inline int cpu_is_online(struct per_dev_info *pdi, int cpu) |
| 522 | { |
| 523 | return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0; |
| 524 | } |
| 525 | |
| 526 | static inline int ppm_hash_pid(pid_t pid) |
| 527 | { |
| 528 | return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK; |
| 529 | } |
| 530 | |
| 531 | static struct process_pid_map *find_ppm(pid_t pid) |
| 532 | { |
| 533 | const int hash_idx = ppm_hash_pid(pid); |
| 534 | struct process_pid_map *ppm; |
| 535 | |
| 536 | ppm = ppm_hash_table[hash_idx]; |
| 537 | while (ppm) { |
| 538 | if (ppm->pid == pid) |
| 539 | return ppm; |
| 540 | |
| 541 | ppm = ppm->hash_next; |
| 542 | } |
| 543 | |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | static void add_ppm_hash(pid_t pid, const char *name) |
| 548 | { |
| 549 | const int hash_idx = ppm_hash_pid(pid); |
| 550 | struct process_pid_map *ppm; |
| 551 | |
| 552 | ppm = find_ppm(pid); |
| 553 | if (!ppm) { |
| 554 | ppm = malloc(sizeof(*ppm)); |
| 555 | memset(ppm, 0, sizeof(*ppm)); |
| 556 | ppm->pid = pid; |
| 557 | strcpy(ppm->comm, name); |
| 558 | ppm->hash_next = ppm_hash_table[hash_idx]; |
| 559 | ppm_hash_table[hash_idx] = ppm; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static void handle_notify(struct blk_io_trace *bit) |
| 564 | { |
| 565 | void *payload = (caddr_t) bit + sizeof(*bit); |
| 566 | __u32 two32[2]; |
| 567 | |
| 568 | switch (bit->action) { |
| 569 | case BLK_TN_PROCESS: |
| 570 | add_ppm_hash(bit->pid, payload); |
| 571 | break; |
| 572 | |
| 573 | case BLK_TN_TIMESTAMP: |
| 574 | if (bit->pdu_len != sizeof(two32)) |
| 575 | return; |
| 576 | memcpy(two32, payload, sizeof(two32)); |
| 577 | if (!data_is_native) { |
| 578 | two32[0] = be32_to_cpu(two32[0]); |
| 579 | two32[1] = be32_to_cpu(two32[1]); |
| 580 | } |
| 581 | start_timestamp = bit->time; |
| 582 | abs_start_time.tv_sec = two32[0]; |
| 583 | abs_start_time.tv_nsec = two32[1]; |
| 584 | if (abs_start_time.tv_nsec < 0) { |
| 585 | abs_start_time.tv_sec--; |
| 586 | abs_start_time.tv_nsec += 1000000000; |
| 587 | } |
| 588 | |
| 589 | break; |
| 590 | |
| 591 | default: |
| 592 | /* Ignore unknown notify events */ |
| 593 | ; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | char *find_process_name(pid_t pid) |
| 598 | { |
| 599 | struct process_pid_map *ppm = find_ppm(pid); |
| 600 | |
| 601 | if (ppm) |
| 602 | return ppm->comm; |
| 603 | |
| 604 | return NULL; |
| 605 | } |
| 606 | |
| 607 | static inline int ppi_hash_pid(pid_t pid) |
| 608 | { |
| 609 | return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK; |
| 610 | } |
| 611 | |
| 612 | static inline int ppi_hash_name(const char *name) |
| 613 | { |
| 614 | return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK; |
| 615 | } |
| 616 | |
| 617 | static inline int ppi_hash(struct per_process_info *ppi) |
| 618 | { |
| 619 | struct process_pid_map *ppm = ppi->ppm; |
| 620 | |
| 621 | if (ppi_hash_by_pid) |
| 622 | return ppi_hash_pid(ppm->pid); |
| 623 | |
| 624 | return ppi_hash_name(ppm->comm); |
| 625 | } |
| 626 | |
| 627 | static inline void add_ppi_to_hash(struct per_process_info *ppi) |
| 628 | { |
| 629 | const int hash_idx = ppi_hash(ppi); |
| 630 | |
| 631 | ppi->hash_next = ppi_hash_table[hash_idx]; |
| 632 | ppi_hash_table[hash_idx] = ppi; |
| 633 | } |
| 634 | |
| 635 | static inline void add_ppi_to_list(struct per_process_info *ppi) |
| 636 | { |
| 637 | ppi->list_next = ppi_list; |
| 638 | ppi_list = ppi; |
| 639 | ppi_list_entries++; |
| 640 | } |
| 641 | |
| 642 | static struct per_process_info *find_ppi_by_name(char *name) |
| 643 | { |
| 644 | const int hash_idx = ppi_hash_name(name); |
| 645 | struct per_process_info *ppi; |
| 646 | |
| 647 | ppi = ppi_hash_table[hash_idx]; |
| 648 | while (ppi) { |
| 649 | struct process_pid_map *ppm = ppi->ppm; |
| 650 | |
| 651 | if (!strcmp(ppm->comm, name)) |
| 652 | return ppi; |
| 653 | |
| 654 | ppi = ppi->hash_next; |
| 655 | } |
| 656 | |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | static struct per_process_info *find_ppi_by_pid(pid_t pid) |
| 661 | { |
| 662 | const int hash_idx = ppi_hash_pid(pid); |
| 663 | struct per_process_info *ppi; |
| 664 | |
| 665 | ppi = ppi_hash_table[hash_idx]; |
| 666 | while (ppi) { |
| 667 | struct process_pid_map *ppm = ppi->ppm; |
| 668 | |
| 669 | if (ppm->pid == pid) |
| 670 | return ppi; |
| 671 | |
| 672 | ppi = ppi->hash_next; |
| 673 | } |
| 674 | |
| 675 | return NULL; |
| 676 | } |
| 677 | |
| 678 | static struct per_process_info *find_ppi(pid_t pid) |
| 679 | { |
| 680 | struct per_process_info *ppi; |
| 681 | char *name; |
| 682 | |
| 683 | if (ppi_hash_by_pid) |
| 684 | return find_ppi_by_pid(pid); |
| 685 | |
| 686 | name = find_process_name(pid); |
| 687 | if (!name) |
| 688 | return NULL; |
| 689 | |
| 690 | ppi = find_ppi_by_name(name); |
| 691 | if (ppi && ppi->ppm->pid != pid) |
| 692 | ppi->more_than_one = 1; |
| 693 | |
| 694 | return ppi; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * struct trace and blktrace allocation cache, we do potentially |
| 699 | * millions of mallocs for these structures while only using at most |
| 700 | * a few thousand at the time |
| 701 | */ |
| 702 | static inline void t_free(struct trace *t) |
| 703 | { |
| 704 | if (t_alloc_cache < 1024) { |
| 705 | t->next = t_alloc_list; |
| 706 | t_alloc_list = t; |
| 707 | t_alloc_cache++; |
| 708 | } else |
| 709 | free(t); |
| 710 | } |
| 711 | |
| 712 | static inline struct trace *t_alloc(void) |
| 713 | { |
| 714 | struct trace *t = t_alloc_list; |
| 715 | |
| 716 | if (t) { |
| 717 | t_alloc_list = t->next; |
| 718 | t_alloc_cache--; |
| 719 | return t; |
| 720 | } |
| 721 | |
| 722 | return malloc(sizeof(*t)); |
| 723 | } |
| 724 | |
| 725 | static inline void bit_free(struct blk_io_trace *bit) |
| 726 | { |
| 727 | if (bit_alloc_cache < 1024 && !bit->pdu_len) { |
| 728 | /* |
| 729 | * abuse a 64-bit field for a next pointer for the free item |
| 730 | */ |
| 731 | bit->time = (__u64) (unsigned long) bit_alloc_list; |
| 732 | bit_alloc_list = (struct blk_io_trace *) bit; |
| 733 | bit_alloc_cache++; |
| 734 | } else |
| 735 | free(bit); |
| 736 | } |
| 737 | |
| 738 | static inline struct blk_io_trace *bit_alloc(void) |
| 739 | { |
| 740 | struct blk_io_trace *bit = bit_alloc_list; |
| 741 | |
| 742 | if (bit) { |
| 743 | bit_alloc_list = (struct blk_io_trace *) (unsigned long) \ |
| 744 | bit->time; |
| 745 | bit_alloc_cache--; |
| 746 | return bit; |
| 747 | } |
| 748 | |
| 749 | return malloc(sizeof(*bit)); |
| 750 | } |
| 751 | |
| 752 | static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t) |
| 753 | { |
| 754 | struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu); |
| 755 | |
| 756 | rb_erase(&t->rb_node, &pci->rb_last); |
| 757 | pci->rb_last_entries--; |
| 758 | |
| 759 | bit_free(t->bit); |
| 760 | t_free(t); |
| 761 | } |
| 762 | |
| 763 | static void put_trace(struct per_dev_info *pdi, struct trace *t) |
| 764 | { |
| 765 | rb_erase(&t->rb_node, &rb_sort_root); |
| 766 | rb_sort_entries--; |
| 767 | |
| 768 | trace_rb_insert_last(pdi, t); |
| 769 | } |
| 770 | |
| 771 | static inline int trace_rb_insert(struct trace *t, struct rb_root *root) |
| 772 | { |
| 773 | struct rb_node **p = &root->rb_node; |
| 774 | struct rb_node *parent = NULL; |
| 775 | struct trace *__t; |
| 776 | |
| 777 | while (*p) { |
| 778 | parent = *p; |
| 779 | |
| 780 | __t = rb_entry(parent, struct trace, rb_node); |
| 781 | |
| 782 | if (t->bit->time < __t->bit->time) |
| 783 | p = &(*p)->rb_left; |
| 784 | else if (t->bit->time > __t->bit->time) |
| 785 | p = &(*p)->rb_right; |
| 786 | else if (t->bit->device < __t->bit->device) |
| 787 | p = &(*p)->rb_left; |
| 788 | else if (t->bit->device > __t->bit->device) |
| 789 | p = &(*p)->rb_right; |
| 790 | else if (t->bit->sequence < __t->bit->sequence) |
| 791 | p = &(*p)->rb_left; |
| 792 | else /* >= sequence */ |
| 793 | p = &(*p)->rb_right; |
| 794 | } |
| 795 | |
| 796 | rb_link_node(&t->rb_node, parent, p); |
| 797 | rb_insert_color(&t->rb_node, root); |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static inline int trace_rb_insert_sort(struct trace *t) |
| 802 | { |
| 803 | if (!trace_rb_insert(t, &rb_sort_root)) { |
| 804 | rb_sort_entries++; |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | return 1; |
| 809 | } |
| 810 | |
| 811 | static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t) |
| 812 | { |
| 813 | struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu); |
| 814 | |
| 815 | if (trace_rb_insert(t, &pci->rb_last)) |
| 816 | return 1; |
| 817 | |
| 818 | pci->rb_last_entries++; |
| 819 | |
| 820 | if (pci->rb_last_entries > rb_batch * pdi->nfiles) { |
| 821 | struct rb_node *n = rb_first(&pci->rb_last); |
| 822 | |
| 823 | t = rb_entry(n, struct trace, rb_node); |
| 824 | __put_trace_last(pdi, t); |
| 825 | } |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | static struct trace *trace_rb_find(dev_t device, unsigned long sequence, |
| 831 | struct rb_root *root, int order) |
| 832 | { |
| 833 | struct rb_node *n = root->rb_node; |
| 834 | struct rb_node *prev = NULL; |
| 835 | struct trace *__t; |
| 836 | |
| 837 | while (n) { |
| 838 | __t = rb_entry(n, struct trace, rb_node); |
| 839 | prev = n; |
| 840 | |
| 841 | if (device < __t->bit->device) |
| 842 | n = n->rb_left; |
| 843 | else if (device > __t->bit->device) |
| 844 | n = n->rb_right; |
| 845 | else if (sequence < __t->bit->sequence) |
| 846 | n = n->rb_left; |
| 847 | else if (sequence > __t->bit->sequence) |
| 848 | n = n->rb_right; |
| 849 | else |
| 850 | return __t; |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * hack - the list may not be sequence ordered because some |
| 855 | * events don't have sequence and time matched. so we end up |
| 856 | * being a little off in the rb lookup here, because we don't |
| 857 | * know the time we are looking for. compensate by browsing |
| 858 | * a little ahead from the last entry to find the match |
| 859 | */ |
| 860 | if (order && prev) { |
| 861 | int max = 5; |
| 862 | |
| 863 | while (((n = rb_next(prev)) != NULL) && max--) { |
| 864 | __t = rb_entry(n, struct trace, rb_node); |
| 865 | |
| 866 | if (__t->bit->device == device && |
| 867 | __t->bit->sequence == sequence) |
| 868 | return __t; |
| 869 | |
| 870 | prev = n; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | return NULL; |
| 875 | } |
| 876 | |
| 877 | static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi, |
| 878 | struct per_cpu_info *pci, |
| 879 | unsigned long seq) |
| 880 | { |
| 881 | return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0); |
| 882 | } |
| 883 | |
| 884 | static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot) |
| 885 | { |
| 886 | struct rb_node **p = &pdi->rb_track.rb_node; |
| 887 | struct rb_node *parent = NULL; |
| 888 | struct io_track *__iot; |
| 889 | |
| 890 | while (*p) { |
| 891 | parent = *p; |
| 892 | __iot = rb_entry(parent, struct io_track, rb_node); |
| 893 | |
| 894 | if (iot->sector < __iot->sector) |
| 895 | p = &(*p)->rb_left; |
| 896 | else if (iot->sector > __iot->sector) |
| 897 | p = &(*p)->rb_right; |
| 898 | else { |
| 899 | fprintf(stderr, |
| 900 | "sector alias (%Lu) on device %d,%d!\n", |
| 901 | (unsigned long long) iot->sector, |
| 902 | MAJOR(pdi->dev), MINOR(pdi->dev)); |
| 903 | return 1; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | rb_link_node(&iot->rb_node, parent, p); |
| 908 | rb_insert_color(&iot->rb_node, &pdi->rb_track); |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector) |
| 913 | { |
| 914 | struct rb_node *n = pdi->rb_track.rb_node; |
| 915 | struct io_track *__iot; |
| 916 | |
| 917 | while (n) { |
| 918 | __iot = rb_entry(n, struct io_track, rb_node); |
| 919 | |
| 920 | if (sector < __iot->sector) |
| 921 | n = n->rb_left; |
| 922 | else if (sector > __iot->sector) |
| 923 | n = n->rb_right; |
| 924 | else |
| 925 | return __iot; |
| 926 | } |
| 927 | |
| 928 | return NULL; |
| 929 | } |
| 930 | |
| 931 | static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid, |
| 932 | __u64 sector) |
| 933 | { |
| 934 | struct io_track *iot; |
| 935 | |
| 936 | iot = __find_track(pdi, sector); |
| 937 | if (!iot) { |
| 938 | iot = malloc(sizeof(*iot)); |
| 939 | iot->ppm = find_ppm(pid); |
| 940 | iot->sector = sector; |
| 941 | track_rb_insert(pdi, iot); |
| 942 | } |
| 943 | |
| 944 | return iot; |
| 945 | } |
| 946 | |
| 947 | static void log_track_frontmerge(struct per_dev_info *pdi, |
| 948 | struct blk_io_trace *t) |
| 949 | { |
| 950 | struct io_track *iot; |
| 951 | |
| 952 | if (!track_ios) |
| 953 | return; |
| 954 | |
| 955 | iot = __find_track(pdi, t->sector + t_sec(t)); |
| 956 | if (!iot) { |
| 957 | if (verbose) |
| 958 | fprintf(stderr, "merge not found for (%d,%d): %llu\n", |
| 959 | MAJOR(pdi->dev), MINOR(pdi->dev), |
| 960 | (unsigned long long) t->sector + t_sec(t)); |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | rb_erase(&iot->rb_node, &pdi->rb_track); |
| 965 | iot->sector -= t_sec(t); |
| 966 | track_rb_insert(pdi, iot); |
| 967 | } |
| 968 | |
| 969 | static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t) |
| 970 | { |
| 971 | struct io_track *iot; |
| 972 | |
| 973 | if (!track_ios) |
| 974 | return; |
| 975 | |
| 976 | iot = find_track(pdi, t->pid, t->sector); |
| 977 | iot->allocation_time = t->time; |
| 978 | } |
| 979 | |
| 980 | static inline int is_remapper(struct per_dev_info *pdi) |
| 981 | { |
| 982 | int major = MAJOR(pdi->dev); |
| 983 | |
| 984 | return (major == 253 || major == 9); |
| 985 | } |
| 986 | |
| 987 | /* |
| 988 | * for md/dm setups, the interesting cycle is Q -> C. So track queueing |
| 989 | * time here, as dispatch time |
| 990 | */ |
| 991 | static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t) |
| 992 | { |
| 993 | struct io_track *iot; |
| 994 | |
| 995 | if (!track_ios) |
| 996 | return; |
| 997 | if (!is_remapper(pdi)) |
| 998 | return; |
| 999 | |
| 1000 | iot = find_track(pdi, t->pid, t->sector); |
| 1001 | iot->dispatch_time = t->time; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * return time between rq allocation and insertion |
| 1006 | */ |
| 1007 | static unsigned long long log_track_insert(struct per_dev_info *pdi, |
| 1008 | struct blk_io_trace *t) |
| 1009 | { |
| 1010 | unsigned long long elapsed; |
| 1011 | struct io_track *iot; |
| 1012 | |
| 1013 | if (!track_ios) |
| 1014 | return -1; |
| 1015 | |
| 1016 | iot = find_track(pdi, t->pid, t->sector); |
| 1017 | iot->queue_time = t->time; |
| 1018 | |
| 1019 | if (!iot->allocation_time) |
| 1020 | return -1; |
| 1021 | |
| 1022 | elapsed = iot->queue_time - iot->allocation_time; |
| 1023 | |
| 1024 | if (per_process_stats) { |
| 1025 | struct per_process_info *ppi = find_ppi(iot->ppm->pid); |
| 1026 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 1027 | |
| 1028 | if (ppi && elapsed > ppi->longest_allocation_wait[w]) |
| 1029 | ppi->longest_allocation_wait[w] = elapsed; |
| 1030 | } |
| 1031 | |
| 1032 | return elapsed; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * return time between queue and issue |
| 1037 | */ |
| 1038 | static unsigned long long log_track_issue(struct per_dev_info *pdi, |
| 1039 | struct blk_io_trace *t) |
| 1040 | { |
| 1041 | unsigned long long elapsed; |
| 1042 | struct io_track *iot; |
| 1043 | |
| 1044 | if (!track_ios) |
| 1045 | return -1; |
| 1046 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) |
| 1047 | return -1; |
| 1048 | |
| 1049 | iot = __find_track(pdi, t->sector); |
| 1050 | if (!iot) { |
| 1051 | if (verbose) |
| 1052 | fprintf(stderr, "issue not found for (%d,%d): %llu\n", |
| 1053 | MAJOR(pdi->dev), MINOR(pdi->dev), |
| 1054 | (unsigned long long) t->sector); |
| 1055 | return -1; |
| 1056 | } |
| 1057 | |
| 1058 | iot->dispatch_time = t->time; |
| 1059 | elapsed = iot->dispatch_time - iot->queue_time; |
| 1060 | |
| 1061 | if (per_process_stats) { |
| 1062 | struct per_process_info *ppi = find_ppi(iot->ppm->pid); |
| 1063 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 1064 | |
| 1065 | if (ppi && elapsed > ppi->longest_dispatch_wait[w]) |
| 1066 | ppi->longest_dispatch_wait[w] = elapsed; |
| 1067 | } |
| 1068 | |
| 1069 | return elapsed; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * return time between dispatch and complete |
| 1074 | */ |
| 1075 | static unsigned long long log_track_complete(struct per_dev_info *pdi, |
| 1076 | struct blk_io_trace *t) |
| 1077 | { |
| 1078 | unsigned long long elapsed; |
| 1079 | struct io_track *iot; |
| 1080 | |
| 1081 | if (!track_ios) |
| 1082 | return -1; |
| 1083 | |
| 1084 | iot = __find_track(pdi, t->sector); |
| 1085 | if (!iot) { |
| 1086 | if (verbose) |
| 1087 | fprintf(stderr,"complete not found for (%d,%d): %llu\n", |
| 1088 | MAJOR(pdi->dev), MINOR(pdi->dev), |
| 1089 | (unsigned long long) t->sector); |
| 1090 | return -1; |
| 1091 | } |
| 1092 | |
| 1093 | iot->completion_time = t->time; |
| 1094 | elapsed = iot->completion_time - iot->dispatch_time; |
| 1095 | |
| 1096 | if (per_process_stats) { |
| 1097 | struct per_process_info *ppi = find_ppi(iot->ppm->pid); |
| 1098 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 1099 | |
| 1100 | if (ppi && elapsed > ppi->longest_completion_wait[w]) |
| 1101 | ppi->longest_completion_wait[w] = elapsed; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * kill the trace, we don't need it after completion |
| 1106 | */ |
| 1107 | rb_erase(&iot->rb_node, &pdi->rb_track); |
| 1108 | free(iot); |
| 1109 | |
| 1110 | return elapsed; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | static struct io_stats *find_process_io_stats(pid_t pid) |
| 1115 | { |
| 1116 | struct per_process_info *ppi = find_ppi(pid); |
| 1117 | |
| 1118 | if (!ppi) { |
| 1119 | ppi = malloc(sizeof(*ppi)); |
| 1120 | memset(ppi, 0, sizeof(*ppi)); |
| 1121 | ppi->ppm = find_ppm(pid); |
| 1122 | add_ppi_to_hash(ppi); |
| 1123 | add_ppi_to_list(ppi); |
| 1124 | } |
| 1125 | |
| 1126 | return &ppi->io_stats; |
| 1127 | } |
| 1128 | |
| 1129 | static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size) |
| 1130 | { |
| 1131 | if (pdi->name) |
| 1132 | snprintf(buffer, size, "%s", pdi->name); |
| 1133 | else |
| 1134 | snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev)); |
| 1135 | return buffer; |
| 1136 | } |
| 1137 | |
| 1138 | static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit) |
| 1139 | { |
| 1140 | unsigned long long this = bit->time; |
| 1141 | unsigned long long last = pdi->last_reported_time; |
| 1142 | |
| 1143 | pdi->backwards = (this < last) ? 'B' : ' '; |
| 1144 | pdi->last_reported_time = this; |
| 1145 | } |
| 1146 | |
| 1147 | static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t, |
| 1148 | int rw) |
| 1149 | { |
| 1150 | if (rw) { |
| 1151 | ios->mwrites++; |
| 1152 | ios->mwrite_kb += t_kb(t); |
| 1153 | } else { |
| 1154 | ios->mreads++; |
| 1155 | ios->mread_kb += t_kb(t); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 1160 | int rw) |
| 1161 | { |
| 1162 | __account_m(&pci->io_stats, t, rw); |
| 1163 | |
| 1164 | if (per_process_stats) { |
| 1165 | struct io_stats *ios = find_process_io_stats(t->pid); |
| 1166 | |
| 1167 | __account_m(ios, t, rw); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t, |
| 1172 | int rw) |
| 1173 | { |
| 1174 | if (rw) { |
| 1175 | ios->qwrites++; |
| 1176 | ios->qwrite_kb += t_kb(t); |
| 1177 | } else { |
| 1178 | ios->qreads++; |
| 1179 | ios->qread_kb += t_kb(t); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | static inline void account_queue(struct blk_io_trace *t, |
| 1184 | struct per_cpu_info *pci, int rw) |
| 1185 | { |
| 1186 | __account_queue(&pci->io_stats, t, rw); |
| 1187 | |
| 1188 | if (per_process_stats) { |
| 1189 | struct io_stats *ios = find_process_io_stats(t->pid); |
| 1190 | |
| 1191 | __account_queue(ios, t, rw); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | static inline void __account_c(struct io_stats *ios, int rw, int bytes) |
| 1196 | { |
| 1197 | if (rw) { |
| 1198 | ios->cwrites++; |
| 1199 | ios->cwrite_kb += bytes >> 10; |
| 1200 | } else { |
| 1201 | ios->creads++; |
| 1202 | ios->cread_kb += bytes >> 10; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 1207 | int rw, int bytes) |
| 1208 | { |
| 1209 | __account_c(&pci->io_stats, rw, bytes); |
| 1210 | |
| 1211 | if (per_process_stats) { |
| 1212 | struct io_stats *ios = find_process_io_stats(t->pid); |
| 1213 | |
| 1214 | __account_c(ios, rw, bytes); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | static inline void __account_issue(struct io_stats *ios, int rw, |
| 1219 | unsigned int bytes) |
| 1220 | { |
| 1221 | if (rw) { |
| 1222 | ios->iwrites++; |
| 1223 | ios->iwrite_kb += bytes >> 10; |
| 1224 | } else { |
| 1225 | ios->ireads++; |
| 1226 | ios->iread_kb += bytes >> 10; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | static inline void account_issue(struct blk_io_trace *t, |
| 1231 | struct per_cpu_info *pci, int rw) |
| 1232 | { |
| 1233 | __account_issue(&pci->io_stats, rw, t->bytes); |
| 1234 | |
| 1235 | if (per_process_stats) { |
| 1236 | struct io_stats *ios = find_process_io_stats(t->pid); |
| 1237 | |
| 1238 | __account_issue(ios, rw, t->bytes); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | static inline void __account_unplug(struct io_stats *ios, int timer) |
| 1243 | { |
| 1244 | if (timer) |
| 1245 | ios->timer_unplugs++; |
| 1246 | else |
| 1247 | ios->io_unplugs++; |
| 1248 | } |
| 1249 | |
| 1250 | static inline void account_unplug(struct blk_io_trace *t, |
| 1251 | struct per_cpu_info *pci, int timer) |
| 1252 | { |
| 1253 | __account_unplug(&pci->io_stats, timer); |
| 1254 | |
| 1255 | if (per_process_stats) { |
| 1256 | struct io_stats *ios = find_process_io_stats(t->pid); |
| 1257 | |
| 1258 | __account_unplug(ios, timer); |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | static inline void __account_requeue(struct io_stats *ios, |
| 1263 | struct blk_io_trace *t, int rw) |
| 1264 | { |
| 1265 | if (rw) { |
| 1266 | ios->wrqueue++; |
| 1267 | ios->iwrite_kb -= t_kb(t); |
| 1268 | } else { |
| 1269 | ios->rrqueue++; |
| 1270 | ios->iread_kb -= t_kb(t); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | static inline void account_requeue(struct blk_io_trace *t, |
| 1275 | struct per_cpu_info *pci, int rw) |
| 1276 | { |
| 1277 | __account_requeue(&pci->io_stats, t, rw); |
| 1278 | |
| 1279 | if (per_process_stats) { |
| 1280 | struct io_stats *ios = find_process_io_stats(t->pid); |
| 1281 | |
| 1282 | __account_requeue(ios, t, rw); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci, |
| 1287 | struct blk_io_trace *t, char *act) |
| 1288 | { |
| 1289 | process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL); |
| 1290 | } |
| 1291 | |
| 1292 | static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci, |
| 1293 | struct blk_io_trace *t, char *act) |
| 1294 | { |
| 1295 | process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL); |
| 1296 | } |
| 1297 | |
| 1298 | static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1299 | char *act) |
| 1300 | { |
| 1301 | process_fmt(act, pci, t, -1, 0, NULL); |
| 1302 | } |
| 1303 | |
| 1304 | static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci, |
| 1305 | struct blk_io_trace *t, char *act) |
| 1306 | { |
| 1307 | process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL); |
| 1308 | } |
| 1309 | |
| 1310 | static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci, |
| 1311 | struct blk_io_trace *t, char *act) |
| 1312 | { |
| 1313 | if (act[0] == 'F') |
| 1314 | log_track_frontmerge(pdi, t); |
| 1315 | |
| 1316 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1317 | } |
| 1318 | |
| 1319 | static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1320 | char *act) |
| 1321 | { |
| 1322 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1323 | } |
| 1324 | |
| 1325 | static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1326 | char *act) |
| 1327 | { |
| 1328 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1329 | } |
| 1330 | |
| 1331 | static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1332 | char *act) |
| 1333 | { |
| 1334 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1335 | } |
| 1336 | |
| 1337 | static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t, |
| 1338 | char *act) |
| 1339 | { |
| 1340 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
| 1341 | } |
| 1342 | |
| 1343 | static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) |
| 1344 | { |
| 1345 | unsigned char *buf = (unsigned char *) t + sizeof(*t); |
| 1346 | |
| 1347 | process_fmt(act, pci, t, -1ULL, t->pdu_len, buf); |
| 1348 | } |
| 1349 | |
| 1350 | static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci) |
| 1351 | { |
| 1352 | int act = t->action & 0xffff; |
| 1353 | |
| 1354 | switch (act) { |
| 1355 | case __BLK_TA_QUEUE: |
| 1356 | log_generic(pci, t, "Q"); |
| 1357 | break; |
| 1358 | case __BLK_TA_GETRQ: |
| 1359 | log_generic(pci, t, "G"); |
| 1360 | break; |
| 1361 | case __BLK_TA_SLEEPRQ: |
| 1362 | log_generic(pci, t, "S"); |
| 1363 | break; |
| 1364 | case __BLK_TA_REQUEUE: |
| 1365 | log_generic(pci, t, "R"); |
| 1366 | break; |
| 1367 | case __BLK_TA_ISSUE: |
| 1368 | log_pc(pci, t, "D"); |
| 1369 | break; |
| 1370 | case __BLK_TA_COMPLETE: |
| 1371 | log_pc(pci, t, "C"); |
| 1372 | break; |
| 1373 | case __BLK_TA_INSERT: |
| 1374 | log_pc(pci, t, "I"); |
| 1375 | break; |
| 1376 | default: |
| 1377 | fprintf(stderr, "Bad pc action %x\n", act); |
| 1378 | break; |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi, |
| 1383 | struct per_cpu_info *pci) |
| 1384 | { |
| 1385 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
| 1386 | int act = t->action & 0xffff; |
| 1387 | |
| 1388 | switch (act) { |
| 1389 | case __BLK_TA_QUEUE: |
| 1390 | log_track_queue(pdi, t); |
| 1391 | account_queue(t, pci, w); |
| 1392 | log_queue(pci, t, "Q"); |
| 1393 | break; |
| 1394 | case __BLK_TA_INSERT: |
| 1395 | log_insert(pdi, pci, t, "I"); |
| 1396 | break; |
| 1397 | case __BLK_TA_BACKMERGE: |
| 1398 | account_m(t, pci, w); |
| 1399 | log_merge(pdi, pci, t, "M"); |
| 1400 | break; |
| 1401 | case __BLK_TA_FRONTMERGE: |
| 1402 | account_m(t, pci, w); |
| 1403 | log_merge(pdi, pci, t, "F"); |
| 1404 | break; |
| 1405 | case __BLK_TA_GETRQ: |
| 1406 | log_track_getrq(pdi, t); |
| 1407 | log_generic(pci, t, "G"); |
| 1408 | break; |
| 1409 | case __BLK_TA_SLEEPRQ: |
| 1410 | log_generic(pci, t, "S"); |
| 1411 | break; |
| 1412 | case __BLK_TA_REQUEUE: |
| 1413 | /* |
| 1414 | * can happen if we miss traces, don't let it go |
| 1415 | * below zero |
| 1416 | */ |
| 1417 | if (pdi->cur_depth[w]) |
| 1418 | pdi->cur_depth[w]--; |
| 1419 | account_requeue(t, pci, w); |
| 1420 | log_queue(pci, t, "R"); |
| 1421 | break; |
| 1422 | case __BLK_TA_ISSUE: |
| 1423 | account_issue(t, pci, w); |
| 1424 | pdi->cur_depth[w]++; |
| 1425 | if (pdi->cur_depth[w] > pdi->max_depth[w]) |
| 1426 | pdi->max_depth[w] = pdi->cur_depth[w]; |
| 1427 | log_issue(pdi, pci, t, "D"); |
| 1428 | break; |
| 1429 | case __BLK_TA_COMPLETE: |
| 1430 | if (pdi->cur_depth[w]) |
| 1431 | pdi->cur_depth[w]--; |
| 1432 | account_c(t, pci, w, t->bytes); |
| 1433 | log_complete(pdi, pci, t, "C"); |
| 1434 | break; |
| 1435 | case __BLK_TA_PLUG: |
| 1436 | log_action(pci, t, "P"); |
| 1437 | break; |
| 1438 | case __BLK_TA_UNPLUG_IO: |
| 1439 | account_unplug(t, pci, 0); |
| 1440 | log_unplug(pci, t, "U"); |
| 1441 | break; |
| 1442 | case __BLK_TA_UNPLUG_TIMER: |
| 1443 | account_unplug(t, pci, 1); |
| 1444 | log_unplug(pci, t, "UT"); |
| 1445 | break; |
| 1446 | case __BLK_TA_SPLIT: |
| 1447 | log_split(pci, t, "X"); |
| 1448 | break; |
| 1449 | case __BLK_TA_BOUNCE: |
| 1450 | log_generic(pci, t, "B"); |
| 1451 | break; |
| 1452 | case __BLK_TA_REMAP: |
| 1453 | log_generic(pci, t, "A"); |
| 1454 | break; |
| 1455 | default: |
| 1456 | fprintf(stderr, "Bad fs action %x\n", t->action); |
| 1457 | break; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci, |
| 1462 | struct per_dev_info *pdi) |
| 1463 | { |
| 1464 | if (text_output) { |
| 1465 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) |
| 1466 | dump_trace_pc(t, pci); |
| 1467 | else |
| 1468 | dump_trace_fs(t, pdi, pci); |
| 1469 | } |
| 1470 | |
| 1471 | if (!pdi->events) |
| 1472 | pdi->first_reported_time = t->time; |
| 1473 | |
| 1474 | pdi->events++; |
| 1475 | |
| 1476 | output_binary(t, sizeof(*t) + t->pdu_len); |
| 1477 | } |
| 1478 | |
| 1479 | /* |
| 1480 | * print in a proper way, not too small and not too big. if more than |
| 1481 | * 1000,000K, turn into M and so on |
| 1482 | */ |
| 1483 | static char *size_cnv(char *dst, unsigned long long num, int in_kb) |
| 1484 | { |
| 1485 | char suff[] = { '\0', 'K', 'M', 'G', 'P' }; |
| 1486 | unsigned int i = 0; |
| 1487 | |
| 1488 | if (in_kb) |
| 1489 | i++; |
| 1490 | |
| 1491 | while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) { |
| 1492 | i++; |
| 1493 | num /= 1000; |
| 1494 | } |
| 1495 | |
| 1496 | sprintf(dst, "%'8Lu%c", num, suff[i]); |
| 1497 | return dst; |
| 1498 | } |
| 1499 | |
| 1500 | static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios, |
| 1501 | char *msg) |
| 1502 | { |
| 1503 | static char x[256], y[256]; |
| 1504 | |
| 1505 | fprintf(ofp, "%s\n", msg); |
| 1506 | |
| 1507 | fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1)); |
| 1508 | fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1)); |
| 1509 | fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1)); |
| 1510 | fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1)); |
| 1511 | fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0)); |
| 1512 | fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0)); |
| 1513 | fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1)); |
| 1514 | fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1)); |
| 1515 | fprintf(ofp, " Read Merges: %s, %siB\t", size_cnv(x, ios->mreads, 0), size_cnv(y, ios->mread_kb, 1)); |
| 1516 | fprintf(ofp, " Write Merges: %s, %siB\n", size_cnv(x, ios->mwrites, 0), size_cnv(y, ios->mwrite_kb, 1)); |
| 1517 | if (pdi) { |
| 1518 | fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' '); |
| 1519 | fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]); |
| 1520 | } |
| 1521 | fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' '); |
| 1522 | fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs); |
| 1523 | } |
| 1524 | |
| 1525 | static void dump_wait_stats(struct per_process_info *ppi) |
| 1526 | { |
| 1527 | unsigned long rawait = ppi->longest_allocation_wait[0] / 1000; |
| 1528 | unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000; |
| 1529 | unsigned long rcwait = ppi->longest_completion_wait[0] / 1000; |
| 1530 | unsigned long wawait = ppi->longest_allocation_wait[1] / 1000; |
| 1531 | unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000; |
| 1532 | unsigned long wcwait = ppi->longest_completion_wait[1] / 1000; |
| 1533 | |
| 1534 | fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' '); |
| 1535 | fprintf(ofp, " Allocation wait: %'8lu\n", wawait); |
| 1536 | fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' '); |
| 1537 | fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait); |
| 1538 | fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' '); |
| 1539 | fprintf(ofp, " Completion wait: %'8lu\n", wcwait); |
| 1540 | } |
| 1541 | |
| 1542 | static int ppi_name_compare(const void *p1, const void *p2) |
| 1543 | { |
| 1544 | struct per_process_info *ppi1 = *((struct per_process_info **) p1); |
| 1545 | struct per_process_info *ppi2 = *((struct per_process_info **) p2); |
| 1546 | int res; |
| 1547 | |
| 1548 | res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm); |
| 1549 | if (!res) |
| 1550 | res = ppi1->ppm->pid > ppi2->ppm->pid; |
| 1551 | |
| 1552 | return res; |
| 1553 | } |
| 1554 | |
| 1555 | static void sort_process_list(void) |
| 1556 | { |
| 1557 | struct per_process_info **ppis; |
| 1558 | struct per_process_info *ppi; |
| 1559 | int i = 0; |
| 1560 | |
| 1561 | ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *)); |
| 1562 | |
| 1563 | ppi = ppi_list; |
| 1564 | while (ppi) { |
| 1565 | ppis[i++] = ppi; |
| 1566 | ppi = ppi->list_next; |
| 1567 | } |
| 1568 | |
| 1569 | qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare); |
| 1570 | |
| 1571 | i = ppi_list_entries - 1; |
| 1572 | ppi_list = NULL; |
| 1573 | while (i >= 0) { |
| 1574 | ppi = ppis[i]; |
| 1575 | |
| 1576 | ppi->list_next = ppi_list; |
| 1577 | ppi_list = ppi; |
| 1578 | i--; |
| 1579 | } |
| 1580 | |
| 1581 | free(ppis); |
| 1582 | } |
| 1583 | |
| 1584 | static void show_process_stats(void) |
| 1585 | { |
| 1586 | struct per_process_info *ppi; |
| 1587 | |
| 1588 | sort_process_list(); |
| 1589 | |
| 1590 | ppi = ppi_list; |
| 1591 | while (ppi) { |
| 1592 | struct process_pid_map *ppm = ppi->ppm; |
| 1593 | char name[64]; |
| 1594 | |
| 1595 | if (ppi->more_than_one) |
| 1596 | sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid); |
| 1597 | else |
| 1598 | sprintf(name, "%s (%u)", ppm->comm, ppm->pid); |
| 1599 | |
| 1600 | dump_io_stats(NULL, &ppi->io_stats, name); |
| 1601 | dump_wait_stats(ppi); |
| 1602 | ppi = ppi->list_next; |
| 1603 | } |
| 1604 | |
| 1605 | fprintf(ofp, "\n"); |
| 1606 | } |
| 1607 | |
| 1608 | static void show_device_and_cpu_stats(void) |
| 1609 | { |
| 1610 | struct per_dev_info *pdi; |
| 1611 | struct per_cpu_info *pci; |
| 1612 | struct io_stats total, *ios; |
| 1613 | unsigned long long rrate, wrate, msec; |
| 1614 | int i, j, pci_events; |
| 1615 | char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3]; |
| 1616 | char name[32]; |
| 1617 | double ratio; |
| 1618 | |
| 1619 | for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { |
| 1620 | |
| 1621 | memset(&total, 0, sizeof(total)); |
| 1622 | pci_events = 0; |
| 1623 | |
| 1624 | if (i > 0) |
| 1625 | fprintf(ofp, "\n"); |
| 1626 | |
| 1627 | for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) { |
| 1628 | if (!pci->nelems) |
| 1629 | continue; |
| 1630 | |
| 1631 | ios = &pci->io_stats; |
| 1632 | total.qreads += ios->qreads; |
| 1633 | total.qwrites += ios->qwrites; |
| 1634 | total.creads += ios->creads; |
| 1635 | total.cwrites += ios->cwrites; |
| 1636 | total.mreads += ios->mreads; |
| 1637 | total.mwrites += ios->mwrites; |
| 1638 | total.ireads += ios->ireads; |
| 1639 | total.iwrites += ios->iwrites; |
| 1640 | total.rrqueue += ios->rrqueue; |
| 1641 | total.wrqueue += ios->wrqueue; |
| 1642 | total.qread_kb += ios->qread_kb; |
| 1643 | total.qwrite_kb += ios->qwrite_kb; |
| 1644 | total.cread_kb += ios->cread_kb; |
| 1645 | total.cwrite_kb += ios->cwrite_kb; |
| 1646 | total.iread_kb += ios->iread_kb; |
| 1647 | total.iwrite_kb += ios->iwrite_kb; |
| 1648 | total.mread_kb += ios->mread_kb; |
| 1649 | total.mwrite_kb += ios->mwrite_kb; |
| 1650 | total.timer_unplugs += ios->timer_unplugs; |
| 1651 | total.io_unplugs += ios->io_unplugs; |
| 1652 | |
| 1653 | snprintf(line, sizeof(line) - 1, "CPU%d (%s):", |
| 1654 | j, get_dev_name(pdi, name, sizeof(name))); |
| 1655 | dump_io_stats(pdi, ios, line); |
| 1656 | pci_events++; |
| 1657 | } |
| 1658 | |
| 1659 | if (pci_events > 1) { |
| 1660 | fprintf(ofp, "\n"); |
| 1661 | snprintf(line, sizeof(line) - 1, "Total (%s):", |
| 1662 | get_dev_name(pdi, name, sizeof(name))); |
| 1663 | dump_io_stats(NULL, &total, line); |
| 1664 | } |
| 1665 | |
| 1666 | wrate = rrate = 0; |
| 1667 | msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000; |
| 1668 | if (msec) { |
| 1669 | rrate = 1000 * total.cread_kb / msec; |
| 1670 | wrate = 1000 * total.cwrite_kb / msec; |
| 1671 | } |
| 1672 | |
| 1673 | fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n", |
| 1674 | rrate, wrate); |
| 1675 | fprintf(ofp, "Events (%s): %'Lu entries\n", |
| 1676 | get_dev_name(pdi, line, sizeof(line)), pdi->events); |
| 1677 | |
| 1678 | collect_pdi_skips(pdi); |
| 1679 | if (!pdi->skips && !pdi->events) |
| 1680 | ratio = 0.0; |
| 1681 | else |
| 1682 | ratio = 100.0 * ((double)pdi->seq_skips / |
| 1683 | (double)(pdi->events + pdi->seq_skips)); |
| 1684 | fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n", |
| 1685 | pdi->skips, pdi->seq_skips, ratio); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | static void find_genesis(void) |
| 1690 | { |
| 1691 | struct trace *t = trace_list; |
| 1692 | |
| 1693 | genesis_time = -1ULL; |
| 1694 | while (t != NULL) { |
| 1695 | if (t->bit->time < genesis_time) |
| 1696 | genesis_time = t->bit->time; |
| 1697 | |
| 1698 | t = t->next; |
| 1699 | } |
| 1700 | |
| 1701 | /* The time stamp record will usually be the first |
| 1702 | * record in the trace, but not always. |
| 1703 | */ |
| 1704 | if (start_timestamp |
| 1705 | && start_timestamp != genesis_time) { |
| 1706 | long delta = genesis_time - start_timestamp; |
| 1707 | |
| 1708 | abs_start_time.tv_sec += SECONDS(delta); |
| 1709 | abs_start_time.tv_nsec += NANO_SECONDS(delta); |
| 1710 | if (abs_start_time.tv_nsec < 0) { |
| 1711 | abs_start_time.tv_nsec += 1000000000; |
| 1712 | abs_start_time.tv_sec -= 1; |
| 1713 | } else |
| 1714 | if (abs_start_time.tv_nsec > 1000000000) { |
| 1715 | abs_start_time.tv_nsec -= 1000000000; |
| 1716 | abs_start_time.tv_sec += 1; |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | static inline int check_stopwatch(struct blk_io_trace *bit) |
| 1722 | { |
| 1723 | if (bit->time < stopwatch_end && |
| 1724 | bit->time >= stopwatch_start) |
| 1725 | return 0; |
| 1726 | |
| 1727 | return 1; |
| 1728 | } |
| 1729 | |
| 1730 | /* |
| 1731 | * return youngest entry read |
| 1732 | */ |
| 1733 | static int sort_entries(unsigned long long *youngest) |
| 1734 | { |
| 1735 | struct per_dev_info *pdi = NULL; |
| 1736 | struct per_cpu_info *pci = NULL; |
| 1737 | struct trace *t; |
| 1738 | |
| 1739 | if (!genesis_time) |
| 1740 | find_genesis(); |
| 1741 | |
| 1742 | *youngest = 0; |
| 1743 | while ((t = trace_list) != NULL) { |
| 1744 | struct blk_io_trace *bit = t->bit; |
| 1745 | |
| 1746 | trace_list = t->next; |
| 1747 | |
| 1748 | bit->time -= genesis_time; |
| 1749 | |
| 1750 | if (bit->time < *youngest || !*youngest) |
| 1751 | *youngest = bit->time; |
| 1752 | |
| 1753 | if (!pdi || pdi->dev != bit->device) { |
| 1754 | pdi = get_dev_info(bit->device); |
| 1755 | pci = NULL; |
| 1756 | } |
| 1757 | |
| 1758 | if (!pci || pci->cpu != bit->cpu) |
| 1759 | pci = get_cpu_info(pdi, bit->cpu); |
| 1760 | |
| 1761 | if (bit->sequence < pci->smallest_seq_read) |
| 1762 | pci->smallest_seq_read = bit->sequence; |
| 1763 | |
| 1764 | if (check_stopwatch(bit)) { |
| 1765 | bit_free(bit); |
| 1766 | t_free(t); |
| 1767 | continue; |
| 1768 | } |
| 1769 | |
| 1770 | if (trace_rb_insert_sort(t)) |
| 1771 | return -1; |
| 1772 | } |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * to continue, we must have traces from all online cpus in the tree |
| 1779 | */ |
| 1780 | static int check_cpu_map(struct per_dev_info *pdi) |
| 1781 | { |
| 1782 | unsigned long *cpu_map; |
| 1783 | struct rb_node *n; |
| 1784 | struct trace *__t; |
| 1785 | unsigned int i; |
| 1786 | int ret, cpu; |
| 1787 | |
| 1788 | /* |
| 1789 | * create a map of the cpus we have traces for |
| 1790 | */ |
| 1791 | cpu_map = malloc(pdi->cpu_map_max / sizeof(long)); |
| 1792 | n = rb_first(&rb_sort_root); |
| 1793 | while (n) { |
| 1794 | __t = rb_entry(n, struct trace, rb_node); |
| 1795 | cpu = __t->bit->cpu; |
| 1796 | |
| 1797 | cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu)); |
| 1798 | n = rb_next(n); |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * we can't continue if pdi->cpu_map has entries set that we don't |
| 1803 | * have in the sort rbtree. the opposite is not a problem, though |
| 1804 | */ |
| 1805 | ret = 0; |
| 1806 | for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) { |
| 1807 | if (pdi->cpu_map[i] & ~(cpu_map[i])) { |
| 1808 | ret = 1; |
| 1809 | break; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | free(cpu_map); |
| 1814 | return ret; |
| 1815 | } |
| 1816 | |
| 1817 | static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force) |
| 1818 | { |
| 1819 | struct blk_io_trace *bit = t->bit; |
| 1820 | unsigned long expected_sequence; |
| 1821 | struct per_cpu_info *pci; |
| 1822 | struct trace *__t; |
| 1823 | |
| 1824 | pci = get_cpu_info(pdi, bit->cpu); |
| 1825 | expected_sequence = pci->last_sequence + 1; |
| 1826 | |
| 1827 | if (!expected_sequence) { |
| 1828 | /* |
| 1829 | * 1 should be the first entry, just allow it |
| 1830 | */ |
| 1831 | if (bit->sequence == 1) |
| 1832 | return 0; |
| 1833 | if (bit->sequence == pci->smallest_seq_read) |
| 1834 | return 0; |
| 1835 | |
| 1836 | return check_cpu_map(pdi); |
| 1837 | } |
| 1838 | |
| 1839 | if (bit->sequence == expected_sequence) |
| 1840 | return 0; |
| 1841 | |
| 1842 | /* |
| 1843 | * we may not have seen that sequence yet. if we are not doing |
| 1844 | * the final run, break and wait for more entries. |
| 1845 | */ |
| 1846 | if (expected_sequence < pci->smallest_seq_read) { |
| 1847 | __t = trace_rb_find_last(pdi, pci, expected_sequence); |
| 1848 | if (!__t) |
| 1849 | goto skip; |
| 1850 | |
| 1851 | __put_trace_last(pdi, __t); |
| 1852 | return 0; |
| 1853 | } else if (!force) { |
| 1854 | return 1; |
| 1855 | } else { |
| 1856 | skip: |
| 1857 | if (check_current_skips(pci, bit->sequence)) |
| 1858 | return 0; |
| 1859 | |
| 1860 | if (expected_sequence < bit->sequence) |
| 1861 | insert_skip(pci, expected_sequence, bit->sequence - 1); |
| 1862 | return 0; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | static void show_entries_rb(int force) |
| 1867 | { |
| 1868 | struct per_dev_info *pdi = NULL; |
| 1869 | struct per_cpu_info *pci = NULL; |
| 1870 | struct blk_io_trace *bit; |
| 1871 | struct rb_node *n; |
| 1872 | struct trace *t; |
| 1873 | |
| 1874 | while ((n = rb_first(&rb_sort_root)) != NULL) { |
| 1875 | if (is_done() && !force && !pipeline) |
| 1876 | break; |
| 1877 | |
| 1878 | t = rb_entry(n, struct trace, rb_node); |
| 1879 | bit = t->bit; |
| 1880 | |
| 1881 | if (read_sequence - t->read_sequence < 1 && !force) |
| 1882 | break; |
| 1883 | |
| 1884 | if (!pdi || pdi->dev != bit->device) { |
| 1885 | pdi = get_dev_info(bit->device); |
| 1886 | pci = NULL; |
| 1887 | } |
| 1888 | |
| 1889 | if (!pdi) { |
| 1890 | fprintf(stderr, "Unknown device ID? (%d,%d)\n", |
| 1891 | MAJOR(bit->device), MINOR(bit->device)); |
| 1892 | break; |
| 1893 | } |
| 1894 | |
| 1895 | if (check_sequence(pdi, t, force)) |
| 1896 | break; |
| 1897 | |
| 1898 | if (!force && bit->time > last_allowed_time) |
| 1899 | break; |
| 1900 | |
| 1901 | check_time(pdi, bit); |
| 1902 | |
| 1903 | if (!pci || pci->cpu != bit->cpu) |
| 1904 | pci = get_cpu_info(pdi, bit->cpu); |
| 1905 | |
| 1906 | pci->last_sequence = bit->sequence; |
| 1907 | |
| 1908 | pci->nelems++; |
| 1909 | |
| 1910 | if (bit->action & (act_mask << BLK_TC_SHIFT)) |
| 1911 | dump_trace(bit, pci, pdi); |
| 1912 | |
| 1913 | put_trace(pdi, t); |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock) |
| 1918 | { |
| 1919 | int ret, bytes_left, fl; |
| 1920 | void *p; |
| 1921 | |
| 1922 | if (block != *fdblock) { |
| 1923 | fl = fcntl(fd, F_GETFL); |
| 1924 | |
| 1925 | if (!block) { |
| 1926 | *fdblock = 0; |
| 1927 | fcntl(fd, F_SETFL, fl | O_NONBLOCK); |
| 1928 | } else { |
| 1929 | *fdblock = 1; |
| 1930 | fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | bytes_left = bytes; |
| 1935 | p = buffer; |
| 1936 | while (bytes_left > 0) { |
| 1937 | ret = read(fd, p, bytes_left); |
| 1938 | if (!ret) |
| 1939 | return 1; |
| 1940 | else if (ret < 0) { |
| 1941 | if (errno != EAGAIN) { |
| 1942 | perror("read"); |
| 1943 | return -1; |
| 1944 | } |
| 1945 | |
| 1946 | /* |
| 1947 | * never do partial reads. we can return if we |
| 1948 | * didn't read anything and we should not block, |
| 1949 | * otherwise wait for data |
| 1950 | */ |
| 1951 | if ((bytes_left == bytes) && !block) |
| 1952 | return 1; |
| 1953 | |
| 1954 | usleep(10); |
| 1955 | continue; |
| 1956 | } else { |
| 1957 | p += ret; |
| 1958 | bytes_left -= ret; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | return 0; |
| 1963 | } |
| 1964 | |
| 1965 | static inline __u16 get_pdulen(struct blk_io_trace *bit) |
| 1966 | { |
| 1967 | if (data_is_native) |
| 1968 | return bit->pdu_len; |
| 1969 | |
| 1970 | return __bswap_16(bit->pdu_len); |
| 1971 | } |
| 1972 | |
| 1973 | static inline __u32 get_magic(struct blk_io_trace *bit) |
| 1974 | { |
| 1975 | if (data_is_native) |
| 1976 | return bit->magic; |
| 1977 | |
| 1978 | return __bswap_32(bit->magic); |
| 1979 | } |
| 1980 | |
| 1981 | static int read_events(int fd, int always_block, int *fdblock) |
| 1982 | { |
| 1983 | struct per_dev_info *pdi = NULL; |
| 1984 | unsigned int events = 0; |
| 1985 | |
| 1986 | while (!is_done() && events < rb_batch) { |
| 1987 | struct blk_io_trace *bit; |
| 1988 | struct trace *t; |
| 1989 | int pdu_len, should_block, ret; |
| 1990 | __u32 magic; |
| 1991 | |
| 1992 | bit = bit_alloc(); |
| 1993 | |
| 1994 | should_block = !events || always_block; |
| 1995 | |
| 1996 | ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock); |
| 1997 | if (ret) { |
| 1998 | bit_free(bit); |
| 1999 | if (!events && ret < 0) |
| 2000 | events = ret; |
| 2001 | break; |
| 2002 | } |
| 2003 | |
| 2004 | /* |
| 2005 | * look at first trace to check whether we need to convert |
| 2006 | * data in the future |
| 2007 | */ |
| 2008 | if (data_is_native == -1 && check_data_endianness(bit->magic)) |
| 2009 | break; |
| 2010 | |
| 2011 | magic = get_magic(bit); |
| 2012 | if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
| 2013 | fprintf(stderr, "Bad magic %x\n", magic); |
| 2014 | break; |
| 2015 | } |
| 2016 | |
| 2017 | pdu_len = get_pdulen(bit); |
| 2018 | if (pdu_len) { |
| 2019 | void *ptr = realloc(bit, sizeof(*bit) + pdu_len); |
| 2020 | |
| 2021 | if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) { |
| 2022 | bit_free(ptr); |
| 2023 | break; |
| 2024 | } |
| 2025 | |
| 2026 | bit = ptr; |
| 2027 | } |
| 2028 | |
| 2029 | trace_to_cpu(bit); |
| 2030 | |
| 2031 | if (verify_trace(bit)) { |
| 2032 | bit_free(bit); |
| 2033 | continue; |
| 2034 | } |
| 2035 | |
| 2036 | /* |
| 2037 | * not a real trace, so grab and handle it here |
| 2038 | */ |
| 2039 | if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) { |
| 2040 | handle_notify(bit); |
| 2041 | output_binary(bit, sizeof(*bit) + bit->pdu_len); |
| 2042 | continue; |
| 2043 | } |
| 2044 | |
| 2045 | t = t_alloc(); |
| 2046 | memset(t, 0, sizeof(*t)); |
| 2047 | t->bit = bit; |
| 2048 | t->read_sequence = read_sequence; |
| 2049 | |
| 2050 | t->next = trace_list; |
| 2051 | trace_list = t; |
| 2052 | |
| 2053 | if (!pdi || pdi->dev != bit->device) |
| 2054 | pdi = get_dev_info(bit->device); |
| 2055 | |
| 2056 | if (bit->time > pdi->last_read_time) |
| 2057 | pdi->last_read_time = bit->time; |
| 2058 | |
| 2059 | events++; |
| 2060 | } |
| 2061 | |
| 2062 | return events; |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | * Managing input streams |
| 2067 | */ |
| 2068 | |
| 2069 | struct ms_stream { |
| 2070 | struct ms_stream *next; |
| 2071 | struct trace *first, *last; |
| 2072 | struct per_dev_info *pdi; |
| 2073 | unsigned int cpu; |
| 2074 | }; |
| 2075 | |
| 2076 | #define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff)) |
| 2077 | |
| 2078 | struct ms_stream *ms_head; |
| 2079 | struct ms_stream *ms_hash[256]; |
| 2080 | |
| 2081 | static void ms_sort(struct ms_stream *msp); |
| 2082 | static int ms_prime(struct ms_stream *msp); |
| 2083 | |
| 2084 | static inline struct trace *ms_peek(struct ms_stream *msp) |
| 2085 | { |
| 2086 | return (msp == NULL) ? NULL : msp->first; |
| 2087 | } |
| 2088 | |
| 2089 | static inline __u64 ms_peek_time(struct ms_stream *msp) |
| 2090 | { |
| 2091 | return ms_peek(msp)->bit->time; |
| 2092 | } |
| 2093 | |
| 2094 | static inline void ms_resort(struct ms_stream *msp) |
| 2095 | { |
| 2096 | if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) { |
| 2097 | ms_head = msp->next; |
| 2098 | msp->next = NULL; |
| 2099 | ms_sort(msp); |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | static inline void ms_deq(struct ms_stream *msp) |
| 2104 | { |
| 2105 | msp->first = msp->first->next; |
| 2106 | if (!msp->first) { |
| 2107 | msp->last = NULL; |
| 2108 | if (!ms_prime(msp)) { |
| 2109 | ms_head = msp->next; |
| 2110 | msp->next = NULL; |
| 2111 | return; |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | ms_resort(msp); |
| 2116 | } |
| 2117 | |
| 2118 | static void ms_sort(struct ms_stream *msp) |
| 2119 | { |
| 2120 | __u64 msp_t = ms_peek_time(msp); |
| 2121 | struct ms_stream *this_msp = ms_head; |
| 2122 | |
| 2123 | if (this_msp == NULL) |
| 2124 | ms_head = msp; |
| 2125 | else if (msp_t < ms_peek_time(this_msp)) { |
| 2126 | msp->next = this_msp; |
| 2127 | ms_head = msp; |
| 2128 | } |
| 2129 | else { |
| 2130 | while (this_msp->next && ms_peek_time(this_msp->next) < msp_t) |
| 2131 | this_msp = this_msp->next; |
| 2132 | |
| 2133 | msp->next = this_msp->next; |
| 2134 | this_msp->next = msp; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | static int ms_prime(struct ms_stream *msp) |
| 2139 | { |
| 2140 | __u32 magic; |
| 2141 | unsigned int i; |
| 2142 | struct trace *t; |
| 2143 | struct per_dev_info *pdi = msp->pdi; |
| 2144 | struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu); |
| 2145 | struct blk_io_trace *bit = NULL; |
| 2146 | int ret, pdu_len, ndone = 0; |
| 2147 | |
| 2148 | for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) { |
| 2149 | bit = bit_alloc(); |
| 2150 | ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock); |
| 2151 | if (ret) |
| 2152 | goto err; |
| 2153 | |
| 2154 | if (data_is_native == -1 && check_data_endianness(bit->magic)) |
| 2155 | goto err; |
| 2156 | |
| 2157 | magic = get_magic(bit); |
| 2158 | if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
| 2159 | fprintf(stderr, "Bad magic %x\n", magic); |
| 2160 | goto err; |
| 2161 | |
| 2162 | } |
| 2163 | |
| 2164 | pdu_len = get_pdulen(bit); |
| 2165 | if (pdu_len) { |
| 2166 | void *ptr = realloc(bit, sizeof(*bit) + pdu_len); |
| 2167 | ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len, |
| 2168 | 1, &pci->fdblock); |
| 2169 | if (ret) { |
| 2170 | free(ptr); |
| 2171 | bit = NULL; |
| 2172 | goto err; |
| 2173 | } |
| 2174 | |
| 2175 | bit = ptr; |
| 2176 | } |
| 2177 | |
| 2178 | trace_to_cpu(bit); |
| 2179 | if (verify_trace(bit)) |
| 2180 | goto err; |
| 2181 | |
| 2182 | if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) { |
| 2183 | handle_notify(bit); |
| 2184 | output_binary(bit, sizeof(*bit) + bit->pdu_len); |
| 2185 | bit_free(bit); |
| 2186 | |
| 2187 | i -= 1; |
| 2188 | continue; |
| 2189 | } |
| 2190 | |
| 2191 | if (bit->time > pdi->last_read_time) |
| 2192 | pdi->last_read_time = bit->time; |
| 2193 | |
| 2194 | t = t_alloc(); |
| 2195 | memset(t, 0, sizeof(*t)); |
| 2196 | t->bit = bit; |
| 2197 | |
| 2198 | if (msp->first == NULL) |
| 2199 | msp->first = msp->last = t; |
| 2200 | else { |
| 2201 | msp->last->next = t; |
| 2202 | msp->last = t; |
| 2203 | } |
| 2204 | |
| 2205 | ndone++; |
| 2206 | } |
| 2207 | |
| 2208 | return ndone; |
| 2209 | |
| 2210 | err: |
| 2211 | if (bit) bit_free(bit); |
| 2212 | |
| 2213 | cpu_mark_offline(pdi, pci->cpu); |
| 2214 | close(pci->fd); |
| 2215 | pci->fd = -1; |
| 2216 | |
| 2217 | return ndone; |
| 2218 | } |
| 2219 | |
| 2220 | static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu) |
| 2221 | { |
| 2222 | struct ms_stream *msp = malloc(sizeof(*msp)); |
| 2223 | |
| 2224 | msp->next = NULL; |
| 2225 | msp->first = msp->last = NULL; |
| 2226 | msp->pdi = pdi; |
| 2227 | msp->cpu = cpu; |
| 2228 | |
| 2229 | if (ms_prime(msp)) |
| 2230 | ms_sort(msp); |
| 2231 | |
| 2232 | return msp; |
| 2233 | } |
| 2234 | |
| 2235 | static int setup_file(struct per_dev_info *pdi, int cpu) |
| 2236 | { |
| 2237 | int len = 0; |
| 2238 | struct stat st; |
| 2239 | char *p, *dname; |
| 2240 | struct per_cpu_info *pci = get_cpu_info(pdi, cpu); |
| 2241 | |
| 2242 | pci->cpu = cpu; |
| 2243 | pci->fdblock = -1; |
| 2244 | |
| 2245 | p = strdup(pdi->name); |
| 2246 | dname = dirname(p); |
| 2247 | if (strcmp(dname, ".")) { |
| 2248 | input_dir = dname; |
| 2249 | p = strdup(pdi->name); |
| 2250 | strcpy(pdi->name, basename(p)); |
| 2251 | } |
| 2252 | free(p); |
| 2253 | |
| 2254 | if (input_dir) |
| 2255 | len = sprintf(pci->fname, "%s/", input_dir); |
| 2256 | |
| 2257 | snprintf(pci->fname + len, sizeof(pci->fname)-1-len, |
| 2258 | "%s.blktrace.%d", pdi->name, pci->cpu); |
| 2259 | if (stat(pci->fname, &st) < 0) |
| 2260 | return 0; |
| 2261 | if (!st.st_size) |
| 2262 | return 1; |
| 2263 | |
| 2264 | pci->fd = open(pci->fname, O_RDONLY); |
| 2265 | if (pci->fd < 0) { |
| 2266 | perror(pci->fname); |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
| 2270 | printf("Input file %s added\n", pci->fname); |
| 2271 | cpu_mark_online(pdi, pci->cpu); |
| 2272 | |
| 2273 | pdi->nfiles++; |
| 2274 | ms_alloc(pdi, pci->cpu); |
| 2275 | |
| 2276 | return 1; |
| 2277 | } |
| 2278 | |
| 2279 | static int handle(struct ms_stream *msp) |
| 2280 | { |
| 2281 | struct trace *t; |
| 2282 | struct per_dev_info *pdi; |
| 2283 | struct per_cpu_info *pci; |
| 2284 | struct blk_io_trace *bit; |
| 2285 | |
| 2286 | t = ms_peek(msp); |
| 2287 | if (t->bit->time > stopwatch_end) |
| 2288 | return 0; |
| 2289 | |
| 2290 | bit = t->bit; |
| 2291 | pdi = msp->pdi; |
| 2292 | pci = get_cpu_info(pdi, msp->cpu); |
| 2293 | pci->nelems++; |
| 2294 | |
| 2295 | bit->time -= genesis_time; |
| 2296 | pdi->last_reported_time = bit->time; |
| 2297 | if (bit->action & (act_mask << BLK_TC_SHIFT)) |
| 2298 | dump_trace(bit, pci, pdi); |
| 2299 | |
| 2300 | ms_deq(msp); |
| 2301 | |
| 2302 | if (text_output) |
| 2303 | trace_rb_insert_last(pdi, t); |
| 2304 | else { |
| 2305 | bit_free(t->bit); |
| 2306 | t_free(t); |
| 2307 | } |
| 2308 | |
| 2309 | return 1; |
| 2310 | } |
| 2311 | |
| 2312 | /* |
| 2313 | * Check if we need to sanitize the name. We allow 'foo', or if foo.blktrace.X |
| 2314 | * is given, then strip back down to 'foo' to avoid missing files. |
| 2315 | */ |
| 2316 | static int name_fixup(char *name) |
| 2317 | { |
| 2318 | char *b; |
| 2319 | |
| 2320 | if (!name) |
| 2321 | return 1; |
| 2322 | |
| 2323 | b = strstr(name, ".blktrace."); |
| 2324 | if (b) |
| 2325 | *b = '\0'; |
| 2326 | |
| 2327 | return 0; |
| 2328 | } |
| 2329 | |
| 2330 | static int do_file(void) |
| 2331 | { |
| 2332 | int i, cpu, ret; |
| 2333 | struct per_dev_info *pdi; |
| 2334 | |
| 2335 | /* |
| 2336 | * first prepare all files for reading |
| 2337 | */ |
| 2338 | for (i = 0; i < ndevices; i++) { |
| 2339 | pdi = &devices[i]; |
| 2340 | ret = name_fixup(pdi->name); |
| 2341 | if (ret) |
| 2342 | return ret; |
| 2343 | |
| 2344 | for (cpu = 0; setup_file(pdi, cpu); cpu++) |
| 2345 | ; |
| 2346 | } |
| 2347 | |
| 2348 | /* |
| 2349 | * Get the initial time stamp |
| 2350 | */ |
| 2351 | if (ms_head) |
| 2352 | genesis_time = ms_peek_time(ms_head); |
| 2353 | |
| 2354 | /* |
| 2355 | * Keep processing traces while any are left |
| 2356 | */ |
| 2357 | while (!is_done() && ms_head && handle(ms_head)) |
| 2358 | ; |
| 2359 | |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
| 2363 | static void do_pipe(int fd) |
| 2364 | { |
| 2365 | unsigned long long youngest; |
| 2366 | int events, fdblock; |
| 2367 | |
| 2368 | last_allowed_time = -1ULL; |
| 2369 | fdblock = -1; |
| 2370 | while ((events = read_events(fd, 0, &fdblock)) > 0) { |
| 2371 | read_sequence++; |
| 2372 | |
| 2373 | #if 0 |
| 2374 | smallest_seq_read = -1U; |
| 2375 | #endif |
| 2376 | |
| 2377 | if (sort_entries(&youngest)) |
| 2378 | break; |
| 2379 | |
| 2380 | if (youngest > stopwatch_end) |
| 2381 | break; |
| 2382 | |
| 2383 | show_entries_rb(0); |
| 2384 | } |
| 2385 | |
| 2386 | if (rb_sort_entries) |
| 2387 | show_entries_rb(1); |
| 2388 | } |
| 2389 | |
| 2390 | static int do_fifo(void) |
| 2391 | { |
| 2392 | int fd; |
| 2393 | |
| 2394 | if (!strcmp(pipename, "-")) |
| 2395 | fd = dup(STDIN_FILENO); |
| 2396 | else |
| 2397 | fd = open(pipename, O_RDONLY); |
| 2398 | |
| 2399 | if (fd == -1) { |
| 2400 | perror("dup stdin"); |
| 2401 | return -1; |
| 2402 | } |
| 2403 | |
| 2404 | do_pipe(fd); |
| 2405 | close(fd); |
| 2406 | return 0; |
| 2407 | } |
| 2408 | |
| 2409 | static void show_stats(void) |
| 2410 | { |
| 2411 | if (!ofp) |
| 2412 | return; |
| 2413 | if (stats_printed) |
| 2414 | return; |
| 2415 | |
| 2416 | stats_printed = 1; |
| 2417 | |
| 2418 | if (per_process_stats) |
| 2419 | show_process_stats(); |
| 2420 | |
| 2421 | if (per_device_and_cpu_stats) |
| 2422 | show_device_and_cpu_stats(); |
| 2423 | |
| 2424 | fflush(ofp); |
| 2425 | } |
| 2426 | |
| 2427 | static void handle_sigint(__attribute__((__unused__)) int sig) |
| 2428 | { |
| 2429 | done = 1; |
| 2430 | } |
| 2431 | |
| 2432 | /* |
| 2433 | * Extract start and duration times from a string, allowing |
| 2434 | * us to specify a time interval of interest within a trace. |
| 2435 | * Format: "duration" (start is zero) or "start:duration". |
| 2436 | */ |
| 2437 | static int find_stopwatch_interval(char *string) |
| 2438 | { |
| 2439 | double value; |
| 2440 | char *sp; |
| 2441 | |
| 2442 | value = strtod(string, &sp); |
| 2443 | if (sp == string) { |
| 2444 | fprintf(stderr,"Invalid stopwatch timer: %s\n", string); |
| 2445 | return 1; |
| 2446 | } |
| 2447 | if (*sp == ':') { |
| 2448 | stopwatch_start = DOUBLE_TO_NANO_ULL(value); |
| 2449 | string = sp + 1; |
| 2450 | value = strtod(string, &sp); |
| 2451 | if (sp == string || *sp != '\0') { |
| 2452 | fprintf(stderr,"Invalid stopwatch duration time: %s\n", |
| 2453 | string); |
| 2454 | return 1; |
| 2455 | } |
| 2456 | } else if (*sp != '\0') { |
| 2457 | fprintf(stderr,"Invalid stopwatch start timer: %s\n", string); |
| 2458 | return 1; |
| 2459 | } |
| 2460 | stopwatch_end = DOUBLE_TO_NANO_ULL(value); |
| 2461 | if (stopwatch_end <= stopwatch_start) { |
| 2462 | fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n", |
| 2463 | stopwatch_start, stopwatch_end); |
| 2464 | return 1; |
| 2465 | } |
| 2466 | |
| 2467 | return 0; |
| 2468 | } |
| 2469 | |
| 2470 | static int is_pipe(const char *str) |
| 2471 | { |
| 2472 | struct stat st; |
| 2473 | |
| 2474 | if (!strcmp(str, "-")) |
| 2475 | return 1; |
| 2476 | if (!stat(str, &st) && S_ISFIFO(st.st_mode)) |
| 2477 | return 1; |
| 2478 | |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
| 2482 | #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vV" |
| 2483 | static char usage_str[] = "\n\n" \ |
| 2484 | "-i <file> | --input=<file>\n" \ |
| 2485 | "[ -a <action field> | --act-mask=<action field> ]\n" \ |
| 2486 | "[ -A <action mask> | --set-mask=<action mask> ]\n" \ |
| 2487 | "[ -b <traces> | --batch=<traces> ]\n" \ |
| 2488 | "[ -d <file> | --dump-binary=<file> ]\n" \ |
| 2489 | "[ -D <dir> | --input-directory=<dir> ]\n" \ |
| 2490 | "[ -f <format> | --format=<format> ]\n" \ |
| 2491 | "[ -F <spec> | --format-spec=<spec> ]\n" \ |
| 2492 | "[ -h | --hash-by-name ]\n" \ |
| 2493 | "[ -o <file> | --output=<file> ]\n" \ |
| 2494 | "[ -O | --no-text-output ]\n" \ |
| 2495 | "[ -q | --quiet ]\n" \ |
| 2496 | "[ -s | --per-program-stats ]\n" \ |
| 2497 | "[ -t | --track-ios ]\n" \ |
| 2498 | "[ -w <time> | --stopwatch=<time> ]\n" \ |
| 2499 | "[ -v | --verbose ]\n" \ |
| 2500 | "[ -V | --version ]\n\n" \ |
| 2501 | "\t-b stdin read batching\n" \ |
| 2502 | "\t-d Output file. If specified, binary data is written to file\n" \ |
| 2503 | "\t-D Directory to prepend to input file names\n" \ |
| 2504 | "\t-f Output format. Customize the output format. The format field\n" \ |
| 2505 | "\t identifies can be found in the documentation\n" \ |
| 2506 | "\t-F Format specification. Can be found in the documentation\n" \ |
| 2507 | "\t-h Hash processes by name, not pid\n" \ |
| 2508 | "\t-i Input file containing trace data, or '-' for stdin\n" \ |
| 2509 | "\t-o Output file. If not given, output is stdout\n" \ |
| 2510 | "\t-O Do NOT output text data\n" \ |
| 2511 | "\t-q Quiet. Don't display any stats at the end of the trace\n" \ |
| 2512 | "\t-s Show per-program io statistics\n" \ |
| 2513 | "\t-t Track individual ios. Will tell you the time a request took\n" \ |
| 2514 | "\t to get queued, to get dispatched, and to get completed\n" \ |
| 2515 | "\t-w Only parse data between the given time interval in seconds.\n" \ |
| 2516 | "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \ |
| 2517 | "\t-v More verbose for marginal errors\n" \ |
| 2518 | "\t-V Print program version info\n\n"; |
| 2519 | |
| 2520 | static void usage(char *prog) |
| 2521 | { |
| 2522 | fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str); |
| 2523 | } |
| 2524 | |
| 2525 | int main(int argc, char *argv[]) |
| 2526 | { |
| 2527 | int i, c, ret, mode; |
| 2528 | int act_mask_tmp = 0; |
| 2529 | char *ofp_buffer = NULL; |
| 2530 | char *bin_ofp_buffer = NULL; |
| 2531 | |
| 2532 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) { |
| 2533 | switch (c) { |
| 2534 | case 'a': |
| 2535 | i = find_mask_map(optarg); |
| 2536 | if (i < 0) { |
| 2537 | fprintf(stderr,"Invalid action mask %s\n", |
| 2538 | optarg); |
| 2539 | return 1; |
| 2540 | } |
| 2541 | act_mask_tmp |= i; |
| 2542 | break; |
| 2543 | |
| 2544 | case 'A': |
| 2545 | if ((sscanf(optarg, "%x", &i) != 1) || |
| 2546 | !valid_act_opt(i)) { |
| 2547 | fprintf(stderr, |
| 2548 | "Invalid set action mask %s/0x%x\n", |
| 2549 | optarg, i); |
| 2550 | return 1; |
| 2551 | } |
| 2552 | act_mask_tmp = i; |
| 2553 | break; |
| 2554 | case 'i': |
| 2555 | if (is_pipe(optarg) && !pipeline) { |
| 2556 | pipeline = 1; |
| 2557 | pipename = strdup(optarg); |
| 2558 | } else if (resize_devices(optarg) != 0) |
| 2559 | return 1; |
| 2560 | break; |
| 2561 | case 'D': |
| 2562 | input_dir = optarg; |
| 2563 | break; |
| 2564 | case 'o': |
| 2565 | output_name = optarg; |
| 2566 | break; |
| 2567 | case 'O': |
| 2568 | text_output = 0; |
| 2569 | break; |
| 2570 | case 'b': |
| 2571 | rb_batch = atoi(optarg); |
| 2572 | if (rb_batch <= 0) |
| 2573 | rb_batch = RB_BATCH_DEFAULT; |
| 2574 | break; |
| 2575 | case 's': |
| 2576 | per_process_stats = 1; |
| 2577 | break; |
| 2578 | case 't': |
| 2579 | track_ios = 1; |
| 2580 | break; |
| 2581 | case 'q': |
| 2582 | per_device_and_cpu_stats = 0; |
| 2583 | break; |
| 2584 | case 'w': |
| 2585 | if (find_stopwatch_interval(optarg) != 0) |
| 2586 | return 1; |
| 2587 | break; |
| 2588 | case 'f': |
| 2589 | set_all_format_specs(optarg); |
| 2590 | break; |
| 2591 | case 'F': |
| 2592 | if (add_format_spec(optarg) != 0) |
| 2593 | return 1; |
| 2594 | break; |
| 2595 | case 'h': |
| 2596 | ppi_hash_by_pid = 0; |
| 2597 | break; |
| 2598 | case 'v': |
| 2599 | verbose++; |
| 2600 | break; |
| 2601 | case 'V': |
| 2602 | printf("%s version %s\n", argv[0], blkparse_version); |
| 2603 | return 0; |
| 2604 | case 'd': |
| 2605 | dump_binary = optarg; |
| 2606 | break; |
| 2607 | default: |
| 2608 | usage(argv[0]); |
| 2609 | return 1; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | while (optind < argc) { |
| 2614 | if (is_pipe(argv[optind]) && !pipeline) { |
| 2615 | pipeline = 1; |
| 2616 | pipename = strdup(argv[optind]); |
| 2617 | } else if (resize_devices(argv[optind]) != 0) |
| 2618 | return 1; |
| 2619 | optind++; |
| 2620 | } |
| 2621 | |
| 2622 | if (!pipeline && !ndevices) { |
| 2623 | usage(argv[0]); |
| 2624 | return 1; |
| 2625 | } |
| 2626 | |
| 2627 | if (act_mask_tmp != 0) |
| 2628 | act_mask = act_mask_tmp; |
| 2629 | |
| 2630 | memset(&rb_sort_root, 0, sizeof(rb_sort_root)); |
| 2631 | |
| 2632 | signal(SIGINT, handle_sigint); |
| 2633 | signal(SIGHUP, handle_sigint); |
| 2634 | signal(SIGTERM, handle_sigint); |
| 2635 | |
| 2636 | setlocale(LC_NUMERIC, "en_US"); |
| 2637 | |
| 2638 | if (text_output) { |
| 2639 | if (!output_name) { |
| 2640 | ofp = fdopen(STDOUT_FILENO, "w"); |
| 2641 | mode = _IOLBF; |
| 2642 | } else { |
| 2643 | char ofname[128]; |
| 2644 | |
| 2645 | snprintf(ofname, sizeof(ofname) - 1, "%s", output_name); |
| 2646 | ofp = fopen(ofname, "w"); |
| 2647 | mode = _IOFBF; |
| 2648 | } |
| 2649 | |
| 2650 | if (!ofp) { |
| 2651 | perror("fopen"); |
| 2652 | return 1; |
| 2653 | } |
| 2654 | |
| 2655 | ofp_buffer = malloc(4096); |
| 2656 | if (setvbuf(ofp, ofp_buffer, mode, 4096)) { |
| 2657 | perror("setvbuf"); |
| 2658 | return 1; |
| 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | if (dump_binary) { |
| 2663 | dump_fp = fopen(dump_binary, "w"); |
| 2664 | if (!dump_fp) { |
| 2665 | perror(dump_binary); |
| 2666 | dump_binary = NULL; |
| 2667 | return 1; |
| 2668 | } |
| 2669 | bin_ofp_buffer = malloc(128 * 1024); |
| 2670 | if (setvbuf(dump_fp, bin_ofp_buffer, _IOFBF, 128 * 1024)) { |
| 2671 | perror("setvbuf binary"); |
| 2672 | return 1; |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | if (pipeline) |
| 2677 | ret = do_fifo(); |
| 2678 | else |
| 2679 | ret = do_file(); |
| 2680 | |
| 2681 | if (!ret) |
| 2682 | show_stats(); |
| 2683 | |
| 2684 | if (ofp_buffer) { |
| 2685 | fflush(ofp); |
| 2686 | free(ofp_buffer); |
| 2687 | } |
| 2688 | if (bin_ofp_buffer) { |
| 2689 | fflush(dump_fp); |
| 2690 | free(bin_ofp_buffer); |
| 2691 | } |
| 2692 | return ret; |
| 2693 | } |