blob: afa699cd1a3f7f4225e4b220654d347c9641397a [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 Axboe8c1fdf02007-05-15 11:54:21 +02006
Jens Axboefb7b71a2007-05-15 08:44:04 +02007#include "list.h"
8#include "fio.h"
9#include "blktrace_api.h"
10
Jens Axboe8c1fdf02007-05-15 11:54:21 +020011/*
12 * Just discard the pdu by seeking past it.
13 */
Jens Axboefb7b71a2007-05-15 08:44:04 +020014static int discard_pdu(int fd, struct blk_io_trace *t)
15{
16 if (t->pdu_len == 0)
17 return 0;
18
19 if (lseek(fd, t->pdu_len, SEEK_CUR) < 0)
20 return errno;
21
22 return 0;
23}
24
Jens Axboe8c1fdf02007-05-15 11:54:21 +020025/*
26 * Check if this is a blktrace binary data file. We read a single trace
27 * into memory and check for the magic signature.
28 */
Jens Axboefb7b71a2007-05-15 08:44:04 +020029int is_blktrace(const char *filename)
30{
31 struct blk_io_trace t;
32 int fd, ret;
33
34 fd = open(filename, O_RDONLY);
35 if (fd < 0) {
36 perror("open blktrace");
37 return 0;
38 }
39
40 ret = read(fd, &t, sizeof(t));
41 close(fd);
42
43 if (ret < 0) {
44 perror("read blktrace");
45 return 0;
46 } else if (ret != sizeof(t)) {
47 log_err("fio: short read on blktrace file\n");
48 return 0;
49 }
50
51 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
52 return 1;
53
54 return 0;
55}
56
Jens Axboe8c1fdf02007-05-15 11:54:21 +020057/*
58 * Store blk_io_trace data in an ipo for later retrieval.
59 */
Jens Axboefdefd982007-05-15 10:12:26 +020060static void store_ipo(struct thread_data *td, unsigned long long offset,
Jens Axboe8c1fdf02007-05-15 11:54:21 +020061 unsigned int bytes, int rw, unsigned long long ttime)
Jens Axboefdefd982007-05-15 10:12:26 +020062{
63 struct io_piece *ipo = malloc(sizeof(*ipo));
64
65 memset(ipo, 0, sizeof(*ipo));
66 INIT_LIST_HEAD(&ipo->list);
Jens Axboea2eea812007-05-15 13:10:41 +020067 /*
68 * the 512 is wrong here, it should be the hardware sector size...
69 */
70 ipo->offset = offset * 512;
Jens Axboefdefd982007-05-15 10:12:26 +020071 ipo->len = bytes;
Jens Axboe8c1fdf02007-05-15 11:54:21 +020072 ipo->delay = ttime / 1000;
Jens Axboefdefd982007-05-15 10:12:26 +020073 if (rw)
74 ipo->ddir = DDIR_WRITE;
75 else
76 ipo->ddir = DDIR_READ;
77
78 list_add_tail(&ipo->list, &td->io_log_list);
79}
80
Jens Axboe8c1fdf02007-05-15 11:54:21 +020081/*
82 * We only care for queue traces, most of the others are side effects
83 * due to internal workings of the block layer.
84 */
85static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
Jens Axboed84f8d42007-05-16 08:47:46 +020086 unsigned long long ttime, unsigned long *ios,
87 unsigned int *bs)
Jens Axboefb7b71a2007-05-15 08:44:04 +020088{
Jens Axboefdefd982007-05-15 10:12:26 +020089 int rw;
90
91 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
92 return;
Jens Axboea2eea812007-05-15 13:10:41 +020093 if (t->action & BLK_TC_ACT(BLK_TC_PC))
94 return;
95
96 /*
97 * should not happen, need to look into that...
98 */
99 if (!t->bytes)
100 return;
Jens Axboefdefd982007-05-15 10:12:26 +0200101
Jens Axboee7a7d702007-05-15 10:13:04 +0200102 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200103
104 if (t->bytes > bs[rw])
105 bs[rw] = t->bytes;
106
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200107 ios[rw]++;
Jens Axboe6df8ada2007-05-15 13:23:19 +0200108 td->o.size += t->bytes;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200109 store_ipo(td, t->sector, t->bytes, rw, ttime);
Jens Axboefb7b71a2007-05-15 08:44:04 +0200110}
111
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200112/*
113 * Load a blktrace file by reading all the blk_io_trace entries, and storing
114 * them as io_pieces like the fio text version would do.
115 */
Jens Axboefb7b71a2007-05-15 08:44:04 +0200116int load_blktrace(struct thread_data *td, const char *filename)
117{
Jens Axboea61edde2007-05-15 14:29:58 +0200118 unsigned long long ttime, delay;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200119 struct blk_io_trace t;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200120 unsigned long ios[2];
Jens Axboea61edde2007-05-15 14:29:58 +0200121 unsigned int cpu;
Jens Axboed84f8d42007-05-16 08:47:46 +0200122 unsigned int rw_bs[2];
Jens Axboefb7b71a2007-05-15 08:44:04 +0200123 int fd;
124
125 fd = open(filename, O_RDONLY);
126 if (fd < 0) {
127 td_verror(td, errno, "open blktrace file");
128 return 1;
129 }
130
Jens Axboe6df8ada2007-05-15 13:23:19 +0200131 td->o.size = 0;
132
Jens Axboea61edde2007-05-15 14:29:58 +0200133 cpu = 0;
Jens Axboed84f8d42007-05-16 08:47:46 +0200134 ttime = 0;
135 ios[0] = ios[1] = 0;
136 rw_bs[0] = rw_bs[1] = 0;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200137 do {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200138 /*
139 * Once this is working fully, I'll add a layer between
140 * here and read to cache trace data. Then we can avoid
141 * doing itsy bitsy reads, but instead pull in a larger
142 * chunk of data at the time.
143 */
Jens Axboefb7b71a2007-05-15 08:44:04 +0200144 int ret = read(fd, &t, sizeof(t));
145
146 if (ret < 0) {
147 td_verror(td, errno, "read blktrace file");
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200148 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200149 } else if (!ret) {
150 break;
151 } else if (ret != sizeof(t)) {
152 log_err("fio: short read on blktrace file\n");
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200153 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200154 }
155
156 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
157 log_err("fio: bad magic in blktrace data\n");
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200158 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200159 }
160 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
161 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200162 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200163 }
164 ret = discard_pdu(fd, &t);
165 if (ret) {
166 td_verror(td, ret, "blktrace lseek");
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200167 goto err;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200168 }
Jens Axboea61edde2007-05-15 14:29:58 +0200169 if (!ttime) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200170 ttime = t.time;
Jens Axboea61edde2007-05-15 14:29:58 +0200171 cpu = t.cpu;
172 }
173 delay = 0;
174 if (cpu == t.cpu)
175 delay = t.time - ttime;
Jens Axboed84f8d42007-05-16 08:47:46 +0200176 handle_trace(td, &t, delay, ios, rw_bs);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200177 ttime = t.time;
Jens Axboea61edde2007-05-15 14:29:58 +0200178 cpu = t.cpu;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200179 } while (1);
180
181 close(fd);
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200182
183 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
184 log_err("fio: found no ios in blktrace data\n");
185 return 1;
Jens Axboed84f8d42007-05-16 08:47:46 +0200186 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200187 td->o.td_ddir = TD_DDIR_READ;
Jens Axboed84f8d42007-05-16 08:47:46 +0200188 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
189 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200190 td->o.td_ddir = TD_DDIR_WRITE;
Jens Axboed84f8d42007-05-16 08:47:46 +0200191 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
192 } else {
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200193 td->o.td_ddir = TD_DDIR_RW;
Jens Axboed84f8d42007-05-16 08:47:46 +0200194 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
195 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
196 }
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200197
198 /*
199 * We need to do direct/raw ios to the device, to avoid getting
200 * read-ahead in our way.
201 */
202 td->o.odirect = 1;
203
Jens Axboefb7b71a2007-05-15 08:44:04 +0200204 return 0;
Jens Axboe8c1fdf02007-05-15 11:54:21 +0200205err:
206 close(fd);
207 return 1;
Jens Axboefb7b71a2007-05-15 08:44:04 +0200208}