blob: 32dda92e858be105a4fc641c961600901f8c499c [file] [log] [blame]
Jens Axboe42a80e32014-09-16 17:06:52 +02001#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;
Jens Axboe183f3922014-09-18 17:23:55 +020020static unsigned int rate_threshold;
21static unsigned int set_rate;
Jens Axboe42a80e32014-09-16 17:06:52 +020022static int output_ascii = 1;
Jens Axboeb064a142014-09-16 17:40:03 +020023static char *filename;
Jens Axboe42a80e32014-09-16 17:06:52 +020024
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];
Jens Axboe42a80e32014-09-16 17:06:52 +020038 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;
Jens Axboe183f3922014-09-18 17:23:55 +020048 uint64_t first_ttime[DDIR_RWDIR_CNT];
49 uint64_t last_ttime[DDIR_RWDIR_CNT];
50 uint64_t kb[DDIR_RWDIR_CNT];
Jens Axboe42a80e32014-09-16 17:06:52 +020051
Jens Axboe42a80e32014-09-16 17:06:52 +020052 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;
Jens Axboed8943e12014-09-16 20:17:55 -060059
60 struct trace_file *files;
61 int nr_files;
62 unsigned int last_major, last_minor;
63
Jens Axboe42a80e32014-09-16 17:06:52 +020064 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
Jens Axboe50331622014-09-16 18:28:42 +020079#define INFLIGHT_HASH_BITS 8
80#define INFLIGHT_HASH_SIZE (1U << INFLIGHT_HASH_BITS)
81static struct flist_head inflight_hash[INFLIGHT_HASH_SIZE];
Jens Axboe42a80e32014-09-16 17:06:52 +020082
83static uint64_t first_ttime = -1ULL;
84
85static struct inflight *inflight_find(uint64_t sector)
86{
Jens Axboe50331622014-09-16 18:28:42 +020087 struct flist_head *inflight_list;
Jens Axboe42a80e32014-09-16 17:06:52 +020088 struct flist_head *e;
89
Jens Axboe50331622014-09-16 18:28:42 +020090 inflight_list = &inflight_hash[hash_long(sector, INFLIGHT_HASH_BITS)];
91
92 flist_for_each(e, inflight_list) {
Jens Axboe42a80e32014-09-16 17:06:52 +020093 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
Jens Axboe50331622014-09-16 18:28:42 +0200112static void __inflight_add(struct inflight *i)
Jens Axboe42a80e32014-09-16 17:06:52 +0200113{
Jens Axboe50331622014-09-16 18:28:42 +0200114 struct flist_head *list;
115
116 list = &inflight_hash[hash_long(i->end_sector, INFLIGHT_HASH_BITS)];
117 flist_add_tail(&i->list, list);
Jens Axboe42a80e32014-09-16 17:06:52 +0200118}
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);
Jens Axboe50331622014-09-16 18:28:42 +0200130 __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 }
Jens Axboe42a80e32014-09-16 17:06:52 +0200141}
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
Jens Axboe183f3922014-09-18 17:23:55 +0200195static int handle_trace_notify(struct blk_io_trace *t)
Jens Axboe42a80e32014-09-16 17:06:52 +0200196{
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:
Jens Axboe50331622014-09-16 18:28:42 +0200207 log_err("unknown trace act %x\n", t->action);
Jens Axboe183f3922014-09-18 17:23:55 +0200208 return 1;
Jens Axboe42a80e32014-09-16 17:06:52 +0200209 }
Jens Axboe183f3922014-09-18 17:23:55 +0200210
211 return 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200212}
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
Jens Axboe183f3922014-09-18 17:23:55 +0200247static int btrace_add_file(struct btrace_pid *p, uint32_t devno)
Jens Axboe42a80e32014-09-16 17:06:52 +0200248{
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
Jens Axboeb064a142014-09-16 17:40:03 +0200255 if (filename)
Jens Axboe183f3922014-09-18 17:23:55 +0200256 return 0;
Jens Axboed8943e12014-09-16 20:17:55 -0600257 if (p->last_major == maj && p->last_minor == min)
Jens Axboe183f3922014-09-18 17:23:55 +0200258 return 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200259
Jens Axboed8943e12014-09-16 20:17:55 -0600260 p->last_major = maj;
261 p->last_minor = min;
Jens Axboe42a80e32014-09-16 17:06:52 +0200262
263 /*
264 * check for this file in our list
265 */
Jens Axboed8943e12014-09-16 20:17:55 -0600266 for (i = 0; i < p->nr_files; i++) {
267 f = &p->files[i];
Jens Axboe42a80e32014-09-16 17:06:52 +0200268
269 if (f->major == maj && f->minor == min)
Jens Axboe183f3922014-09-18 17:23:55 +0200270 return 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200271 }
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);
Jens Axboe183f3922014-09-18 17:23:55 +0200276 if (!output_ascii) {
277 log_err("fio: use -d to specify device\n");
278 return 1;
279 }
280 return 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200281 }
282
Jens Axboed8943e12014-09-16 20:17:55 -0600283 p->files = realloc(p->files, (p->nr_files + 1) * sizeof(*f));
284 f = &p->files[p->nr_files];
Jens Axboe42a80e32014-09-16 17:06:52 +0200285 f->name = strdup(dev);
286 f->major = maj;
287 f->minor = min;
Jens Axboed8943e12014-09-16 20:17:55 -0600288 p->nr_files++;
Jens Axboe183f3922014-09-18 17:23:55 +0200289 return 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200290}
291
Jens Axboe183f3922014-09-18 17:23:55 +0200292static 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;
298}
299
300static int handle_trace_discard(struct blk_io_trace *t, struct btrace_pid *p)
Jens Axboe42a80e32014-09-16 17:06:52 +0200301{
Jens Axboed8943e12014-09-16 20:17:55 -0600302 struct btrace_out *o = &p->o;
303
Jens Axboe183f3922014-09-18 17:23:55 +0200304 if (btrace_add_file(p, t->device))
305 return 1;
Jens Axboe42a80e32014-09-16 17:06:52 +0200306
Jens Axboe183f3922014-09-18 17:23:55 +0200307 if (o->first_ttime[2] == -1ULL)
308 o->first_ttime[2] = t->time;
Jens Axboe42a80e32014-09-16 17:06:52 +0200309
310 o->ios[DDIR_TRIM]++;
311 add_bs(o, t->bytes, DDIR_TRIM);
Jens Axboe183f3922014-09-18 17:23:55 +0200312 return 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200313}
314
Jens Axboe183f3922014-09-18 17:23:55 +0200315static int handle_trace_fs(struct blk_io_trace *t, struct btrace_pid *p)
Jens Axboe42a80e32014-09-16 17:06:52 +0200316{
Jens Axboed8943e12014-09-16 20:17:55 -0600317 struct btrace_out *o = &p->o;
Jens Axboe42a80e32014-09-16 17:06:52 +0200318 int rw;
319
Jens Axboe183f3922014-09-18 17:23:55 +0200320 if (btrace_add_file(p, t->device))
321 return 1;
Jens Axboe42a80e32014-09-16 17:06:52 +0200322
323 first_ttime = min(first_ttime, (uint64_t) t->time);
324
Jens Axboe42a80e32014-09-16 17:06:52 +0200325 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
326
Jens Axboe183f3922014-09-18 17:23:55 +0200327 if (o->first_ttime[rw] == -1ULL)
328 o->first_ttime[rw] = t->time;
329
Jens Axboe42a80e32014-09-16 17:06:52 +0200330 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);
Jens Axboe183f3922014-09-18 17:23:55 +0200337 return 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200338}
339
Jens Axboe183f3922014-09-18 17:23:55 +0200340static int handle_queue_trace(struct blk_io_trace *t, struct btrace_pid *p)
Jens Axboe42a80e32014-09-16 17:06:52 +0200341{
342 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
Jens Axboe183f3922014-09-18 17:23:55 +0200343 return handle_trace_notify(t);
Jens Axboe42a80e32014-09-16 17:06:52 +0200344 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
Jens Axboe183f3922014-09-18 17:23:55 +0200345 return handle_trace_discard(t, p);
Jens Axboe42a80e32014-09-16 17:06:52 +0200346 else
Jens Axboe183f3922014-09-18 17:23:55 +0200347 return handle_trace_fs(t, p);
Jens Axboe42a80e32014-09-16 17:06:52 +0200348}
349
Jens Axboe183f3922014-09-18 17:23:55 +0200350static int handle_trace(struct blk_io_trace *t, struct btrace_pid *p)
Jens Axboe42a80e32014-09-16 17:06:52 +0200351{
352 unsigned int act = t->action & 0xffff;
Jens Axboe183f3922014-09-18 17:23:55 +0200353 int ret = 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200354
355 if (act == __BLK_TA_QUEUE) {
356 inflight_add(p, t->sector, t->bytes);
Jens Axboe183f3922014-09-18 17:23:55 +0200357 ret = handle_queue_trace(t, p);
Jens Axboe42a80e32014-09-16 17:06:52 +0200358 } 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);
Jens Axboe183f3922014-09-18 17:23:55 +0200368 if (i)
369 inflight_merge(i, t_to_rwdir(t), t->bytes);
Jens Axboe42a80e32014-09-16 17:06:52 +0200370 } 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);
Jens Axboe183f3922014-09-18 17:23:55 +0200378 if (i)
379 inflight_merge(i, t_to_rwdir(t), 0);
Jens Axboe42a80e32014-09-16 17:06:52 +0200380 } else if (act == __BLK_TA_COMPLETE) {
381 struct inflight *i;
382
383 i = inflight_find(t->sector + (t->bytes >> 9));
Jens Axboe047623b2014-09-17 17:58:31 +0200384 if (i) {
Jens Axboe183f3922014-09-18 17:23:55 +0200385 i->p->o.kb[t_to_rwdir(t)] += (t->bytes >> 10);
Jens Axboe42a80e32014-09-16 17:06:52 +0200386 inflight_remove(i);
Jens Axboe047623b2014-09-17 17:58:31 +0200387 }
Jens Axboe42a80e32014-09-16 17:06:52 +0200388 }
Jens Axboe183f3922014-09-18 17:23:55 +0200389
390 return ret;
Jens Axboe42a80e32014-09-16 17:06:52 +0200391}
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));
Jens Axboe42a80e32014-09-16 17:06:52 +0200434
Jens Axboe183f3922014-09-18 17:23:55 +0200435 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
436 p->o.first_ttime[i] = -1ULL;
437 p->o.last_ttime[i] = -1ULL;
Jens Axboe42a80e32014-09-16 17:06:52 +0200438 p->o.last_end[i] = -1ULL;
Jens Axboe183f3922014-09-18 17:23:55 +0200439 }
Jens Axboe42a80e32014-09-16 17:06:52 +0200440
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 */
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700453static int load_blktrace(const char *fname, int need_swap)
Jens Axboe42a80e32014-09-16 17:06:52 +0200454{
455 struct btrace_pid *p;
456 unsigned long traces;
457 struct blk_io_trace t;
458 struct fifo *fifo;
Jens Axboe183f3922014-09-18 17:23:55 +0200459 int fd, ret = 0;
Jens Axboe42a80e32014-09-16 17:06:52 +0200460
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700461 fd = open(fname, O_RDONLY);
Jens Axboe42a80e32014-09-16 17:06:52 +0200462 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 {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700471 ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
Jens Axboe42a80e32014-09-16 17:06:52 +0200472 if (ret < 0)
473 goto err;
474 else if (!ret)
475 break;
476 else if (ret < (int) sizeof(t)) {
Jens Axboe50331622014-09-16 18:28:42 +0200477 log_err("fio: short fifo get\n");
Jens Axboe42a80e32014-09-16 17:06:52 +0200478 break;
479 }
480
481 if (need_swap)
482 byteswap_trace(&t);
483
484 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
Jens Axboe50331622014-09-16 18:28:42 +0200485 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
Jens Axboe42a80e32014-09-16 17:06:52 +0200486 goto err;
487 }
488 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
Jens Axboe50331622014-09-16 18:28:42 +0200489 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
Jens Axboe42a80e32014-09-16 17:06:52 +0200490 goto err;
491 }
492 ret = discard_pdu(fifo, fd, &t);
493 if (ret < 0) {
Jens Axboe50331622014-09-16 18:28:42 +0200494 log_err("blktrace lseek\n");
Jens Axboe42a80e32014-09-16 17:06:52 +0200495 goto err;
496 } else if (t.pdu_len != ret) {
Jens Axboe50331622014-09-16 18:28:42 +0200497 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
Jens Axboe42a80e32014-09-16 17:06:52 +0200498 goto err;
499 }
500
501 p = pid_hash_get(t.pid);
Jens Axboe183f3922014-09-18 17:23:55 +0200502 ret = handle_trace(&t, p);
503 if (ret)
504 break;
505 p->o.last_ttime[t_to_rwdir(&t)] = t.time;
Jens Axboe42a80e32014-09-16 17:06:52 +0200506 traces++;
507 } while (1);
508
509 fifo_free(fifo);
510 close(fd);
511
Jens Axboe183f3922014-09-18 17:23:55 +0200512 if (ret)
513 return ret;
514
Jens Axboe42a80e32014-09-16 17:06:52 +0200515 if (output_ascii)
516 printf("Traces loaded: %lu\n", traces);
517
518 return 0;
519err:
520 close(fd);
521 fifo_free(fifo);
522 return 1;
523}
524
525static int bs_cmp(const void *ba, const void *bb)
526{
527 const struct bs *bsa = ba;
528 const struct bs *bsb = bb;
529
530 return bsb->nr - bsa->nr;
531}
532
Jens Axboe183f3922014-09-18 17:23:55 +0200533static unsigned long o_to_kb_rate(struct btrace_out *o, int rw)
534{
535 uint64_t usec = (o->last_ttime[rw] - o->first_ttime[rw]) / 1000ULL;
536 uint64_t val;
537
538 if (!usec)
539 return 0;
540
541 val = o->kb[rw] * 1000ULL;
542 return val / (usec / 1000ULL);
543}
544
545static uint64_t o_first_ttime(struct btrace_out *o)
546{
547 uint64_t first;
548
549 first = min(o->first_ttime[0], o->first_ttime[1]);
550 return min(first, o->first_ttime[2]);
551}
552
553static uint64_t o_longest_ttime(struct btrace_out *o)
554{
555 uint64_t ret = 0;
556 int i;
557
558 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
559 uint64_t diff;
560
561 diff = o->last_ttime[i] - o->first_ttime[i];
562 ret = max(diff, ret);
563 }
564
565 return ret;
566}
567
Jens Axboe42a80e32014-09-16 17:06:52 +0200568static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
569{
570 const char *msg[] = { "reads", "writes", "trims" };
571 struct btrace_out *o = &p->o;
Jens Axboe047623b2014-09-17 17:58:31 +0200572 unsigned long total, usec;
Jens Axboe42a80e32014-09-16 17:06:52 +0200573 int i, j;
574
575 printf("[pid:\t%u]\n", p->pid);
576
577 total = ddir_rw_sum(o->ios);
578 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
579 float perc;
580
581 if (!o->ios[i])
582 continue;
583
584 ios[i] += o->ios[i] + o->merges[i];
585 printf("%s\n", msg[i]);
586 perc = ((float) o->ios[i] * 100.0) / (float) total;
587 printf("\tios: %lu (perc=%3.2f%%)\n", o->ios[i], perc);
588 perc = ((float) o->merges[i] * 100.0) / (float) total;
589 printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc);
590 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
Jens Axboeb2a657f2014-09-23 16:06:04 -0600591 printf("\tseq: %lu (perc=%3.2f%%)\n", (unsigned long) o->seq[i], perc);
Jens Axboe183f3922014-09-18 17:23:55 +0200592 printf("\trate: %lu KB/sec\n", o_to_kb_rate(o, i));
Jens Axboe42a80e32014-09-16 17:06:52 +0200593
594 for (j = 0; j < o->nr_bs[i]; j++) {
595 struct bs *bs = &o->bs[i][j];
596
597 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
598 printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc);
599 }
600 }
601
602 printf("depth:\t%u\n", o->depth);
Jens Axboe183f3922014-09-18 17:23:55 +0200603 usec = o_longest_ttime(o) / 1000ULL;
Jens Axboe047623b2014-09-17 17:58:31 +0200604 printf("usec:\t%lu (delay=%llu)\n", usec, (unsigned long long) o->start_delay);
Jens Axboe42a80e32014-09-16 17:06:52 +0200605
606 printf("files:\t");
Jens Axboed8943e12014-09-16 20:17:55 -0600607 for (i = 0; i < p->nr_files; i++)
608 printf("%s,", p->files[i].name);
Jens Axboe42a80e32014-09-16 17:06:52 +0200609 printf("\n");
610
611 printf("\n");
612}
613
614static int __output_p_fio(struct btrace_pid *p, unsigned long *ios)
615{
616 struct btrace_out *o = &p->o;
617 unsigned long total;
Jens Axboecdc4d672014-09-16 17:35:55 +0200618 unsigned long long time;
Jens Axboe42a80e32014-09-16 17:06:52 +0200619 float perc;
620 int i, j;
621
622 if ((o->ios[0] + o->ios[1]) && o->ios[2]) {
623 log_err("fio: trace has both read/write and trim\n");
624 return 1;
625 }
Jens Axboe183f3922014-09-18 17:23:55 +0200626 if (!p->nr_files) {
627 log_err("fio: no devices found\n");
628 return 1;
629 }
Jens Axboe42a80e32014-09-16 17:06:52 +0200630
631 printf("[pid%u]\n", p->pid);
632 printf("direct=1\n");
633 if (o->depth == 1)
634 printf("ioengine=sync\n");
635 else
636 printf("ioengine=libaio\niodepth=%u\n", o->depth);
637
638 if (o->ios[0] && !o->ios[1])
639 printf("rw=randread\n");
640 else if (!o->ios[0] && o->ios[1])
641 printf("rw=randwrite\n");
642 else if (o->ios[2])
643 printf("rw=randtrim\n");
644 else {
645 printf("rw=randrw\n");
646 total = ddir_rw_sum(o->ios);
647 perc = ((float) o->ios[0] * 100.0) / (float) total;
648 printf("rwmixread=%u\n", (int) (perc + 0.99));
649 }
650
Jens Axboeb064a142014-09-16 17:40:03 +0200651 printf("percentage_random=");
Jens Axboe42a80e32014-09-16 17:06:52 +0200652 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
653 if (o->seq[i] && o->ios[i]) {
654 perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
655 if (perc >= 99.0)
656 perc = 100.0;
657 } else
658 perc = 100.0;
659
660 if (i)
661 printf(",");
Jens Axboeb064a142014-09-16 17:40:03 +0200662 perc = 100.0 - perc;
Jens Axboe42a80e32014-09-16 17:06:52 +0200663 printf("%u", (int) perc);
664 }
665 printf("\n");
666
667 printf("filename=");
Jens Axboed8943e12014-09-16 20:17:55 -0600668 for (i = 0; i < p->nr_files; i++) {
Jens Axboe42a80e32014-09-16 17:06:52 +0200669 if (i)
670 printf(":");
Jens Axboed8943e12014-09-16 20:17:55 -0600671 printf("%s", p->files[i].name);
Jens Axboe42a80e32014-09-16 17:06:52 +0200672 }
673 printf("\n");
674
675 printf("startdelay=%llus\n", o->start_delay / 1000000ULL);
676
Jens Axboe183f3922014-09-18 17:23:55 +0200677 time = o_longest_ttime(o);
Jens Axboecdc4d672014-09-16 17:35:55 +0200678 time = (time + 1000000000ULL - 1) / 1000000000ULL;
679 printf("runtime=%llus\n", time);
680
Jens Axboe42a80e32014-09-16 17:06:52 +0200681 printf("bssplit=");
682 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
683
684 if (i && o->nr_bs[i - 1] && o->nr_bs[i])
685 printf(",");
686
687 for (j = 0; j < o->nr_bs[i]; j++) {
688 struct bs *bs = &o->bs[i][j];
689
690 perc = (((float) bs->nr * 100.0) / (float) o->ios[i]);
691 if (perc < 1.00)
692 continue;
693 if (j)
694 printf(":");
695 if (j + 1 == o->nr_bs[i])
696 printf("%u/", bs->bs);
697 else
698 printf("%u/%u", bs->bs, (int) perc);
699 }
700 }
Jens Axboe183f3922014-09-18 17:23:55 +0200701 printf("\n");
Jens Axboe42a80e32014-09-16 17:06:52 +0200702
Jens Axboe183f3922014-09-18 17:23:55 +0200703 if (set_rate) {
704 printf("rate=");
705 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
706 unsigned long rate;
707
708 rate = o_to_kb_rate(o, i);
709 if (i)
710 printf(",");
711 if (rate)
712 printf("%luk", rate);
713 }
714 printf("\n");
715 }
716
717 printf("\n");
Jens Axboe42a80e32014-09-16 17:06:52 +0200718 return 0;
719}
720
721static int __output_p(struct btrace_pid *p, unsigned long *ios)
722{
723 struct btrace_out *o = &p->o;
724 int i, ret = 0;
725
726 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
727 if (o->nr_bs[i] <= 1)
728 continue;
729 qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp);
730 }
731
Jens Axboeb064a142014-09-16 17:40:03 +0200732 if (filename) {
Jens Axboed8943e12014-09-16 20:17:55 -0600733 p->files = malloc(sizeof(struct trace_file));
734 p->nr_files++;
735 p->files[0].name = filename;
Jens Axboeb064a142014-09-16 17:40:03 +0200736 }
737
Jens Axboe42a80e32014-09-16 17:06:52 +0200738 if (output_ascii)
739 __output_p_ascii(p, ios);
740 else
741 ret = __output_p_fio(p, ios);
742
743 return ret;
744}
745
Jens Axboe183f3922014-09-18 17:23:55 +0200746static void remove_ddir(struct btrace_out *o, int rw)
747{
748 o->ios[rw] = 0;
749}
750
Jens Axboe42a80e32014-09-16 17:06:52 +0200751static int prune_entry(struct btrace_out *o)
752{
Jens Axboe183f3922014-09-18 17:23:55 +0200753 unsigned long rate;
Jens Axboe42a80e32014-09-16 17:06:52 +0200754 uint64_t time;
Jens Axboe183f3922014-09-18 17:23:55 +0200755 int i;
Jens Axboe42a80e32014-09-16 17:06:52 +0200756
757 if (ddir_rw_sum(o->ios) < ios_threshold)
758 return 1;
759
Jens Axboe183f3922014-09-18 17:23:55 +0200760 time = o_longest_ttime(o) / 1000ULL;
Jens Axboe42a80e32014-09-16 17:06:52 +0200761 if (time < rt_threshold)
762 return 1;
763
Jens Axboe183f3922014-09-18 17:23:55 +0200764 rate = 0;
765 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
766 unsigned long this_rate;
767
768 this_rate = o_to_kb_rate(o, i);
769 if (this_rate < rate_threshold) {
770 remove_ddir(o, i);
771 this_rate = 0;
772 }
773 rate += this_rate;
774 }
775
776 if (rate < rate_threshold)
777 return 1;
778
Jens Axboe42a80e32014-09-16 17:06:52 +0200779 return 0;
780}
781
782static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b)
783{
784 struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list);
785 struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list);
786
787 return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios);
788}
789
Jens Axboe50331622014-09-16 18:28:42 +0200790static void free_p(struct btrace_pid *p)
791{
792 struct btrace_out *o = &p->o;
793 int i;
794
Jens Axboed8943e12014-09-16 20:17:55 -0600795 for (i = 0; i < p->nr_files; i++) {
796 if (p->files[i].name && p->files[i].name != filename)
797 free(p->files[i].name);
Jens Axboe50331622014-09-16 18:28:42 +0200798 }
799
800 for (i = 0; i < DDIR_RWDIR_CNT; i++)
801 free(o->bs[i]);
802
Jens Axboed8943e12014-09-16 20:17:55 -0600803 free(p->files);
Jens Axboe50331622014-09-16 18:28:42 +0200804 flist_del(&p->pid_list);
805 flist_del(&p->hash_list);
806 free(p);
807}
808
Jens Axboe42a80e32014-09-16 17:06:52 +0200809static int output_p(void)
810{
811 unsigned long ios[DDIR_RWDIR_CNT];
812 struct flist_head *e, *tmp;
813 int ret = 0;
814
815 flist_for_each_safe(e, tmp, &pid_list) {
816 struct btrace_pid *p;
817
818 p = flist_entry(e, struct btrace_pid, pid_list);
819 if (prune_entry(&p->o)) {
Jens Axboe50331622014-09-16 18:28:42 +0200820 free_p(p);
Jens Axboe42a80e32014-09-16 17:06:52 +0200821 continue;
822 }
Jens Axboe183f3922014-09-18 17:23:55 +0200823 p->o.start_delay = (o_first_ttime(&p->o) / 1000ULL) - first_ttime;
Jens Axboe42a80e32014-09-16 17:06:52 +0200824 }
825
826 memset(ios, 0, sizeof(ios));
827
828 flist_sort(NULL, &pid_list, entry_cmp);
829
830 flist_for_each(e, &pid_list) {
831 struct btrace_pid *p;
832
833 p = flist_entry(e, struct btrace_pid, pid_list);
834 ret |= __output_p(p, ios);
Jens Axboe183f3922014-09-18 17:23:55 +0200835 if (ret && !output_ascii)
836 break;
Jens Axboe42a80e32014-09-16 17:06:52 +0200837 }
838
839 if (output_ascii)
840 printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]);
841
842 return ret;
843}
844
845static int usage(char *argv[])
846{
Jens Axboe50331622014-09-16 18:28:42 +0200847 log_err("%s: <blktrace bin file>\n", argv[0]);
848 log_err("\t-t\tUsec threshold to ignore task\n");
849 log_err("\t-n\tNumber IOS threshold to ignore task\n");
850 log_err("\t-f\tFio job file output\n");
851 log_err("\t-d\tUse this file/device for replay\n");
Jens Axboe183f3922014-09-18 17:23:55 +0200852 log_err("\t-r\tIgnore jobs with less than this KB/sec rate\n");
853 log_err("\t-R\tSet rate in fio job\n");
Jens Axboe42a80e32014-09-16 17:06:52 +0200854 return 1;
855}
856
Jens Axboe50331622014-09-16 18:28:42 +0200857static int trace_needs_swap(const char *trace_file, int *swap)
858{
859 struct blk_io_trace t;
860 int fd, ret;
861
862 *swap = -1;
863
864 fd = open(trace_file, O_RDONLY);
865 if (fd < 0) {
866 perror("open");
867 return 1;
868 }
869
870 ret = read(fd, &t, sizeof(t));
871 if (ret < 0) {
Jens Axboe31da12c2014-10-14 19:55:18 -0600872 close(fd);
Jens Axboe50331622014-09-16 18:28:42 +0200873 perror("read");
874 return 1;
875 } else if (ret != sizeof(t)) {
Jens Axboe31da12c2014-10-14 19:55:18 -0600876 close(fd);
Jens Axboe50331622014-09-16 18:28:42 +0200877 log_err("fio: short read on trace file\n");
878 return 1;
879 }
880
881 close(fd);
882
883 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
884 *swap = 0;
885 else {
886 /*
887 * Maybe it needs to be endian swapped...
888 */
889 t.magic = fio_swap32(t.magic);
890 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
891 *swap = 1;
892 }
893
894 if (*swap == -1) {
895 log_err("fio: blktrace appears corrupt\n");
896 return 1;
897 }
898
899 return 0;
900}
901
Jens Axboe42a80e32014-09-16 17:06:52 +0200902int main(int argc, char *argv[])
903{
Jens Axboe50331622014-09-16 18:28:42 +0200904 int need_swap, i, c;
Jens Axboe42a80e32014-09-16 17:06:52 +0200905
906 if (argc < 2)
907 return usage(argv);
908
Jens Axboe183f3922014-09-18 17:23:55 +0200909 while ((c = getopt(argc, argv, "t:n:fd:r:R")) != -1) {
Jens Axboe42a80e32014-09-16 17:06:52 +0200910 switch (c) {
Jens Axboe183f3922014-09-18 17:23:55 +0200911 case 'R':
912 set_rate = 1;
913 break;
914 case 'r':
915 rate_threshold = atoi(optarg);
916 break;
Jens Axboe42a80e32014-09-16 17:06:52 +0200917 case 't':
918 rt_threshold = atoi(optarg);
919 break;
920 case 'n':
921 ios_threshold = atoi(optarg);
922 break;
923 case 'f':
924 output_ascii = 0;
925 break;
Jens Axboeb064a142014-09-16 17:40:03 +0200926 case 'd':
927 filename = strdup(optarg);
928 break;
Jens Axboe42a80e32014-09-16 17:06:52 +0200929 case '?':
930 default:
931 return usage(argv);
932 }
933 }
934
935 if (argc == optind)
936 return usage(argv);
937
Jens Axboe50331622014-09-16 18:28:42 +0200938 if (trace_needs_swap(argv[optind], &need_swap))
Jens Axboe42a80e32014-09-16 17:06:52 +0200939 return 1;
Jens Axboe42a80e32014-09-16 17:06:52 +0200940
941 for (i = 0; i < PID_HASH_SIZE; i++)
942 INIT_FLIST_HEAD(&pid_hash[i]);
Jens Axboe50331622014-09-16 18:28:42 +0200943 for (i = 0; i < INFLIGHT_HASH_SIZE; i++)
944 INIT_FLIST_HEAD(&inflight_hash[i]);
Jens Axboe42a80e32014-09-16 17:06:52 +0200945
946 load_blktrace(argv[optind], need_swap);
947 first_ttime /= 1000ULL;
948
949 return output_p();
950}