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