fio: add libpmem engine
[fio.git] / blktrace.c
CommitLineData
fb7b71a3
JA
1/*
2 * blktrace support code for fio
3 */
4#include <stdio.h>
5#include <stdlib.h>
5e6c2067 6#include <sys/stat.h>
26e616d0
JA
7#include <sys/ioctl.h>
8#include <linux/fs.h>
5e6c2067 9#include <dirent.h>
8c1fdf04 10
01743ee1 11#include "flist.h"
fb7b71a3 12#include "fio.h"
a3e59412 13#include "blktrace.h"
fb7b71a3 14#include "blktrace_api.h"
984f30c9 15#include "oslib/linux-dev-lookup.h"
fb7b71a3 16
2da7df1d 17#define TRACE_FIFO_SIZE 8192
e2887563
JA
18
19/*
20 * fifo refill frontend, to avoid reading data in trace sized bites
21 */
22static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
23{
24 char buf[TRACE_FIFO_SIZE];
f12b323f 25 unsigned int total;
e2887563
JA
26 int ret;
27
f12b323f
JA
28 total = sizeof(buf);
29 if (total > fifo_room(fifo))
30 total = fifo_room(fifo);
e2887563 31
f12b323f
JA
32 ret = read(fd, buf, total);
33 if (ret < 0) {
34 td_verror(td, errno, "read blktrace file");
35 return -1;
e2887563
JA
36 }
37
f12b323f
JA
38 if (ret > 0)
39 ret = fifo_put(fifo, buf, ret);
40
bd6f78b2 41 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
f12b323f 42 return ret;
e2887563
JA
43}
44
45/*
46 * Retrieve 'len' bytes from the fifo, refilling if necessary.
47 */
48static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
49 void *buf, unsigned int len)
50{
f12b323f
JA
51 if (fifo_len(fifo) < len) {
52 int ret = refill_fifo(td, fifo, fd);
e2887563 53
f12b323f
JA
54 if (ret < 0)
55 return ret;
56 }
e2887563
JA
57
58 return fifo_get(fifo, buf, len);
59}
60
8c1fdf04
JA
61/*
62 * Just discard the pdu by seeking past it.
63 */
f12b323f
JA
64static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
65 struct blk_io_trace *t)
fb7b71a3
JA
66{
67 if (t->pdu_len == 0)
68 return 0;
69
bd6f78b2 70 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
f12b323f 71 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
fb7b71a3
JA
72}
73
8c1fdf04
JA
74/*
75 * Check if this is a blktrace binary data file. We read a single trace
76 * into memory and check for the magic signature.
77 */
d95b34a6 78int is_blktrace(const char *filename, int *need_swap)
fb7b71a3
JA
79{
80 struct blk_io_trace t;
81 int fd, ret;
82
83 fd = open(filename, O_RDONLY);
4dced407 84 if (fd < 0)
fb7b71a3 85 return 0;
fb7b71a3
JA
86
87 ret = read(fd, &t, sizeof(t));
88 close(fd);
89
90 if (ret < 0) {
91 perror("read blktrace");
92 return 0;
93 } else if (ret != sizeof(t)) {
94 log_err("fio: short read on blktrace file\n");
95 return 0;
96 }
97
d95b34a6
JA
98 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
99 *need_swap = 0;
fb7b71a3 100 return 1;
d95b34a6
JA
101 }
102
103 /*
104 * Maybe it needs to be endian swapped...
105 */
106 t.magic = fio_swap32(t.magic);
107 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
108 *need_swap = 1;
109 return 1;
110 }
fb7b71a3
JA
111
112 return 0;
113}
114
c69aa91f
JA
115#define FMINORBITS 20
116#define FMINORMASK ((1U << FMINORBITS) - 1)
117#define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
118#define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
eeb9c2aa 119
89ac1d48 120static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
691c8fb0
JA
121{
122 struct io_piece *ipo;
123
124 ipo = calloc(1, sizeof(*ipo));
0d29de83 125 init_ipo(ipo);
691c8fb0
JA
126
127 ipo->ddir = DDIR_INVAL;
128 ipo->fileno = fileno;
89ac1d48 129 ipo->file_action = action;
01743ee1 130 flist_add_tail(&ipo->list, &td->io_log_list);
691c8fb0
JA
131}
132
26e616d0 133static int get_dev_blocksize(const char *dev, unsigned int *bs)
5e6c2067 134{
26e616d0
JA
135 int fd;
136
137 fd = open(dev, O_RDONLY);
138 if (fd < 0)
139 return 1;
140
141 if (ioctl(fd, BLKSSZGET, bs) < 0) {
142 close(fd);
143 return 1;
144 }
145
146 close(fd);
147 return 0;
148}
149
150static int trace_add_file(struct thread_data *td, __u32 device,
151 unsigned int *bs)
152{
153 static unsigned int last_maj, last_min, last_fileno, last_bs;
c69aa91f
JA
154 unsigned int maj = FMAJOR(device);
155 unsigned int min = FMINOR(device);
5e6c2067 156 struct fio_file *f;
5e6c2067 157 unsigned int i;
26e616d0 158 char dev[256];
5e6c2067 159
26e616d0
JA
160 if (last_maj == maj && last_min == min) {
161 *bs = last_bs;
89ac1d48 162 return last_fileno;
26e616d0 163 }
5e6c2067
JA
164
165 last_maj = maj;
166 last_min = min;
167
168 /*
169 * check for this file in our list
170 */
26e616d0 171 for_each_file(td, f, i) {
89ac1d48
SL
172 if (f->major == maj && f->minor == min) {
173 last_fileno = f->fileno;
26e616d0
JA
174 last_bs = f->bs;
175 goto out;
89ac1d48 176 }
26e616d0 177 }
5e6c2067
JA
178
179 strcpy(dev, "/dev");
40bafa33 180 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
26e616d0 181 unsigned int this_bs;
691c8fb0
JA
182 int fileno;
183
40bafa33
JA
184 if (td->o.replay_redirect)
185 dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
186 " with: %s\n", maj, min,
187 td->o.replay_redirect);
188 else
189 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
190
bd6f78b2 191 dprint(FD_BLKTRACE, "add devices %s\n", dev);
49ffb4a2 192 fileno = add_file_exclusive(td, dev);
26e616d0
JA
193
194 if (get_dev_blocksize(dev, &this_bs))
195 this_bs = 512;
196
b53f2c54 197 td->o.open_files++;
5903e7b7
JA
198 td->files[fileno]->major = maj;
199 td->files[fileno]->minor = min;
26e616d0 200 td->files[fileno]->bs = this_bs;
89ac1d48 201 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
26e616d0 202
89ac1d48 203 last_fileno = fileno;
26e616d0 204 last_bs = this_bs;
bd6f78b2 205 }
f01b34ae 206
26e616d0
JA
207out:
208 *bs = last_bs;
89ac1d48 209 return last_fileno;
5e6c2067
JA
210}
211
0c63576e
JA
212static void t_bytes_align(struct thread_options *o, struct blk_io_trace *t)
213{
214 if (!o->replay_align)
215 return;
216
217 t->bytes = (t->bytes + o->replay_align - 1) & ~(o->replay_align - 1);
218}
219
8c1fdf04
JA
220/*
221 * Store blk_io_trace data in an ipo for later retrieval.
222 */
fdefd987 223static void store_ipo(struct thread_data *td, unsigned long long offset,
89ac1d48 224 unsigned int bytes, int rw, unsigned long long ttime,
26e616d0 225 int fileno, unsigned int bs)
fdefd987
JA
226{
227 struct io_piece *ipo = malloc(sizeof(*ipo));
228
0d29de83
JA
229 init_ipo(ipo);
230
26e616d0 231 ipo->offset = offset * bs;
0c63576e
JA
232 if (td->o.replay_scale)
233 ipo->offset = ipo->offset / td->o.replay_scale;
a79f17bf 234 ipo_bytes_align(td->o.replay_align, ipo);
fdefd987 235 ipo->len = bytes;
8c1fdf04 236 ipo->delay = ttime / 1000;
fdefd987
JA
237 if (rw)
238 ipo->ddir = DDIR_WRITE;
239 else
240 ipo->ddir = DDIR_READ;
89ac1d48 241 ipo->fileno = fileno;
fdefd987 242
bd6f78b2
JA
243 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
244 ipo->ddir, ipo->offset,
245 ipo->len, ipo->delay);
691c8fb0 246 queue_io_piece(td, ipo);
fdefd987
JA
247}
248
0b9d69ec 249static void handle_trace_notify(struct blk_io_trace *t)
cd991b9e 250{
691c8fb0
JA
251 switch (t->action) {
252 case BLK_TN_PROCESS:
24653680 253 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
d95b34a6 254 t->action, t->pid);
691c8fb0
JA
255 break;
256 case BLK_TN_TIMESTAMP:
24653680 257 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
d95b34a6 258 t->action, t->pid);
691c8fb0 259 break;
ff58fced
JA
260 case BLK_TN_MESSAGE:
261 break;
691c8fb0
JA
262 default:
263 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
264 break;
265 }
266}
5b3023b8 267
24653680
JA
268static void handle_trace_discard(struct thread_data *td,
269 struct blk_io_trace *t,
270 unsigned long long ttime,
26e616d0 271 unsigned long *ios, unsigned int *rw_bs)
ff58fced
JA
272{
273 struct io_piece *ipo = malloc(sizeof(*ipo));
26e616d0 274 unsigned int bs;
89ac1d48 275 int fileno;
ff58fced 276
0d29de83 277 init_ipo(ipo);
26e616d0 278 fileno = trace_add_file(td, t->device, &bs);
ff58fced 279
24653680 280 ios[DDIR_TRIM]++;
26e616d0
JA
281 if (t->bytes > rw_bs[DDIR_TRIM])
282 rw_bs[DDIR_TRIM] = t->bytes;
24653680 283
ff58fced
JA
284 td->o.size += t->bytes;
285
286 memset(ipo, 0, sizeof(*ipo));
287 INIT_FLIST_HEAD(&ipo->list);
288
26e616d0 289 ipo->offset = t->sector * bs;
0c63576e
JA
290 if (td->o.replay_scale)
291 ipo->offset = ipo->offset / td->o.replay_scale;
a79f17bf 292 ipo_bytes_align(td->o.replay_align, ipo);
ff58fced
JA
293 ipo->len = t->bytes;
294 ipo->delay = ttime / 1000;
295 ipo->ddir = DDIR_TRIM;
89ac1d48 296 ipo->fileno = fileno;
ff58fced
JA
297
298 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
299 ipo->offset, ipo->len,
300 ipo->delay);
301 queue_io_piece(td, ipo);
302}
303
691c8fb0
JA
304static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
305 unsigned long long ttime, unsigned long *ios,
26e616d0 306 unsigned int *rw_bs)
691c8fb0 307{
26e616d0 308 unsigned int bs;
691c8fb0 309 int rw;
89ac1d48 310 int fileno;
5b3023b8 311
26e616d0 312 fileno = trace_add_file(td, t->device, &bs);
5b3023b8
JA
313
314 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
315
26e616d0
JA
316 if (t->bytes > rw_bs[rw])
317 rw_bs[rw] = t->bytes;
5b3023b8
JA
318
319 ios[rw]++;
320 td->o.size += t->bytes;
26e616d0 321 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno, bs);
cd991b9e
JA
322}
323
691c8fb0
JA
324/*
325 * We only care for queue traces, most of the others are side effects
326 * due to internal workings of the block layer.
327 */
328static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
24653680 329 unsigned long *ios, unsigned int *bs)
691c8fb0 330{
24653680 331 static unsigned long long last_ttime;
0c63576e 332 unsigned long long delay = 0;
24653680 333
691c8fb0
JA
334 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
335 return;
24653680
JA
336
337 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
338 if (!last_ttime || td->o.no_stall) {
339 last_ttime = t->time;
340 delay = 0;
341 } else {
342 delay = t->time - last_ttime;
343 last_ttime = t->time;
344 }
345 }
691c8fb0 346
0c63576e
JA
347 t_bytes_align(&td->o, t);
348
691c8fb0 349 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
aec2de20 350 handle_trace_notify(t);
ff58fced 351 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
24653680 352 handle_trace_discard(td, t, delay, ios, bs);
691c8fb0 353 else
24653680 354 handle_trace_fs(td, t, delay, ios, bs);
691c8fb0
JA
355}
356
d95b34a6
JA
357static void byteswap_trace(struct blk_io_trace *t)
358{
359 t->magic = fio_swap32(t->magic);
360 t->sequence = fio_swap32(t->sequence);
361 t->time = fio_swap64(t->time);
362 t->sector = fio_swap64(t->sector);
363 t->bytes = fio_swap32(t->bytes);
364 t->action = fio_swap32(t->action);
365 t->pid = fio_swap32(t->pid);
366 t->device = fio_swap32(t->device);
367 t->cpu = fio_swap32(t->cpu);
368 t->error = fio_swap16(t->error);
369 t->pdu_len = fio_swap16(t->pdu_len);
370}
371
24653680
JA
372static int t_is_write(struct blk_io_trace *t)
373{
374 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
375}
376
a6eaf6c9
JA
377static enum fio_ddir t_get_ddir(struct blk_io_trace *t)
378{
379 if (t->action & BLK_TC_ACT(BLK_TC_READ))
380 return DDIR_READ;
381 else if (t->action & BLK_TC_ACT(BLK_TC_WRITE))
382 return DDIR_WRITE;
383 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
384 return DDIR_TRIM;
385
386 return DDIR_INVAL;
387}
388
389static void depth_inc(struct blk_io_trace *t, int *depth)
390{
391 enum fio_ddir ddir;
392
393 ddir = t_get_ddir(t);
394 if (ddir != DDIR_INVAL)
395 depth[ddir]++;
396}
397
398static void depth_dec(struct blk_io_trace *t, int *depth)
399{
400 enum fio_ddir ddir;
401
402 ddir = t_get_ddir(t);
403 if (ddir != DDIR_INVAL)
404 depth[ddir]--;
405}
406
407static void depth_end(struct blk_io_trace *t, int *this_depth, int *depth)
408{
409 enum fio_ddir ddir = DDIR_INVAL;
410
411 ddir = t_get_ddir(t);
412 if (ddir != DDIR_INVAL) {
413 depth[ddir] = max(depth[ddir], this_depth[ddir]);
414 this_depth[ddir] = 0;
415 }
416}
417
8c1fdf04
JA
418/*
419 * Load a blktrace file by reading all the blk_io_trace entries, and storing
420 * them as io_pieces like the fio text version would do.
421 */
d95b34a6 422int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
fb7b71a3
JA
423{
424 struct blk_io_trace t;
24653680
JA
425 unsigned long ios[DDIR_RWDIR_CNT], skipped_writes;
426 unsigned int rw_bs[DDIR_RWDIR_CNT];
e2887563 427 struct fifo *fifo;
5903e7b7 428 int fd, i, old_state;
89ac1d48 429 struct fio_file *f;
a6eaf6c9 430 int this_depth[DDIR_RWDIR_CNT], depth[DDIR_RWDIR_CNT], max_depth;
fb7b71a3
JA
431
432 fd = open(filename, O_RDONLY);
433 if (fd < 0) {
434 td_verror(td, errno, "open blktrace file");
435 return 1;
436 }
437
e2887563
JA
438 fifo = fifo_alloc(TRACE_FIFO_SIZE);
439
8edd973d 440 old_state = td_bump_runstate(td, TD_SETTING_UP);
5903e7b7 441
6df8adaa
JA
442 td->o.size = 0;
443
a6eaf6c9
JA
444 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
445 ios[i] = 0;
446 rw_bs[i] = 0;
447 this_depth[i] = 0;
448 depth[i] = 0;
449 }
450
4241ea8f 451 skipped_writes = 0;
fb7b71a3 452 do {
e2887563 453 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
fb7b71a3 454
e2887563 455 if (ret < 0)
8c1fdf04 456 goto err;
e2887563
JA
457 else if (!ret)
458 break;
459 else if (ret < (int) sizeof(t)) {
460 log_err("fio: short fifo get\n");
fb7b71a3 461 break;
fb7b71a3
JA
462 }
463
d95b34a6
JA
464 if (need_swap)
465 byteswap_trace(&t);
466
fb7b71a3 467 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
5ec10eaa
JA
468 log_err("fio: bad magic in blktrace data: %x\n",
469 t.magic);
8c1fdf04 470 goto err;
fb7b71a3
JA
471 }
472 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
5ec10eaa
JA
473 log_err("fio: bad blktrace version %d\n",
474 t.magic & 0xff);
8c1fdf04 475 goto err;
fb7b71a3 476 }
f12b323f
JA
477 ret = discard_pdu(td, fifo, fd, &t);
478 if (ret < 0) {
fb7b71a3 479 td_verror(td, ret, "blktrace lseek");
8c1fdf04 480 goto err;
f12b323f
JA
481 } else if (t.pdu_len != ret) {
482 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
483 goto err;
fb7b71a3 484 }
691c8fb0 485 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
eb5fdcf1 486 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
a6eaf6c9
JA
487 depth_inc(&t, this_depth);
488 else if (((t.action & 0xffff) == __BLK_TA_BACKMERGE) ||
489 ((t.action & 0xffff) == __BLK_TA_FRONTMERGE))
490 depth_dec(&t, this_depth);
491 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE)
492 depth_end(&t, this_depth, depth);
691c8fb0 493
24653680 494 if (t_is_write(&t) && read_only) {
691c8fb0 495 skipped_writes++;
24653680 496 continue;
64bbb865 497 }
a6edd638 498 }
24653680
JA
499
500 handle_trace(td, &t, ios, rw_bs);
fb7b71a3
JA
501 } while (1);
502
3643a1f2 503 for_each_file(td, f, i)
89ac1d48 504 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
89ac1d48 505
38470f85 506 fifo_free(fifo);
fb7b71a3 507 close(fd);
8c1fdf04 508
8edd973d 509 td_restore_runstate(td, old_state);
5903e7b7 510
f01b34ae
JA
511 if (!td->files_index) {
512 log_err("fio: did not find replay device(s)\n");
513 return 1;
514 }
515
eb5fdcf1
JA
516 /*
517 * For stacked devices, we don't always get a COMPLETE event so
518 * the depth grows to insane values. Limit it to something sane(r).
519 */
a6eaf6c9
JA
520 max_depth = 0;
521 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
522 if (depth[i] > 1024)
523 depth[i] = 1024;
524 else if (!depth[i] && ios[i])
525 depth[i] = 1;
526 max_depth = max(depth[i], max_depth);
527 }
eb5fdcf1 528
4241ea8f 529 if (skipped_writes)
5ec10eaa
JA
530 log_err("fio: %s skips replay of %lu writes due to read-only\n",
531 td->o.name, skipped_writes);
4241ea8f 532
8c1fdf04
JA
533 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
534 log_err("fio: found no ios in blktrace data\n");
535 return 1;
24653680 536 } else if (ios[DDIR_READ] && !ios[DDIR_WRITE]) {
8c1fdf04 537 td->o.td_ddir = TD_DDIR_READ;
d84f8d49
JA
538 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
539 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
8c1fdf04 540 td->o.td_ddir = TD_DDIR_WRITE;
d84f8d49
JA
541 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
542 } else {
8c1fdf04 543 td->o.td_ddir = TD_DDIR_RW;
d84f8d49
JA
544 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
545 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
24653680 546 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
d84f8d49 547 }
8c1fdf04
JA
548
549 /*
550 * We need to do direct/raw ios to the device, to avoid getting
76a00ec6
JA
551 * read-ahead in our way. But only do so if the minimum block size
552 * is a multiple of 4k, otherwise we don't know if it's safe to do so.
8c1fdf04 553 */
76a00ec6 554 if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
a6eaf6c9 555 td->o.odirect = 1;
8c1fdf04 556
eb5fdcf1 557 /*
8a16f59b 558 * If depth wasn't manually set, use probed depth
eb5fdcf1 559 */
8a16f59b 560 if (!fio_option_is_set(&td->o, iodepth))
a6eaf6c9 561 td->o.iodepth = td->o.iodepth_low = max_depth;
eb5fdcf1 562
fb7b71a3 563 return 0;
8c1fdf04
JA
564err:
565 close(fd);
38470f85 566 fifo_free(fifo);
8c1fdf04 567 return 1;
fb7b71a3 568}