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