btrace2fio: create separate jobs for pid with both read/write and trim
[fio.git] / t / btrace2fio.c
CommitLineData
40bafa33 1#include <stdio.h>
40bafa33
JA
2#include <unistd.h>
3#include <inttypes.h>
ccfcf5bf 4#include <math.h>
40bafa33
JA
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"
3d2d14bc 14#include "../minmax.h"
984f30c9 15#include "../oslib/linux-dev-lookup.h"
40bafa33
JA
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];
4870138d 65 uint64_t kib[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) {
4870138d 409 i->p->o.kib[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
4870138d 559static unsigned long o_to_kib_rate(struct btrace_out *o, int rw)
8fc46b4d
JA
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
4870138d 571 val = o->kib[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);
4870138d 626 printf("\trate: %lu KiB/sec\n", o_to_kib_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
13eafd5b
SL
648static int __output_p_fio(struct btrace_pid *p, unsigned long *ios,
649 const char *name_postfix)
40bafa33
JA
650{
651 struct btrace_out *o = &p->o;
652 unsigned long total;
1a8cad44 653 unsigned long long time;
40bafa33
JA
654 float perc;
655 int i, j;
656
657 if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
13eafd5b
SL
658 unsigned long ios_bak[DDIR_RWDIR_CNT];
659
660 memcpy(ios_bak, o->ios, DDIR_RWDIR_CNT * sizeof(unsigned long));
661
662 /* create job for read/write */
663 o->ios[2] = 0;
664 __output_p_fio(p, ios, "");
665 o->ios[2] = ios_bak[2];
666
667 /* create job for trim */
668 o->ios[0] = 0;
669 o->ios[1] = 0;
670 __output_p_fio(p, ios, "_trim");
671 o->ios[0] = ios_bak[0];
672 o->ios[1] = ios_bak[1];
673
674 return 0;
40bafa33 675 }
8fc46b4d
JA
676 if (!p->nr_files) {
677 log_err("fio: no devices found\n");
678 return 1;
679 }
40bafa33 680
13eafd5b 681 printf("[pid%u%s", p->pid, name_postfix);
ccfcf5bf
JA
682 if (p->nr_merge_pids)
683 for (i = 0; i < p->nr_merge_pids; i++)
684 printf(",pid%u", p->merge_pids[i]);
685 printf("]\n");
686
687 printf("numjobs=%u\n", p->numjobs);
40bafa33
JA
688 printf("direct=1\n");
689 if (o->depth == 1)
690 printf("ioengine=sync\n");
691 else
692 printf("ioengine=libaio\niodepth=%u\n", o->depth);
693
694 if (o->ios[0] && !o->ios[1])
695 printf("rw=randread\n");
696 else if (!o->ios[0] && o->ios[1])
697 printf("rw=randwrite\n");
698 else if (o->ios[2])
699 printf("rw=randtrim\n");
700 else {
701 printf("rw=randrw\n");
702 total = ddir_rw_sum(o->ios);
703 perc = ((float) o->ios[0] * 100.0) / (float) total;
ccfcf5bf 704 printf("rwmixread=%u\n", (int) floor(perc + 0.50));
40bafa33
JA
705 }
706
35661615 707 printf("percentage_random=");
40bafa33
JA
708 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
709 if (o->seq[i] && o->ios[i]) {
710 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
711 if (perc >= 99.0)
712 perc = 100.0;
713 } else
714 perc = 100.0;
715
716 if (i)
717 printf(",");
35661615 718 perc = 100.0 - perc;
ccfcf5bf 719 printf("%u", (int) floor(perc + 0.5));
40bafa33
JA
720 }
721 printf("\n");
722
723 printf("filename=");
65ff9cd5 724 for (i = 0; i < p->nr_files; i++) {
40bafa33
JA
725 if (i)
726 printf(":");
65ff9cd5 727 printf("%s", p->files[i].name);
40bafa33
JA
728 }
729 printf("\n");
730
ccfcf5bf
JA
731 if (o->start_delay / 1000000ULL)
732 printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
40bafa33 733
8fc46b4d 734 time = o_longest_ttime(o);
1a8cad44
JA
735 time = (time + 1000000000ULL - 1) / 1000000000ULL;
736 printf("runtime=%llus\n", time);
737
40bafa33
JA
738 printf("bssplit=");
739 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
740
741 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
742 printf(",");
743
744 for (j = 0; j < o->nr_bs[i]; j++) {
745 struct bs *bs = &o->bs[i][j];
746
747 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
748 if (perc < 1.00)
749 continue;
750 if (j)
751 printf(":");
752 if (j + 1 == o->nr_bs[i])
753 printf("%u/", bs->bs);
754 else
ccfcf5bf 755 printf("%u/%u", bs->bs, (int) floor(perc + 0.5));
40bafa33
JA
756 }
757 }
8fc46b4d 758 printf("\n");
40bafa33 759
8fc46b4d
JA
760 if (set_rate) {
761 printf("rate=");
762 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
763 unsigned long rate;
764
4870138d 765 rate = o_to_kib_rate(o, i);
8fc46b4d
JA
766 if (i)
767 printf(",");
768 if (rate)
769 printf("%luk", rate);
770 }
771 printf("\n");
772 }
773
9ffad67a
JA
774 if (n_add_opts)
775 for (i = 0; i < n_add_opts; i++)
776 printf("%s\n", add_opts[i]);
777
8fc46b4d 778 printf("\n");
40bafa33
JA
779 return 0;
780}
781
782static int __output_p(struct btrace_pid *p, unsigned long *ios)
783{
784 struct btrace_out *o = &p->o;
785 int i, ret = 0;
786
787 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
788 if (o->nr_bs[i] <= 1)
789 continue;
790 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
791 }
792
35661615 793 if (filename) {
65ff9cd5
JA
794 p->files = malloc(sizeof(struct trace_file));
795 p->nr_files++;
796 p->files[0].name = filename;
35661615
JA
797 }
798
40bafa33
JA
799 if (output_ascii)
800 __output_p_ascii(p, ios);
801 else
13eafd5b 802 ret = __output_p_fio(p, ios, "");
40bafa33
JA
803
804 return ret;
805}
806
8fc46b4d
JA
807static void remove_ddir(struct btrace_out *o, int rw)
808{
809 o->ios[rw] = 0;
810}
811
40bafa33
JA
812static int prune_entry(struct btrace_out *o)
813{
8fc46b4d 814 unsigned long rate;
40bafa33 815 uint64_t time;
8fc46b4d 816 int i;
40bafa33
JA
817
818 if (ddir_rw_sum(o->ios) < ios_threshold)
819 return 1;
820
8fc46b4d 821 time = o_longest_ttime(o) / 1000ULL;
40bafa33
JA
822 if (time < rt_threshold)
823 return 1;
824
8fc46b4d
JA
825 rate = 0;
826 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
827 unsigned long this_rate;
828
4870138d 829 this_rate = o_to_kib_rate(o, i);
8fc46b4d
JA
830 if (this_rate < rate_threshold) {
831 remove_ddir(o, i);
832 this_rate = 0;
833 }
834 rate += this_rate;
835 }
836
837 if (rate < rate_threshold)
838 return 1;
839
40bafa33
JA
840 return 0;
841}
842
843static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
844{
845 struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list);
846 struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list);
847
848 return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
849}
850
17bf0853
JA
851static void free_p(struct btrace_pid *p)
852{
853 struct btrace_out *o = &p->o;
854 int i;
855
65ff9cd5
JA
856 for (i = 0; i < p->nr_files; i++) {
857 if (p->files[i].name && p->files[i].name != filename)
858 free(p->files[i].name);
17bf0853
JA
859 }
860
861 for (i = 0; i < DDIR_RWDIR_CNT; i++)
862 free(o->bs[i]);
863
65ff9cd5 864 free(p->files);
17bf0853
JA
865 flist_del(&p->pid_list);
866 flist_del(&p->hash_list);
867 free(p);
868}
869
ccfcf5bf
JA
870static int entries_close(struct btrace_pid *pida, struct btrace_pid *pidb)
871{
872 float perca, percb, fdiff;
873 int i, idiff;
874
875 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
876 if ((pida->o.ios[i] && !pidb->o.ios[i]) ||
877 (pidb->o.ios[i] && !pida->o.ios[i]))
878 return 0;
879 if (pida->o.ios[i] && pidb->o.ios[i]) {
880 perca = ((float) pida->o.seq[i] * 100.0) / (float) pida->o.ios[i];
881 percb = ((float) pidb->o.seq[i] * 100.0) / (float) pidb->o.ios[i];
882 fdiff = perca - percb;
883 if (fabs(fdiff) > random_diff)
884 return 0;
885 }
886
887 idiff = pida->o.depth - pidb->o.depth;
888 if (abs(idiff) > depth_diff)
889 return 0;
890 }
891
892 return 1;
893}
894
895static void merge_bs(struct bs **bsap, unsigned int *nr_bsap,
896 struct bs *bsb, unsigned int nr_bsb)
897{
898 struct bs *bsa = *bsap;
899 unsigned int nr_bsa = *nr_bsap;
900 int a, b;
901
902 for (b = 0; b < nr_bsb; b++) {
903 int next, found = 0;
904
905 for (a = 0; a < nr_bsa; a++) {
906 if (bsb[b].bs != bsa[a].bs)
907 continue;
908
909 bsa[a].nr += bsb[b].nr;
910 bsa[a].merges += bsb[b].merges;
911 found = 1;
912 break;
913 }
914
915 if (found)
916 continue;
917
918 next = *nr_bsap;
919 bsa = realloc(bsa, (next + 1) * sizeof(struct bs));
920 bsa[next].bs = bsb[b].bs;
921 bsa[next].nr = bsb[b].nr;
922 (*nr_bsap)++;
923 *bsap = bsa;
924 }
925}
926
927static int merge_entries(struct btrace_pid *pida, struct btrace_pid *pidb)
928{
929 int i;
930
931 if (!entries_close(pida, pidb))
932 return 0;
933
934 pida->nr_merge_pids++;
935 pida->merge_pids = realloc(pida->merge_pids, pida->nr_merge_pids * sizeof(pid_t));
936 pida->merge_pids[pida->nr_merge_pids - 1] = pidb->pid;
937
938 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
939 struct btrace_out *oa = &pida->o;
940 struct btrace_out *ob = &pidb->o;
941
942 oa->ios[i] += ob->ios[i];
943 oa->merges[i] += ob->merges[i];
944 oa->seq[i] += ob->seq[i];
4870138d 945 oa->kib[i] += ob->kib[i];
ccfcf5bf
JA
946 oa->first_ttime[i] = min(oa->first_ttime[i], ob->first_ttime[i]);
947 oa->last_ttime[i] = max(oa->last_ttime[i], ob->last_ttime[i]);
948 merge_bs(&oa->bs[i], &oa->nr_bs[i], ob->bs[i], ob->nr_bs[i]);
949 }
950
951 pida->o.start_delay = min(pida->o.start_delay, pidb->o.start_delay);
952 pida->o.depth = (pida->o.depth + pidb->o.depth) / 2;
953 return 1;
954}
955
8a68c41c 956static void check_merges(struct btrace_pid *p, struct flist_head *pidlist)
ccfcf5bf
JA
957{
958 struct flist_head *e, *tmp;
959
960 if (p->ignore)
961 return;
962
8a68c41c 963 flist_for_each_safe(e, tmp, pidlist) {
ccfcf5bf
JA
964 struct btrace_pid *pidb;
965
966 pidb = flist_entry(e, struct btrace_pid, pid_list);
967 if (pidb == p)
968 continue;
969
970 if (merge_entries(p, pidb)) {
971 pidb->ignore = 1;
972 p->numjobs++;
973 }
974 }
975}
976
40bafa33
JA
977static int output_p(void)
978{
979 unsigned long ios[DDIR_RWDIR_CNT];
980 struct flist_head *e, *tmp;
85fcdac8 981 int depth_disabled = 0;
40bafa33
JA
982 int ret = 0;
983
984 flist_for_each_safe(e, tmp, &pid_list) {
985 struct btrace_pid *p;
986
987 p = flist_entry(e, struct btrace_pid, pid_list);
988 if (prune_entry(&p->o)) {
17bf0853 989 free_p(p);
40bafa33
JA
990 continue;
991 }
8fc46b4d 992 p->o.start_delay = (o_first_ttime(&p->o) / 1000ULL) - first_ttime;
85fcdac8 993 depth_disabled += p->o.depth_disabled;
40bafa33
JA
994 }
995
ccfcf5bf
JA
996 if (collapse_entries) {
997 struct btrace_pid *p;
998
999 flist_for_each_safe(e, tmp, &pid_list) {
1000 p = flist_entry(e, struct btrace_pid, pid_list);
1001 check_merges(p, &pid_list);
1002 }
1003
1004 flist_for_each_safe(e, tmp, &pid_list) {
1005 p = flist_entry(e, struct btrace_pid, pid_list);
1006 if (p->ignore)
1007 free_p(p);
1008 }
1009 }
1010
85fcdac8
JA
1011 if (depth_disabled)
1012 log_err("fio: missing completion traces, depths capped at %u\n", max_depth);
1013
40bafa33
JA
1014 memset(ios, 0, sizeof(ios));
1015
1016 flist_sort(NULL, &pid_list, entry_cmp);
1017
1018 flist_for_each(e, &pid_list) {
1019 struct btrace_pid *p;
1020
1021 p = flist_entry(e, struct btrace_pid, pid_list);
1022 ret |= __output_p(p, ios);
8fc46b4d
JA
1023 if (ret && !output_ascii)
1024 break;
40bafa33
JA
1025 }
1026
1027 if (output_ascii)
1028 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
1029
1030 return ret;
1031}
1032
1033static int usage(char *argv[])
1034{
ccfcf5bf 1035 log_err("%s: [options] <blktrace bin file>\n", argv[0]);
17bf0853
JA
1036 log_err("\t-t\tUsec threshold to ignore task\n");
1037 log_err("\t-n\tNumber IOS threshold to ignore task\n");
1038 log_err("\t-f\tFio job file output\n");
1039 log_err("\t-d\tUse this file/device for replay\n");
4870138d 1040 log_err("\t-r\tIgnore jobs with less than this KiB/sec rate\n");
ccfcf5bf 1041 log_err("\t-R\tSet rate in fio job (def=%u)\n", set_rate);
85fcdac8 1042 log_err("\t-D\tCap queue depth at this value (def=%u)\n", max_depth);
ccfcf5bf
JA
1043 log_err("\t-c\tCollapse \"identical\" jobs (def=%u)\n", collapse_entries);
1044 log_err("\t-u\tDepth difference for collapse (def=%u)\n", depth_diff);
1045 log_err("\t-x\tRandom difference for collapse (def=%u)\n", random_diff);
9ffad67a 1046 log_err("\t-a\tAdditional fio option to add to job file\n");
40bafa33
JA
1047 return 1;
1048}
1049
17bf0853 1050static int trace_needs_swap(const char *trace_file, int *swap)
40bafa33 1051{
40bafa33 1052 struct blk_io_trace t;
17bf0853
JA
1053 int fd, ret;
1054
1055 *swap = -1;
13eafd5b 1056
17bf0853
JA
1057 fd = open(trace_file, O_RDONLY);
1058 if (fd < 0) {
1059 perror("open");
1060 return 1;
1061 }
1062
1063 ret = read(fd, &t, sizeof(t));
1064 if (ret < 0) {
18be35e8 1065 close(fd);
17bf0853
JA
1066 perror("read");
1067 return 1;
1068 } else if (ret != sizeof(t)) {
18be35e8 1069 close(fd);
17bf0853
JA
1070 log_err("fio: short read on trace file\n");
1071 return 1;
1072 }
1073
1074 close(fd);
1075
1076 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1077 *swap = 0;
1078 else {
1079 /*
1080 * Maybe it needs to be endian swapped...
1081 */
1082 t.magic = fio_swap32(t.magic);
1083 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1084 *swap = 1;
1085 }
1086
1087 if (*swap == -1) {
1088 log_err("fio: blktrace appears corrupt\n");
1089 return 1;
1090 }
1091
1092 return 0;
1093}
1094
1095int main(int argc, char *argv[])
1096{
1097 int need_swap, i, c;
40bafa33
JA
1098
1099 if (argc < 2)
1100 return usage(argv);
1101
9ffad67a 1102 while ((c = getopt(argc, argv, "t:n:fd:r:RD:c:u:x:a:")) != -1) {
40bafa33 1103 switch (c) {
8fc46b4d
JA
1104 case 'R':
1105 set_rate = 1;
1106 break;
1107 case 'r':
1108 rate_threshold = atoi(optarg);
1109 break;
40bafa33
JA
1110 case 't':
1111 rt_threshold = atoi(optarg);
1112 break;
1113 case 'n':
1114 ios_threshold = atoi(optarg);
1115 break;
1116 case 'f':
1117 output_ascii = 0;
1118 break;
35661615
JA
1119 case 'd':
1120 filename = strdup(optarg);
1121 break;
85fcdac8
JA
1122 case 'D':
1123 max_depth = atoi(optarg);
1124 break;
ccfcf5bf
JA
1125 case 'c':
1126 collapse_entries = atoi(optarg);
1127 break;
1128 case 'u':
1129 depth_diff = atoi(optarg);
1130 break;
1131 case 'x':
1132 random_diff = atoi(optarg);
1133 break;
9ffad67a
JA
1134 case 'a':
1135 add_opts = realloc(add_opts, (n_add_opts + 1) * sizeof(char *));
1136 add_opts[n_add_opts] = strdup(optarg);
1137 n_add_opts++;
1138 break;
40bafa33
JA
1139 case '?':
1140 default:
1141 return usage(argv);
1142 }
1143 }
1144
1145 if (argc == optind)
1146 return usage(argv);
1147
17bf0853 1148 if (trace_needs_swap(argv[optind], &need_swap))
40bafa33 1149 return 1;
40bafa33
JA
1150
1151 for (i = 0; i < PID_HASH_SIZE; i++)
1152 INIT_FLIST_HEAD(&pid_hash[i]);
17bf0853
JA
1153 for (i = 0; i < INFLIGHT_HASH_SIZE; i++)
1154 INIT_FLIST_HEAD(&inflight_hash[i]);
40bafa33
JA
1155
1156 load_blktrace(argv[optind], need_swap);
1157 first_ttime /= 1000ULL;
1158
1159 return output_p();
1160}