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