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