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 unsigned int rate_threshold;
21 static unsigned int set_rate;
22 static int output_ascii = 1;
23 static char *filename;
37 unsigned long ios[DDIR_RWDIR_CNT];
38 unsigned long merges[DDIR_RWDIR_CNT];
40 uint64_t last_end[DDIR_RWDIR_CNT];
41 uint64_t seq[DDIR_RWDIR_CNT];
43 struct bs *bs[DDIR_RWDIR_CNT];
44 unsigned int nr_bs[DDIR_RWDIR_CNT];
48 uint64_t first_ttime[DDIR_RWDIR_CNT];
49 uint64_t last_ttime[DDIR_RWDIR_CNT];
50 uint64_t kb[DDIR_RWDIR_CNT];
56 struct flist_head hash_list;
57 struct flist_head pid_list;
60 struct trace_file *files;
62 unsigned int last_major, last_minor;
68 struct flist_head list;
73 #define PID_HASH_BITS 10
74 #define PID_HASH_SIZE (1U << PID_HASH_BITS)
76 static struct flist_head pid_hash[PID_HASH_SIZE];
77 static FLIST_HEAD(pid_list);
79 #define INFLIGHT_HASH_BITS 8
80 #define INFLIGHT_HASH_SIZE (1U << INFLIGHT_HASH_BITS)
81 static struct flist_head inflight_hash[INFLIGHT_HASH_SIZE];
83 static uint64_t first_ttime = -1ULL;
85 static struct inflight *inflight_find(uint64_t sector)
87 struct flist_head *inflight_list;
90 inflight_list = &inflight_hash[hash_long(sector, INFLIGHT_HASH_BITS)];
92 flist_for_each(e, inflight_list) {
93 struct inflight *i = flist_entry(e, struct inflight, list);
95 if (i->end_sector == sector)
102 static void inflight_remove(struct inflight *i)
104 struct btrace_out *o = &i->p->o;
107 assert(o->inflight >= 0);
112 static void __inflight_add(struct inflight *i)
114 struct flist_head *list;
116 list = &inflight_hash[hash_long(i->end_sector, INFLIGHT_HASH_BITS)];
117 flist_add_tail(&i->list, list);
120 static void inflight_add(struct btrace_pid *p, uint64_t sector, uint32_t len)
122 struct btrace_out *o = &p->o;
125 i = calloc(1, sizeof(*i));
128 o->depth = max((int) o->depth, o->inflight);
129 i->end_sector = sector + (len >> 9);
133 static void inflight_merge(struct inflight *i, int rw, unsigned int size)
135 i->p->o.merges[rw]++;
137 i->end_sector += (size >> 9);
144 * fifo refill frontend, to avoid reading data in trace sized bites
146 static int refill_fifo(struct fifo *fifo, int fd)
148 char buf[TRACE_FIFO_SIZE];
153 if (total > fifo_room(fifo))
154 total = fifo_room(fifo);
156 ret = read(fd, buf, total);
158 perror("read refill");
163 ret = fifo_put(fifo, buf, ret);
169 * Retrieve 'len' bytes from the fifo, refilling if necessary.
171 static int trace_fifo_get(struct fifo *fifo, int fd, void *buf,
174 if (fifo_len(fifo) < len) {
175 int ret = refill_fifo(fifo, fd);
181 return fifo_get(fifo, buf, len);
185 * Just discard the pdu by seeking past it.
187 static int discard_pdu(struct fifo *fifo, int fd, struct blk_io_trace *t)
192 return trace_fifo_get(fifo, fd, NULL, t->pdu_len);
195 static int handle_trace_notify(struct blk_io_trace *t)
199 //printf("got process notify: %x, %d\n", t->action, t->pid);
201 case BLK_TN_TIMESTAMP:
202 //printf("got timestamp notify: %x, %d\n", t->action, t->pid);
207 log_err("unknown trace act %x\n", t->action);
214 static void __add_bs(struct btrace_out *o, unsigned int len, int rw)
216 o->bs[rw] = realloc(o->bs[rw], (o->nr_bs[rw] + 1) * sizeof(struct bs));
217 o->bs[rw][o->nr_bs[rw]].bs = len;
218 o->bs[rw][o->nr_bs[rw]].nr = 1;
222 static void add_bs(struct btrace_out *o, unsigned int len, int rw)
224 struct bs *bs = o->bs[rw];
228 __add_bs(o, len, rw);
232 for (i = 0; i < o->nr_bs[rw]; i++) {
233 if (bs[i].bs == len) {
239 __add_bs(o, len, rw);
242 #define FMINORBITS 20
243 #define FMINORMASK ((1U << FMINORBITS) - 1)
244 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
245 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
247 static int btrace_add_file(struct btrace_pid *p, uint32_t devno)
249 unsigned int maj = FMAJOR(devno);
250 unsigned int min = FMINOR(devno);
251 struct trace_file *f;
257 if (p->last_major == maj && p->last_minor == min)
264 * check for this file in our list
266 for (i = 0; i < p->nr_files; i++) {
269 if (f->major == maj && f->minor == min)
274 if (!blktrace_lookup_device(NULL, dev, maj, min)) {
275 log_err("fio: failed to find device %u/%u\n", maj, min);
277 log_err("fio: use -d to specify device\n");
283 p->files = realloc(p->files, (p->nr_files + 1) * sizeof(*f));
284 f = &p->files[p->nr_files];
285 f->name = strdup(dev);
292 static int t_to_rwdir(struct blk_io_trace *t)
294 if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
297 return (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
300 static int handle_trace_discard(struct blk_io_trace *t, struct btrace_pid *p)
302 struct btrace_out *o = &p->o;
304 if (btrace_add_file(p, t->device))
307 if (o->first_ttime[2] == -1ULL)
308 o->first_ttime[2] = t->time;
311 add_bs(o, t->bytes, DDIR_TRIM);
315 static int handle_trace_fs(struct blk_io_trace *t, struct btrace_pid *p)
317 struct btrace_out *o = &p->o;
320 if (btrace_add_file(p, t->device))
323 first_ttime = min(first_ttime, (uint64_t) t->time);
325 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
327 if (o->first_ttime[rw] == -1ULL)
328 o->first_ttime[rw] = t->time;
330 add_bs(o, t->bytes, rw);
333 if (t->sector == o->last_end[rw] || o->last_end[rw] == -1ULL)
336 o->last_end[rw] = t->sector + (t->bytes >> 9);
340 static int handle_queue_trace(struct blk_io_trace *t, struct btrace_pid *p)
342 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
343 return handle_trace_notify(t);
344 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
345 return handle_trace_discard(t, p);
347 return handle_trace_fs(t, p);
350 static int handle_trace(struct blk_io_trace *t, struct btrace_pid *p)
352 unsigned int act = t->action & 0xffff;
355 if (act == __BLK_TA_QUEUE) {
356 inflight_add(p, t->sector, t->bytes);
357 ret = handle_queue_trace(t, p);
358 } else if (act == __BLK_TA_REQUEUE) {
360 } else if (act == __BLK_TA_BACKMERGE) {
363 i = inflight_find(t->sector + (t->bytes >> 9));
367 i = inflight_find(t->sector);
369 inflight_merge(i, t_to_rwdir(t), t->bytes);
370 } else if (act == __BLK_TA_FRONTMERGE) {
373 i = inflight_find(t->sector + (t->bytes >> 9));
377 i = inflight_find(t->sector);
379 inflight_merge(i, t_to_rwdir(t), 0);
380 } else if (act == __BLK_TA_COMPLETE) {
383 i = inflight_find(t->sector + (t->bytes >> 9));
385 i->p->o.kb[t_to_rwdir(t)] += (t->bytes >> 10);
393 static void byteswap_trace(struct blk_io_trace *t)
395 t->magic = fio_swap32(t->magic);
396 t->sequence = fio_swap32(t->sequence);
397 t->time = fio_swap64(t->time);
398 t->sector = fio_swap64(t->sector);
399 t->bytes = fio_swap32(t->bytes);
400 t->action = fio_swap32(t->action);
401 t->pid = fio_swap32(t->pid);
402 t->device = fio_swap32(t->device);
403 t->cpu = fio_swap32(t->cpu);
404 t->error = fio_swap16(t->error);
405 t->pdu_len = fio_swap16(t->pdu_len);
408 static struct btrace_pid *pid_hash_find(pid_t pid, struct flist_head *list)
410 struct flist_head *e;
411 struct btrace_pid *p;
413 flist_for_each(e, list) {
414 p = flist_entry(e, struct btrace_pid, hash_list);
422 static struct btrace_pid *pid_hash_get(pid_t pid)
424 struct flist_head *hash_list;
425 struct btrace_pid *p;
427 hash_list = &pid_hash[hash_long(pid, PID_HASH_BITS)];
429 p = pid_hash_find(pid, hash_list);
433 p = calloc(1, sizeof(*p));
435 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
436 p->o.first_ttime[i] = -1ULL;
437 p->o.last_ttime[i] = -1ULL;
438 p->o.last_end[i] = -1ULL;
442 flist_add_tail(&p->hash_list, hash_list);
443 flist_add_tail(&p->pid_list, &pid_list);
450 * Load a blktrace file by reading all the blk_io_trace entries, and storing
451 * them as io_pieces like the fio text version would do.
453 static int load_blktrace(const char *filename, int need_swap)
455 struct btrace_pid *p;
456 unsigned long traces;
457 struct blk_io_trace t;
461 fd = open(filename, O_RDONLY);
463 perror("open trace file\n");
467 fifo = fifo_alloc(TRACE_FIFO_SIZE);
471 int ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
477 else if (ret < (int) sizeof(t)) {
478 log_err("fio: short fifo get\n");
485 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
486 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
489 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
490 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
493 ret = discard_pdu(fifo, fd, &t);
495 log_err("blktrace lseek\n");
497 } else if (t.pdu_len != ret) {
498 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
502 p = pid_hash_get(t.pid);
503 ret = handle_trace(&t, p);
506 p->o.last_ttime[t_to_rwdir(&t)] = t.time;
517 printf("Traces loaded: %lu\n", traces);
526 static int bs_cmp(const void *ba, const void *bb)
528 const struct bs *bsa = ba;
529 const struct bs *bsb = bb;
531 return bsb->nr - bsa->nr;
534 static unsigned long o_to_kb_rate(struct btrace_out *o, int rw)
536 uint64_t usec = (o->last_ttime[rw] - o->first_ttime[rw]) / 1000ULL;
542 val = o->kb[rw] * 1000ULL;
543 return val / (usec / 1000ULL);
546 static uint64_t o_first_ttime(struct btrace_out *o)
550 first = min(o->first_ttime[0], o->first_ttime[1]);
551 return min(first, o->first_ttime[2]);
554 static uint64_t o_longest_ttime(struct btrace_out *o)
559 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
562 diff = o->last_ttime[i] - o->first_ttime[i];
563 ret = max(diff, ret);
569 static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
571 const char *msg[] = { "reads", "writes", "trims" };
572 struct btrace_out *o = &p->o;
573 unsigned long total, usec;
576 printf("[pid:\t%u]\n", p->pid);
578 total = ddir_rw_sum(o->ios);
579 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
585 ios[i] += o->ios[i] + o->merges[i];
586 printf("%s\n", msg[i]);
587 perc = ((float) o->ios[i] * 100.0) / (float) total;
588 printf("\tios: %lu (perc=%3.2f%%)\n", o->ios[i], perc);
589 perc = ((float) o->merges[i] * 100.0) / (float) total;
590 printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc);
591 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
592 printf("\tseq: %lu (perc=%3.2f%%)\n", (unsigned long) o->seq[i], perc);
593 printf("\trate: %lu KB/sec\n", o_to_kb_rate(o, i));
595 for (j = 0; j < o->nr_bs[i]; j++) {
596 struct bs *bs = &o->bs[i][j];
598 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
599 printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc);
603 printf("depth:\t%u\n", o->depth);
604 usec = o_longest_ttime(o) / 1000ULL;
605 printf("usec:\t%lu (delay=%llu)\n", usec, (unsigned long long) o->start_delay);
608 for (i = 0; i < p->nr_files; i++)
609 printf("%s,", p->files[i].name);
615 static int __output_p_fio(struct btrace_pid *p, unsigned long *ios)
617 struct btrace_out *o = &p->o;
619 unsigned long long time;
623 if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
624 log_err("fio: trace has both read/write and trim\n");
628 log_err("fio: no devices found\n");
632 printf("[pid%u]\n", p->pid);
633 printf("direct=1\n");
635 printf("ioengine=sync\n");
637 printf("ioengine=libaio\niodepth=%u\n", o->depth);
639 if (o->ios[0] && !o->ios[1])
640 printf("rw=randread\n");
641 else if (!o->ios[0] && o->ios[1])
642 printf("rw=randwrite\n");
644 printf("rw=randtrim\n");
646 printf("rw=randrw\n");
647 total = ddir_rw_sum(o->ios);
648 perc = ((float) o->ios[0] * 100.0) / (float) total;
649 printf("rwmixread=%u\n", (int) (perc + 0.99));
652 printf("percentage_random=");
653 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
654 if (o->seq[i] && o->ios[i]) {
655 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
664 printf("%u", (int) perc);
669 for (i = 0; i < p->nr_files; i++) {
672 printf("%s", p->files[i].name);
676 printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
678 time = o_longest_ttime(o);
679 time = (time + 1000000000ULL - 1) / 1000000000ULL;
680 printf("runtime=%llus\n", time);
683 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
685 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
688 for (j = 0; j < o->nr_bs[i]; j++) {
689 struct bs *bs = &o->bs[i][j];
691 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
696 if (j + 1 == o->nr_bs[i])
697 printf("%u/", bs->bs);
699 printf("%u/%u", bs->bs, (int) perc);
706 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
709 rate = o_to_kb_rate(o, i);
713 printf("%luk", rate);
722 static int __output_p(struct btrace_pid *p, unsigned long *ios)
724 struct btrace_out *o = &p->o;
727 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
728 if (o->nr_bs[i] <= 1)
730 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
734 p->files = malloc(sizeof(struct trace_file));
736 p->files[0].name = filename;
740 __output_p_ascii(p, ios);
742 ret = __output_p_fio(p, ios);
747 static void remove_ddir(struct btrace_out *o, int rw)
752 static int prune_entry(struct btrace_out *o)
758 if (ddir_rw_sum(o->ios) < ios_threshold)
761 time = o_longest_ttime(o) / 1000ULL;
762 if (time < rt_threshold)
766 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
767 unsigned long this_rate;
769 this_rate = o_to_kb_rate(o, i);
770 if (this_rate < rate_threshold) {
777 if (rate < rate_threshold)
783 static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
785 struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list);
786 struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list);
788 return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
791 static void free_p(struct btrace_pid *p)
793 struct btrace_out *o = &p->o;
796 for (i = 0; i < p->nr_files; i++) {
797 if (p->files[i].name && p->files[i].name != filename)
798 free(p->files[i].name);
801 for (i = 0; i < DDIR_RWDIR_CNT; i++)
805 flist_del(&p->pid_list);
806 flist_del(&p->hash_list);
810 static int output_p(void)
812 unsigned long ios[DDIR_RWDIR_CNT];
813 struct flist_head *e, *tmp;
816 flist_for_each_safe(e, tmp, &pid_list) {
817 struct btrace_pid *p;
819 p = flist_entry(e, struct btrace_pid, pid_list);
820 if (prune_entry(&p->o)) {
824 p->o.start_delay = (o_first_ttime(&p->o) / 1000ULL) - first_ttime;
827 memset(ios, 0, sizeof(ios));
829 flist_sort(NULL, &pid_list, entry_cmp);
831 flist_for_each(e, &pid_list) {
832 struct btrace_pid *p;
834 p = flist_entry(e, struct btrace_pid, pid_list);
835 ret |= __output_p(p, ios);
836 if (ret && !output_ascii)
841 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
846 static int usage(char *argv[])
848 log_err("%s: <blktrace bin file>\n", argv[0]);
849 log_err("\t-t\tUsec threshold to ignore task\n");
850 log_err("\t-n\tNumber IOS threshold to ignore task\n");
851 log_err("\t-f\tFio job file output\n");
852 log_err("\t-d\tUse this file/device for replay\n");
853 log_err("\t-r\tIgnore jobs with less than this KB/sec rate\n");
854 log_err("\t-R\tSet rate in fio job\n");
858 static int trace_needs_swap(const char *trace_file, int *swap)
860 struct blk_io_trace t;
865 fd = open(trace_file, O_RDONLY);
871 ret = read(fd, &t, sizeof(t));
876 } else if (ret != sizeof(t)) {
878 log_err("fio: short read on trace file\n");
884 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
888 * Maybe it needs to be endian swapped...
890 t.magic = fio_swap32(t.magic);
891 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
896 log_err("fio: blktrace appears corrupt\n");
903 int main(int argc, char *argv[])
910 while ((c = getopt(argc, argv, "t:n:fd:r:R")) != -1) {
916 rate_threshold = atoi(optarg);
919 rt_threshold = atoi(optarg);
922 ios_threshold = atoi(optarg);
928 filename = strdup(optarg);
939 if (trace_needs_swap(argv[optind], &need_swap))
942 for (i = 0; i < PID_HASH_SIZE; i++)
943 INIT_FLIST_HEAD(&pid_hash[i]);
944 for (i = 0; i < INFLIGHT_HASH_SIZE; i++)
945 INIT_FLIST_HEAD(&inflight_hash[i]);
947 load_blktrace(argv[optind], need_swap);
948 first_ttime /= 1000ULL;