Commit | Line | Data |
---|---|---|
fb7b71a3 JA |
1 | /* |
2 | * blktrace support code for fio | |
3 | */ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
8c1fdf04 | 6 | |
fb7b71a3 JA |
7 | #include "list.h" |
8 | #include "fio.h" | |
9 | #include "blktrace_api.h" | |
10 | ||
f12b323f | 11 | #define TRACE_FIFO_SIZE 65536 |
e2887563 JA |
12 | |
13 | /* | |
14 | * fifo refill frontend, to avoid reading data in trace sized bites | |
15 | */ | |
16 | static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd) | |
17 | { | |
18 | char buf[TRACE_FIFO_SIZE]; | |
f12b323f | 19 | unsigned int total; |
e2887563 JA |
20 | int ret; |
21 | ||
f12b323f JA |
22 | total = sizeof(buf); |
23 | if (total > fifo_room(fifo)) | |
24 | total = fifo_room(fifo); | |
e2887563 | 25 | |
f12b323f JA |
26 | ret = read(fd, buf, total); |
27 | if (ret < 0) { | |
28 | td_verror(td, errno, "read blktrace file"); | |
29 | return -1; | |
e2887563 JA |
30 | } |
31 | ||
f12b323f JA |
32 | if (ret > 0) |
33 | ret = fifo_put(fifo, buf, ret); | |
34 | ||
35 | return ret; | |
e2887563 JA |
36 | } |
37 | ||
38 | /* | |
39 | * Retrieve 'len' bytes from the fifo, refilling if necessary. | |
40 | */ | |
41 | static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd, | |
42 | void *buf, unsigned int len) | |
43 | { | |
f12b323f JA |
44 | if (fifo_len(fifo) < len) { |
45 | int ret = refill_fifo(td, fifo, fd); | |
e2887563 | 46 | |
f12b323f JA |
47 | if (ret < 0) |
48 | return ret; | |
49 | } | |
e2887563 JA |
50 | |
51 | return fifo_get(fifo, buf, len); | |
52 | } | |
53 | ||
8c1fdf04 JA |
54 | /* |
55 | * Just discard the pdu by seeking past it. | |
56 | */ | |
f12b323f JA |
57 | static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd, |
58 | struct blk_io_trace *t) | |
fb7b71a3 JA |
59 | { |
60 | if (t->pdu_len == 0) | |
61 | return 0; | |
62 | ||
f12b323f | 63 | return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len); |
fb7b71a3 JA |
64 | } |
65 | ||
8c1fdf04 JA |
66 | /* |
67 | * Check if this is a blktrace binary data file. We read a single trace | |
68 | * into memory and check for the magic signature. | |
69 | */ | |
fb7b71a3 JA |
70 | int is_blktrace(const char *filename) |
71 | { | |
72 | struct blk_io_trace t; | |
73 | int fd, ret; | |
74 | ||
75 | fd = open(filename, O_RDONLY); | |
76 | if (fd < 0) { | |
77 | perror("open blktrace"); | |
78 | return 0; | |
79 | } | |
80 | ||
81 | ret = read(fd, &t, sizeof(t)); | |
82 | close(fd); | |
83 | ||
84 | if (ret < 0) { | |
85 | perror("read blktrace"); | |
86 | return 0; | |
87 | } else if (ret != sizeof(t)) { | |
88 | log_err("fio: short read on blktrace file\n"); | |
89 | return 0; | |
90 | } | |
91 | ||
92 | if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) | |
93 | return 1; | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
8c1fdf04 JA |
98 | /* |
99 | * Store blk_io_trace data in an ipo for later retrieval. | |
100 | */ | |
fdefd987 | 101 | static void store_ipo(struct thread_data *td, unsigned long long offset, |
8c1fdf04 | 102 | unsigned int bytes, int rw, unsigned long long ttime) |
fdefd987 JA |
103 | { |
104 | struct io_piece *ipo = malloc(sizeof(*ipo)); | |
105 | ||
106 | memset(ipo, 0, sizeof(*ipo)); | |
107 | INIT_LIST_HEAD(&ipo->list); | |
a2eea81b JA |
108 | /* |
109 | * the 512 is wrong here, it should be the hardware sector size... | |
110 | */ | |
111 | ipo->offset = offset * 512; | |
fdefd987 | 112 | ipo->len = bytes; |
8c1fdf04 | 113 | ipo->delay = ttime / 1000; |
fdefd987 JA |
114 | if (rw) |
115 | ipo->ddir = DDIR_WRITE; | |
116 | else | |
117 | ipo->ddir = DDIR_READ; | |
118 | ||
119 | list_add_tail(&ipo->list, &td->io_log_list); | |
120 | } | |
121 | ||
8c1fdf04 JA |
122 | /* |
123 | * We only care for queue traces, most of the others are side effects | |
124 | * due to internal workings of the block layer. | |
125 | */ | |
126 | static void handle_trace(struct thread_data *td, struct blk_io_trace *t, | |
d84f8d49 JA |
127 | unsigned long long ttime, unsigned long *ios, |
128 | unsigned int *bs) | |
fb7b71a3 | 129 | { |
fdefd987 JA |
130 | int rw; |
131 | ||
132 | if ((t->action & 0xffff) != __BLK_TA_QUEUE) | |
133 | return; | |
a2eea81b JA |
134 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) |
135 | return; | |
3f3d3616 | 136 | if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY)) |
a2eea81b | 137 | return; |
fdefd987 | 138 | |
e7a7d70b | 139 | rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
d84f8d49 JA |
140 | |
141 | if (t->bytes > bs[rw]) | |
142 | bs[rw] = t->bytes; | |
143 | ||
8c1fdf04 | 144 | ios[rw]++; |
6df8adaa | 145 | td->o.size += t->bytes; |
8c1fdf04 | 146 | store_ipo(td, t->sector, t->bytes, rw, ttime); |
fb7b71a3 JA |
147 | } |
148 | ||
8c1fdf04 JA |
149 | /* |
150 | * Load a blktrace file by reading all the blk_io_trace entries, and storing | |
151 | * them as io_pieces like the fio text version would do. | |
152 | */ | |
fb7b71a3 JA |
153 | int load_blktrace(struct thread_data *td, const char *filename) |
154 | { | |
a61eddec | 155 | unsigned long long ttime, delay; |
fb7b71a3 | 156 | struct blk_io_trace t; |
8c1fdf04 | 157 | unsigned long ios[2]; |
a61eddec | 158 | unsigned int cpu; |
d84f8d49 | 159 | unsigned int rw_bs[2]; |
e2887563 | 160 | struct fifo *fifo; |
fb7b71a3 JA |
161 | int fd; |
162 | ||
163 | fd = open(filename, O_RDONLY); | |
164 | if (fd < 0) { | |
165 | td_verror(td, errno, "open blktrace file"); | |
166 | return 1; | |
167 | } | |
168 | ||
e2887563 JA |
169 | fifo = fifo_alloc(TRACE_FIFO_SIZE); |
170 | ||
6df8adaa JA |
171 | td->o.size = 0; |
172 | ||
a61eddec | 173 | cpu = 0; |
d84f8d49 JA |
174 | ttime = 0; |
175 | ios[0] = ios[1] = 0; | |
176 | rw_bs[0] = rw_bs[1] = 0; | |
fb7b71a3 | 177 | do { |
e2887563 | 178 | int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t)); |
fb7b71a3 | 179 | |
e2887563 | 180 | if (ret < 0) |
8c1fdf04 | 181 | goto err; |
e2887563 JA |
182 | else if (!ret) |
183 | break; | |
184 | else if (ret < (int) sizeof(t)) { | |
185 | log_err("fio: short fifo get\n"); | |
fb7b71a3 | 186 | break; |
fb7b71a3 JA |
187 | } |
188 | ||
189 | if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { | |
f12b323f | 190 | log_err("fio: bad magic in blktrace data: %x\n", t.magic); |
8c1fdf04 | 191 | goto err; |
fb7b71a3 JA |
192 | } |
193 | if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) { | |
194 | log_err("fio: bad blktrace version %d\n", t.magic & 0xff); | |
8c1fdf04 | 195 | goto err; |
fb7b71a3 | 196 | } |
f12b323f JA |
197 | ret = discard_pdu(td, fifo, fd, &t); |
198 | if (ret < 0) { | |
fb7b71a3 | 199 | td_verror(td, ret, "blktrace lseek"); |
8c1fdf04 | 200 | goto err; |
f12b323f JA |
201 | } else if (t.pdu_len != ret) { |
202 | log_err("fio: discarded %d of %d\n", ret, t.pdu_len); | |
203 | goto err; | |
fb7b71a3 | 204 | } |
a61eddec | 205 | if (!ttime) { |
8c1fdf04 | 206 | ttime = t.time; |
a61eddec JA |
207 | cpu = t.cpu; |
208 | } | |
209 | delay = 0; | |
210 | if (cpu == t.cpu) | |
211 | delay = t.time - ttime; | |
d84f8d49 | 212 | handle_trace(td, &t, delay, ios, rw_bs); |
8c1fdf04 | 213 | ttime = t.time; |
a61eddec | 214 | cpu = t.cpu; |
fb7b71a3 JA |
215 | } while (1); |
216 | ||
38470f85 | 217 | fifo_free(fifo); |
fb7b71a3 | 218 | close(fd); |
8c1fdf04 JA |
219 | |
220 | if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) { | |
221 | log_err("fio: found no ios in blktrace data\n"); | |
222 | return 1; | |
d84f8d49 | 223 | } else if (ios[DDIR_READ] && !ios[DDIR_READ]) { |
8c1fdf04 | 224 | td->o.td_ddir = TD_DDIR_READ; |
d84f8d49 JA |
225 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
226 | } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) { | |
8c1fdf04 | 227 | td->o.td_ddir = TD_DDIR_WRITE; |
d84f8d49 JA |
228 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; |
229 | } else { | |
8c1fdf04 | 230 | td->o.td_ddir = TD_DDIR_RW; |
d84f8d49 JA |
231 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
232 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; | |
233 | } | |
8c1fdf04 JA |
234 | |
235 | /* | |
236 | * We need to do direct/raw ios to the device, to avoid getting | |
237 | * read-ahead in our way. | |
238 | */ | |
239 | td->o.odirect = 1; | |
240 | ||
fb7b71a3 | 241 | return 0; |
8c1fdf04 JA |
242 | err: |
243 | close(fd); | |
38470f85 | 244 | fifo_free(fifo); |
8c1fdf04 | 245 | return 1; |
fb7b71a3 | 246 | } |