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