Reinstate unified Makefile
[fio.git] / blktrace.c
CommitLineData
fb7b71a3
JA
1/*
2 * blktrace support code for fio
3 */
4#include <stdio.h>
5#include <stdlib.h>
5e6c2067
JA
6#include <sys/stat.h>
7#include <dirent.h>
8c1fdf04 8
01743ee1 9#include "flist.h"
fb7b71a3
JA
10#include "fio.h"
11#include "blktrace_api.h"
12
2da7df1d 13#define TRACE_FIFO_SIZE 8192
e2887563
JA
14
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];
f12b323f 21 unsigned int total;
e2887563
JA
22 int ret;
23
f12b323f
JA
24 total = sizeof(buf);
25 if (total > fifo_room(fifo))
26 total = fifo_room(fifo);
e2887563 27
f12b323f
JA
28 ret = read(fd, buf, total);
29 if (ret < 0) {
30 td_verror(td, errno, "read blktrace file");
31 return -1;
e2887563
JA
32 }
33
f12b323f
JA
34 if (ret > 0)
35 ret = fifo_put(fifo, buf, ret);
36
bd6f78b2 37 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
f12b323f 38 return ret;
e2887563
JA
39}
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{
f12b323f
JA
47 if (fifo_len(fifo) < len) {
48 int ret = refill_fifo(td, fifo, fd);
e2887563 49
f12b323f
JA
50 if (ret < 0)
51 return ret;
52 }
e2887563
JA
53
54 return fifo_get(fifo, buf, len);
55}
56
8c1fdf04
JA
57/*
58 * Just discard the pdu by seeking past it.
59 */
f12b323f
JA
60static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
61 struct blk_io_trace *t)
fb7b71a3
JA
62{
63 if (t->pdu_len == 0)
64 return 0;
65
bd6f78b2 66 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
f12b323f 67 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
fb7b71a3
JA
68}
69
8c1fdf04
JA
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 */
fb7b71a3
JA
74int is_blktrace(const char *filename)
75{
76 struct blk_io_trace t;
77 int fd, ret;
78
79 fd = open(filename, O_RDONLY);
4dced407 80 if (fd < 0)
fb7b71a3 81 return 0;
fb7b71a3
JA
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
d1c46c04
DN
100static int lookup_device(struct thread_data *td, char *path, unsigned int maj,
101 unsigned int min)
5e6c2067
JA
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
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)) {
d1c46c04 125 found = lookup_device(td, full_path, maj, min);
5e6c2067
JA
126 if (found) {
127 strcpy(path, full_path);
128 break;
129 }
130 }
131
132 if (!S_ISBLK(st.st_mode))
133 continue;
134
d1c46c04
DN
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
5e6c2067 149 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
5ec10eaa 150 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
5e6c2067
JA
151 strcpy(path, full_path);
152 found = 1;
153 break;
154 }
155 }
156
157 closedir(D);
158 return found;
159}
160
c69aa91f
JA
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))
eeb9c2aa 165
691c8fb0
JA
166static void trace_add_open_event(struct thread_data *td, int fileno)
167{
168 struct io_piece *ipo;
169
170 ipo = calloc(1, sizeof(*ipo));
0d29de83 171 init_ipo(ipo);
691c8fb0
JA
172
173 ipo->ddir = DDIR_INVAL;
174 ipo->fileno = fileno;
175 ipo->file_action = FIO_LOG_OPEN_FILE;
01743ee1 176 flist_add_tail(&ipo->list, &td->io_log_list);
691c8fb0
JA
177}
178
5e6c2067
JA
179static void trace_add_file(struct thread_data *td, __u32 device)
180{
181 static unsigned int last_maj, last_min;
c69aa91f
JA
182 unsigned int maj = FMAJOR(device);
183 unsigned int min = FMINOR(device);
5e6c2067
JA
184 struct fio_file *f;
185 char dev[256];
186 unsigned int i;
187
188 if (last_maj == maj && last_min == min)
189 return;
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)
198 if (f->major == maj && f->minor == min)
199 return;
200
201 strcpy(dev, "/dev");
d1c46c04 202 if (lookup_device(td, dev, maj, min)) {
691c8fb0
JA
203 int fileno;
204
bd6f78b2 205 dprint(FD_BLKTRACE, "add devices %s\n", dev);
49ffb4a2 206 fileno = add_file_exclusive(td, dev);
691c8fb0 207 trace_add_open_event(td, fileno);
bd6f78b2 208 }
5e6c2067
JA
209}
210
8c1fdf04
JA
211/*
212 * Store blk_io_trace data in an ipo for later retrieval.
213 */
fdefd987 214static void store_ipo(struct thread_data *td, unsigned long long offset,
8c1fdf04 215 unsigned int bytes, int rw, unsigned long long ttime)
fdefd987
JA
216{
217 struct io_piece *ipo = malloc(sizeof(*ipo));
218
0d29de83
JA
219 init_ipo(ipo);
220
a2eea81b
JA
221 /*
222 * the 512 is wrong here, it should be the hardware sector size...
223 */
224 ipo->offset = offset * 512;
fdefd987 225 ipo->len = bytes;
8c1fdf04 226 ipo->delay = ttime / 1000;
fdefd987
JA
227 if (rw)
228 ipo->ddir = DDIR_WRITE;
229 else
230 ipo->ddir = DDIR_READ;
231
bd6f78b2
JA
232 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
233 ipo->ddir, ipo->offset,
234 ipo->len, ipo->delay);
691c8fb0 235 queue_io_piece(td, ipo);
fdefd987
JA
236}
237
0b9d69ec 238static void handle_trace_notify(struct blk_io_trace *t)
cd991b9e 239{
691c8fb0
JA
240 switch (t->action) {
241 case BLK_TN_PROCESS:
242 printf("got process notify: %x, %d\n", t->action, t->pid);
243 break;
244 case BLK_TN_TIMESTAMP:
245 printf("got timestamp notify: %x, %d\n", t->action, t->pid);
246 break;
ff58fced
JA
247 case BLK_TN_MESSAGE:
248 break;
691c8fb0
JA
249 default:
250 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
251 break;
252 }
253}
5b3023b8 254
ff58fced
JA
255static void handle_trace_discard(struct thread_data *td, struct blk_io_trace *t,
256 unsigned long long ttime, unsigned long *ios)
257{
258 struct io_piece *ipo = malloc(sizeof(*ipo));
259
0d29de83 260 init_ipo(ipo);
ff58fced
JA
261 trace_add_file(td, t->device);
262
263 ios[DDIR_WRITE]++;
264 td->o.size += t->bytes;
265
266 memset(ipo, 0, sizeof(*ipo));
267 INIT_FLIST_HEAD(&ipo->list);
268
269 /*
270 * the 512 is wrong here, it should be the hardware sector size...
271 */
272 ipo->offset = t->sector * 512;
273 ipo->len = t->bytes;
274 ipo->delay = ttime / 1000;
275 ipo->ddir = DDIR_TRIM;
276
277 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
278 ipo->offset, ipo->len,
279 ipo->delay);
280 queue_io_piece(td, ipo);
281}
282
691c8fb0
JA
283static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
284 unsigned long long ttime, unsigned long *ios,
285 unsigned int *bs)
286{
287 int rw;
5b3023b8
JA
288
289 trace_add_file(td, t->device);
290
291 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
292
293 if (t->bytes > bs[rw])
294 bs[rw] = t->bytes;
295
296 ios[rw]++;
297 td->o.size += t->bytes;
298 store_ipo(td, t->sector, t->bytes, rw, ttime);
cd991b9e
JA
299}
300
691c8fb0
JA
301/*
302 * We only care for queue traces, most of the others are side effects
303 * due to internal workings of the block layer.
304 */
305static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
306 unsigned long long ttime, unsigned long *ios,
307 unsigned int *bs)
308{
309 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
310 return;
311 if (t->action & BLK_TC_ACT(BLK_TC_PC))
312 return;
313
314 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
aec2de20 315 handle_trace_notify(t);
ff58fced
JA
316 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
317 handle_trace_discard(td, t, ttime, ios);
691c8fb0
JA
318 else
319 handle_trace_fs(td, t, ttime, ios, bs);
320}
321
8c1fdf04
JA
322/*
323 * Load a blktrace file by reading all the blk_io_trace entries, and storing
324 * them as io_pieces like the fio text version would do.
325 */
fb7b71a3
JA
326int load_blktrace(struct thread_data *td, const char *filename)
327{
a61eddec 328 unsigned long long ttime, delay;
fb7b71a3 329 struct blk_io_trace t;
4241ea8f 330 unsigned long ios[2], skipped_writes;
a61eddec 331 unsigned int cpu;
d84f8d49 332 unsigned int rw_bs[2];
e2887563 333 struct fifo *fifo;
fb7b71a3
JA
334 int fd;
335
336 fd = open(filename, O_RDONLY);
337 if (fd < 0) {
338 td_verror(td, errno, "open blktrace file");
339 return 1;
340 }
341
e2887563
JA
342 fifo = fifo_alloc(TRACE_FIFO_SIZE);
343
6df8adaa
JA
344 td->o.size = 0;
345
a61eddec 346 cpu = 0;
d84f8d49
JA
347 ttime = 0;
348 ios[0] = ios[1] = 0;
349 rw_bs[0] = rw_bs[1] = 0;
4241ea8f 350 skipped_writes = 0;
fb7b71a3 351 do {
e2887563 352 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
fb7b71a3 353
e2887563 354 if (ret < 0)
8c1fdf04 355 goto err;
e2887563
JA
356 else if (!ret)
357 break;
358 else if (ret < (int) sizeof(t)) {
359 log_err("fio: short fifo get\n");
fb7b71a3 360 break;
fb7b71a3
JA
361 }
362
363 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
5ec10eaa
JA
364 log_err("fio: bad magic in blktrace data: %x\n",
365 t.magic);
8c1fdf04 366 goto err;
fb7b71a3
JA
367 }
368 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
5ec10eaa
JA
369 log_err("fio: bad blktrace version %d\n",
370 t.magic & 0xff);
8c1fdf04 371 goto err;
fb7b71a3 372 }
f12b323f
JA
373 ret = discard_pdu(td, fifo, fd, &t);
374 if (ret < 0) {
fb7b71a3 375 td_verror(td, ret, "blktrace lseek");
8c1fdf04 376 goto err;
f12b323f
JA
377 } else if (t.pdu_len != ret) {
378 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
379 goto err;
fb7b71a3 380 }
691c8fb0
JA
381 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
382 if (!ttime) {
383 ttime = t.time;
384 cpu = t.cpu;
385 }
386
387 delay = 0;
388 if (cpu == t.cpu)
389 delay = t.time - ttime;
390 if ((t.action & BLK_TC_ACT(BLK_TC_WRITE)) && read_only)
391 skipped_writes++;
64bbb865
DN
392 else {
393 /*
394 * set delay to zero if no_stall enabled for
395 * fast replay
396 */
397 if (td->o.no_stall)
398 delay = 0;
399
691c8fb0 400 handle_trace(td, &t, delay, ios, rw_bs);
64bbb865 401 }
691c8fb0 402
8c1fdf04 403 ttime = t.time;
a61eddec 404 cpu = t.cpu;
a6edd638
JA
405 } else {
406 delay = 0;
4241ea8f 407 handle_trace(td, &t, delay, ios, rw_bs);
a6edd638 408 }
fb7b71a3
JA
409 } while (1);
410
38470f85 411 fifo_free(fifo);
fb7b71a3 412 close(fd);
8c1fdf04 413
4241ea8f 414 if (skipped_writes)
5ec10eaa
JA
415 log_err("fio: %s skips replay of %lu writes due to read-only\n",
416 td->o.name, skipped_writes);
4241ea8f 417
8c1fdf04
JA
418 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
419 log_err("fio: found no ios in blktrace data\n");
420 return 1;
d84f8d49 421 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
8c1fdf04 422 td->o.td_ddir = TD_DDIR_READ;
d84f8d49
JA
423 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
424 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
8c1fdf04 425 td->o.td_ddir = TD_DDIR_WRITE;
d84f8d49
JA
426 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
427 } else {
8c1fdf04 428 td->o.td_ddir = TD_DDIR_RW;
d84f8d49
JA
429 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
430 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
431 }
8c1fdf04
JA
432
433 /*
434 * We need to do direct/raw ios to the device, to avoid getting
435 * read-ahead in our way.
436 */
437 td->o.odirect = 1;
438
fb7b71a3 439 return 0;
8c1fdf04
JA
440err:
441 close(fd);
38470f85 442 fifo_free(fifo);
8c1fdf04 443 return 1;
fb7b71a3 444}