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