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