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