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];
54 struct flist_head hash_list;
55 struct flist_head pid_list;
58 struct trace_file *files;
60 unsigned int last_major, last_minor;
66 struct flist_head list;
71 #define PID_HASH_BITS 10
72 #define PID_HASH_SIZE (1U << PID_HASH_BITS)
74 static struct flist_head pid_hash[PID_HASH_SIZE];
75 static FLIST_HEAD(pid_list);
77 #define INFLIGHT_HASH_BITS 8
78 #define INFLIGHT_HASH_SIZE (1U << INFLIGHT_HASH_BITS)
79 static struct flist_head inflight_hash[INFLIGHT_HASH_SIZE];
81 static uint64_t first_ttime = -1ULL;
83 static struct inflight *inflight_find(uint64_t sector)
85 struct flist_head *inflight_list;
88 inflight_list = &inflight_hash[hash_long(sector, INFLIGHT_HASH_BITS)];
90 flist_for_each(e, inflight_list) {
91 struct inflight *i = flist_entry(e, struct inflight, list);
93 if (i->end_sector == sector)
100 static void inflight_remove(struct inflight *i)
102 struct btrace_out *o = &i->p->o;
105 assert(o->inflight >= 0);
110 static void __inflight_add(struct inflight *i)
112 struct flist_head *list;
114 list = &inflight_hash[hash_long(i->end_sector, INFLIGHT_HASH_BITS)];
115 flist_add_tail(&i->list, list);
118 static void inflight_add(struct btrace_pid *p, uint64_t sector, uint32_t len)
120 struct btrace_out *o = &p->o;
123 i = calloc(1, sizeof(*i));
126 o->depth = max((int) o->depth, o->inflight);
127 i->end_sector = sector + (len >> 9);
131 static void inflight_merge(struct inflight *i, int rw, unsigned int size)
133 i->p->o.merges[rw]++;
135 i->end_sector += (size >> 9);
142 * fifo refill frontend, to avoid reading data in trace sized bites
144 static int refill_fifo(struct fifo *fifo, int fd)
146 char buf[TRACE_FIFO_SIZE];
151 if (total > fifo_room(fifo))
152 total = fifo_room(fifo);
154 ret = read(fd, buf, total);
156 perror("read refill");
161 ret = fifo_put(fifo, buf, ret);
167 * Retrieve 'len' bytes from the fifo, refilling if necessary.
169 static int trace_fifo_get(struct fifo *fifo, int fd, void *buf,
172 if (fifo_len(fifo) < len) {
173 int ret = refill_fifo(fifo, fd);
179 return fifo_get(fifo, buf, len);
183 * Just discard the pdu by seeking past it.
185 static int discard_pdu(struct fifo *fifo, int fd, struct blk_io_trace *t)
190 return trace_fifo_get(fifo, fd, NULL, t->pdu_len);
193 static void handle_trace_notify(struct blk_io_trace *t)
197 //printf("got process notify: %x, %d\n", t->action, t->pid);
199 case BLK_TN_TIMESTAMP:
200 //printf("got timestamp notify: %x, %d\n", t->action, t->pid);
205 log_err("unknown trace act %x\n", t->action);
210 static void __add_bs(struct btrace_out *o, unsigned int len, int rw)
212 o->bs[rw] = realloc(o->bs[rw], (o->nr_bs[rw] + 1) * sizeof(struct bs));
213 o->bs[rw][o->nr_bs[rw]].bs = len;
214 o->bs[rw][o->nr_bs[rw]].nr = 1;
218 static void add_bs(struct btrace_out *o, unsigned int len, int rw)
220 struct bs *bs = o->bs[rw];
224 __add_bs(o, len, rw);
228 for (i = 0; i < o->nr_bs[rw]; i++) {
229 if (bs[i].bs == len) {
235 __add_bs(o, len, rw);
238 #define FMINORBITS 20
239 #define FMINORMASK ((1U << FMINORBITS) - 1)
240 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
241 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
243 static void btrace_add_file(struct btrace_pid *p, uint32_t devno)
245 unsigned int maj = FMAJOR(devno);
246 unsigned int min = FMINOR(devno);
247 struct trace_file *f;
253 if (p->last_major == maj && p->last_minor == min)
260 * check for this file in our list
262 for (i = 0; i < p->nr_files; i++) {
265 if (f->major == maj && f->minor == min)
270 if (!blktrace_lookup_device(NULL, dev, maj, min)) {
271 log_err("fio: failed to find device %u/%u\n", maj, min);
275 p->files = realloc(p->files, (p->nr_files + 1) * sizeof(*f));
276 f = &p->files[p->nr_files];
277 f->name = strdup(dev);
283 static void handle_trace_discard(struct blk_io_trace *t, struct btrace_pid *p)
285 struct btrace_out *o = &p->o;
287 btrace_add_file(p, t->device);
289 if (o->first_ttime == -1ULL)
290 o->first_ttime = t->time;
293 add_bs(o, t->bytes, DDIR_TRIM);
296 static void handle_trace_fs(struct blk_io_trace *t, struct btrace_pid *p)
298 struct btrace_out *o = &p->o;
301 btrace_add_file(p, t->device);
303 first_ttime = min(first_ttime, (uint64_t) t->time);
305 if (o->first_ttime == -1ULL)
306 o->first_ttime = t->time;
308 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
310 add_bs(o, t->bytes, rw);
313 if (t->sector == o->last_end[rw] || o->last_end[rw] == -1ULL)
316 o->last_end[rw] = t->sector + (t->bytes >> 9);
319 static void handle_queue_trace(struct blk_io_trace *t, struct btrace_pid *p)
321 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
322 handle_trace_notify(t);
323 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
324 handle_trace_discard(t, p);
326 handle_trace_fs(t, p);
329 static void handle_trace(struct blk_io_trace *t, struct btrace_pid *p)
331 unsigned int act = t->action & 0xffff;
333 if (act == __BLK_TA_QUEUE) {
334 inflight_add(p, t->sector, t->bytes);
335 handle_queue_trace(t, p);
336 } else if (act == __BLK_TA_REQUEUE) {
338 } else if (act == __BLK_TA_BACKMERGE) {
341 i = inflight_find(t->sector + (t->bytes >> 9));
345 i = inflight_find(t->sector);
347 int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
349 inflight_merge(i, rw, t->bytes);
351 } else if (act == __BLK_TA_FRONTMERGE) {
354 i = inflight_find(t->sector + (t->bytes >> 9));
358 i = inflight_find(t->sector);
360 int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
362 inflight_merge(i, rw, 0);
364 } else if (act == __BLK_TA_COMPLETE) {
367 i = inflight_find(t->sector + (t->bytes >> 9));
373 static void byteswap_trace(struct blk_io_trace *t)
375 t->magic = fio_swap32(t->magic);
376 t->sequence = fio_swap32(t->sequence);
377 t->time = fio_swap64(t->time);
378 t->sector = fio_swap64(t->sector);
379 t->bytes = fio_swap32(t->bytes);
380 t->action = fio_swap32(t->action);
381 t->pid = fio_swap32(t->pid);
382 t->device = fio_swap32(t->device);
383 t->cpu = fio_swap32(t->cpu);
384 t->error = fio_swap16(t->error);
385 t->pdu_len = fio_swap16(t->pdu_len);
388 static struct btrace_pid *pid_hash_find(pid_t pid, struct flist_head *list)
390 struct flist_head *e;
391 struct btrace_pid *p;
393 flist_for_each(e, list) {
394 p = flist_entry(e, struct btrace_pid, hash_list);
402 static struct btrace_pid *pid_hash_get(pid_t pid)
404 struct flist_head *hash_list;
405 struct btrace_pid *p;
407 hash_list = &pid_hash[hash_long(pid, PID_HASH_BITS)];
409 p = pid_hash_find(pid, hash_list);
413 p = calloc(1, sizeof(*p));
414 p->o.first_ttime = -1ULL;
415 p->o.last_ttime = -1ULL;
417 for (i = 0; i < DDIR_RWDIR_CNT; i++)
418 p->o.last_end[i] = -1ULL;
421 flist_add_tail(&p->hash_list, hash_list);
422 flist_add_tail(&p->pid_list, &pid_list);
429 * Load a blktrace file by reading all the blk_io_trace entries, and storing
430 * them as io_pieces like the fio text version would do.
432 static int load_blktrace(const char *filename, int need_swap)
434 struct btrace_pid *p;
435 unsigned long traces;
436 struct blk_io_trace t;
440 fd = open(filename, O_RDONLY);
442 perror("open trace file\n");
446 fifo = fifo_alloc(TRACE_FIFO_SIZE);
450 int ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
456 else if (ret < (int) sizeof(t)) {
457 log_err("fio: short fifo get\n");
464 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
465 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
468 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
469 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
472 ret = discard_pdu(fifo, fd, &t);
474 log_err("blktrace lseek\n");
476 } else if (t.pdu_len != ret) {
477 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
481 p = pid_hash_get(t.pid);
483 p->o.last_ttime = t.time;
491 printf("Traces loaded: %lu\n", traces);
500 static int bs_cmp(const void *ba, const void *bb)
502 const struct bs *bsa = ba;
503 const struct bs *bsb = bb;
505 return bsb->nr - bsa->nr;
508 static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
510 const char *msg[] = { "reads", "writes", "trims" };
511 struct btrace_out *o = &p->o;
515 printf("[pid:\t%u]\n", p->pid);
517 total = ddir_rw_sum(o->ios);
518 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
524 ios[i] += o->ios[i] + o->merges[i];
525 printf("%s\n", msg[i]);
526 perc = ((float) o->ios[i] * 100.0) / (float) total;
527 printf("\tios: %lu (perc=%3.2f%%)\n", o->ios[i], perc);
528 perc = ((float) o->merges[i] * 100.0) / (float) total;
529 printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc);
530 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
531 printf("\tseq: %lu (perc=%3.2f%%)\n", o->seq[i], perc);
533 for (j = 0; j < o->nr_bs[i]; j++) {
534 struct bs *bs = &o->bs[i][j];
536 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
537 printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc);
541 printf("depth:\t%u\n", o->depth);
542 printf("usec:\t%llu (delay=%llu)\n", (o->last_ttime - o->first_ttime) / 1000ULL, (unsigned long long) o->start_delay);
545 for (i = 0; i < p->nr_files; i++)
546 printf("%s,", p->files[i].name);
552 static int __output_p_fio(struct btrace_pid *p, unsigned long *ios)
554 struct btrace_out *o = &p->o;
556 unsigned long long time;
560 if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
561 log_err("fio: trace has both read/write and trim\n");
565 printf("[pid%u]\n", p->pid);
566 printf("direct=1\n");
568 printf("ioengine=sync\n");
570 printf("ioengine=libaio\niodepth=%u\n", o->depth);
572 if (o->ios[0] && !o->ios[1])
573 printf("rw=randread\n");
574 else if (!o->ios[0] && o->ios[1])
575 printf("rw=randwrite\n");
577 printf("rw=randtrim\n");
579 printf("rw=randrw\n");
580 total = ddir_rw_sum(o->ios);
581 perc = ((float) o->ios[0] * 100.0) / (float) total;
582 printf("rwmixread=%u\n", (int) (perc + 0.99));
585 printf("percentage_random=");
586 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
587 if (o->seq[i] && o->ios[i]) {
588 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
597 printf("%u", (int) perc);
602 for (i = 0; i < p->nr_files; i++) {
605 printf("%s", p->files[i].name);
609 printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
611 time = o->last_ttime - o->first_ttime;
612 time = (time + 1000000000ULL - 1) / 1000000000ULL;
613 printf("runtime=%llus\n", time);
616 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
618 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
621 for (j = 0; j < o->nr_bs[i]; j++) {
622 struct bs *bs = &o->bs[i][j];
624 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
629 if (j + 1 == o->nr_bs[i])
630 printf("%u/", bs->bs);
632 printf("%u/%u", bs->bs, (int) perc);
640 static int __output_p(struct btrace_pid *p, unsigned long *ios)
642 struct btrace_out *o = &p->o;
645 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
646 if (o->nr_bs[i] <= 1)
648 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
652 p->files = malloc(sizeof(struct trace_file));
654 p->files[0].name = filename;
658 __output_p_ascii(p, ios);
660 ret = __output_p_fio(p, ios);
665 static int prune_entry(struct btrace_out *o)
669 if (ddir_rw_sum(o->ios) < ios_threshold)
672 time = (o->last_ttime - o->first_ttime) / 1000ULL;
673 if (time < rt_threshold)
679 static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
681 struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list);
682 struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list);
684 return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
687 static void free_p(struct btrace_pid *p)
689 struct btrace_out *o = &p->o;
692 for (i = 0; i < p->nr_files; i++) {
693 if (p->files[i].name && p->files[i].name != filename)
694 free(p->files[i].name);
697 for (i = 0; i < DDIR_RWDIR_CNT; i++)
701 flist_del(&p->pid_list);
702 flist_del(&p->hash_list);
706 static int output_p(void)
708 unsigned long ios[DDIR_RWDIR_CNT];
709 struct flist_head *e, *tmp;
712 flist_for_each_safe(e, tmp, &pid_list) {
713 struct btrace_pid *p;
715 p = flist_entry(e, struct btrace_pid, pid_list);
716 if (prune_entry(&p->o)) {
720 p->o.start_delay = (p->o.first_ttime / 1000ULL) - first_ttime;
723 memset(ios, 0, sizeof(ios));
725 flist_sort(NULL, &pid_list, entry_cmp);
727 flist_for_each(e, &pid_list) {
728 struct btrace_pid *p;
730 p = flist_entry(e, struct btrace_pid, pid_list);
731 ret |= __output_p(p, ios);
735 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
740 static int usage(char *argv[])
742 log_err("%s: <blktrace bin file>\n", argv[0]);
743 log_err("\t-t\tUsec threshold to ignore task\n");
744 log_err("\t-n\tNumber IOS threshold to ignore task\n");
745 log_err("\t-f\tFio job file output\n");
746 log_err("\t-d\tUse this file/device for replay\n");
750 static int trace_needs_swap(const char *trace_file, int *swap)
752 struct blk_io_trace t;
757 fd = open(trace_file, O_RDONLY);
763 ret = read(fd, &t, sizeof(t));
767 } else if (ret != sizeof(t)) {
768 log_err("fio: short read on trace file\n");
774 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
778 * Maybe it needs to be endian swapped...
780 t.magic = fio_swap32(t.magic);
781 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
786 log_err("fio: blktrace appears corrupt\n");
793 int main(int argc, char *argv[])
800 while ((c = getopt(argc, argv, "t:n:fd:")) != -1) {
803 rt_threshold = atoi(optarg);
806 ios_threshold = atoi(optarg);
812 filename = strdup(optarg);
823 if (trace_needs_swap(argv[optind], &need_swap))
826 for (i = 0; i < PID_HASH_SIZE; i++)
827 INIT_FLIST_HEAD(&pid_hash[i]);
828 for (i = 0; i < INFLIGHT_HASH_SIZE; i++)
829 INIT_FLIST_HEAD(&inflight_hash[i]);
831 load_blktrace(argv[optind], need_swap);
832 first_ttime /= 1000ULL;