configure: add support for --prefix
[fio.git] / blktrace.c
... / ...
CommitLineData
1/*
2 * blktrace support code for fio
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <sys/stat.h>
7#include <dirent.h>
8
9#include "flist.h"
10#include "fio.h"
11#include "blktrace_api.h"
12#include "lib/linux-dev-lookup.h"
13
14#define TRACE_FIFO_SIZE 8192
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];
22 unsigned int total;
23 int ret;
24
25 total = sizeof(buf);
26 if (total > fifo_room(fifo))
27 total = fifo_room(fifo);
28
29 ret = read(fd, buf, total);
30 if (ret < 0) {
31 td_verror(td, errno, "read blktrace file");
32 return -1;
33 }
34
35 if (ret > 0)
36 ret = fifo_put(fifo, buf, ret);
37
38 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
39 return ret;
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{
48 if (fifo_len(fifo) < len) {
49 int ret = refill_fifo(td, fifo, fd);
50
51 if (ret < 0)
52 return ret;
53 }
54
55 return fifo_get(fifo, buf, len);
56}
57
58/*
59 * Just discard the pdu by seeking past it.
60 */
61static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
62 struct blk_io_trace *t)
63{
64 if (t->pdu_len == 0)
65 return 0;
66
67 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
68 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
69}
70
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 */
75int is_blktrace(const char *filename, int *need_swap)
76{
77 struct blk_io_trace t;
78 int fd, ret;
79
80 fd = open(filename, O_RDONLY);
81 if (fd < 0)
82 return 0;
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
95 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
96 *need_swap = 0;
97 return 1;
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 }
108
109 return 0;
110}
111
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))
116
117static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
118{
119 struct io_piece *ipo;
120
121 ipo = calloc(1, sizeof(*ipo));
122 init_ipo(ipo);
123
124 ipo->ddir = DDIR_INVAL;
125 ipo->fileno = fileno;
126 ipo->file_action = action;
127 flist_add_tail(&ipo->list, &td->io_log_list);
128}
129
130static int trace_add_file(struct thread_data *td, __u32 device)
131{
132 static unsigned int last_maj, last_min, last_fileno;
133 unsigned int maj = FMAJOR(device);
134 unsigned int min = FMINOR(device);
135 struct fio_file *f;
136 char dev[256];
137 unsigned int i;
138
139 if (last_maj == maj && last_min == min)
140 return last_fileno;
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)
149 if (f->major == maj && f->minor == min) {
150 last_fileno = f->fileno;
151 return last_fileno;
152 }
153
154 strcpy(dev, "/dev");
155 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
156 int fileno;
157
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
165 dprint(FD_BLKTRACE, "add devices %s\n", dev);
166 fileno = add_file_exclusive(td, dev);
167 td->o.open_files++;
168 td->files[fileno]->major = maj;
169 td->files[fileno]->minor = min;
170 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
171 last_fileno = fileno;
172 }
173
174 return last_fileno;
175}
176
177/*
178 * Store blk_io_trace data in an ipo for later retrieval.
179 */
180static void store_ipo(struct thread_data *td, unsigned long long offset,
181 unsigned int bytes, int rw, unsigned long long ttime,
182 int fileno)
183{
184 struct io_piece *ipo = malloc(sizeof(*ipo));
185
186 init_ipo(ipo);
187
188 /*
189 * the 512 is wrong here, it should be the hardware sector size...
190 */
191 ipo->offset = offset * 512;
192 ipo->len = bytes;
193 ipo->delay = ttime / 1000;
194 if (rw)
195 ipo->ddir = DDIR_WRITE;
196 else
197 ipo->ddir = DDIR_READ;
198 ipo->fileno = fileno;
199
200 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
201 ipo->ddir, ipo->offset,
202 ipo->len, ipo->delay);
203 queue_io_piece(td, ipo);
204}
205
206static void handle_trace_notify(struct blk_io_trace *t)
207{
208 switch (t->action) {
209 case BLK_TN_PROCESS:
210 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
211 t->action, t->pid);
212 break;
213 case BLK_TN_TIMESTAMP:
214 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
215 t->action, t->pid);
216 break;
217 case BLK_TN_MESSAGE:
218 break;
219 default:
220 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
221 break;
222 }
223}
224
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)
229{
230 struct io_piece *ipo = malloc(sizeof(*ipo));
231 int fileno;
232
233 init_ipo(ipo);
234 fileno = trace_add_file(td, t->device);
235
236 ios[DDIR_TRIM]++;
237 if (t->bytes > bs[DDIR_TRIM])
238 bs[DDIR_TRIM] = t->bytes;
239
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;
252 ipo->fileno = fileno;
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
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;
265 int fileno;
266
267 fileno = trace_add_file(td, t->device);
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;
276 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
277}
278
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,
284 unsigned long *ios, unsigned int *bs)
285{
286 static unsigned long long last_ttime;
287 unsigned long long delay;
288
289 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
290 return;
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 }
301
302 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
303 handle_trace_notify(t);
304 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
305 handle_trace_discard(td, t, delay, ios, bs);
306 else
307 handle_trace_fs(td, t, delay, ios, bs);
308}
309
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
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
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
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 */
375int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
376{
377 struct blk_io_trace t;
378 unsigned long ios[DDIR_RWDIR_CNT], skipped_writes;
379 unsigned int rw_bs[DDIR_RWDIR_CNT];
380 struct fifo *fifo;
381 int fd, i, old_state;
382 struct fio_file *f;
383 int this_depth[DDIR_RWDIR_CNT], depth[DDIR_RWDIR_CNT], max_depth;
384
385 fd = open(filename, O_RDONLY);
386 if (fd < 0) {
387 td_verror(td, errno, "open blktrace file");
388 return 1;
389 }
390
391 fifo = fifo_alloc(TRACE_FIFO_SIZE);
392
393 old_state = td_bump_runstate(td, TD_SETTING_UP);
394
395 td->o.size = 0;
396
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
404 skipped_writes = 0;
405 do {
406 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
407
408 if (ret < 0)
409 goto err;
410 else if (!ret)
411 break;
412 else if (ret < (int) sizeof(t)) {
413 log_err("fio: short fifo get\n");
414 break;
415 }
416
417 if (need_swap)
418 byteswap_trace(&t);
419
420 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
421 log_err("fio: bad magic in blktrace data: %x\n",
422 t.magic);
423 goto err;
424 }
425 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
426 log_err("fio: bad blktrace version %d\n",
427 t.magic & 0xff);
428 goto err;
429 }
430 ret = discard_pdu(td, fifo, fd, &t);
431 if (ret < 0) {
432 td_verror(td, ret, "blktrace lseek");
433 goto err;
434 } else if (t.pdu_len != ret) {
435 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
436 goto err;
437 }
438 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
439 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
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);
446
447 if (t_is_write(&t) && read_only) {
448 skipped_writes++;
449 continue;
450 }
451 }
452
453 handle_trace(td, &t, ios, rw_bs);
454 } while (1);
455
456 for (i = 0; i < td->files_index; i++) {
457 f = td->files[i];
458 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
459 }
460
461 fifo_free(fifo);
462 close(fd);
463
464 td_restore_runstate(td, old_state);
465
466 if (!td->files_index) {
467 log_err("fio: did not find replay device(s)\n");
468 return 1;
469 }
470
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 */
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 }
483
484 if (skipped_writes)
485 log_err("fio: %s skips replay of %lu writes due to read-only\n",
486 td->o.name, skipped_writes);
487
488 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
489 log_err("fio: found no ios in blktrace data\n");
490 return 1;
491 } else if (ios[DDIR_READ] && !ios[DDIR_WRITE]) {
492 td->o.td_ddir = TD_DDIR_READ;
493 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
494 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
495 td->o.td_ddir = TD_DDIR_WRITE;
496 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
497 } else {
498 td->o.td_ddir = TD_DDIR_RW;
499 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
500 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
501 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
502 }
503
504 /*
505 * We need to do direct/raw ios to the device, to avoid getting
506 * read-ahead in our way. But only do so if the minimum block size
507 * is a multiple of 4k, otherwise we don't know if it's safe to do so.
508 */
509 if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
510 td->o.odirect = 1;
511
512 /*
513 * If depth wasn't manually set, use probed depth
514 */
515 if (!fio_option_is_set(&td->o, iodepth))
516 td->o.iodepth = td->o.iodepth_low = max_depth;
517
518 return 0;
519err:
520 close(fd);
521 fifo_free(fifo);
522 return 1;
523}