Commit | Line | Data |
---|---|---|
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 | |
fb7b71a3 JA |
9 | #include "list.h" |
10 | #include "fio.h" | |
11 | #include "blktrace_api.h" | |
12 | ||
f12b323f | 13 | #define TRACE_FIFO_SIZE 65536 |
e2887563 JA |
14 | |
15 | /* | |
16 | * fifo refill frontend, to avoid reading data in trace sized bites | |
17 | */ | |
18 | static 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 | ||
37 | return ret; | |
e2887563 JA |
38 | } |
39 | ||
40 | /* | |
41 | * Retrieve 'len' bytes from the fifo, refilling if necessary. | |
42 | */ | |
43 | static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd, | |
44 | void *buf, unsigned int len) | |
45 | { | |
f12b323f JA |
46 | if (fifo_len(fifo) < len) { |
47 | int ret = refill_fifo(td, fifo, fd); | |
e2887563 | 48 | |
f12b323f JA |
49 | if (ret < 0) |
50 | return ret; | |
51 | } | |
e2887563 JA |
52 | |
53 | return fifo_get(fifo, buf, len); | |
54 | } | |
55 | ||
8c1fdf04 JA |
56 | /* |
57 | * Just discard the pdu by seeking past it. | |
58 | */ | |
f12b323f JA |
59 | static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd, |
60 | struct blk_io_trace *t) | |
fb7b71a3 JA |
61 | { |
62 | if (t->pdu_len == 0) | |
63 | return 0; | |
64 | ||
f12b323f | 65 | return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len); |
fb7b71a3 JA |
66 | } |
67 | ||
8c1fdf04 JA |
68 | /* |
69 | * Check if this is a blktrace binary data file. We read a single trace | |
70 | * into memory and check for the magic signature. | |
71 | */ | |
fb7b71a3 JA |
72 | int is_blktrace(const char *filename) |
73 | { | |
74 | struct blk_io_trace t; | |
75 | int fd, ret; | |
76 | ||
77 | fd = open(filename, O_RDONLY); | |
78 | if (fd < 0) { | |
79 | perror("open blktrace"); | |
80 | return 0; | |
81 | } | |
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 | ||
5e6c2067 JA |
100 | static 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)) { | |
135 | strcpy(path, full_path); | |
136 | found = 1; | |
137 | break; | |
138 | } | |
139 | } | |
140 | ||
141 | closedir(D); | |
142 | return found; | |
143 | } | |
144 | ||
eeb9c2aa JA |
145 | #define MINORBITS 20 |
146 | #define MINORMASK ((1U << MINORBITS) - 1) | |
147 | #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS)) | |
148 | #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK)) | |
149 | ||
5e6c2067 JA |
150 | static void trace_add_file(struct thread_data *td, __u32 device) |
151 | { | |
152 | static unsigned int last_maj, last_min; | |
eeb9c2aa JA |
153 | unsigned int maj = MAJOR(device); |
154 | unsigned int min = MINOR(device); | |
5e6c2067 JA |
155 | struct fio_file *f; |
156 | char dev[256]; | |
157 | unsigned int i; | |
158 | ||
159 | if (last_maj == maj && last_min == min) | |
160 | return; | |
161 | ||
162 | last_maj = maj; | |
163 | last_min = min; | |
164 | ||
165 | /* | |
166 | * check for this file in our list | |
167 | */ | |
168 | for_each_file(td, f, i) | |
169 | if (f->major == maj && f->minor == min) | |
170 | return; | |
171 | ||
172 | strcpy(dev, "/dev"); | |
173 | if (lookup_device(dev, maj, min)) | |
174 | add_file(td, dev); | |
175 | } | |
176 | ||
8c1fdf04 JA |
177 | /* |
178 | * Store blk_io_trace data in an ipo for later retrieval. | |
179 | */ | |
fdefd987 | 180 | static void store_ipo(struct thread_data *td, unsigned long long offset, |
8c1fdf04 | 181 | unsigned int bytes, int rw, unsigned long long ttime) |
fdefd987 JA |
182 | { |
183 | struct io_piece *ipo = malloc(sizeof(*ipo)); | |
184 | ||
185 | memset(ipo, 0, sizeof(*ipo)); | |
186 | INIT_LIST_HEAD(&ipo->list); | |
a2eea81b JA |
187 | /* |
188 | * the 512 is wrong here, it should be the hardware sector size... | |
189 | */ | |
190 | ipo->offset = offset * 512; | |
fdefd987 | 191 | ipo->len = bytes; |
8c1fdf04 | 192 | ipo->delay = ttime / 1000; |
fdefd987 JA |
193 | if (rw) |
194 | ipo->ddir = DDIR_WRITE; | |
195 | else | |
196 | ipo->ddir = DDIR_READ; | |
197 | ||
198 | list_add_tail(&ipo->list, &td->io_log_list); | |
199 | } | |
200 | ||
8c1fdf04 JA |
201 | /* |
202 | * We only care for queue traces, most of the others are side effects | |
203 | * due to internal workings of the block layer. | |
204 | */ | |
205 | static void handle_trace(struct thread_data *td, struct blk_io_trace *t, | |
d84f8d49 JA |
206 | unsigned long long ttime, unsigned long *ios, |
207 | unsigned int *bs) | |
fb7b71a3 | 208 | { |
fdefd987 JA |
209 | int rw; |
210 | ||
211 | if ((t->action & 0xffff) != __BLK_TA_QUEUE) | |
212 | return; | |
a2eea81b JA |
213 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) |
214 | return; | |
3f3d3616 | 215 | if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY)) |
a2eea81b | 216 | return; |
fdefd987 | 217 | |
5e6c2067 JA |
218 | trace_add_file(td, t->device); |
219 | ||
e7a7d70b | 220 | rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
d84f8d49 JA |
221 | |
222 | if (t->bytes > bs[rw]) | |
223 | bs[rw] = t->bytes; | |
224 | ||
8c1fdf04 | 225 | ios[rw]++; |
6df8adaa | 226 | td->o.size += t->bytes; |
8c1fdf04 | 227 | store_ipo(td, t->sector, t->bytes, rw, ttime); |
fb7b71a3 JA |
228 | } |
229 | ||
8c1fdf04 JA |
230 | /* |
231 | * Load a blktrace file by reading all the blk_io_trace entries, and storing | |
232 | * them as io_pieces like the fio text version would do. | |
233 | */ | |
fb7b71a3 JA |
234 | int load_blktrace(struct thread_data *td, const char *filename) |
235 | { | |
a61eddec | 236 | unsigned long long ttime, delay; |
fb7b71a3 | 237 | struct blk_io_trace t; |
8c1fdf04 | 238 | unsigned long ios[2]; |
a61eddec | 239 | unsigned int cpu; |
d84f8d49 | 240 | unsigned int rw_bs[2]; |
e2887563 | 241 | struct fifo *fifo; |
fb7b71a3 JA |
242 | int fd; |
243 | ||
244 | fd = open(filename, O_RDONLY); | |
245 | if (fd < 0) { | |
246 | td_verror(td, errno, "open blktrace file"); | |
247 | return 1; | |
248 | } | |
249 | ||
e2887563 JA |
250 | fifo = fifo_alloc(TRACE_FIFO_SIZE); |
251 | ||
6df8adaa JA |
252 | td->o.size = 0; |
253 | ||
a61eddec | 254 | cpu = 0; |
d84f8d49 JA |
255 | ttime = 0; |
256 | ios[0] = ios[1] = 0; | |
257 | rw_bs[0] = rw_bs[1] = 0; | |
fb7b71a3 | 258 | do { |
e2887563 | 259 | int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t)); |
fb7b71a3 | 260 | |
e2887563 | 261 | if (ret < 0) |
8c1fdf04 | 262 | goto err; |
e2887563 JA |
263 | else if (!ret) |
264 | break; | |
265 | else if (ret < (int) sizeof(t)) { | |
266 | log_err("fio: short fifo get\n"); | |
fb7b71a3 | 267 | break; |
fb7b71a3 JA |
268 | } |
269 | ||
270 | if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { | |
f12b323f | 271 | log_err("fio: bad magic in blktrace data: %x\n", t.magic); |
8c1fdf04 | 272 | goto err; |
fb7b71a3 JA |
273 | } |
274 | if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) { | |
275 | log_err("fio: bad blktrace version %d\n", t.magic & 0xff); | |
8c1fdf04 | 276 | goto err; |
fb7b71a3 | 277 | } |
f12b323f JA |
278 | ret = discard_pdu(td, fifo, fd, &t); |
279 | if (ret < 0) { | |
fb7b71a3 | 280 | td_verror(td, ret, "blktrace lseek"); |
8c1fdf04 | 281 | goto err; |
f12b323f JA |
282 | } else if (t.pdu_len != ret) { |
283 | log_err("fio: discarded %d of %d\n", ret, t.pdu_len); | |
284 | goto err; | |
fb7b71a3 | 285 | } |
a61eddec | 286 | if (!ttime) { |
8c1fdf04 | 287 | ttime = t.time; |
a61eddec JA |
288 | cpu = t.cpu; |
289 | } | |
290 | delay = 0; | |
291 | if (cpu == t.cpu) | |
292 | delay = t.time - ttime; | |
d84f8d49 | 293 | handle_trace(td, &t, delay, ios, rw_bs); |
8c1fdf04 | 294 | ttime = t.time; |
a61eddec | 295 | cpu = t.cpu; |
fb7b71a3 JA |
296 | } while (1); |
297 | ||
38470f85 | 298 | fifo_free(fifo); |
fb7b71a3 | 299 | close(fd); |
8c1fdf04 JA |
300 | |
301 | if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) { | |
302 | log_err("fio: found no ios in blktrace data\n"); | |
303 | return 1; | |
d84f8d49 | 304 | } else if (ios[DDIR_READ] && !ios[DDIR_READ]) { |
8c1fdf04 | 305 | td->o.td_ddir = TD_DDIR_READ; |
d84f8d49 JA |
306 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
307 | } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) { | |
8c1fdf04 | 308 | td->o.td_ddir = TD_DDIR_WRITE; |
d84f8d49 JA |
309 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; |
310 | } else { | |
8c1fdf04 | 311 | td->o.td_ddir = TD_DDIR_RW; |
d84f8d49 JA |
312 | td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ]; |
313 | td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE]; | |
314 | } | |
8c1fdf04 JA |
315 | |
316 | /* | |
317 | * We need to do direct/raw ios to the device, to avoid getting | |
318 | * read-ahead in our way. | |
319 | */ | |
320 | td->o.odirect = 1; | |
321 | ||
fb7b71a3 | 322 | return 0; |
8c1fdf04 JA |
323 | err: |
324 | close(fd); | |
38470f85 | 325 | fifo_free(fifo); |
8c1fdf04 | 326 | return 1; |
fb7b71a3 | 327 | } |