blob: 3c23b973d971fb0f75011c54f7d616c64a42e931 [file] [log] [blame]
Jens Axboefb7b71a2007-05-15 08:44:04 +02001/*
2 * blktrace support code for fio
3 */
4#include <stdio.h>
5#include <stdlib.h>
Jens Axboe5e6c2062007-05-16 14:40:29 +02006#include <sys/stat.h>
7#include <dirent.h>
Jens Axboe8c1fdf02007-05-15 11:54:21 +02008
Jens Axboe01743ee2008-06-02 12:19:19 +02009#include "flist.h"
Jens Axboefb7b71a2007-05-15 08:44:04 +020010#include "fio.h"
11#include "blktrace_api.h"
12
Jens Axboe2da7df12010-08-01 21:27:28 +020013#define TRACE_FIFO_SIZE 8192
Jens Axboee2887562007-05-16 09:25:09 +020014
15/*
16 * fifo refill frontend, to avoid reading data in trace sized bites
17 */
18static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
19{
20 char buf[TRACE_FIFO_SIZE];
Jens Axboef12b3232007-05-16 12:16:44 +020021 unsigned int total;
Jens Axboee2887562007-05-16 09:25:09 +020022 int ret;
23
Jens Axboef12b3232007-05-16 12:16:44 +020024 total = sizeof(buf);
25 if (total > fifo_room(fifo))
26 total = fifo_room(fifo);
Jens Axboee2887562007-05-16 09:25:09 +020027
Jens Axboef12b3232007-05-16 12:16:44 +020028 ret = read(fd, buf, total);
29 if (ret < 0) {
30 td_verror(td, errno, "read blktrace file");
31 return -1;
Jens Axboee2887562007-05-16 09:25:09 +020032 }
33
Jens Axboef12b3232007-05-16 12:16:44 +020034 if (ret > 0)
35 ret = fifo_put(fifo, buf, ret);
36
Jens Axboebd6f78b2008-02-01 20:27:52 +010037 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
Jens Axboef12b3232007-05-16 12:16:44 +020038 return ret;
Jens Axboee2887562007-05-16 09:25:09 +020039}
40
41/*
42 * Retrieve 'len' bytes from the fifo, refilling if necessary.
43 */
44static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
45 void *buf, unsigned int len)
46{
Jens Axboef12b3232007-05-16 12:16:44 +020047 if (fifo_len(fifo) < len) {
48 int ret = refill_fifo(td, fifo, fd);
Jens Axboee2887562007-05-16 09:25:09 +020049
Jens Axboef12b3232007-05-16 12:16:44 +020050 if (ret < 0)
51 return ret;
52 }
Jens Axboee2887562007-05-16 09:25:09 +020053
54 return fifo_get(fifo, buf, len);
55}
56
Jens Axboe8c1fdf02007-05-15 11:54:21 +020057/*
58 * Just discard the pdu by seeking past it.
59 */
Jens Axboef12b3232007-05-16 12:16:44 +020060static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
61 struct blk_io_trace *t)
Jens Axboefb7b71a2007-05-15 08:44:04 +020062{
63 if (t->pdu_len == 0)
64 return 0;
65
Jens Axboebd6f78b2008-02-01 20:27:52 +010066 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
Jens Axboef12b3232007-05-16 12:16:44 +020067 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
Jens Axboefb7b71a2007-05-15 08:44:04 +020068}
69
Jens Axboe8c1fdf02007-05-15 11:54:21 +020070/*
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 Axboed95b34a2013-11-21 09:55:49 -070074int is_blktrace(const char *filename, int *need_swap)
Jens Axboefb7b71a2007-05-15 08:44:04 +020075{
76 struct blk_io_trace t;
77 int fd, ret;
78
79 fd = open(filename, O_RDONLY);
Jens Axboe4dced402007-07-23 14:16:55 +020080 if (fd < 0)
Jens Axboefb7b71a2007-05-15 08:44:04 +020081 return 0;
Jens Axboefb7b71a2007-05-15 08:44:04 +020082
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
Jens Axboed95b34a2013-11-21 09:55:49 -070094 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
95 *need_swap = 0;
Jens Axboefb7b71a2007-05-15 08:44:04 +020096 return 1;
Jens Axboed95b34a2013-11-21 09:55:49 -070097 }
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 }
Jens Axboefb7b71a2007-05-15 08:44:04 +0200107
108 return 0;
109}
110
David Nellansd1c46c02010-08-31 21:20:47 +0200111static int lookup_device(struct thread_data *td, char *path, unsigned int maj,
112 unsigned int min)
Jens Axboe5e6c2062007-05-16 14:40:29 +0200113{
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
Bruce Cranb9fd7882012-02-20 17:01:46 +0000129 sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200130 if (lstat(full_path, &st) == -1) {
131 perror("lstat");
132 break;
133 }
134
135 if (S_ISDIR(st.st_mode)) {
David Nellansd1c46c02010-08-31 21:20:47 +0200136 found = lookup_device(td, full_path, maj, min);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200137 if (found) {
138 strcpy(path, full_path);
139 break;
140 }
141 }
142
143 if (!S_ISBLK(st.st_mode))
144 continue;
145
David Nellansd1c46c02010-08-31 21:20:47 +0200146 /*
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"
Jens Axboe25a4e782014-04-11 00:08:41 +0200153 " with: %s\n", maj, min,
David Nellansd1c46c02010-08-31 21:20:47 +0200154 td->o.replay_redirect);
155 strcpy(path, td->o.replay_redirect);
156 found = 1;
157 break;
158 }
159
Jens Axboe5e6c2062007-05-16 14:40:29 +0200160 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100161 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200162 strcpy(path, full_path);
163 found = 1;
164 break;
165 }
166 }
167
168 closedir(D);
169 return found;
170}
171
Jens Axboec69aa912007-05-22 15:51:50 +0200172#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))
Jens Axboeeeb9c2a2007-05-16 18:28:47 +0200176
Shaohua Li89ac1d42012-06-11 08:56:32 +0200177static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
Jens Axboe691c8fb2008-03-07 14:26:26 +0100178{
179 struct io_piece *ipo;
180
181 ipo = calloc(1, sizeof(*ipo));
Jens Axboe0d29de82010-09-01 13:54:15 +0200182 init_ipo(ipo);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100183
184 ipo->ddir = DDIR_INVAL;
185 ipo->fileno = fileno;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200186 ipo->file_action = action;
Jens Axboe01743ee2008-06-02 12:19:19 +0200187 flist_add_tail(&ipo->list, &td->io_log_list);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100188}
189
Shaohua Li89ac1d42012-06-11 08:56:32 +0200190static int trace_add_file(struct thread_data *td, __u32 device)
Jens Axboe5e6c2062007-05-16 14:40:29 +0200191{
Shaohua Li89ac1d42012-06-11 08:56:32 +0200192 static unsigned int last_maj, last_min, last_fileno;
Jens Axboec69aa912007-05-22 15:51:50 +0200193 unsigned int maj = FMAJOR(device);
194 unsigned int min = FMINOR(device);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200195 struct fio_file *f;
196 char dev[256];
197 unsigned int i;
198
199 if (last_maj == maj && last_min == min)
Shaohua Li89ac1d42012-06-11 08:56:32 +0200200 return last_fileno;
Jens Axboe5e6c2062007-05-16 14:40:29 +0200201
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)
Shaohua Li89ac1d42012-06-11 08:56:32 +0200209 if (f->major == maj && f->minor == min) {
210 last_fileno = f->fileno;
211 return last_fileno;
212 }
Jens Axboe5e6c2062007-05-16 14:40:29 +0200213
214 strcpy(dev, "/dev");
David Nellansd1c46c02010-08-31 21:20:47 +0200215 if (lookup_device(td, dev, maj, min)) {
Jens Axboe691c8fb2008-03-07 14:26:26 +0100216 int fileno;
217
Jens Axboebd6f78b2008-02-01 20:27:52 +0100218 dprint(FD_BLKTRACE, "add devices %s\n", dev);
Jens Axboe49ffb4a2010-08-24 15:01:37 +0200219 fileno = add_file_exclusive(td, dev);
Jens Axboeb53f2c52014-04-08 21:07:12 -0600220 td->o.open_files++;
Jens Axboe5903e7b2014-02-26 13:42:13 -0800221 td->files[fileno]->major = maj;
222 td->files[fileno]->minor = min;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200223 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
224 last_fileno = fileno;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100225 }
Jens Axboef01b34a2013-11-21 11:13:12 -0700226
Shaohua Li89ac1d42012-06-11 08:56:32 +0200227 return last_fileno;
Jens Axboe5e6c2062007-05-16 14:40:29 +0200228}
229
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200230/*
231 * Store blk_io_trace data in an ipo for later retrieval.
232 */
Jens Axboefdefd982007-05-15 10:12:26 +0200233static void store_ipo(struct thread_data *td, unsigned long long offset,
Shaohua Li89ac1d42012-06-11 08:56:32 +0200234 unsigned int bytes, int rw, unsigned long long ttime,
235 int fileno)
Jens Axboefdefd982007-05-15 10:12:26 +0200236{
237 struct io_piece *ipo = malloc(sizeof(*ipo));
238
Jens Axboe0d29de82010-09-01 13:54:15 +0200239 init_ipo(ipo);
240
Jens Axboea2eea812007-05-15 13:10:41 +0200241 /*
242 * the 512 is wrong here, it should be the hardware sector size...
243 */
244 ipo->offset = offset * 512;
Jens Axboefdefd982007-05-15 10:12:26 +0200245 ipo->len = bytes;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200246 ipo->delay = ttime / 1000;
Jens Axboefdefd982007-05-15 10:12:26 +0200247 if (rw)
248 ipo->ddir = DDIR_WRITE;
249 else
250 ipo->ddir = DDIR_READ;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200251 ipo->fileno = fileno;
Jens Axboefdefd982007-05-15 10:12:26 +0200252
Jens Axboebd6f78b2008-02-01 20:27:52 +0100253 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
254 ipo->ddir, ipo->offset,
255 ipo->len, ipo->delay);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100256 queue_io_piece(td, ipo);
Jens Axboefdefd982007-05-15 10:12:26 +0200257}
258
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200259static void handle_trace_notify(struct blk_io_trace *t)
Jens Axboe691c8fb2008-03-07 14:26:26 +0100260{
261 switch (t->action) {
262 case BLK_TN_PROCESS:
Jens Axboe5bca6f92014-07-14 15:07:11 +0200263 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
Jens Axboed95b34a2013-11-21 09:55:49 -0700264 t->action, t->pid);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100265 break;
266 case BLK_TN_TIMESTAMP:
Jens Axboe5bca6f92014-07-14 15:07:11 +0200267 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
Jens Axboed95b34a2013-11-21 09:55:49 -0700268 t->action, t->pid);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100269 break;
Jens Axboeff58fce2010-08-25 12:02:08 +0200270 case BLK_TN_MESSAGE:
271 break;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100272 default:
273 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
274 break;
275 }
276}
277
Jens Axboe5bca6f92014-07-14 15:07:11 +0200278static void handle_trace_discard(struct thread_data *td,
279 struct blk_io_trace *t,
280 unsigned long long ttime,
281 unsigned long *ios, unsigned int *bs)
Jens Axboeff58fce2010-08-25 12:02:08 +0200282{
283 struct io_piece *ipo = malloc(sizeof(*ipo));
Shaohua Li89ac1d42012-06-11 08:56:32 +0200284 int fileno;
Jens Axboeff58fce2010-08-25 12:02:08 +0200285
Jens Axboe0d29de82010-09-01 13:54:15 +0200286 init_ipo(ipo);
Shaohua Li89ac1d42012-06-11 08:56:32 +0200287 fileno = trace_add_file(td, t->device);
Jens Axboeff58fce2010-08-25 12:02:08 +0200288
Jens Axboe5bca6f92014-07-14 15:07:11 +0200289 ios[DDIR_TRIM]++;
290 if (t->bytes > bs[DDIR_TRIM])
291 bs[DDIR_TRIM] = t->bytes;
292
Jens Axboeff58fce2010-08-25 12:02:08 +0200293 td->o.size += t->bytes;
294
295 memset(ipo, 0, sizeof(*ipo));
296 INIT_FLIST_HEAD(&ipo->list);
297
298 /*
299 * the 512 is wrong here, it should be the hardware sector size...
300 */
301 ipo->offset = t->sector * 512;
302 ipo->len = t->bytes;
303 ipo->delay = ttime / 1000;
304 ipo->ddir = DDIR_TRIM;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200305 ipo->fileno = fileno;
Jens Axboeff58fce2010-08-25 12:02:08 +0200306
307 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
308 ipo->offset, ipo->len,
309 ipo->delay);
310 queue_io_piece(td, ipo);
311}
312
Jens Axboe691c8fb2008-03-07 14:26:26 +0100313static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
314 unsigned long long ttime, unsigned long *ios,
315 unsigned int *bs)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200316{
Jens Axboefdefd982007-05-15 10:12:26 +0200317 int rw;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200318 int fileno;
Jens Axboefdefd982007-05-15 10:12:26 +0200319
Shaohua Li89ac1d42012-06-11 08:56:32 +0200320 fileno = trace_add_file(td, t->device);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200321
Jens Axboee7a7d702007-05-15 10:13:04 +0200322 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200323
324 if (t->bytes > bs[rw])
325 bs[rw] = t->bytes;
326
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200327 ios[rw]++;
Jens Axboe6df8ada2007-05-15 13:23:19 +0200328 td->o.size += t->bytes;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200329 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200330}
331
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200332/*
Jens Axboe691c8fb2008-03-07 14:26:26 +0100333 * We only care for queue traces, most of the others are side effects
334 * due to internal workings of the block layer.
335 */
336static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
Jens Axboe5bca6f92014-07-14 15:07:11 +0200337 unsigned long *ios, unsigned int *bs)
Jens Axboe691c8fb2008-03-07 14:26:26 +0100338{
Jens Axboe5bca6f92014-07-14 15:07:11 +0200339 static unsigned long long last_ttime;
340 unsigned long long delay;
341
Jens Axboe691c8fb2008-03-07 14:26:26 +0100342 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
343 return;
Jens Axboe5bca6f92014-07-14 15:07:11 +0200344
345 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
346 if (!last_ttime || td->o.no_stall) {
347 last_ttime = t->time;
348 delay = 0;
349 } else {
350 delay = t->time - last_ttime;
351 last_ttime = t->time;
352 }
353 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100354
355 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
Jens Axboeaec2de22008-04-24 12:44:42 +0200356 handle_trace_notify(t);
Jens Axboeff58fce2010-08-25 12:02:08 +0200357 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
Jens Axboe5bca6f92014-07-14 15:07:11 +0200358 handle_trace_discard(td, t, delay, ios, bs);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100359 else
Jens Axboe5bca6f92014-07-14 15:07:11 +0200360 handle_trace_fs(td, t, delay, ios, bs);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100361}
362
Jens Axboed95b34a2013-11-21 09:55:49 -0700363static void byteswap_trace(struct blk_io_trace *t)
364{
365 t->magic = fio_swap32(t->magic);
366 t->sequence = fio_swap32(t->sequence);
367 t->time = fio_swap64(t->time);
368 t->sector = fio_swap64(t->sector);
369 t->bytes = fio_swap32(t->bytes);
370 t->action = fio_swap32(t->action);
371 t->pid = fio_swap32(t->pid);
372 t->device = fio_swap32(t->device);
373 t->cpu = fio_swap32(t->cpu);
374 t->error = fio_swap16(t->error);
375 t->pdu_len = fio_swap16(t->pdu_len);
376}
377
Jens Axboe5bca6f92014-07-14 15:07:11 +0200378static int t_is_write(struct blk_io_trace *t)
379{
380 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
381}
382
Jens Axboe691c8fb2008-03-07 14:26:26 +0100383/*
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200384 * Load a blktrace file by reading all the blk_io_trace entries, and storing
385 * them as io_pieces like the fio text version would do.
386 */
Jens Axboed95b34a2013-11-21 09:55:49 -0700387int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200388{
389 struct blk_io_trace t;
Jens Axboe5bca6f92014-07-14 15:07:11 +0200390 unsigned long ios[DDIR_RWDIR_CNT], skipped_writes;
391 unsigned int rw_bs[DDIR_RWDIR_CNT];
Jens Axboee2887562007-05-16 09:25:09 +0200392 struct fifo *fifo;
Jens Axboe5903e7b2014-02-26 13:42:13 -0800393 int fd, i, old_state;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200394 struct fio_file *f;
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600395 int this_depth, depth;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200396
397 fd = open(filename, O_RDONLY);
398 if (fd < 0) {
399 td_verror(td, errno, "open blktrace file");
400 return 1;
401 }
402
Jens Axboee2887562007-05-16 09:25:09 +0200403 fifo = fifo_alloc(TRACE_FIFO_SIZE);
404
Jens Axboe8edd9732014-03-01 08:24:03 -0700405 old_state = td_bump_runstate(td, TD_SETTING_UP);
Jens Axboe5903e7b2014-02-26 13:42:13 -0800406
Jens Axboe6df8ada2007-05-15 13:23:19 +0200407 td->o.size = 0;
408
Jens Axboed84f8d42007-05-16 08:47:46 +0200409 ios[0] = ios[1] = 0;
410 rw_bs[0] = rw_bs[1] = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +0200411 skipped_writes = 0;
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600412 this_depth = depth = 0;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200413 do {
Jens Axboee2887562007-05-16 09:25:09 +0200414 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
Jens Axboefb7b71a2007-05-15 08:44:04 +0200415
Jens Axboee2887562007-05-16 09:25:09 +0200416 if (ret < 0)
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200417 goto err;
Jens Axboee2887562007-05-16 09:25:09 +0200418 else if (!ret)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200419 break;
Jens Axboee2887562007-05-16 09:25:09 +0200420 else if (ret < (int) sizeof(t)) {
421 log_err("fio: short fifo get\n");
422 break;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200423 }
424
Jens Axboed95b34a2013-11-21 09:55:49 -0700425 if (need_swap)
426 byteswap_trace(&t);
427
Jens Axboefb7b71a2007-05-15 08:44:04 +0200428 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100429 log_err("fio: bad magic in blktrace data: %x\n",
430 t.magic);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200431 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200432 }
433 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100434 log_err("fio: bad blktrace version %d\n",
435 t.magic & 0xff);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200436 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200437 }
Jens Axboef12b3232007-05-16 12:16:44 +0200438 ret = discard_pdu(td, fifo, fd, &t);
439 if (ret < 0) {
Jens Axboefb7b71a2007-05-15 08:44:04 +0200440 td_verror(td, ret, "blktrace lseek");
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200441 goto err;
Jens Axboef12b3232007-05-16 12:16:44 +0200442 } else if (t.pdu_len != ret) {
443 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
444 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200445 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100446 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600447 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
448 this_depth++;
449 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE) {
450 depth = max(depth, this_depth);
451 this_depth = 0;
452 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100453
Jens Axboe5bca6f92014-07-14 15:07:11 +0200454 if (t_is_write(&t) && read_only) {
Jens Axboe691c8fb2008-03-07 14:26:26 +0100455 skipped_writes++;
Jens Axboe5bca6f92014-07-14 15:07:11 +0200456 continue;
David Nellans64bbb862010-08-24 22:13:30 +0200457 }
Jens Axboea6edd632008-03-07 21:41:54 +0100458 }
Jens Axboe5bca6f92014-07-14 15:07:11 +0200459
460 handle_trace(td, &t, ios, rw_bs);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200461 } while (1);
462
Jens Axboe85a47ca2012-06-11 13:19:13 +0200463 for (i = 0; i < td->files_index; i++) {
Jens Axboef01b34a2013-11-21 11:13:12 -0700464 f = td->files[i];
Shaohua Li89ac1d42012-06-11 08:56:32 +0200465 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
Jens Axboe85a47ca2012-06-11 13:19:13 +0200466 }
Shaohua Li89ac1d42012-06-11 08:56:32 +0200467
Jens Axboe38470f82007-05-16 09:26:23 +0200468 fifo_free(fifo);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200469 close(fd);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200470
Jens Axboe8edd9732014-03-01 08:24:03 -0700471 td_restore_runstate(td, old_state);
Jens Axboe5903e7b2014-02-26 13:42:13 -0800472
Jens Axboef01b34a2013-11-21 11:13:12 -0700473 if (!td->files_index) {
474 log_err("fio: did not find replay device(s)\n");
475 return 1;
476 }
477
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600478 /*
479 * For stacked devices, we don't always get a COMPLETE event so
480 * the depth grows to insane values. Limit it to something sane(r).
481 */
482 if (!depth || depth > 1024)
483 depth = 1024;
484
Jens Axboe4241ea82007-09-12 08:18:36 +0200485 if (skipped_writes)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100486 log_err("fio: %s skips replay of %lu writes due to read-only\n",
487 td->o.name, skipped_writes);
Jens Axboe4241ea82007-09-12 08:18:36 +0200488
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200489 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
490 log_err("fio: found no ios in blktrace data\n");
491 return 1;
Jens Axboe5bca6f92014-07-14 15:07:11 +0200492 } else if (ios[DDIR_READ] && !ios[DDIR_WRITE]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200493 td->o.td_ddir = TD_DDIR_READ;
Jens Axboed84f8d42007-05-16 08:47:46 +0200494 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
495 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200496 td->o.td_ddir = TD_DDIR_WRITE;
Jens Axboed84f8d42007-05-16 08:47:46 +0200497 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
498 } else {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200499 td->o.td_ddir = TD_DDIR_RW;
Jens Axboed84f8d42007-05-16 08:47:46 +0200500 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
501 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
Jens Axboe5bca6f92014-07-14 15:07:11 +0200502 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
Jens Axboed84f8d42007-05-16 08:47:46 +0200503 }
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200504
505 /*
506 * We need to do direct/raw ios to the device, to avoid getting
507 * read-ahead in our way.
508 */
509 td->o.odirect = 1;
510
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600511 /*
512 * we don't know if this option was set or not. it defaults to 1,
513 * so we'll just guess that we should override it if it's still 1
514 */
515 if (td->o.iodepth != 1)
516 td->o.iodepth = depth;
517
Jens Axboefb7b71a2007-05-15 08:44:04 +0200518 return 0;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200519err:
520 close(fd);
Jens Axboe38470f82007-05-16 09:26:23 +0200521 fifo_free(fifo);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200522 return 1;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200523}