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