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