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