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