Commit | Line | Data |
---|---|---|
ac9b9101 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 | */ | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <libgen.h> | |
8 | #include <assert.h> | |
b26317c9 JA |
9 | #include <sys/types.h> |
10 | #include <sys/stat.h> | |
11 | #include <unistd.h> | |
aee2ab67 JA |
12 | #ifdef CONFIG_ZLIB |
13 | #include <zlib.h> | |
14 | #endif | |
15 | ||
ac9b9101 JA |
16 | #include "flist.h" |
17 | #include "fio.h" | |
18 | #include "verify.h" | |
19 | #include "trim.h" | |
243bfe19 | 20 | #include "filelock.h" |
78d55e72 | 21 | #include "lib/tp.h" |
ac9b9101 JA |
22 | |
23 | static const char iolog_ver2[] = "fio version 2 iolog"; | |
24 | ||
aee2ab67 JA |
25 | #ifdef CONFIG_ZLIB |
26 | ||
27 | struct iolog_compress { | |
28 | struct flist_head list; | |
29 | void *buf; | |
30 | size_t len; | |
31 | unsigned int seq; | |
b26317c9 | 32 | int nofree; |
aee2ab67 JA |
33 | }; |
34 | ||
35 | #define GZ_CHUNK 131072 | |
36 | ||
37 | static struct iolog_compress *get_new_chunk(unsigned int seq) | |
38 | { | |
39 | struct iolog_compress *c; | |
40 | ||
41 | c = malloc(sizeof(*c)); | |
42 | INIT_FLIST_HEAD(&c->list); | |
43 | c->buf = malloc(GZ_CHUNK); | |
44 | c->len = 0; | |
45 | c->seq = seq; | |
b26317c9 | 46 | c->nofree = 0; |
aee2ab67 JA |
47 | return c; |
48 | } | |
49 | ||
50 | static void free_chunk(struct iolog_compress *ic) | |
51 | { | |
b26317c9 JA |
52 | if (!ic->nofree) { |
53 | free(ic->buf); | |
54 | free(ic); | |
55 | } | |
aee2ab67 JA |
56 | } |
57 | ||
58 | #endif | |
59 | ||
ac9b9101 JA |
60 | void queue_io_piece(struct thread_data *td, struct io_piece *ipo) |
61 | { | |
62 | flist_add_tail(&ipo->list, &td->io_log_list); | |
63 | td->total_io_size += ipo->len; | |
64 | } | |
65 | ||
66 | void log_io_u(struct thread_data *td, struct io_u *io_u) | |
67 | { | |
68 | const char *act[] = { "read", "write", "sync", "datasync", | |
69 | "sync_file_range", "wait", "trim" }; | |
70 | ||
71 | assert(io_u->ddir <= 6); | |
72 | ||
73 | if (!td->o.write_iolog_file) | |
74 | return; | |
75 | ||
76 | fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name, | |
77 | act[io_u->ddir], io_u->offset, | |
78 | io_u->buflen); | |
79 | } | |
80 | ||
81 | void log_file(struct thread_data *td, struct fio_file *f, | |
82 | enum file_log_act what) | |
83 | { | |
84 | const char *act[] = { "add", "open", "close" }; | |
85 | ||
86 | assert(what < 3); | |
87 | ||
88 | if (!td->o.write_iolog_file) | |
89 | return; | |
90 | ||
91 | ||
92 | /* | |
93 | * this happens on the pre-open/close done before the job starts | |
94 | */ | |
95 | if (!td->iolog_f) | |
96 | return; | |
97 | ||
98 | fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]); | |
99 | } | |
100 | ||
101 | static void iolog_delay(struct thread_data *td, unsigned long delay) | |
102 | { | |
103 | unsigned long usec = utime_since_now(&td->last_issue); | |
30b18672 | 104 | unsigned long this_delay; |
ac9b9101 JA |
105 | |
106 | if (delay < usec) | |
107 | return; | |
108 | ||
109 | delay -= usec; | |
110 | ||
111 | /* | |
112 | * less than 100 usec delay, just regard it as noise | |
113 | */ | |
114 | if (delay < 100) | |
115 | return; | |
116 | ||
30b18672 JA |
117 | while (delay && !td->terminate) { |
118 | this_delay = delay; | |
119 | if (this_delay > 500000) | |
120 | this_delay = 500000; | |
121 | ||
122 | usec_sleep(td, this_delay); | |
123 | delay -= this_delay; | |
124 | } | |
ac9b9101 JA |
125 | } |
126 | ||
127 | static int ipo_special(struct thread_data *td, struct io_piece *ipo) | |
128 | { | |
129 | struct fio_file *f; | |
130 | int ret; | |
131 | ||
132 | /* | |
133 | * Not a special ipo | |
134 | */ | |
135 | if (ipo->ddir != DDIR_INVAL) | |
136 | return 0; | |
137 | ||
138 | f = td->files[ipo->fileno]; | |
139 | ||
140 | switch (ipo->file_action) { | |
141 | case FIO_LOG_OPEN_FILE: | |
142 | ret = td_io_open_file(td, f); | |
143 | if (!ret) | |
144 | break; | |
145 | td_verror(td, ret, "iolog open file"); | |
146 | return -1; | |
147 | case FIO_LOG_CLOSE_FILE: | |
148 | td_io_close_file(td, f); | |
149 | break; | |
150 | case FIO_LOG_UNLINK_FILE: | |
151 | unlink(f->file_name); | |
152 | break; | |
153 | default: | |
154 | log_err("fio: bad file action %d\n", ipo->file_action); | |
155 | break; | |
156 | } | |
157 | ||
158 | return 1; | |
159 | } | |
160 | ||
161 | int read_iolog_get(struct thread_data *td, struct io_u *io_u) | |
162 | { | |
163 | struct io_piece *ipo; | |
164 | unsigned long elapsed; | |
3c3ed070 | 165 | |
ac9b9101 JA |
166 | while (!flist_empty(&td->io_log_list)) { |
167 | int ret; | |
168 | ||
169 | ipo = flist_entry(td->io_log_list.next, struct io_piece, list); | |
170 | flist_del(&ipo->list); | |
171 | remove_trim_entry(td, ipo); | |
172 | ||
173 | ret = ipo_special(td, ipo); | |
174 | if (ret < 0) { | |
175 | free(ipo); | |
176 | break; | |
177 | } else if (ret > 0) { | |
178 | free(ipo); | |
179 | continue; | |
180 | } | |
181 | ||
182 | io_u->ddir = ipo->ddir; | |
183 | if (ipo->ddir != DDIR_WAIT) { | |
184 | io_u->offset = ipo->offset; | |
185 | io_u->buflen = ipo->len; | |
186 | io_u->file = td->files[ipo->fileno]; | |
187 | get_file(io_u->file); | |
188 | dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset, | |
189 | io_u->buflen, io_u->file->file_name); | |
190 | if (ipo->delay) | |
191 | iolog_delay(td, ipo->delay); | |
192 | } else { | |
193 | elapsed = mtime_since_genesis(); | |
194 | if (ipo->delay > elapsed) | |
195 | usec_sleep(td, (ipo->delay - elapsed) * 1000); | |
ac9b9101 JA |
196 | } |
197 | ||
198 | free(ipo); | |
3c3ed070 | 199 | |
ac9b9101 JA |
200 | if (io_u->ddir != DDIR_WAIT) |
201 | return 0; | |
202 | } | |
203 | ||
204 | td->done = 1; | |
205 | return 1; | |
206 | } | |
207 | ||
208 | void prune_io_piece_log(struct thread_data *td) | |
209 | { | |
210 | struct io_piece *ipo; | |
211 | struct rb_node *n; | |
212 | ||
213 | while ((n = rb_first(&td->io_hist_tree)) != NULL) { | |
214 | ipo = rb_entry(n, struct io_piece, rb_node); | |
215 | rb_erase(n, &td->io_hist_tree); | |
216 | remove_trim_entry(td, ipo); | |
217 | td->io_hist_len--; | |
218 | free(ipo); | |
219 | } | |
220 | ||
221 | while (!flist_empty(&td->io_hist_list)) { | |
222 | ipo = flist_entry(td->io_hist_list.next, struct io_piece, list); | |
223 | flist_del(&ipo->list); | |
224 | remove_trim_entry(td, ipo); | |
225 | td->io_hist_len--; | |
226 | free(ipo); | |
227 | } | |
228 | } | |
229 | ||
230 | /* | |
231 | * log a successful write, so we can unwind the log for verify | |
232 | */ | |
233 | void log_io_piece(struct thread_data *td, struct io_u *io_u) | |
234 | { | |
235 | struct rb_node **p, *parent; | |
236 | struct io_piece *ipo, *__ipo; | |
237 | ||
238 | ipo = malloc(sizeof(struct io_piece)); | |
239 | init_ipo(ipo); | |
240 | ipo->file = io_u->file; | |
241 | ipo->offset = io_u->offset; | |
242 | ipo->len = io_u->buflen; | |
da0a7bd2 | 243 | ipo->numberio = io_u->numberio; |
f9401285 JA |
244 | ipo->flags = IP_F_IN_FLIGHT; |
245 | ||
246 | io_u->ipo = ipo; | |
ac9b9101 JA |
247 | |
248 | if (io_u_should_trim(td, io_u)) { | |
249 | flist_add_tail(&ipo->trim_list, &td->trim_list); | |
250 | td->trim_entries++; | |
251 | } | |
252 | ||
253 | /* | |
254 | * We don't need to sort the entries, if: | |
255 | * | |
256 | * Sequential writes, or | |
257 | * Random writes that lay out the file as it goes along | |
258 | * | |
259 | * For both these cases, just reading back data in the order we | |
260 | * wrote it out is the fastest. | |
261 | * | |
262 | * One exception is if we don't have a random map AND we are doing | |
263 | * verifies, in that case we need to check for duplicate blocks and | |
264 | * drop the old one, which we rely on the rb insert/lookup for | |
265 | * handling. | |
266 | */ | |
c4b6117b | 267 | if (((!td->o.verifysort) || !td_random(td) || !td->o.overwrite) && |
ac9b9101 JA |
268 | (file_randommap(td, ipo->file) || td->o.verify == VERIFY_NONE)) { |
269 | INIT_FLIST_HEAD(&ipo->list); | |
270 | flist_add_tail(&ipo->list, &td->io_hist_list); | |
271 | ipo->flags |= IP_F_ONLIST; | |
272 | td->io_hist_len++; | |
273 | return; | |
274 | } | |
275 | ||
276 | RB_CLEAR_NODE(&ipo->rb_node); | |
277 | ||
278 | /* | |
279 | * Sort the entry into the verification list | |
280 | */ | |
281 | restart: | |
282 | p = &td->io_hist_tree.rb_node; | |
283 | parent = NULL; | |
284 | while (*p) { | |
285 | parent = *p; | |
286 | ||
287 | __ipo = rb_entry(parent, struct io_piece, rb_node); | |
288 | if (ipo->file < __ipo->file) | |
289 | p = &(*p)->rb_left; | |
290 | else if (ipo->file > __ipo->file) | |
291 | p = &(*p)->rb_right; | |
292 | else if (ipo->offset < __ipo->offset) | |
293 | p = &(*p)->rb_left; | |
294 | else if (ipo->offset > __ipo->offset) | |
295 | p = &(*p)->rb_right; | |
296 | else { | |
885ac623 JA |
297 | dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu", |
298 | __ipo->offset, __ipo->len, | |
299 | ipo->offset, ipo->len); | |
ac9b9101 JA |
300 | td->io_hist_len--; |
301 | rb_erase(parent, &td->io_hist_tree); | |
302 | remove_trim_entry(td, __ipo); | |
303 | free(__ipo); | |
304 | goto restart; | |
305 | } | |
306 | } | |
307 | ||
308 | rb_link_node(&ipo->rb_node, parent, p); | |
309 | rb_insert_color(&ipo->rb_node, &td->io_hist_tree); | |
310 | ipo->flags |= IP_F_ONRB; | |
311 | td->io_hist_len++; | |
312 | } | |
313 | ||
890b6656 JA |
314 | void unlog_io_piece(struct thread_data *td, struct io_u *io_u) |
315 | { | |
316 | struct io_piece *ipo = io_u->ipo; | |
317 | ||
318 | if (!ipo) | |
319 | return; | |
320 | ||
321 | if (ipo->flags & IP_F_ONRB) | |
322 | rb_erase(&ipo->rb_node, &td->io_hist_tree); | |
323 | else if (ipo->flags & IP_F_ONLIST) | |
324 | flist_del(&ipo->list); | |
325 | ||
326 | free(ipo); | |
327 | io_u->ipo = NULL; | |
328 | td->io_hist_len--; | |
329 | } | |
330 | ||
331 | void trim_io_piece(struct thread_data *td, struct io_u *io_u) | |
332 | { | |
333 | struct io_piece *ipo = io_u->ipo; | |
334 | ||
335 | if (!ipo) | |
336 | return; | |
337 | ||
338 | ipo->len = io_u->xfer_buflen - io_u->resid; | |
339 | } | |
340 | ||
ac9b9101 JA |
341 | void write_iolog_close(struct thread_data *td) |
342 | { | |
343 | fflush(td->iolog_f); | |
344 | fclose(td->iolog_f); | |
345 | free(td->iolog_buf); | |
346 | td->iolog_f = NULL; | |
347 | td->iolog_buf = NULL; | |
348 | } | |
349 | ||
350 | /* | |
351 | * Read version 2 iolog data. It is enhanced to include per-file logging, | |
352 | * syncs, etc. | |
353 | */ | |
354 | static int read_iolog2(struct thread_data *td, FILE *f) | |
355 | { | |
356 | unsigned long long offset; | |
357 | unsigned int bytes; | |
358 | int reads, writes, waits, fileno = 0, file_action = 0; /* stupid gcc */ | |
359 | char *fname, *act; | |
360 | char *str, *p; | |
361 | enum fio_ddir rw; | |
362 | ||
363 | free_release_files(td); | |
364 | ||
365 | /* | |
366 | * Read in the read iolog and store it, reuse the infrastructure | |
367 | * for doing verifications. | |
368 | */ | |
369 | str = malloc(4096); | |
370 | fname = malloc(256+16); | |
371 | act = malloc(256+16); | |
372 | ||
373 | reads = writes = waits = 0; | |
374 | while ((p = fgets(str, 4096, f)) != NULL) { | |
375 | struct io_piece *ipo; | |
376 | int r; | |
377 | ||
378 | r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, | |
379 | &bytes); | |
380 | if (r == 4) { | |
381 | /* | |
382 | * Check action first | |
383 | */ | |
384 | if (!strcmp(act, "wait")) | |
385 | rw = DDIR_WAIT; | |
386 | else if (!strcmp(act, "read")) | |
387 | rw = DDIR_READ; | |
388 | else if (!strcmp(act, "write")) | |
389 | rw = DDIR_WRITE; | |
390 | else if (!strcmp(act, "sync")) | |
391 | rw = DDIR_SYNC; | |
392 | else if (!strcmp(act, "datasync")) | |
393 | rw = DDIR_DATASYNC; | |
394 | else if (!strcmp(act, "trim")) | |
395 | rw = DDIR_TRIM; | |
396 | else { | |
397 | log_err("fio: bad iolog file action: %s\n", | |
398 | act); | |
399 | continue; | |
400 | } | |
033ace1e | 401 | fileno = get_fileno(td, fname); |
ac9b9101 JA |
402 | } else if (r == 2) { |
403 | rw = DDIR_INVAL; | |
404 | if (!strcmp(act, "add")) { | |
5903e7b7 | 405 | fileno = add_file(td, fname, 0, 1); |
ac9b9101 JA |
406 | file_action = FIO_LOG_ADD_FILE; |
407 | continue; | |
408 | } else if (!strcmp(act, "open")) { | |
409 | fileno = get_fileno(td, fname); | |
410 | file_action = FIO_LOG_OPEN_FILE; | |
411 | } else if (!strcmp(act, "close")) { | |
412 | fileno = get_fileno(td, fname); | |
413 | file_action = FIO_LOG_CLOSE_FILE; | |
414 | } else { | |
415 | log_err("fio: bad iolog file action: %s\n", | |
416 | act); | |
417 | continue; | |
418 | } | |
419 | } else { | |
420 | log_err("bad iolog2: %s", p); | |
421 | continue; | |
422 | } | |
423 | ||
424 | if (rw == DDIR_READ) | |
425 | reads++; | |
426 | else if (rw == DDIR_WRITE) { | |
427 | /* | |
428 | * Don't add a write for ro mode | |
429 | */ | |
430 | if (read_only) | |
431 | continue; | |
432 | writes++; | |
433 | } else if (rw == DDIR_WAIT) { | |
434 | waits++; | |
435 | } else if (rw == DDIR_INVAL) { | |
436 | } else if (!ddir_sync(rw)) { | |
437 | log_err("bad ddir: %d\n", rw); | |
438 | continue; | |
439 | } | |
440 | ||
441 | /* | |
442 | * Make note of file | |
443 | */ | |
444 | ipo = malloc(sizeof(*ipo)); | |
445 | init_ipo(ipo); | |
446 | ipo->ddir = rw; | |
447 | if (rw == DDIR_WAIT) { | |
448 | ipo->delay = offset; | |
449 | } else { | |
450 | ipo->offset = offset; | |
451 | ipo->len = bytes; | |
42793d94 | 452 | if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw]) |
ac9b9101 JA |
453 | td->o.max_bs[rw] = bytes; |
454 | ipo->fileno = fileno; | |
455 | ipo->file_action = file_action; | |
70afff59 | 456 | td->o.size += bytes; |
ac9b9101 | 457 | } |
3c3ed070 | 458 | |
ac9b9101 JA |
459 | queue_io_piece(td, ipo); |
460 | } | |
461 | ||
462 | free(str); | |
463 | free(act); | |
464 | free(fname); | |
465 | ||
466 | if (writes && read_only) { | |
467 | log_err("fio: <%s> skips replay of %d writes due to" | |
468 | " read-only\n", td->o.name, writes); | |
469 | writes = 0; | |
470 | } | |
471 | ||
472 | if (!reads && !writes && !waits) | |
473 | return 1; | |
474 | else if (reads && !writes) | |
475 | td->o.td_ddir = TD_DDIR_READ; | |
476 | else if (!reads && writes) | |
477 | td->o.td_ddir = TD_DDIR_WRITE; | |
478 | else | |
479 | td->o.td_ddir = TD_DDIR_RW; | |
480 | ||
481 | return 0; | |
482 | } | |
483 | ||
484 | /* | |
485 | * open iolog, check version, and call appropriate parser | |
486 | */ | |
487 | static int init_iolog_read(struct thread_data *td) | |
488 | { | |
489 | char buffer[256], *p; | |
490 | FILE *f; | |
491 | int ret; | |
492 | ||
493 | f = fopen(td->o.read_iolog_file, "r"); | |
494 | if (!f) { | |
495 | perror("fopen read iolog"); | |
496 | return 1; | |
497 | } | |
498 | ||
499 | p = fgets(buffer, sizeof(buffer), f); | |
500 | if (!p) { | |
501 | td_verror(td, errno, "iolog read"); | |
502 | log_err("fio: unable to read iolog\n"); | |
503 | fclose(f); | |
504 | return 1; | |
505 | } | |
506 | ||
507 | /* | |
508 | * version 2 of the iolog stores a specific string as the | |
509 | * first line, check for that | |
510 | */ | |
511 | if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2))) | |
512 | ret = read_iolog2(td, f); | |
513 | else { | |
514 | log_err("fio: iolog version 1 is no longer supported\n"); | |
515 | ret = 1; | |
516 | } | |
517 | ||
518 | fclose(f); | |
519 | return ret; | |
520 | } | |
521 | ||
522 | /* | |
523 | * Set up a log for storing io patterns. | |
524 | */ | |
525 | static int init_iolog_write(struct thread_data *td) | |
526 | { | |
527 | struct fio_file *ff; | |
528 | FILE *f; | |
529 | unsigned int i; | |
530 | ||
531 | f = fopen(td->o.write_iolog_file, "a"); | |
532 | if (!f) { | |
533 | perror("fopen write iolog"); | |
534 | return 1; | |
535 | } | |
536 | ||
537 | /* | |
538 | * That's it for writing, setup a log buffer and we're done. | |
539 | */ | |
540 | td->iolog_f = f; | |
541 | td->iolog_buf = malloc(8192); | |
542 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); | |
543 | ||
544 | /* | |
545 | * write our version line | |
546 | */ | |
547 | if (fprintf(f, "%s\n", iolog_ver2) < 0) { | |
548 | perror("iolog init\n"); | |
549 | return 1; | |
550 | } | |
551 | ||
552 | /* | |
553 | * add all known files | |
554 | */ | |
555 | for_each_file(td, ff, i) | |
556 | log_file(td, ff, FIO_LOG_ADD_FILE); | |
557 | ||
558 | return 0; | |
559 | } | |
560 | ||
561 | int init_iolog(struct thread_data *td) | |
562 | { | |
563 | int ret = 0; | |
564 | ||
565 | if (td->o.read_iolog_file) { | |
d95b34a6 JA |
566 | int need_swap; |
567 | ||
ac9b9101 JA |
568 | /* |
569 | * Check if it's a blktrace file and load that if possible. | |
570 | * Otherwise assume it's a normal log file and load that. | |
571 | */ | |
d95b34a6 JA |
572 | if (is_blktrace(td->o.read_iolog_file, &need_swap)) |
573 | ret = load_blktrace(td, td->o.read_iolog_file, need_swap); | |
ac9b9101 JA |
574 | else |
575 | ret = init_iolog_read(td); | |
576 | } else if (td->o.write_iolog_file) | |
577 | ret = init_iolog_write(td); | |
578 | ||
f01b34ae JA |
579 | if (ret) |
580 | td_verror(td, EINVAL, "failed initializing iolog"); | |
581 | ||
ac9b9101 JA |
582 | return ret; |
583 | } | |
584 | ||
aee2ab67 JA |
585 | void setup_log(struct io_log **log, struct log_params *p, |
586 | const char *filename) | |
ac9b9101 JA |
587 | { |
588 | struct io_log *l = malloc(sizeof(*l)); | |
589 | ||
b8bc8cba | 590 | memset(l, 0, sizeof(*l)); |
ac9b9101 JA |
591 | l->nr_samples = 0; |
592 | l->max_samples = 1024; | |
aee2ab67 JA |
593 | l->log_type = p->log_type; |
594 | l->log_offset = p->log_offset; | |
595 | l->log_gz = p->log_gz; | |
b26317c9 | 596 | l->log_gz_store = p->log_gz_store; |
ae588852 | 597 | l->log = malloc(l->max_samples * log_entry_sz(l)); |
aee2ab67 | 598 | l->avg_msec = p->avg_msec; |
cb7e0ace | 599 | l->filename = strdup(filename); |
aee2ab67 JA |
600 | l->td = p->td; |
601 | ||
b26317c9 JA |
602 | if (l->log_offset) |
603 | l->log_ddir_mask = 0x80000000; | |
604 | ||
aee2ab67 JA |
605 | INIT_FLIST_HEAD(&l->chunk_list); |
606 | ||
607 | if (l->log_gz && !p->td) | |
608 | l->log_gz = 0; | |
609 | else if (l->log_gz) { | |
610 | pthread_mutex_init(&l->chunk_lock, NULL); | |
611 | p->td->flags |= TD_F_COMPRESS_LOG; | |
612 | } | |
613 | ||
ac9b9101 JA |
614 | *log = l; |
615 | } | |
616 | ||
2e802282 JA |
617 | #ifdef CONFIG_SETVBUF |
618 | static void *set_file_buffer(FILE *f) | |
619 | { | |
620 | size_t size = 1048576; | |
621 | void *buf; | |
622 | ||
623 | buf = malloc(size); | |
624 | setvbuf(f, buf, _IOFBF, size); | |
625 | return buf; | |
626 | } | |
627 | ||
628 | static void clear_file_buffer(void *buf) | |
629 | { | |
630 | free(buf); | |
631 | } | |
632 | #else | |
633 | static void *set_file_buffer(FILE *f) | |
634 | { | |
635 | return NULL; | |
636 | } | |
637 | ||
638 | static void clear_file_buffer(void *buf) | |
639 | { | |
640 | } | |
641 | #endif | |
642 | ||
518dac09 | 643 | void free_log(struct io_log *log) |
cb7e0ace JA |
644 | { |
645 | free(log->log); | |
646 | free(log->filename); | |
647 | free(log); | |
648 | } | |
649 | ||
b26317c9 | 650 | static void flush_samples(FILE *f, void *samples, uint64_t sample_size) |
ac9b9101 | 651 | { |
b26317c9 JA |
652 | struct io_sample *s; |
653 | int log_offset; | |
654 | uint64_t i, nr_samples; | |
655 | ||
656 | if (!sample_size) | |
657 | return; | |
658 | ||
659 | s = __get_sample(samples, 0, 0); | |
660 | if (s->__ddir & 0x80000000) | |
661 | log_offset = 1; | |
662 | else | |
663 | log_offset = 0; | |
664 | ||
665 | nr_samples = sample_size / __log_entry_sz(log_offset); | |
ac9b9101 | 666 | |
aee2ab67 | 667 | for (i = 0; i < nr_samples; i++) { |
b26317c9 | 668 | s = __get_sample(samples, log_offset, i); |
ac9b9101 | 669 | |
aee2ab67 | 670 | if (!log_offset) { |
ae588852 JA |
671 | fprintf(f, "%lu, %lu, %u, %u\n", |
672 | (unsigned long) s->time, | |
673 | (unsigned long) s->val, | |
b26317c9 | 674 | io_sample_ddir(s), s->bs); |
ae588852 JA |
675 | } else { |
676 | struct io_sample_offset *so = (void *) s; | |
677 | ||
678 | fprintf(f, "%lu, %lu, %u, %u, %llu\n", | |
679 | (unsigned long) s->time, | |
680 | (unsigned long) s->val, | |
b26317c9 | 681 | io_sample_ddir(s), s->bs, |
ae588852 JA |
682 | (unsigned long long) so->offset); |
683 | } | |
ac9b9101 | 684 | } |
aee2ab67 JA |
685 | } |
686 | ||
687 | #ifdef CONFIG_ZLIB | |
b26317c9 | 688 | static int z_stream_init(z_stream *stream, int gz_hdr) |
aee2ab67 | 689 | { |
b26317c9 JA |
690 | int wbits = 15; |
691 | ||
aee2ab67 JA |
692 | stream->zalloc = Z_NULL; |
693 | stream->zfree = Z_NULL; | |
694 | stream->opaque = Z_NULL; | |
695 | stream->next_in = Z_NULL; | |
696 | ||
b26317c9 JA |
697 | if (gz_hdr) |
698 | wbits += 32; | |
699 | ||
700 | if (inflateInit2(stream, wbits) != Z_OK) | |
aee2ab67 JA |
701 | return 1; |
702 | ||
703 | return 0; | |
704 | } | |
705 | ||
706 | struct flush_chunk_iter { | |
707 | unsigned int seq; | |
708 | void *buf; | |
709 | size_t buf_size; | |
710 | size_t buf_used; | |
711 | size_t chunk_sz; | |
712 | }; | |
713 | ||
b26317c9 | 714 | static void finish_chunk(z_stream *stream, FILE *f, |
aee2ab67 JA |
715 | struct flush_chunk_iter *iter) |
716 | { | |
aee2ab67 JA |
717 | int ret; |
718 | ||
719 | ret = inflateEnd(stream); | |
720 | if (ret != Z_OK) | |
721 | log_err("fio: failed to end log inflation (%d)\n", ret); | |
722 | ||
b26317c9 | 723 | flush_samples(f, iter->buf, iter->buf_used); |
aee2ab67 JA |
724 | free(iter->buf); |
725 | iter->buf = NULL; | |
726 | iter->buf_size = iter->buf_used = 0; | |
727 | } | |
728 | ||
b26317c9 | 729 | static int flush_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f, |
aee2ab67 JA |
730 | z_stream *stream, struct flush_chunk_iter *iter) |
731 | { | |
732 | if (ic->seq != iter->seq) { | |
733 | if (iter->seq) | |
b26317c9 | 734 | finish_chunk(stream, f, iter); |
aee2ab67 | 735 | |
b26317c9 | 736 | z_stream_init(stream, gz_hdr); |
aee2ab67 JA |
737 | iter->seq = ic->seq; |
738 | } | |
739 | ||
740 | stream->avail_in = ic->len; | |
741 | stream->next_in = ic->buf; | |
742 | ||
743 | if (!iter->buf_size) { | |
744 | iter->buf_size = iter->chunk_sz; | |
745 | iter->buf = malloc(iter->buf_size); | |
746 | } | |
747 | ||
748 | while (stream->avail_in) { | |
b26317c9 | 749 | size_t this_out = iter->buf_size - iter->buf_used; |
aee2ab67 JA |
750 | int err; |
751 | ||
b26317c9 | 752 | stream->avail_out = this_out; |
aee2ab67 JA |
753 | stream->next_out = iter->buf + iter->buf_used; |
754 | ||
755 | err = inflate(stream, Z_NO_FLUSH); | |
756 | if (err < 0) { | |
757 | log_err("fio: failed inflating log: %d\n", err); | |
758 | break; | |
759 | } | |
760 | ||
b26317c9 JA |
761 | iter->buf_used += this_out - stream->avail_out; |
762 | ||
763 | if (!stream->avail_out) { | |
764 | iter->buf_size += iter->chunk_sz; | |
765 | iter->buf = realloc(iter->buf, iter->buf_size); | |
766 | continue; | |
767 | } | |
768 | ||
769 | if (err == Z_STREAM_END) | |
770 | break; | |
aee2ab67 JA |
771 | } |
772 | ||
773 | free_chunk(ic); | |
774 | return 0; | |
775 | } | |
776 | ||
777 | static void flush_gz_chunks(struct io_log *log, FILE *f) | |
778 | { | |
779 | struct flush_chunk_iter iter = { .chunk_sz = log->log_gz, }; | |
780 | struct flist_head *node; | |
781 | z_stream stream; | |
782 | ||
783 | while (!flist_empty(&log->chunk_list)) { | |
784 | struct iolog_compress *ic; | |
785 | ||
786 | node = log->chunk_list.next; | |
787 | ic = flist_entry(node, struct iolog_compress, list); | |
788 | flist_del(&ic->list); | |
b26317c9 JA |
789 | |
790 | if (log->log_gz_store) { | |
791 | fwrite(ic->buf, ic->len, 1, f); | |
792 | free_chunk(ic); | |
793 | } else | |
794 | flush_chunk(ic, log->log_gz_store, f, &stream, &iter); | |
aee2ab67 JA |
795 | } |
796 | ||
797 | if (iter.seq) { | |
b26317c9 | 798 | finish_chunk(&stream, f, &iter); |
aee2ab67 JA |
799 | free(iter.buf); |
800 | } | |
801 | } | |
802 | ||
b26317c9 JA |
803 | int iolog_file_inflate(const char *file) |
804 | { | |
805 | struct flush_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, }; | |
806 | struct iolog_compress ic; | |
807 | z_stream stream; | |
808 | struct stat sb; | |
fd771971 | 809 | ssize_t ret; |
b26317c9 JA |
810 | FILE *f; |
811 | ||
b26317c9 JA |
812 | f = fopen(file, "r"); |
813 | if (!f) { | |
814 | perror("fopen"); | |
815 | return 1; | |
816 | } | |
817 | ||
fd771971 JA |
818 | if (stat(file, &sb) < 0) { |
819 | fclose(f); | |
820 | perror("stat"); | |
821 | return 1; | |
822 | } | |
823 | ||
b26317c9 JA |
824 | ic.buf = malloc(sb.st_size); |
825 | ic.len = sb.st_size; | |
826 | ic.nofree = 1; | |
827 | ic.seq = 1; | |
828 | ||
829 | ret = fread(ic.buf, ic.len, 1, f); | |
830 | if (ret < 0) { | |
831 | perror("fread"); | |
832 | fclose(f); | |
833 | return 1; | |
834 | } else if (ret != 1) { | |
835 | log_err("fio: short read on reading log\n"); | |
836 | fclose(f); | |
837 | return 1; | |
838 | } | |
839 | ||
840 | fclose(f); | |
841 | ||
842 | flush_chunk(&ic, 1, stdout, &stream, &iter); | |
843 | ||
844 | if (iter.seq) { | |
845 | finish_chunk(&stream, stdout, &iter); | |
846 | free(iter.buf); | |
847 | } | |
848 | ||
849 | free(ic.buf); | |
850 | return 0; | |
851 | } | |
852 | ||
aee2ab67 JA |
853 | #else |
854 | ||
855 | static void flush_gz_chunks(struct io_log *log, FILE *f) | |
856 | { | |
857 | } | |
858 | ||
859 | #endif | |
860 | ||
861 | void flush_log(struct io_log *log) | |
862 | { | |
863 | void *buf; | |
864 | FILE *f; | |
865 | ||
866 | f = fopen(log->filename, "w"); | |
867 | if (!f) { | |
868 | perror("fopen log"); | |
869 | return; | |
870 | } | |
871 | ||
872 | buf = set_file_buffer(f); | |
873 | ||
874 | flush_gz_chunks(log, f); | |
875 | ||
b26317c9 | 876 | flush_samples(f, log->log, log->nr_samples * log_entry_sz(log)); |
ac9b9101 JA |
877 | |
878 | fclose(f); | |
2e802282 | 879 | clear_file_buffer(buf); |
ac9b9101 JA |
880 | } |
881 | ||
cb7e0ace | 882 | static int finish_log(struct thread_data *td, struct io_log *log, int trylock) |
ac9b9101 | 883 | { |
aee2ab67 JA |
884 | if (td->tp_data) |
885 | iolog_flush(log, 1); | |
886 | ||
243bfe19 | 887 | if (trylock) { |
cb7e0ace | 888 | if (fio_trylock_file(log->filename)) |
243bfe19 JA |
889 | return 1; |
890 | } else | |
cb7e0ace | 891 | fio_lock_file(log->filename); |
243bfe19 | 892 | |
aee2ab67 | 893 | if (td->client_type == FIO_CLIENT_TYPE_GUI) |
cb7e0ace | 894 | fio_send_iolog(td, log, log->filename); |
aee2ab67 JA |
895 | else |
896 | flush_log(log); | |
243bfe19 | 897 | |
cb7e0ace | 898 | fio_unlock_file(log->filename); |
518dac09 | 899 | free_log(log); |
243bfe19 | 900 | return 0; |
ac9b9101 JA |
901 | } |
902 | ||
aee2ab67 JA |
903 | #ifdef CONFIG_ZLIB |
904 | ||
905 | struct iolog_flush_data { | |
906 | struct tp_work work; | |
907 | struct io_log *log; | |
908 | void *samples; | |
909 | uint64_t nr_samples; | |
910 | }; | |
911 | ||
912 | static int gz_work(struct tp_work *work) | |
913 | { | |
914 | struct iolog_flush_data *data; | |
915 | struct iolog_compress *c; | |
916 | struct flist_head list; | |
917 | unsigned int seq; | |
918 | z_stream stream; | |
919 | size_t total = 0; | |
d73ac887 | 920 | int ret; |
aee2ab67 JA |
921 | |
922 | INIT_FLIST_HEAD(&list); | |
923 | ||
924 | data = container_of(work, struct iolog_flush_data, work); | |
925 | ||
926 | stream.zalloc = Z_NULL; | |
927 | stream.zfree = Z_NULL; | |
928 | stream.opaque = Z_NULL; | |
929 | ||
b26317c9 | 930 | ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION); |
b26317c9 | 931 | if (ret != Z_OK) { |
aee2ab67 JA |
932 | log_err("fio: failed to init gz stream\n"); |
933 | return 0; | |
934 | } | |
935 | ||
936 | seq = ++data->log->chunk_seq; | |
b26317c9 | 937 | |
aee2ab67 JA |
938 | stream.next_in = (void *) data->samples; |
939 | stream.avail_in = data->nr_samples * log_entry_sz(data->log); | |
940 | ||
941 | do { | |
942 | c = get_new_chunk(seq); | |
943 | stream.avail_out = GZ_CHUNK; | |
944 | stream.next_out = c->buf; | |
945 | ret = deflate(&stream, Z_NO_FLUSH); | |
946 | if (ret < 0) { | |
947 | log_err("fio: deflate log (%d)\n", ret); | |
948 | break; | |
949 | } | |
950 | ||
951 | c->len = GZ_CHUNK - stream.avail_out; | |
952 | flist_add_tail(&c->list, &list); | |
953 | total += c->len; | |
954 | } while (stream.avail_in); | |
955 | ||
956 | stream.next_out = c->buf + c->len; | |
957 | stream.avail_out = GZ_CHUNK - c->len; | |
958 | ||
959 | ret = deflate(&stream, Z_FINISH); | |
960 | if (ret == Z_STREAM_END) | |
961 | c->len = GZ_CHUNK - stream.avail_out; | |
962 | else { | |
963 | do { | |
964 | c = get_new_chunk(seq); | |
965 | stream.avail_out = GZ_CHUNK; | |
966 | stream.next_out = c->buf; | |
967 | ret = deflate(&stream, Z_FINISH); | |
968 | c->len = GZ_CHUNK - stream.avail_out; | |
969 | flist_add_tail(&c->list, &list); | |
970 | } while (ret != Z_STREAM_END); | |
971 | } | |
972 | ||
973 | ret = deflateEnd(&stream); | |
974 | if (ret != Z_OK) | |
975 | log_err("fio: deflateEnd %d\n", ret); | |
976 | ||
977 | free(data->samples); | |
978 | ||
979 | if (!flist_empty(&list)) { | |
980 | pthread_mutex_lock(&data->log->chunk_lock); | |
981 | flist_splice_tail(&list, &data->log->chunk_list); | |
982 | pthread_mutex_unlock(&data->log->chunk_lock); | |
983 | } | |
984 | ||
985 | if (work->wait) { | |
986 | work->done = 1; | |
987 | pthread_cond_signal(&work->cv); | |
988 | } else | |
989 | free(data); | |
990 | ||
991 | return 0; | |
992 | } | |
993 | ||
994 | int iolog_flush(struct io_log *log, int wait) | |
995 | { | |
b26317c9 | 996 | struct tp_data *tdat = log->td->tp_data; |
aee2ab67 JA |
997 | struct iolog_flush_data *data; |
998 | size_t sample_size; | |
999 | ||
1000 | data = malloc(sizeof(*data)); | |
1001 | if (!data) | |
1002 | return 1; | |
1003 | ||
1004 | data->log = log; | |
1005 | ||
1006 | sample_size = log->nr_samples * log_entry_sz(log); | |
1007 | data->samples = malloc(sample_size); | |
1008 | if (!data->samples) { | |
1009 | free(data); | |
1010 | return 1; | |
1011 | } | |
1012 | ||
1013 | memcpy(data->samples, log->log, sample_size); | |
1014 | data->nr_samples = log->nr_samples; | |
1015 | data->work.fn = gz_work; | |
1016 | log->nr_samples = 0; | |
1017 | ||
1018 | if (wait) { | |
1019 | pthread_mutex_init(&data->work.lock, NULL); | |
1020 | pthread_cond_init(&data->work.cv, NULL); | |
1021 | data->work.wait = 1; | |
1022 | } else | |
1023 | data->work.wait = 0; | |
1024 | ||
b26317c9 | 1025 | tp_queue_work(tdat, &data->work); |
aee2ab67 JA |
1026 | |
1027 | if (wait) { | |
1028 | pthread_mutex_lock(&data->work.lock); | |
1029 | while (!data->work.done) | |
1030 | pthread_cond_wait(&data->work.cv, &data->work.lock); | |
1031 | pthread_mutex_unlock(&data->work.lock); | |
1032 | free(data); | |
1033 | } | |
1034 | ||
1035 | return 0; | |
1036 | } | |
1037 | ||
1038 | #else | |
1039 | ||
1040 | int iolog_flush(struct io_log *log, int wait) | |
1041 | { | |
1042 | return 1; | |
1043 | } | |
1044 | ||
1045 | #endif | |
1046 | ||
cb7e0ace | 1047 | static int write_iops_log(struct thread_data *td, int try) |
905e3d4f | 1048 | { |
cb7e0ace | 1049 | struct io_log *log = td->iops_log; |
905e3d4f JA |
1050 | |
1051 | if (!log) | |
1052 | return 0; | |
1053 | ||
cb7e0ace | 1054 | return finish_log(td, log, try); |
905e3d4f JA |
1055 | } |
1056 | ||
1057 | static int write_slat_log(struct thread_data *td, int try) | |
1058 | { | |
cb7e0ace | 1059 | struct io_log *log = td->slat_log; |
905e3d4f | 1060 | |
cb7e0ace JA |
1061 | if (!log) |
1062 | return 0; | |
1063 | ||
1064 | return finish_log(td, log, try); | |
905e3d4f JA |
1065 | } |
1066 | ||
1067 | static int write_clat_log(struct thread_data *td, int try) | |
1068 | { | |
cb7e0ace | 1069 | struct io_log *log = td->clat_log; |
905e3d4f | 1070 | |
cb7e0ace JA |
1071 | if (!log) |
1072 | return 0; | |
1073 | ||
1074 | return finish_log(td, log, try); | |
905e3d4f JA |
1075 | } |
1076 | ||
1077 | static int write_lat_log(struct thread_data *td, int try) | |
1078 | { | |
cb7e0ace JA |
1079 | struct io_log *log = td->lat_log; |
1080 | ||
1081 | if (!log) | |
1082 | return 0; | |
905e3d4f | 1083 | |
cb7e0ace | 1084 | return finish_log(td, log, try); |
905e3d4f JA |
1085 | } |
1086 | ||
1087 | static int write_bandw_log(struct thread_data *td, int try) | |
1088 | { | |
cb7e0ace JA |
1089 | struct io_log *log = td->bw_log; |
1090 | ||
1091 | if (!log) | |
1092 | return 0; | |
905e3d4f | 1093 | |
cb7e0ace | 1094 | return finish_log(td, log, try); |
905e3d4f JA |
1095 | } |
1096 | ||
1097 | enum { | |
1098 | BW_LOG_MASK = 1, | |
1099 | LAT_LOG_MASK = 2, | |
1100 | SLAT_LOG_MASK = 4, | |
1101 | CLAT_LOG_MASK = 8, | |
1102 | IOPS_LOG_MASK = 16, | |
1103 | ||
905e3d4f JA |
1104 | ALL_LOG_NR = 5, |
1105 | }; | |
1106 | ||
1107 | struct log_type { | |
1108 | unsigned int mask; | |
1109 | int (*fn)(struct thread_data *, int); | |
1110 | }; | |
1111 | ||
1112 | static struct log_type log_types[] = { | |
1113 | { | |
1114 | .mask = BW_LOG_MASK, | |
1115 | .fn = write_bandw_log, | |
1116 | }, | |
1117 | { | |
1118 | .mask = LAT_LOG_MASK, | |
1119 | .fn = write_lat_log, | |
1120 | }, | |
1121 | { | |
1122 | .mask = SLAT_LOG_MASK, | |
1123 | .fn = write_slat_log, | |
1124 | }, | |
1125 | { | |
1126 | .mask = CLAT_LOG_MASK, | |
1127 | .fn = write_clat_log, | |
1128 | }, | |
1129 | { | |
1130 | .mask = IOPS_LOG_MASK, | |
1131 | .fn = write_iops_log, | |
1132 | }, | |
1133 | }; | |
1134 | ||
1135 | void fio_writeout_logs(struct thread_data *td) | |
1136 | { | |
ea5409f9 | 1137 | unsigned int log_mask = 0; |
905e3d4f JA |
1138 | unsigned int log_left = ALL_LOG_NR; |
1139 | int old_state, i; | |
1140 | ||
1141 | old_state = td_bump_runstate(td, TD_FINISHING); | |
1142 | ||
1143 | finalize_logs(td); | |
1144 | ||
1145 | while (log_left) { | |
1146 | int prev_log_left = log_left; | |
1147 | ||
1148 | for (i = 0; i < ALL_LOG_NR && log_left; i++) { | |
1149 | struct log_type *lt = &log_types[i]; | |
1150 | int ret; | |
1151 | ||
ea5409f9 | 1152 | if (!(log_mask & lt->mask)) { |
905e3d4f JA |
1153 | ret = lt->fn(td, log_left != 1); |
1154 | if (!ret) { | |
1155 | log_left--; | |
ea5409f9 | 1156 | log_mask |= lt->mask; |
905e3d4f JA |
1157 | } |
1158 | } | |
1159 | } | |
1160 | ||
1161 | if (prev_log_left == log_left) | |
1162 | usleep(5000); | |
1163 | } | |
1164 | ||
1165 | td_restore_runstate(td, old_state); | |
1166 | } |