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 | ||
f29b25a3 | 60 | restart: |
3c39a379 JA |
61 | if (!list_empty(&td->io_log_list)) { |
62 | ipo = list_entry(td->io_log_list.next, struct io_piece, list); | |
63 | list_del(&ipo->list); | |
64 | io_u->offset = ipo->offset; | |
65 | io_u->buflen = ipo->len; | |
66 | io_u->ddir = ipo->ddir; | |
53cdc686 | 67 | io_u->file = ipo->file; |
a61eddec | 68 | |
f29b25a3 JA |
69 | /* |
70 | * invalid ddir, this is a file action | |
71 | */ | |
72 | if (io_u->ddir == DDIR_INVAL) { | |
73 | struct fio_file *f = &td->files[ipo->fileno]; | |
74 | ||
75 | if (ipo->file_action == FIO_LOG_OPEN_FILE) { | |
76 | assert(!td_io_open_file(td, f)); | |
77 | free(ipo); | |
78 | goto restart; | |
79 | } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) { | |
80 | td_io_close_file(td, f); | |
81 | free(ipo); | |
82 | goto restart; | |
83 | } | |
84 | } | |
85 | ||
a61eddec JA |
86 | if (ipo->delay) |
87 | iolog_delay(td, ipo->delay); | |
88 | ||
fcb11708 JA |
89 | /* |
90 | * work around, this needs a format change to work for > 1 file | |
91 | */ | |
92 | if (!io_u->file) | |
93 | io_u->file = &td->files[0]; | |
3c39a379 JA |
94 | free(ipo); |
95 | return 0; | |
96 | } | |
97 | ||
98 | return 1; | |
99 | } | |
100 | ||
101 | void prune_io_piece_log(struct thread_data *td) | |
102 | { | |
103 | struct io_piece *ipo; | |
4b87898e | 104 | struct rb_node *n; |
3c39a379 | 105 | |
4b87898e JA |
106 | while ((n = rb_first(&td->io_hist_tree)) != NULL) { |
107 | ipo = rb_entry(n, struct io_piece, rb_node); | |
108 | rb_erase(n, &td->io_hist_tree); | |
3c39a379 JA |
109 | free(ipo); |
110 | } | |
111 | } | |
112 | ||
113 | /* | |
34403fb1 | 114 | * log a successful write, so we can unwind the log for verify |
3c39a379 JA |
115 | */ |
116 | void log_io_piece(struct thread_data *td, struct io_u *io_u) | |
117 | { | |
8de8f047 | 118 | struct rb_node **p, *parent; |
4b87898e | 119 | struct io_piece *ipo, *__ipo; |
3c39a379 | 120 | |
4b87898e | 121 | ipo = malloc(sizeof(struct io_piece)); |
53cdc686 | 122 | ipo->file = io_u->file; |
3c39a379 JA |
123 | ipo->offset = io_u->offset; |
124 | ipo->len = io_u->buflen; | |
125 | ||
8de8f047 JA |
126 | /* |
127 | * We don't need to sort the entries, if: | |
128 | * | |
129 | * Sequential writes, or | |
130 | * Random writes that lay out the file as it goes along | |
131 | * | |
132 | * For both these cases, just reading back data in the order we | |
133 | * wrote it out is the fastest. | |
134 | */ | |
160b966d JA |
135 | if (!td_random(td) || !td->o.overwrite || |
136 | (io_u->file->flags & FIO_FILE_NOSORT)) { | |
8de8f047 JA |
137 | INIT_LIST_HEAD(&ipo->list); |
138 | list_add_tail(&ipo->list, &td->io_hist_list); | |
139 | return; | |
140 | } | |
141 | ||
142 | RB_CLEAR_NODE(&ipo->rb_node); | |
143 | p = &td->io_hist_tree.rb_node; | |
144 | parent = NULL; | |
145 | ||
3c39a379 | 146 | /* |
4b87898e | 147 | * Sort the entry into the verification list |
3c39a379 | 148 | */ |
4b87898e JA |
149 | while (*p) { |
150 | parent = *p; | |
151 | ||
152 | __ipo = rb_entry(parent, struct io_piece, rb_node); | |
bb5d7d0b | 153 | if (ipo->offset <= __ipo->offset) |
4b87898e | 154 | p = &(*p)->rb_left; |
4b87898e | 155 | else |
bb5d7d0b | 156 | p = &(*p)->rb_right; |
3c39a379 JA |
157 | } |
158 | ||
4b87898e JA |
159 | rb_link_node(&ipo->rb_node, parent, p); |
160 | rb_insert_color(&ipo->rb_node, &td->io_hist_tree); | |
3c39a379 JA |
161 | } |
162 | ||
163 | void write_iolog_close(struct thread_data *td) | |
164 | { | |
165 | fflush(td->iolog_f); | |
166 | fclose(td->iolog_f); | |
167 | free(td->iolog_buf); | |
f29b25a3 JA |
168 | td->iolog_f = NULL; |
169 | td->iolog_buf = NULL; | |
3c39a379 JA |
170 | } |
171 | ||
fb71fbd7 | 172 | /* |
f29b25a3 JA |
173 | * Read version 2 iolog data. It is enhanced to include per-file logging, |
174 | * syncs, etc. | |
fb71fbd7 | 175 | */ |
f29b25a3 | 176 | static int read_iolog2(struct thread_data *td, FILE *f) |
3c39a379 JA |
177 | { |
178 | unsigned long long offset; | |
179 | unsigned int bytes; | |
f29b25a3 JA |
180 | int rw, reads, writes, fileno = 0, file_action = 0; /* stupid gcc */ |
181 | char *fname, *act; | |
3c39a379 | 182 | char *str, *p; |
3c39a379 | 183 | |
f29b25a3 JA |
184 | free_release_files(td); |
185 | ||
186 | /* | |
187 | * Read in the read iolog and store it, reuse the infrastructure | |
188 | * for doing verifications. | |
189 | */ | |
190 | str = malloc(4096); | |
191 | fname = malloc(256+16); | |
192 | act = malloc(256+16); | |
193 | ||
194 | reads = writes = 0; | |
195 | while ((p = fgets(str, 4096, f)) != NULL) { | |
196 | struct io_piece *ipo; | |
197 | int r; | |
198 | ||
199 | r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, &bytes); | |
200 | if (r == 4) { | |
201 | /* | |
202 | * Check action first | |
203 | */ | |
204 | if (!strcmp(act, "read")) | |
205 | rw = DDIR_READ; | |
206 | else if (!strcmp(act, "write")) | |
207 | rw = DDIR_WRITE; | |
208 | else if (!strcmp(act, "sync")) | |
209 | rw = DDIR_SYNC; | |
210 | else { | |
211 | log_err("fio: bad iolog file action: %s\n",act); | |
212 | continue; | |
213 | } | |
214 | } else if (r == 2) { | |
215 | rw = DDIR_INVAL; | |
216 | if (!strcmp(act, "add")) { | |
217 | td->o.nr_files++; | |
218 | fileno = add_file(td, fname); | |
219 | file_action = FIO_LOG_ADD_FILE; | |
220 | continue; | |
221 | } else if (!strcmp(act, "open")) { | |
222 | fileno = get_fileno(td, fname); | |
223 | file_action = FIO_LOG_OPEN_FILE; | |
224 | } else if (!strcmp(act, "close")) { | |
225 | fileno = get_fileno(td, fname); | |
226 | file_action = FIO_LOG_CLOSE_FILE; | |
227 | } else { | |
228 | log_err("fio: bad iolog file action: %s\n",act); | |
229 | continue; | |
230 | } | |
231 | } else { | |
232 | log_err("bad iolog2: %s", p); | |
233 | continue; | |
234 | } | |
235 | ||
236 | if (rw == DDIR_READ) | |
237 | reads++; | |
238 | else if (rw == DDIR_WRITE) | |
239 | writes++; | |
240 | else if (rw != DDIR_SYNC && rw != DDIR_INVAL) { | |
241 | log_err("bad ddir: %d\n", rw); | |
242 | continue; | |
243 | } | |
244 | ||
245 | /* | |
246 | * Make note of file | |
247 | */ | |
248 | ipo = malloc(sizeof(*ipo)); | |
249 | memset(ipo, 0, sizeof(*ipo)); | |
250 | INIT_LIST_HEAD(&ipo->list); | |
251 | ipo->offset = offset; | |
252 | ipo->len = bytes; | |
253 | ipo->ddir = (enum fio_ddir) rw; | |
254 | if (bytes > td->o.max_bs[rw]) | |
255 | td->o.max_bs[rw] = bytes; | |
256 | if (rw == DDIR_INVAL) { | |
257 | ipo->fileno = fileno; | |
258 | ipo->file_action = file_action; | |
259 | } | |
260 | list_add_tail(&ipo->list, &td->io_log_list); | |
3c39a379 JA |
261 | } |
262 | ||
f29b25a3 JA |
263 | free(str); |
264 | free(act); | |
265 | free(fname); | |
266 | ||
267 | if (!reads && !writes) | |
268 | return 1; | |
269 | else if (reads && !writes) | |
270 | td->o.td_ddir = TD_DDIR_READ; | |
271 | else if (!reads && writes) | |
272 | td->o.td_ddir = TD_DDIR_WRITE; | |
273 | else | |
274 | td->o.td_ddir = TD_DDIR_RW; | |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
279 | /* | |
280 | * Read version 1 iolog data. | |
281 | */ | |
282 | static int read_iolog(struct thread_data *td, FILE *f) | |
283 | { | |
284 | unsigned long long offset; | |
285 | unsigned int bytes; | |
286 | char *str, *p; | |
287 | int rw, reads, writes; | |
288 | ||
3c39a379 JA |
289 | /* |
290 | * Read in the read iolog and store it, reuse the infrastructure | |
291 | * for doing verifications. | |
292 | */ | |
293 | str = malloc(4096); | |
fb71fbd7 | 294 | reads = writes = 0; |
3c39a379 JA |
295 | while ((p = fgets(str, 4096, f)) != NULL) { |
296 | struct io_piece *ipo; | |
297 | ||
298 | if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) { | |
3b70d7e5 | 299 | log_err("bad iolog: %s\n", p); |
3c39a379 JA |
300 | continue; |
301 | } | |
302 | if (rw == DDIR_READ) | |
303 | reads++; | |
304 | else if (rw == DDIR_WRITE) | |
305 | writes++; | |
c38e9468 | 306 | else if (rw != DDIR_SYNC) { |
3b70d7e5 | 307 | log_err("bad ddir: %d\n", rw); |
3c39a379 JA |
308 | continue; |
309 | } | |
310 | ||
311 | ipo = malloc(sizeof(*ipo)); | |
733ed597 | 312 | memset(ipo, 0, sizeof(*ipo)); |
3c39a379 JA |
313 | INIT_LIST_HEAD(&ipo->list); |
314 | ipo->offset = offset; | |
315 | ipo->len = bytes; | |
1e97cce9 | 316 | ipo->ddir = (enum fio_ddir) rw; |
2dc1bbeb JA |
317 | if (bytes > td->o.max_bs[rw]) |
318 | td->o.max_bs[rw] = bytes; | |
3c39a379 | 319 | list_add_tail(&ipo->list, &td->io_log_list); |
3c39a379 JA |
320 | } |
321 | ||
322 | free(str); | |
3c39a379 | 323 | |
fb71fbd7 | 324 | if (!reads && !writes) |
3c39a379 | 325 | return 1; |
fb71fbd7 | 326 | else if (reads && !writes) |
2dc1bbeb | 327 | td->o.td_ddir = TD_DDIR_READ; |
3c39a379 | 328 | else if (!reads && writes) |
36361ebb | 329 | td->o.td_ddir = TD_DDIR_WRITE; |
3c39a379 | 330 | else |
2dc1bbeb | 331 | td->o.td_ddir = TD_DDIR_RW; |
3c39a379 JA |
332 | |
333 | return 0; | |
334 | } | |
8914a9d8 | 335 | |
fb71fbd7 | 336 | /* |
f29b25a3 | 337 | * open iolog, check version, and call appropriate parser |
fb71fbd7 | 338 | */ |
f29b25a3 | 339 | static int init_iolog_read(struct thread_data *td) |
fb71fbd7 | 340 | { |
f29b25a3 | 341 | char buffer[256], *p; |
076efc7c | 342 | FILE *f; |
f29b25a3 JA |
343 | int ret; |
344 | ||
345 | f = fopen(td->o.read_iolog_file, "r"); | |
346 | if (!f) { | |
347 | perror("fopen read iolog"); | |
348 | return 1; | |
349 | } | |
fb71fbd7 | 350 | |
f29b25a3 JA |
351 | p = fgets(buffer, sizeof(buffer), f); |
352 | if (!p) { | |
353 | td_verror(td, errno, "iolog read"); | |
354 | log_err("fio: unable to read iolog\n"); | |
733ed597 JA |
355 | return 1; |
356 | } | |
357 | ||
f29b25a3 JA |
358 | /* |
359 | * version 2 of the iolog stores a specific string as the | |
360 | * first line, check for that | |
361 | */ | |
362 | if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2))) | |
363 | ret = read_iolog2(td, f); | |
364 | else { | |
365 | /* | |
366 | * seek back to the beginning | |
367 | */ | |
368 | if (fseek(f, 0, SEEK_SET) < 0) { | |
369 | td_verror(td, errno, "iolog read"); | |
370 | log_err("fio: unable to read iolog\n"); | |
371 | return 1; | |
372 | } | |
373 | ||
374 | ret = read_iolog(td, f); | |
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 | ||
463 | for (i = 0; i < log->nr_samples; i++) | |
464 | fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir); | |
465 | ||
466 | fclose(f); | |
467 | free(log->log); | |
468 | free(log); | |
469 | } | |
bb3884d8 JA |
470 | |
471 | void finish_log(struct thread_data *td, struct io_log *log, const char *name) | |
472 | { | |
473 | char file_name[256]; | |
474 | ||
475 | snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name); | |
476 | __finish_log(log, file_name); | |
477 | } |