[PATCH] README: update to say it needs 2.6.14-rc1 or newer
[blktrace.git] / blkparse.c
CommitLineData
d956a2cd
JA
1/*
2 * block queue tracing parse application
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
d0ca268b
JA
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <unistd.h>
24#include <stdio.h>
25#include <fcntl.h>
26#include <stdlib.h>
8fc0abbc 27#include <string.h>
d5396421 28#include <getopt.h>
412819ce
JA
29#include <errno.h>
30#include <signal.h>
d69db225 31#include <locale.h>
46e6968b 32#include <limits.h>
d0ca268b 33
8fc0abbc
JA
34#include "blktrace.h"
35#include "rbtree.h"
d0ca268b 36
2e3e8ded
JA
37#define SECONDS(x) ((unsigned long long)(x) / 1000000000)
38#define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000)
46e6968b 39#define DOUBLE_TO_NANO_ULL(d) ((unsigned long long)((d) * 1000000000))
cfab07eb 40
e7c9f3ff
NS
41#define MINORBITS 20
42#define MINORMASK ((1U << MINORBITS) - 1)
43#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
44#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
45
46#define min(a, b) ((a) < (b) ? (a) : (b))
d5396421 47
152f6476
JA
48struct io_stats {
49 unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
50 unsigned long ireads, iwrites;
51 unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
52 unsigned long long iread_kb, iwrite_kb;
53};
54
d5396421 55struct per_cpu_info {
d0ca268b
JA
56 int cpu;
57 int nelems;
d0ca268b
JA
58
59 int fd;
87b72777 60 char fname[128];
d0ca268b 61
152f6476
JA
62 struct io_stats io_stats;
63};
8fc0abbc 64
e7c9f3ff
NS
65struct per_dev_info {
66 dev_t id;
67 char *name;
68
69 int backwards;
70 unsigned long long events;
71 unsigned long long last_reported_time;
72 struct io_stats io_stats;
73
74 int ncpus;
75 struct per_cpu_info *cpus;
76};
77
152f6476
JA
78struct per_process_info {
79 char name[16];
80 __u32 pid;
81 struct io_stats io_stats;
82 struct per_process_info *hash_next, *list_next;
50adc0ba
JA
83
84 /*
85 * individual io stats
86 */
b9d40d6f
JA
87 unsigned long long longest_allocation_wait[2];
88 unsigned long long longest_dispatch_wait[2];
89 unsigned long long longest_completion_wait[2];
d0ca268b
JA
90};
91
152f6476
JA
92#define PPI_HASH_SHIFT (8)
93static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
94static struct per_process_info *ppi_list;
95
46e6968b 96#define S_OPTS "i:o:b:stqw:"
d5396421
JA
97static struct option l_opts[] = {
98 {
99 .name = "input",
100 .has_arg = 1,
101 .flag = NULL,
102 .val = 'i'
103 },
104 {
105 .name = "output",
106 .has_arg = 1,
107 .flag = NULL,
108 .val = 'o'
109 },
79f19470
JA
110 {
111 .name = "batch",
112 .has_arg = 1,
113 .flag = NULL,
114 .val = 'b'
115 },
152f6476
JA
116 {
117 .name = "per program stats",
118 .has_arg = 0,
119 .flag = NULL,
120 .val = 's'
121 },
7997c5b0
JA
122 {
123 .name = "track ios",
124 .has_arg = 0,
125 .flag = NULL,
126 .val = 't'
127 },
1e1c60f1
NS
128 {
129 .name = "quiet",
130 .has_arg = 0,
131 .flag = NULL,
132 .val = 'q'
133 },
46e6968b
NS
134 {
135 .name = "stopwatch",
136 .has_arg = 1,
137 .flag = NULL,
138 .val = 'w'
139 },
d5396421
JA
140 {
141 .name = NULL,
142 .has_arg = 0,
143 .flag = NULL,
144 .val = 0
145 }
146};
147
7997c5b0
JA
148static struct rb_root rb_sort_root;
149static struct rb_root rb_track_root;
8fc0abbc 150
7997c5b0
JA
151/*
152 * for sorting the displayed output
153 */
8fc0abbc
JA
154struct trace {
155 struct blk_io_trace *bit;
156 struct rb_node rb_node;
157};
158
7997c5b0
JA
159/*
160 * for tracking individual ios
161 */
162struct io_track {
163 struct rb_node rb_node;
164
e7c9f3ff 165 dev_t device;
7997c5b0
JA
166 __u64 sector;
167 __u32 pid;
95c15013 168 unsigned long long allocation_time;
7997c5b0
JA
169 unsigned long long queue_time;
170 unsigned long long dispatch_time;
171 unsigned long long completion_time;
172};
173
e7c9f3ff
NS
174static int ndevices;
175static struct per_dev_info *devices;
176static char *get_dev_name(struct per_dev_info *, char *, int);
d0ca268b 177
152f6476 178static FILE *ofp;
e7c9f3ff
NS
179static char *output_name;
180
181static unsigned long long genesis_time;
46e6968b
NS
182static unsigned long long stopwatch_start; /* start from zero by default */
183static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
152f6476
JA
184
185static int per_process_stats;
7997c5b0 186static int track_ios;
d0ca268b 187
79f19470
JA
188#define RB_BATCH_DEFAULT (1024)
189static int rb_batch = RB_BATCH_DEFAULT;
190
e7c9f3ff
NS
191static int pipeline;
192
412819ce
JA
193#define is_done() (*(volatile int *)(&done))
194static volatile int done;
195
152f6476
JA
196static inline unsigned long hash_long(unsigned long val)
197{
16ef714e
JA
198#if __WORDSIZE == 32
199 val *= 0x9e370001UL;
200#elif __WORDSIZE == 64
201 val *= 0x9e37fffffffc0001UL;
202#else
203#error unknown word size
204#endif
205
206 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
152f6476
JA
207}
208
209static inline void add_process_to_hash(struct per_process_info *ppi)
210{
211 const int hash_idx = hash_long(ppi->pid);
212
213 ppi->hash_next = ppi_hash[hash_idx];
214 ppi_hash[hash_idx] = ppi;
215}
216
217static inline void add_process_to_list(struct per_process_info *ppi)
218{
219 ppi->list_next = ppi_list;
220 ppi_list = ppi;
221}
222
223static struct per_process_info *find_process_by_pid(__u32 pid)
224{
225 const int hash_idx = hash_long(pid);
226 struct per_process_info *ppi;
227
228 ppi = ppi_hash[hash_idx];
229 while (ppi) {
230 if (ppi->pid == pid)
231 return ppi;
232
233 ppi = ppi->hash_next;
234 }
235
236 return NULL;
237}
238
7997c5b0
JA
239static inline int trace_rb_insert(struct trace *t)
240{
241 struct rb_node **p = &rb_sort_root.rb_node;
242 struct rb_node *parent = NULL;
243 struct trace *__t;
244
e7c9f3ff
NS
245 if (genesis_time == 0 || t->bit->time < genesis_time)
246 genesis_time = t->bit->time;
247
7997c5b0
JA
248 while (*p) {
249 parent = *p;
250 __t = rb_entry(parent, struct trace, rb_node);
251
e7c9f3ff
NS
252 if (t->bit->time < __t->bit->time)
253 p = &(*p)->rb_left;
254 else if (t->bit->time > __t->bit->time)
255 p = &(*p)->rb_right;
256 else if (t->bit->device < __t->bit->device)
257 p = &(*p)->rb_left;
258 else if (t->bit->device > __t->bit->device)
259 p = &(*p)->rb_right;
260 else if (t->bit->sequence < __t->bit->sequence)
7997c5b0
JA
261 p = &(*p)->rb_left;
262 else if (t->bit->sequence > __t->bit->sequence)
263 p = &(*p)->rb_right;
e7c9f3ff
NS
264 else if (t->bit->device == __t->bit->device) {
265 fprintf(stderr,
266 "sequence alias (%d) on device %d,%d!\n",
267 t->bit->sequence,
268 MAJOR(t->bit->device), MINOR(t->bit->device));
7997c5b0
JA
269 return 1;
270 }
271 }
272
273 rb_link_node(&t->rb_node, parent, p);
274 rb_insert_color(&t->rb_node, &rb_sort_root);
275 return 0;
276}
277
278static inline int track_rb_insert(struct io_track *iot)
279{
280 struct rb_node **p = &rb_track_root.rb_node;
281 struct rb_node *parent = NULL;
282 struct io_track *__iot;
283
284 while (*p) {
285 parent = *p;
e7c9f3ff 286
7997c5b0
JA
287 __iot = rb_entry(parent, struct io_track, rb_node);
288
e7c9f3ff
NS
289 if (iot->device < __iot->device)
290 p = &(*p)->rb_left;
291 else if (iot->device > __iot->device)
292 p = &(*p)->rb_right;
293 else if (iot->sector < __iot->sector)
7997c5b0
JA
294 p = &(*p)->rb_left;
295 else if (iot->sector > __iot->sector)
296 p = &(*p)->rb_right;
297 else {
e7c9f3ff
NS
298 fprintf(stderr,
299 "sector alias (%llu) on device %d,%d!\n",
300 iot->sector,
301 MAJOR(iot->device), MINOR(iot->device));
7997c5b0
JA
302 return 1;
303 }
304 }
305
306 rb_link_node(&iot->rb_node, parent, p);
307 rb_insert_color(&iot->rb_node, &rb_track_root);
308 return 0;
309}
310
e7c9f3ff 311static struct io_track *__find_track(dev_t device, __u64 sector)
7997c5b0
JA
312{
313 struct rb_node **p = &rb_track_root.rb_node;
314 struct rb_node *parent = NULL;
315 struct io_track *__iot;
316
317 while (*p) {
318 parent = *p;
319
320 __iot = rb_entry(parent, struct io_track, rb_node);
321
e7c9f3ff
NS
322 if (device < __iot->device)
323 p = &(*p)->rb_left;
324 else if (device > __iot->device)
325 p = &(*p)->rb_right;
326 else if (sector < __iot->sector)
7997c5b0
JA
327 p = &(*p)->rb_left;
328 else if (sector > __iot->sector)
329 p = &(*p)->rb_right;
330 else
331 return __iot;
332 }
333
334 return NULL;
335}
336
e7c9f3ff 337static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
7997c5b0 338{
916b5501 339 struct io_track *iot;
7997c5b0 340
e7c9f3ff 341 iot = __find_track(device, sector);
7997c5b0
JA
342 if (!iot) {
343 iot = malloc(sizeof(*iot));
50adc0ba 344 iot->pid = pid;
e7c9f3ff 345 iot->device = device;
7997c5b0
JA
346 iot->sector = sector;
347 track_rb_insert(iot);
348 }
349
350 return iot;
351}
352
2e3e8ded
JA
353static void log_track_merge(struct blk_io_trace *t)
354{
355 struct io_track *iot;
356
357 if (!track_ios)
358 return;
359 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
360 return;
361
e7c9f3ff 362 iot = __find_track(t->device, t->sector - (t->bytes >> 10));
2e3e8ded
JA
363 if (!iot) {
364 fprintf(stderr, "Trying to merge on non-existing request\n");
365 return;
366 }
367
368 rb_erase(&iot->rb_node, &rb_track_root);
369 iot->sector -= t->bytes >> 10;
370 track_rb_insert(iot);
371}
372
95c15013 373static void log_track_getrq(struct blk_io_trace *t)
2e3e8ded
JA
374{
375 struct io_track *iot;
376
377 if (!track_ios)
378 return;
379
e7c9f3ff 380 iot = find_track(t->pid, t->device, t->sector);
95c15013
JA
381 iot->allocation_time = t->time;
382}
383
384
385/*
386 * return time between rq allocation and queue
387 */
388static unsigned long long log_track_queue(struct blk_io_trace *t)
389{
50adc0ba 390 unsigned long long elapsed;
95c15013
JA
391 struct io_track *iot;
392
393 if (!track_ios)
394 return -1;
395
e7c9f3ff 396 iot = find_track(t->pid, t->device, t->sector);
2e3e8ded 397 iot->queue_time = t->time;
50adc0ba
JA
398 elapsed = iot->queue_time - iot->allocation_time;
399
400 if (per_process_stats) {
401 struct per_process_info *ppi = find_process_by_pid(iot->pid);
b9d40d6f 402 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 403
b9d40d6f
JA
404 if (ppi && elapsed > ppi->longest_allocation_wait[w])
405 ppi->longest_allocation_wait[w] = elapsed;
50adc0ba
JA
406 }
407
408 return elapsed;
2e3e8ded
JA
409}
410
411/*
412 * return time between queue and issue
413 */
414static unsigned long long log_track_issue(struct blk_io_trace *t)
415{
50adc0ba 416 unsigned long long elapsed;
2e3e8ded
JA
417 struct io_track *iot;
418
419 if (!track_ios)
420 return -1;
421 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
422 return -1;
423
e7c9f3ff 424 iot = __find_track(t->device, t->sector);
2e3e8ded
JA
425 if (!iot) {
426 fprintf(stderr, "Trying to issue on non-existing request\n");
427 return -1;
428 }
429
430 iot->dispatch_time = t->time;
50adc0ba
JA
431 elapsed = iot->dispatch_time - iot->queue_time;
432
433 if (per_process_stats) {
434 struct per_process_info *ppi = find_process_by_pid(iot->pid);
b9d40d6f 435 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 436
b9d40d6f
JA
437 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
438 ppi->longest_dispatch_wait[w] = elapsed;
50adc0ba
JA
439 }
440
441 return elapsed;
2e3e8ded
JA
442}
443
444/*
445 * return time between dispatch and complete
446 */
447static unsigned long long log_track_complete(struct blk_io_trace *t)
448{
449 unsigned long long elapsed;
450 struct io_track *iot;
451
452 if (!track_ios)
453 return -1;
454 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
455 return -1;
456
e7c9f3ff 457 iot = __find_track(t->device, t->sector);
2e3e8ded
JA
458 if (!iot) {
459 fprintf(stderr, "Trying to dispatch on non-existing request\n");
460 return -1;
461 }
462
463 iot->completion_time = t->time;
464 elapsed = iot->completion_time - iot->dispatch_time;
465
50adc0ba
JA
466 if (per_process_stats) {
467 struct per_process_info *ppi = find_process_by_pid(iot->pid);
b9d40d6f 468 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 469
b9d40d6f
JA
470 if (ppi && elapsed > ppi->longest_completion_wait[w])
471 ppi->longest_completion_wait[w] = elapsed;
50adc0ba
JA
472 }
473
2e3e8ded
JA
474 /*
475 * kill the trace, we don't need it after completion
476 */
477 rb_erase(&iot->rb_node, &rb_track_root);
478 free(iot);
479
480 return elapsed;
481}
482
483
152f6476
JA
484static struct io_stats *find_process_io_stats(__u32 pid, char *name)
485{
486 struct per_process_info *ppi = find_process_by_pid(pid);
487
488 if (!ppi) {
489 ppi = malloc(sizeof(*ppi));
490 memset(ppi, 0, sizeof(*ppi));
491 strncpy(ppi->name, name, sizeof(ppi->name));
492 ppi->pid = pid;
493 add_process_to_hash(ppi);
494 add_process_to_list(ppi);
495 }
496
497 return &ppi->io_stats;
498}
499
e7c9f3ff
NS
500
501static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
a718bd37 502{
e7c9f3ff
NS
503 struct per_cpu_info *cpus = pdi->cpus;
504 int ncpus = pdi->ncpus;
505 int new_count = cpu + 1;
506 int new_space, size;
a718bd37
NS
507 char *new_start;
508
e7c9f3ff
NS
509 size = new_count * sizeof(struct per_cpu_info);
510 cpus = realloc(cpus, size);
511 if (!cpus) {
512 char name[20];
513 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
514 get_dev_name(pdi, name, sizeof(name)), size);
a718bd37
NS
515 exit(1);
516 }
517
e7c9f3ff
NS
518 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
519 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
a718bd37 520 memset(new_start, 0, new_space);
e7c9f3ff
NS
521
522 pdi->ncpus = new_count;
523 pdi->cpus = cpus;
524}
525
526static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
527{
528 if (cpu >= pdi->ncpus)
529 resize_cpu_info(pdi, cpu);
530 return &pdi->cpus[cpu];
a718bd37
NS
531}
532
e7c9f3ff
NS
533
534static int resize_devices(char *name)
a718bd37 535{
e7c9f3ff 536 int size = (ndevices + 1) * sizeof(struct per_dev_info);
c499bf38 537
e7c9f3ff
NS
538 devices = realloc(devices, size);
539 if (!devices) {
540 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
541 return 1;
542 }
543 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
544 devices[ndevices].name = name;
545 ndevices++;
546 return 0;
547}
a718bd37 548
e7c9f3ff
NS
549static struct per_dev_info *get_dev_info(dev_t id, int create)
550{
551 int i;
c499bf38 552
e7c9f3ff
NS
553 for (i = 0; i < ndevices; i++)
554 if (devices[i].id == id)
555 return &devices[i];
556 if (!create)
557 return NULL;
558 if (resize_devices(NULL) != 0)
559 return NULL;
560 return &devices[ndevices-1];
a718bd37
NS
561}
562
e7c9f3ff
NS
563static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
564{
565 if (pdi->name)
566 snprintf(buffer, size, "%s", pdi->name);
567 else
568 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
569 return buffer;
570}
571
572
573static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
cfab07eb
AB
574{
575 unsigned long long this = bit->time;
e7c9f3ff 576 unsigned long long last = pdi->last_reported_time;
cfab07eb 577
e7c9f3ff
NS
578 pdi->backwards = (this < last) ? 'B' : ' ';
579 pdi->last_reported_time = this;
cfab07eb
AB
580}
581
e7c9f3ff 582
152f6476
JA
583static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
584 int rw)
d0ca268b
JA
585{
586 if (rw) {
152f6476
JA
587 ios->mwrites++;
588 ios->qwrite_kb += t->bytes >> 10;
d0ca268b 589 } else {
152f6476
JA
590 ios->mreads++;
591 ios->qread_kb += t->bytes >> 10;
592 }
593}
594
595static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
596 int rw)
597{
598 __account_m(&pci->io_stats, t, rw);
599
600 if (per_process_stats) {
601 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
602
603 __account_m(ios, t, rw);
d0ca268b
JA
604 }
605}
606
152f6476
JA
607static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
608 int rw)
d0ca268b
JA
609{
610 if (rw) {
152f6476
JA
611 ios->qwrites++;
612 ios->qwrite_kb += t->bytes >> 10;
d0ca268b 613 } else {
152f6476
JA
614 ios->qreads++;
615 ios->qread_kb += t->bytes >> 10;
616 }
617}
618
619static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
620 int rw)
621{
622 __account_q(&pci->io_stats, t, rw);
623
624 if (per_process_stats) {
625 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
626
627 __account_q(ios, t, rw);
d0ca268b
JA
628 }
629}
630
152f6476 631static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
d0ca268b
JA
632{
633 if (rw) {
152f6476
JA
634 ios->cwrites++;
635 ios->cwrite_kb += bytes >> 10;
d0ca268b 636 } else {
152f6476
JA
637 ios->creads++;
638 ios->cread_kb += bytes >> 10;
639 }
640}
641
642static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
643 int rw, int bytes)
644{
645 __account_c(&pci->io_stats, rw, bytes);
646
647 if (per_process_stats) {
648 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
649
650 __account_c(ios, rw, bytes);
d0ca268b
JA
651 }
652}
653
152f6476 654static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
afd2d7ad 655{
656 if (rw) {
152f6476
JA
657 ios->iwrites++;
658 ios->iwrite_kb += bytes >> 10;
afd2d7ad 659 } else {
152f6476
JA
660 ios->ireads++;
661 ios->iread_kb += bytes >> 10;
afd2d7ad 662 }
663}
664
152f6476
JA
665static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
666 int rw)
d0ca268b 667{
152f6476
JA
668 __account_i(&pci->io_stats, rw, t->bytes);
669
670 if (per_process_stats) {
671 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
d5396421 672
152f6476
JA
673 __account_i(ios, rw, t->bytes);
674 }
675}
676
677static void output(struct per_cpu_info *pci, char *s)
678{
679 fprintf(ofp, "%s", s);
d0ca268b
JA
680}
681
3aabcd89
JA
682static char hstring[256];
683static char tstring[256];
d0ca268b 684
d5396421
JA
685static inline char *setup_header(struct per_cpu_info *pci,
686 struct blk_io_trace *t, char act)
d0ca268b
JA
687{
688 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
689 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
690 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
691 char rwbs[4];
692 int i = 0;
693
694 if (w)
695 rwbs[i++] = 'W';
696 else
697 rwbs[i++] = 'R';
698 if (b)
699 rwbs[i++] = 'B';
700 if (s)
701 rwbs[i++] = 'S';
702
703 rwbs[i] = '\0';
704
e7c9f3ff
NS
705 sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %c %3s",
706 MAJOR(t->device), MINOR(t->device), pci->cpu,
cfab07eb
AB
707 (unsigned long)t->sequence, SECONDS(t->time),
708 NANO_SECONDS(t->time), t->pid, act, rwbs);
d0ca268b
JA
709
710 return hstring;
711}
712
d5396421
JA
713static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
714 char act)
d0ca268b 715{
2e3e8ded
JA
716 unsigned long long elapsed = log_track_complete(t);
717
718 if (elapsed != -1ULL) {
b9d40d6f 719 unsigned long usec = elapsed / 1000;
2e3e8ded 720
b9d40d6f 721 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
2e3e8ded
JA
722 setup_header(pci, t, act),
723 (unsigned long long)t->sector, t->bytes >> 9,
724 usec, t->error);
725 } else {
726 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
727 (unsigned long long)t->sector, t->bytes >> 9, t->error);
728 }
729
d5396421 730 output(pci, tstring);
d0ca268b
JA
731}
732
d5396421
JA
733static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
734 char act)
d0ca268b 735{
95c15013 736 unsigned long long elapsed = log_track_queue(t);
2e3e8ded 737
95c15013 738 if (elapsed != -1ULL) {
b9d40d6f 739 unsigned long usec = elapsed / 1000;
95c15013 740
b9d40d6f 741 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
95c15013
JA
742 setup_header(pci, t, act),
743 (unsigned long long)t->sector, t->bytes >> 9,
744 usec, t->comm);
745 } else {
746 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
747 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
748 }
d5396421 749 output(pci, tstring);
d0ca268b
JA
750}
751
d5396421
JA
752static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
753 char act)
d0ca268b 754{
2e3e8ded
JA
755 unsigned long long elapsed = log_track_issue(t);
756
757 if (elapsed != -1ULL) {
758 double usec = (double) elapsed / 1000;
759
555d3a31 760 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
2e3e8ded
JA
761 setup_header(pci, t, act),
762 (unsigned long long)t->sector, t->bytes >> 9,
763 usec, t->comm);
764 } else {
765 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
766 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
767 }
768
d5396421 769 output(pci, tstring);
d0ca268b
JA
770}
771
d5396421
JA
772static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
773 char act)
d0ca268b 774{
2e3e8ded
JA
775 log_track_merge(t);
776
984c63b7 777 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
2955af9d 778 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
d5396421 779 output(pci, tstring);
d0ca268b
JA
780}
781
dfe34da1
JA
782static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
783 char act)
784{
785 sprintf(tstring,"%s [%s]\n", setup_header(pci, t, act), t->comm);
786 output(pci, tstring);
787}
788
d5396421
JA
789static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
790 char act)
d0ca268b 791{
2955af9d
NS
792 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
793 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
d5396421 794 output(pci, tstring);
d0ca268b
JA
795}
796
67e14fdc
JA
797static int log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
798 char act)
799{
800 __u64 *depth;
801 int len;
802
803 len = sprintf(tstring,"%s ", setup_header(pci, t, act));
804 depth = (__u64 *) t + sizeof(*t);
805 sprintf(tstring + len, "%u\n", (unsigned int) be64_to_cpu(*depth));
806 output(pci, tstring);
807
808 return 0;
809}
810
d5396421 811static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char act)
d0ca268b 812{
87b72777
JA
813 unsigned char *buf;
814 int i;
d0ca268b 815
d5396421
JA
816 sprintf(tstring,"%s ", setup_header(pci, t, act));
817 output(pci, tstring);
d0ca268b 818
87b72777 819 buf = (unsigned char *) t + sizeof(*t);
d0ca268b
JA
820 for (i = 0; i < t->pdu_len; i++) {
821 sprintf(tstring,"%02x ", buf[i]);
d5396421 822 output(pci, tstring);
d0ca268b
JA
823 }
824
825 if (act == 'C') {
2955af9d
NS
826 sprintf(tstring,"[%d]\n", t->error);
827 output(pci, tstring);
828 } else {
829 sprintf(tstring,"[%s]\n", t->comm);
d5396421 830 output(pci, tstring);
d0ca268b 831 }
87b72777 832 return 0;
d0ca268b
JA
833}
834
d5396421 835static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
d0ca268b 836{
87b72777
JA
837 int ret = 0;
838
d0ca268b
JA
839 switch (t->action & 0xffff) {
840 case __BLK_TA_QUEUE:
d5396421 841 log_generic(pci, t, 'Q');
d0ca268b
JA
842 break;
843 case __BLK_TA_GETRQ:
d5396421 844 log_generic(pci, t, 'G');
d0ca268b
JA
845 break;
846 case __BLK_TA_SLEEPRQ:
d5396421 847 log_generic(pci, t, 'S');
d0ca268b
JA
848 break;
849 case __BLK_TA_REQUEUE:
d5396421 850 log_generic(pci, t, 'R');
d0ca268b
JA
851 break;
852 case __BLK_TA_ISSUE:
d5396421 853 ret = log_pc(pci, t, 'D');
d0ca268b
JA
854 break;
855 case __BLK_TA_COMPLETE:
d5396421 856 log_pc(pci, t, 'C');
d0ca268b
JA
857 break;
858 default:
859 fprintf(stderr, "Bad pc action %x\n", t->action);
87b72777
JA
860 ret = 1;
861 break;
d0ca268b
JA
862 }
863
87b72777 864 return ret;
d0ca268b
JA
865}
866
d5396421 867static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
d0ca268b
JA
868{
869 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
7997c5b0 870 int act = t->action & 0xffff;
d0ca268b 871
7997c5b0 872 switch (act) {
d0ca268b 873 case __BLK_TA_QUEUE:
152f6476 874 account_q(t, pci, w);
d5396421 875 log_queue(pci, t, 'Q');
d0ca268b
JA
876 break;
877 case __BLK_TA_BACKMERGE:
152f6476 878 account_m(t, pci, w);
d5396421 879 log_merge(pci, t, 'M');
d0ca268b
JA
880 break;
881 case __BLK_TA_FRONTMERGE:
152f6476 882 account_m(t, pci, w);
d5396421 883 log_merge(pci, t, 'F');
d0ca268b
JA
884 break;
885 case __BLK_TA_GETRQ:
95c15013 886 log_track_getrq(t);
d5396421 887 log_generic(pci, t, 'G');
d0ca268b
JA
888 break;
889 case __BLK_TA_SLEEPRQ:
d5396421 890 log_generic(pci, t, 'S');
d0ca268b
JA
891 break;
892 case __BLK_TA_REQUEUE:
152f6476 893 account_c(t, pci, w, -t->bytes);
d5396421 894 log_queue(pci, t, 'R');
d0ca268b
JA
895 break;
896 case __BLK_TA_ISSUE:
152f6476 897 account_i(t, pci, w);
d5396421 898 log_issue(pci, t, 'D');
d0ca268b
JA
899 break;
900 case __BLK_TA_COMPLETE:
152f6476 901 account_c(t, pci, w, t->bytes);
d5396421 902 log_complete(pci, t, 'C');
d0ca268b 903 break;
88b1a526 904 case __BLK_TA_PLUG:
dfe34da1 905 log_action(pci, t, 'P');
88b1a526
JA
906 break;
907 case __BLK_TA_UNPLUG:
67e14fdc 908 log_unplug(pci, t, 'U');
88b1a526 909 break;
d0ca268b
JA
910 default:
911 fprintf(stderr, "Bad fs action %x\n", t->action);
1f79c4a0 912 break;
d0ca268b 913 }
d0ca268b
JA
914}
915
e7c9f3ff
NS
916static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
917 struct per_dev_info *pdi)
d0ca268b 918{
87b72777
JA
919 int ret = 0;
920
d0ca268b 921 if (t->action & BLK_TC_ACT(BLK_TC_PC))
d5396421 922 ret = dump_trace_pc(t, pci);
d0ca268b 923 else
d5396421 924 dump_trace_fs(t, pci);
87b72777 925
e7c9f3ff 926 pdi->events++;
87b72777 927 return ret;
d0ca268b
JA
928}
929
152f6476 930static void dump_io_stats(struct io_stats *ios, char *msg)
5c017e4b 931{
152f6476
JA
932 fprintf(ofp, "%s\n", msg);
933
934 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
935 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
0a6b8fc4 936
152f6476
JA
937 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
938 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
939 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
940 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
941 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
0a6b8fc4 942
152f6476 943 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
5c017e4b
JA
944}
945
50adc0ba
JA
946static void dump_wait_stats(struct per_process_info *ppi)
947{
b9d40d6f
JA
948 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
949 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
950 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
951 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
952 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
953 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
954
955 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
956 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
957 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
958 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
959 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
960 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
50adc0ba
JA
961}
962
152f6476
JA
963static void show_process_stats(void)
964{
965 struct per_process_info *ppi;
966
967 ppi = ppi_list;
968 while (ppi) {
969 dump_io_stats(&ppi->io_stats, ppi->name);
50adc0ba 970 dump_wait_stats(ppi);
152f6476
JA
971 ppi = ppi->list_next;
972 }
973
974 fprintf(ofp, "\n");
975}
976
e7c9f3ff 977static void show_device_and_cpu_stats(void)
d0ca268b 978{
e7c9f3ff
NS
979 struct per_dev_info *pdi;
980 struct per_cpu_info *pci;
981 struct io_stats total, *ios;
982 int i, j, pci_events;
983 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
984 char name[32];
985
986 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
987
988 memset(&total, 0, sizeof(total));
989 pci_events = 0;
990
991 if (i > 0)
992 fprintf(ofp, "\n");
993
994 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
995 if (!pci->nelems)
996 continue;
997
998 ios = &pci->io_stats;
999 total.qreads += ios->qreads;
1000 total.qwrites += ios->qwrites;
1001 total.creads += ios->creads;
1002 total.cwrites += ios->cwrites;
1003 total.mreads += ios->mreads;
1004 total.mwrites += ios->mwrites;
1005 total.ireads += ios->ireads;
1006 total.iwrites += ios->iwrites;
1007 total.qread_kb += ios->qread_kb;
1008 total.qwrite_kb += ios->qwrite_kb;
1009 total.cread_kb += ios->cread_kb;
1010 total.cwrite_kb += ios->cwrite_kb;
1011 total.iread_kb += ios->iread_kb;
1012 total.iwrite_kb += ios->iwrite_kb;
1013
1014 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1015 j, get_dev_name(pdi, name, sizeof(name)));
1016 dump_io_stats(ios, line);
1017 pci_events++;
1018 }
5c017e4b 1019
e7c9f3ff
NS
1020 if (pci_events > 1) {
1021 fprintf(ofp, "\n");
1022 snprintf(line, sizeof(line) - 1, "Total (%s):",
1023 get_dev_name(pdi, name, sizeof(name)));
1024 dump_io_stats(&total, line);
1025 }
d0ca268b 1026
e7c9f3ff
NS
1027 fprintf(ofp, "Events (%s): %'Lu\n",
1028 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1029 }
d0ca268b
JA
1030}
1031
2ff323b0
JA
1032static struct blk_io_trace *find_trace(void *p, unsigned long offset, int nr)
1033{
1034 unsigned long max_offset = min(offset,nr * sizeof(struct blk_io_trace));
1035 unsigned long off;
1036 struct blk_io_trace *bit;
1037 __u32 magic;
1038
1039 for (off = 0; off < max_offset; off++) {
1040 bit = p + off;
1041
1042 magic = be32_to_cpu(bit->magic);
1043 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1044 return bit;
1045 }
1046
1047 return NULL;
1048}
1049
e7c9f3ff
NS
1050static int sort_entries(void *traces, unsigned long offset, int nr,
1051 struct per_dev_info *fpdi, struct per_cpu_info *fpci)
8fc0abbc 1052{
e7c9f3ff 1053 struct per_dev_info *pdi;
412819ce 1054 struct per_cpu_info *pci;
8fc0abbc
JA
1055 struct blk_io_trace *bit;
1056 struct trace *t;
1057 void *start = traces;
8fc0abbc 1058
6fe4709e 1059 while (traces - start <= offset - sizeof(*bit)) {
412819ce
JA
1060 if (!nr)
1061 break;
1062
2ff323b0
JA
1063 bit = find_trace(traces, offset - (traces - start), nr);
1064 if (!bit)
1065 break;
6fe4709e 1066
8fc0abbc 1067 t = malloc(sizeof(*t));
e7c9f3ff
NS
1068 if (!t) {
1069 fprintf(stderr, "Out of memory, seq %d on dev %d,%d\n",
1070 bit->sequence,
1071 MAJOR(bit->device), MINOR(bit->device));
1072 return -1;
1073 }
8fc0abbc
JA
1074 t->bit = bit;
1075 memset(&t->rb_node, 0, sizeof(t->rb_node));
1076
6fe4709e
JA
1077 trace_to_cpu(bit);
1078
e7c9f3ff
NS
1079 if (verify_trace(bit)) {
1080 free(t);
66fa7233 1081 break;
e7c9f3ff 1082 }
66fa7233 1083
e7c9f3ff
NS
1084 pdi = fpdi ? fpdi : get_dev_info(bit->device, 1);
1085 pdi->id = bit->device;
1086 pci = fpci ? fpci : get_cpu_info(pdi, bit->cpu);
1087 pci->cpu = bit->cpu;
412819ce
JA
1088 pci->nelems++;
1089
e7c9f3ff
NS
1090 if (trace_rb_insert(t)) {
1091 free(t);
8fc0abbc 1092 return -1;
e7c9f3ff 1093 }
8fc0abbc
JA
1094
1095 traces += sizeof(*bit) + bit->pdu_len;
412819ce 1096 nr--;
6fe4709e 1097 }
8fc0abbc 1098
e7c9f3ff 1099 return 0;
8fc0abbc
JA
1100}
1101
412819ce
JA
1102static void free_entries_rb(void)
1103{
1104 struct rb_node *n;
1105
7997c5b0 1106 while ((n = rb_first(&rb_sort_root)) != NULL) {
412819ce
JA
1107 struct trace *t = rb_entry(n, struct trace, rb_node);
1108
7997c5b0 1109 rb_erase(&t->rb_node, &rb_sort_root);
412819ce
JA
1110 free(t);
1111 }
1112}
1113
d5396421 1114static void show_entries_rb(void)
8fc0abbc 1115{
e7c9f3ff 1116 struct per_dev_info *pdi;
8fc0abbc 1117 struct blk_io_trace *bit;
3aabcd89 1118 struct rb_node *n;
8fc0abbc
JA
1119 struct trace *t;
1120 int cpu;
1121
7997c5b0 1122 n = rb_first(&rb_sort_root);
3aabcd89
JA
1123 if (!n)
1124 return;
8fc0abbc 1125
3aabcd89 1126 do {
8fc0abbc
JA
1127 t = rb_entry(n, struct trace, rb_node);
1128 bit = t->bit;
1129
e7c9f3ff
NS
1130 pdi = get_dev_info(bit->device, 0);
1131 if (!pdi) {
1132 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1133 MAJOR(bit->device), MINOR(bit->device));
1134 break;
1135 }
d5396421 1136 cpu = bit->cpu;
e7c9f3ff
NS
1137 if (cpu > pdi->ncpus) {
1138 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1139 cpu, MAJOR(bit->device), MINOR(bit->device));
87b72777 1140 break;
8fc0abbc
JA
1141 }
1142
cfab07eb 1143 bit->time -= genesis_time;
46e6968b
NS
1144 if (bit->time < stopwatch_start)
1145 continue;
1146 if (bit->time >= stopwatch_end)
1147 break;
8fc0abbc 1148
e7c9f3ff 1149 check_time(pdi, bit);
8fc0abbc 1150
e7c9f3ff 1151 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
87b72777
JA
1152 break;
1153
8fc0abbc
JA
1154 } while ((n = rb_next(n)) != NULL);
1155}
1156
1f79c4a0
JA
1157static int read_data(int fd, void *buffer, int bytes, int block)
1158{
1159 int ret, bytes_left, fl;
1160 void *p;
1161
1162 fl = fcntl(fd, F_GETFL);
1163
1164 if (!block)
1165 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1166 else
1167 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1168
1169 bytes_left = bytes;
1170 p = buffer;
1171 while (bytes_left > 0) {
1172 ret = read(fd, p, bytes_left);
1173 if (!ret)
1174 return 1;
1175 else if (ret < 0) {
1176 if (errno != EAGAIN)
1177 perror("read");
1178 return -1;
1179 } else {
1180 p += ret;
1181 bytes_left -= ret;
1182 }
1183 }
1184
1185 return 0;
1186}
1187
d5396421 1188static int do_file(void)
d0ca268b 1189{
e7c9f3ff
NS
1190 struct per_dev_info *pdi;
1191 int i, j, nfiles = 0;
d0ca268b 1192
e7c9f3ff
NS
1193 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1194 for (j = 0;; j++, nfiles++) {
1195 struct per_cpu_info *pci;
1196 struct stat st;
1197 void *tb;
87b72777 1198
e7c9f3ff
NS
1199 pci = get_cpu_info(pdi, j);
1200 pci->cpu = j;
d0ca268b 1201
e7c9f3ff
NS
1202 snprintf(pci->fname, sizeof(pci->fname)-1,
1203 "%s_out.%d", pdi->name, j);
1204 if (stat(pci->fname, &st) < 0)
1205 break;
1206 if (!st.st_size)
1207 continue;
1208
1209 printf("Processing %s\n", pci->fname);
1210
1211 tb = malloc(st.st_size);
1212 if (!tb) {
1213 fprintf(stderr, "Out of memory, skip file %s\n",
1214 pci->fname);
1215 continue;
1216 }
1217
1218 pci->fd = open(pci->fname, O_RDONLY);
1219 if (pci->fd < 0) {
1220 perror(pci->fname);
1221 free(tb);
1222 continue;
1223 }
1224
1225 if (read_data(pci->fd, tb, st.st_size, 1)) {
1226 close(pci->fd);
1227 free(tb);
1228 continue;
1229 }
1230
1231 if (sort_entries(tb, st.st_size, ~0U, pdi, pci) == -1) {
1232 close(pci->fd);
1233 free(tb);
1234 continue;
1235 }
1236
1237 printf("Completed %s (CPU%d %d, entries)\n",
1238 pci->fname, j, pci->nelems);
1239 close(pci->fd);
d0ca268b 1240 }
d5396421
JA
1241 }
1242
1243 if (!nfiles) {
1244 fprintf(stderr, "No files found\n");
1245 return 1;
1246 }
1247
1248 show_entries_rb();
d5396421
JA
1249 return 0;
1250}
1251
2ff323b0 1252static void resize_buffer(void **buffer, long *size, long offset)
d5396421 1253{
2ff323b0 1254 long old_size = *size;
d5396421 1255
51128a28
JA
1256 if (*size == 0)
1257 *size = 64 * sizeof(struct blk_io_trace);
1258
2ff323b0
JA
1259 *size *= 2;
1260 *buffer = realloc(*buffer, *size);
51128a28
JA
1261
1262 if (old_size)
1263 memset(*buffer + offset, 0, *size - old_size);
412819ce 1264}
d5396421 1265
0a94916b 1266static int read_sort_events(int fd, void **buffer, long *max_offset)
412819ce 1267{
0a94916b 1268 long offset;
412819ce 1269 int events;
d5396421 1270
51128a28 1271 events = offset = 0;
412819ce
JA
1272 do {
1273 struct blk_io_trace *t;
1274 int pdu_len;
51128a28 1275 __u32 magic;
d5396421 1276
0a94916b
JA
1277 if (*max_offset - offset < sizeof(*t))
1278 resize_buffer(buffer, max_offset, offset);
d5396421 1279
c80c18a7
JA
1280 if (read_data(fd, *buffer + offset, sizeof(*t), !events))
1281 break;
d5396421 1282
412819ce
JA
1283 t = *buffer + offset;
1284 offset += sizeof(*t);
d5396421 1285
51128a28
JA
1286 magic = be32_to_cpu(t->magic);
1287 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1288 fprintf(stderr, "Bad magic %x\n", magic);
1289 break;
1290 }
1291
412819ce 1292 pdu_len = be16_to_cpu(t->pdu_len);
2ff323b0 1293 if (pdu_len) {
51128a28 1294 if (*max_offset - offset <= pdu_len)
0a94916b 1295 resize_buffer(buffer, max_offset, offset);
d5396421 1296
2ff323b0
JA
1297 if (read_data(fd, *buffer + offset, pdu_len, 1))
1298 break;
d5396421 1299
2ff323b0
JA
1300 offset += pdu_len;
1301 }
d5396421 1302
412819ce 1303 events++;
79f19470 1304 } while (!is_done() && events < rb_batch);
d5396421 1305
412819ce
JA
1306 return events;
1307}
d5396421 1308
412819ce
JA
1309static int do_stdin(void)
1310{
1311 int fd;
51128a28 1312 void *ptr = NULL;
0a94916b 1313 long max_offset;
d5396421 1314
1f79c4a0 1315 fd = dup(STDIN_FILENO);
0a94916b 1316 max_offset = 0;
412819ce
JA
1317 do {
1318 int events;
d5396421 1319
0a94916b 1320 events = read_sort_events(fd, &ptr, &max_offset);
412819ce
JA
1321 if (!events)
1322 break;
1323
e7c9f3ff 1324 if (sort_entries(ptr, ~0UL, events, NULL, NULL) == -1)
2ff323b0
JA
1325 break;
1326
412819ce
JA
1327 show_entries_rb();
1328 free_entries_rb();
d5396421
JA
1329 } while (1);
1330
51128a28
JA
1331 if (ptr)
1332 free(ptr);
1333
d5396421 1334 close(fd);
d5396421
JA
1335 return 0;
1336}
d0ca268b 1337
1f79c4a0 1338static void flush_output(void)
412819ce 1339{
152f6476 1340 fflush(ofp);
412819ce
JA
1341}
1342
1f79c4a0 1343static void handle_sigint(int sig)
412819ce
JA
1344{
1345 done = 1;
1346 flush_output();
1347}
1348
46e6968b
NS
1349/*
1350 * Extract start and duration times from a string, allowing
1351 * us to specify a time interval of interest within a trace.
1352 * Format: "duration" (start is zero) or "start:duration".
1353 */
1354static int find_stopwatch_interval(char *string)
1355{
1356 double value;
1357 char *sp;
1358
1359 value = strtod(string, &sp);
1360 if (sp == string) {
1361 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1362 return 1;
1363 }
1364 if (*sp == ':') {
1365 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1366 string = sp + 1;
1367 value = strtod(string, &sp);
1368 if (sp == string || *sp != '\0') {
1369 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1370 string);
1371 return 1;
1372 }
1373 } else if (*sp != '\0') {
1374 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1375 return 1;
1376 }
1377 stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
1378 return 0;
1379}
1380
1f79c4a0
JA
1381static void usage(char *prog)
1382{
46e6968b
NS
1383 fprintf(stderr, "Usage: %s "
1384 "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
1385 prog);
1f79c4a0
JA
1386}
1387
d5396421
JA
1388int main(int argc, char *argv[])
1389{
152f6476 1390 char *ofp_buffer;
a66877e6 1391 int c, ret, mode;
1e1c60f1 1392 int per_device_and_cpu_stats = 1;
d5396421
JA
1393
1394 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1395 switch (c) {
1396 case 'i':
e7c9f3ff
NS
1397 if (!strcmp(optarg, "-") && !pipeline)
1398 pipeline = 1;
1399 else if (resize_devices(optarg) != 0)
1400 return 1;
d5396421
JA
1401 break;
1402 case 'o':
66efebf8 1403 output_name = optarg;
d5396421 1404 break;
79f19470
JA
1405 case 'b':
1406 rb_batch = atoi(optarg);
1407 if (rb_batch <= 0)
1408 rb_batch = RB_BATCH_DEFAULT;
1409 break;
152f6476
JA
1410 case 's':
1411 per_process_stats = 1;
1412 break;
7997c5b0
JA
1413 case 't':
1414 track_ios = 1;
1415 break;
1e1c60f1
NS
1416 case 'q':
1417 per_device_and_cpu_stats = 0;
1418 break;
46e6968b
NS
1419 case 'w':
1420 if (find_stopwatch_interval(optarg) != 0)
1421 return 1;
1422 break;
d5396421 1423 default:
1f79c4a0 1424 usage(argv[0]);
d5396421
JA
1425 return 1;
1426 }
d0ca268b
JA
1427 }
1428
e7c9f3ff
NS
1429 while (optind < argc) {
1430 if (!strcmp(argv[optind], "-") && !pipeline)
1431 pipeline = 1;
1432 else if (resize_devices(argv[optind]) != 0)
1433 return 1;
1434 optind++;
1435 }
1436
1437 if (!pipeline && !ndevices) {
1f79c4a0 1438 usage(argv[0]);
d5396421
JA
1439 return 1;
1440 }
1441
7997c5b0
JA
1442 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1443 memset(&rb_track_root, 0, sizeof(rb_track_root));
412819ce
JA
1444
1445 signal(SIGINT, handle_sigint);
1446 signal(SIGHUP, handle_sigint);
1447 signal(SIGTERM, handle_sigint);
d5396421 1448
d69db225
JA
1449 setlocale(LC_NUMERIC, "en_US");
1450
a66877e6 1451 if (!output_name) {
152f6476 1452 ofp = fdopen(STDOUT_FILENO, "w");
a66877e6
JA
1453 mode = _IOLBF;
1454 } else {
152f6476
JA
1455 char ofname[128];
1456
1457 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1458 ofp = fopen(ofname, "w");
a66877e6 1459 mode = _IOFBF;
152f6476
JA
1460 }
1461
1462 if (!ofp) {
1463 perror("fopen");
1464 return 1;
1465 }
1466
1467 ofp_buffer = malloc(4096);
a66877e6 1468 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
152f6476
JA
1469 perror("setvbuf");
1470 return 1;
1471 }
1472
e7c9f3ff 1473 if (pipeline)
d5396421
JA
1474 ret = do_stdin();
1475 else
1476 ret = do_file();
1477
152f6476
JA
1478 if (per_process_stats)
1479 show_process_stats();
1480
1e1c60f1
NS
1481 if (per_device_and_cpu_stats)
1482 show_device_and_cpu_stats();
152f6476 1483
412819ce 1484 flush_output();
d5396421 1485 return ret;
d0ca268b 1486}