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