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