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