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