blob: 9ce4ae29c8cbe358c4ce0913e84d407721d13c17 [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 Axboefb7b71a2007-05-15 08:44:04 +020074int is_blktrace(const char *filename)
75{
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
94 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
95 return 1;
96
97 return 0;
98}
99
David Nellansd1c46c02010-08-31 21:20:47 +0200100static int lookup_device(struct thread_data *td, char *path, unsigned int maj,
101 unsigned int min)
Jens Axboe5e6c2062007-05-16 14:40:29 +0200102{
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
118 sprintf(full_path, "%s/%s", path, dir->d_name);
119 if (lstat(full_path, &st) == -1) {
120 perror("lstat");
121 break;
122 }
123
124 if (S_ISDIR(st.st_mode)) {
David Nellansd1c46c02010-08-31 21:20:47 +0200125 found = lookup_device(td, full_path, maj, min);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200126 if (found) {
127 strcpy(path, full_path);
128 break;
129 }
130 }
131
132 if (!S_ISBLK(st.st_mode))
133 continue;
134
David Nellansd1c46c02010-08-31 21:20:47 +0200135 /*
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 Axboe5e6c2062007-05-16 14:40:29 +0200149 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100150 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200151 strcpy(path, full_path);
152 found = 1;
153 break;
154 }
155 }
156
157 closedir(D);
158 return found;
159}
160
Jens Axboec69aa912007-05-22 15:51:50 +0200161#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 Axboeeeb9c2a2007-05-16 18:28:47 +0200165
Jens Axboe691c8fb2008-03-07 14:26:26 +0100166static void trace_add_open_event(struct thread_data *td, int fileno)
167{
168 struct io_piece *ipo;
169
170 ipo = calloc(1, sizeof(*ipo));
171
172 ipo->ddir = DDIR_INVAL;
173 ipo->fileno = fileno;
174 ipo->file_action = FIO_LOG_OPEN_FILE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200175 flist_add_tail(&ipo->list, &td->io_log_list);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100176}
177
Jens Axboe5e6c2062007-05-16 14:40:29 +0200178static void trace_add_file(struct thread_data *td, __u32 device)
179{
180 static unsigned int last_maj, last_min;
Jens Axboec69aa912007-05-22 15:51:50 +0200181 unsigned int maj = FMAJOR(device);
182 unsigned int min = FMINOR(device);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200183 struct fio_file *f;
184 char dev[256];
185 unsigned int i;
186
187 if (last_maj == maj && last_min == min)
188 return;
189
190 last_maj = maj;
191 last_min = min;
192
193 /*
194 * check for this file in our list
195 */
196 for_each_file(td, f, i)
197 if (f->major == maj && f->minor == min)
198 return;
199
200 strcpy(dev, "/dev");
David Nellansd1c46c02010-08-31 21:20:47 +0200201 if (lookup_device(td, dev, maj, min)) {
Jens Axboe691c8fb2008-03-07 14:26:26 +0100202 int fileno;
203
Jens Axboebd6f78b2008-02-01 20:27:52 +0100204 dprint(FD_BLKTRACE, "add devices %s\n", dev);
Jens Axboe49ffb4a2010-08-24 15:01:37 +0200205 fileno = add_file_exclusive(td, dev);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100206 trace_add_open_event(td, fileno);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100207 }
Jens Axboe5e6c2062007-05-16 14:40:29 +0200208}
209
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200210/*
211 * Store blk_io_trace data in an ipo for later retrieval.
212 */
Jens Axboefdefd982007-05-15 10:12:26 +0200213static void store_ipo(struct thread_data *td, unsigned long long offset,
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200214 unsigned int bytes, int rw, unsigned long long ttime)
Jens Axboefdefd982007-05-15 10:12:26 +0200215{
216 struct io_piece *ipo = malloc(sizeof(*ipo));
217
218 memset(ipo, 0, sizeof(*ipo));
Jens Axboe01743ee2008-06-02 12:19:19 +0200219 INIT_FLIST_HEAD(&ipo->list);
Jens Axboea2eea812007-05-15 13:10:41 +0200220 /*
221 * the 512 is wrong here, it should be the hardware sector size...
222 */
223 ipo->offset = offset * 512;
Jens Axboefdefd982007-05-15 10:12:26 +0200224 ipo->len = bytes;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200225 ipo->delay = ttime / 1000;
Jens Axboefdefd982007-05-15 10:12:26 +0200226 if (rw)
227 ipo->ddir = DDIR_WRITE;
228 else
229 ipo->ddir = DDIR_READ;
230
Jens Axboebd6f78b2008-02-01 20:27:52 +0100231 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
232 ipo->ddir, ipo->offset,
233 ipo->len, ipo->delay);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100234 queue_io_piece(td, ipo);
Jens Axboefdefd982007-05-15 10:12:26 +0200235}
236
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200237static void handle_trace_notify(struct blk_io_trace *t)
Jens Axboe691c8fb2008-03-07 14:26:26 +0100238{
239 switch (t->action) {
240 case BLK_TN_PROCESS:
241 printf("got process notify: %x, %d\n", t->action, t->pid);
242 break;
243 case BLK_TN_TIMESTAMP:
244 printf("got timestamp notify: %x, %d\n", t->action, t->pid);
245 break;
Jens Axboeff58fce2010-08-25 12:02:08 +0200246 case BLK_TN_MESSAGE:
247 break;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100248 default:
249 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
250 break;
251 }
252}
253
Jens Axboeff58fce2010-08-25 12:02:08 +0200254static void handle_trace_discard(struct thread_data *td, struct blk_io_trace *t,
255 unsigned long long ttime, unsigned long *ios)
256{
257 struct io_piece *ipo = malloc(sizeof(*ipo));
258
259 trace_add_file(td, t->device);
260
261 ios[DDIR_WRITE]++;
262 td->o.size += t->bytes;
263
264 memset(ipo, 0, sizeof(*ipo));
265 INIT_FLIST_HEAD(&ipo->list);
266
267 /*
268 * the 512 is wrong here, it should be the hardware sector size...
269 */
270 ipo->offset = t->sector * 512;
271 ipo->len = t->bytes;
272 ipo->delay = ttime / 1000;
273 ipo->ddir = DDIR_TRIM;
274
275 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
276 ipo->offset, ipo->len,
277 ipo->delay);
278 queue_io_piece(td, ipo);
279}
280
Jens Axboe691c8fb2008-03-07 14:26:26 +0100281static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
282 unsigned long long ttime, unsigned long *ios,
283 unsigned int *bs)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200284{
Jens Axboefdefd982007-05-15 10:12:26 +0200285 int rw;
286
Jens Axboe5e6c2062007-05-16 14:40:29 +0200287 trace_add_file(td, t->device);
288
Jens Axboee7a7d702007-05-15 10:13:04 +0200289 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200290
291 if (t->bytes > bs[rw])
292 bs[rw] = t->bytes;
293
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200294 ios[rw]++;
Jens Axboe6df8ada2007-05-15 13:23:19 +0200295 td->o.size += t->bytes;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200296 store_ipo(td, t->sector, t->bytes, rw, ttime);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200297}
298
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200299/*
Jens Axboe691c8fb2008-03-07 14:26:26 +0100300 * We only care for queue traces, most of the others are side effects
301 * due to internal workings of the block layer.
302 */
303static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
304 unsigned long long ttime, unsigned long *ios,
305 unsigned int *bs)
306{
307 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
308 return;
309 if (t->action & BLK_TC_ACT(BLK_TC_PC))
310 return;
311
312 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
Jens Axboeaec2de22008-04-24 12:44:42 +0200313 handle_trace_notify(t);
Jens Axboeff58fce2010-08-25 12:02:08 +0200314 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
315 handle_trace_discard(td, t, ttime, ios);
Jens Axboe691c8fb2008-03-07 14:26:26 +0100316 else
317 handle_trace_fs(td, t, ttime, ios, bs);
318}
319
320/*
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200321 * Load a blktrace file by reading all the blk_io_trace entries, and storing
322 * them as io_pieces like the fio text version would do.
323 */
Jens Axboefb7b71a2007-05-15 08:44:04 +0200324int load_blktrace(struct thread_data *td, const char *filename)
325{
Jens Axboea61edde2007-05-15 14:29:58 +0200326 unsigned long long ttime, delay;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200327 struct blk_io_trace t;
Jens Axboe4241ea82007-09-12 08:18:36 +0200328 unsigned long ios[2], skipped_writes;
Jens Axboea61edde2007-05-15 14:29:58 +0200329 unsigned int cpu;
Jens Axboed84f8d42007-05-16 08:47:46 +0200330 unsigned int rw_bs[2];
Jens Axboee2887562007-05-16 09:25:09 +0200331 struct fifo *fifo;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200332 int fd;
333
334 fd = open(filename, O_RDONLY);
335 if (fd < 0) {
336 td_verror(td, errno, "open blktrace file");
337 return 1;
338 }
339
Jens Axboee2887562007-05-16 09:25:09 +0200340 fifo = fifo_alloc(TRACE_FIFO_SIZE);
341
Jens Axboe6df8ada2007-05-15 13:23:19 +0200342 td->o.size = 0;
343
Jens Axboea61edde2007-05-15 14:29:58 +0200344 cpu = 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200345 ttime = 0;
346 ios[0] = ios[1] = 0;
347 rw_bs[0] = rw_bs[1] = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +0200348 skipped_writes = 0;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200349 do {
Jens Axboee2887562007-05-16 09:25:09 +0200350 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
Jens Axboefb7b71a2007-05-15 08:44:04 +0200351
Jens Axboee2887562007-05-16 09:25:09 +0200352 if (ret < 0)
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200353 goto err;
Jens Axboee2887562007-05-16 09:25:09 +0200354 else if (!ret)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200355 break;
Jens Axboee2887562007-05-16 09:25:09 +0200356 else if (ret < (int) sizeof(t)) {
357 log_err("fio: short fifo get\n");
358 break;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200359 }
360
361 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100362 log_err("fio: bad magic in blktrace data: %x\n",
363 t.magic);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200364 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200365 }
366 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100367 log_err("fio: bad blktrace version %d\n",
368 t.magic & 0xff);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200369 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200370 }
Jens Axboef12b3232007-05-16 12:16:44 +0200371 ret = discard_pdu(td, fifo, fd, &t);
372 if (ret < 0) {
Jens Axboefb7b71a2007-05-15 08:44:04 +0200373 td_verror(td, ret, "blktrace lseek");
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200374 goto err;
Jens Axboef12b3232007-05-16 12:16:44 +0200375 } else if (t.pdu_len != ret) {
376 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
377 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200378 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100379 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
380 if (!ttime) {
381 ttime = t.time;
382 cpu = t.cpu;
383 }
384
385 delay = 0;
386 if (cpu == t.cpu)
387 delay = t.time - ttime;
388 if ((t.action & BLK_TC_ACT(BLK_TC_WRITE)) && read_only)
389 skipped_writes++;
David Nellans64bbb862010-08-24 22:13:30 +0200390 else {
391 /*
392 * set delay to zero if no_stall enabled for
393 * fast replay
394 */
395 if (td->o.no_stall)
396 delay = 0;
397
Jens Axboe691c8fb2008-03-07 14:26:26 +0100398 handle_trace(td, &t, delay, ios, rw_bs);
David Nellans64bbb862010-08-24 22:13:30 +0200399 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100400
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200401 ttime = t.time;
Jens Axboea61edde2007-05-15 14:29:58 +0200402 cpu = t.cpu;
Jens Axboea6edd632008-03-07 21:41:54 +0100403 } else {
404 delay = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +0200405 handle_trace(td, &t, delay, ios, rw_bs);
Jens Axboea6edd632008-03-07 21:41:54 +0100406 }
Jens Axboefb7b71a2007-05-15 08:44:04 +0200407 } while (1);
408
Jens Axboe38470f82007-05-16 09:26:23 +0200409 fifo_free(fifo);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200410 close(fd);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200411
Jens Axboe4241ea82007-09-12 08:18:36 +0200412 if (skipped_writes)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100413 log_err("fio: %s skips replay of %lu writes due to read-only\n",
414 td->o.name, skipped_writes);
Jens Axboe4241ea82007-09-12 08:18:36 +0200415
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200416 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
417 log_err("fio: found no ios in blktrace data\n");
418 return 1;
Jens Axboed84f8d42007-05-16 08:47:46 +0200419 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200420 td->o.td_ddir = TD_DDIR_READ;
Jens Axboed84f8d42007-05-16 08:47:46 +0200421 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
422 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200423 td->o.td_ddir = TD_DDIR_WRITE;
Jens Axboed84f8d42007-05-16 08:47:46 +0200424 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
425 } else {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200426 td->o.td_ddir = TD_DDIR_RW;
Jens Axboed84f8d42007-05-16 08:47:46 +0200427 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
428 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
429 }
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200430
431 /*
432 * We need to do direct/raw ios to the device, to avoid getting
433 * read-ahead in our way.
434 */
435 td->o.odirect = 1;
436
Jens Axboefb7b71a2007-05-15 08:44:04 +0200437 return 0;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200438err:
439 close(fd);
Jens Axboe38470f82007-05-16 09:26:23 +0200440 fifo_free(fifo);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200441 return 1;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200442}