Commit | Line | Data |
---|---|---|
f29b25a3 JA |
1 | /* |
2 | * Code related to writing an iolog of what a thread is doing, and to | |
3 | * later read that back and replay | |
4 | */ | |
3c39a379 JA |
5 | #include <stdio.h> |
6 | #include <stdlib.h> | |
f29b25a3 | 7 | #include <assert.h> |
3c39a379 JA |
8 | #include "list.h" |
9 | #include "fio.h" | |
10 | ||
f29b25a3 JA |
11 | static const char iolog_ver2[] = "fio version 2 iolog"; |
12 | ||
691c8fb0 JA |
13 | void queue_io_piece(struct thread_data *td, struct io_piece *ipo) |
14 | { | |
15 | list_add_tail(&ipo->list, &td->io_log_list); | |
16 | td->total_io_size += ipo->len; | |
17 | } | |
18 | ||
f29b25a3 | 19 | void log_io_u(struct thread_data *td, struct io_u *io_u) |
3c39a379 | 20 | { |
f29b25a3 JA |
21 | const char *act[] = { "read", "write", "sync" }; |
22 | ||
23 | assert(io_u->ddir < 3); | |
24 | ||
25 | if (!td->o.write_iolog_file) | |
26 | return; | |
27 | ||
5ec10eaa JA |
28 | fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name, |
29 | act[io_u->ddir], io_u->offset, | |
30 | io_u->buflen); | |
f29b25a3 JA |
31 | } |
32 | ||
33 | void log_file(struct thread_data *td, struct fio_file *f, | |
34 | enum file_log_act what) | |
35 | { | |
36 | const char *act[] = { "add", "open", "close" }; | |
37 | ||
38 | assert(what < 3); | |
39 | ||
40 | if (!td->o.write_iolog_file) | |
41 | return; | |
42 | ||
393ca7e9 JA |
43 | |
44 | /* | |
45 | * this happens on the pre-open/close done before the job starts | |
46 | */ | |
47 | if (!td->iolog_f) | |
48 | return; | |
49 | ||
f29b25a3 | 50 | fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]); |
3c39a379 JA |
51 | } |
52 | ||
a61eddec JA |
53 | static void iolog_delay(struct thread_data *td, unsigned long delay) |
54 | { | |
55 | unsigned long usec = utime_since_now(&td->last_issue); | |
56 | ||
57 | if (delay < usec) | |
58 | return; | |
59 | ||
60 | delay -= usec; | |
61 | ||
62 | /* | |
63 | * less than 100 usec delay, just regard it as noise | |
64 | */ | |
65 | if (delay < 100) | |
66 | return; | |
67 | ||
68 | usec_sleep(td, delay); | |
69 | } | |
70 | ||
f718273e JA |
71 | static int ipo_special(struct thread_data *td, struct io_piece *ipo) |
72 | { | |
73 | struct fio_file *f; | |
74 | int ret; | |
75 | ||
76 | /* | |
77 | * Not a special ipo | |
78 | */ | |
79 | if (ipo->ddir != DDIR_INVAL) | |
80 | return 0; | |
81 | ||
82 | f = td->files[ipo->fileno]; | |
83 | ||
84 | switch (ipo->file_action) { | |
85 | case FIO_LOG_OPEN_FILE: | |
86 | ret = td_io_open_file(td, f); | |
87 | if (!ret) { | |
88 | free(ipo); | |
89 | break; | |
90 | } | |
91 | td_verror(td, ret, "iolog open file"); | |
92 | return -1; | |
93 | case FIO_LOG_CLOSE_FILE: | |
94 | td_io_close_file(td, f); | |
95 | break; | |
96 | case FIO_LOG_UNLINK_FILE: | |
97 | unlink(f->file_name); | |
98 | break; | |
99 | default: | |
100 | log_err("fio: bad file action %d\n", ipo->file_action); | |
101 | break; | |
102 | } | |
103 | ||
104 | return 1; | |
105 | } | |
106 | ||
3c39a379 JA |
107 | int read_iolog_get(struct thread_data *td, struct io_u *io_u) |
108 | { | |
109 | struct io_piece *ipo; | |
110 | ||
b3f4b4f5 | 111 | while (!list_empty(&td->io_log_list)) { |
f718273e JA |
112 | int ret; |
113 | ||
3c39a379 JA |
114 | ipo = list_entry(td->io_log_list.next, struct io_piece, list); |
115 | list_del(&ipo->list); | |
a61eddec | 116 | |
f718273e JA |
117 | ret = ipo_special(td, ipo); |
118 | if (ret < 0) { | |
119 | free(ipo); | |
120 | break; | |
121 | } else if (ret > 0) { | |
122 | free(ipo); | |
123 | continue; | |
f29b25a3 JA |
124 | } |
125 | ||
429f6675 JA |
126 | io_u->offset = ipo->offset; |
127 | io_u->buflen = ipo->len; | |
128 | io_u->ddir = ipo->ddir; | |
126d65c6 | 129 | io_u->file = td->files[ipo->fileno]; |
429f6675 JA |
130 | get_file(io_u->file); |
131 | ||
ee56ad50 JA |
132 | dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset, |
133 | io_u->buflen, io_u->file->file_name); | |
134 | ||
a61eddec JA |
135 | if (ipo->delay) |
136 | iolog_delay(td, ipo->delay); | |
137 | ||
3c39a379 JA |
138 | free(ipo); |
139 | return 0; | |
140 | } | |
141 | ||
20e354ef | 142 | td->done = 1; |
3c39a379 JA |
143 | return 1; |
144 | } | |
145 | ||
146 | void prune_io_piece_log(struct thread_data *td) | |
147 | { | |
148 | struct io_piece *ipo; | |
4b87898e | 149 | struct rb_node *n; |
3c39a379 | 150 | |
4b87898e JA |
151 | while ((n = rb_first(&td->io_hist_tree)) != NULL) { |
152 | ipo = rb_entry(n, struct io_piece, rb_node); | |
153 | rb_erase(n, &td->io_hist_tree); | |
3c39a379 JA |
154 | free(ipo); |
155 | } | |
8ce9cd3d JA |
156 | |
157 | while (!list_empty(&td->io_hist_list)) { | |
158 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
159 | list_del(&ipo->list); | |
160 | free(ipo); | |
161 | } | |
3c39a379 JA |
162 | } |
163 | ||
164 | /* | |
34403fb1 | 165 | * log a successful write, so we can unwind the log for verify |
3c39a379 JA |
166 | */ |
167 | void log_io_piece(struct thread_data *td, struct io_u *io_u) | |
168 | { | |
8de8f047 | 169 | struct rb_node **p, *parent; |
4b87898e | 170 | struct io_piece *ipo, *__ipo; |
3c39a379 | 171 | |
4b87898e | 172 | ipo = malloc(sizeof(struct io_piece)); |
53cdc686 | 173 | ipo->file = io_u->file; |
3c39a379 JA |
174 | ipo->offset = io_u->offset; |
175 | ipo->len = io_u->buflen; | |
176 | ||
8de8f047 JA |
177 | /* |
178 | * We don't need to sort the entries, if: | |
179 | * | |
180 | * Sequential writes, or | |
181 | * Random writes that lay out the file as it goes along | |
182 | * | |
183 | * For both these cases, just reading back data in the order we | |
184 | * wrote it out is the fastest. | |
185 | */ | |
9b23c9fb | 186 | if (!td_random(td) || !td->o.overwrite) { |
8de8f047 JA |
187 | INIT_LIST_HEAD(&ipo->list); |
188 | list_add_tail(&ipo->list, &td->io_hist_list); | |
189 | return; | |
190 | } | |
191 | ||
192 | RB_CLEAR_NODE(&ipo->rb_node); | |
193 | p = &td->io_hist_tree.rb_node; | |
194 | parent = NULL; | |
195 | ||
3c39a379 | 196 | /* |
4b87898e | 197 | * Sort the entry into the verification list |
3c39a379 | 198 | */ |
4b87898e JA |
199 | while (*p) { |
200 | parent = *p; | |
201 | ||
202 | __ipo = rb_entry(parent, struct io_piece, rb_node); | |
bb5d7d0b | 203 | if (ipo->offset <= __ipo->offset) |
4b87898e | 204 | p = &(*p)->rb_left; |
4b87898e | 205 | else |
bb5d7d0b | 206 | p = &(*p)->rb_right; |
3c39a379 JA |
207 | } |
208 | ||
4b87898e JA |
209 | rb_link_node(&ipo->rb_node, parent, p); |
210 | rb_insert_color(&ipo->rb_node, &td->io_hist_tree); | |
3c39a379 JA |
211 | } |
212 | ||
213 | void write_iolog_close(struct thread_data *td) | |
214 | { | |
215 | fflush(td->iolog_f); | |
216 | fclose(td->iolog_f); | |
217 | free(td->iolog_buf); | |
f29b25a3 JA |
218 | td->iolog_f = NULL; |
219 | td->iolog_buf = NULL; | |
3c39a379 JA |
220 | } |
221 | ||
fb71fbd7 | 222 | /* |
f29b25a3 JA |
223 | * Read version 2 iolog data. It is enhanced to include per-file logging, |
224 | * syncs, etc. | |
fb71fbd7 | 225 | */ |
f29b25a3 | 226 | static int read_iolog2(struct thread_data *td, FILE *f) |
3c39a379 JA |
227 | { |
228 | unsigned long long offset; | |
229 | unsigned int bytes; | |
53fa9b69 | 230 | int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */ |
f29b25a3 | 231 | char *fname, *act; |
3c39a379 | 232 | char *str, *p; |
53fa9b69 | 233 | enum fio_ddir rw; |
3c39a379 | 234 | |
f29b25a3 JA |
235 | free_release_files(td); |
236 | ||
237 | /* | |
238 | * Read in the read iolog and store it, reuse the infrastructure | |
239 | * for doing verifications. | |
240 | */ | |
241 | str = malloc(4096); | |
242 | fname = malloc(256+16); | |
243 | act = malloc(256+16); | |
244 | ||
245 | reads = writes = 0; | |
246 | while ((p = fgets(str, 4096, f)) != NULL) { | |
247 | struct io_piece *ipo; | |
248 | int r; | |
249 | ||
5ec10eaa JA |
250 | r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, |
251 | &bytes); | |
f29b25a3 JA |
252 | if (r == 4) { |
253 | /* | |
254 | * Check action first | |
255 | */ | |
256 | if (!strcmp(act, "read")) | |
257 | rw = DDIR_READ; | |
258 | else if (!strcmp(act, "write")) | |
259 | rw = DDIR_WRITE; | |
260 | else if (!strcmp(act, "sync")) | |
261 | rw = DDIR_SYNC; | |
262 | else { | |
5ec10eaa JA |
263 | log_err("fio: bad iolog file action: %s\n", |
264 | act); | |
f29b25a3 JA |
265 | continue; |
266 | } | |
267 | } else if (r == 2) { | |
268 | rw = DDIR_INVAL; | |
269 | if (!strcmp(act, "add")) { | |
270 | td->o.nr_files++; | |
271 | fileno = add_file(td, fname); | |
272 | file_action = FIO_LOG_ADD_FILE; | |
273 | continue; | |
274 | } else if (!strcmp(act, "open")) { | |
275 | fileno = get_fileno(td, fname); | |
276 | file_action = FIO_LOG_OPEN_FILE; | |
277 | } else if (!strcmp(act, "close")) { | |
278 | fileno = get_fileno(td, fname); | |
279 | file_action = FIO_LOG_CLOSE_FILE; | |
280 | } else { | |
5ec10eaa JA |
281 | log_err("fio: bad iolog file action: %s\n", |
282 | act); | |
f29b25a3 JA |
283 | continue; |
284 | } | |
285 | } else { | |
286 | log_err("bad iolog2: %s", p); | |
287 | continue; | |
288 | } | |
5ec10eaa | 289 | |
f29b25a3 JA |
290 | if (rw == DDIR_READ) |
291 | reads++; | |
4241ea8f | 292 | else if (rw == DDIR_WRITE) { |
4241ea8f JA |
293 | /* |
294 | * Don't add a write for ro mode | |
295 | */ | |
296 | if (read_only) | |
297 | continue; | |
ed4aa707 | 298 | writes++; |
4241ea8f | 299 | } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) { |
f29b25a3 JA |
300 | log_err("bad ddir: %d\n", rw); |
301 | continue; | |
302 | } | |
303 | ||
304 | /* | |
305 | * Make note of file | |
306 | */ | |
307 | ipo = malloc(sizeof(*ipo)); | |
308 | memset(ipo, 0, sizeof(*ipo)); | |
309 | INIT_LIST_HEAD(&ipo->list); | |
310 | ipo->offset = offset; | |
311 | ipo->len = bytes; | |
53fa9b69 | 312 | ipo->ddir = rw; |
f29b25a3 JA |
313 | if (bytes > td->o.max_bs[rw]) |
314 | td->o.max_bs[rw] = bytes; | |
315 | if (rw == DDIR_INVAL) { | |
316 | ipo->fileno = fileno; | |
317 | ipo->file_action = file_action; | |
318 | } | |
691c8fb0 | 319 | queue_io_piece(td, ipo); |
3c39a379 JA |
320 | } |
321 | ||
f29b25a3 JA |
322 | free(str); |
323 | free(act); | |
324 | free(fname); | |
325 | ||
4241ea8f | 326 | if (writes && read_only) { |
5ec10eaa JA |
327 | log_err("fio: <%s> skips replay of %d writes due to" |
328 | " read-only\n", td->o.name, writes); | |
4241ea8f JA |
329 | writes = 0; |
330 | } | |
331 | ||
f29b25a3 JA |
332 | if (!reads && !writes) |
333 | return 1; | |
334 | else if (reads && !writes) | |
335 | td->o.td_ddir = TD_DDIR_READ; | |
336 | else if (!reads && writes) | |
337 | td->o.td_ddir = TD_DDIR_WRITE; | |
338 | else | |
339 | td->o.td_ddir = TD_DDIR_RW; | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
fb71fbd7 | 344 | /* |
f29b25a3 | 345 | * open iolog, check version, and call appropriate parser |
fb71fbd7 | 346 | */ |
f29b25a3 | 347 | static int init_iolog_read(struct thread_data *td) |
fb71fbd7 | 348 | { |
f29b25a3 | 349 | char buffer[256], *p; |
076efc7c | 350 | FILE *f; |
f29b25a3 JA |
351 | int ret; |
352 | ||
353 | f = fopen(td->o.read_iolog_file, "r"); | |
354 | if (!f) { | |
355 | perror("fopen read iolog"); | |
356 | return 1; | |
357 | } | |
fb71fbd7 | 358 | |
f29b25a3 JA |
359 | p = fgets(buffer, sizeof(buffer), f); |
360 | if (!p) { | |
361 | td_verror(td, errno, "iolog read"); | |
362 | log_err("fio: unable to read iolog\n"); | |
733ed597 JA |
363 | return 1; |
364 | } | |
365 | ||
f29b25a3 JA |
366 | /* |
367 | * version 2 of the iolog stores a specific string as the | |
368 | * first line, check for that | |
369 | */ | |
370 | if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2))) | |
371 | ret = read_iolog2(td, f); | |
372 | else { | |
aec2de20 JA |
373 | log_err("fio: iolog version 1 is no longer supported\n"); |
374 | ret = 1; | |
f29b25a3 JA |
375 | } |
376 | ||
377 | fclose(f); | |
378 | return ret; | |
379 | } | |
380 | ||
381 | /* | |
382 | * Setup a log for storing io patterns. | |
383 | */ | |
384 | static int init_iolog_write(struct thread_data *td) | |
385 | { | |
386 | struct fio_file *ff; | |
387 | FILE *f; | |
388 | unsigned int i; | |
389 | ||
2dc1bbeb | 390 | f = fopen(td->o.write_iolog_file, "w+"); |
fb71fbd7 JA |
391 | if (!f) { |
392 | perror("fopen write iolog"); | |
393 | return 1; | |
394 | } | |
395 | ||
396 | /* | |
397 | * That's it for writing, setup a log buffer and we're done. | |
398 | */ | |
399 | td->iolog_f = f; | |
400 | td->iolog_buf = malloc(8192); | |
401 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); | |
f29b25a3 JA |
402 | |
403 | /* | |
404 | * write our version line | |
405 | */ | |
406 | if (fprintf(f, "%s\n", iolog_ver2) < 0) { | |
407 | perror("iolog init\n"); | |
408 | return 1; | |
409 | } | |
410 | ||
411 | /* | |
412 | * add all known files | |
413 | */ | |
414 | for_each_file(td, ff, i) | |
415 | log_file(td, ff, FIO_LOG_ADD_FILE); | |
416 | ||
fb71fbd7 JA |
417 | return 0; |
418 | } | |
419 | ||
420 | int init_iolog(struct thread_data *td) | |
421 | { | |
b4a6a59a JA |
422 | int ret = 0; |
423 | ||
ba0fbe10 | 424 | if (td->io_ops->flags & FIO_DISKLESSIO) |
f48b467c JA |
425 | return 0; |
426 | ||
fb7b71a3 JA |
427 | if (td->o.read_iolog_file) { |
428 | /* | |
429 | * Check if it's a blktrace file and load that if possible. | |
430 | * Otherwise assume it's a normal log file and load that. | |
431 | */ | |
432 | if (is_blktrace(td->o.read_iolog_file)) | |
433 | ret = load_blktrace(td, td->o.read_iolog_file); | |
434 | else | |
435 | ret = init_iolog_read(td); | |
436 | } else if (td->o.write_iolog_file) | |
b4a6a59a | 437 | ret = init_iolog_write(td); |
fb71fbd7 | 438 | |
1e97cce9 | 439 | return ret; |
fb71fbd7 JA |
440 | } |
441 | ||
8914a9d8 JA |
442 | void setup_log(struct io_log **log) |
443 | { | |
444 | struct io_log *l = malloc(sizeof(*l)); | |
445 | ||
446 | l->nr_samples = 0; | |
447 | l->max_samples = 1024; | |
448 | l->log = malloc(l->max_samples * sizeof(struct io_sample)); | |
449 | *log = l; | |
450 | } | |
451 | ||
bb3884d8 | 452 | void __finish_log(struct io_log *log, const char *name) |
8914a9d8 | 453 | { |
8914a9d8 | 454 | unsigned int i; |
bb3884d8 | 455 | FILE *f; |
8914a9d8 | 456 | |
bb3884d8 | 457 | f = fopen(name, "w"); |
8914a9d8 JA |
458 | if (!f) { |
459 | perror("fopen log"); | |
460 | return; | |
461 | } | |
462 | ||
5ec10eaa JA |
463 | for (i = 0; i < log->nr_samples; i++) { |
464 | fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, | |
465 | log->log[i].ddir); | |
466 | } | |
8914a9d8 JA |
467 | |
468 | fclose(f); | |
469 | free(log->log); | |
470 | free(log); | |
471 | } | |
bb3884d8 JA |
472 | |
473 | void finish_log(struct thread_data *td, struct io_log *log, const char *name) | |
474 | { | |
748b23a3 | 475 | char file_name[256], *p; |
bb3884d8 | 476 | |
84f83179 | 477 | snprintf(file_name, 200, "%s_%s.log", td->o.name, name); |
748b23a3 JA |
478 | p = basename(file_name); |
479 | __finish_log(log, p); | |
bb3884d8 | 480 | } |