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];
55 struct flist_head hash_list;
56 struct flist_head pid_list;
59 struct trace_file *files;
61 unsigned int last_major, last_minor;
67 struct flist_head list;
72 #define PID_HASH_BITS 10
73 #define PID_HASH_SIZE (1U << PID_HASH_BITS)
75 static struct flist_head pid_hash[PID_HASH_SIZE];
76 static FLIST_HEAD(pid_list);
78 #define INFLIGHT_HASH_BITS 8
79 #define INFLIGHT_HASH_SIZE (1U << INFLIGHT_HASH_BITS)
80 static struct flist_head inflight_hash[INFLIGHT_HASH_SIZE];
82 static uint64_t first_ttime = -1ULL;
84 static struct inflight *inflight_find(uint64_t sector)
86 struct flist_head *inflight_list;
89 inflight_list = &inflight_hash[hash_long(sector, INFLIGHT_HASH_BITS)];
91 flist_for_each(e, inflight_list) {
92 struct inflight *i = flist_entry(e, struct inflight, list);
94 if (i->end_sector == sector)
101 static void inflight_remove(struct inflight *i)
103 struct btrace_out *o = &i->p->o;
106 assert(o->inflight >= 0);
111 static void __inflight_add(struct inflight *i)
113 struct flist_head *list;
115 list = &inflight_hash[hash_long(i->end_sector, INFLIGHT_HASH_BITS)];
116 flist_add_tail(&i->list, list);
119 static void inflight_add(struct btrace_pid *p, uint64_t sector, uint32_t len)
121 struct btrace_out *o = &p->o;
124 i = calloc(1, sizeof(*i));
127 o->depth = max((int) o->depth, o->inflight);
128 i->end_sector = sector + (len >> 9);
132 static void inflight_merge(struct inflight *i, int rw, unsigned int size)
134 i->p->o.merges[rw]++;
136 i->end_sector += (size >> 9);
143 * fifo refill frontend, to avoid reading data in trace sized bites
145 static int refill_fifo(struct fifo *fifo, int fd)
147 char buf[TRACE_FIFO_SIZE];
152 if (total > fifo_room(fifo))
153 total = fifo_room(fifo);
155 ret = read(fd, buf, total);
157 perror("read refill");
162 ret = fifo_put(fifo, buf, ret);
168 * Retrieve 'len' bytes from the fifo, refilling if necessary.
170 static int trace_fifo_get(struct fifo *fifo, int fd, void *buf,
173 if (fifo_len(fifo) < len) {
174 int ret = refill_fifo(fifo, fd);
180 return fifo_get(fifo, buf, len);
184 * Just discard the pdu by seeking past it.
186 static int discard_pdu(struct fifo *fifo, int fd, struct blk_io_trace *t)
191 return trace_fifo_get(fifo, fd, NULL, t->pdu_len);
194 static void handle_trace_notify(struct blk_io_trace *t)
198 //printf("got process notify: %x, %d\n", t->action, t->pid);
200 case BLK_TN_TIMESTAMP:
201 //printf("got timestamp notify: %x, %d\n", t->action, t->pid);
206 log_err("unknown trace act %x\n", t->action);
211 static void __add_bs(struct btrace_out *o, unsigned int len, int rw)
213 o->bs[rw] = realloc(o->bs[rw], (o->nr_bs[rw] + 1) * sizeof(struct bs));
214 o->bs[rw][o->nr_bs[rw]].bs = len;
215 o->bs[rw][o->nr_bs[rw]].nr = 1;
219 static void add_bs(struct btrace_out *o, unsigned int len, int rw)
221 struct bs *bs = o->bs[rw];
225 __add_bs(o, len, rw);
229 for (i = 0; i < o->nr_bs[rw]; i++) {
230 if (bs[i].bs == len) {
236 __add_bs(o, len, rw);
239 #define FMINORBITS 20
240 #define FMINORMASK ((1U << FMINORBITS) - 1)
241 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
242 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
244 static void btrace_add_file(struct btrace_pid *p, uint32_t devno)
246 unsigned int maj = FMAJOR(devno);
247 unsigned int min = FMINOR(devno);
248 struct trace_file *f;
254 if (p->last_major == maj && p->last_minor == min)
261 * check for this file in our list
263 for (i = 0; i < p->nr_files; i++) {
266 if (f->major == maj && f->minor == min)
271 if (!blktrace_lookup_device(NULL, dev, maj, min)) {
272 log_err("fio: failed to find device %u/%u\n", maj, min);
276 p->files = realloc(p->files, (p->nr_files + 1) * sizeof(*f));
277 f = &p->files[p->nr_files];
278 f->name = strdup(dev);
284 static void handle_trace_discard(struct blk_io_trace *t, struct btrace_pid *p)
286 struct btrace_out *o = &p->o;
288 btrace_add_file(p, t->device);
290 if (o->first_ttime == -1ULL)
291 o->first_ttime = t->time;
294 add_bs(o, t->bytes, DDIR_TRIM);
297 static void handle_trace_fs(struct blk_io_trace *t, struct btrace_pid *p)
299 struct btrace_out *o = &p->o;
302 btrace_add_file(p, t->device);
304 first_ttime = min(first_ttime, (uint64_t) t->time);
306 if (o->first_ttime == -1ULL)
307 o->first_ttime = t->time;
309 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
311 add_bs(o, t->bytes, rw);
314 if (t->sector == o->last_end[rw] || o->last_end[rw] == -1ULL)
317 o->last_end[rw] = t->sector + (t->bytes >> 9);
320 static void handle_queue_trace(struct blk_io_trace *t, struct btrace_pid *p)
322 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
323 handle_trace_notify(t);
324 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
325 handle_trace_discard(t, p);
327 handle_trace_fs(t, p);
330 static void handle_trace(struct blk_io_trace *t, struct btrace_pid *p)
332 unsigned int act = t->action & 0xffff;
334 if (act == __BLK_TA_QUEUE) {
335 inflight_add(p, t->sector, t->bytes);
336 handle_queue_trace(t, p);
337 } else if (act == __BLK_TA_REQUEUE) {
339 } else if (act == __BLK_TA_BACKMERGE) {
342 i = inflight_find(t->sector + (t->bytes >> 9));
346 i = inflight_find(t->sector);
348 int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
350 inflight_merge(i, rw, t->bytes);
352 } else if (act == __BLK_TA_FRONTMERGE) {
355 i = inflight_find(t->sector + (t->bytes >> 9));
359 i = inflight_find(t->sector);
361 int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
363 inflight_merge(i, rw, 0);
365 } else if (act == __BLK_TA_COMPLETE) {
368 i = inflight_find(t->sector + (t->bytes >> 9));
370 i->p->o.kb += (t->bytes >> 10);
376 static void byteswap_trace(struct blk_io_trace *t)
378 t->magic = fio_swap32(t->magic);
379 t->sequence = fio_swap32(t->sequence);
380 t->time = fio_swap64(t->time);
381 t->sector = fio_swap64(t->sector);
382 t->bytes = fio_swap32(t->bytes);
383 t->action = fio_swap32(t->action);
384 t->pid = fio_swap32(t->pid);
385 t->device = fio_swap32(t->device);
386 t->cpu = fio_swap32(t->cpu);
387 t->error = fio_swap16(t->error);
388 t->pdu_len = fio_swap16(t->pdu_len);
391 static struct btrace_pid *pid_hash_find(pid_t pid, struct flist_head *list)
393 struct flist_head *e;
394 struct btrace_pid *p;
396 flist_for_each(e, list) {
397 p = flist_entry(e, struct btrace_pid, hash_list);
405 static struct btrace_pid *pid_hash_get(pid_t pid)
407 struct flist_head *hash_list;
408 struct btrace_pid *p;
410 hash_list = &pid_hash[hash_long(pid, PID_HASH_BITS)];
412 p = pid_hash_find(pid, hash_list);
416 p = calloc(1, sizeof(*p));
417 p->o.first_ttime = -1ULL;
418 p->o.last_ttime = -1ULL;
420 for (i = 0; i < DDIR_RWDIR_CNT; i++)
421 p->o.last_end[i] = -1ULL;
424 flist_add_tail(&p->hash_list, hash_list);
425 flist_add_tail(&p->pid_list, &pid_list);
432 * Load a blktrace file by reading all the blk_io_trace entries, and storing
433 * them as io_pieces like the fio text version would do.
435 static int load_blktrace(const char *filename, int need_swap)
437 struct btrace_pid *p;
438 unsigned long traces;
439 struct blk_io_trace t;
443 fd = open(filename, O_RDONLY);
445 perror("open trace file\n");
449 fifo = fifo_alloc(TRACE_FIFO_SIZE);
453 int ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
459 else if (ret < (int) sizeof(t)) {
460 log_err("fio: short fifo get\n");
467 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
468 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
471 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
472 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
475 ret = discard_pdu(fifo, fd, &t);
477 log_err("blktrace lseek\n");
479 } else if (t.pdu_len != ret) {
480 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
484 p = pid_hash_get(t.pid);
486 p->o.last_ttime = t.time;
494 printf("Traces loaded: %lu\n", traces);
503 static int bs_cmp(const void *ba, const void *bb)
505 const struct bs *bsa = ba;
506 const struct bs *bsb = bb;
508 return bsb->nr - bsa->nr;
511 static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
513 const char *msg[] = { "reads", "writes", "trims" };
514 struct btrace_out *o = &p->o;
515 unsigned long total, usec;
518 printf("[pid:\t%u]\n", p->pid);
520 total = ddir_rw_sum(o->ios);
521 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
527 ios[i] += o->ios[i] + o->merges[i];
528 printf("%s\n", msg[i]);
529 perc = ((float) o->ios[i] * 100.0) / (float) total;
530 printf("\tios: %lu (perc=%3.2f%%)\n", o->ios[i], perc);
531 perc = ((float) o->merges[i] * 100.0) / (float) total;
532 printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc);
533 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
534 printf("\tseq: %lu (perc=%3.2f%%)\n", o->seq[i], perc);
536 for (j = 0; j < o->nr_bs[i]; j++) {
537 struct bs *bs = &o->bs[i][j];
539 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
540 printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc);
544 printf("depth:\t%u\n", o->depth);
545 usec = (o->last_ttime - o->first_ttime) / 1000ULL;
546 printf("usec:\t%lu (delay=%llu)\n", usec, (unsigned long long) o->start_delay);
547 printf("rate:\t%.2fKB/sec\n", ((float) o->kb * 1000.0) / ((float) usec / 1000.0));
550 for (i = 0; i < p->nr_files; i++)
551 printf("%s,", p->files[i].name);
557 static int __output_p_fio(struct btrace_pid *p, unsigned long *ios)
559 struct btrace_out *o = &p->o;
561 unsigned long long time;
565 if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
566 log_err("fio: trace has both read/write and trim\n");
570 printf("[pid%u]\n", p->pid);
571 printf("direct=1\n");
573 printf("ioengine=sync\n");
575 printf("ioengine=libaio\niodepth=%u\n", o->depth);
577 if (o->ios[0] && !o->ios[1])
578 printf("rw=randread\n");
579 else if (!o->ios[0] && o->ios[1])
580 printf("rw=randwrite\n");
582 printf("rw=randtrim\n");
584 printf("rw=randrw\n");
585 total = ddir_rw_sum(o->ios);
586 perc = ((float) o->ios[0] * 100.0) / (float) total;
587 printf("rwmixread=%u\n", (int) (perc + 0.99));
590 printf("percentage_random=");
591 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
592 if (o->seq[i] && o->ios[i]) {
593 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
602 printf("%u", (int) perc);
607 for (i = 0; i < p->nr_files; i++) {
610 printf("%s", p->files[i].name);
614 printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
616 time = o->last_ttime - o->first_ttime;
617 time = (time + 1000000000ULL - 1) / 1000000000ULL;
618 printf("runtime=%llus\n", time);
621 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
623 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
626 for (j = 0; j < o->nr_bs[i]; j++) {
627 struct bs *bs = &o->bs[i][j];
629 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
634 if (j + 1 == o->nr_bs[i])
635 printf("%u/", bs->bs);
637 printf("%u/%u", bs->bs, (int) perc);
645 static int __output_p(struct btrace_pid *p, unsigned long *ios)
647 struct btrace_out *o = &p->o;
650 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
651 if (o->nr_bs[i] <= 1)
653 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
657 p->files = malloc(sizeof(struct trace_file));
659 p->files[0].name = filename;
663 __output_p_ascii(p, ios);
665 ret = __output_p_fio(p, ios);
670 static int prune_entry(struct btrace_out *o)
674 if (ddir_rw_sum(o->ios) < ios_threshold)
677 time = (o->last_ttime - o->first_ttime) / 1000ULL;
678 if (time < rt_threshold)
684 static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
686 struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list);
687 struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list);
689 return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
692 static void free_p(struct btrace_pid *p)
694 struct btrace_out *o = &p->o;
697 for (i = 0; i < p->nr_files; i++) {
698 if (p->files[i].name && p->files[i].name != filename)
699 free(p->files[i].name);
702 for (i = 0; i < DDIR_RWDIR_CNT; i++)
706 flist_del(&p->pid_list);
707 flist_del(&p->hash_list);
711 static int output_p(void)
713 unsigned long ios[DDIR_RWDIR_CNT];
714 struct flist_head *e, *tmp;
717 flist_for_each_safe(e, tmp, &pid_list) {
718 struct btrace_pid *p;
720 p = flist_entry(e, struct btrace_pid, pid_list);
721 if (prune_entry(&p->o)) {
725 p->o.start_delay = (p->o.first_ttime / 1000ULL) - first_ttime;
728 memset(ios, 0, sizeof(ios));
730 flist_sort(NULL, &pid_list, entry_cmp);
732 flist_for_each(e, &pid_list) {
733 struct btrace_pid *p;
735 p = flist_entry(e, struct btrace_pid, pid_list);
736 ret |= __output_p(p, ios);
740 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
745 static int usage(char *argv[])
747 log_err("%s: <blktrace bin file>\n", argv[0]);
748 log_err("\t-t\tUsec threshold to ignore task\n");
749 log_err("\t-n\tNumber IOS threshold to ignore task\n");
750 log_err("\t-f\tFio job file output\n");
751 log_err("\t-d\tUse this file/device for replay\n");
755 static int trace_needs_swap(const char *trace_file, int *swap)
757 struct blk_io_trace t;
762 fd = open(trace_file, O_RDONLY);
768 ret = read(fd, &t, sizeof(t));
772 } else if (ret != sizeof(t)) {
773 log_err("fio: short read on trace file\n");
779 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
783 * Maybe it needs to be endian swapped...
785 t.magic = fio_swap32(t.magic);
786 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
791 log_err("fio: blktrace appears corrupt\n");
798 int main(int argc, char *argv[])
805 while ((c = getopt(argc, argv, "t:n:fd:")) != -1) {
808 rt_threshold = atoi(optarg);
811 ios_threshold = atoi(optarg);
817 filename = strdup(optarg);
828 if (trace_needs_swap(argv[optind], &need_swap))
831 for (i = 0; i < PID_HASH_SIZE; i++)
832 INIT_FLIST_HEAD(&pid_hash[i]);
833 for (i = 0; i < INFLIGHT_HASH_SIZE; i++)
834 INIT_FLIST_HEAD(&inflight_hash[i]);
836 load_blktrace(argv[optind], need_swap);
837 first_ttime /= 1000ULL;