blob: b7071808ccec0b9aec5a49fa85460ad823a963be [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 Axboefb7b71a2007-05-15 08:44:04 +02009#include "list.h"
10#include "fio.h"
11#include "blktrace_api.h"
12
Jens Axboef12b3232007-05-16 12:16:44 +020013#define TRACE_FIFO_SIZE 65536
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
Jens Axboe5e6c2062007-05-16 14:40:29 +0200100static int lookup_device(char *path, unsigned int maj, unsigned int min)
101{
102 struct dirent *dir;
103 struct stat st;
104 int found = 0;
105 DIR *D;
106
107 D = opendir(path);
108 if (!D)
109 return 0;
110
111 while ((dir = readdir(D)) != NULL) {
112 char full_path[256];
113
114 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
115 continue;
116
117 sprintf(full_path, "%s/%s", path, dir->d_name);
118 if (lstat(full_path, &st) == -1) {
119 perror("lstat");
120 break;
121 }
122
123 if (S_ISDIR(st.st_mode)) {
124 found = lookup_device(full_path, maj, min);
125 if (found) {
126 strcpy(path, full_path);
127 break;
128 }
129 }
130
131 if (!S_ISBLK(st.st_mode))
132 continue;
133
134 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100135 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200136 strcpy(path, full_path);
137 found = 1;
138 break;
139 }
140 }
141
142 closedir(D);
143 return found;
144}
145
Jens Axboec69aa912007-05-22 15:51:50 +0200146#define FMINORBITS 20
147#define FMINORMASK ((1U << FMINORBITS) - 1)
148#define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
149#define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
Jens Axboeeeb9c2a2007-05-16 18:28:47 +0200150
Jens Axboe5e6c2062007-05-16 14:40:29 +0200151static void trace_add_file(struct thread_data *td, __u32 device)
152{
153 static unsigned int last_maj, last_min;
Jens Axboec69aa912007-05-22 15:51:50 +0200154 unsigned int maj = FMAJOR(device);
155 unsigned int min = FMINOR(device);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200156 struct fio_file *f;
157 char dev[256];
158 unsigned int i;
159
160 if (last_maj == maj && last_min == min)
161 return;
162
163 last_maj = maj;
164 last_min = min;
165
166 /*
167 * check for this file in our list
168 */
169 for_each_file(td, f, i)
170 if (f->major == maj && f->minor == min)
171 return;
172
173 strcpy(dev, "/dev");
Jens Axboebd6f78b2008-02-01 20:27:52 +0100174 if (lookup_device(dev, maj, min)) {
175 dprint(FD_BLKTRACE, "add devices %s\n", dev);
Jens Axboe5e6c2062007-05-16 14:40:29 +0200176 add_file(td, dev);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100177 }
Jens Axboe5e6c2062007-05-16 14:40:29 +0200178}
179
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200180/*
181 * Store blk_io_trace data in an ipo for later retrieval.
182 */
Jens Axboefdefd982007-05-15 10:12:26 +0200183static void store_ipo(struct thread_data *td, unsigned long long offset,
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200184 unsigned int bytes, int rw, unsigned long long ttime)
Jens Axboefdefd982007-05-15 10:12:26 +0200185{
186 struct io_piece *ipo = malloc(sizeof(*ipo));
187
188 memset(ipo, 0, sizeof(*ipo));
189 INIT_LIST_HEAD(&ipo->list);
Jens Axboea2eea812007-05-15 13:10:41 +0200190 /*
191 * the 512 is wrong here, it should be the hardware sector size...
192 */
193 ipo->offset = offset * 512;
Jens Axboefdefd982007-05-15 10:12:26 +0200194 ipo->len = bytes;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200195 ipo->delay = ttime / 1000;
Jens Axboefdefd982007-05-15 10:12:26 +0200196 if (rw)
197 ipo->ddir = DDIR_WRITE;
198 else
199 ipo->ddir = DDIR_READ;
200
Jens Axboebd6f78b2008-02-01 20:27:52 +0100201 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
202 ipo->ddir, ipo->offset,
203 ipo->len, ipo->delay);
Jens Axboefdefd982007-05-15 10:12:26 +0200204 list_add_tail(&ipo->list, &td->io_log_list);
205}
206
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200207/*
208 * We only care for queue traces, most of the others are side effects
209 * due to internal workings of the block layer.
210 */
211static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
Jens Axboed84f8d42007-05-16 08:47:46 +0200212 unsigned long long ttime, unsigned long *ios,
213 unsigned int *bs)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200214{
Jens Axboefdefd982007-05-15 10:12:26 +0200215 int rw;
216
217 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
218 return;
Jens Axboea2eea812007-05-15 13:10:41 +0200219 if (t->action & BLK_TC_ACT(BLK_TC_PC))
220 return;
Jens Axboe3f3d3612007-05-16 12:29:06 +0200221 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
Jens Axboea2eea812007-05-15 13:10:41 +0200222 return;
Jens Axboefdefd982007-05-15 10:12:26 +0200223
Jens Axboe5e6c2062007-05-16 14:40:29 +0200224 trace_add_file(td, t->device);
225
Jens Axboee7a7d702007-05-15 10:13:04 +0200226 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200227
228 if (t->bytes > bs[rw])
229 bs[rw] = t->bytes;
230
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200231 ios[rw]++;
Jens Axboe6df8ada2007-05-15 13:23:19 +0200232 td->o.size += t->bytes;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200233 store_ipo(td, t->sector, t->bytes, rw, ttime);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200234}
235
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200236/*
237 * Load a blktrace file by reading all the blk_io_trace entries, and storing
238 * them as io_pieces like the fio text version would do.
239 */
Jens Axboefb7b71a2007-05-15 08:44:04 +0200240int load_blktrace(struct thread_data *td, const char *filename)
241{
Jens Axboea61edde2007-05-15 14:29:58 +0200242 unsigned long long ttime, delay;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200243 struct blk_io_trace t;
Jens Axboe4241ea82007-09-12 08:18:36 +0200244 unsigned long ios[2], skipped_writes;
Jens Axboea61edde2007-05-15 14:29:58 +0200245 unsigned int cpu;
Jens Axboed84f8d42007-05-16 08:47:46 +0200246 unsigned int rw_bs[2];
Jens Axboee2887562007-05-16 09:25:09 +0200247 struct fifo *fifo;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200248 int fd;
249
250 fd = open(filename, O_RDONLY);
251 if (fd < 0) {
252 td_verror(td, errno, "open blktrace file");
253 return 1;
254 }
255
Jens Axboee2887562007-05-16 09:25:09 +0200256 fifo = fifo_alloc(TRACE_FIFO_SIZE);
257
Jens Axboe6df8ada2007-05-15 13:23:19 +0200258 td->o.size = 0;
259
Jens Axboea61edde2007-05-15 14:29:58 +0200260 cpu = 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200261 ttime = 0;
262 ios[0] = ios[1] = 0;
263 rw_bs[0] = rw_bs[1] = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +0200264 skipped_writes = 0;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200265 do {
Jens Axboee2887562007-05-16 09:25:09 +0200266 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
Jens Axboefb7b71a2007-05-15 08:44:04 +0200267
Jens Axboee2887562007-05-16 09:25:09 +0200268 if (ret < 0)
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200269 goto err;
Jens Axboee2887562007-05-16 09:25:09 +0200270 else if (!ret)
Jens Axboefb7b71a2007-05-15 08:44:04 +0200271 break;
Jens Axboee2887562007-05-16 09:25:09 +0200272 else if (ret < (int) sizeof(t)) {
273 log_err("fio: short fifo get\n");
274 break;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200275 }
276
277 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100278 log_err("fio: bad magic in blktrace data: %x\n",
279 t.magic);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200280 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200281 }
282 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100283 log_err("fio: bad blktrace version %d\n",
284 t.magic & 0xff);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200285 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200286 }
Jens Axboef12b3232007-05-16 12:16:44 +0200287 ret = discard_pdu(td, fifo, fd, &t);
288 if (ret < 0) {
Jens Axboefb7b71a2007-05-15 08:44:04 +0200289 td_verror(td, ret, "blktrace lseek");
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200290 goto err;
Jens Axboef12b3232007-05-16 12:16:44 +0200291 } else if (t.pdu_len != ret) {
292 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
293 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200294 }
Jens Axboef3f72c52007-08-23 09:03:26 +0200295 if (t.action & BLK_TC_ACT(BLK_TC_NOTIFY))
296 continue;
Jens Axboea61edde2007-05-15 14:29:58 +0200297 if (!ttime) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200298 ttime = t.time;
Jens Axboea61edde2007-05-15 14:29:58 +0200299 cpu = t.cpu;
300 }
301 delay = 0;
302 if (cpu == t.cpu)
303 delay = t.time - ttime;
Jens Axboe4241ea82007-09-12 08:18:36 +0200304 if ((t.action & BLK_TC_ACT(BLK_TC_WRITE)) && read_only)
305 skipped_writes++;
306 else
307 handle_trace(td, &t, delay, ios, rw_bs);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200308 ttime = t.time;
Jens Axboea61edde2007-05-15 14:29:58 +0200309 cpu = t.cpu;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200310 } while (1);
311
Jens Axboe38470f82007-05-16 09:26:23 +0200312 fifo_free(fifo);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200313 close(fd);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200314
Jens Axboe4241ea82007-09-12 08:18:36 +0200315 if (skipped_writes)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100316 log_err("fio: %s skips replay of %lu writes due to read-only\n",
317 td->o.name, skipped_writes);
Jens Axboe4241ea82007-09-12 08:18:36 +0200318
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200319 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
320 log_err("fio: found no ios in blktrace data\n");
321 return 1;
Jens Axboed84f8d42007-05-16 08:47:46 +0200322 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200323 td->o.td_ddir = TD_DDIR_READ;
Jens Axboed84f8d42007-05-16 08:47:46 +0200324 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
325 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200326 td->o.td_ddir = TD_DDIR_WRITE;
Jens Axboed84f8d42007-05-16 08:47:46 +0200327 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
328 } else {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200329 td->o.td_ddir = TD_DDIR_RW;
Jens Axboed84f8d42007-05-16 08:47:46 +0200330 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
331 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
332 }
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200333
334 /*
335 * We need to do direct/raw ios to the device, to avoid getting
336 * read-ahead in our way.
337 */
338 td->o.odirect = 1;
339
Jens Axboefb7b71a2007-05-15 08:44:04 +0200340 return 0;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200341err:
342 close(fd);
Jens Axboe38470f82007-05-16 09:26:23 +0200343 fifo_free(fifo);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200344 return 1;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200345}