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 #define INFLIGHT_HASH_BITS 8
77 #define INFLIGHT_HASH_SIZE (1U << INFLIGHT_HASH_BITS)
78 static struct flist_head inflight_hash[INFLIGHT_HASH_SIZE];
80 static uint64_t first_ttime = -1ULL;
82 static struct inflight *inflight_find(uint64_t sector)
84 struct flist_head *inflight_list;
87 inflight_list = &inflight_hash[hash_long(sector, INFLIGHT_HASH_BITS)];
89 flist_for_each(e, inflight_list) {
90 struct inflight *i = flist_entry(e, struct inflight, list);
92 if (i->end_sector == sector)
99 static void inflight_remove(struct inflight *i)
101 struct btrace_out *o = &i->p->o;
104 assert(o->inflight >= 0);
109 static void __inflight_add(struct inflight *i)
111 struct flist_head *list;
113 list = &inflight_hash[hash_long(i->end_sector, INFLIGHT_HASH_BITS)];
114 flist_add_tail(&i->list, list);
117 static void inflight_add(struct btrace_pid *p, uint64_t sector, uint32_t len)
119 struct btrace_out *o = &p->o;
122 i = calloc(1, sizeof(*i));
125 o->depth = max((int) o->depth, o->inflight);
126 i->end_sector = sector + (len >> 9);
130 static void inflight_merge(struct inflight *i, int rw, unsigned int size)
132 i->p->o.merges[rw]++;
134 i->end_sector += (size >> 9);
141 * fifo refill frontend, to avoid reading data in trace sized bites
143 static int refill_fifo(struct fifo *fifo, int fd)
145 char buf[TRACE_FIFO_SIZE];
150 if (total > fifo_room(fifo))
151 total = fifo_room(fifo);
153 ret = read(fd, buf, total);
155 perror("read refill");
160 ret = fifo_put(fifo, buf, ret);
166 * Retrieve 'len' bytes from the fifo, refilling if necessary.
168 static int trace_fifo_get(struct fifo *fifo, int fd, void *buf,
171 if (fifo_len(fifo) < len) {
172 int ret = refill_fifo(fifo, fd);
178 return fifo_get(fifo, buf, len);
182 * Just discard the pdu by seeking past it.
184 static int discard_pdu(struct fifo *fifo, int fd, struct blk_io_trace *t)
189 return trace_fifo_get(fifo, fd, NULL, t->pdu_len);
192 static void handle_trace_notify(struct blk_io_trace *t)
196 //printf("got process notify: %x, %d\n", t->action, t->pid);
198 case BLK_TN_TIMESTAMP:
199 //printf("got timestamp notify: %x, %d\n", t->action, t->pid);
204 log_err("unknown trace act %x\n", t->action);
209 static void __add_bs(struct btrace_out *o, unsigned int len, int rw)
211 o->bs[rw] = realloc(o->bs[rw], (o->nr_bs[rw] + 1) * sizeof(struct bs));
212 o->bs[rw][o->nr_bs[rw]].bs = len;
213 o->bs[rw][o->nr_bs[rw]].nr = 1;
217 static void add_bs(struct btrace_out *o, unsigned int len, int rw)
219 struct bs *bs = o->bs[rw];
223 __add_bs(o, len, rw);
227 for (i = 0; i < o->nr_bs[rw]; i++) {
228 if (bs[i].bs == len) {
234 __add_bs(o, len, rw);
237 #define FMINORBITS 20
238 #define FMINORMASK ((1U << FMINORBITS) - 1)
239 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
240 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
242 static void btrace_add_file(struct btrace_out *o, uint32_t devno)
244 unsigned int maj = FMAJOR(devno);
245 unsigned int min = FMINOR(devno);
246 struct trace_file *f;
252 if (o->last_major == maj && o->last_minor == min)
259 * check for this file in our list
261 for (i = 0; i < o->nr_files; i++) {
264 if (f->major == maj && f->minor == min)
269 if (!blktrace_lookup_device(NULL, dev, maj, min)) {
270 log_err("fio: failed to find device %u/%u\n", maj, min);
274 o->files = realloc(o->files, (o->nr_files + 1) * sizeof(*f));
275 f = &o->files[o->nr_files];
276 f->name = strdup(dev);
282 static void handle_trace_discard(struct blk_io_trace *t, struct btrace_out *o)
284 btrace_add_file(o, t->device);
286 if (o->first_ttime == -1ULL)
287 o->first_ttime = t->time;
290 add_bs(o, t->bytes, DDIR_TRIM);
293 static void handle_trace_fs(struct blk_io_trace *t, struct btrace_out *o)
297 btrace_add_file(o, t->device);
299 first_ttime = min(first_ttime, (uint64_t) t->time);
301 if (o->first_ttime == -1ULL)
302 o->first_ttime = t->time;
304 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
306 add_bs(o, t->bytes, rw);
309 if (t->sector == o->last_end[rw] || o->last_end[rw] == -1ULL)
312 o->last_end[rw] = t->sector + (t->bytes >> 9);
315 static void handle_queue_trace(struct blk_io_trace *t, struct btrace_out *o)
317 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
318 handle_trace_notify(t);
319 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
320 handle_trace_discard(t, o);
322 handle_trace_fs(t, o);
325 static void handle_trace(struct blk_io_trace *t, struct btrace_pid *p)
327 unsigned int act = t->action & 0xffff;
329 if (act == __BLK_TA_QUEUE) {
330 inflight_add(p, t->sector, t->bytes);
331 handle_queue_trace(t, &p->o);
332 } else if (act == __BLK_TA_REQUEUE) {
334 } else if (act == __BLK_TA_BACKMERGE) {
337 i = inflight_find(t->sector + (t->bytes >> 9));
341 i = inflight_find(t->sector);
343 int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
345 inflight_merge(i, rw, t->bytes);
347 } else if (act == __BLK_TA_FRONTMERGE) {
350 i = inflight_find(t->sector + (t->bytes >> 9));
354 i = inflight_find(t->sector);
356 int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
358 inflight_merge(i, rw, 0);
360 } else if (act == __BLK_TA_COMPLETE) {
363 i = inflight_find(t->sector + (t->bytes >> 9));
369 static void byteswap_trace(struct blk_io_trace *t)
371 t->magic = fio_swap32(t->magic);
372 t->sequence = fio_swap32(t->sequence);
373 t->time = fio_swap64(t->time);
374 t->sector = fio_swap64(t->sector);
375 t->bytes = fio_swap32(t->bytes);
376 t->action = fio_swap32(t->action);
377 t->pid = fio_swap32(t->pid);
378 t->device = fio_swap32(t->device);
379 t->cpu = fio_swap32(t->cpu);
380 t->error = fio_swap16(t->error);
381 t->pdu_len = fio_swap16(t->pdu_len);
384 static struct btrace_pid *pid_hash_find(pid_t pid, struct flist_head *list)
386 struct flist_head *e;
387 struct btrace_pid *p;
389 flist_for_each(e, list) {
390 p = flist_entry(e, struct btrace_pid, hash_list);
398 static struct btrace_pid *pid_hash_get(pid_t pid)
400 struct flist_head *hash_list;
401 struct btrace_pid *p;
403 hash_list = &pid_hash[hash_long(pid, PID_HASH_BITS)];
405 p = pid_hash_find(pid, hash_list);
409 p = calloc(1, sizeof(*p));
410 p->o.first_ttime = -1ULL;
411 p->o.last_ttime = -1ULL;
413 for (i = 0; i < DDIR_RWDIR_CNT; i++)
414 p->o.last_end[i] = -1ULL;
417 flist_add_tail(&p->hash_list, hash_list);
418 flist_add_tail(&p->pid_list, &pid_list);
425 * Load a blktrace file by reading all the blk_io_trace entries, and storing
426 * them as io_pieces like the fio text version would do.
428 static int load_blktrace(const char *filename, int need_swap)
430 struct btrace_pid *p;
431 unsigned long traces;
432 struct blk_io_trace t;
436 fd = open(filename, O_RDONLY);
438 perror("open trace file\n");
442 fifo = fifo_alloc(TRACE_FIFO_SIZE);
446 int ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
452 else if (ret < (int) sizeof(t)) {
453 log_err("fio: short fifo get\n");
460 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
461 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
464 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
465 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
468 ret = discard_pdu(fifo, fd, &t);
470 log_err("blktrace lseek\n");
472 } else if (t.pdu_len != ret) {
473 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
477 p = pid_hash_get(t.pid);
479 p->o.last_ttime = t.time;
487 printf("Traces loaded: %lu\n", traces);
496 static int bs_cmp(const void *ba, const void *bb)
498 const struct bs *bsa = ba;
499 const struct bs *bsb = bb;
501 return bsb->nr - bsa->nr;
504 static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
506 const char *msg[] = { "reads", "writes", "trims" };
507 struct btrace_out *o = &p->o;
511 printf("[pid:\t%u]\n", p->pid);
513 total = ddir_rw_sum(o->ios);
514 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
520 ios[i] += o->ios[i] + o->merges[i];
521 printf("%s\n", msg[i]);
522 perc = ((float) o->ios[i] * 100.0) / (float) total;
523 printf("\tios: %lu (perc=%3.2f%%)\n", o->ios[i], perc);
524 perc = ((float) o->merges[i] * 100.0) / (float) total;
525 printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc);
526 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
527 printf("\tseq: %lu (perc=%3.2f%%)\n", o->seq[i], perc);
529 for (j = 0; j < o->nr_bs[i]; j++) {
530 struct bs *bs = &o->bs[i][j];
532 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
533 printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc);
537 printf("depth:\t%u\n", o->depth);
538 printf("usec:\t%llu (delay=%llu)\n", (o->last_ttime - o->first_ttime) / 1000ULL, (unsigned long long) o->start_delay);
541 for (i = 0; i < o->nr_files; i++)
542 printf("%s,", o->files[i].name);
548 static int __output_p_fio(struct btrace_pid *p, unsigned long *ios)
550 struct btrace_out *o = &p->o;
552 unsigned long long time;
556 if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
557 log_err("fio: trace has both read/write and trim\n");
561 printf("[pid%u]\n", p->pid);
562 printf("direct=1\n");
564 printf("ioengine=sync\n");
566 printf("ioengine=libaio\niodepth=%u\n", o->depth);
568 if (o->ios[0] && !o->ios[1])
569 printf("rw=randread\n");
570 else if (!o->ios[0] && o->ios[1])
571 printf("rw=randwrite\n");
573 printf("rw=randtrim\n");
575 printf("rw=randrw\n");
576 total = ddir_rw_sum(o->ios);
577 perc = ((float) o->ios[0] * 100.0) / (float) total;
578 printf("rwmixread=%u\n", (int) (perc + 0.99));
581 printf("percentage_random=");
582 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
583 if (o->seq[i] && o->ios[i]) {
584 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
593 printf("%u", (int) perc);
598 for (i = 0; i < o->nr_files; i++) {
601 printf("%s", o->files[i].name);
605 printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
607 time = o->last_ttime - o->first_ttime;
608 time = (time + 1000000000ULL - 1) / 1000000000ULL;
609 printf("runtime=%llus\n", time);
612 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
614 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
617 for (j = 0; j < o->nr_bs[i]; j++) {
618 struct bs *bs = &o->bs[i][j];
620 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
625 if (j + 1 == o->nr_bs[i])
626 printf("%u/", bs->bs);
628 printf("%u/%u", bs->bs, (int) perc);
636 static int __output_p(struct btrace_pid *p, unsigned long *ios)
638 struct btrace_out *o = &p->o;
641 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
642 if (o->nr_bs[i] <= 1)
644 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
648 o->files = malloc(sizeof(struct trace_file));
650 o->files[0].name = filename;
654 __output_p_ascii(p, ios);
656 ret = __output_p_fio(p, ios);
661 static int prune_entry(struct btrace_out *o)
665 if (ddir_rw_sum(o->ios) < ios_threshold)
668 time = (o->last_ttime - o->first_ttime) / 1000ULL;
669 if (time < rt_threshold)
675 static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
677 struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list);
678 struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list);
680 return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
683 static void free_p(struct btrace_pid *p)
685 struct btrace_out *o = &p->o;
688 for (i = 0; i < o->nr_files; i++) {
689 if (o->files[i].name && o->files[i].name != filename)
690 free(o->files[i].name);
693 for (i = 0; i < DDIR_RWDIR_CNT; i++)
697 flist_del(&p->pid_list);
698 flist_del(&p->hash_list);
702 static int output_p(void)
704 unsigned long ios[DDIR_RWDIR_CNT];
705 struct flist_head *e, *tmp;
708 flist_for_each_safe(e, tmp, &pid_list) {
709 struct btrace_pid *p;
711 p = flist_entry(e, struct btrace_pid, pid_list);
712 if (prune_entry(&p->o)) {
716 p->o.start_delay = (p->o.first_ttime / 1000ULL) - first_ttime;
719 memset(ios, 0, sizeof(ios));
721 flist_sort(NULL, &pid_list, entry_cmp);
723 flist_for_each(e, &pid_list) {
724 struct btrace_pid *p;
726 p = flist_entry(e, struct btrace_pid, pid_list);
727 ret |= __output_p(p, ios);
731 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
736 static int usage(char *argv[])
738 log_err("%s: <blktrace bin file>\n", argv[0]);
739 log_err("\t-t\tUsec threshold to ignore task\n");
740 log_err("\t-n\tNumber IOS threshold to ignore task\n");
741 log_err("\t-f\tFio job file output\n");
742 log_err("\t-d\tUse this file/device for replay\n");
746 static int trace_needs_swap(const char *trace_file, int *swap)
748 struct blk_io_trace t;
753 fd = open(trace_file, O_RDONLY);
759 ret = read(fd, &t, sizeof(t));
763 } else if (ret != sizeof(t)) {
764 log_err("fio: short read on trace file\n");
770 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
774 * Maybe it needs to be endian swapped...
776 t.magic = fio_swap32(t.magic);
777 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
782 log_err("fio: blktrace appears corrupt\n");
789 int main(int argc, char *argv[])
796 while ((c = getopt(argc, argv, "t:n:fd:")) != -1) {
799 rt_threshold = atoi(optarg);
802 ios_threshold = atoi(optarg);
808 filename = strdup(optarg);
819 if (trace_needs_swap(argv[optind], &need_swap))
822 for (i = 0; i < PID_HASH_SIZE; i++)
823 INIT_FLIST_HEAD(&pid_hash[i]);
824 for (i = 0; i < INFLIGHT_HASH_SIZE; i++)
825 INIT_FLIST_HEAD(&inflight_hash[i]);
827 load_blktrace(argv[optind], need_swap);
828 first_ttime /= 1000ULL;