Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * blktrace support code for fio |
| 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 6 | #include <sys/stat.h> |
| 7 | #include <dirent.h> |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 8 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 9 | #include "flist.h" |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 10 | #include "fio.h" |
| 11 | #include "blktrace_api.h" |
Jens Axboe | 42a80e3 | 2014-09-16 17:06:52 +0200 | [diff] [blame] | 12 | #include "lib/linux-dev-lookup.h" |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 13 | |
Jens Axboe | 2da7df1 | 2010-08-01 21:27:28 +0200 | [diff] [blame] | 14 | #define TRACE_FIFO_SIZE 8192 |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * fifo refill frontend, to avoid reading data in trace sized bites |
| 18 | */ |
| 19 | static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd) |
| 20 | { |
| 21 | char buf[TRACE_FIFO_SIZE]; |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 22 | unsigned int total; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 23 | int ret; |
| 24 | |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 25 | total = sizeof(buf); |
| 26 | if (total > fifo_room(fifo)) |
| 27 | total = fifo_room(fifo); |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 28 | |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 29 | ret = read(fd, buf, total); |
| 30 | if (ret < 0) { |
| 31 | td_verror(td, errno, "read blktrace file"); |
| 32 | return -1; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 35 | if (ret > 0) |
| 36 | ret = fifo_put(fifo, buf, ret); |
| 37 | |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 38 | dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret); |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 39 | return ret; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Retrieve 'len' bytes from the fifo, refilling if necessary. |
| 44 | */ |
| 45 | static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd, |
| 46 | void *buf, unsigned int len) |
| 47 | { |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 48 | if (fifo_len(fifo) < len) { |
| 49 | int ret = refill_fifo(td, fifo, fd); |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 50 | |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 51 | if (ret < 0) |
| 52 | return ret; |
| 53 | } |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 54 | |
| 55 | return fifo_get(fifo, buf, len); |
| 56 | } |
| 57 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 58 | /* |
| 59 | * Just discard the pdu by seeking past it. |
| 60 | */ |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 61 | static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd, |
| 62 | struct blk_io_trace *t) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 63 | { |
| 64 | if (t->pdu_len == 0) |
| 65 | return 0; |
| 66 | |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 67 | dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len); |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 68 | return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 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 | */ |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 75 | int is_blktrace(const char *filename, int *need_swap) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 76 | { |
| 77 | struct blk_io_trace t; |
| 78 | int fd, ret; |
| 79 | |
| 80 | fd = open(filename, O_RDONLY); |
Jens Axboe | 4dced40 | 2007-07-23 14:16:55 +0200 | [diff] [blame] | 81 | if (fd < 0) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 82 | return 0; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 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 | |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 95 | if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) { |
| 96 | *need_swap = 0; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 97 | return 1; |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 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 | } |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Jens Axboe | c69aa91 | 2007-05-22 15:51:50 +0200 | [diff] [blame] | 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)) |
Jens Axboe | eeb9c2a | 2007-05-16 18:28:47 +0200 | [diff] [blame] | 116 | |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 117 | static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action) |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 118 | { |
| 119 | struct io_piece *ipo; |
| 120 | |
| 121 | ipo = calloc(1, sizeof(*ipo)); |
Jens Axboe | 0d29de8 | 2010-09-01 13:54:15 +0200 | [diff] [blame] | 122 | init_ipo(ipo); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 123 | |
| 124 | ipo->ddir = DDIR_INVAL; |
| 125 | ipo->fileno = fileno; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 126 | ipo->file_action = action; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 127 | flist_add_tail(&ipo->list, &td->io_log_list); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 130 | static int trace_add_file(struct thread_data *td, __u32 device) |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 131 | { |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 132 | static unsigned int last_maj, last_min, last_fileno; |
Jens Axboe | c69aa91 | 2007-05-22 15:51:50 +0200 | [diff] [blame] | 133 | unsigned int maj = FMAJOR(device); |
| 134 | unsigned int min = FMINOR(device); |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 135 | struct fio_file *f; |
| 136 | char dev[256]; |
| 137 | unsigned int i; |
| 138 | |
| 139 | if (last_maj == maj && last_min == min) |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 140 | return last_fileno; |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 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) |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 149 | if (f->major == maj && f->minor == min) { |
| 150 | last_fileno = f->fileno; |
| 151 | return last_fileno; |
| 152 | } |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 153 | |
| 154 | strcpy(dev, "/dev"); |
Jens Axboe | 42a80e3 | 2014-09-16 17:06:52 +0200 | [diff] [blame] | 155 | if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) { |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 156 | int fileno; |
| 157 | |
Jens Axboe | 42a80e3 | 2014-09-16 17:06:52 +0200 | [diff] [blame] | 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 | |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 165 | dprint(FD_BLKTRACE, "add devices %s\n", dev); |
Jens Axboe | 49ffb4a | 2010-08-24 15:01:37 +0200 | [diff] [blame] | 166 | fileno = add_file_exclusive(td, dev); |
Jens Axboe | b53f2c5 | 2014-04-08 21:07:12 -0600 | [diff] [blame] | 167 | td->o.open_files++; |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 168 | td->files[fileno]->major = maj; |
| 169 | td->files[fileno]->minor = min; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 170 | trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE); |
| 171 | last_fileno = fileno; |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 172 | } |
Jens Axboe | f01b34a | 2013-11-21 11:13:12 -0700 | [diff] [blame] | 173 | |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 174 | return last_fileno; |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 177 | /* |
| 178 | * Store blk_io_trace data in an ipo for later retrieval. |
| 179 | */ |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 180 | static void store_ipo(struct thread_data *td, unsigned long long offset, |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 181 | unsigned int bytes, int rw, unsigned long long ttime, |
| 182 | int fileno) |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 183 | { |
| 184 | struct io_piece *ipo = malloc(sizeof(*ipo)); |
| 185 | |
Jens Axboe | 0d29de8 | 2010-09-01 13:54:15 +0200 | [diff] [blame] | 186 | init_ipo(ipo); |
| 187 | |
Jens Axboe | a2eea81 | 2007-05-15 13:10:41 +0200 | [diff] [blame] | 188 | /* |
| 189 | * the 512 is wrong here, it should be the hardware sector size... |
| 190 | */ |
| 191 | ipo->offset = offset * 512; |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 192 | ipo->len = bytes; |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 193 | ipo->delay = ttime / 1000; |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 194 | if (rw) |
| 195 | ipo->ddir = DDIR_WRITE; |
| 196 | else |
| 197 | ipo->ddir = DDIR_READ; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 198 | ipo->fileno = fileno; |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 199 | |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 200 | dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n", |
| 201 | ipo->ddir, ipo->offset, |
| 202 | ipo->len, ipo->delay); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 203 | queue_io_piece(td, ipo); |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 206 | static void handle_trace_notify(struct blk_io_trace *t) |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 207 | { |
| 208 | switch (t->action) { |
| 209 | case BLK_TN_PROCESS: |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 210 | dprint(FD_BLKTRACE, "got process notify: %x, %d\n", |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 211 | t->action, t->pid); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 212 | break; |
| 213 | case BLK_TN_TIMESTAMP: |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 214 | dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n", |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 215 | t->action, t->pid); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 216 | break; |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 217 | case BLK_TN_MESSAGE: |
| 218 | break; |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 219 | default: |
| 220 | dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action); |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 225 | static 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) |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 229 | { |
| 230 | struct io_piece *ipo = malloc(sizeof(*ipo)); |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 231 | int fileno; |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 232 | |
Jens Axboe | 0d29de8 | 2010-09-01 13:54:15 +0200 | [diff] [blame] | 233 | init_ipo(ipo); |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 234 | fileno = trace_add_file(td, t->device); |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 235 | |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 236 | ios[DDIR_TRIM]++; |
| 237 | if (t->bytes > bs[DDIR_TRIM]) |
| 238 | bs[DDIR_TRIM] = t->bytes; |
| 239 | |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 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; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 252 | ipo->fileno = fileno; |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 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 | |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 260 | static 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) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 263 | { |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 264 | int rw; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 265 | int fileno; |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 266 | |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 267 | fileno = trace_add_file(td, t->device); |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 268 | |
Jens Axboe | e7a7d70 | 2007-05-15 10:13:04 +0200 | [diff] [blame] | 269 | rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 270 | |
| 271 | if (t->bytes > bs[rw]) |
| 272 | bs[rw] = t->bytes; |
| 273 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 274 | ios[rw]++; |
Jens Axboe | 6df8ada | 2007-05-15 13:23:19 +0200 | [diff] [blame] | 275 | td->o.size += t->bytes; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 276 | store_ipo(td, t->sector, t->bytes, rw, ttime, fileno); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 279 | /* |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 280 | * We only care for queue traces, most of the others are side effects |
| 281 | * due to internal workings of the block layer. |
| 282 | */ |
| 283 | static void handle_trace(struct thread_data *td, struct blk_io_trace *t, |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 284 | unsigned long *ios, unsigned int *bs) |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 285 | { |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 286 | static unsigned long long last_ttime; |
| 287 | unsigned long long delay; |
| 288 | |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 289 | if ((t->action & 0xffff) != __BLK_TA_QUEUE) |
| 290 | return; |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 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 | } |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 301 | |
| 302 | if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY)) |
Jens Axboe | aec2de2 | 2008-04-24 12:44:42 +0200 | [diff] [blame] | 303 | handle_trace_notify(t); |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 304 | else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD)) |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 305 | handle_trace_discard(td, t, delay, ios, bs); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 306 | else |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 307 | handle_trace_fs(td, t, delay, ios, bs); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 310 | static 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 | |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 325 | static 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 | |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 330 | /* |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 331 | * Load a blktrace file by reading all the blk_io_trace entries, and storing |
| 332 | * them as io_pieces like the fio text version would do. |
| 333 | */ |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 334 | int load_blktrace(struct thread_data *td, const char *filename, int need_swap) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 335 | { |
| 336 | struct blk_io_trace t; |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 337 | unsigned long ios[DDIR_RWDIR_CNT], skipped_writes; |
| 338 | unsigned int rw_bs[DDIR_RWDIR_CNT]; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 339 | struct fifo *fifo; |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 340 | int fd, i, old_state; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 341 | struct fio_file *f; |
Jens Axboe | eb5fdcf | 2014-04-08 13:43:19 -0600 | [diff] [blame] | 342 | int this_depth, depth; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 343 | |
| 344 | fd = open(filename, O_RDONLY); |
| 345 | if (fd < 0) { |
| 346 | td_verror(td, errno, "open blktrace file"); |
| 347 | return 1; |
| 348 | } |
| 349 | |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 350 | fifo = fifo_alloc(TRACE_FIFO_SIZE); |
| 351 | |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 352 | old_state = td_bump_runstate(td, TD_SETTING_UP); |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 353 | |
Jens Axboe | 6df8ada | 2007-05-15 13:23:19 +0200 | [diff] [blame] | 354 | td->o.size = 0; |
| 355 | |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 356 | ios[0] = ios[1] = 0; |
| 357 | rw_bs[0] = rw_bs[1] = 0; |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 358 | skipped_writes = 0; |
Jens Axboe | eb5fdcf | 2014-04-08 13:43:19 -0600 | [diff] [blame] | 359 | this_depth = depth = 0; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 360 | do { |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 361 | int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t)); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 362 | |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 363 | if (ret < 0) |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 364 | goto err; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 365 | else if (!ret) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 366 | break; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 367 | else if (ret < (int) sizeof(t)) { |
| 368 | log_err("fio: short fifo get\n"); |
| 369 | break; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 372 | if (need_swap) |
| 373 | byteswap_trace(&t); |
| 374 | |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 375 | if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 376 | log_err("fio: bad magic in blktrace data: %x\n", |
| 377 | t.magic); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 378 | goto err; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 379 | } |
| 380 | if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 381 | log_err("fio: bad blktrace version %d\n", |
| 382 | t.magic & 0xff); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 383 | goto err; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 384 | } |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 385 | ret = discard_pdu(td, fifo, fd, &t); |
| 386 | if (ret < 0) { |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 387 | td_verror(td, ret, "blktrace lseek"); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 388 | goto err; |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 389 | } else if (t.pdu_len != ret) { |
| 390 | log_err("fio: discarded %d of %d\n", ret, t.pdu_len); |
| 391 | goto err; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 392 | } |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 393 | if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) { |
Jens Axboe | eb5fdcf | 2014-04-08 13:43:19 -0600 | [diff] [blame] | 394 | if ((t.action & 0xffff) == __BLK_TA_QUEUE) |
| 395 | this_depth++; |
| 396 | else if ((t.action & 0xffff) == __BLK_TA_COMPLETE) { |
| 397 | depth = max(depth, this_depth); |
| 398 | this_depth = 0; |
| 399 | } |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 400 | |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 401 | if (t_is_write(&t) && read_only) { |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 402 | skipped_writes++; |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 403 | continue; |
David Nellans | 64bbb86 | 2010-08-24 22:13:30 +0200 | [diff] [blame] | 404 | } |
Jens Axboe | a6edd63 | 2008-03-07 21:41:54 +0100 | [diff] [blame] | 405 | } |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 406 | |
| 407 | handle_trace(td, &t, ios, rw_bs); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 408 | } while (1); |
| 409 | |
Jens Axboe | 85a47ca | 2012-06-11 13:19:13 +0200 | [diff] [blame] | 410 | for (i = 0; i < td->files_index; i++) { |
Jens Axboe | f01b34a | 2013-11-21 11:13:12 -0700 | [diff] [blame] | 411 | f = td->files[i]; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 412 | trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE); |
Jens Axboe | 85a47ca | 2012-06-11 13:19:13 +0200 | [diff] [blame] | 413 | } |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 414 | |
Jens Axboe | 38470f8 | 2007-05-16 09:26:23 +0200 | [diff] [blame] | 415 | fifo_free(fifo); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 416 | close(fd); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 417 | |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 418 | td_restore_runstate(td, old_state); |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 419 | |
Jens Axboe | f01b34a | 2013-11-21 11:13:12 -0700 | [diff] [blame] | 420 | if (!td->files_index) { |
| 421 | log_err("fio: did not find replay device(s)\n"); |
| 422 | return 1; |
| 423 | } |
| 424 | |
Jens Axboe | eb5fdcf | 2014-04-08 13:43:19 -0600 | [diff] [blame] | 425 | /* |
| 426 | * For stacked devices, we don't always get a COMPLETE event so |
| 427 | * the depth grows to insane values. Limit it to something sane(r). |
| 428 | */ |
| 429 | if (!depth || depth > 1024) |
| 430 | depth = 1024; |
| 431 | |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 432 | if (skipped_writes) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 433 | log_err("fio: %s skips replay of %lu writes due to read-only\n", |
| 434 | td->o.name, skipped_writes); |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 435 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 436 | if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) { |
| 437 | log_err("fio: found no ios in blktrace data\n"); |
| 438 | return 1; |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 439 | } else if (ios[DDIR_READ] && !ios[DDIR_WRITE]) { |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 440 | td->o.td_ddir = TD_DDIR_READ; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 441 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
| 442 | } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) { |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 443 | td->o.td_ddir = TD_DDIR_WRITE; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 444 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; |
| 445 | } else { |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 446 | td->o.td_ddir = TD_DDIR_RW; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 447 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
| 448 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; |
Jens Axboe | 5bca6f9 | 2014-07-14 15:07:11 +0200 | [diff] [blame] | 449 | td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM]; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 450 | } |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * We need to do direct/raw ios to the device, to avoid getting |
| 454 | * read-ahead in our way. |
| 455 | */ |
| 456 | td->o.odirect = 1; |
| 457 | |
Jens Axboe | eb5fdcf | 2014-04-08 13:43:19 -0600 | [diff] [blame] | 458 | /* |
| 459 | * we don't know if this option was set or not. it defaults to 1, |
| 460 | * so we'll just guess that we should override it if it's still 1 |
| 461 | */ |
Jens Axboe | 15ed5ae | 2014-11-21 13:49:00 -0700 | [diff] [blame] | 462 | if (td->o.iodepth == 1) |
| 463 | td->o.iodepth = td->o.iodepth_low = depth; |
Jens Axboe | eb5fdcf | 2014-04-08 13:43:19 -0600 | [diff] [blame] | 464 | |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 465 | return 0; |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 466 | err: |
| 467 | close(fd); |
Jens Axboe | 38470f8 | 2007-05-16 09:26:23 +0200 | [diff] [blame] | 468 | fifo_free(fifo); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 469 | return 1; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 470 | } |