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" |
| 12 | |
Jens Axboe | 2da7df1 | 2010-08-01 21:27:28 +0200 | [diff] [blame] | 13 | #define TRACE_FIFO_SIZE 8192 |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 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]; |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 21 | unsigned int total; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 22 | int ret; |
| 23 | |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 24 | total = sizeof(buf); |
| 25 | if (total > fifo_room(fifo)) |
| 26 | total = fifo_room(fifo); |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 27 | |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 28 | ret = read(fd, buf, total); |
| 29 | if (ret < 0) { |
| 30 | td_verror(td, errno, "read blktrace file"); |
| 31 | return -1; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 34 | if (ret > 0) |
| 35 | ret = fifo_put(fifo, buf, ret); |
| 36 | |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 37 | dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret); |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 38 | return ret; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 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 | { |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 47 | if (fifo_len(fifo) < len) { |
| 48 | int ret = refill_fifo(td, fifo, fd); |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 49 | |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 50 | if (ret < 0) |
| 51 | return ret; |
| 52 | } |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 53 | |
| 54 | return fifo_get(fifo, buf, len); |
| 55 | } |
| 56 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 57 | /* |
| 58 | * Just discard the pdu by seeking past it. |
| 59 | */ |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 60 | static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd, |
| 61 | struct blk_io_trace *t) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 62 | { |
| 63 | if (t->pdu_len == 0) |
| 64 | return 0; |
| 65 | |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 66 | dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len); |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 67 | return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 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 | */ |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 74 | int is_blktrace(const char *filename) |
| 75 | { |
| 76 | struct blk_io_trace t; |
| 77 | int fd, ret; |
| 78 | |
| 79 | fd = open(filename, O_RDONLY); |
Jens Axboe | 4dced40 | 2007-07-23 14:16:55 +0200 | [diff] [blame] | 80 | if (fd < 0) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 81 | return 0; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 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 | |
| 94 | if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) |
| 95 | return 1; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
David Nellans | d1c46c0 | 2010-08-31 21:20:47 +0200 | [diff] [blame] | 100 | static int lookup_device(struct thread_data *td, char *path, unsigned int maj, |
| 101 | unsigned int min) |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 102 | { |
| 103 | struct dirent *dir; |
| 104 | struct stat st; |
| 105 | int found = 0; |
| 106 | DIR *D; |
| 107 | |
| 108 | D = opendir(path); |
| 109 | if (!D) |
| 110 | return 0; |
| 111 | |
| 112 | while ((dir = readdir(D)) != NULL) { |
| 113 | char full_path[256]; |
| 114 | |
| 115 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 116 | continue; |
| 117 | |
Bruce Cran | b9fd788 | 2012-02-20 17:01:46 +0000 | [diff] [blame] | 118 | sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name); |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 119 | if (lstat(full_path, &st) == -1) { |
| 120 | perror("lstat"); |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | if (S_ISDIR(st.st_mode)) { |
David Nellans | d1c46c0 | 2010-08-31 21:20:47 +0200 | [diff] [blame] | 125 | found = lookup_device(td, full_path, maj, min); |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 126 | if (found) { |
| 127 | strcpy(path, full_path); |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (!S_ISBLK(st.st_mode)) |
| 133 | continue; |
| 134 | |
David Nellans | d1c46c0 | 2010-08-31 21:20:47 +0200 | [diff] [blame] | 135 | /* |
| 136 | * If replay_redirect is set then always return this device |
| 137 | * upon lookup which overrides the device lookup based on |
| 138 | * major minor in the actual blktrace |
| 139 | */ |
| 140 | if (td->o.replay_redirect) { |
| 141 | dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden" |
| 142 | " with: %s", maj, min, |
| 143 | td->o.replay_redirect); |
| 144 | strcpy(path, td->o.replay_redirect); |
| 145 | found = 1; |
| 146 | break; |
| 147 | } |
| 148 | |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 149 | if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 150 | dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min); |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 151 | strcpy(path, full_path); |
| 152 | found = 1; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | closedir(D); |
| 158 | return found; |
| 159 | } |
| 160 | |
Jens Axboe | c69aa91 | 2007-05-22 15:51:50 +0200 | [diff] [blame] | 161 | #define FMINORBITS 20 |
| 162 | #define FMINORMASK ((1U << FMINORBITS) - 1) |
| 163 | #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS)) |
| 164 | #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK)) |
Jens Axboe | eeb9c2a | 2007-05-16 18:28:47 +0200 | [diff] [blame] | 165 | |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 166 | 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] | 167 | { |
| 168 | struct io_piece *ipo; |
| 169 | |
| 170 | ipo = calloc(1, sizeof(*ipo)); |
Jens Axboe | 0d29de8 | 2010-09-01 13:54:15 +0200 | [diff] [blame] | 171 | init_ipo(ipo); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 172 | |
| 173 | ipo->ddir = DDIR_INVAL; |
| 174 | ipo->fileno = fileno; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 175 | ipo->file_action = action; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 176 | flist_add_tail(&ipo->list, &td->io_log_list); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 179 | static int trace_add_file(struct thread_data *td, __u32 device) |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 180 | { |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 181 | static unsigned int last_maj, last_min, last_fileno; |
Jens Axboe | c69aa91 | 2007-05-22 15:51:50 +0200 | [diff] [blame] | 182 | unsigned int maj = FMAJOR(device); |
| 183 | unsigned int min = FMINOR(device); |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 184 | struct fio_file *f; |
| 185 | char dev[256]; |
| 186 | unsigned int i; |
| 187 | |
| 188 | if (last_maj == maj && last_min == min) |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 189 | return last_fileno; |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 190 | |
| 191 | last_maj = maj; |
| 192 | last_min = min; |
| 193 | |
| 194 | /* |
| 195 | * check for this file in our list |
| 196 | */ |
| 197 | for_each_file(td, f, i) |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 198 | if (f->major == maj && f->minor == min) { |
| 199 | last_fileno = f->fileno; |
| 200 | return last_fileno; |
| 201 | } |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 202 | |
| 203 | strcpy(dev, "/dev"); |
David Nellans | d1c46c0 | 2010-08-31 21:20:47 +0200 | [diff] [blame] | 204 | if (lookup_device(td, dev, maj, min)) { |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 205 | int fileno; |
| 206 | |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 207 | dprint(FD_BLKTRACE, "add devices %s\n", dev); |
Jens Axboe | 49ffb4a | 2010-08-24 15:01:37 +0200 | [diff] [blame] | 208 | fileno = add_file_exclusive(td, dev); |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 209 | trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE); |
| 210 | last_fileno = fileno; |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 211 | } |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 212 | return last_fileno; |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 215 | /* |
| 216 | * Store blk_io_trace data in an ipo for later retrieval. |
| 217 | */ |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 218 | static void store_ipo(struct thread_data *td, unsigned long long offset, |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 219 | unsigned int bytes, int rw, unsigned long long ttime, |
| 220 | int fileno) |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 221 | { |
| 222 | struct io_piece *ipo = malloc(sizeof(*ipo)); |
| 223 | |
Jens Axboe | 0d29de8 | 2010-09-01 13:54:15 +0200 | [diff] [blame] | 224 | init_ipo(ipo); |
| 225 | |
Jens Axboe | a2eea81 | 2007-05-15 13:10:41 +0200 | [diff] [blame] | 226 | /* |
| 227 | * the 512 is wrong here, it should be the hardware sector size... |
| 228 | */ |
| 229 | ipo->offset = offset * 512; |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 230 | ipo->len = bytes; |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 231 | ipo->delay = ttime / 1000; |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 232 | if (rw) |
| 233 | ipo->ddir = DDIR_WRITE; |
| 234 | else |
| 235 | ipo->ddir = DDIR_READ; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 236 | ipo->fileno = fileno; |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 237 | |
Jens Axboe | bd6f78b | 2008-02-01 20:27:52 +0100 | [diff] [blame] | 238 | dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n", |
| 239 | ipo->ddir, ipo->offset, |
| 240 | ipo->len, ipo->delay); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 241 | queue_io_piece(td, ipo); |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 244 | static void handle_trace_notify(struct blk_io_trace *t) |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 245 | { |
| 246 | switch (t->action) { |
| 247 | case BLK_TN_PROCESS: |
| 248 | printf("got process notify: %x, %d\n", t->action, t->pid); |
| 249 | break; |
| 250 | case BLK_TN_TIMESTAMP: |
| 251 | printf("got timestamp notify: %x, %d\n", t->action, t->pid); |
| 252 | break; |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 253 | case BLK_TN_MESSAGE: |
| 254 | break; |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 255 | default: |
| 256 | dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 261 | static void handle_trace_discard(struct thread_data *td, struct blk_io_trace *t, |
| 262 | unsigned long long ttime, unsigned long *ios) |
| 263 | { |
| 264 | struct io_piece *ipo = malloc(sizeof(*ipo)); |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 265 | int fileno; |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 266 | |
Jens Axboe | 0d29de8 | 2010-09-01 13:54:15 +0200 | [diff] [blame] | 267 | init_ipo(ipo); |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 268 | fileno = trace_add_file(td, t->device); |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 269 | |
| 270 | ios[DDIR_WRITE]++; |
| 271 | td->o.size += t->bytes; |
| 272 | |
| 273 | memset(ipo, 0, sizeof(*ipo)); |
| 274 | INIT_FLIST_HEAD(&ipo->list); |
| 275 | |
| 276 | /* |
| 277 | * the 512 is wrong here, it should be the hardware sector size... |
| 278 | */ |
| 279 | ipo->offset = t->sector * 512; |
| 280 | ipo->len = t->bytes; |
| 281 | ipo->delay = ttime / 1000; |
| 282 | ipo->ddir = DDIR_TRIM; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 283 | ipo->fileno = fileno; |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 284 | |
| 285 | dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n", |
| 286 | ipo->offset, ipo->len, |
| 287 | ipo->delay); |
| 288 | queue_io_piece(td, ipo); |
| 289 | } |
| 290 | |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 291 | static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t, |
| 292 | unsigned long long ttime, unsigned long *ios, |
| 293 | unsigned int *bs) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 294 | { |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 295 | int rw; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 296 | int fileno; |
Jens Axboe | fdefd98 | 2007-05-15 10:12:26 +0200 | [diff] [blame] | 297 | |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 298 | fileno = trace_add_file(td, t->device); |
Jens Axboe | 5e6c206 | 2007-05-16 14:40:29 +0200 | [diff] [blame] | 299 | |
Jens Axboe | e7a7d70 | 2007-05-15 10:13:04 +0200 | [diff] [blame] | 300 | rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 301 | |
| 302 | if (t->bytes > bs[rw]) |
| 303 | bs[rw] = t->bytes; |
| 304 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 305 | ios[rw]++; |
Jens Axboe | 6df8ada | 2007-05-15 13:23:19 +0200 | [diff] [blame] | 306 | td->o.size += t->bytes; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 307 | store_ipo(td, t->sector, t->bytes, rw, ttime, fileno); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 310 | /* |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 311 | * We only care for queue traces, most of the others are side effects |
| 312 | * due to internal workings of the block layer. |
| 313 | */ |
| 314 | static void handle_trace(struct thread_data *td, struct blk_io_trace *t, |
| 315 | unsigned long long ttime, unsigned long *ios, |
| 316 | unsigned int *bs) |
| 317 | { |
| 318 | if ((t->action & 0xffff) != __BLK_TA_QUEUE) |
| 319 | return; |
| 320 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) |
| 321 | return; |
| 322 | |
| 323 | if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY)) |
Jens Axboe | aec2de2 | 2008-04-24 12:44:42 +0200 | [diff] [blame] | 324 | handle_trace_notify(t); |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 325 | else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD)) |
| 326 | handle_trace_discard(td, t, ttime, ios); |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 327 | else |
| 328 | handle_trace_fs(td, t, ttime, ios, bs); |
| 329 | } |
| 330 | |
| 331 | /* |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 332 | * Load a blktrace file by reading all the blk_io_trace entries, and storing |
| 333 | * them as io_pieces like the fio text version would do. |
| 334 | */ |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 335 | int load_blktrace(struct thread_data *td, const char *filename) |
| 336 | { |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 337 | unsigned long long ttime, delay; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 338 | struct blk_io_trace t; |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 339 | unsigned long ios[2], skipped_writes; |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 340 | unsigned int cpu; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 341 | unsigned int rw_bs[2]; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 342 | struct fifo *fifo; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 343 | int fd, i; |
| 344 | struct fio_file *f; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 345 | |
| 346 | fd = open(filename, O_RDONLY); |
| 347 | if (fd < 0) { |
| 348 | td_verror(td, errno, "open blktrace file"); |
| 349 | return 1; |
| 350 | } |
| 351 | |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 352 | fifo = fifo_alloc(TRACE_FIFO_SIZE); |
| 353 | |
Jens Axboe | 6df8ada | 2007-05-15 13:23:19 +0200 | [diff] [blame] | 354 | td->o.size = 0; |
| 355 | |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 356 | cpu = 0; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 357 | ttime = 0; |
| 358 | ios[0] = ios[1] = 0; |
| 359 | rw_bs[0] = rw_bs[1] = 0; |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 360 | skipped_writes = 0; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 361 | do { |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 362 | int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t)); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 363 | |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 364 | if (ret < 0) |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 365 | goto err; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 366 | else if (!ret) |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 367 | break; |
Jens Axboe | e288756 | 2007-05-16 09:25:09 +0200 | [diff] [blame] | 368 | else if (ret < (int) sizeof(t)) { |
| 369 | log_err("fio: short fifo get\n"); |
| 370 | break; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 374 | log_err("fio: bad magic in blktrace data: %x\n", |
| 375 | t.magic); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 376 | goto err; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 377 | } |
| 378 | if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 379 | log_err("fio: bad blktrace version %d\n", |
| 380 | t.magic & 0xff); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 381 | goto err; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 382 | } |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 383 | ret = discard_pdu(td, fifo, fd, &t); |
| 384 | if (ret < 0) { |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 385 | td_verror(td, ret, "blktrace lseek"); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 386 | goto err; |
Jens Axboe | f12b323 | 2007-05-16 12:16:44 +0200 | [diff] [blame] | 387 | } else if (t.pdu_len != ret) { |
| 388 | log_err("fio: discarded %d of %d\n", ret, t.pdu_len); |
| 389 | goto err; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 390 | } |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 391 | if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) { |
| 392 | if (!ttime) { |
| 393 | ttime = t.time; |
| 394 | cpu = t.cpu; |
| 395 | } |
| 396 | |
| 397 | delay = 0; |
| 398 | if (cpu == t.cpu) |
| 399 | delay = t.time - ttime; |
| 400 | if ((t.action & BLK_TC_ACT(BLK_TC_WRITE)) && read_only) |
| 401 | skipped_writes++; |
David Nellans | 64bbb86 | 2010-08-24 22:13:30 +0200 | [diff] [blame] | 402 | else { |
| 403 | /* |
| 404 | * set delay to zero if no_stall enabled for |
| 405 | * fast replay |
| 406 | */ |
| 407 | if (td->o.no_stall) |
| 408 | delay = 0; |
| 409 | |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 410 | handle_trace(td, &t, delay, ios, rw_bs); |
David Nellans | 64bbb86 | 2010-08-24 22:13:30 +0200 | [diff] [blame] | 411 | } |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 412 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 413 | ttime = t.time; |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 414 | cpu = t.cpu; |
Jens Axboe | a6edd63 | 2008-03-07 21:41:54 +0100 | [diff] [blame] | 415 | } else { |
| 416 | delay = 0; |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 417 | handle_trace(td, &t, delay, ios, rw_bs); |
Jens Axboe | a6edd63 | 2008-03-07 21:41:54 +0100 | [diff] [blame] | 418 | } |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 419 | } while (1); |
| 420 | |
Jens Axboe | 85a47ca | 2012-06-11 13:19:13 +0200 | [diff] [blame] | 421 | for (i = 0; i < td->files_index; i++) { |
| 422 | f= td->files[i]; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 423 | trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE); |
Jens Axboe | 85a47ca | 2012-06-11 13:19:13 +0200 | [diff] [blame] | 424 | } |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 425 | |
Jens Axboe | 38470f8 | 2007-05-16 09:26:23 +0200 | [diff] [blame] | 426 | fifo_free(fifo); |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 427 | close(fd); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 428 | |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 429 | if (skipped_writes) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 430 | log_err("fio: %s skips replay of %lu writes due to read-only\n", |
| 431 | td->o.name, skipped_writes); |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 432 | |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 433 | if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) { |
| 434 | log_err("fio: found no ios in blktrace data\n"); |
| 435 | return 1; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 436 | } else if (ios[DDIR_READ] && !ios[DDIR_READ]) { |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 437 | td->o.td_ddir = TD_DDIR_READ; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 438 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
| 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_WRITE; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 441 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; |
| 442 | } else { |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 443 | td->o.td_ddir = TD_DDIR_RW; |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 444 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
| 445 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; |
| 446 | } |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 447 | |
| 448 | /* |
| 449 | * We need to do direct/raw ios to the device, to avoid getting |
| 450 | * read-ahead in our way. |
| 451 | */ |
| 452 | td->o.odirect = 1; |
| 453 | |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 454 | return 0; |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 455 | err: |
| 456 | close(fd); |
Jens Axboe | 38470f8 | 2007-05-16 09:26:23 +0200 | [diff] [blame] | 457 | fifo_free(fifo); |
Jens Axboe | 8c1fdf0 | 2007-05-15 11:54:21 +0200 | [diff] [blame] | 458 | return 1; |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 459 | } |