fio: print io_u errors on one line
[fio.git] / t / btrace2fio.c
1 #include <stdio.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <inttypes.h>
5 #include <assert.h>
6
7 #include "../io_ddir.h"
8 #include "../flist.h"
9 #include "../hash.h"
10 #include "../fifo.h"
11 #include "../blktrace_api.h"
12 #include "../os/os.h"
13 #include "../log.h"
14 #include "../lib/linux-dev-lookup.h"
15
16 #define TRACE_FIFO_SIZE 8192
17
18 static unsigned int rt_threshold = 1000000;
19 static unsigned int ios_threshold = 10;
20 static int output_ascii = 1;
21 static char *filename;
22
23 struct bs {
24         unsigned int bs;
25         unsigned int nr;
26         int merges;
27 };
28
29 struct trace_file {
30         char *name;
31         int major, minor;
32 };
33
34 struct btrace_out {
35         unsigned long ios[DDIR_RWDIR_CNT];
36         unsigned long rw_bs[DDIR_RWDIR_CNT];
37         unsigned long merges[DDIR_RWDIR_CNT];
38
39         uint64_t last_end[DDIR_RWDIR_CNT];
40         uint64_t seq[DDIR_RWDIR_CNT];
41
42         struct bs *bs[DDIR_RWDIR_CNT];
43         unsigned int nr_bs[DDIR_RWDIR_CNT];
44
45         int inflight;
46         unsigned int depth;
47         uint64_t first_ttime;
48         uint64_t last_ttime;
49
50         struct trace_file *files;
51         int nr_files;
52         unsigned int last_major, last_minor;
53
54         uint64_t start_delay;
55 };
56
57 struct btrace_pid {
58         struct flist_head hash_list;
59         struct flist_head pid_list;
60         pid_t pid;
61         struct btrace_out o;
62 };
63
64 struct inflight {
65         struct flist_head list;
66         struct btrace_pid *p;
67         uint64_t end_sector;
68 };
69
70 #define PID_HASH_BITS   10
71 #define PID_HASH_SIZE   (1U << PID_HASH_BITS)
72
73 static struct flist_head pid_hash[PID_HASH_SIZE];
74 static FLIST_HEAD(pid_list);
75
76 #define INFLIGHT_HASH_BITS      8
77 #define INFLIGHT_HASH_SIZE      (1U << INFLIGHT_HASH_BITS)
78 static struct flist_head inflight_hash[INFLIGHT_HASH_SIZE];
79
80 static uint64_t first_ttime = -1ULL;
81
82 static struct inflight *inflight_find(uint64_t sector)
83 {
84         struct flist_head *inflight_list;
85         struct flist_head *e;
86
87         inflight_list = &inflight_hash[hash_long(sector, INFLIGHT_HASH_BITS)];
88
89         flist_for_each(e, inflight_list) {
90                 struct inflight *i = flist_entry(e, struct inflight, list);
91
92                 if (i->end_sector == sector)
93                         return i;
94         }
95
96         return NULL;
97 }
98
99 static void inflight_remove(struct inflight *i)
100 {
101         struct btrace_out *o = &i->p->o;
102
103         o->inflight--;
104         assert(o->inflight >= 0);
105         flist_del(&i->list);
106         free(i);
107 }
108
109 static void __inflight_add(struct inflight *i)
110 {
111         struct flist_head *list;
112
113         list = &inflight_hash[hash_long(i->end_sector, INFLIGHT_HASH_BITS)];
114         flist_add_tail(&i->list, list);
115 }
116
117 static void inflight_add(struct btrace_pid *p, uint64_t sector, uint32_t len)
118 {
119         struct btrace_out *o = &p->o;
120         struct inflight *i;
121
122         i = calloc(1, sizeof(*i));
123         i->p = p;
124         o->inflight++;
125         o->depth = max((int) o->depth, o->inflight);
126         i->end_sector = sector + (len >> 9);
127         __inflight_add(i);
128 }
129
130 static void inflight_merge(struct inflight *i, int rw, unsigned int size)
131 {
132         i->p->o.merges[rw]++;
133         if (size) {
134                 i->end_sector += (size >> 9);
135                 flist_del(&i->list);
136                 __inflight_add(i);
137         }
138 }
139
140 /*
141  * fifo refill frontend, to avoid reading data in trace sized bites
142  */
143 static int refill_fifo(struct fifo *fifo, int fd)
144 {
145         char buf[TRACE_FIFO_SIZE];
146         unsigned int total;
147         int ret;
148
149         total = sizeof(buf);
150         if (total > fifo_room(fifo))
151                 total = fifo_room(fifo);
152
153         ret = read(fd, buf, total);
154         if (ret < 0) {
155                 perror("read refill");
156                 return -1;
157         }
158
159         if (ret > 0)
160                 ret = fifo_put(fifo, buf, ret);
161
162         return ret;
163 }
164
165 /*
166  * Retrieve 'len' bytes from the fifo, refilling if necessary.
167  */
168 static int trace_fifo_get(struct fifo *fifo, int fd, void *buf,
169                           unsigned int len)
170 {
171         if (fifo_len(fifo) < len) {
172                 int ret = refill_fifo(fifo, fd);
173
174                 if (ret < 0)
175                         return ret;
176         }
177
178         return fifo_get(fifo, buf, len);
179 }
180
181 /*
182  * Just discard the pdu by seeking past it.
183  */
184 static int discard_pdu(struct fifo *fifo, int fd, struct blk_io_trace *t)
185 {
186         if (t->pdu_len == 0)
187                 return 0;
188
189         return trace_fifo_get(fifo, fd, NULL, t->pdu_len);
190 }
191
192 static void handle_trace_notify(struct blk_io_trace *t)
193 {
194         switch (t->action) {
195         case BLK_TN_PROCESS:
196                 //printf("got process notify: %x, %d\n", t->action, t->pid);
197                 break;
198         case BLK_TN_TIMESTAMP:
199                 //printf("got timestamp notify: %x, %d\n", t->action, t->pid);
200                 break;
201         case BLK_TN_MESSAGE:
202                 break;
203         default:
204                 log_err("unknown trace act %x\n", t->action);
205                 break;
206         }
207 }
208
209 static void __add_bs(struct btrace_out *o, unsigned int len, int rw)
210 {
211         o->bs[rw] = realloc(o->bs[rw], (o->nr_bs[rw] + 1) * sizeof(struct bs));
212         o->bs[rw][o->nr_bs[rw]].bs = len;
213         o->bs[rw][o->nr_bs[rw]].nr = 1;
214         o->nr_bs[rw]++;
215 }
216
217 static void add_bs(struct btrace_out *o, unsigned int len, int rw)
218 {
219         struct bs *bs = o->bs[rw];
220         int i;
221
222         if (!o->nr_bs[rw]) {
223                 __add_bs(o, len, rw);
224                 return;
225         }
226
227         for (i = 0; i < o->nr_bs[rw]; i++) {
228                 if (bs[i].bs == len) {
229                         bs[i].nr++;
230                         return;
231                 }
232         }
233
234         __add_bs(o, len, rw);
235 }
236
237 #define FMINORBITS      20
238 #define FMINORMASK      ((1U << FMINORBITS) - 1)
239 #define FMAJOR(dev)     ((unsigned int) ((dev) >> FMINORBITS))
240 #define FMINOR(dev)     ((unsigned int) ((dev) & FMINORMASK))
241
242 static void btrace_add_file(struct btrace_out *o, uint32_t devno)
243 {
244         unsigned int maj = FMAJOR(devno);
245         unsigned int min = FMINOR(devno);
246         struct trace_file *f;
247         unsigned int i;
248         char dev[256];
249
250         if (filename)
251                 return;
252         if (o->last_major == maj && o->last_minor == min)
253                 return;
254
255         o->last_major = maj;
256         o->last_minor = min;
257
258         /*
259          * check for this file in our list
260          */
261         for (i = 0; i < o->nr_files; i++) {
262                 f = &o->files[i];
263
264                 if (f->major == maj && f->minor == min)
265                         return;
266         }
267
268         strcpy(dev, "/dev");
269         if (!blktrace_lookup_device(NULL, dev, maj, min)) {
270                 log_err("fio: failed to find device %u/%u\n", maj, min);
271                 return;
272         }
273
274         o->files = realloc(o->files, (o->nr_files + 1) * sizeof(*f));
275         f = &o->files[o->nr_files];
276         f->name = strdup(dev);
277         f->major = maj;
278         f->minor = min;
279         o->nr_files++;
280 }
281
282 static void handle_trace_discard(struct blk_io_trace *t, struct btrace_out *o)
283 {
284         btrace_add_file(o, t->device);
285
286         if (o->first_ttime == -1ULL)
287                 o->first_ttime = t->time;
288
289         o->ios[DDIR_TRIM]++;
290         add_bs(o, t->bytes, DDIR_TRIM);
291 }
292
293 static void handle_trace_fs(struct blk_io_trace *t, struct btrace_out *o)
294 {
295         int rw;
296
297         btrace_add_file(o, t->device);
298
299         first_ttime = min(first_ttime, (uint64_t) t->time);
300
301         if (o->first_ttime == -1ULL)
302                 o->first_ttime = t->time;
303
304         rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
305
306         add_bs(o, t->bytes, rw);
307         o->ios[rw]++;
308
309         if (t->sector == o->last_end[rw] || o->last_end[rw] == -1ULL)
310                 o->seq[rw]++;
311
312         o->last_end[rw] = t->sector + (t->bytes >> 9);
313 }
314
315 static void handle_queue_trace(struct blk_io_trace *t, struct btrace_out *o)
316 {
317         if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
318                 handle_trace_notify(t);
319         else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
320                 handle_trace_discard(t, o);
321         else
322                 handle_trace_fs(t, o);
323 }
324
325 static void handle_trace(struct blk_io_trace *t, struct btrace_pid *p)
326 {
327         unsigned int act = t->action & 0xffff;
328
329         if (act == __BLK_TA_QUEUE) {
330                 inflight_add(p, t->sector, t->bytes);
331                 handle_queue_trace(t, &p->o);
332         } else if (act == __BLK_TA_REQUEUE) {
333                 p->o.inflight--;
334         } else if (act == __BLK_TA_BACKMERGE) {
335                 struct inflight *i;
336
337                 i = inflight_find(t->sector + (t->bytes >> 9));
338                 if (i)
339                         inflight_remove(i);
340
341                 i = inflight_find(t->sector);
342                 if (i) {
343                         int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
344
345                         inflight_merge(i, rw, t->bytes);
346                 }
347         } else if (act == __BLK_TA_FRONTMERGE) {
348                 struct inflight *i;
349
350                 i = inflight_find(t->sector + (t->bytes >> 9));
351                 if (i)
352                         inflight_remove(i);
353
354                 i = inflight_find(t->sector);
355                 if (i) {
356                         int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
357
358                         inflight_merge(i, rw, 0);
359                 }
360         } else if (act == __BLK_TA_COMPLETE) {
361                 struct inflight *i;
362
363                 i = inflight_find(t->sector + (t->bytes >> 9));
364                 if (i)
365                         inflight_remove(i);
366         }
367 }
368
369 static void byteswap_trace(struct blk_io_trace *t)
370 {
371         t->magic = fio_swap32(t->magic);
372         t->sequence = fio_swap32(t->sequence);
373         t->time = fio_swap64(t->time);
374         t->sector = fio_swap64(t->sector);
375         t->bytes = fio_swap32(t->bytes);
376         t->action = fio_swap32(t->action);
377         t->pid = fio_swap32(t->pid);
378         t->device = fio_swap32(t->device);
379         t->cpu = fio_swap32(t->cpu);
380         t->error = fio_swap16(t->error);
381         t->pdu_len = fio_swap16(t->pdu_len);
382 }
383
384 static struct btrace_pid *pid_hash_find(pid_t pid, struct flist_head *list)
385 {
386         struct flist_head *e;
387         struct btrace_pid *p;
388
389         flist_for_each(e, list) {
390                 p = flist_entry(e, struct btrace_pid, hash_list);
391                 if (p->pid == pid)
392                         return p;
393         }
394
395         return NULL;
396 }
397
398 static struct btrace_pid *pid_hash_get(pid_t pid)
399 {
400         struct flist_head *hash_list;
401         struct btrace_pid *p;
402
403         hash_list = &pid_hash[hash_long(pid, PID_HASH_BITS)];
404
405         p = pid_hash_find(pid, hash_list);
406         if (!p) {
407                 int i;
408
409                 p = calloc(1, sizeof(*p));
410                 p->o.first_ttime = -1ULL;
411                 p->o.last_ttime = -1ULL;
412
413                 for (i = 0; i < DDIR_RWDIR_CNT; i++)
414                         p->o.last_end[i] = -1ULL;
415
416                 p->pid = pid;
417                 flist_add_tail(&p->hash_list, hash_list);
418                 flist_add_tail(&p->pid_list, &pid_list);
419         }
420
421         return p;
422 }
423
424 /*
425  * Load a blktrace file by reading all the blk_io_trace entries, and storing
426  * them as io_pieces like the fio text version would do.
427  */
428 static int load_blktrace(const char *filename, int need_swap)
429 {
430         struct btrace_pid *p;
431         unsigned long traces;
432         struct blk_io_trace t;
433         struct fifo *fifo;
434         int fd;
435
436         fd = open(filename, O_RDONLY);
437         if (fd < 0) {
438                 perror("open trace file\n");
439                 return 1;
440         }
441
442         fifo = fifo_alloc(TRACE_FIFO_SIZE);
443
444         traces = 0;
445         do {
446                 int ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
447
448                 if (ret < 0)
449                         goto err;
450                 else if (!ret)
451                         break;
452                 else if (ret < (int) sizeof(t)) {
453                         log_err("fio: short fifo get\n");
454                         break;
455                 }
456
457                 if (need_swap)
458                         byteswap_trace(&t);
459
460                 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
461                         log_err("fio: bad magic in blktrace data: %x\n", t.magic);
462                         goto err;
463                 }
464                 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
465                         log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
466                         goto err;
467                 }
468                 ret = discard_pdu(fifo, fd, &t);
469                 if (ret < 0) {
470                         log_err("blktrace lseek\n");
471                         goto err;
472                 } else if (t.pdu_len != ret) {
473                         log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
474                         goto err;
475                 }
476
477                 p = pid_hash_get(t.pid);
478                 handle_trace(&t, p);
479                 p->o.last_ttime = t.time;
480                 traces++;
481         } while (1);
482
483         fifo_free(fifo);
484         close(fd);
485
486         if (output_ascii)
487                 printf("Traces loaded: %lu\n", traces);
488
489         return 0;
490 err:
491         close(fd);
492         fifo_free(fifo);
493         return 1;
494 }
495
496 static int bs_cmp(const void *ba, const void *bb)
497 {
498         const struct bs *bsa = ba;
499         const struct bs *bsb = bb;
500
501         return bsb->nr - bsa->nr;
502 }
503
504 static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
505 {
506         const char *msg[] = { "reads", "writes", "trims" };
507         struct btrace_out *o = &p->o;
508         unsigned long total;
509         int i, j;
510
511         printf("[pid:\t%u]\n", p->pid);
512
513         total = ddir_rw_sum(o->ios);
514         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
515                 float perc;
516
517                 if (!o->ios[i])
518                         continue;
519
520                 ios[i] += o->ios[i] + o->merges[i];
521                 printf("%s\n", msg[i]);
522                 perc = ((float) o->ios[i] * 100.0) / (float) total;
523                 printf("\tios:    %lu (perc=%3.2f%%)\n", o->ios[i], perc);
524                 perc = ((float) o->merges[i] * 100.0) / (float) total;
525                 printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc);
526                 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
527                 printf("\tseq:    %lu (perc=%3.2f%%)\n", o->seq[i], perc);
528
529                 for (j = 0; j < o->nr_bs[i]; j++) {
530                         struct bs *bs = &o->bs[i][j];
531
532                         perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
533                         printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc);
534                 }
535         }
536
537         printf("depth:\t%u\n", o->depth);
538         printf("usec:\t%llu (delay=%llu)\n", (o->last_ttime - o->first_ttime) / 1000ULL, (unsigned long long) o->start_delay);
539
540         printf("files:\t");
541         for (i = 0; i < o->nr_files; i++)
542                 printf("%s,", o->files[i].name);
543         printf("\n");
544
545         printf("\n");
546 }
547
548 static int __output_p_fio(struct btrace_pid *p, unsigned long *ios)
549 {
550         struct btrace_out *o = &p->o;
551         unsigned long total;
552         unsigned long long time;
553         float perc;
554         int i, j;
555
556         if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
557                 log_err("fio: trace has both read/write and trim\n");
558                 return 1;
559         }
560
561         printf("[pid%u]\n", p->pid);
562         printf("direct=1\n");
563         if (o->depth == 1)
564                 printf("ioengine=sync\n");
565         else
566                 printf("ioengine=libaio\niodepth=%u\n", o->depth);
567
568         if (o->ios[0] && !o->ios[1])
569                 printf("rw=randread\n");
570         else if (!o->ios[0] && o->ios[1])
571                 printf("rw=randwrite\n");
572         else if (o->ios[2])
573                 printf("rw=randtrim\n");
574         else {
575                 printf("rw=randrw\n");
576                 total = ddir_rw_sum(o->ios);
577                 perc = ((float) o->ios[0] * 100.0) / (float) total;
578                 printf("rwmixread=%u\n", (int) (perc + 0.99));
579         }
580
581         printf("percentage_random=");
582         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
583                 if (o->seq[i] && o->ios[i]) {
584                         perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
585                         if (perc >= 99.0)
586                                 perc = 100.0;
587                 } else
588                         perc = 100.0;
589
590                 if (i)
591                         printf(",");
592                 perc = 100.0 - perc;
593                 printf("%u", (int) perc);
594         }
595         printf("\n");
596
597         printf("filename=");
598         for (i = 0; i < o->nr_files; i++) {
599                 if (i)
600                         printf(":");
601                 printf("%s", o->files[i].name);
602         }
603         printf("\n");
604
605         printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
606
607         time = o->last_ttime - o->first_ttime;
608         time = (time + 1000000000ULL - 1) / 1000000000ULL;
609         printf("runtime=%llus\n", time);
610
611         printf("bssplit=");
612         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
613
614                 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
615                         printf(",");
616
617                 for (j = 0; j < o->nr_bs[i]; j++) {
618                         struct bs *bs = &o->bs[i][j];
619
620                         perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
621                         if (perc < 1.00)
622                                 continue;
623                         if (j)
624                                 printf(":");
625                         if (j + 1 == o->nr_bs[i])
626                                 printf("%u/", bs->bs);
627                         else
628                                 printf("%u/%u", bs->bs, (int) perc);
629                 }
630         }
631         printf("\n\n");
632
633         return 0;
634 }
635
636 static int __output_p(struct btrace_pid *p, unsigned long *ios)
637 {
638         struct btrace_out *o = &p->o;
639         int i, ret = 0;
640
641         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
642                 if (o->nr_bs[i] <= 1)
643                         continue;
644                 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
645         }
646
647         if (filename) {
648                 o->files = malloc(sizeof(struct trace_file));
649                 o->nr_files++;
650                 o->files[0].name = filename;
651         }
652
653         if (output_ascii)
654                 __output_p_ascii(p, ios);
655         else
656                 ret = __output_p_fio(p, ios);
657
658         return ret;
659 }
660
661 static int prune_entry(struct btrace_out *o)
662 {
663         uint64_t time;
664
665         if (ddir_rw_sum(o->ios) < ios_threshold)
666                 return 1;
667
668         time = (o->last_ttime - o->first_ttime) / 1000ULL;
669         if (time < rt_threshold)
670                 return 1;
671
672         return 0;
673 }
674
675 static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
676 {
677         struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list);
678         struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list);
679
680         return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
681 }
682
683 static void free_p(struct btrace_pid *p)
684 {
685         struct btrace_out *o = &p->o;
686         int i;
687
688         for (i = 0; i < o->nr_files; i++) {
689                 if (o->files[i].name && o->files[i].name != filename)
690                         free(o->files[i].name);
691         }
692
693         for (i = 0; i < DDIR_RWDIR_CNT; i++)
694                 free(o->bs[i]);
695
696         free(o->files);
697         flist_del(&p->pid_list);
698         flist_del(&p->hash_list);
699         free(p);
700 }
701
702 static int output_p(void)
703 {
704         unsigned long ios[DDIR_RWDIR_CNT];
705         struct flist_head *e, *tmp;
706         int ret = 0;
707
708         flist_for_each_safe(e, tmp, &pid_list) {
709                 struct btrace_pid *p;
710
711                 p = flist_entry(e, struct btrace_pid, pid_list);
712                 if (prune_entry(&p->o)) {
713                         free_p(p);
714                         continue;
715                 }
716                 p->o.start_delay = (p->o.first_ttime / 1000ULL) - first_ttime;
717         }
718
719         memset(ios, 0, sizeof(ios));
720
721         flist_sort(NULL, &pid_list, entry_cmp);
722
723         flist_for_each(e, &pid_list) {
724                 struct btrace_pid *p;
725
726                 p = flist_entry(e, struct btrace_pid, pid_list);
727                 ret |= __output_p(p, ios);
728         }
729
730         if (output_ascii)
731                 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
732
733         return ret;
734 }
735
736 static int usage(char *argv[])
737 {
738         log_err("%s: <blktrace bin file>\n", argv[0]);
739         log_err("\t-t\tUsec threshold to ignore task\n");
740         log_err("\t-n\tNumber IOS threshold to ignore task\n");
741         log_err("\t-f\tFio job file output\n");
742         log_err("\t-d\tUse this file/device for replay\n");
743         return 1;
744 }
745
746 static int trace_needs_swap(const char *trace_file, int *swap)
747 {
748         struct blk_io_trace t;
749         int fd, ret;
750
751         *swap = -1;
752         
753         fd = open(trace_file, O_RDONLY);
754         if (fd < 0) {
755                 perror("open");
756                 return 1;
757         }
758
759         ret = read(fd, &t, sizeof(t));
760         if (ret < 0) {
761                 perror("read");
762                 return 1;
763         } else if (ret != sizeof(t)) {
764                 log_err("fio: short read on trace file\n");
765                 return 1;
766         }
767
768         close(fd);
769
770         if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
771                 *swap = 0;
772         else {
773                 /*
774                  * Maybe it needs to be endian swapped...
775                  */
776                 t.magic = fio_swap32(t.magic);
777                 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
778                         *swap = 1;
779         }
780
781         if (*swap == -1) {
782                 log_err("fio: blktrace appears corrupt\n");
783                 return 1;
784         }
785
786         return 0;
787 }
788
789 int main(int argc, char *argv[])
790 {
791         int need_swap, i, c;
792
793         if (argc < 2)
794                 return usage(argv);
795
796         while ((c = getopt(argc, argv, "t:n:fd:")) != -1) {
797                 switch (c) {
798                 case 't':
799                         rt_threshold = atoi(optarg);
800                         break;
801                 case 'n':
802                         ios_threshold = atoi(optarg);
803                         break;
804                 case 'f':
805                         output_ascii = 0;
806                         break;
807                 case 'd':
808                         filename = strdup(optarg);
809                         break;
810                 case '?':
811                 default:
812                         return usage(argv);
813                 }
814         }
815
816         if (argc == optind)
817                 return usage(argv);
818
819         if (trace_needs_swap(argv[optind], &need_swap))
820                 return 1;
821
822         for (i = 0; i < PID_HASH_SIZE; i++)
823                 INIT_FLIST_HEAD(&pid_hash[i]);
824         for (i = 0; i < INFLIGHT_HASH_SIZE; i++)
825                 INIT_FLIST_HEAD(&inflight_hash[i]);
826
827         load_blktrace(argv[optind], need_swap);
828         first_ttime /= 1000ULL;
829
830         return output_p();
831 }