blob: 29eed50c391c422da566eb063cb2bc4863bd19a1 [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 Axboed95b34a2013-11-21 09:55:49 -0700263 log_info("blktrace: got process notify: %x, %d\n",
264 t->action, t->pid);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100265 break;
266 case BLK_TN_TIMESTAMP:
Jens Axboed95b34a2013-11-21 09:55:49 -0700267 log_info("blktrace: got timestamp notify: %x, %d\n",
268 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 Axboeff58fce2010-08-25 12:02:08 +0200278static void handle_trace_discard(struct thread_data *td, struct blk_io_trace *t,
279 unsigned long long ttime, unsigned long *ios)
280{
281 struct io_piece *ipo = malloc(sizeof(*ipo));
Shaohua Li89ac1d42012-06-11 08:56:32 +0200282 int fileno;
Jens Axboeff58fce2010-08-25 12:02:08 +0200283
Jens Axboe0d29de82010-09-01 13:54:15 +0200284 init_ipo(ipo);
Shaohua Li89ac1d42012-06-11 08:56:32 +0200285 fileno = trace_add_file(td, t->device);
Jens Axboeff58fce2010-08-25 12:02:08 +0200286
287 ios[DDIR_WRITE]++;
288 td->o.size += t->bytes;
289
290 memset(ipo, 0, sizeof(*ipo));
291 INIT_FLIST_HEAD(&ipo->list);
292
293 /*
294 * the 512 is wrong here, it should be the hardware sector size...
295 */
296 ipo->offset = t->sector * 512;
297 ipo->len = t->bytes;
298 ipo->delay = ttime / 1000;
299 ipo->ddir = DDIR_TRIM;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200300 ipo->fileno = fileno;
Jens Axboeff58fce2010-08-25 12:02:08 +0200301
302 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
303 ipo->offset, ipo->len,
304 ipo->delay);
305 queue_io_piece(td, ipo);
306}
307
Jens Axboe691c8fb2008-03-07 14:26:26 +0100308static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
309 unsigned long long ttime, unsigned long *ios,
310 unsigned int *bs)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200311{
Jens Axboefdefd982007-05-15 10:12:26 +0200312 int rw;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200313 int fileno;
Jens Axboefdefd982007-05-15 10:12:26 +0200314
Shaohua Li89ac1d42012-06-11 08:56:32 +0200315 fileno = trace_add_file(td, t->device);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200316
Jens Axboee7a7d702007-05-15 10:13:04 +0200317 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200318
319 if (t->bytes > bs[rw])
320 bs[rw] = t->bytes;
321
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200322 ios[rw]++;
Jens Axboe6df8ada2007-05-15 13:23:19 +0200323 td->o.size += t->bytes;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200324 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200325}
326
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200327/*
Jens Axboe691c8fb2008-03-07 14:26:26 +0100328 * We only care for queue traces, most of the others are side effects
329 * due to internal workings of the block layer.
330 */
331static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
332 unsigned long long ttime, unsigned long *ios,
333 unsigned int *bs)
334{
335 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
336 return;
337 if (t->action & BLK_TC_ACT(BLK_TC_PC))
338 return;
339
340 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
Jens Axboeaec2de22008-04-24 12:44:42 +0200341 handle_trace_notify(t);
Jens Axboeff58fce2010-08-25 12:02:08 +0200342 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
343 handle_trace_discard(td, t, ttime, ios);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100344 else
345 handle_trace_fs(td, t, ttime, ios, bs);
346}
347
Jens Axboed95b34a2013-11-21 09:55:49 -0700348static void byteswap_trace(struct blk_io_trace *t)
349{
350 t->magic = fio_swap32(t->magic);
351 t->sequence = fio_swap32(t->sequence);
352 t->time = fio_swap64(t->time);
353 t->sector = fio_swap64(t->sector);
354 t->bytes = fio_swap32(t->bytes);
355 t->action = fio_swap32(t->action);
356 t->pid = fio_swap32(t->pid);
357 t->device = fio_swap32(t->device);
358 t->cpu = fio_swap32(t->cpu);
359 t->error = fio_swap16(t->error);
360 t->pdu_len = fio_swap16(t->pdu_len);
361}
362
Jens Axboe691c8fb2008-03-07 14:26:26 +0100363/*
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200364 * Load a blktrace file by reading all the blk_io_trace entries, and storing
365 * them as io_pieces like the fio text version would do.
366 */
Jens Axboed95b34a2013-11-21 09:55:49 -0700367int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200368{
Jens Axboea61edde2007-05-15 14:29:58 +0200369 unsigned long long ttime, delay;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200370 struct blk_io_trace t;
Jens Axboe4241ea82007-09-12 08:18:36 +0200371 unsigned long ios[2], skipped_writes;
Jens Axboea61edde2007-05-15 14:29:58 +0200372 unsigned int cpu;
Jens Axboed84f8d42007-05-16 08:47:46 +0200373 unsigned int rw_bs[2];
Jens Axboee2887562007-05-16 09:25:09 +0200374 struct fifo *fifo;
Jens Axboe5903e7b2014-02-26 13:42:13 -0800375 int fd, i, old_state;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200376 struct fio_file *f;
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600377 int this_depth, depth;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200378
379 fd = open(filename, O_RDONLY);
380 if (fd < 0) {
381 td_verror(td, errno, "open blktrace file");
382 return 1;
383 }
384
Jens Axboee2887562007-05-16 09:25:09 +0200385 fifo = fifo_alloc(TRACE_FIFO_SIZE);
386
Jens Axboe8edd9732014-03-01 08:24:03 -0700387 old_state = td_bump_runstate(td, TD_SETTING_UP);
Jens Axboe5903e7b2014-02-26 13:42:13 -0800388
Jens Axboe6df8ada2007-05-15 13:23:19 +0200389 td->o.size = 0;
390
Jens Axboea61edde2007-05-15 14:29:58 +0200391 cpu = 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200392 ttime = 0;
393 ios[0] = ios[1] = 0;
394 rw_bs[0] = rw_bs[1] = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +0200395 skipped_writes = 0;
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600396 this_depth = depth = 0;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200397 do {
Jens Axboee2887562007-05-16 09:25:09 +0200398 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
Jens Axboefb7b71a2007-05-15 08:44:04 +0200399
Jens Axboee2887562007-05-16 09:25:09 +0200400 if (ret < 0)
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200401 goto err;
Jens Axboee2887562007-05-16 09:25:09 +0200402 else if (!ret)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200403 break;
Jens Axboee2887562007-05-16 09:25:09 +0200404 else if (ret < (int) sizeof(t)) {
405 log_err("fio: short fifo get\n");
406 break;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200407 }
408
Jens Axboed95b34a2013-11-21 09:55:49 -0700409 if (need_swap)
410 byteswap_trace(&t);
411
Jens Axboefb7b71a2007-05-15 08:44:04 +0200412 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100413 log_err("fio: bad magic in blktrace data: %x\n",
414 t.magic);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200415 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200416 }
417 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100418 log_err("fio: bad blktrace version %d\n",
419 t.magic & 0xff);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200420 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200421 }
Jens Axboef12b3232007-05-16 12:16:44 +0200422 ret = discard_pdu(td, fifo, fd, &t);
423 if (ret < 0) {
Jens Axboefb7b71a2007-05-15 08:44:04 +0200424 td_verror(td, ret, "blktrace lseek");
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200425 goto err;
Jens Axboef12b3232007-05-16 12:16:44 +0200426 } else if (t.pdu_len != ret) {
427 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
428 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200429 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100430 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600431 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
432 this_depth++;
433 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE) {
434 depth = max(depth, this_depth);
435 this_depth = 0;
436 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100437 if (!ttime) {
438 ttime = t.time;
439 cpu = t.cpu;
440 }
441
442 delay = 0;
443 if (cpu == t.cpu)
444 delay = t.time - ttime;
445 if ((t.action & BLK_TC_ACT(BLK_TC_WRITE)) && read_only)
446 skipped_writes++;
David Nellans64bbb862010-08-24 22:13:30 +0200447 else {
448 /*
449 * set delay to zero if no_stall enabled for
450 * fast replay
451 */
452 if (td->o.no_stall)
453 delay = 0;
454
Jens Axboe691c8fb2008-03-07 14:26:26 +0100455 handle_trace(td, &t, delay, ios, rw_bs);
David Nellans64bbb862010-08-24 22:13:30 +0200456 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100457
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200458 ttime = t.time;
Jens Axboea61edde2007-05-15 14:29:58 +0200459 cpu = t.cpu;
Jens Axboea6edd632008-03-07 21:41:54 +0100460 } else {
461 delay = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +0200462 handle_trace(td, &t, delay, ios, rw_bs);
Jens Axboea6edd632008-03-07 21:41:54 +0100463 }
Jens Axboefb7b71a2007-05-15 08:44:04 +0200464 } while (1);
465
Jens Axboe85a47ca2012-06-11 13:19:13 +0200466 for (i = 0; i < td->files_index; i++) {
Jens Axboef01b34a2013-11-21 11:13:12 -0700467 f = td->files[i];
Shaohua Li89ac1d42012-06-11 08:56:32 +0200468 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
Jens Axboe85a47ca2012-06-11 13:19:13 +0200469 }
Shaohua Li89ac1d42012-06-11 08:56:32 +0200470
Jens Axboe38470f82007-05-16 09:26:23 +0200471 fifo_free(fifo);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200472 close(fd);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200473
Jens Axboe8edd9732014-03-01 08:24:03 -0700474 td_restore_runstate(td, old_state);
Jens Axboe5903e7b2014-02-26 13:42:13 -0800475
Jens Axboef01b34a2013-11-21 11:13:12 -0700476 if (!td->files_index) {
477 log_err("fio: did not find replay device(s)\n");
478 return 1;
479 }
480
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600481 /*
482 * For stacked devices, we don't always get a COMPLETE event so
483 * the depth grows to insane values. Limit it to something sane(r).
484 */
485 if (!depth || depth > 1024)
486 depth = 1024;
487
Jens Axboe4241ea82007-09-12 08:18:36 +0200488 if (skipped_writes)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100489 log_err("fio: %s skips replay of %lu writes due to read-only\n",
490 td->o.name, skipped_writes);
Jens Axboe4241ea82007-09-12 08:18:36 +0200491
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200492 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
493 log_err("fio: found no ios in blktrace data\n");
494 return 1;
Jens Axboed84f8d42007-05-16 08:47:46 +0200495 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200496 td->o.td_ddir = TD_DDIR_READ;
Jens Axboed84f8d42007-05-16 08:47:46 +0200497 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
498 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200499 td->o.td_ddir = TD_DDIR_WRITE;
Jens Axboed84f8d42007-05-16 08:47:46 +0200500 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
501 } else {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200502 td->o.td_ddir = TD_DDIR_RW;
Jens Axboed84f8d42007-05-16 08:47:46 +0200503 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
504 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
505 }
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200506
507 /*
508 * We need to do direct/raw ios to the device, to avoid getting
509 * read-ahead in our way.
510 */
511 td->o.odirect = 1;
512
Jens Axboeeb5fdcf2014-04-08 13:43:19 -0600513 /*
514 * we don't know if this option was set or not. it defaults to 1,
515 * so we'll just guess that we should override it if it's still 1
516 */
517 if (td->o.iodepth != 1)
518 td->o.iodepth = depth;
519
Jens Axboefb7b71a2007-05-15 08:44:04 +0200520 return 0;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200521err:
522 close(fd);
Jens Axboe38470f82007-05-16 09:26:23 +0200523 fifo_free(fifo);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200524 return 1;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200525}