7 #include "../io_ddir.h"
11 #include "../blktrace_api.h"
14 #include "../lib/linux-dev-lookup.h"
16 #define TRACE_FIFO_SIZE 8192
18 static unsigned int rt_threshold = 1000000;
19 static unsigned int ios_threshold = 10;
20 static int output_ascii = 1;
21 static char *filename;
35 unsigned long ios[DDIR_RWDIR_CNT];
36 unsigned long rw_bs[DDIR_RWDIR_CNT];
37 unsigned long merges[DDIR_RWDIR_CNT];
39 uint64_t last_end[DDIR_RWDIR_CNT];
40 uint64_t seq[DDIR_RWDIR_CNT];
42 struct bs *bs[DDIR_RWDIR_CNT];
43 unsigned int nr_bs[DDIR_RWDIR_CNT];
50 struct trace_file *files;
52 unsigned int last_major, last_minor;
58 struct flist_head hash_list;
59 struct flist_head pid_list;
65 struct flist_head list;
70 #define PID_HASH_BITS 10
71 #define PID_HASH_SIZE (1U << PID_HASH_BITS)
73 static struct flist_head pid_hash[PID_HASH_SIZE];
74 static FLIST_HEAD(pid_list);
76 static FLIST_HEAD(inflight_list);
78 static uint64_t first_ttime = -1ULL;
80 static struct inflight *inflight_find(uint64_t sector)
84 flist_for_each(e, &inflight_list) {
85 struct inflight *i = flist_entry(e, struct inflight, list);
87 if (i->end_sector == sector)
94 static void inflight_remove(struct inflight *i)
96 struct btrace_out *o = &i->p->o;
99 assert(o->inflight >= 0);
104 static void inflight_merge(struct inflight *i, int rw, unsigned int size)
106 i->p->o.merges[rw]++;
108 i->end_sector += (size >> 9);
111 static void inflight_add(struct btrace_pid *p, uint64_t sector, uint32_t len)
113 struct btrace_out *o = &p->o;
116 i = calloc(1, sizeof(*i));
119 o->depth = max((int) o->depth, o->inflight);
120 i->end_sector = sector + (len >> 9);
121 flist_add_tail(&i->list, &inflight_list);
125 * fifo refill frontend, to avoid reading data in trace sized bites
127 static int refill_fifo(struct fifo *fifo, int fd)
129 char buf[TRACE_FIFO_SIZE];
134 if (total > fifo_room(fifo))
135 total = fifo_room(fifo);
137 ret = read(fd, buf, total);
139 perror("read refill");
144 ret = fifo_put(fifo, buf, ret);
150 * Retrieve 'len' bytes from the fifo, refilling if necessary.
152 static int trace_fifo_get(struct fifo *fifo, int fd, void *buf,
155 if (fifo_len(fifo) < len) {
156 int ret = refill_fifo(fifo, fd);
162 return fifo_get(fifo, buf, len);
166 * Just discard the pdu by seeking past it.
168 static int discard_pdu(struct fifo *fifo, int fd, struct blk_io_trace *t)
173 return trace_fifo_get(fifo, fd, NULL, t->pdu_len);
176 static void handle_trace_notify(struct blk_io_trace *t)
180 //printf("got process notify: %x, %d\n", t->action, t->pid);
182 case BLK_TN_TIMESTAMP:
183 //printf("got timestamp notify: %x, %d\n", t->action, t->pid);
188 fprintf(stderr, "unknown trace act %x\n", t->action);
193 static void __add_bs(struct btrace_out *o, unsigned int len, int rw)
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;
201 static void add_bs(struct btrace_out *o, unsigned int len, int rw)
203 struct bs *bs = o->bs[rw];
207 __add_bs(o, len, rw);
211 for (i = 0; i < o->nr_bs[rw]; i++) {
212 if (bs[i].bs == len) {
218 __add_bs(o, len, rw);
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))
226 static void btrace_add_file(struct btrace_out *o, uint32_t devno)
228 unsigned int maj = FMAJOR(devno);
229 unsigned int min = FMINOR(devno);
230 struct trace_file *f;
236 if (o->last_major == maj && o->last_minor == min)
243 * check for this file in our list
245 for (i = 0; i < o->nr_files; i++) {
248 if (f->major == maj && f->minor == min)
253 if (!blktrace_lookup_device(NULL, dev, maj, min)) {
254 log_err("fio: failed to find device %u/%u\n", maj, min);
258 o->files = realloc(o->files, (o->nr_files + 1) * sizeof(*f));
259 f = &o->files[o->nr_files];
260 f->name = strdup(dev);
266 static void handle_trace_discard(struct blk_io_trace *t, struct btrace_out *o)
268 btrace_add_file(o, t->device);
270 if (o->first_ttime == -1ULL)
271 o->first_ttime = t->time;
274 add_bs(o, t->bytes, DDIR_TRIM);
277 static void handle_trace_fs(struct blk_io_trace *t, struct btrace_out *o)
281 btrace_add_file(o, t->device);
283 first_ttime = min(first_ttime, (uint64_t) t->time);
285 if (o->first_ttime == -1ULL)
286 o->first_ttime = t->time;
288 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
290 add_bs(o, t->bytes, rw);
293 if (t->sector == o->last_end[rw] || o->last_end[rw] == -1ULL)
296 o->last_end[rw] = t->sector + (t->bytes >> 9);
299 static void handle_queue_trace(struct blk_io_trace *t, struct btrace_out *o)
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);
306 handle_trace_fs(t, o);
309 static void handle_trace(struct blk_io_trace *t, struct btrace_pid *p)
311 unsigned int act = t->action & 0xffff;
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) {
318 } else if (act == __BLK_TA_BACKMERGE) {
321 i = inflight_find(t->sector + (t->bytes >> 9));
325 i = inflight_find(t->sector);
327 int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
329 inflight_merge(i, rw, t->bytes);
331 } else if (act == __BLK_TA_FRONTMERGE) {
334 i = inflight_find(t->sector + (t->bytes >> 9));
338 i = inflight_find(t->sector);
340 int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
342 inflight_merge(i, rw, 0);
344 } else if (act == __BLK_TA_COMPLETE) {
347 i = inflight_find(t->sector + (t->bytes >> 9));
353 static void byteswap_trace(struct blk_io_trace *t)
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);
368 static struct btrace_pid *pid_hash_find(pid_t pid, struct flist_head *list)
370 struct flist_head *e;
371 struct btrace_pid *p;
373 flist_for_each(e, list) {
374 p = flist_entry(e, struct btrace_pid, hash_list);
382 static struct btrace_pid *pid_hash_get(pid_t pid)
384 struct flist_head *hash_list;
385 struct btrace_pid *p;
387 hash_list = &pid_hash[hash_long(pid, PID_HASH_BITS)];
389 p = pid_hash_find(pid, hash_list);
393 p = calloc(1, sizeof(*p));
394 p->o.first_ttime = -1ULL;
395 p->o.last_ttime = -1ULL;
397 for (i = 0; i < DDIR_RWDIR_CNT; i++)
398 p->o.last_end[i] = -1ULL;
401 flist_add_tail(&p->hash_list, hash_list);
402 flist_add_tail(&p->pid_list, &pid_list);
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.
412 static int load_blktrace(const char *filename, int need_swap)
414 struct btrace_pid *p;
415 unsigned long traces;
416 struct blk_io_trace t;
420 fd = open(filename, O_RDONLY);
422 perror("open trace file\n");
426 fifo = fifo_alloc(TRACE_FIFO_SIZE);
430 int ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
436 else if (ret < (int) sizeof(t)) {
437 fprintf(stderr, "fio: short fifo get\n");
444 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
445 fprintf(stderr, "fio: bad magic in blktrace data: %x\n",
449 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
450 fprintf(stderr, "fio: bad blktrace version %d\n",
454 ret = discard_pdu(fifo, fd, &t);
456 fprintf(stderr, "blktrace lseek\n");
458 } else if (t.pdu_len != ret) {
459 fprintf(stderr, "fio: discarded %d of %d\n", ret, t.pdu_len);
463 p = pid_hash_get(t.pid);
465 p->o.last_ttime = t.time;
473 printf("Traces loaded: %lu\n", traces);
482 static int bs_cmp(const void *ba, const void *bb)
484 const struct bs *bsa = ba;
485 const struct bs *bsb = bb;
487 return bsb->nr - bsa->nr;
490 static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
492 const char *msg[] = { "reads", "writes", "trims" };
493 struct btrace_out *o = &p->o;
497 printf("[pid:\t%u]\n", p->pid);
499 total = ddir_rw_sum(o->ios);
500 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
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);
515 for (j = 0; j < o->nr_bs[i]; j++) {
516 struct bs *bs = &o->bs[i][j];
518 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
519 printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc);
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);
527 for (i = 0; i < o->nr_files; i++)
528 printf("%s,", o->files[i].name);
534 static int __output_p_fio(struct btrace_pid *p, unsigned long *ios)
536 struct btrace_out *o = &p->o;
538 unsigned long long time;
542 if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
543 log_err("fio: trace has both read/write and trim\n");
547 printf("[pid%u]\n", p->pid);
548 printf("direct=1\n");
550 printf("ioengine=sync\n");
552 printf("ioengine=libaio\niodepth=%u\n", o->depth);
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");
559 printf("rw=randtrim\n");
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));
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];
579 printf("%u", (int) perc);
584 for (i = 0; i < o->nr_files; i++) {
587 printf("%s", o->files[i].name);
591 printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
593 time = o->last_ttime - o->first_ttime;
594 time = (time + 1000000000ULL - 1) / 1000000000ULL;
595 printf("runtime=%llus\n", time);
598 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
600 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
603 for (j = 0; j < o->nr_bs[i]; j++) {
604 struct bs *bs = &o->bs[i][j];
606 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
611 if (j + 1 == o->nr_bs[i])
612 printf("%u/", bs->bs);
614 printf("%u/%u", bs->bs, (int) perc);
622 static int __output_p(struct btrace_pid *p, unsigned long *ios)
624 struct btrace_out *o = &p->o;
627 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
628 if (o->nr_bs[i] <= 1)
630 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
634 o->files = malloc(sizeof(struct trace_file));
636 o->files[0].name = filename;
640 __output_p_ascii(p, ios);
642 ret = __output_p_fio(p, ios);
647 static int prune_entry(struct btrace_out *o)
651 if (ddir_rw_sum(o->ios) < ios_threshold)
654 time = (o->last_ttime - o->first_ttime) / 1000ULL;
655 if (time < rt_threshold)
661 static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
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);
666 return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
669 static int output_p(void)
671 unsigned long ios[DDIR_RWDIR_CNT];
672 struct flist_head *e, *tmp;
675 flist_for_each_safe(e, tmp, &pid_list) {
676 struct btrace_pid *p;
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);
685 p->o.start_delay = (p->o.first_ttime / 1000ULL) - first_ttime;
688 memset(ios, 0, sizeof(ios));
690 flist_sort(NULL, &pid_list, entry_cmp);
692 flist_for_each(e, &pid_list) {
693 struct btrace_pid *p;
695 p = flist_entry(e, struct btrace_pid, pid_list);
696 ret |= __output_p(p, ios);
700 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
705 static int usage(char *argv[])
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");
715 int main(int argc, char *argv[])
717 int fd, ret, need_swap = -1;
718 struct blk_io_trace t;
724 while ((c = getopt(argc, argv, "t:n:fd:")) != -1) {
727 rt_threshold = atoi(optarg);
730 ios_threshold = atoi(optarg);
736 filename = strdup(optarg);
747 fd = open(argv[optind], O_RDONLY);
753 ret = read(fd, &t, sizeof(t));
757 } else if (ret != sizeof(t)) {
758 fprintf(stderr, "fio: short read on trace file\n");
764 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
768 * Maybe it needs to be endian swapped...
770 t.magic = fio_swap32(t.magic);
771 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
775 if (need_swap == -1) {
776 fprintf(stderr, "fio: blktrace appears corrupt\n");
780 for (i = 0; i < PID_HASH_SIZE; i++)
781 INIT_FLIST_HEAD(&pid_hash[i]);
783 load_blktrace(argv[optind], need_swap);
784 first_ttime /= 1000ULL;