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