Makefile: add -Wshadow
[fio.git] / t / btrace2fio.c
CommitLineData
40bafa33
JA
1#include <stdio.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <inttypes.h>
5#include <assert.h>
6
7#include "../io_ddir.h"
8#include "../flist.h"
9#include "../hash.h"
10#include "../fifo.h"
11#include "../blktrace_api.h"
12#include "../os/os.h"
13#include "../log.h"
14#include "../lib/linux-dev-lookup.h"
15
16#define TRACE_FIFO_SIZE 8192
17
18static unsigned int rt_threshold = 1000000;
19static unsigned int ios_threshold = 10;
8fc46b4d
JA
20static unsigned int rate_threshold;
21static unsigned int set_rate;
85fcdac8 22static unsigned int max_depth = 256;
40bafa33 23static int output_ascii = 1;
35661615 24static char *filename;
40bafa33
JA
25
26struct bs {
27 unsigned int bs;
28 unsigned int nr;
29 int merges;
30};
31
32struct trace_file {
33 char *name;
34 int major, minor;
35};
36
37struct btrace_out {
38 unsigned long ios[DDIR_RWDIR_CNT];
40bafa33
JA
39 unsigned long merges[DDIR_RWDIR_CNT];
40
41 uint64_t last_end[DDIR_RWDIR_CNT];
42 uint64_t seq[DDIR_RWDIR_CNT];
43
44 struct bs *bs[DDIR_RWDIR_CNT];
45 unsigned int nr_bs[DDIR_RWDIR_CNT];
46
47 int inflight;
48 unsigned int depth;
85fcdac8
JA
49 int depth_disabled;
50 int complete_seen;
51
8fc46b4d
JA
52 uint64_t first_ttime[DDIR_RWDIR_CNT];
53 uint64_t last_ttime[DDIR_RWDIR_CNT];
54 uint64_t kb[DDIR_RWDIR_CNT];
40bafa33 55
40bafa33
JA
56 uint64_t start_delay;
57};
58
59struct btrace_pid {
60 struct flist_head hash_list;
61 struct flist_head pid_list;
62 pid_t pid;
65ff9cd5
JA
63
64 struct trace_file *files;
65 int nr_files;
66 unsigned int last_major, last_minor;
67
40bafa33
JA
68 struct btrace_out o;
69};
70
71struct inflight {
72 struct flist_head list;
73 struct btrace_pid *p;
74 uint64_t end_sector;
75};
76
77#define PID_HASH_BITS 10
78#define PID_HASH_SIZE (1U << PID_HASH_BITS)
79
80static struct flist_head pid_hash[PID_HASH_SIZE];
81static FLIST_HEAD(pid_list);
82
17bf0853
JA
83#define INFLIGHT_HASH_BITS 8
84#define INFLIGHT_HASH_SIZE (1U << INFLIGHT_HASH_BITS)
85static struct flist_head inflight_hash[INFLIGHT_HASH_SIZE];
40bafa33
JA
86
87static uint64_t first_ttime = -1ULL;
88
89static struct inflight *inflight_find(uint64_t sector)
90{
17bf0853 91 struct flist_head *inflight_list;
40bafa33
JA
92 struct flist_head *e;
93
17bf0853
JA
94 inflight_list = &inflight_hash[hash_long(sector, INFLIGHT_HASH_BITS)];
95
96 flist_for_each(e, inflight_list) {
40bafa33
JA
97 struct inflight *i = flist_entry(e, struct inflight, list);
98
99 if (i->end_sector == sector)
100 return i;
101 }
102
103 return NULL;
104}
105
106static void inflight_remove(struct inflight *i)
107{
108 struct btrace_out *o = &i->p->o;
109
110 o->inflight--;
111 assert(o->inflight >= 0);
112 flist_del(&i->list);
113 free(i);
114}
115
17bf0853 116static void __inflight_add(struct inflight *i)
40bafa33 117{
17bf0853
JA
118 struct flist_head *list;
119
120 list = &inflight_hash[hash_long(i->end_sector, INFLIGHT_HASH_BITS)];
121 flist_add_tail(&i->list, list);
40bafa33
JA
122}
123
124static void inflight_add(struct btrace_pid *p, uint64_t sector, uint32_t len)
125{
126 struct btrace_out *o = &p->o;
127 struct inflight *i;
128
129 i = calloc(1, sizeof(*i));
130 i->p = p;
131 o->inflight++;
85fcdac8
JA
132 if (!o->depth_disabled) {
133 o->depth = max((int) o->depth, o->inflight);
134 if (o->depth >= max_depth && !o->complete_seen) {
135 o->depth_disabled = 1;
136 o->depth = max_depth;
137 }
138 }
40bafa33 139 i->end_sector = sector + (len >> 9);
17bf0853
JA
140 __inflight_add(i);
141}
142
143static void inflight_merge(struct inflight *i, int rw, unsigned int size)
144{
145 i->p->o.merges[rw]++;
146 if (size) {
147 i->end_sector += (size >> 9);
148 flist_del(&i->list);
149 __inflight_add(i);
150 }
40bafa33
JA
151}
152
153/*
154 * fifo refill frontend, to avoid reading data in trace sized bites
155 */
156static int refill_fifo(struct fifo *fifo, int fd)
157{
158 char buf[TRACE_FIFO_SIZE];
159 unsigned int total;
160 int ret;
161
162 total = sizeof(buf);
163 if (total > fifo_room(fifo))
164 total = fifo_room(fifo);
165
166 ret = read(fd, buf, total);
167 if (ret < 0) {
168 perror("read refill");
169 return -1;
170 }
171
172 if (ret > 0)
173 ret = fifo_put(fifo, buf, ret);
174
175 return ret;
176}
177
178/*
179 * Retrieve 'len' bytes from the fifo, refilling if necessary.
180 */
181static int trace_fifo_get(struct fifo *fifo, int fd, void *buf,
182 unsigned int len)
183{
184 if (fifo_len(fifo) < len) {
185 int ret = refill_fifo(fifo, fd);
186
187 if (ret < 0)
188 return ret;
189 }
190
191 return fifo_get(fifo, buf, len);
192}
193
194/*
195 * Just discard the pdu by seeking past it.
196 */
197static int discard_pdu(struct fifo *fifo, int fd, struct blk_io_trace *t)
198{
199 if (t->pdu_len == 0)
200 return 0;
201
202 return trace_fifo_get(fifo, fd, NULL, t->pdu_len);
203}
204
8fc46b4d 205static int handle_trace_notify(struct blk_io_trace *t)
40bafa33
JA
206{
207 switch (t->action) {
208 case BLK_TN_PROCESS:
209 //printf("got process notify: %x, %d\n", t->action, t->pid);
210 break;
211 case BLK_TN_TIMESTAMP:
212 //printf("got timestamp notify: %x, %d\n", t->action, t->pid);
213 break;
214 case BLK_TN_MESSAGE:
215 break;
216 default:
17bf0853 217 log_err("unknown trace act %x\n", t->action);
8fc46b4d 218 return 1;
40bafa33 219 }
8fc46b4d
JA
220
221 return 0;
40bafa33
JA
222}
223
224static void __add_bs(struct btrace_out *o, unsigned int len, int rw)
225{
226 o->bs[rw] = realloc(o->bs[rw], (o->nr_bs[rw] + 1) * sizeof(struct bs));
227 o->bs[rw][o->nr_bs[rw]].bs = len;
228 o->bs[rw][o->nr_bs[rw]].nr = 1;
229 o->nr_bs[rw]++;
230}
231
232static void add_bs(struct btrace_out *o, unsigned int len, int rw)
233{
234 struct bs *bs = o->bs[rw];
235 int i;
236
237 if (!o->nr_bs[rw]) {
238 __add_bs(o, len, rw);
239 return;
240 }
241
242 for (i = 0; i < o->nr_bs[rw]; i++) {
243 if (bs[i].bs == len) {
244 bs[i].nr++;
245 return;
246 }
247 }
248
249 __add_bs(o, len, rw);
250}
251
252#define FMINORBITS 20
253#define FMINORMASK ((1U << FMINORBITS) - 1)
254#define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
255#define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
256
8fc46b4d 257static int btrace_add_file(struct btrace_pid *p, uint32_t devno)
40bafa33
JA
258{
259 unsigned int maj = FMAJOR(devno);
260 unsigned int min = FMINOR(devno);
261 struct trace_file *f;
262 unsigned int i;
263 char dev[256];
264
35661615 265 if (filename)
8fc46b4d 266 return 0;
65ff9cd5 267 if (p->last_major == maj && p->last_minor == min)
8fc46b4d 268 return 0;
40bafa33 269
65ff9cd5
JA
270 p->last_major = maj;
271 p->last_minor = min;
40bafa33
JA
272
273 /*
274 * check for this file in our list
275 */
65ff9cd5
JA
276 for (i = 0; i < p->nr_files; i++) {
277 f = &p->files[i];
40bafa33
JA
278
279 if (f->major == maj && f->minor == min)
8fc46b4d 280 return 0;
40bafa33
JA
281 }
282
283 strcpy(dev, "/dev");
284 if (!blktrace_lookup_device(NULL, dev, maj, min)) {
285 log_err("fio: failed to find device %u/%u\n", maj, min);
8fc46b4d
JA
286 if (!output_ascii) {
287 log_err("fio: use -d to specify device\n");
288 return 1;
289 }
290 return 0;
40bafa33
JA
291 }
292
65ff9cd5
JA
293 p->files = realloc(p->files, (p->nr_files + 1) * sizeof(*f));
294 f = &p->files[p->nr_files];
40bafa33
JA
295 f->name = strdup(dev);
296 f->major = maj;
297 f->minor = min;
65ff9cd5 298 p->nr_files++;
8fc46b4d
JA
299 return 0;
300}
301
302static int t_to_rwdir(struct blk_io_trace *t)
303{
304 if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
305 return DDIR_TRIM;
306
307 return (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
40bafa33
JA
308}
309
8fc46b4d 310static int handle_trace_discard(struct blk_io_trace *t, struct btrace_pid *p)
40bafa33 311{
65ff9cd5
JA
312 struct btrace_out *o = &p->o;
313
8fc46b4d
JA
314 if (btrace_add_file(p, t->device))
315 return 1;
40bafa33 316
8fc46b4d
JA
317 if (o->first_ttime[2] == -1ULL)
318 o->first_ttime[2] = t->time;
40bafa33
JA
319
320 o->ios[DDIR_TRIM]++;
321 add_bs(o, t->bytes, DDIR_TRIM);
8fc46b4d 322 return 0;
40bafa33
JA
323}
324
8fc46b4d 325static int handle_trace_fs(struct blk_io_trace *t, struct btrace_pid *p)
40bafa33 326{
65ff9cd5 327 struct btrace_out *o = &p->o;
40bafa33
JA
328 int rw;
329
8fc46b4d
JA
330 if (btrace_add_file(p, t->device))
331 return 1;
40bafa33
JA
332
333 first_ttime = min(first_ttime, (uint64_t) t->time);
334
40bafa33
JA
335 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
336
8fc46b4d
JA
337 if (o->first_ttime[rw] == -1ULL)
338 o->first_ttime[rw] = t->time;
339
40bafa33
JA
340 add_bs(o, t->bytes, rw);
341 o->ios[rw]++;
342
343 if (t->sector == o->last_end[rw] || o->last_end[rw] == -1ULL)
344 o->seq[rw]++;
345
346 o->last_end[rw] = t->sector + (t->bytes >> 9);
8fc46b4d 347 return 0;
40bafa33
JA
348}
349
8fc46b4d 350static int handle_queue_trace(struct blk_io_trace *t, struct btrace_pid *p)
40bafa33
JA
351{
352 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
8fc46b4d 353 return handle_trace_notify(t);
40bafa33 354 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
8fc46b4d 355 return handle_trace_discard(t, p);
40bafa33 356 else
8fc46b4d 357 return handle_trace_fs(t, p);
40bafa33
JA
358}
359
8fc46b4d 360static int handle_trace(struct blk_io_trace *t, struct btrace_pid *p)
40bafa33
JA
361{
362 unsigned int act = t->action & 0xffff;
8fc46b4d 363 int ret = 0;
40bafa33
JA
364
365 if (act == __BLK_TA_QUEUE) {
366 inflight_add(p, t->sector, t->bytes);
8fc46b4d 367 ret = handle_queue_trace(t, p);
40bafa33
JA
368 } else if (act == __BLK_TA_REQUEUE) {
369 p->o.inflight--;
370 } else if (act == __BLK_TA_BACKMERGE) {
371 struct inflight *i;
372
373 i = inflight_find(t->sector + (t->bytes >> 9));
374 if (i)
375 inflight_remove(i);
376
377 i = inflight_find(t->sector);
8fc46b4d
JA
378 if (i)
379 inflight_merge(i, t_to_rwdir(t), t->bytes);
40bafa33
JA
380 } else if (act == __BLK_TA_FRONTMERGE) {
381 struct inflight *i;
382
383 i = inflight_find(t->sector + (t->bytes >> 9));
384 if (i)
385 inflight_remove(i);
386
387 i = inflight_find(t->sector);
8fc46b4d
JA
388 if (i)
389 inflight_merge(i, t_to_rwdir(t), 0);
40bafa33
JA
390 } else if (act == __BLK_TA_COMPLETE) {
391 struct inflight *i;
392
393 i = inflight_find(t->sector + (t->bytes >> 9));
cea475cd 394 if (i) {
8fc46b4d 395 i->p->o.kb[t_to_rwdir(t)] += (t->bytes >> 10);
85fcdac8 396 i->p->o.complete_seen = 1;
40bafa33 397 inflight_remove(i);
cea475cd 398 }
40bafa33 399 }
8fc46b4d
JA
400
401 return ret;
40bafa33
JA
402}
403
404static void byteswap_trace(struct blk_io_trace *t)
405{
406 t->magic = fio_swap32(t->magic);
407 t->sequence = fio_swap32(t->sequence);
408 t->time = fio_swap64(t->time);
409 t->sector = fio_swap64(t->sector);
410 t->bytes = fio_swap32(t->bytes);
411 t->action = fio_swap32(t->action);
412 t->pid = fio_swap32(t->pid);
413 t->device = fio_swap32(t->device);
414 t->cpu = fio_swap32(t->cpu);
415 t->error = fio_swap16(t->error);
416 t->pdu_len = fio_swap16(t->pdu_len);
417}
418
419static struct btrace_pid *pid_hash_find(pid_t pid, struct flist_head *list)
420{
421 struct flist_head *e;
422 struct btrace_pid *p;
423
424 flist_for_each(e, list) {
425 p = flist_entry(e, struct btrace_pid, hash_list);
426 if (p->pid == pid)
427 return p;
428 }
429
430 return NULL;
431}
432
433static struct btrace_pid *pid_hash_get(pid_t pid)
434{
435 struct flist_head *hash_list;
436 struct btrace_pid *p;
437
438 hash_list = &pid_hash[hash_long(pid, PID_HASH_BITS)];
439
440 p = pid_hash_find(pid, hash_list);
441 if (!p) {
442 int i;
443
444 p = calloc(1, sizeof(*p));
40bafa33 445
8fc46b4d
JA
446 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
447 p->o.first_ttime[i] = -1ULL;
448 p->o.last_ttime[i] = -1ULL;
40bafa33 449 p->o.last_end[i] = -1ULL;
8fc46b4d 450 }
40bafa33
JA
451
452 p->pid = pid;
453 flist_add_tail(&p->hash_list, hash_list);
454 flist_add_tail(&p->pid_list, &pid_list);
455 }
456
457 return p;
458}
459
460/*
461 * Load a blktrace file by reading all the blk_io_trace entries, and storing
462 * them as io_pieces like the fio text version would do.
463 */
8a1db9a1 464static int load_blktrace(const char *fname, int need_swap)
40bafa33
JA
465{
466 struct btrace_pid *p;
467 unsigned long traces;
468 struct blk_io_trace t;
469 struct fifo *fifo;
8fc46b4d 470 int fd, ret = 0;
40bafa33 471
8a1db9a1 472 fd = open(fname, O_RDONLY);
40bafa33
JA
473 if (fd < 0) {
474 perror("open trace file\n");
475 return 1;
476 }
477
478 fifo = fifo_alloc(TRACE_FIFO_SIZE);
479
480 traces = 0;
481 do {
8a1db9a1 482 ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
40bafa33
JA
483 if (ret < 0)
484 goto err;
485 else if (!ret)
486 break;
487 else if (ret < (int) sizeof(t)) {
17bf0853 488 log_err("fio: short fifo get\n");
40bafa33
JA
489 break;
490 }
491
492 if (need_swap)
493 byteswap_trace(&t);
494
495 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
17bf0853 496 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
40bafa33
JA
497 goto err;
498 }
499 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
17bf0853 500 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
40bafa33
JA
501 goto err;
502 }
503 ret = discard_pdu(fifo, fd, &t);
504 if (ret < 0) {
17bf0853 505 log_err("blktrace lseek\n");
40bafa33
JA
506 goto err;
507 } else if (t.pdu_len != ret) {
17bf0853 508 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
40bafa33
JA
509 goto err;
510 }
511
512 p = pid_hash_get(t.pid);
8fc46b4d
JA
513 ret = handle_trace(&t, p);
514 if (ret)
515 break;
516 p->o.last_ttime[t_to_rwdir(&t)] = t.time;
40bafa33
JA
517 traces++;
518 } while (1);
519
520 fifo_free(fifo);
521 close(fd);
522
8fc46b4d
JA
523 if (ret)
524 return ret;
525
40bafa33
JA
526 if (output_ascii)
527 printf("Traces loaded: %lu\n", traces);
528
529 return 0;
530err:
531 close(fd);
532 fifo_free(fifo);
533 return 1;
534}
535
536static int bs_cmp(const void *ba, const void *bb)
537{
538 const struct bs *bsa = ba;
539 const struct bs *bsb = bb;
540
541 return bsb->nr - bsa->nr;
542}
543
8fc46b4d
JA
544static unsigned long o_to_kb_rate(struct btrace_out *o, int rw)
545{
546 uint64_t usec = (o->last_ttime[rw] - o->first_ttime[rw]) / 1000ULL;
547 uint64_t val;
548
549 if (!usec)
550 return 0;
551
552 val = o->kb[rw] * 1000ULL;
553 return val / (usec / 1000ULL);
554}
555
556static uint64_t o_first_ttime(struct btrace_out *o)
557{
558 uint64_t first;
559
560 first = min(o->first_ttime[0], o->first_ttime[1]);
561 return min(first, o->first_ttime[2]);
562}
563
564static uint64_t o_longest_ttime(struct btrace_out *o)
565{
566 uint64_t ret = 0;
567 int i;
568
569 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
570 uint64_t diff;
571
572 diff = o->last_ttime[i] - o->first_ttime[i];
573 ret = max(diff, ret);
574 }
575
576 return ret;
577}
578
40bafa33
JA
579static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
580{
581 const char *msg[] = { "reads", "writes", "trims" };
582 struct btrace_out *o = &p->o;
cea475cd 583 unsigned long total, usec;
40bafa33
JA
584 int i, j;
585
586 printf("[pid:\t%u]\n", p->pid);
587
588 total = ddir_rw_sum(o->ios);
589 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
590 float perc;
591
592 if (!o->ios[i])
593 continue;
594
595 ios[i] += o->ios[i] + o->merges[i];
596 printf("%s\n", msg[i]);
597 perc = ((float) o->ios[i] * 100.0) / (float) total;
598 printf("\tios: %lu (perc=%3.2f%%)\n", o->ios[i], perc);
599 perc = ((float) o->merges[i] * 100.0) / (float) total;
600 printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc);
601 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
761c2729 602 printf("\tseq: %lu (perc=%3.2f%%)\n", (unsigned long) o->seq[i], perc);
8fc46b4d 603 printf("\trate: %lu KB/sec\n", o_to_kb_rate(o, i));
40bafa33
JA
604
605 for (j = 0; j < o->nr_bs[i]; j++) {
606 struct bs *bs = &o->bs[i][j];
607
608 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
609 printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc);
610 }
611 }
612
613 printf("depth:\t%u\n", o->depth);
8fc46b4d 614 usec = o_longest_ttime(o) / 1000ULL;
cea475cd 615 printf("usec:\t%lu (delay=%llu)\n", usec, (unsigned long long) o->start_delay);
40bafa33
JA
616
617 printf("files:\t");
65ff9cd5
JA
618 for (i = 0; i < p->nr_files; i++)
619 printf("%s,", p->files[i].name);
40bafa33
JA
620 printf("\n");
621
622 printf("\n");
623}
624
625static int __output_p_fio(struct btrace_pid *p, unsigned long *ios)
626{
627 struct btrace_out *o = &p->o;
628 unsigned long total;
1a8cad44 629 unsigned long long time;
40bafa33
JA
630 float perc;
631 int i, j;
632
633 if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
634 log_err("fio: trace has both read/write and trim\n");
635 return 1;
636 }
8fc46b4d
JA
637 if (!p->nr_files) {
638 log_err("fio: no devices found\n");
639 return 1;
640 }
40bafa33
JA
641
642 printf("[pid%u]\n", p->pid);
643 printf("direct=1\n");
644 if (o->depth == 1)
645 printf("ioengine=sync\n");
646 else
647 printf("ioengine=libaio\niodepth=%u\n", o->depth);
648
649 if (o->ios[0] && !o->ios[1])
650 printf("rw=randread\n");
651 else if (!o->ios[0] && o->ios[1])
652 printf("rw=randwrite\n");
653 else if (o->ios[2])
654 printf("rw=randtrim\n");
655 else {
656 printf("rw=randrw\n");
657 total = ddir_rw_sum(o->ios);
658 perc = ((float) o->ios[0] * 100.0) / (float) total;
659 printf("rwmixread=%u\n", (int) (perc + 0.99));
660 }
661
35661615 662 printf("percentage_random=");
40bafa33
JA
663 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
664 if (o->seq[i] && o->ios[i]) {
665 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
666 if (perc >= 99.0)
667 perc = 100.0;
668 } else
669 perc = 100.0;
670
671 if (i)
672 printf(",");
35661615 673 perc = 100.0 - perc;
40bafa33
JA
674 printf("%u", (int) perc);
675 }
676 printf("\n");
677
678 printf("filename=");
65ff9cd5 679 for (i = 0; i < p->nr_files; i++) {
40bafa33
JA
680 if (i)
681 printf(":");
65ff9cd5 682 printf("%s", p->files[i].name);
40bafa33
JA
683 }
684 printf("\n");
685
686 printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
687
8fc46b4d 688 time = o_longest_ttime(o);
1a8cad44
JA
689 time = (time + 1000000000ULL - 1) / 1000000000ULL;
690 printf("runtime=%llus\n", time);
691
40bafa33
JA
692 printf("bssplit=");
693 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
694
695 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
696 printf(",");
697
698 for (j = 0; j < o->nr_bs[i]; j++) {
699 struct bs *bs = &o->bs[i][j];
700
701 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
702 if (perc < 1.00)
703 continue;
704 if (j)
705 printf(":");
706 if (j + 1 == o->nr_bs[i])
707 printf("%u/", bs->bs);
708 else
709 printf("%u/%u", bs->bs, (int) perc);
710 }
711 }
8fc46b4d 712 printf("\n");
40bafa33 713
8fc46b4d
JA
714 if (set_rate) {
715 printf("rate=");
716 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
717 unsigned long rate;
718
719 rate = o_to_kb_rate(o, i);
720 if (i)
721 printf(",");
722 if (rate)
723 printf("%luk", rate);
724 }
725 printf("\n");
726 }
727
728 printf("\n");
40bafa33
JA
729 return 0;
730}
731
732static int __output_p(struct btrace_pid *p, unsigned long *ios)
733{
734 struct btrace_out *o = &p->o;
735 int i, ret = 0;
736
737 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
738 if (o->nr_bs[i] <= 1)
739 continue;
740 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
741 }
742
35661615 743 if (filename) {
65ff9cd5
JA
744 p->files = malloc(sizeof(struct trace_file));
745 p->nr_files++;
746 p->files[0].name = filename;
35661615
JA
747 }
748
40bafa33
JA
749 if (output_ascii)
750 __output_p_ascii(p, ios);
751 else
752 ret = __output_p_fio(p, ios);
753
754 return ret;
755}
756
8fc46b4d
JA
757static void remove_ddir(struct btrace_out *o, int rw)
758{
759 o->ios[rw] = 0;
760}
761
40bafa33
JA
762static int prune_entry(struct btrace_out *o)
763{
8fc46b4d 764 unsigned long rate;
40bafa33 765 uint64_t time;
8fc46b4d 766 int i;
40bafa33
JA
767
768 if (ddir_rw_sum(o->ios) < ios_threshold)
769 return 1;
770
8fc46b4d 771 time = o_longest_ttime(o) / 1000ULL;
40bafa33
JA
772 if (time < rt_threshold)
773 return 1;
774
8fc46b4d
JA
775 rate = 0;
776 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
777 unsigned long this_rate;
778
779 this_rate = o_to_kb_rate(o, i);
780 if (this_rate < rate_threshold) {
781 remove_ddir(o, i);
782 this_rate = 0;
783 }
784 rate += this_rate;
785 }
786
787 if (rate < rate_threshold)
788 return 1;
789
40bafa33
JA
790 return 0;
791}
792
793static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
794{
795 struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list);
796 struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list);
797
798 return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
799}
800
17bf0853
JA
801static void free_p(struct btrace_pid *p)
802{
803 struct btrace_out *o = &p->o;
804 int i;
805
65ff9cd5
JA
806 for (i = 0; i < p->nr_files; i++) {
807 if (p->files[i].name && p->files[i].name != filename)
808 free(p->files[i].name);
17bf0853
JA
809 }
810
811 for (i = 0; i < DDIR_RWDIR_CNT; i++)
812 free(o->bs[i]);
813
65ff9cd5 814 free(p->files);
17bf0853
JA
815 flist_del(&p->pid_list);
816 flist_del(&p->hash_list);
817 free(p);
818}
819
40bafa33
JA
820static int output_p(void)
821{
822 unsigned long ios[DDIR_RWDIR_CNT];
823 struct flist_head *e, *tmp;
85fcdac8 824 int depth_disabled = 0;
40bafa33
JA
825 int ret = 0;
826
827 flist_for_each_safe(e, tmp, &pid_list) {
828 struct btrace_pid *p;
829
830 p = flist_entry(e, struct btrace_pid, pid_list);
831 if (prune_entry(&p->o)) {
17bf0853 832 free_p(p);
40bafa33
JA
833 continue;
834 }
8fc46b4d 835 p->o.start_delay = (o_first_ttime(&p->o) / 1000ULL) - first_ttime;
85fcdac8 836 depth_disabled += p->o.depth_disabled;
40bafa33
JA
837 }
838
85fcdac8
JA
839 if (depth_disabled)
840 log_err("fio: missing completion traces, depths capped at %u\n", max_depth);
841
40bafa33
JA
842 memset(ios, 0, sizeof(ios));
843
844 flist_sort(NULL, &pid_list, entry_cmp);
845
846 flist_for_each(e, &pid_list) {
847 struct btrace_pid *p;
848
849 p = flist_entry(e, struct btrace_pid, pid_list);
850 ret |= __output_p(p, ios);
8fc46b4d
JA
851 if (ret && !output_ascii)
852 break;
40bafa33
JA
853 }
854
855 if (output_ascii)
856 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
857
858 return ret;
859}
860
861static int usage(char *argv[])
862{
17bf0853
JA
863 log_err("%s: <blktrace bin file>\n", argv[0]);
864 log_err("\t-t\tUsec threshold to ignore task\n");
865 log_err("\t-n\tNumber IOS threshold to ignore task\n");
866 log_err("\t-f\tFio job file output\n");
867 log_err("\t-d\tUse this file/device for replay\n");
8fc46b4d
JA
868 log_err("\t-r\tIgnore jobs with less than this KB/sec rate\n");
869 log_err("\t-R\tSet rate in fio job\n");
85fcdac8 870 log_err("\t-D\tCap queue depth at this value (def=%u)\n", max_depth);
40bafa33
JA
871 return 1;
872}
873
17bf0853 874static int trace_needs_swap(const char *trace_file, int *swap)
40bafa33 875{
40bafa33 876 struct blk_io_trace t;
17bf0853
JA
877 int fd, ret;
878
879 *swap = -1;
880
881 fd = open(trace_file, O_RDONLY);
882 if (fd < 0) {
883 perror("open");
884 return 1;
885 }
886
887 ret = read(fd, &t, sizeof(t));
888 if (ret < 0) {
18be35e8 889 close(fd);
17bf0853
JA
890 perror("read");
891 return 1;
892 } else if (ret != sizeof(t)) {
18be35e8 893 close(fd);
17bf0853
JA
894 log_err("fio: short read on trace file\n");
895 return 1;
896 }
897
898 close(fd);
899
900 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
901 *swap = 0;
902 else {
903 /*
904 * Maybe it needs to be endian swapped...
905 */
906 t.magic = fio_swap32(t.magic);
907 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
908 *swap = 1;
909 }
910
911 if (*swap == -1) {
912 log_err("fio: blktrace appears corrupt\n");
913 return 1;
914 }
915
916 return 0;
917}
918
919int main(int argc, char *argv[])
920{
921 int need_swap, i, c;
40bafa33
JA
922
923 if (argc < 2)
924 return usage(argv);
925
85fcdac8 926 while ((c = getopt(argc, argv, "t:n:fd:r:RD:")) != -1) {
40bafa33 927 switch (c) {
8fc46b4d
JA
928 case 'R':
929 set_rate = 1;
930 break;
931 case 'r':
932 rate_threshold = atoi(optarg);
933 break;
40bafa33
JA
934 case 't':
935 rt_threshold = atoi(optarg);
936 break;
937 case 'n':
938 ios_threshold = atoi(optarg);
939 break;
940 case 'f':
941 output_ascii = 0;
942 break;
35661615
JA
943 case 'd':
944 filename = strdup(optarg);
945 break;
85fcdac8
JA
946 case 'D':
947 max_depth = atoi(optarg);
948 break;
40bafa33
JA
949 case '?':
950 default:
951 return usage(argv);
952 }
953 }
954
955 if (argc == optind)
956 return usage(argv);
957
17bf0853 958 if (trace_needs_swap(argv[optind], &need_swap))
40bafa33 959 return 1;
40bafa33
JA
960
961 for (i = 0; i < PID_HASH_SIZE; i++)
962 INIT_FLIST_HEAD(&pid_hash[i]);
17bf0853
JA
963 for (i = 0; i < INFLIGHT_HASH_SIZE; i++)
964 INIT_FLIST_HEAD(&inflight_hash[i]);
40bafa33
JA
965
966 load_blktrace(argv[optind], need_swap);
967 first_ttime /= 1000ULL;
968
969 return output_p();
970}