blkparse: Allow request tracking on non md/dm devices
[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",
3c667f3c 1009 MAJOR(t->device), MINOR(t->device),
57ea8602 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
1030/*
1031 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
1032 * time here, as dispatch time
1033 */
1034static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
1035{
1036 struct io_track *iot;
1037
1038 if (!track_ios)
1039 return;
753f9091 1040
bfc70ad5 1041 iot = find_track(pdi, t->pid, t->sector);
753f9091
JA
1042 iot->dispatch_time = t->time;
1043}
1044
95c15013 1045/*
b6076a9b 1046 * return time between rq allocation and insertion
95c15013 1047 */
f7bd1a9b
JA
1048static unsigned long long log_track_insert(struct per_dev_info *pdi,
1049 struct blk_io_trace *t)
95c15013 1050{
50adc0ba 1051 unsigned long long elapsed;
95c15013
JA
1052 struct io_track *iot;
1053
1054 if (!track_ios)
1055 return -1;
1056
bfc70ad5 1057 iot = find_track(pdi, t->pid, t->sector);
2e3e8ded 1058 iot->queue_time = t->time;
acd70d21
JA
1059
1060 if (!iot->allocation_time)
1061 return -1;
1062
50adc0ba
JA
1063 elapsed = iot->queue_time - iot->allocation_time;
1064
1065 if (per_process_stats) {
2990e589 1066 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
b9d40d6f 1067 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 1068
b9d40d6f
JA
1069 if (ppi && elapsed > ppi->longest_allocation_wait[w])
1070 ppi->longest_allocation_wait[w] = elapsed;
50adc0ba
JA
1071 }
1072
1073 return elapsed;
2e3e8ded
JA
1074}
1075
1076/*
1077 * return time between queue and issue
1078 */
f7bd1a9b
JA
1079static unsigned long long log_track_issue(struct per_dev_info *pdi,
1080 struct blk_io_trace *t)
2e3e8ded 1081{
50adc0ba 1082 unsigned long long elapsed;
2e3e8ded
JA
1083 struct io_track *iot;
1084
1085 if (!track_ios)
1086 return -1;
1087 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
1088 return -1;
1089
f7bd1a9b 1090 iot = __find_track(pdi, t->sector);
cb2a1a62 1091 if (!iot) {
57ea8602
JA
1092 if (verbose)
1093 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
3c667f3c 1094 MAJOR(t->device), MINOR(t->device),
57ea8602 1095 (unsigned long long) t->sector);
2e3e8ded 1096 return -1;
cb2a1a62 1097 }
2e3e8ded
JA
1098
1099 iot->dispatch_time = t->time;
50adc0ba
JA
1100 elapsed = iot->dispatch_time - iot->queue_time;
1101
1102 if (per_process_stats) {
2990e589 1103 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
b9d40d6f 1104 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 1105
b9d40d6f
JA
1106 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
1107 ppi->longest_dispatch_wait[w] = elapsed;
50adc0ba
JA
1108 }
1109
1110 return elapsed;
2e3e8ded
JA
1111}
1112
1113/*
1114 * return time between dispatch and complete
1115 */
f7bd1a9b
JA
1116static unsigned long long log_track_complete(struct per_dev_info *pdi,
1117 struct blk_io_trace *t)
2e3e8ded
JA
1118{
1119 unsigned long long elapsed;
1120 struct io_track *iot;
1121
1122 if (!track_ios)
1123 return -1;
2e3e8ded 1124
f7bd1a9b 1125 iot = __find_track(pdi, t->sector);
cb2a1a62 1126 if (!iot) {
57ea8602
JA
1127 if (verbose)
1128 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
3c667f3c 1129 MAJOR(t->device), MINOR(t->device),
57ea8602 1130 (unsigned long long) t->sector);
2e3e8ded 1131 return -1;
cb2a1a62 1132 }
2e3e8ded
JA
1133
1134 iot->completion_time = t->time;
1135 elapsed = iot->completion_time - iot->dispatch_time;
1136
50adc0ba 1137 if (per_process_stats) {
2990e589 1138 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
b9d40d6f 1139 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 1140
b9d40d6f
JA
1141 if (ppi && elapsed > ppi->longest_completion_wait[w])
1142 ppi->longest_completion_wait[w] = elapsed;
50adc0ba
JA
1143 }
1144
2e3e8ded
JA
1145 /*
1146 * kill the trace, we don't need it after completion
1147 */
f7bd1a9b 1148 rb_erase(&iot->rb_node, &pdi->rb_track);
2e3e8ded
JA
1149 free(iot);
1150
1151 return elapsed;
1152}
1153
1154
9e4cd1b8 1155static struct io_stats *find_process_io_stats(pid_t pid)
152f6476 1156{
bfc70ad5 1157 struct per_process_info *ppi = find_ppi(pid);
152f6476
JA
1158
1159 if (!ppi) {
1160 ppi = malloc(sizeof(*ppi));
1161 memset(ppi, 0, sizeof(*ppi));
2990e589 1162 ppi->ppm = find_ppm(pid);
ebe2d1aa
JA
1163 if (!ppi->ppm)
1164 ppi->ppm = add_ppm_hash(pid, "unknown");
bfc70ad5
JA
1165 add_ppi_to_hash(ppi);
1166 add_ppi_to_list(ppi);
152f6476
JA
1167 }
1168
1169 return &ppi->io_stats;
1170}
1171
e7c9f3ff
NS
1172static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1173{
1174 if (pdi->name)
1175 snprintf(buffer, size, "%s", pdi->name);
1176 else
f7bd1a9b 1177 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
e7c9f3ff
NS
1178 return buffer;
1179}
1180
e7c9f3ff 1181static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
cfab07eb
AB
1182{
1183 unsigned long long this = bit->time;
e7c9f3ff 1184 unsigned long long last = pdi->last_reported_time;
cfab07eb 1185
e7c9f3ff
NS
1186 pdi->backwards = (this < last) ? 'B' : ' ';
1187 pdi->last_reported_time = this;
cfab07eb
AB
1188}
1189
fb2ec796
JA
1190static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1191 int rw)
d0ca268b 1192{
fb2ec796 1193 if (rw) {
152f6476 1194 ios->mwrites++;
fb2ec796 1195 ios->mwrite_kb += t_kb(t);
cd0ae0f6 1196 ios->mwrite_b += t_b(t);
fb2ec796 1197 } else {
152f6476 1198 ios->mreads++;
fb2ec796 1199 ios->mread_kb += t_kb(t);
cd0ae0f6 1200 ios->mread_b += t_b(t);
fb2ec796 1201 }
152f6476
JA
1202}
1203
1204static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1205 int rw)
1206{
fb2ec796 1207 __account_m(&pci->io_stats, t, rw);
152f6476
JA
1208
1209 if (per_process_stats) {
bfc70ad5 1210 struct io_stats *ios = find_process_io_stats(t->pid);
152f6476 1211
fb2ec796 1212 __account_m(ios, t, rw);
d0ca268b
JA
1213 }
1214}
1215
801646d6
CS
1216static inline void __account_pc_queue(struct io_stats *ios,
1217 struct blk_io_trace *t, int rw)
1218{
1219 if (rw) {
1220 ios->qwrites_pc++;
1221 ios->qwrite_kb_pc += t_kb(t);
cd0ae0f6 1222 ios->qwrite_b_pc += t_b(t);
801646d6
CS
1223 } else {
1224 ios->qreads_pc++;
1225 ios->qread_kb += t_kb(t);
cd0ae0f6 1226 ios->qread_b_pc += t_b(t);
801646d6
CS
1227 }
1228}
1229
1230static inline void account_pc_queue(struct blk_io_trace *t,
1231 struct per_cpu_info *pci, int rw)
1232{
1233 __account_pc_queue(&pci->io_stats, t, rw);
1234
1235 if (per_process_stats) {
1236 struct io_stats *ios = find_process_io_stats(t->pid);
1237
1238 __account_pc_queue(ios, t, rw);
1239 }
1240}
1241
1242static inline void __account_pc_issue(struct io_stats *ios, int rw,
1243 unsigned int bytes)
1244{
1245 if (rw) {
1246 ios->iwrites_pc++;
1247 ios->iwrite_kb_pc += bytes >> 10;
cd0ae0f6 1248 ios->iwrite_b_pc += bytes & 1023;
801646d6
CS
1249 } else {
1250 ios->ireads_pc++;
1251 ios->iread_kb_pc += bytes >> 10;
cd0ae0f6 1252 ios->iread_b_pc += bytes & 1023;
801646d6
CS
1253 }
1254}
1255
1256static inline void account_pc_issue(struct blk_io_trace *t,
1257 struct per_cpu_info *pci, int rw)
1258{
1259 __account_pc_issue(&pci->io_stats, rw, t->bytes);
1260
1261 if (per_process_stats) {
1262 struct io_stats *ios = find_process_io_stats(t->pid);
1263
1264 __account_pc_issue(ios, rw, t->bytes);
1265 }
1266}
1267
1268static inline void __account_pc_requeue(struct io_stats *ios,
1269 struct blk_io_trace *t, int rw)
1270{
1271 if (rw) {
1272 ios->wrqueue_pc++;
1273 ios->iwrite_kb_pc -= t_kb(t);
cd0ae0f6 1274 ios->iwrite_b_pc -= t_b(t);
801646d6
CS
1275 } else {
1276 ios->rrqueue_pc++;
1277 ios->iread_kb_pc -= t_kb(t);
cd0ae0f6 1278 ios->iread_b_pc -= t_b(t);
801646d6
CS
1279 }
1280}
1281
1282static inline void account_pc_requeue(struct blk_io_trace *t,
1283 struct per_cpu_info *pci, int rw)
1284{
1285 __account_pc_requeue(&pci->io_stats, t, rw);
1286
1287 if (per_process_stats) {
1288 struct io_stats *ios = find_process_io_stats(t->pid);
1289
1290 __account_pc_requeue(ios, t, rw);
1291 }
1292}
1293
1294static inline void __account_pc_c(struct io_stats *ios, int rw)
1295{
1296 if (rw)
1297 ios->cwrites_pc++;
1298 else
1299 ios->creads_pc++;
1300}
1301
1302static inline void account_pc_c(struct blk_io_trace *t,
1303 struct per_cpu_info *pci, int rw)
1304{
1305 __account_pc_c(&pci->io_stats, rw);
1306
1307 if (per_process_stats) {
1308 struct io_stats *ios = find_process_io_stats(t->pid);
1309
1310 __account_pc_c(ios, rw);
1311 }
1312}
1313
b6076a9b
JA
1314static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1315 int rw)
d0ca268b
JA
1316{
1317 if (rw) {
152f6476 1318 ios->qwrites++;
ae957cbc 1319 ios->qwrite_kb += t_kb(t);
cd0ae0f6 1320 ios->qwrite_b += t_b(t);
d0ca268b 1321 } else {
152f6476 1322 ios->qreads++;
ae957cbc 1323 ios->qread_kb += t_kb(t);
cd0ae0f6 1324 ios->qread_b += t_b(t);
152f6476
JA
1325 }
1326}
1327
b6076a9b
JA
1328static inline void account_queue(struct blk_io_trace *t,
1329 struct per_cpu_info *pci, int rw)
152f6476 1330{
b6076a9b 1331 __account_queue(&pci->io_stats, t, rw);
152f6476
JA
1332
1333 if (per_process_stats) {
bfc70ad5 1334 struct io_stats *ios = find_process_io_stats(t->pid);
152f6476 1335
b6076a9b 1336 __account_queue(ios, t, rw);
d0ca268b
JA
1337 }
1338}
1339
e21dc4dd 1340static inline void __account_c(struct io_stats *ios, int rw, int bytes)
d0ca268b
JA
1341{
1342 if (rw) {
152f6476
JA
1343 ios->cwrites++;
1344 ios->cwrite_kb += bytes >> 10;
cd0ae0f6 1345 ios->cwrite_b += bytes & 1023;
d0ca268b 1346 } else {
152f6476
JA
1347 ios->creads++;
1348 ios->cread_kb += bytes >> 10;
cd0ae0f6 1349 ios->cread_b += bytes & 1023;
152f6476
JA
1350 }
1351}
1352
1353static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1354 int rw, int bytes)
1355{
1356 __account_c(&pci->io_stats, rw, bytes);
1357
1358 if (per_process_stats) {
bfc70ad5 1359 struct io_stats *ios = find_process_io_stats(t->pid);
152f6476
JA
1360
1361 __account_c(ios, rw, bytes);
d0ca268b
JA
1362 }
1363}
1364
b6076a9b
JA
1365static inline void __account_issue(struct io_stats *ios, int rw,
1366 unsigned int bytes)
afd2d7ad 1367{
1368 if (rw) {
152f6476
JA
1369 ios->iwrites++;
1370 ios->iwrite_kb += bytes >> 10;
cd0ae0f6 1371 ios->iwrite_b += bytes & 1023;
afd2d7ad 1372 } else {
152f6476
JA
1373 ios->ireads++;
1374 ios->iread_kb += bytes >> 10;
cd0ae0f6 1375 ios->iread_b += bytes & 1023;
afd2d7ad 1376 }
1377}
1378
b6076a9b
JA
1379static inline void account_issue(struct blk_io_trace *t,
1380 struct per_cpu_info *pci, int rw)
d0ca268b 1381{
b6076a9b 1382 __account_issue(&pci->io_stats, rw, t->bytes);
152f6476
JA
1383
1384 if (per_process_stats) {
bfc70ad5 1385 struct io_stats *ios = find_process_io_stats(t->pid);
d5396421 1386
b6076a9b 1387 __account_issue(ios, rw, t->bytes);
152f6476
JA
1388 }
1389}
1390
06639b27
JA
1391static inline void __account_unplug(struct io_stats *ios, int timer)
1392{
1393 if (timer)
1394 ios->timer_unplugs++;
1395 else
1396 ios->io_unplugs++;
1397}
1398
1399static inline void account_unplug(struct blk_io_trace *t,
1400 struct per_cpu_info *pci, int timer)
1401{
1402 __account_unplug(&pci->io_stats, timer);
1403
1404 if (per_process_stats) {
bfc70ad5 1405 struct io_stats *ios = find_process_io_stats(t->pid);
06639b27
JA
1406
1407 __account_unplug(ios, timer);
1408 }
1409}
1410
4054070a
JA
1411static inline void __account_requeue(struct io_stats *ios,
1412 struct blk_io_trace *t, int rw)
1413{
1414 if (rw) {
1415 ios->wrqueue++;
1416 ios->iwrite_kb -= t_kb(t);
cd0ae0f6 1417 ios->iwrite_b -= t_b(t);
4054070a
JA
1418 } else {
1419 ios->rrqueue++;
1420 ios->iread_kb -= t_kb(t);
cd0ae0f6 1421 ios->iread_b -= t_b(t);
4054070a
JA
1422 }
1423}
1424
1425static inline void account_requeue(struct blk_io_trace *t,
1426 struct per_cpu_info *pci, int rw)
1427{
1428 __account_requeue(&pci->io_stats, t, rw);
1429
1430 if (per_process_stats) {
bfc70ad5 1431 struct io_stats *ios = find_process_io_stats(t->pid);
4054070a
JA
1432
1433 __account_requeue(ios, t, rw);
1434 }
1435}
1436
f7bd1a9b
JA
1437static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1438 struct blk_io_trace *t, char *act)
ab197ca7 1439{
f7bd1a9b 1440 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
ab197ca7
AB
1441}
1442
f7bd1a9b
JA
1443static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1444 struct blk_io_trace *t, char *act)
b6076a9b 1445{
f7bd1a9b 1446 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
b6076a9b
JA
1447}
1448
ab197ca7
AB
1449static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1450 char *act)
1451{
b6076a9b 1452 process_fmt(act, pci, t, -1, 0, NULL);
ab197ca7 1453}
2e3e8ded 1454
f7bd1a9b
JA
1455static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1456 struct blk_io_trace *t, char *act)
ab197ca7 1457{
f7bd1a9b 1458 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
d0ca268b
JA
1459}
1460
f7bd1a9b
JA
1461static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1462 struct blk_io_trace *t, char *act)
d0ca268b 1463{
a01516de 1464 if (act[0] == 'F')
f7bd1a9b 1465 log_track_frontmerge(pdi, t);
2e3e8ded 1466
ab197ca7 1467 process_fmt(act, pci, t, -1ULL, 0, NULL);
d0ca268b
JA
1468}
1469
dfe34da1 1470static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 1471 char *act)
dfe34da1 1472{
ab197ca7 1473 process_fmt(act, pci, t, -1ULL, 0, NULL);
dfe34da1
JA
1474}
1475
d5396421 1476static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 1477 char *act)
d0ca268b 1478{
ab197ca7 1479 process_fmt(act, pci, t, -1ULL, 0, NULL);
d0ca268b
JA
1480}
1481
ab197ca7 1482static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 1483 char *act)
67e14fdc 1484{
ab197ca7 1485 process_fmt(act, pci, t, -1ULL, 0, NULL);
67e14fdc
JA
1486}
1487
93f1c611
JA
1488static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1489 char *act)
1490{
1491 process_fmt(act, pci, t, -1ULL, 0, NULL);
1492}
1493
ab197ca7 1494static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
d0ca268b 1495{
ab197ca7 1496 unsigned char *buf = (unsigned char *) t + sizeof(*t);
d0ca268b 1497
ab197ca7 1498 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
d0ca268b
JA
1499}
1500
c82a8c9d
CS
1501static void dump_trace_pc(struct blk_io_trace *t, struct per_dev_info *pdi,
1502 struct per_cpu_info *pci)
d0ca268b 1503{
c82a8c9d 1504 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
56f2af81
JA
1505 int act = t->action & 0xffff;
1506
1507 switch (act) {
d0ca268b 1508 case __BLK_TA_QUEUE:
3639a11e 1509 log_generic(pci, t, "Q");
801646d6 1510 account_pc_queue(t, pci, w);
d0ca268b
JA
1511 break;
1512 case __BLK_TA_GETRQ:
3639a11e 1513 log_generic(pci, t, "G");
d0ca268b
JA
1514 break;
1515 case __BLK_TA_SLEEPRQ:
3639a11e 1516 log_generic(pci, t, "S");
d0ca268b
JA
1517 break;
1518 case __BLK_TA_REQUEUE:
c82a8c9d
CS
1519 /*
1520 * can happen if we miss traces, don't let it go
1521 * below zero
1522 */
1523 if (pdi->cur_depth[w])
1524 pdi->cur_depth[w]--;
801646d6 1525 account_pc_requeue(t, pci, w);
3639a11e 1526 log_generic(pci, t, "R");
d0ca268b
JA
1527 break;
1528 case __BLK_TA_ISSUE:
801646d6 1529 account_pc_issue(t, pci, w);
c82a8c9d
CS
1530 pdi->cur_depth[w]++;
1531 if (pdi->cur_depth[w] > pdi->max_depth[w])
1532 pdi->max_depth[w] = pdi->cur_depth[w];
ab197ca7 1533 log_pc(pci, t, "D");
d0ca268b
JA
1534 break;
1535 case __BLK_TA_COMPLETE:
c82a8c9d
CS
1536 if (pdi->cur_depth[w])
1537 pdi->cur_depth[w]--;
3639a11e 1538 log_pc(pci, t, "C");
801646d6 1539 account_pc_c(t, pci, w);
d0ca268b 1540 break;
56f2af81
JA
1541 case __BLK_TA_INSERT:
1542 log_pc(pci, t, "I");
1543 break;
d0ca268b 1544 default:
56f2af81 1545 fprintf(stderr, "Bad pc action %x\n", act);
87b72777 1546 break;
d0ca268b 1547 }
d0ca268b
JA
1548}
1549
f7bd1a9b
JA
1550static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1551 struct per_cpu_info *pci)
d0ca268b 1552{
649c7b66 1553 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
7997c5b0 1554 int act = t->action & 0xffff;
d0ca268b 1555
7997c5b0 1556 switch (act) {
d0ca268b 1557 case __BLK_TA_QUEUE:
753f9091 1558 log_track_queue(pdi, t);
b6076a9b 1559 account_queue(t, pci, w);
3639a11e 1560 log_queue(pci, t, "Q");
d0ca268b 1561 break;
b6076a9b 1562 case __BLK_TA_INSERT:
f7bd1a9b 1563 log_insert(pdi, pci, t, "I");
b6076a9b 1564 break;
d0ca268b 1565 case __BLK_TA_BACKMERGE:
152f6476 1566 account_m(t, pci, w);
f7bd1a9b 1567 log_merge(pdi, pci, t, "M");
d0ca268b
JA
1568 break;
1569 case __BLK_TA_FRONTMERGE:
152f6476 1570 account_m(t, pci, w);
f7bd1a9b 1571 log_merge(pdi, pci, t, "F");
d0ca268b
JA
1572 break;
1573 case __BLK_TA_GETRQ:
f7bd1a9b 1574 log_track_getrq(pdi, t);
3639a11e 1575 log_generic(pci, t, "G");
d0ca268b
JA
1576 break;
1577 case __BLK_TA_SLEEPRQ:
3639a11e 1578 log_generic(pci, t, "S");
d0ca268b
JA
1579 break;
1580 case __BLK_TA_REQUEUE:
65f2deb5
JA
1581 /*
1582 * can happen if we miss traces, don't let it go
1583 * below zero
1584 */
1585 if (pdi->cur_depth[w])
1586 pdi->cur_depth[w]--;
4054070a 1587 account_requeue(t, pci, w);
3639a11e 1588 log_queue(pci, t, "R");
d0ca268b
JA
1589 break;
1590 case __BLK_TA_ISSUE:
b6076a9b 1591 account_issue(t, pci, w);
649c7b66
JA
1592 pdi->cur_depth[w]++;
1593 if (pdi->cur_depth[w] > pdi->max_depth[w])
1594 pdi->max_depth[w] = pdi->cur_depth[w];
f7bd1a9b 1595 log_issue(pdi, pci, t, "D");
d0ca268b
JA
1596 break;
1597 case __BLK_TA_COMPLETE:
65f2deb5
JA
1598 if (pdi->cur_depth[w])
1599 pdi->cur_depth[w]--;
152f6476 1600 account_c(t, pci, w, t->bytes);
f7bd1a9b 1601 log_complete(pdi, pci, t, "C");
d0ca268b 1602 break;
88b1a526 1603 case __BLK_TA_PLUG:
3639a11e 1604 log_action(pci, t, "P");
88b1a526 1605 break;
3639a11e 1606 case __BLK_TA_UNPLUG_IO:
06639b27 1607 account_unplug(t, pci, 0);
3639a11e
JA
1608 log_unplug(pci, t, "U");
1609 break;
1610 case __BLK_TA_UNPLUG_TIMER:
06639b27 1611 account_unplug(t, pci, 1);
3639a11e 1612 log_unplug(pci, t, "UT");
88b1a526 1613 break;
93f1c611
JA
1614 case __BLK_TA_SPLIT:
1615 log_split(pci, t, "X");
1616 break;
1617 case __BLK_TA_BOUNCE:
1618 log_generic(pci, t, "B");
1619 break;
a8f30e64
JA
1620 case __BLK_TA_REMAP:
1621 log_generic(pci, t, "A");
1622 break;
c54b9dd9 1623 case __BLK_TA_DRV_DATA:
c701176c 1624 have_drv_data = 1;
c54b9dd9
SR
1625 /* dump to binary file only */
1626 break;
d0ca268b
JA
1627 default:
1628 fprintf(stderr, "Bad fs action %x\n", t->action);
1f79c4a0 1629 break;
d0ca268b 1630 }
d0ca268b
JA
1631}
1632
ff3a732c
JA
1633static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1634 struct per_dev_info *pdi)
d0ca268b 1635{
234db09d 1636 if (text_output) {
1a15f6a8
AB
1637 if (t->action == BLK_TN_MESSAGE)
1638 handle_notify(t);
1639 else if (t->action & BLK_TC_ACT(BLK_TC_PC))
c82a8c9d 1640 dump_trace_pc(t, pdi, pci);
234db09d
AB
1641 else
1642 dump_trace_fs(t, pdi, pci);
1643 }
87b72777 1644
20ed6177
JA
1645 if (!pdi->events)
1646 pdi->first_reported_time = t->time;
1647
e7c9f3ff 1648 pdi->events++;
a2594911 1649
19cfaf3f
AB
1650 if (bin_output_msgs ||
1651 !(t->action & BLK_TC_ACT(BLK_TC_NOTIFY) &&
1652 t->action == BLK_TN_MESSAGE))
1653 output_binary(t, sizeof(*t) + t->pdu_len);
d0ca268b
JA
1654}
1655
4c523165
JA
1656/*
1657 * print in a proper way, not too small and not too big. if more than
1658 * 1000,000K, turn into M and so on
1659 */
1660static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1661{
da19e768 1662 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
0dc3602c 1663 unsigned int i = 0;
4c523165
JA
1664
1665 if (in_kb)
1666 i++;
1667
0dc3602c 1668 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
4c523165
JA
1669 i++;
1670 num /= 1000;
1671 }
1672
1673 sprintf(dst, "%'8Lu%c", num, suff[i]);
1674 return dst;
1675}
1676
649c7b66
JA
1677static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1678 char *msg)
5c017e4b 1679{
4c523165
JA
1680 static char x[256], y[256];
1681
152f6476
JA
1682 fprintf(ofp, "%s\n", msg);
1683
cd0ae0f6
ID
1684 fprintf(ofp, " Reads Queued: %s, %siB\t",
1685 size_cnv(x, ios->qreads, 0),
1686 size_cnv(y, ios->qread_kb + (ios->qread_b>>10), 1));
1687 fprintf(ofp, " Writes Queued: %s, %siB\n",
1688 size_cnv(x, ios->qwrites, 0),
1689 size_cnv(y, ios->qwrite_kb + (ios->qwrite_b>>10), 1));
1690 fprintf(ofp, " Read Dispatches: %s, %siB\t",
1691 size_cnv(x, ios->ireads, 0),
1692 size_cnv(y, ios->iread_kb + (ios->iread_b>>10), 1));
1693 fprintf(ofp, " Write Dispatches: %s, %siB\n",
1694 size_cnv(x, ios->iwrites, 0),
1695 size_cnv(y, ios->iwrite_kb + (ios->iwrite_b>>10), 1));
4054070a
JA
1696 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1697 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
cd0ae0f6
ID
1698 fprintf(ofp, " Reads Completed: %s, %siB\t",
1699 size_cnv(x, ios->creads, 0),
1700 size_cnv(y, ios->cread_kb + (ios->cread_b>>10), 1));
1701 fprintf(ofp, " Writes Completed: %s, %siB\n",
1702 size_cnv(x, ios->cwrites, 0),
1703 size_cnv(y, ios->cwrite_kb + (ios->cwrite_b>>10), 1));
1704 fprintf(ofp, " Read Merges: %s, %siB\t",
1705 size_cnv(x, ios->mreads, 0),
1706 size_cnv(y, ios->mread_kb + (ios->mread_b>>10), 1));
1707 fprintf(ofp, " Write Merges: %s, %siB\n",
1708 size_cnv(x, ios->mwrites, 0),
1709 size_cnv(y, ios->mwrite_kb + (ios->mwrite_b>>10), 1));
649c7b66
JA
1710 if (pdi) {
1711 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1712 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1713 }
801646d6
CS
1714 if (ios->qreads_pc || ios->qwrites_pc || ios->ireads_pc || ios->iwrites_pc ||
1715 ios->rrqueue_pc || ios->wrqueue_pc || ios->creads_pc || ios->cwrites_pc) {
cd0ae0f6
ID
1716 fprintf(ofp, " PC Reads Queued: %s, %siB\t",
1717 size_cnv(x, ios->qreads_pc, 0),
1718 size_cnv(y,
1719 ios->qread_kb_pc + (ios->qread_b_pc>>10), 1));
1720 fprintf(ofp, " PC Writes Queued: %s, %siB\n",
1721 size_cnv(x, ios->qwrites_pc, 0),
1722 size_cnv(y,
1723 ios->qwrite_kb_pc + (ios->qwrite_b_pc>>10), 1));
1724 fprintf(ofp, " PC Read Disp.: %s, %siB\t",
1725 size_cnv(x, ios->ireads_pc, 0),
1726 size_cnv(y,
1727 ios->iread_kb_pc + (ios->iread_b_pc>>10), 1));
1728 fprintf(ofp, " PC Write Disp.: %s, %siB\n",
1729 size_cnv(x, ios->iwrites_pc, 0),
1730 size_cnv(y,
1731 ios->iwrite_kb_pc + (ios->iwrite_b_pc>>10),
1732 1));
801646d6
CS
1733 fprintf(ofp, " PC Reads Req.: %s\t\t", size_cnv(x, ios->rrqueue_pc, 0));
1734 fprintf(ofp, " PC Writes Req.: %s\n", size_cnv(x, ios->wrqueue_pc, 0));
1735 fprintf(ofp, " PC Reads Compl.: %s\t\t", size_cnv(x, ios->creads_pc, 0));
d0576a3a 1736 fprintf(ofp, " PC Writes Compl.: %s\n", size_cnv(x, ios->cwrites_pc, 0));
801646d6 1737 }
06639b27
JA
1738 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1739 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
5c017e4b
JA
1740}
1741
50adc0ba
JA
1742static void dump_wait_stats(struct per_process_info *ppi)
1743{
b9d40d6f
JA
1744 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1745 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1746 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1747 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1748 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1749 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1750
1751 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1752 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1753 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1754 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1755 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1756 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
50adc0ba
JA
1757}
1758
886ecf0e
JA
1759static int ppi_name_compare(const void *p1, const void *p2)
1760{
1761 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1762 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1763 int res;
1764
2990e589 1765 res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
886ecf0e 1766 if (!res)
2990e589 1767 res = ppi1->ppm->pid > ppi2->ppm->pid;
886ecf0e
JA
1768
1769 return res;
1770}
1771
a7263b8f
WZ
1772static int ppi_event_compare(const void *p1, const void *p2)
1773{
1774 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1775 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1776 struct io_stats *ios1 = &ppi1->io_stats;
1777 struct io_stats *ios2 = &ppi2->io_stats;
1778 unsigned long io1, io2;
1779 unsigned long long kb1,kb2;
1780 int sort_by_kb = 1;
1781
1782 io1 = io2 = 0;
1783 kb1 = kb2 = 0;
1784
1785 switch (per_process_stats_event) {
1786 case SORT_PROG_EVENT_QKB: /* KB: Queued read and write */
1787 kb1 = ios1->qwrite_kb + (ios1->qwrite_b>>10) +
1788 ios1->qread_kb + (ios1->qread_b>>10);
1789 kb2 = ios2->qwrite_kb + (ios2->qwrite_b>>10) +
1790 ios2->qread_kb + (ios2->qread_b>>10);
1791 break;
1792 case SORT_PROG_EVENT_RKB: /* KB: Queued Read */
1793 kb1 = ios1->qread_kb + (ios1->qread_b>>10);
1794 kb2 = ios2->qread_kb + (ios2->qread_b>>10);
1795 break;
1796 case SORT_PROG_EVENT_WKB: /* KB: Queued Write */
1797 kb1 = ios1->qwrite_kb + (ios1->qwrite_b>>10);
1798 kb2 = ios2->qwrite_kb + (ios2->qwrite_b>>10);
1799 break;
1800 case SORT_PROG_EVENT_CKB: /* KB: Complete */
1801 kb1 = ios1->cwrite_kb + (ios1->cwrite_b>>10) +
1802 ios1->cread_kb + (ios1->cread_b>>10);
1803 kb2 = ios2->cwrite_kb + (ios2->cwrite_b>>10) +
1804 ios2->cread_kb + (ios2->cread_b>>10);
1805 break;
1806 case SORT_PROG_EVENT_QIO: /* IO: Queued read and write */
1807 sort_by_kb = 0;
1808 io1 = ios1->qreads + ios1->qwrites;
1809 io2 = ios2->qreads + ios2->qwrites;
1810 break;
1811 case SORT_PROG_EVENT_RIO: /* IO: Queued Read */
1812 sort_by_kb = 0;
1813 io1 = ios1->qreads;
1814 io2 = ios2->qreads;
1815 break;
1816 case SORT_PROG_EVENT_WIO: /* IO: Queued Write */
1817 sort_by_kb = 0;
1818 io1 = ios1->qwrites;
1819 io2 = ios2->qwrites;
1820 break;
1821 case SORT_PROG_EVENT_CIO: /* IO: Complete */
1822 sort_by_kb = 0;
1823 io1 = ios1->creads + ios1->cwrites;
1824 io2 = ios2->creads + ios2->cwrites;
1825 break;
1826 }
1827
1828
1829 /* compare kb */
1830 if (sort_by_kb) {
1831 if (kb1 > kb2)
1832 return 1;
1833 else if (kb1 == kb2)
1834 return 0;
1835 return -1;
1836 }
1837
1838 /* compare io */
1839 if (io1 > io2)
1840 return 1;
1841 else if (io1 == io2)
1842 return 0;
1843 return -1;
1844}
1845
1846static int ppi_compare(const void *p1, const void *p2)
1847{
1848 if (per_process_stats_event == SORT_PROG_EVENT_N)
1849 return ppi_name_compare(p1, p2);
1850
1851 return ppi_event_compare(p1, p2);
1852}
1853
886ecf0e
JA
1854static void sort_process_list(void)
1855{
1856 struct per_process_info **ppis;
1857 struct per_process_info *ppi;
1858 int i = 0;
1859
1860 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1861
1862 ppi = ppi_list;
1863 while (ppi) {
06e6f286 1864 ppis[i++] = ppi;
886ecf0e
JA
1865 ppi = ppi->list_next;
1866 }
1867
a7263b8f 1868 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_compare);
886ecf0e
JA
1869
1870 i = ppi_list_entries - 1;
1871 ppi_list = NULL;
1872 while (i >= 0) {
1873 ppi = ppis[i];
1874
1875 ppi->list_next = ppi_list;
1876 ppi_list = ppi;
1877 i--;
1878 }
50c38702
JA
1879
1880 free(ppis);
886ecf0e
JA
1881}
1882
152f6476
JA
1883static void show_process_stats(void)
1884{
1885 struct per_process_info *ppi;
1886
886ecf0e
JA
1887 sort_process_list();
1888
152f6476
JA
1889 ppi = ppi_list;
1890 while (ppi) {
2990e589 1891 struct process_pid_map *ppm = ppi->ppm;
ce8b6b4f
JA
1892 char name[64];
1893
715d8021 1894 if (ppi->more_than_one)
2990e589 1895 sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
715d8021 1896 else
2990e589 1897 sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
bf0720af 1898
649c7b66 1899 dump_io_stats(NULL, &ppi->io_stats, name);
50adc0ba 1900 dump_wait_stats(ppi);
152f6476
JA
1901 ppi = ppi->list_next;
1902 }
1903
1904 fprintf(ofp, "\n");
1905}
1906
e7c9f3ff 1907static void show_device_and_cpu_stats(void)
d0ca268b 1908{
e7c9f3ff
NS
1909 struct per_dev_info *pdi;
1910 struct per_cpu_info *pci;
1911 struct io_stats total, *ios;
20ed6177 1912 unsigned long long rrate, wrate, msec;
e7c9f3ff
NS
1913 int i, j, pci_events;
1914 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1915 char name[32];
8a82e321 1916 double ratio;
e7c9f3ff
NS
1917
1918 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1919
1920 memset(&total, 0, sizeof(total));
1921 pci_events = 0;
1922
1923 if (i > 0)
1924 fprintf(ofp, "\n");
1925
1926 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1927 if (!pci->nelems)
1928 continue;
1929
1930 ios = &pci->io_stats;
1931 total.qreads += ios->qreads;
1932 total.qwrites += ios->qwrites;
1933 total.creads += ios->creads;
1934 total.cwrites += ios->cwrites;
1935 total.mreads += ios->mreads;
1936 total.mwrites += ios->mwrites;
1937 total.ireads += ios->ireads;
1938 total.iwrites += ios->iwrites;
4054070a
JA
1939 total.rrqueue += ios->rrqueue;
1940 total.wrqueue += ios->wrqueue;
e7c9f3ff
NS
1941 total.qread_kb += ios->qread_kb;
1942 total.qwrite_kb += ios->qwrite_kb;
1943 total.cread_kb += ios->cread_kb;
1944 total.cwrite_kb += ios->cwrite_kb;
1945 total.iread_kb += ios->iread_kb;
1946 total.iwrite_kb += ios->iwrite_kb;
fb2ec796
JA
1947 total.mread_kb += ios->mread_kb;
1948 total.mwrite_kb += ios->mwrite_kb;
cd0ae0f6
ID
1949 total.qread_b += ios->qread_b;
1950 total.qwrite_b += ios->qwrite_b;
1951 total.cread_b += ios->cread_b;
1952 total.cwrite_b += ios->cwrite_b;
1953 total.iread_b += ios->iread_b;
1954 total.iwrite_b += ios->iwrite_b;
1955 total.mread_b += ios->mread_b;
1956 total.mwrite_b += ios->mwrite_b;
801646d6
CS
1957
1958 total.qreads_pc += ios->qreads_pc;
1959 total.qwrites_pc += ios->qwrites_pc;
1960 total.creads_pc += ios->creads_pc;
1961 total.cwrites_pc += ios->cwrites_pc;
1962 total.ireads_pc += ios->ireads_pc;
1963 total.iwrites_pc += ios->iwrites_pc;
1964 total.rrqueue_pc += ios->rrqueue_pc;
1965 total.wrqueue_pc += ios->wrqueue_pc;
1966 total.qread_kb_pc += ios->qread_kb_pc;
1967 total.qwrite_kb_pc += ios->qwrite_kb_pc;
1968 total.iread_kb_pc += ios->iread_kb_pc;
1969 total.iwrite_kb_pc += ios->iwrite_kb_pc;
cd0ae0f6
ID
1970 total.qread_b_pc += ios->qread_b_pc;
1971 total.qwrite_b_pc += ios->qwrite_b_pc;
1972 total.iread_b_pc += ios->iread_b_pc;
1973 total.iwrite_b_pc += ios->iwrite_b_pc;
801646d6 1974
06639b27
JA
1975 total.timer_unplugs += ios->timer_unplugs;
1976 total.io_unplugs += ios->io_unplugs;
e7c9f3ff
NS
1977
1978 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1979 j, get_dev_name(pdi, name, sizeof(name)));
649c7b66 1980 dump_io_stats(pdi, ios, line);
e7c9f3ff
NS
1981 pci_events++;
1982 }
5c017e4b 1983
e7c9f3ff
NS
1984 if (pci_events > 1) {
1985 fprintf(ofp, "\n");
1986 snprintf(line, sizeof(line) - 1, "Total (%s):",
1987 get_dev_name(pdi, name, sizeof(name)));
649c7b66 1988 dump_io_stats(NULL, &total, line);
e7c9f3ff 1989 }
d0ca268b 1990
20ed6177 1991 wrate = rrate = 0;
20ed6177
JA
1992 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1993 if (msec) {
cd0ae0f6
ID
1994 rrate = ((1000 * total.cread_kb) + total.cread_b) /
1995 msec;
1996 wrate = ((1000 * total.cwrite_kb) + total.cwrite_b) /
1997 msec;
20ed6177
JA
1998 }
1999
dce0f678
AB
2000 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
2001 rrate, wrate);
2002 fprintf(ofp, "Events (%s): %'Lu entries\n",
2003 get_dev_name(pdi, line, sizeof(line)), pdi->events);
492da111
AB
2004
2005 collect_pdi_skips(pdi);
8a82e321
MZ
2006 if (!pdi->skips && !pdi->events)
2007 ratio = 0.0;
2008 else
2009 ratio = 100.0 * ((double)pdi->seq_skips /
2010 (double)(pdi->events + pdi->seq_skips));
492da111 2011 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
8a82e321 2012 pdi->skips, pdi->seq_skips, ratio);
e7c9f3ff 2013 }
d0ca268b
JA
2014}
2015
d025d6c6
HM
2016static void correct_abs_start_time(void)
2017{
2018 long delta = genesis_time - start_timestamp;
2019
2020 abs_start_time.tv_sec += SECONDS(delta);
2021 abs_start_time.tv_nsec += NANO_SECONDS(delta);
2022 if (abs_start_time.tv_nsec < 0) {
2023 abs_start_time.tv_nsec += 1000000000;
2024 abs_start_time.tv_sec -= 1;
2025 } else
2026 if (abs_start_time.tv_nsec > 1000000000) {
2027 abs_start_time.tv_nsec -= 1000000000;
2028 abs_start_time.tv_sec += 1;
2029 }
2030}
2031
4f0ae44f
JA
2032static void find_genesis(void)
2033{
2034 struct trace *t = trace_list;
2035
2036 genesis_time = -1ULL;
2037 while (t != NULL) {
2038 if (t->bit->time < genesis_time)
2039 genesis_time = t->bit->time;
2040
2041 t = t->next;
2042 }
7bd4fd0a
OK
2043
2044 /* The time stamp record will usually be the first
2045 * record in the trace, but not always.
2046 */
2047 if (start_timestamp
2048 && start_timestamp != genesis_time) {
d025d6c6 2049 correct_abs_start_time();
7bd4fd0a 2050 }
4f0ae44f
JA
2051}
2052
7f4d89e6 2053static inline int check_stopwatch(struct blk_io_trace *bit)
4f0ae44f 2054{
7f4d89e6
JA
2055 if (bit->time < stopwatch_end &&
2056 bit->time >= stopwatch_start)
4f0ae44f
JA
2057 return 0;
2058
2059 return 1;
2060}
2061
53c68c88
JA
2062/*
2063 * return youngest entry read
2064 */
2065static int sort_entries(unsigned long long *youngest)
4f0ae44f 2066{
210824c3
JA
2067 struct per_dev_info *pdi = NULL;
2068 struct per_cpu_info *pci = NULL;
4f0ae44f 2069 struct trace *t;
4f0ae44f
JA
2070
2071 if (!genesis_time)
2072 find_genesis();
2073
d6222db8 2074 *youngest = 0;
4f0ae44f
JA
2075 while ((t = trace_list) != NULL) {
2076 struct blk_io_trace *bit = t->bit;
2077
2078 trace_list = t->next;
2079
7f4d89e6 2080 bit->time -= genesis_time;
4f0ae44f 2081
d6222db8
JA
2082 if (bit->time < *youngest || !*youngest)
2083 *youngest = bit->time;
2084
210824c3
JA
2085 if (!pdi || pdi->dev != bit->device) {
2086 pdi = get_dev_info(bit->device);
2087 pci = NULL;
2088 }
2089
2090 if (!pci || pci->cpu != bit->cpu)
2091 pci = get_cpu_info(pdi, bit->cpu);
2092
2093 if (bit->sequence < pci->smallest_seq_read)
2094 pci->smallest_seq_read = bit->sequence;
774a1a10 2095
7f4d89e6 2096 if (check_stopwatch(bit)) {
4f0ae44f
JA
2097 bit_free(bit);
2098 t_free(t);
2099 continue;
2100 }
2101
2a1b3424 2102 if (trace_rb_insert_sort(t))
53c68c88 2103 return -1;
4f0ae44f
JA
2104 }
2105
53c68c88 2106 return 0;
4f0ae44f
JA
2107}
2108
824c2b39
JA
2109/*
2110 * to continue, we must have traces from all online cpus in the tree
2111 */
2112static int check_cpu_map(struct per_dev_info *pdi)
2113{
2114 unsigned long *cpu_map;
2115 struct rb_node *n;
2116 struct trace *__t;
2117 unsigned int i;
2118 int ret, cpu;
2119
2120 /*
2121 * create a map of the cpus we have traces for
2122 */
2123 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
cd992d08 2124 memset(cpu_map, 0, sizeof(*cpu_map));
824c2b39
JA
2125 n = rb_first(&rb_sort_root);
2126 while (n) {
2127 __t = rb_entry(n, struct trace, rb_node);
2128 cpu = __t->bit->cpu;
2129
2130 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
2131 n = rb_next(n);
2132 }
2133
2134 /*
b1c8e614
JA
2135 * we can't continue if pdi->cpu_map has entries set that we don't
2136 * have in the sort rbtree. the opposite is not a problem, though
824c2b39
JA
2137 */
2138 ret = 0;
2139 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
2140 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
2141 ret = 1;
2142 break;
2143 }
2144 }
2145
2146 free(cpu_map);
2147 return ret;
2148}
2149
a141a7cd 2150static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
2a1b3424 2151{
1ca323a5 2152 struct blk_io_trace *bit = t->bit;
210824c3
JA
2153 unsigned long expected_sequence;
2154 struct per_cpu_info *pci;
1ca323a5 2155 struct trace *__t;
492da111 2156
210824c3
JA
2157 pci = get_cpu_info(pdi, bit->cpu);
2158 expected_sequence = pci->last_sequence + 1;
2159
774a1a10 2160 if (!expected_sequence) {
774a1a10
JA
2161 /*
2162 * 1 should be the first entry, just allow it
2163 */
2164 if (bit->sequence == 1)
2165 return 0;
210824c3 2166 if (bit->sequence == pci->smallest_seq_read)
79ee9704 2167 return 0;
774a1a10 2168
824c2b39 2169 return check_cpu_map(pdi);
774a1a10 2170 }
2a1b3424
JA
2171
2172 if (bit->sequence == expected_sequence)
2173 return 0;
2174
2a1b3424 2175 /*
1c7c54aa
JA
2176 * we may not have seen that sequence yet. if we are not doing
2177 * the final run, break and wait for more entries.
1c24add6 2178 */
210824c3
JA
2179 if (expected_sequence < pci->smallest_seq_read) {
2180 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1ca323a5 2181 if (!__t)
1c7c54aa 2182 goto skip;
2a1b3424 2183
1ca323a5 2184 __put_trace_last(pdi, __t);
2a1b3424 2185 return 0;
a141a7cd
JA
2186 } else if (!force) {
2187 return 1;
0b07f23e 2188 } else {
1c7c54aa 2189skip:
66930177 2190 if (check_current_skips(pci, bit->sequence))
492da111
AB
2191 return 0;
2192
965eca2d 2193 if (expected_sequence < bit->sequence)
66930177 2194 insert_skip(pci, expected_sequence, bit->sequence - 1);
1c7c54aa
JA
2195 return 0;
2196 }
2a1b3424
JA
2197}
2198
a649216c 2199static void show_entries_rb(int force)
8fc0abbc 2200{
1f7afa72
JA
2201 struct per_dev_info *pdi = NULL;
2202 struct per_cpu_info *pci = NULL;
8fc0abbc 2203 struct blk_io_trace *bit;
3aabcd89 2204 struct rb_node *n;
8fc0abbc 2205 struct trace *t;
1f7afa72 2206
7d747d22 2207 while ((n = rb_first(&rb_sort_root)) != NULL) {
dd90748f 2208 if (is_done() && !force && !pipeline)
1f7afa72 2209 break;
8fc0abbc
JA
2210
2211 t = rb_entry(n, struct trace, rb_node);
2212 bit = t->bit;
2213
a43c1c17
JA
2214 if (read_sequence - t->read_sequence < 1 && !force)
2215 break;
2216
210824c3 2217 if (!pdi || pdi->dev != bit->device) {
287fa3d6 2218 pdi = get_dev_info(bit->device);
210824c3
JA
2219 pci = NULL;
2220 }
1f7afa72 2221
e7c9f3ff
NS
2222 if (!pdi) {
2223 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
2224 MAJOR(bit->device), MINOR(bit->device));
2225 break;
2226 }
1f7afa72 2227
9bf422b1
TM
2228 if (!(bit->action == BLK_TN_MESSAGE) &&
2229 check_sequence(pdi, t, force))
a141a7cd 2230 break;
cb2a1a62 2231
a141a7cd
JA
2232 if (!force && bit->time > last_allowed_time)
2233 break;
8fc0abbc 2234
4f0ae44f 2235 check_time(pdi, bit);
8fc0abbc 2236
4f0ae44f
JA
2237 if (!pci || pci->cpu != bit->cpu)
2238 pci = get_cpu_info(pdi, bit->cpu);
287fa3d6 2239
9bf422b1
TM
2240 if (!(bit->action == BLK_TN_MESSAGE))
2241 pci->last_sequence = bit->sequence;
210824c3 2242
cbc927b6
JA
2243 pci->nelems++;
2244
66930177 2245 if (bit->action & (act_mask << BLK_TC_SHIFT))
98f8386b 2246 dump_trace(bit, pci, pdi);
87b72777 2247
2a1b3424 2248 put_trace(pdi, t);
cb2a1a62 2249 }
8fc0abbc
JA
2250}
2251
c0e0dbc2 2252static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1f79c4a0
JA
2253{
2254 int ret, bytes_left, fl;
2255 void *p;
2256
c0e0dbc2
JA
2257 if (block != *fdblock) {
2258 fl = fcntl(fd, F_GETFL);
1f79c4a0 2259
c0e0dbc2
JA
2260 if (!block) {
2261 *fdblock = 0;
2262 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
2263 } else {
2264 *fdblock = 1;
2265 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
2266 }
2267 }
1f79c4a0
JA
2268
2269 bytes_left = bytes;
2270 p = buffer;
2271 while (bytes_left > 0) {
2272 ret = read(fd, p, bytes_left);
2273 if (!ret)
2274 return 1;
2275 else if (ret < 0) {
db7e0552 2276 if (errno != EAGAIN) {
1f79c4a0 2277 perror("read");
db7e0552
JA
2278 return -1;
2279 }
a649216c 2280
5c0f40f7
JA
2281 /*
2282 * never do partial reads. we can return if we
2283 * didn't read anything and we should not block,
2284 * otherwise wait for data
2285 */
2286 if ((bytes_left == bytes) && !block)
2287 return 1;
2288
2289 usleep(10);
2290 continue;
1f79c4a0
JA
2291 } else {
2292 p += ret;
2293 bytes_left -= ret;
2294 }
2295 }
2296
2297 return 0;
2298}
2299
017d1660
JA
2300static inline __u16 get_pdulen(struct blk_io_trace *bit)
2301{
2302 if (data_is_native)
2303 return bit->pdu_len;
2304
2305 return __bswap_16(bit->pdu_len);
2306}
2307
2308static inline __u32 get_magic(struct blk_io_trace *bit)
2309{
2310 if (data_is_native)
2311 return bit->magic;
2312
2313 return __bswap_32(bit->magic);
2314}
2315
c0e0dbc2 2316static int read_events(int fd, int always_block, int *fdblock)
cb2a1a62 2317{
287fa3d6 2318 struct per_dev_info *pdi = NULL;
e820abd7 2319 unsigned int events = 0;
7d747d22
JA
2320
2321 while (!is_done() && events < rb_batch) {
2322 struct blk_io_trace *bit;
2323 struct trace *t;
db7e0552 2324 int pdu_len, should_block, ret;
7d747d22
JA
2325 __u32 magic;
2326
d36421e4 2327 bit = bit_alloc();
cb2a1a62 2328
c0e0dbc2
JA
2329 should_block = !events || always_block;
2330
db7e0552
JA
2331 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
2332 if (ret) {
eb9bd4e9 2333 bit_free(bit);
db7e0552
JA
2334 if (!events && ret < 0)
2335 events = ret;
cb2a1a62 2336 break;
eb9bd4e9 2337 }
cb2a1a62 2338
017d1660
JA
2339 /*
2340 * look at first trace to check whether we need to convert
2341 * data in the future
2342 */
9e4cd1b8 2343 if (data_is_native == -1 && check_data_endianness(bit->magic))
017d1660
JA
2344 break;
2345
2346 magic = get_magic(bit);
7d747d22
JA
2347 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2348 fprintf(stderr, "Bad magic %x\n", magic);
2349 break;
2350 }
2351
017d1660 2352 pdu_len = get_pdulen(bit);
7d747d22
JA
2353 if (pdu_len) {
2354 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2355
c0e0dbc2 2356 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
eb9bd4e9 2357 bit_free(ptr);
7d747d22 2358 break;
eb9bd4e9 2359 }
7d747d22
JA
2360
2361 bit = ptr;
2362 }
2363
d6222db8
JA
2364 trace_to_cpu(bit);
2365
2366 if (verify_trace(bit)) {
2367 bit_free(bit);
2368 continue;
2369 }
2370
bfc70ad5
JA
2371 /*
2372 * not a real trace, so grab and handle it here
2373 */
1a15f6a8 2374 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) {
7bd4fd0a 2375 handle_notify(bit);
a2594911 2376 output_binary(bit, sizeof(*bit) + bit->pdu_len);
bfc70ad5
JA
2377 continue;
2378 }
2379
d36421e4 2380 t = t_alloc();
cb2a1a62
JA
2381 memset(t, 0, sizeof(*t));
2382 t->bit = bit;
a43c1c17 2383 t->read_sequence = read_sequence;
cb2a1a62 2384
7d747d22
JA
2385 t->next = trace_list;
2386 trace_list = t;
1f7afa72 2387
f7bd1a9b 2388 if (!pdi || pdi->dev != bit->device)
287fa3d6
JA
2389 pdi = get_dev_info(bit->device);
2390
2391 if (bit->time > pdi->last_read_time)
2392 pdi->last_read_time = bit->time;
2393
7d747d22 2394 events++;
cb2a1a62
JA
2395 }
2396
7d747d22 2397 return events;
cb2a1a62
JA
2398}
2399
70317a16
AB
2400/*
2401 * Managing input streams
2402 */
2403
2404struct ms_stream {
2405 struct ms_stream *next;
2406 struct trace *first, *last;
73877e12 2407 struct per_dev_info *pdi;
70317a16
AB
2408 unsigned int cpu;
2409};
d0ca268b 2410
70317a16 2411#define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff))
73877e12 2412
70317a16
AB
2413struct ms_stream *ms_head;
2414struct ms_stream *ms_hash[256];
87b72777 2415
70317a16
AB
2416static void ms_sort(struct ms_stream *msp);
2417static int ms_prime(struct ms_stream *msp);
2418
2419static inline struct trace *ms_peek(struct ms_stream *msp)
2420{
2421 return (msp == NULL) ? NULL : msp->first;
2422}
d0ca268b 2423
70317a16
AB
2424static inline __u64 ms_peek_time(struct ms_stream *msp)
2425{
2426 return ms_peek(msp)->bit->time;
2427}
d1d7f15f 2428
70317a16
AB
2429static inline void ms_resort(struct ms_stream *msp)
2430{
2431 if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) {
2432 ms_head = msp->next;
2433 msp->next = NULL;
2434 ms_sort(msp);
2435 }
2436}
e7c9f3ff 2437
70317a16
AB
2438static inline void ms_deq(struct ms_stream *msp)
2439{
2440 msp->first = msp->first->next;
2441 if (!msp->first) {
2442 msp->last = NULL;
2443 if (!ms_prime(msp)) {
2444 ms_head = msp->next;
2445 msp->next = NULL;
2446 return;
d0ca268b 2447 }
d5396421
JA
2448 }
2449
70317a16
AB
2450 ms_resort(msp);
2451}
53c68c88 2452
70317a16
AB
2453static void ms_sort(struct ms_stream *msp)
2454{
2455 __u64 msp_t = ms_peek_time(msp);
2456 struct ms_stream *this_msp = ms_head;
d5396421 2457
70317a16
AB
2458 if (this_msp == NULL)
2459 ms_head = msp;
2460 else if (msp_t < ms_peek_time(this_msp)) {
2461 msp->next = this_msp;
2462 ms_head = msp;
2463 }
2464 else {
2465 while (this_msp->next && ms_peek_time(this_msp->next) < msp_t)
2466 this_msp = this_msp->next;
73877e12 2467
70317a16
AB
2468 msp->next = this_msp->next;
2469 this_msp->next = msp;
2470 }
2471}
d5396421 2472
70317a16
AB
2473static int ms_prime(struct ms_stream *msp)
2474{
2475 __u32 magic;
2476 unsigned int i;
2477 struct trace *t;
2478 struct per_dev_info *pdi = msp->pdi;
2479 struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu);
2480 struct blk_io_trace *bit = NULL;
2481 int ret, pdu_len, ndone = 0;
d5396421 2482
70317a16
AB
2483 for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) {
2484 bit = bit_alloc();
2485 ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock);
2486 if (ret)
2487 goto err;
51128a28 2488
70317a16
AB
2489 if (data_is_native == -1 && check_data_endianness(bit->magic))
2490 goto err;
210824c3 2491
70317a16
AB
2492 magic = get_magic(bit);
2493 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2494 fprintf(stderr, "Bad magic %x\n", magic);
2495 goto err;
d5396421 2496
70317a16 2497 }
d5396421 2498
70317a16
AB
2499 pdu_len = get_pdulen(bit);
2500 if (pdu_len) {
2501 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2502 ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len,
2503 1, &pci->fdblock);
2504 if (ret) {
2505 free(ptr);
4eb899a6 2506 bit = NULL;
70317a16 2507 goto err;
7d747d22 2508 }
70317a16
AB
2509
2510 bit = ptr;
2ff323b0 2511 }
d5396421 2512
70317a16
AB
2513 trace_to_cpu(bit);
2514 if (verify_trace(bit))
2515 goto err;
53c68c88 2516
13d48592
TM
2517 if (bit->cpu != pci->cpu) {
2518 fprintf(stderr, "cpu %d trace info has error cpu %d\n",
2519 pci->cpu, bit->cpu);
2520 continue;
2521 }
2522
1a15f6a8 2523 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) {
7bd4fd0a 2524 handle_notify(bit);
70317a16
AB
2525 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2526 bit_free(bit);
287fa3d6 2527
70317a16
AB
2528 i -= 1;
2529 continue;
2530 }
cb2a1a62 2531
70317a16
AB
2532 if (bit->time > pdi->last_read_time)
2533 pdi->last_read_time = bit->time;
d5396421 2534
70317a16
AB
2535 t = t_alloc();
2536 memset(t, 0, sizeof(*t));
2537 t->bit = bit;
2538
2539 if (msp->first == NULL)
2540 msp->first = msp->last = t;
2541 else {
2542 msp->last->next = t;
2543 msp->last = t;
2544 }
2545
2546 ndone++;
2547 }
2548
2549 return ndone;
2550
2551err:
2552 if (bit) bit_free(bit);
2553
2554 cpu_mark_offline(pdi, pci->cpu);
2555 close(pci->fd);
2556 pci->fd = -1;
2557
2558 return ndone;
2559}
2560
2561static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu)
2562{
2563 struct ms_stream *msp = malloc(sizeof(*msp));
2564
2565 msp->next = NULL;
2566 msp->first = msp->last = NULL;
2567 msp->pdi = pdi;
2568 msp->cpu = cpu;
2569
2570 if (ms_prime(msp))
2571 ms_sort(msp);
2572
2573 return msp;
2574}
2575
2576static int setup_file(struct per_dev_info *pdi, int cpu)
2577{
2578 int len = 0;
2579 struct stat st;
2580 char *p, *dname;
2581 struct per_cpu_info *pci = get_cpu_info(pdi, cpu);
2582
2583 pci->cpu = cpu;
2584 pci->fdblock = -1;
2585
2586 p = strdup(pdi->name);
2587 dname = dirname(p);
2588 if (strcmp(dname, ".")) {
2589 input_dir = dname;
2590 p = strdup(pdi->name);
2591 strcpy(pdi->name, basename(p));
2592 }
2593 free(p);
2594
2595 if (input_dir)
2596 len = sprintf(pci->fname, "%s/", input_dir);
2597
2598 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
2599 "%s.blktrace.%d", pdi->name, pci->cpu);
8afe3d7d 2600 if (stat(pci->fname, &st) < 0)
70317a16 2601 return 0;
8afe3d7d
AB
2602 if (!st.st_size)
2603 return 1;
70317a16
AB
2604
2605 pci->fd = open(pci->fname, O_RDONLY);
2606 if (pci->fd < 0) {
2607 perror(pci->fname);
2608 return 0;
2609 }
2610
2611 printf("Input file %s added\n", pci->fname);
2612 cpu_mark_online(pdi, pci->cpu);
2613
2614 pdi->nfiles++;
2615 ms_alloc(pdi, pci->cpu);
2616
2617 return 1;
2618}
2619
2620static int handle(struct ms_stream *msp)
2621{
2622 struct trace *t;
2623 struct per_dev_info *pdi;
2624 struct per_cpu_info *pci;
2625 struct blk_io_trace *bit;
2626
2627 t = ms_peek(msp);
70317a16
AB
2628
2629 bit = t->bit;
2630 pdi = msp->pdi;
2631 pci = get_cpu_info(pdi, msp->cpu);
2632 pci->nelems++;
8091de93 2633 bit->time -= genesis_time;
7072ee3f
LU
2634
2635 if (t->bit->time > stopwatch_end)
2636 return 0;
2637
8091de93 2638 pdi->last_reported_time = bit->time;
7072ee3f
LU
2639 if ((bit->action & (act_mask << BLK_TC_SHIFT))&&
2640 t->bit->time >= stopwatch_start)
70317a16
AB
2641 dump_trace(bit, pci, pdi);
2642
2643 ms_deq(msp);
2644
2645 if (text_output)
2646 trace_rb_insert_last(pdi, t);
2647 else {
2648 bit_free(t->bit);
2649 t_free(t);
2650 }
2651
2652 return 1;
2653}
2654
7d340756
MZ
2655/*
2656 * Check if we need to sanitize the name. We allow 'foo', or if foo.blktrace.X
2657 * is given, then strip back down to 'foo' to avoid missing files.
2658 */
2659static int name_fixup(char *name)
2660{
2661 char *b;
2662
2663 if (!name)
2664 return 1;
2665
2666 b = strstr(name, ".blktrace.");
2667 if (b)
2668 *b = '\0';
2669
2670 return 0;
2671}
2672
70317a16
AB
2673static int do_file(void)
2674{
7d340756 2675 int i, cpu, ret;
70317a16
AB
2676 struct per_dev_info *pdi;
2677
2678 /*
2679 * first prepare all files for reading
2680 */
2681 for (i = 0; i < ndevices; i++) {
2682 pdi = &devices[i];
7d340756
MZ
2683 ret = name_fixup(pdi->name);
2684 if (ret)
2685 return ret;
2686
70317a16
AB
2687 for (cpu = 0; setup_file(pdi, cpu); cpu++)
2688 ;
a2b1f355
ES
2689
2690 if (!cpu) {
2691 fprintf(stderr,"No input files found for %s\n",
2692 pdi->name);
2693 return 1;
2694 }
70317a16
AB
2695 }
2696
8091de93
AB
2697 /*
2698 * Get the initial time stamp
2699 */
2700 if (ms_head)
2701 genesis_time = ms_peek_time(ms_head);
2702
918e9797
HM
2703 /*
2704 * Correct abs_start_time if necessary
2705 */
2706 if (start_timestamp
2707 && start_timestamp != genesis_time) {
2708 correct_abs_start_time();
2709 }
2710
70317a16
AB
2711 /*
2712 * Keep processing traces while any are left
2713 */
2714 while (!is_done() && ms_head && handle(ms_head))
2715 ;
a649216c 2716
7d747d22 2717 return 0;
412819ce 2718}
d5396421 2719
67076cbc 2720static void do_pipe(int fd)
412819ce 2721{
53c68c88 2722 unsigned long long youngest;
67076cbc 2723 int events, fdblock;
d5396421 2724
be925321 2725 last_allowed_time = -1ULL;
c0e0dbc2 2726 fdblock = -1;
db7e0552 2727 while ((events = read_events(fd, 0, &fdblock)) > 0) {
4ab42801 2728 read_sequence++;
412819ce 2729
210824c3 2730#if 0
0b07f23e 2731 smallest_seq_read = -1U;
210824c3 2732#endif
0b07f23e 2733
53c68c88
JA
2734 if (sort_entries(&youngest))
2735 break;
2736
2737 if (youngest > stopwatch_end)
2ff323b0
JA
2738 break;
2739
763d936e 2740 show_entries_rb(0);
0b07f23e 2741 }
d5396421 2742
a649216c
JA
2743 if (rb_sort_entries)
2744 show_entries_rb(1);
67076cbc
JA
2745}
2746
2747static int do_fifo(void)
2748{
2749 int fd;
2750
2751 if (!strcmp(pipename, "-"))
2752 fd = dup(STDIN_FILENO);
2753 else
2754 fd = open(pipename, O_RDONLY);
2755
2756 if (fd == -1) {
2757 perror("dup stdin");
2758 return -1;
2759 }
a649216c 2760
67076cbc 2761 do_pipe(fd);
d5396421 2762 close(fd);
d5396421
JA
2763 return 0;
2764}
d0ca268b 2765
cbc927b6 2766static void show_stats(void)
412819ce 2767{
cbc927b6
JA
2768 if (!ofp)
2769 return;
2770 if (stats_printed)
2771 return;
2772
2773 stats_printed = 1;
2774
2775 if (per_process_stats)
2776 show_process_stats();
2777
2778 if (per_device_and_cpu_stats)
2779 show_device_and_cpu_stats();
2780
152f6476 2781 fflush(ofp);
412819ce
JA
2782}
2783
e820abd7 2784static void handle_sigint(__attribute__((__unused__)) int sig)
412819ce
JA
2785{
2786 done = 1;
412819ce
JA
2787}
2788
46e6968b
NS
2789/*
2790 * Extract start and duration times from a string, allowing
2791 * us to specify a time interval of interest within a trace.
2792 * Format: "duration" (start is zero) or "start:duration".
2793 */
2794static int find_stopwatch_interval(char *string)
2795{
2796 double value;
2797 char *sp;
2798
2799 value = strtod(string, &sp);
2800 if (sp == string) {
2801 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2802 return 1;
2803 }
2804 if (*sp == ':') {
2805 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2806 string = sp + 1;
2807 value = strtod(string, &sp);
2808 if (sp == string || *sp != '\0') {
2809 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2810 string);
2811 return 1;
2812 }
2813 } else if (*sp != '\0') {
2814 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2815 return 1;
2816 }
1b928247
JA
2817 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2818 if (stopwatch_end <= stopwatch_start) {
2819 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2820 stopwatch_start, stopwatch_end);
2821 return 1;
2822 }
2823
46e6968b
NS
2824 return 0;
2825}
2826
67076cbc
JA
2827static int is_pipe(const char *str)
2828{
2829 struct stat st;
2830
2831 if (!strcmp(str, "-"))
2832 return 1;
2833 if (!stat(str, &st) && S_ISFIFO(st.st_mode))
2834 return 1;
2835
2836 return 0;
2837}
2838
a7263b8f
WZ
2839static int get_program_sort_event(const char *str)
2840{
2841 char evt = str[0];
2842
2843 switch (evt) {
2844 case 'N':
2845 per_process_stats_event = SORT_PROG_EVENT_N;
2846 break;
2847 case 'Q':
2848 per_process_stats_event = SORT_PROG_EVENT_QKB;
2849 break;
2850 case 'q':
2851 per_process_stats_event = SORT_PROG_EVENT_QIO;
2852 break;
2853 case 'R':
2854 per_process_stats_event = SORT_PROG_EVENT_RKB;
2855 break;
2856 case 'r':
2857 per_process_stats_event = SORT_PROG_EVENT_RIO;
2858 break;
2859 case 'W':
2860 per_process_stats_event = SORT_PROG_EVENT_WKB;
2861 break;
2862 case 'w':
2863 per_process_stats_event = SORT_PROG_EVENT_WIO;
2864 break;
2865 case 'C':
2866 per_process_stats_event = SORT_PROG_EVENT_CKB;
2867 break;
2868 case 'c':
2869 per_process_stats_event = SORT_PROG_EVENT_CIO;
2870 break;
2871 default:
2872 return 1;
2873 }
2874
2875 return 0;
2876}
2877
2878#define S_OPTS "a:A:b:D:d:f:F:hi:o:OqsS:tw:vVM"
234db09d
AB
2879static char usage_str[] = "\n\n" \
2880 "-i <file> | --input=<file>\n" \
2881 "[ -a <action field> | --act-mask=<action field> ]\n" \
2882 "[ -A <action mask> | --set-mask=<action mask> ]\n" \
2883 "[ -b <traces> | --batch=<traces> ]\n" \
2884 "[ -d <file> | --dump-binary=<file> ]\n" \
2885 "[ -D <dir> | --input-directory=<dir> ]\n" \
2886 "[ -f <format> | --format=<format> ]\n" \
2887 "[ -F <spec> | --format-spec=<spec> ]\n" \
2888 "[ -h | --hash-by-name ]\n" \
2889 "[ -o <file> | --output=<file> ]\n" \
2890 "[ -O | --no-text-output ]\n" \
2891 "[ -q | --quiet ]\n" \
2892 "[ -s | --per-program-stats ]\n" \
a7263b8f 2893 "[ -S <event> | --sort-program-stats=<event> ]\n" \
234db09d
AB
2894 "[ -t | --track-ios ]\n" \
2895 "[ -w <time> | --stopwatch=<time> ]\n" \
19cfaf3f 2896 "[ -M | --no-msgs\n" \
234db09d
AB
2897 "[ -v | --verbose ]\n" \
2898 "[ -V | --version ]\n\n" \
541c9bf6
ES
2899 "\t-a Only trace specified actions. See documentation\n" \
2900 "\t-A Give trace mask as a single value. See documentation\n" \
234db09d
AB
2901 "\t-b stdin read batching\n" \
2902 "\t-d Output file. If specified, binary data is written to file\n" \
d1d7f15f 2903 "\t-D Directory to prepend to input file names\n" \
234db09d
AB
2904 "\t-f Output format. Customize the output format. The format field\n" \
2905 "\t identifies can be found in the documentation\n" \
2906 "\t-F Format specification. Can be found in the documentation\n" \
2907 "\t-h Hash processes by name, not pid\n" \
2908 "\t-i Input file containing trace data, or '-' for stdin\n" \
52724a0e 2909 "\t-o Output file. If not given, output is stdout\n" \
234db09d
AB
2910 "\t-O Do NOT output text data\n" \
2911 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
52724a0e 2912 "\t-s Show per-program io statistics\n" \
a7263b8f
WZ
2913 "\t-S Show per-program io statistics sorted by N/Q/q/R/r/W/w/C/c\n" \
2914 "\t N:Name, Q/q:Queued(read & write), R/r:Queued Read, W/w:Queued Write, C/c:Complete.\n" \
2915 "\t Sort programs by how much data(KB): Q,R,W,C.\n" \
2916 "\t Sort programs by how many IO operations: q,r,w,c.\n" \
2917 "\t if -S was used, the -s parameter will be ignored.\n" \
52724a0e
JA
2918 "\t-t Track individual ios. Will tell you the time a request took\n" \
2919 "\t to get queued, to get dispatched, and to get completed\n" \
52724a0e
JA
2920 "\t-w Only parse data between the given time interval in seconds.\n" \
2921 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
19cfaf3f 2922 "\t-M Do not output messages to binary file\n" \
57ea8602
JA
2923 "\t-v More verbose for marginal errors\n" \
2924 "\t-V Print program version info\n\n";
52724a0e 2925
1f79c4a0
JA
2926static void usage(char *prog)
2927{
bc14c53f 2928 fprintf(stderr, "Usage: %s %s", prog, usage_str);
1f79c4a0
JA
2929}
2930
d5396421
JA
2931int main(int argc, char *argv[])
2932{
98f8386b 2933 int i, c, ret, mode;
98f8386b 2934 int act_mask_tmp = 0;
234db09d 2935 char *ofp_buffer = NULL;
346d8a74 2936 char *bin_ofp_buffer = NULL;
d5396421
JA
2937
2938 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2939 switch (c) {
98f8386b
AB
2940 case 'a':
2941 i = find_mask_map(optarg);
2942 if (i < 0) {
2943 fprintf(stderr,"Invalid action mask %s\n",
2944 optarg);
2945 return 1;
2946 }
2947 act_mask_tmp |= i;
2948 break;
2949
2950 case 'A':
2951 if ((sscanf(optarg, "%x", &i) != 1) ||
2952 !valid_act_opt(i)) {
2953 fprintf(stderr,
2954 "Invalid set action mask %s/0x%x\n",
2955 optarg, i);
2956 return 1;
2957 }
2958 act_mask_tmp = i;
2959 break;
d5396421 2960 case 'i':
67076cbc 2961 if (is_pipe(optarg) && !pipeline) {
e7c9f3ff 2962 pipeline = 1;
67076cbc
JA
2963 pipename = strdup(optarg);
2964 } else if (resize_devices(optarg) != 0)
e7c9f3ff 2965 return 1;
d5396421 2966 break;
d1d7f15f
JA
2967 case 'D':
2968 input_dir = optarg;
2969 break;
d5396421 2970 case 'o':
66efebf8 2971 output_name = optarg;
d5396421 2972 break;
234db09d
AB
2973 case 'O':
2974 text_output = 0;
2975 break;
79f19470
JA
2976 case 'b':
2977 rb_batch = atoi(optarg);
2978 if (rb_batch <= 0)
2979 rb_batch = RB_BATCH_DEFAULT;
2980 break;
152f6476
JA
2981 case 's':
2982 per_process_stats = 1;
2983 break;
a7263b8f
WZ
2984 case 'S':
2985 per_process_stats = 1;
2986 if (get_program_sort_event(optarg))
2987 return 1;
2988 break;
7997c5b0
JA
2989 case 't':
2990 track_ios = 1;
2991 break;
1e1c60f1
NS
2992 case 'q':
2993 per_device_and_cpu_stats = 0;
2994 break;
46e6968b
NS
2995 case 'w':
2996 if (find_stopwatch_interval(optarg) != 0)
2997 return 1;
2998 break;
ab197ca7
AB
2999 case 'f':
3000 set_all_format_specs(optarg);
3001 break;
3002 case 'F':
3003 if (add_format_spec(optarg) != 0)
3004 return 1;
3005 break;
d915dee6 3006 case 'h':
715d8021 3007 ppi_hash_by_pid = 0;
bf0720af 3008 break;
52724a0e 3009 case 'v':
57ea8602
JA
3010 verbose++;
3011 break;
3012 case 'V':
52724a0e
JA
3013 printf("%s version %s\n", argv[0], blkparse_version);
3014 return 0;
a2594911
AB
3015 case 'd':
3016 dump_binary = optarg;
3017 break;
19cfaf3f
AB
3018 case 'M':
3019 bin_output_msgs = 0;
3020 break;
d5396421 3021 default:
1f79c4a0 3022 usage(argv[0]);
d5396421
JA
3023 return 1;
3024 }
d0ca268b
JA
3025 }
3026
e7c9f3ff 3027 while (optind < argc) {
67076cbc 3028 if (is_pipe(argv[optind]) && !pipeline) {
e7c9f3ff 3029 pipeline = 1;
00cd3044 3030 pipename = strdup(argv[optind]);
67076cbc 3031 } else if (resize_devices(argv[optind]) != 0)
e7c9f3ff
NS
3032 return 1;
3033 optind++;
3034 }
3035
3036 if (!pipeline && !ndevices) {
1f79c4a0 3037 usage(argv[0]);
d5396421
JA
3038 return 1;
3039 }
3040
98f8386b
AB
3041 if (act_mask_tmp != 0)
3042 act_mask = act_mask_tmp;
3043
7997c5b0 3044 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
412819ce
JA
3045
3046 signal(SIGINT, handle_sigint);
3047 signal(SIGHUP, handle_sigint);
3048 signal(SIGTERM, handle_sigint);
d5396421 3049
d69db225
JA
3050 setlocale(LC_NUMERIC, "en_US");
3051
234db09d
AB
3052 if (text_output) {
3053 if (!output_name) {
3054 ofp = fdopen(STDOUT_FILENO, "w");
3055 mode = _IOLBF;
3056 } else {
c3ce73f5 3057 char ofname[PATH_MAX];
152f6476 3058
234db09d
AB
3059 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
3060 ofp = fopen(ofname, "w");
3061 mode = _IOFBF;
3062 }
152f6476 3063
234db09d
AB
3064 if (!ofp) {
3065 perror("fopen");
3066 return 1;
3067 }
152f6476 3068
234db09d
AB
3069 ofp_buffer = malloc(4096);
3070 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
3071 perror("setvbuf");
3072 return 1;
3073 }
152f6476
JA
3074 }
3075
a2594911 3076 if (dump_binary) {
cf659442
JA
3077 if (!strcmp(dump_binary, "-"))
3078 dump_fp = stdout;
3079 else {
3080 dump_fp = fopen(dump_binary, "w");
3081 if (!dump_fp) {
3082 perror(dump_binary);
3083 dump_binary = NULL;
3084 return 1;
3085 }
a2594911 3086 }
346d8a74
AB
3087 bin_ofp_buffer = malloc(128 * 1024);
3088 if (setvbuf(dump_fp, bin_ofp_buffer, _IOFBF, 128 * 1024)) {
3089 perror("setvbuf binary");
3090 return 1;
3091 }
a2594911
AB
3092 }
3093
e7c9f3ff 3094 if (pipeline)
67076cbc 3095 ret = do_fifo();
d5396421
JA
3096 else
3097 ret = do_file();
3098
fb863d7c
MZ
3099 if (!ret)
3100 show_stats();
3101
c701176c
MP
3102 if (have_drv_data && !dump_binary)
3103 printf("\ndiscarded traces containing low-level device driver "
3104 "specific data (only available in binary output)\n");
3105
8091de93
AB
3106 if (ofp_buffer) {
3107 fflush(ofp);
234db09d 3108 free(ofp_buffer);
8091de93
AB
3109 }
3110 if (bin_ofp_buffer) {
3111 fflush(dump_fp);
346d8a74 3112 free(bin_ofp_buffer);
8091de93 3113 }
d5396421 3114 return ret;
d0ca268b 3115}