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