Commit | Line | Data |
---|---|---|
fb7b71a3 JA |
1 | /* |
2 | * blktrace support code for fio | |
3 | */ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
87a48ada | 6 | #include <unistd.h> |
5ab088aa | 7 | #include <errno.h> |
169b7ce1 | 8 | #include <sys/sysmacros.h> |
8c1fdf04 | 9 | |
01743ee1 | 10 | #include "flist.h" |
fb7b71a3 | 11 | #include "fio.h" |
10f74940 | 12 | #include "iolog.h" |
a3e59412 | 13 | #include "blktrace.h" |
fb7b71a3 | 14 | #include "blktrace_api.h" |
984f30c9 | 15 | #include "oslib/linux-dev-lookup.h" |
fb7b71a3 | 16 | |
661592d4 LS |
17 | struct file_cache { |
18 | unsigned int maj; | |
19 | unsigned int min; | |
20 | unsigned int fileno; | |
21 | }; | |
22 | ||
8c1fdf04 JA |
23 | /* |
24 | * Just discard the pdu by seeking past it. | |
25 | */ | |
5ab088aa | 26 | static int discard_pdu(FILE* f, struct blk_io_trace *t) |
fb7b71a3 JA |
27 | { |
28 | if (t->pdu_len == 0) | |
29 | return 0; | |
30 | ||
bd6f78b2 | 31 | dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len); |
5ab088aa LS |
32 | if (fseek(f, t->pdu_len, SEEK_CUR) < 0) |
33 | return -errno; | |
34 | ||
35 | return t->pdu_len; | |
fb7b71a3 JA |
36 | } |
37 | ||
8c1fdf04 JA |
38 | /* |
39 | * Check if this is a blktrace binary data file. We read a single trace | |
40 | * into memory and check for the magic signature. | |
41 | */ | |
b153f94a | 42 | bool is_blktrace(const char *filename, int *need_swap) |
fb7b71a3 JA |
43 | { |
44 | struct blk_io_trace t; | |
45 | int fd, ret; | |
46 | ||
47 | fd = open(filename, O_RDONLY); | |
4dced407 | 48 | if (fd < 0) |
b153f94a | 49 | return false; |
fb7b71a3 JA |
50 | |
51 | ret = read(fd, &t, sizeof(t)); | |
52 | close(fd); | |
53 | ||
54 | if (ret < 0) { | |
55 | perror("read blktrace"); | |
b153f94a | 56 | return false; |
fb7b71a3 JA |
57 | } else if (ret != sizeof(t)) { |
58 | log_err("fio: short read on blktrace file\n"); | |
b153f94a | 59 | return false; |
fb7b71a3 JA |
60 | } |
61 | ||
d95b34a6 JA |
62 | if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) { |
63 | *need_swap = 0; | |
b153f94a | 64 | return true; |
d95b34a6 JA |
65 | } |
66 | ||
67 | /* | |
68 | * Maybe it needs to be endian swapped... | |
69 | */ | |
70 | t.magic = fio_swap32(t.magic); | |
71 | if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) { | |
72 | *need_swap = 1; | |
b153f94a | 73 | return true; |
d95b34a6 | 74 | } |
fb7b71a3 | 75 | |
b153f94a | 76 | return false; |
fb7b71a3 JA |
77 | } |
78 | ||
c69aa91f JA |
79 | #define FMINORBITS 20 |
80 | #define FMINORMASK ((1U << FMINORBITS) - 1) | |
81 | #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS)) | |
82 | #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK)) | |
eeb9c2aa | 83 | |
89ac1d48 | 84 | static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action) |
691c8fb0 JA |
85 | { |
86 | struct io_piece *ipo; | |
87 | ||
88 | ipo = calloc(1, sizeof(*ipo)); | |
0d29de83 | 89 | init_ipo(ipo); |
691c8fb0 JA |
90 | |
91 | ipo->ddir = DDIR_INVAL; | |
92 | ipo->fileno = fileno; | |
89ac1d48 | 93 | ipo->file_action = action; |
01743ee1 | 94 | flist_add_tail(&ipo->list, &td->io_log_list); |
691c8fb0 JA |
95 | } |
96 | ||
661592d4 LS |
97 | static int trace_add_file(struct thread_data *td, __u32 device, |
98 | struct file_cache *cache) | |
5e6c2067 | 99 | { |
c69aa91f JA |
100 | unsigned int maj = FMAJOR(device); |
101 | unsigned int min = FMINOR(device); | |
5e6c2067 | 102 | struct fio_file *f; |
26e616d0 | 103 | char dev[256]; |
6aaf98e9 | 104 | unsigned int i; |
5e6c2067 | 105 | |
661592d4 LS |
106 | if (cache->maj == maj && cache->min == min) |
107 | return cache->fileno; | |
5e6c2067 | 108 | |
661592d4 LS |
109 | cache->maj = maj; |
110 | cache->min = min; | |
5e6c2067 JA |
111 | |
112 | /* | |
113 | * check for this file in our list | |
114 | */ | |
6aaf98e9 | 115 | for_each_file(td, f, i) |
89ac1d48 | 116 | if (f->major == maj && f->minor == min) { |
661592d4 LS |
117 | cache->fileno = f->fileno; |
118 | return cache->fileno; | |
89ac1d48 | 119 | } |
5e6c2067 JA |
120 | |
121 | strcpy(dev, "/dev"); | |
40bafa33 | 122 | if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) { |
691c8fb0 JA |
123 | int fileno; |
124 | ||
40bafa33 JA |
125 | if (td->o.replay_redirect) |
126 | dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden" | |
127 | " with: %s\n", maj, min, | |
128 | td->o.replay_redirect); | |
129 | else | |
130 | dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min); | |
131 | ||
bd6f78b2 | 132 | dprint(FD_BLKTRACE, "add devices %s\n", dev); |
49ffb4a2 | 133 | fileno = add_file_exclusive(td, dev); |
b53f2c54 | 134 | td->o.open_files++; |
5903e7b7 JA |
135 | td->files[fileno]->major = maj; |
136 | td->files[fileno]->minor = min; | |
89ac1d48 | 137 | trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE); |
661592d4 | 138 | cache->fileno = fileno; |
bd6f78b2 | 139 | } |
f01b34ae | 140 | |
661592d4 | 141 | return cache->fileno; |
5e6c2067 JA |
142 | } |
143 | ||
0c63576e JA |
144 | static void t_bytes_align(struct thread_options *o, struct blk_io_trace *t) |
145 | { | |
146 | if (!o->replay_align) | |
147 | return; | |
148 | ||
149 | t->bytes = (t->bytes + o->replay_align - 1) & ~(o->replay_align - 1); | |
150 | } | |
151 | ||
8c1fdf04 JA |
152 | /* |
153 | * Store blk_io_trace data in an ipo for later retrieval. | |
154 | */ | |
fdefd987 | 155 | static void store_ipo(struct thread_data *td, unsigned long long offset, |
89ac1d48 | 156 | unsigned int bytes, int rw, unsigned long long ttime, |
6aaf98e9 | 157 | int fileno) |
fdefd987 | 158 | { |
8812d7f2 | 159 | struct io_piece *ipo; |
fdefd987 | 160 | |
8812d7f2 | 161 | ipo = calloc(1, sizeof(*ipo)); |
0d29de83 JA |
162 | init_ipo(ipo); |
163 | ||
6aaf98e9 | 164 | ipo->offset = offset * 512; |
0c63576e JA |
165 | if (td->o.replay_scale) |
166 | ipo->offset = ipo->offset / td->o.replay_scale; | |
a79f17bf | 167 | ipo_bytes_align(td->o.replay_align, ipo); |
fdefd987 | 168 | ipo->len = bytes; |
8c1fdf04 | 169 | ipo->delay = ttime / 1000; |
fdefd987 JA |
170 | if (rw) |
171 | ipo->ddir = DDIR_WRITE; | |
172 | else | |
173 | ipo->ddir = DDIR_READ; | |
89ac1d48 | 174 | ipo->fileno = fileno; |
fdefd987 | 175 | |
bd6f78b2 JA |
176 | dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n", |
177 | ipo->ddir, ipo->offset, | |
178 | ipo->len, ipo->delay); | |
691c8fb0 | 179 | queue_io_piece(td, ipo); |
fdefd987 JA |
180 | } |
181 | ||
10f74940 | 182 | static bool handle_trace_notify(struct blk_io_trace *t) |
cd991b9e | 183 | { |
691c8fb0 JA |
184 | switch (t->action) { |
185 | case BLK_TN_PROCESS: | |
24653680 | 186 | dprint(FD_BLKTRACE, "got process notify: %x, %d\n", |
d95b34a6 | 187 | t->action, t->pid); |
691c8fb0 JA |
188 | break; |
189 | case BLK_TN_TIMESTAMP: | |
24653680 | 190 | dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n", |
d95b34a6 | 191 | t->action, t->pid); |
691c8fb0 | 192 | break; |
ff58fced JA |
193 | case BLK_TN_MESSAGE: |
194 | break; | |
691c8fb0 JA |
195 | default: |
196 | dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action); | |
197 | break; | |
198 | } | |
10f74940 | 199 | return false; |
691c8fb0 | 200 | } |
5b3023b8 | 201 | |
10f74940 | 202 | static bool handle_trace_discard(struct thread_data *td, |
24653680 JA |
203 | struct blk_io_trace *t, |
204 | unsigned long long ttime, | |
661592d4 LS |
205 | unsigned long *ios, unsigned long long *bs, |
206 | struct file_cache *cache) | |
ff58fced | 207 | { |
8812d7f2 | 208 | struct io_piece *ipo; |
89ac1d48 | 209 | int fileno; |
ff58fced | 210 | |
d7235efb | 211 | if (td->o.replay_skip & (1u << DDIR_TRIM)) |
10f74940 | 212 | return false; |
d7235efb | 213 | |
8812d7f2 | 214 | ipo = calloc(1, sizeof(*ipo)); |
0d29de83 | 215 | init_ipo(ipo); |
661592d4 | 216 | fileno = trace_add_file(td, t->device, cache); |
ff58fced | 217 | |
24653680 | 218 | ios[DDIR_TRIM]++; |
6aaf98e9 DZ |
219 | if (t->bytes > bs[DDIR_TRIM]) |
220 | bs[DDIR_TRIM] = t->bytes; | |
24653680 | 221 | |
ff58fced JA |
222 | td->o.size += t->bytes; |
223 | ||
ff58fced JA |
224 | INIT_FLIST_HEAD(&ipo->list); |
225 | ||
6aaf98e9 | 226 | ipo->offset = t->sector * 512; |
0c63576e JA |
227 | if (td->o.replay_scale) |
228 | ipo->offset = ipo->offset / td->o.replay_scale; | |
a79f17bf | 229 | ipo_bytes_align(td->o.replay_align, ipo); |
ff58fced JA |
230 | ipo->len = t->bytes; |
231 | ipo->delay = ttime / 1000; | |
232 | ipo->ddir = DDIR_TRIM; | |
89ac1d48 | 233 | ipo->fileno = fileno; |
ff58fced JA |
234 | |
235 | dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n", | |
236 | ipo->offset, ipo->len, | |
237 | ipo->delay); | |
238 | queue_io_piece(td, ipo); | |
10f74940 | 239 | return true; |
ff58fced JA |
240 | } |
241 | ||
19a8064e JA |
242 | static void dump_trace(struct blk_io_trace *t) |
243 | { | |
244 | log_err("blktrace: ignoring zero byte trace: action=%x\n", t->action); | |
245 | } | |
246 | ||
10f74940 | 247 | static bool handle_trace_fs(struct thread_data *td, struct blk_io_trace *t, |
691c8fb0 | 248 | unsigned long long ttime, unsigned long *ios, |
661592d4 | 249 | unsigned long long *bs, struct file_cache *cache) |
691c8fb0 JA |
250 | { |
251 | int rw; | |
89ac1d48 | 252 | int fileno; |
5b3023b8 | 253 | |
661592d4 | 254 | fileno = trace_add_file(td, t->device, cache); |
5b3023b8 JA |
255 | |
256 | rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; | |
257 | ||
d7235efb JA |
258 | if (rw) { |
259 | if (td->o.replay_skip & (1u << DDIR_WRITE)) | |
10f74940 | 260 | return false; |
d7235efb JA |
261 | } else { |
262 | if (td->o.replay_skip & (1u << DDIR_READ)) | |
10f74940 | 263 | return false; |
d7235efb JA |
264 | } |
265 | ||
19a8064e JA |
266 | if (!t->bytes) { |
267 | if (!fio_did_warn(FIO_WARN_BTRACE_ZERO)) | |
268 | dump_trace(t); | |
10f74940 | 269 | return false; |
19a8064e | 270 | } |
c1f22c21 | 271 | |
6aaf98e9 DZ |
272 | if (t->bytes > bs[rw]) |
273 | bs[rw] = t->bytes; | |
5b3023b8 JA |
274 | |
275 | ios[rw]++; | |
276 | td->o.size += t->bytes; | |
6aaf98e9 | 277 | store_ipo(td, t->sector, t->bytes, rw, ttime, fileno); |
10f74940 | 278 | return true; |
cd991b9e JA |
279 | } |
280 | ||
10f74940 | 281 | static bool handle_trace_flush(struct thread_data *td, struct blk_io_trace *t, |
661592d4 LS |
282 | unsigned long long ttime, unsigned long *ios, |
283 | struct file_cache *cache) | |
65c42cc8 JA |
284 | { |
285 | struct io_piece *ipo; | |
65c42cc8 JA |
286 | int fileno; |
287 | ||
d7235efb | 288 | if (td->o.replay_skip & (1u << DDIR_SYNC)) |
10f74940 | 289 | return false; |
d7235efb | 290 | |
65c42cc8 JA |
291 | ipo = calloc(1, sizeof(*ipo)); |
292 | init_ipo(ipo); | |
661592d4 | 293 | fileno = trace_add_file(td, t->device, cache); |
65c42cc8 JA |
294 | |
295 | ipo->delay = ttime / 1000; | |
296 | ipo->ddir = DDIR_SYNC; | |
297 | ipo->fileno = fileno; | |
298 | ||
811f5421 | 299 | ios[DDIR_SYNC]++; |
65c42cc8 | 300 | dprint(FD_BLKTRACE, "store flush delay=%lu\n", ipo->delay); |
78c0d7a0 JA |
301 | |
302 | if (!(td->flags & TD_F_SYNCS)) | |
303 | td->flags |= TD_F_SYNCS; | |
304 | ||
65c42cc8 | 305 | queue_io_piece(td, ipo); |
10f74940 | 306 | return true; |
65c42cc8 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 | */ | |
10f74940 | 313 | static bool queue_trace(struct thread_data *td, struct blk_io_trace *t, |
661592d4 LS |
314 | unsigned long *ios, unsigned long long *bs, |
315 | struct file_cache *cache) | |
691c8fb0 | 316 | { |
315bbf01 | 317 | unsigned long long *last_ttime = &td->io_log_last_ttime; |
0c63576e | 318 | unsigned long long delay = 0; |
24653680 | 319 | |
691c8fb0 | 320 | if ((t->action & 0xffff) != __BLK_TA_QUEUE) |
10f74940 | 321 | return false; |
24653680 JA |
322 | |
323 | if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) { | |
315bbf01 | 324 | delay = delay_since_ttime(td, t->time); |
661592d4 | 325 | *last_ttime = t->time; |
24653680 | 326 | } |
691c8fb0 | 327 | |
0c63576e JA |
328 | t_bytes_align(&td->o, t); |
329 | ||
691c8fb0 | 330 | if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY)) |
10f74940 | 331 | return handle_trace_notify(t); |
ff58fced | 332 | else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD)) |
661592d4 | 333 | return handle_trace_discard(td, t, delay, ios, bs, cache); |
65c42cc8 | 334 | else if (t->action & BLK_TC_ACT(BLK_TC_FLUSH)) |
661592d4 | 335 | return handle_trace_flush(td, t, delay, ios, cache); |
691c8fb0 | 336 | else |
661592d4 | 337 | return handle_trace_fs(td, t, delay, ios, bs, cache); |
691c8fb0 JA |
338 | } |
339 | ||
d95b34a6 JA |
340 | static 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 | ||
b153f94a | 355 | static bool t_is_write(struct blk_io_trace *t) |
24653680 JA |
356 | { |
357 | return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0; | |
358 | } | |
359 | ||
a6eaf6c9 JA |
360 | static 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 | ||
372 | static 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 | ||
381 | static 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 | ||
390 | static 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 | */ | |
10f74940 LS |
405 | bool init_blktrace_read(struct thread_data *td, const char *filename, int need_swap) |
406 | { | |
407 | int old_state; | |
408 | ||
409 | td->io_log_rfile = fopen(filename, "rb"); | |
410 | if (!td->io_log_rfile) { | |
411 | td_verror(td, errno, "open blktrace file"); | |
412 | goto err; | |
413 | } | |
414 | td->io_log_blktrace_swap = need_swap; | |
315bbf01 | 415 | td->io_log_last_ttime = 0; |
10f74940 LS |
416 | td->o.size = 0; |
417 | ||
418 | free_release_files(td); | |
419 | ||
420 | old_state = td_bump_runstate(td, TD_SETTING_UP); | |
421 | ||
422 | if (!read_blktrace(td)) { | |
423 | goto err; | |
424 | } | |
425 | ||
426 | td_restore_runstate(td, old_state); | |
427 | ||
428 | if (!td->files_index) { | |
429 | log_err("fio: did not find replay device(s)\n"); | |
430 | return false; | |
431 | } | |
432 | ||
433 | return true; | |
434 | ||
435 | err: | |
436 | if (td->io_log_rfile) { | |
437 | fclose(td->io_log_rfile); | |
438 | td->io_log_rfile = NULL; | |
439 | } | |
440 | return false; | |
441 | } | |
442 | ||
443 | bool read_blktrace(struct thread_data* td) | |
fb7b71a3 JA |
444 | { |
445 | struct blk_io_trace t; | |
2fe71558 LU |
446 | struct file_cache cache = { |
447 | .maj = ~0U, | |
448 | .min = ~0U, | |
449 | }; | |
811f5421 | 450 | unsigned long ios[DDIR_RWDIR_SYNC_CNT] = { }; |
10f74940 | 451 | unsigned long long rw_bs[DDIR_RWDIR_CNT] = { }; |
811f5421 | 452 | unsigned long skipped_writes; |
10f74940 LS |
453 | FILE *f = td->io_log_rfile; |
454 | int i, max_depth; | |
5ab088aa | 455 | struct fio_file *fiof; |
811f5421 JA |
456 | int this_depth[DDIR_RWDIR_CNT] = { }; |
457 | int depth[DDIR_RWDIR_CNT] = { }; | |
10f74940 | 458 | int64_t items_to_fetch = 0; |
fb7b71a3 | 459 | |
10f74940 LS |
460 | if (td->o.read_iolog_chunked) { |
461 | items_to_fetch = iolog_items_to_fetch(td); | |
462 | if (!items_to_fetch) | |
463 | return true; | |
fb7b71a3 JA |
464 | } |
465 | ||
4241ea8f | 466 | skipped_writes = 0; |
fb7b71a3 | 467 | do { |
5ab088aa | 468 | int ret = fread(&t, 1, sizeof(t), f); |
fb7b71a3 | 469 | |
5ab088aa LS |
470 | if (ferror(f)) { |
471 | td_verror(td, errno, "read blktrace file"); | |
8c1fdf04 | 472 | goto err; |
5ab088aa | 473 | } else if (feof(f)) { |
e2887563 | 474 | break; |
5ab088aa LS |
475 | } else if (ret < (int) sizeof(t)) { |
476 | log_err("fio: iolog short read\n"); | |
fb7b71a3 | 477 | break; |
fb7b71a3 JA |
478 | } |
479 | ||
10f74940 | 480 | if (td->io_log_blktrace_swap) |
d95b34a6 JA |
481 | byteswap_trace(&t); |
482 | ||
fb7b71a3 | 483 | if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
5ec10eaa JA |
484 | log_err("fio: bad magic in blktrace data: %x\n", |
485 | t.magic); | |
8c1fdf04 | 486 | goto err; |
fb7b71a3 JA |
487 | } |
488 | if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) { | |
5ec10eaa JA |
489 | log_err("fio: bad blktrace version %d\n", |
490 | t.magic & 0xff); | |
8c1fdf04 | 491 | goto err; |
fb7b71a3 | 492 | } |
5ab088aa | 493 | ret = discard_pdu(f, &t); |
f12b323f | 494 | if (ret < 0) { |
874a61d0 | 495 | td_verror(td, -ret, "blktrace lseek"); |
8c1fdf04 | 496 | goto err; |
fb7b71a3 | 497 | } |
691c8fb0 | 498 | if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) { |
eb5fdcf1 | 499 | if ((t.action & 0xffff) == __BLK_TA_QUEUE) |
a6eaf6c9 JA |
500 | depth_inc(&t, this_depth); |
501 | else if (((t.action & 0xffff) == __BLK_TA_BACKMERGE) || | |
502 | ((t.action & 0xffff) == __BLK_TA_FRONTMERGE)) | |
503 | depth_dec(&t, this_depth); | |
504 | else if ((t.action & 0xffff) == __BLK_TA_COMPLETE) | |
505 | depth_end(&t, this_depth, depth); | |
691c8fb0 | 506 | |
24653680 | 507 | if (t_is_write(&t) && read_only) { |
691c8fb0 | 508 | skipped_writes++; |
24653680 | 509 | continue; |
64bbb865 | 510 | } |
a6edd638 | 511 | } |
24653680 | 512 | |
661592d4 | 513 | if (!queue_trace(td, &t, ios, rw_bs, &cache)) |
10f74940 | 514 | continue; |
fb7b71a3 | 515 | |
10f74940 LS |
516 | if (td->o.read_iolog_chunked) { |
517 | td->io_log_current++; | |
518 | items_to_fetch--; | |
519 | if (items_to_fetch == 0) | |
520 | break; | |
521 | } | |
522 | } while (1); | |
89ac1d48 | 523 | |
10f74940 LS |
524 | if (td->o.read_iolog_chunked) { |
525 | td->io_log_highmark = td->io_log_current; | |
526 | td->io_log_checkmark = (td->io_log_highmark + 1) / 2; | |
527 | fio_gettime(&td->io_log_highmark_time, NULL); | |
528 | } | |
8c1fdf04 | 529 | |
10f74940 LS |
530 | if (skipped_writes) |
531 | log_err("fio: %s skips replay of %lu writes due to read-only\n", | |
532 | td->o.name, skipped_writes); | |
5903e7b7 | 533 | |
10f74940 LS |
534 | if (td->o.read_iolog_chunked) { |
535 | if (td->io_log_current == 0) { | |
536 | return false; | |
537 | } | |
538 | td->o.td_ddir = TD_DDIR_RW; | |
539 | if ((rw_bs[DDIR_READ] > td->o.max_bs[DDIR_READ] || | |
540 | rw_bs[DDIR_WRITE] > td->o.max_bs[DDIR_WRITE] || | |
541 | rw_bs[DDIR_TRIM] > td->o.max_bs[DDIR_TRIM]) && | |
542 | td->orig_buffer) | |
543 | { | |
544 | td->o.max_bs[DDIR_READ] = max(td->o.max_bs[DDIR_READ], rw_bs[DDIR_READ]); | |
545 | td->o.max_bs[DDIR_WRITE] = max(td->o.max_bs[DDIR_WRITE], rw_bs[DDIR_WRITE]); | |
546 | td->o.max_bs[DDIR_TRIM] = max(td->o.max_bs[DDIR_TRIM], rw_bs[DDIR_TRIM]); | |
547 | io_u_quiesce(td); | |
548 | free_io_mem(td); | |
6d8fe6e8 SK |
549 | if (init_io_u_buffers(td)) |
550 | return false; | |
10f74940 LS |
551 | } |
552 | return true; | |
f01b34ae JA |
553 | } |
554 | ||
10f74940 LS |
555 | for_each_file(td, fiof, i) |
556 | trace_add_open_close_event(td, fiof->fileno, FIO_LOG_CLOSE_FILE); | |
557 | ||
558 | fclose(td->io_log_rfile); | |
559 | td->io_log_rfile = NULL; | |
560 | ||
eb5fdcf1 JA |
561 | /* |
562 | * For stacked devices, we don't always get a COMPLETE event so | |
563 | * the depth grows to insane values. Limit it to something sane(r). | |
564 | */ | |
a6eaf6c9 JA |
565 | max_depth = 0; |
566 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
567 | if (depth[i] > 1024) | |
568 | depth[i] = 1024; | |
569 | else if (!depth[i] && ios[i]) | |
570 | depth[i] = 1; | |
571 | max_depth = max(depth[i], max_depth); | |
572 | } | |
eb5fdcf1 | 573 | |
811f5421 JA |
574 | if (!ios[DDIR_READ] && !ios[DDIR_WRITE] && !ios[DDIR_TRIM] && |
575 | !ios[DDIR_SYNC]) { | |
8c1fdf04 | 576 | log_err("fio: found no ios in blktrace data\n"); |
b153f94a | 577 | return false; |
252928cb | 578 | } |
579 | ||
580 | td->o.td_ddir = 0; | |
581 | if (ios[DDIR_READ]) { | |
582 | td->o.td_ddir |= TD_DDIR_READ; | |
d84f8d49 | 583 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
252928cb | 584 | } |
585 | if (ios[DDIR_WRITE]) { | |
586 | td->o.td_ddir |= TD_DDIR_WRITE; | |
d84f8d49 | 587 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; |
252928cb | 588 | } |
589 | if (ios[DDIR_TRIM]) { | |
590 | td->o.td_ddir |= TD_DDIR_TRIM; | |
24653680 | 591 | td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM]; |
d84f8d49 | 592 | } |
8c1fdf04 | 593 | |
eb5fdcf1 | 594 | /* |
8a16f59b | 595 | * If depth wasn't manually set, use probed depth |
eb5fdcf1 | 596 | */ |
8a16f59b | 597 | if (!fio_option_is_set(&td->o, iodepth)) |
a6eaf6c9 | 598 | td->o.iodepth = td->o.iodepth_low = max_depth; |
eb5fdcf1 | 599 | |
b153f94a | 600 | return true; |
8c1fdf04 | 601 | err: |
5ab088aa | 602 | fclose(f); |
b153f94a | 603 | return false; |
fb7b71a3 | 604 | } |
b9921d1a | 605 | |
87a48ada DZ |
606 | static int init_merge_param_list(fio_fp64_t *vals, struct blktrace_cursor *bcs, |
607 | int nr_logs, int def, size_t off) | |
608 | { | |
609 | int i = 0, len = 0; | |
610 | ||
611 | while (len < FIO_IO_U_LIST_MAX_LEN && vals[len].u.f != 0.0) | |
612 | len++; | |
613 | ||
614 | if (len && len != nr_logs) | |
615 | return len; | |
616 | ||
617 | for (i = 0; i < nr_logs; i++) { | |
618 | int *val = (int *)((char *)&bcs[i] + off); | |
619 | *val = def; | |
620 | if (len) | |
621 | *val = (int)vals[i].u.f; | |
622 | } | |
623 | ||
624 | return 0; | |
625 | ||
626 | } | |
627 | ||
b9921d1a DZ |
628 | static int find_earliest_io(struct blktrace_cursor *bcs, int nr_logs) |
629 | { | |
630 | __u64 time = ~(__u64)0; | |
631 | int idx = 0, i; | |
632 | ||
633 | for (i = 0; i < nr_logs; i++) { | |
634 | if (bcs[i].t.time < time) { | |
635 | time = bcs[i].t.time; | |
636 | idx = i; | |
637 | } | |
638 | } | |
639 | ||
640 | return idx; | |
641 | } | |
642 | ||
643 | static void merge_finish_file(struct blktrace_cursor *bcs, int i, int *nr_logs) | |
644 | { | |
55bfd8c8 DZ |
645 | bcs[i].iter++; |
646 | if (bcs[i].iter < bcs[i].nr_iter) { | |
5ab088aa | 647 | fseek(bcs[i].f, 0, SEEK_SET); |
55bfd8c8 DZ |
648 | return; |
649 | } | |
650 | ||
b9921d1a DZ |
651 | *nr_logs -= 1; |
652 | ||
653 | /* close file */ | |
5ab088aa | 654 | fclose(bcs[i].f); |
b9921d1a DZ |
655 | |
656 | /* keep active files contiguous */ | |
657 | memmove(&bcs[i], &bcs[*nr_logs], sizeof(bcs[i])); | |
658 | } | |
659 | ||
660 | static int read_trace(struct thread_data *td, struct blktrace_cursor *bc) | |
661 | { | |
662 | int ret = 0; | |
663 | struct blk_io_trace *t = &bc->t; | |
664 | ||
665 | read_skip: | |
666 | /* read an io trace */ | |
5ab088aa LS |
667 | ret = fread(&t, 1, sizeof(t), bc->f); |
668 | if (ferror(bc->f)) { | |
669 | td_verror(td, errno, "read blktrace file"); | |
55bfd8c8 | 670 | return ret; |
5ab088aa | 671 | } else if (feof(bc->f)) { |
55bfd8c8 DZ |
672 | if (!bc->length) |
673 | bc->length = bc->t.time; | |
b9921d1a DZ |
674 | return ret; |
675 | } else if (ret < (int) sizeof(*t)) { | |
5ab088aa | 676 | log_err("fio: iolog short read\n"); |
b9921d1a DZ |
677 | return -1; |
678 | } | |
679 | ||
680 | if (bc->swap) | |
681 | byteswap_trace(t); | |
682 | ||
683 | /* skip over actions that fio does not care about */ | |
684 | if ((t->action & 0xffff) != __BLK_TA_QUEUE || | |
685 | t_get_ddir(t) == DDIR_INVAL) { | |
5ab088aa | 686 | ret = discard_pdu(bc->f, t); |
b9921d1a | 687 | if (ret < 0) { |
874a61d0 | 688 | td_verror(td, -ret, "blktrace lseek"); |
b9921d1a | 689 | return ret; |
b9921d1a DZ |
690 | } |
691 | goto read_skip; | |
692 | } | |
693 | ||
55bfd8c8 | 694 | t->time = (t->time + bc->iter * bc->length) * bc->scalar / 100; |
87a48ada | 695 | |
b9921d1a DZ |
696 | return ret; |
697 | } | |
698 | ||
699 | static int write_trace(FILE *fp, struct blk_io_trace *t) | |
700 | { | |
701 | /* pdu is not used so just write out only the io trace */ | |
702 | t->pdu_len = 0; | |
703 | return fwrite((void *)t, sizeof(*t), 1, fp); | |
704 | } | |
705 | ||
706 | int merge_blktrace_iologs(struct thread_data *td) | |
707 | { | |
708 | int nr_logs = get_max_str_idx(td->o.read_iolog_file); | |
709 | struct blktrace_cursor *bcs = malloc(sizeof(struct blktrace_cursor) * | |
710 | nr_logs); | |
711 | struct blktrace_cursor *bc; | |
712 | FILE *merge_fp; | |
713 | char *str, *ptr, *name, *merge_buf; | |
714 | int i, ret; | |
715 | ||
87a48ada DZ |
716 | ret = init_merge_param_list(td->o.merge_blktrace_scalars, bcs, nr_logs, |
717 | 100, offsetof(struct blktrace_cursor, | |
718 | scalar)); | |
719 | if (ret) { | |
720 | log_err("fio: merge_blktrace_scalars(%d) != nr_logs(%d)\n", | |
721 | ret, nr_logs); | |
722 | goto err_param; | |
723 | } | |
724 | ||
55bfd8c8 DZ |
725 | ret = init_merge_param_list(td->o.merge_blktrace_iters, bcs, nr_logs, |
726 | 1, offsetof(struct blktrace_cursor, | |
727 | nr_iter)); | |
728 | if (ret) { | |
729 | log_err("fio: merge_blktrace_iters(%d) != nr_logs(%d)\n", | |
730 | ret, nr_logs); | |
731 | goto err_param; | |
732 | } | |
733 | ||
b9921d1a DZ |
734 | /* setup output file */ |
735 | merge_fp = fopen(td->o.merge_blktrace_file, "w"); | |
736 | merge_buf = malloc(128 * 1024); | |
c81ab051 BVA |
737 | if (!merge_buf) |
738 | goto err_out_file; | |
b9921d1a DZ |
739 | ret = setvbuf(merge_fp, merge_buf, _IOFBF, 128 * 1024); |
740 | if (ret) | |
c81ab051 | 741 | goto err_merge_buf; |
b9921d1a DZ |
742 | |
743 | /* setup input files */ | |
744 | str = ptr = strdup(td->o.read_iolog_file); | |
745 | nr_logs = 0; | |
746 | for (i = 0; (name = get_next_str(&ptr)) != NULL; i++) { | |
5ab088aa LS |
747 | bcs[i].f = fopen(name, "rb"); |
748 | if (!bcs[i].f) { | |
b9921d1a | 749 | log_err("fio: could not open file: %s\n", name); |
5ab088aa | 750 | ret = -errno; |
2ba46d1b | 751 | free(str); |
b9921d1a DZ |
752 | goto err_file; |
753 | } | |
b9921d1a DZ |
754 | nr_logs++; |
755 | ||
756 | if (!is_blktrace(name, &bcs[i].swap)) { | |
757 | log_err("fio: file is not a blktrace: %s\n", name); | |
2ba46d1b | 758 | free(str); |
b9921d1a DZ |
759 | goto err_file; |
760 | } | |
761 | ||
762 | ret = read_trace(td, &bcs[i]); | |
763 | if (ret < 0) { | |
2ba46d1b | 764 | free(str); |
b9921d1a DZ |
765 | goto err_file; |
766 | } else if (!ret) { | |
767 | merge_finish_file(bcs, i, &nr_logs); | |
768 | i--; | |
769 | } | |
770 | } | |
771 | free(str); | |
772 | ||
773 | /* merge files */ | |
774 | while (nr_logs) { | |
775 | i = find_earliest_io(bcs, nr_logs); | |
776 | bc = &bcs[i]; | |
777 | /* skip over the pdu */ | |
5ab088aa | 778 | ret = discard_pdu(bc->f, &bc->t); |
b9921d1a | 779 | if (ret < 0) { |
874a61d0 | 780 | td_verror(td, -ret, "blktrace lseek"); |
b9921d1a | 781 | goto err_file; |
b9921d1a DZ |
782 | } |
783 | ||
784 | ret = write_trace(merge_fp, &bc->t); | |
785 | ret = read_trace(td, bc); | |
786 | if (ret < 0) | |
787 | goto err_file; | |
788 | else if (!ret) | |
789 | merge_finish_file(bcs, i, &nr_logs); | |
790 | } | |
791 | ||
792 | /* set iolog file to read from the newly merged file */ | |
793 | td->o.read_iolog_file = td->o.merge_blktrace_file; | |
794 | ret = 0; | |
795 | ||
796 | err_file: | |
797 | /* cleanup */ | |
798 | for (i = 0; i < nr_logs; i++) { | |
5ab088aa | 799 | fclose(bcs[i].f); |
b9921d1a | 800 | } |
c81ab051 BVA |
801 | err_merge_buf: |
802 | free(merge_buf); | |
b9921d1a DZ |
803 | err_out_file: |
804 | fflush(merge_fp); | |
805 | fclose(merge_fp); | |
87a48ada | 806 | err_param: |
b9921d1a DZ |
807 | free(bcs); |
808 | ||
809 | return ret; | |
810 | } |