iolog: fix double free when verified I/O overlaps
[fio.git] / iolog.c
CommitLineData
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"
a47591e4 21#include "smalloc.h"
a3e59412 22#include "blktrace.h"
ac9b9101 23
7e419452
JA
24static int iolog_flush(struct io_log *log);
25
ac9b9101
JA
26static const char iolog_ver2[] = "fio version 2 iolog";
27
28void queue_io_piece(struct thread_data *td, struct io_piece *ipo)
29{
30 flist_add_tail(&ipo->list, &td->io_log_list);
31 td->total_io_size += ipo->len;
32}
33
1f440ece 34void log_io_u(const struct thread_data *td, const struct io_u *io_u)
ac9b9101 35{
ac9b9101
JA
36 if (!td->o.write_iolog_file)
37 return;
38
39 fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name,
173375c2
JA
40 io_ddir_name(io_u->ddir),
41 io_u->offset, io_u->buflen);
ac9b9101
JA
42}
43
44void log_file(struct thread_data *td, struct fio_file *f,
45 enum file_log_act what)
46{
47 const char *act[] = { "add", "open", "close" };
48
49 assert(what < 3);
50
51 if (!td->o.write_iolog_file)
52 return;
53
54
55 /*
56 * this happens on the pre-open/close done before the job starts
57 */
58 if (!td->iolog_f)
59 return;
60
61 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
62}
63
64static void iolog_delay(struct thread_data *td, unsigned long delay)
65{
df8fb609
JA
66 uint64_t usec = utime_since_now(&td->last_issue);
67 uint64_t this_delay;
8b6a404c 68 struct timespec ts;
ac9b9101 69
df8fb609
JA
70 if (delay < td->time_offset) {
71 td->time_offset = 0;
72 return;
73 }
74
75 delay -= td->time_offset;
ac9b9101
JA
76 if (delay < usec)
77 return;
78
79 delay -= usec;
80
8b6a404c 81 fio_gettime(&ts, NULL);
30b18672
JA
82 while (delay && !td->terminate) {
83 this_delay = delay;
84 if (this_delay > 500000)
85 this_delay = 500000;
86
87 usec_sleep(td, this_delay);
88 delay -= this_delay;
89 }
df8fb609 90
8b6a404c 91 usec = utime_since_now(&ts);
df8fb609
JA
92 if (usec > delay)
93 td->time_offset = usec - delay;
94 else
95 td->time_offset = 0;
ac9b9101
JA
96}
97
98static int ipo_special(struct thread_data *td, struct io_piece *ipo)
99{
100 struct fio_file *f;
101 int ret;
102
103 /*
104 * Not a special ipo
105 */
106 if (ipo->ddir != DDIR_INVAL)
107 return 0;
108
109 f = td->files[ipo->fileno];
110
111 switch (ipo->file_action) {
112 case FIO_LOG_OPEN_FILE:
96d663ad
SW
113 if (td->o.replay_redirect && fio_file_open(f)) {
114 dprint(FD_FILE, "iolog: ignoring re-open of file %s\n",
115 f->file_name);
116 break;
117 }
ac9b9101
JA
118 ret = td_io_open_file(td, f);
119 if (!ret)
120 break;
121 td_verror(td, ret, "iolog open file");
122 return -1;
123 case FIO_LOG_CLOSE_FILE:
124 td_io_close_file(td, f);
125 break;
126 case FIO_LOG_UNLINK_FILE:
38ef9c90 127 td_io_unlink_file(td, f);
ac9b9101
JA
128 break;
129 default:
130 log_err("fio: bad file action %d\n", ipo->file_action);
131 break;
132 }
133
134 return 1;
135}
136
137int read_iolog_get(struct thread_data *td, struct io_u *io_u)
138{
139 struct io_piece *ipo;
140 unsigned long elapsed;
3c3ed070 141
ac9b9101
JA
142 while (!flist_empty(&td->io_log_list)) {
143 int ret;
144
9342d5f8 145 ipo = flist_first_entry(&td->io_log_list, struct io_piece, list);
ac9b9101
JA
146 flist_del(&ipo->list);
147 remove_trim_entry(td, ipo);
148
149 ret = ipo_special(td, ipo);
150 if (ret < 0) {
151 free(ipo);
152 break;
153 } else if (ret > 0) {
154 free(ipo);
155 continue;
156 }
157
158 io_u->ddir = ipo->ddir;
159 if (ipo->ddir != DDIR_WAIT) {
160 io_u->offset = ipo->offset;
161 io_u->buflen = ipo->len;
162 io_u->file = td->files[ipo->fileno];
163 get_file(io_u->file);
164 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
165 io_u->buflen, io_u->file->file_name);
166 if (ipo->delay)
167 iolog_delay(td, ipo->delay);
168 } else {
169 elapsed = mtime_since_genesis();
170 if (ipo->delay > elapsed)
171 usec_sleep(td, (ipo->delay - elapsed) * 1000);
ac9b9101
JA
172 }
173
174 free(ipo);
3c3ed070 175
ac9b9101
JA
176 if (io_u->ddir != DDIR_WAIT)
177 return 0;
178 }
179
180 td->done = 1;
181 return 1;
182}
183
184void prune_io_piece_log(struct thread_data *td)
185{
186 struct io_piece *ipo;
187 struct rb_node *n;
188
189 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
190 ipo = rb_entry(n, struct io_piece, rb_node);
191 rb_erase(n, &td->io_hist_tree);
192 remove_trim_entry(td, ipo);
193 td->io_hist_len--;
194 free(ipo);
195 }
196
197 while (!flist_empty(&td->io_hist_list)) {
4e1020f8 198 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
ac9b9101
JA
199 flist_del(&ipo->list);
200 remove_trim_entry(td, ipo);
201 td->io_hist_len--;
202 free(ipo);
203 }
204}
205
206/*
207 * log a successful write, so we can unwind the log for verify
208 */
209void log_io_piece(struct thread_data *td, struct io_u *io_u)
210{
211 struct rb_node **p, *parent;
212 struct io_piece *ipo, *__ipo;
213
214 ipo = malloc(sizeof(struct io_piece));
215 init_ipo(ipo);
216 ipo->file = io_u->file;
217 ipo->offset = io_u->offset;
218 ipo->len = io_u->buflen;
da0a7bd2 219 ipo->numberio = io_u->numberio;
f9401285
JA
220 ipo->flags = IP_F_IN_FLIGHT;
221
222 io_u->ipo = ipo;
ac9b9101
JA
223
224 if (io_u_should_trim(td, io_u)) {
225 flist_add_tail(&ipo->trim_list, &td->trim_list);
226 td->trim_entries++;
227 }
228
229 /*
230 * We don't need to sort the entries, if:
231 *
232 * Sequential writes, or
233 * Random writes that lay out the file as it goes along
234 *
235 * For both these cases, just reading back data in the order we
236 * wrote it out is the fastest.
237 *
238 * One exception is if we don't have a random map AND we are doing
239 * verifies, in that case we need to check for duplicate blocks and
240 * drop the old one, which we rely on the rb insert/lookup for
241 * handling.
242 */
c4b6117b 243 if (((!td->o.verifysort) || !td_random(td) || !td->o.overwrite) &&
ac9b9101
JA
244 (file_randommap(td, ipo->file) || td->o.verify == VERIFY_NONE)) {
245 INIT_FLIST_HEAD(&ipo->list);
246 flist_add_tail(&ipo->list, &td->io_hist_list);
247 ipo->flags |= IP_F_ONLIST;
248 td->io_hist_len++;
249 return;
250 }
251
252 RB_CLEAR_NODE(&ipo->rb_node);
253
254 /*
255 * Sort the entry into the verification list
256 */
257restart:
258 p = &td->io_hist_tree.rb_node;
259 parent = NULL;
260 while (*p) {
3b1ff869 261 int overlap = 0;
ac9b9101
JA
262 parent = *p;
263
264 __ipo = rb_entry(parent, struct io_piece, rb_node);
265 if (ipo->file < __ipo->file)
266 p = &(*p)->rb_left;
267 else if (ipo->file > __ipo->file)
268 p = &(*p)->rb_right;
3b1ff869 269 else if (ipo->offset < __ipo->offset) {
ac9b9101 270 p = &(*p)->rb_left;
3b1ff869
JE
271 overlap = ipo->offset + ipo->len > __ipo->offset;
272 }
273 else if (ipo->offset > __ipo->offset) {
ac9b9101 274 p = &(*p)->rb_right;
3b1ff869
JE
275 overlap = __ipo->offset + __ipo->len > ipo->offset;
276 }
277 else
278 overlap = 1;
279
280 if (overlap) {
7abe6a27 281 dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu\n",
885ac623
JA
282 __ipo->offset, __ipo->len,
283 ipo->offset, ipo->len);
ac9b9101
JA
284 td->io_hist_len--;
285 rb_erase(parent, &td->io_hist_tree);
286 remove_trim_entry(td, __ipo);
e8b74655
SW
287 if (!(__ipo->flags & IP_F_IN_FLIGHT))
288 free(__ipo);
ac9b9101
JA
289 goto restart;
290 }
291 }
292
293 rb_link_node(&ipo->rb_node, parent, p);
294 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
295 ipo->flags |= IP_F_ONRB;
296 td->io_hist_len++;
297}
298
890b6656
JA
299void unlog_io_piece(struct thread_data *td, struct io_u *io_u)
300{
301 struct io_piece *ipo = io_u->ipo;
302
66347cfa
DE
303 if (td->ts.nr_block_infos) {
304 uint32_t *info = io_u_block_info(td, io_u);
305 if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) {
306 if (io_u->ddir == DDIR_TRIM)
307 *info = BLOCK_INFO_SET_STATE(*info,
308 BLOCK_STATE_TRIM_FAILURE);
309 else if (io_u->ddir == DDIR_WRITE)
310 *info = BLOCK_INFO_SET_STATE(*info,
311 BLOCK_STATE_WRITE_FAILURE);
312 }
313 }
314
890b6656
JA
315 if (!ipo)
316 return;
317
318 if (ipo->flags & IP_F_ONRB)
319 rb_erase(&ipo->rb_node, &td->io_hist_tree);
320 else if (ipo->flags & IP_F_ONLIST)
321 flist_del(&ipo->list);
322
323 free(ipo);
324 io_u->ipo = NULL;
325 td->io_hist_len--;
326}
327
1f440ece 328void trim_io_piece(struct thread_data *td, const struct io_u *io_u)
890b6656
JA
329{
330 struct io_piece *ipo = io_u->ipo;
331
332 if (!ipo)
333 return;
334
335 ipo->len = io_u->xfer_buflen - io_u->resid;
336}
337
ac9b9101
JA
338void write_iolog_close(struct thread_data *td)
339{
340 fflush(td->iolog_f);
341 fclose(td->iolog_f);
342 free(td->iolog_buf);
343 td->iolog_f = NULL;
344 td->iolog_buf = NULL;
345}
346
347/*
348 * Read version 2 iolog data. It is enhanced to include per-file logging,
349 * syncs, etc.
350 */
351static int read_iolog2(struct thread_data *td, FILE *f)
352{
353 unsigned long long offset;
354 unsigned int bytes;
355 int reads, writes, waits, fileno = 0, file_action = 0; /* stupid gcc */
dac34079 356 char *rfname, *fname, *act;
ac9b9101
JA
357 char *str, *p;
358 enum fio_ddir rw;
359
360 free_release_files(td);
361
362 /*
363 * Read in the read iolog and store it, reuse the infrastructure
364 * for doing verifications.
365 */
366 str = malloc(4096);
dac34079 367 rfname = fname = malloc(256+16);
ac9b9101
JA
368 act = malloc(256+16);
369
370 reads = writes = waits = 0;
371 while ((p = fgets(str, 4096, f)) != NULL) {
372 struct io_piece *ipo;
373 int r;
374
dac34079 375 r = sscanf(p, "%256s %256s %llu %u", rfname, act, &offset,
ac9b9101 376 &bytes);
dac34079
JA
377
378 if (td->o.replay_redirect)
379 fname = td->o.replay_redirect;
380
ac9b9101
JA
381 if (r == 4) {
382 /*
383 * Check action first
384 */
385 if (!strcmp(act, "wait"))
386 rw = DDIR_WAIT;
387 else if (!strcmp(act, "read"))
388 rw = DDIR_READ;
389 else if (!strcmp(act, "write"))
390 rw = DDIR_WRITE;
391 else if (!strcmp(act, "sync"))
392 rw = DDIR_SYNC;
393 else if (!strcmp(act, "datasync"))
394 rw = DDIR_DATASYNC;
395 else if (!strcmp(act, "trim"))
396 rw = DDIR_TRIM;
397 else {
398 log_err("fio: bad iolog file action: %s\n",
399 act);
400 continue;
401 }
033ace1e 402 fileno = get_fileno(td, fname);
ac9b9101
JA
403 } else if (r == 2) {
404 rw = DDIR_INVAL;
405 if (!strcmp(act, "add")) {
96d663ad
SW
406 if (td->o.replay_redirect &&
407 get_fileno(td, fname) != -1) {
408 dprint(FD_FILE, "iolog: ignoring"
409 " re-add of file %s\n", fname);
410 } else {
411 fileno = add_file(td, fname, 0, 1);
412 file_action = FIO_LOG_ADD_FILE;
413 }
ac9b9101
JA
414 continue;
415 } else if (!strcmp(act, "open")) {
416 fileno = get_fileno(td, fname);
417 file_action = FIO_LOG_OPEN_FILE;
418 } else if (!strcmp(act, "close")) {
419 fileno = get_fileno(td, fname);
420 file_action = FIO_LOG_CLOSE_FILE;
421 } else {
422 log_err("fio: bad iolog file action: %s\n",
423 act);
424 continue;
425 }
426 } else {
c5839011 427 log_err("bad iolog2: %s\n", p);
ac9b9101
JA
428 continue;
429 }
430
431 if (rw == DDIR_READ)
432 reads++;
433 else if (rw == DDIR_WRITE) {
434 /*
435 * Don't add a write for ro mode
436 */
437 if (read_only)
438 continue;
439 writes++;
440 } else if (rw == DDIR_WAIT) {
74dff443
JA
441 if (td->o.no_stall)
442 continue;
ac9b9101
JA
443 waits++;
444 } else if (rw == DDIR_INVAL) {
445 } else if (!ddir_sync(rw)) {
446 log_err("bad ddir: %d\n", rw);
447 continue;
448 }
449
450 /*
451 * Make note of file
452 */
453 ipo = malloc(sizeof(*ipo));
454 init_ipo(ipo);
455 ipo->ddir = rw;
456 if (rw == DDIR_WAIT) {
457 ipo->delay = offset;
458 } else {
a79f17bf
SW
459 if (td->o.replay_scale)
460 ipo->offset = offset / td->o.replay_scale;
461 else
462 ipo->offset = offset;
463 ipo_bytes_align(td->o.replay_align, ipo);
464
ac9b9101 465 ipo->len = bytes;
42793d94 466 if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw])
ac9b9101
JA
467 td->o.max_bs[rw] = bytes;
468 ipo->fileno = fileno;
469 ipo->file_action = file_action;
70afff59 470 td->o.size += bytes;
ac9b9101 471 }
3c3ed070 472
ac9b9101
JA
473 queue_io_piece(td, ipo);
474 }
475
476 free(str);
477 free(act);
dac34079 478 free(rfname);
ac9b9101
JA
479
480 if (writes && read_only) {
481 log_err("fio: <%s> skips replay of %d writes due to"
482 " read-only\n", td->o.name, writes);
483 writes = 0;
484 }
485
486 if (!reads && !writes && !waits)
487 return 1;
488 else if (reads && !writes)
489 td->o.td_ddir = TD_DDIR_READ;
490 else if (!reads && writes)
491 td->o.td_ddir = TD_DDIR_WRITE;
492 else
493 td->o.td_ddir = TD_DDIR_RW;
494
495 return 0;
496}
497
498/*
499 * open iolog, check version, and call appropriate parser
500 */
501static int init_iolog_read(struct thread_data *td)
502{
503 char buffer[256], *p;
504 FILE *f;
505 int ret;
506
507 f = fopen(td->o.read_iolog_file, "r");
508 if (!f) {
509 perror("fopen read iolog");
510 return 1;
511 }
512
513 p = fgets(buffer, sizeof(buffer), f);
514 if (!p) {
515 td_verror(td, errno, "iolog read");
516 log_err("fio: unable to read iolog\n");
517 fclose(f);
518 return 1;
519 }
520
521 /*
522 * version 2 of the iolog stores a specific string as the
523 * first line, check for that
524 */
525 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
526 ret = read_iolog2(td, f);
527 else {
528 log_err("fio: iolog version 1 is no longer supported\n");
529 ret = 1;
530 }
531
532 fclose(f);
533 return ret;
534}
535
536/*
537 * Set up a log for storing io patterns.
538 */
539static int init_iolog_write(struct thread_data *td)
540{
541 struct fio_file *ff;
542 FILE *f;
543 unsigned int i;
544
545 f = fopen(td->o.write_iolog_file, "a");
546 if (!f) {
547 perror("fopen write iolog");
548 return 1;
549 }
550
551 /*
552 * That's it for writing, setup a log buffer and we're done.
553 */
554 td->iolog_f = f;
555 td->iolog_buf = malloc(8192);
556 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
557
558 /*
559 * write our version line
560 */
561 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
562 perror("iolog init\n");
563 return 1;
564 }
565
566 /*
567 * add all known files
568 */
569 for_each_file(td, ff, i)
570 log_file(td, ff, FIO_LOG_ADD_FILE);
571
572 return 0;
573}
574
575int init_iolog(struct thread_data *td)
576{
577 int ret = 0;
578
579 if (td->o.read_iolog_file) {
d95b34a6
JA
580 int need_swap;
581
ac9b9101
JA
582 /*
583 * Check if it's a blktrace file and load that if possible.
584 * Otherwise assume it's a normal log file and load that.
585 */
d95b34a6
JA
586 if (is_blktrace(td->o.read_iolog_file, &need_swap))
587 ret = load_blktrace(td, td->o.read_iolog_file, need_swap);
ac9b9101
JA
588 else
589 ret = init_iolog_read(td);
590 } else if (td->o.write_iolog_file)
591 ret = init_iolog_write(td);
592
f01b34ae
JA
593 if (ret)
594 td_verror(td, EINVAL, "failed initializing iolog");
595
ac9b9101
JA
596 return ret;
597}
598
aee2ab67
JA
599void setup_log(struct io_log **log, struct log_params *p,
600 const char *filename)
ac9b9101 601{
e75cc8f3 602 struct io_log *l;
65a4d15c
KC
603 int i;
604 struct io_u_plat_entry *entry;
605 struct flist_head *list;
ac9b9101 606
7e419452
JA
607 l = scalloc(1, sizeof(*l));
608 INIT_FLIST_HEAD(&l->io_logs);
aee2ab67
JA
609 l->log_type = p->log_type;
610 l->log_offset = p->log_offset;
611 l->log_gz = p->log_gz;
b26317c9 612 l->log_gz_store = p->log_gz_store;
aee2ab67 613 l->avg_msec = p->avg_msec;
1e613c9c
KC
614 l->hist_msec = p->hist_msec;
615 l->hist_coarseness = p->hist_coarseness;
cb7e0ace 616 l->filename = strdup(filename);
aee2ab67
JA
617 l->td = p->td;
618
65a4d15c
KC
619 /* Initialize histogram lists for each r/w direction,
620 * with initial io_u_plat of all zeros:
621 */
622 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
d730bc58 623 list = &l->hist_window[i].list;
65a4d15c
KC
624 INIT_FLIST_HEAD(list);
625 entry = calloc(1, sizeof(struct io_u_plat_entry));
626 flist_add(&entry->list, list);
627 }
628
1fed2080
JA
629 if (l->td && l->td->o.io_submit_mode != IO_MODE_OFFLOAD) {
630 struct io_logs *p;
631
632 p = calloc(1, sizeof(*l->pending));
a9afa45a 633 p->max_samples = DEF_LOG_ENTRIES;
1fed2080
JA
634 p->log = calloc(p->max_samples, log_entry_sz(l));
635 l->pending = p;
636 }
637
b26317c9 638 if (l->log_offset)
49e98daa 639 l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT;
b26317c9 640
aee2ab67
JA
641 INIT_FLIST_HEAD(&l->chunk_list);
642
643 if (l->log_gz && !p->td)
644 l->log_gz = 0;
19417eda 645 else if (l->log_gz || l->log_gz_store) {
34febb23 646 mutex_init_pshared(&l->chunk_lock);
858bce70 647 mutex_init_pshared(&l->deferred_free_lock);
aee2ab67
JA
648 p->td->flags |= TD_F_COMPRESS_LOG;
649 }
650
ac9b9101
JA
651 *log = l;
652}
653
2e802282
JA
654#ifdef CONFIG_SETVBUF
655static void *set_file_buffer(FILE *f)
656{
657 size_t size = 1048576;
658 void *buf;
659
660 buf = malloc(size);
661 setvbuf(f, buf, _IOFBF, size);
662 return buf;
663}
664
665static void clear_file_buffer(void *buf)
666{
667 free(buf);
668}
669#else
670static void *set_file_buffer(FILE *f)
671{
672 return NULL;
673}
674
675static void clear_file_buffer(void *buf)
676{
677}
678#endif
679
518dac09 680void free_log(struct io_log *log)
cb7e0ace 681{
7e419452
JA
682 while (!flist_empty(&log->io_logs)) {
683 struct io_logs *cur_log;
684
685 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
686 flist_del_init(&cur_log->list);
687 free(cur_log->log);
2b7625e2 688 sfree(cur_log);
7e419452
JA
689 }
690
1fed2080
JA
691 if (log->pending) {
692 free(log->pending->log);
693 free(log->pending);
694 log->pending = NULL;
695 }
696
697 free(log->pending);
cb7e0ace 698 free(log->filename);
a47591e4 699 sfree(log);
cb7e0ace
JA
700}
701
0225849f 702unsigned long hist_sum(int j, int stride, unsigned int *io_u_plat,
65a4d15c 703 unsigned int *io_u_plat_last)
548df930 704{
f2c972cc
JA
705 unsigned long sum;
706 int k;
548df930 707
868f6f03
KC
708 if (io_u_plat_last) {
709 for (k = sum = 0; k < stride; k++)
710 sum += io_u_plat[j + k] - io_u_plat_last[j + k];
711 } else {
712 for (k = sum = 0; k < stride; k++)
713 sum += io_u_plat[j + k];
714 }
548df930 715
1e613c9c
KC
716 return sum;
717}
718
a89ba4b1
JA
719static void flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
720 uint64_t sample_size)
1e613c9c
KC
721{
722 struct io_sample *s;
723 int log_offset;
724 uint64_t i, j, nr_samples;
65a4d15c 725 struct io_u_plat_entry *entry, *entry_before;
1e613c9c 726 unsigned int *io_u_plat;
65a4d15c 727 unsigned int *io_u_plat_before;
1e613c9c
KC
728
729 int stride = 1 << hist_coarseness;
730
731 if (!sample_size)
732 return;
733
734 s = __get_sample(samples, 0, 0);
735 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
736
737 nr_samples = sample_size / __log_entry_sz(log_offset);
738
739 for (i = 0; i < nr_samples; i++) {
740 s = __get_sample(samples, log_offset, i);
d730bc58 741
af2fde19 742 entry = s->data.plat_entry;
65a4d15c 743 io_u_plat = entry->io_u_plat;
d730bc58 744
65a4d15c
KC
745 entry_before = flist_first_entry(&entry->list, struct io_u_plat_entry, list);
746 io_u_plat_before = entry_before->io_u_plat;
d730bc58
JA
747
748 fprintf(f, "%lu, %u, %u, ", (unsigned long) s->time,
749 io_sample_ddir(s), s->bs);
1e613c9c 750 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
d730bc58
JA
751 fprintf(f, "%lu, ", hist_sum(j, stride, io_u_plat,
752 io_u_plat_before));
1e613c9c 753 }
d730bc58 754 fprintf(f, "%lu\n", (unsigned long)
65a4d15c 755 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat,
d730bc58
JA
756 io_u_plat_before));
757
65a4d15c
KC
758 flist_del(&entry_before->list);
759 free(entry_before);
1e613c9c
KC
760 }
761}
762
bead01d7 763void flush_samples(FILE *f, void *samples, uint64_t sample_size)
ac9b9101 764{
b26317c9
JA
765 struct io_sample *s;
766 int log_offset;
767 uint64_t i, nr_samples;
768
769 if (!sample_size)
770 return;
771
772 s = __get_sample(samples, 0, 0);
49e98daa 773 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
b26317c9
JA
774
775 nr_samples = sample_size / __log_entry_sz(log_offset);
ac9b9101 776
aee2ab67 777 for (i = 0; i < nr_samples; i++) {
b26317c9 778 s = __get_sample(samples, log_offset, i);
ac9b9101 779
aee2ab67 780 if (!log_offset) {
af2fde19 781 fprintf(f, "%lu, %" PRId64 ", %u, %u\n",
ae588852 782 (unsigned long) s->time,
af2fde19 783 s->data.val,
b26317c9 784 io_sample_ddir(s), s->bs);
ae588852
JA
785 } else {
786 struct io_sample_offset *so = (void *) s;
787
af2fde19 788 fprintf(f, "%lu, %" PRId64 ", %u, %u, %llu\n",
ae588852 789 (unsigned long) s->time,
af2fde19 790 s->data.val,
b26317c9 791 io_sample_ddir(s), s->bs,
ae588852
JA
792 (unsigned long long) so->offset);
793 }
ac9b9101 794 }
aee2ab67
JA
795}
796
797#ifdef CONFIG_ZLIB
97eabb2a
JA
798
799struct iolog_flush_data {
155f2f02 800 struct workqueue_work work;
97eabb2a
JA
801 struct io_log *log;
802 void *samples;
7e419452
JA
803 uint32_t nr_samples;
804 bool free;
97eabb2a
JA
805};
806
97eabb2a
JA
807#define GZ_CHUNK 131072
808
809static struct iolog_compress *get_new_chunk(unsigned int seq)
810{
811 struct iolog_compress *c;
812
813 c = malloc(sizeof(*c));
814 INIT_FLIST_HEAD(&c->list);
815 c->buf = malloc(GZ_CHUNK);
816 c->len = 0;
817 c->seq = seq;
97eabb2a
JA
818 return c;
819}
820
821static void free_chunk(struct iolog_compress *ic)
822{
c07826cd
JA
823 free(ic->buf);
824 free(ic);
97eabb2a
JA
825}
826
b26317c9 827static int z_stream_init(z_stream *stream, int gz_hdr)
aee2ab67 828{
b26317c9
JA
829 int wbits = 15;
830
acb2a69b 831 memset(stream, 0, sizeof(*stream));
aee2ab67
JA
832 stream->zalloc = Z_NULL;
833 stream->zfree = Z_NULL;
834 stream->opaque = Z_NULL;
835 stream->next_in = Z_NULL;
836
1a8e7458
JA
837 /*
838 * zlib magic - add 32 for auto-detection of gz header or not,
839 * if we decide to store files in a gzip friendly format.
840 */
b26317c9
JA
841 if (gz_hdr)
842 wbits += 32;
843
844 if (inflateInit2(stream, wbits) != Z_OK)
aee2ab67
JA
845 return 1;
846
847 return 0;
848}
849
1a8e7458 850struct inflate_chunk_iter {
aee2ab67 851 unsigned int seq;
dad95da1 852 int err;
aee2ab67
JA
853 void *buf;
854 size_t buf_size;
855 size_t buf_used;
856 size_t chunk_sz;
857};
858
b26317c9 859static void finish_chunk(z_stream *stream, FILE *f,
1a8e7458 860 struct inflate_chunk_iter *iter)
aee2ab67 861{
aee2ab67
JA
862 int ret;
863
864 ret = inflateEnd(stream);
865 if (ret != Z_OK)
50f940f1
JA
866 log_err("fio: failed to end log inflation seq %d (%d)\n",
867 iter->seq, ret);
aee2ab67 868
b26317c9 869 flush_samples(f, iter->buf, iter->buf_used);
aee2ab67
JA
870 free(iter->buf);
871 iter->buf = NULL;
872 iter->buf_size = iter->buf_used = 0;
873}
874
1a8e7458
JA
875/*
876 * Iterative chunk inflation. Handles cases where we cross into a new
877 * sequence, doing flush finish of previous chunk if needed.
878 */
879static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f,
880 z_stream *stream, struct inflate_chunk_iter *iter)
aee2ab67 881{
0c56718d
JA
882 size_t ret;
883
0f989d2e 884 dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u\n",
0c56718d
JA
885 (unsigned long) ic->len, ic->seq);
886
aee2ab67
JA
887 if (ic->seq != iter->seq) {
888 if (iter->seq)
b26317c9 889 finish_chunk(stream, f, iter);
aee2ab67 890
b26317c9 891 z_stream_init(stream, gz_hdr);
aee2ab67
JA
892 iter->seq = ic->seq;
893 }
894
895 stream->avail_in = ic->len;
896 stream->next_in = ic->buf;
897
898 if (!iter->buf_size) {
899 iter->buf_size = iter->chunk_sz;
900 iter->buf = malloc(iter->buf_size);
901 }
902
903 while (stream->avail_in) {
b26317c9 904 size_t this_out = iter->buf_size - iter->buf_used;
aee2ab67
JA
905 int err;
906
b26317c9 907 stream->avail_out = this_out;
aee2ab67
JA
908 stream->next_out = iter->buf + iter->buf_used;
909
910 err = inflate(stream, Z_NO_FLUSH);
911 if (err < 0) {
912 log_err("fio: failed inflating log: %d\n", err);
dad95da1 913 iter->err = err;
aee2ab67
JA
914 break;
915 }
916
b26317c9
JA
917 iter->buf_used += this_out - stream->avail_out;
918
919 if (!stream->avail_out) {
920 iter->buf_size += iter->chunk_sz;
921 iter->buf = realloc(iter->buf, iter->buf_size);
922 continue;
923 }
924
925 if (err == Z_STREAM_END)
926 break;
aee2ab67
JA
927 }
928
0c56718d
JA
929 ret = (void *) stream->next_in - ic->buf;
930
d0d0cf0a 931 dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) iter->buf_size);
0c56718d
JA
932
933 return ret;
aee2ab67
JA
934}
935
1a8e7458
JA
936/*
937 * Inflate stored compressed chunks, or write them directly to the log
938 * file if so instructed.
939 */
dad95da1 940static int inflate_gz_chunks(struct io_log *log, FILE *f)
aee2ab67 941{
1a8e7458 942 struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, };
aee2ab67
JA
943 z_stream stream;
944
945 while (!flist_empty(&log->chunk_list)) {
946 struct iolog_compress *ic;
947
9342d5f8 948 ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list);
aee2ab67 949 flist_del(&ic->list);
b26317c9 950
1a8e7458
JA
951 if (log->log_gz_store) {
952 size_t ret;
953
0c56718d
JA
954 dprint(FD_COMPRESS, "log write chunk size=%lu, "
955 "seq=%u\n", (unsigned long) ic->len, ic->seq);
956
1a8e7458 957 ret = fwrite(ic->buf, ic->len, 1, f);
dad95da1
JA
958 if (ret != 1 || ferror(f)) {
959 iter.err = errno;
1a8e7458 960 log_err("fio: error writing compressed log\n");
dad95da1 961 }
1a8e7458
JA
962 } else
963 inflate_chunk(ic, log->log_gz_store, f, &stream, &iter);
c07826cd
JA
964
965 free_chunk(ic);
aee2ab67
JA
966 }
967
968 if (iter.seq) {
b26317c9 969 finish_chunk(&stream, f, &iter);
aee2ab67
JA
970 free(iter.buf);
971 }
dad95da1
JA
972
973 return iter.err;
aee2ab67
JA
974}
975
1a8e7458
JA
976/*
977 * Open compressed log file and decompress the stored chunks and
978 * write them to stdout. The chunks are stored sequentially in the
979 * file, so we iterate over them and do them one-by-one.
980 */
b26317c9
JA
981int iolog_file_inflate(const char *file)
982{
1a8e7458 983 struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, };
b26317c9
JA
984 struct iolog_compress ic;
985 z_stream stream;
986 struct stat sb;
fd771971 987 ssize_t ret;
f302710c
JA
988 size_t total;
989 void *buf;
b26317c9
JA
990 FILE *f;
991
b26317c9
JA
992 f = fopen(file, "r");
993 if (!f) {
994 perror("fopen");
995 return 1;
996 }
997
fd771971
JA
998 if (stat(file, &sb) < 0) {
999 fclose(f);
1000 perror("stat");
1001 return 1;
1002 }
1003
f302710c 1004 ic.buf = buf = malloc(sb.st_size);
b26317c9 1005 ic.len = sb.st_size;
b26317c9
JA
1006 ic.seq = 1;
1007
1008 ret = fread(ic.buf, ic.len, 1, f);
1009 if (ret < 0) {
1010 perror("fread");
1011 fclose(f);
bb1116fe 1012 free(buf);
b26317c9
JA
1013 return 1;
1014 } else if (ret != 1) {
1015 log_err("fio: short read on reading log\n");
1016 fclose(f);
bb1116fe 1017 free(buf);
b26317c9
JA
1018 return 1;
1019 }
1020
1021 fclose(f);
1022
1a8e7458
JA
1023 /*
1024 * Each chunk will return Z_STREAM_END. We don't know how many
1025 * chunks are in the file, so we just keep looping and incrementing
1026 * the sequence number until we have consumed the whole compressed
1027 * file.
1028 */
f302710c
JA
1029 total = ic.len;
1030 do {
8a1db9a1 1031 size_t iret;
f302710c 1032
8a1db9a1
JA
1033 iret = inflate_chunk(&ic, 1, stdout, &stream, &iter);
1034 total -= iret;
f302710c
JA
1035 if (!total)
1036 break;
dad95da1
JA
1037 if (iter.err)
1038 break;
f302710c
JA
1039
1040 ic.seq++;
8a1db9a1
JA
1041 ic.len -= iret;
1042 ic.buf += iret;
f302710c 1043 } while (1);
b26317c9
JA
1044
1045 if (iter.seq) {
1046 finish_chunk(&stream, stdout, &iter);
1047 free(iter.buf);
1048 }
1049
f302710c 1050 free(buf);
dad95da1 1051 return iter.err;
b26317c9
JA
1052}
1053
aee2ab67
JA
1054#else
1055
dad95da1 1056static int inflate_gz_chunks(struct io_log *log, FILE *f)
aee2ab67 1057{
b7364e21 1058 return 0;
aee2ab67
JA
1059}
1060
a68c66b2
JA
1061int iolog_file_inflate(const char *file)
1062{
1063 log_err("fio: log inflation not possible without zlib\n");
1064 return 1;
1065}
1066
aee2ab67
JA
1067#endif
1068
60a25727 1069void flush_log(struct io_log *log, bool do_append)
aee2ab67
JA
1070{
1071 void *buf;
1072 FILE *f;
1073
3a5db920
JA
1074 if (!do_append)
1075 f = fopen(log->filename, "w");
1076 else
1077 f = fopen(log->filename, "a");
aee2ab67
JA
1078 if (!f) {
1079 perror("fopen log");
1080 return;
1081 }
1082
1083 buf = set_file_buffer(f);
1084
1a8e7458 1085 inflate_gz_chunks(log, f);
aee2ab67 1086
7e419452
JA
1087 while (!flist_empty(&log->io_logs)) {
1088 struct io_logs *cur_log;
1089
1090 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1091 flist_del_init(&cur_log->list);
1e613c9c 1092
a9179eeb 1093 if (log->td && log == log->td->clat_hist_log)
1e613c9c 1094 flush_hist_samples(f, log->hist_coarseness, cur_log->log,
868f6f03 1095 log_sample_sz(log, cur_log));
1e613c9c 1096 else
868f6f03 1097 flush_samples(f, cur_log->log, log_sample_sz(log, cur_log));
1e613c9c 1098
90d2d53f 1099 sfree(cur_log);
7e419452 1100 }
ac9b9101
JA
1101
1102 fclose(f);
2e802282 1103 clear_file_buffer(buf);
ac9b9101
JA
1104}
1105
cb7e0ace 1106static int finish_log(struct thread_data *td, struct io_log *log, int trylock)
ac9b9101 1107{
155f2f02 1108 if (td->flags & TD_F_COMPRESS_LOG)
7e419452 1109 iolog_flush(log);
aee2ab67 1110
243bfe19 1111 if (trylock) {
cb7e0ace 1112 if (fio_trylock_file(log->filename))
243bfe19
JA
1113 return 1;
1114 } else
cb7e0ace 1115 fio_lock_file(log->filename);
243bfe19 1116
0cba0f91 1117 if (td->client_type == FIO_CLIENT_TYPE_GUI || is_backend)
cb7e0ace 1118 fio_send_iolog(td, log, log->filename);
aee2ab67 1119 else
3a5db920 1120 flush_log(log, !td->o.per_job_logs);
243bfe19 1121
cb7e0ace 1122 fio_unlock_file(log->filename);
518dac09 1123 free_log(log);
243bfe19 1124 return 0;
ac9b9101
JA
1125}
1126
0cba0f91
JA
1127size_t log_chunk_sizes(struct io_log *log)
1128{
1129 struct flist_head *entry;
1130 size_t ret;
1131
1132 if (flist_empty(&log->chunk_list))
1133 return 0;
1134
1135 ret = 0;
1136 pthread_mutex_lock(&log->chunk_lock);
1137 flist_for_each(entry, &log->chunk_list) {
1138 struct iolog_compress *c;
1139
1140 c = flist_entry(entry, struct iolog_compress, list);
1141 ret += c->len;
1142 }
1143 pthread_mutex_unlock(&log->chunk_lock);
1144 return ret;
1145}
1146
aee2ab67
JA
1147#ifdef CONFIG_ZLIB
1148
858bce70
JA
1149static bool warned_on_drop;
1150
1151static void iolog_put_deferred(struct io_log *log, void *ptr)
1152{
1153 if (!ptr)
1154 return;
1155
1156 pthread_mutex_lock(&log->deferred_free_lock);
1157 if (log->deferred < IOLOG_MAX_DEFER) {
1158 log->deferred_items[log->deferred] = ptr;
1159 log->deferred++;
1160 } else if (!warned_on_drop) {
1161 log_err("fio: had to drop log entry free\n");
1162 warned_on_drop = true;
1163 }
1164 pthread_mutex_unlock(&log->deferred_free_lock);
1165}
1166
1167static void iolog_free_deferred(struct io_log *log)
1168{
1169 int i;
1170
1171 if (!log->deferred)
1172 return;
1173
1174 pthread_mutex_lock(&log->deferred_free_lock);
1175
1176 for (i = 0; i < log->deferred; i++) {
1177 free(log->deferred_items[i]);
1178 log->deferred_items[i] = NULL;
1179 }
1180
1181 log->deferred = 0;
1182 pthread_mutex_unlock(&log->deferred_free_lock);
1183}
1184
78e93aa6 1185static int gz_work(struct iolog_flush_data *data)
c143980a 1186{
040238ec 1187 struct iolog_compress *c = NULL;
aee2ab67
JA
1188 struct flist_head list;
1189 unsigned int seq;
1190 z_stream stream;
1191 size_t total = 0;
d73ac887 1192 int ret;
aee2ab67
JA
1193
1194 INIT_FLIST_HEAD(&list);
1195
7e419452 1196 memset(&stream, 0, sizeof(stream));
aee2ab67
JA
1197 stream.zalloc = Z_NULL;
1198 stream.zfree = Z_NULL;
1199 stream.opaque = Z_NULL;
1200
b26317c9 1201 ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
b26317c9 1202 if (ret != Z_OK) {
aee2ab67 1203 log_err("fio: failed to init gz stream\n");
152ea035 1204 goto err;
aee2ab67
JA
1205 }
1206
1207 seq = ++data->log->chunk_seq;
b26317c9 1208
aee2ab67
JA
1209 stream.next_in = (void *) data->samples;
1210 stream.avail_in = data->nr_samples * log_entry_sz(data->log);
1211
d0d0cf0a
JA
1212 dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u, log=%s\n",
1213 (unsigned long) stream.avail_in, seq,
1214 data->log->filename);
aee2ab67 1215 do {
040238ec 1216 if (c)
0caccfa7
JA
1217 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq,
1218 (unsigned long) c->len);
aee2ab67
JA
1219 c = get_new_chunk(seq);
1220 stream.avail_out = GZ_CHUNK;
1221 stream.next_out = c->buf;
1222 ret = deflate(&stream, Z_NO_FLUSH);
1223 if (ret < 0) {
1224 log_err("fio: deflate log (%d)\n", ret);
dad95da1
JA
1225 free_chunk(c);
1226 goto err;
aee2ab67
JA
1227 }
1228
1229 c->len = GZ_CHUNK - stream.avail_out;
1230 flist_add_tail(&c->list, &list);
1231 total += c->len;
1232 } while (stream.avail_in);
1233
1234 stream.next_out = c->buf + c->len;
1235 stream.avail_out = GZ_CHUNK - c->len;
1236
1237 ret = deflate(&stream, Z_FINISH);
6fa3ad51
JA
1238 if (ret < 0) {
1239 /*
1240 * Z_BUF_ERROR is special, it just means we need more
1241 * output space. We'll handle that below. Treat any other
1242 * error as fatal.
1243 */
1244 if (ret != Z_BUF_ERROR) {
1245 log_err("fio: deflate log (%d)\n", ret);
1246 flist_del(&c->list);
1247 free_chunk(c);
1248 goto err;
1249 }
1250 }
1251
1252 total -= c->len;
1253 c->len = GZ_CHUNK - stream.avail_out;
1254 total += c->len;
0caccfa7 1255 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, (unsigned long) c->len);
040238ec 1256
6fa3ad51 1257 if (ret != Z_STREAM_END) {
aee2ab67
JA
1258 do {
1259 c = get_new_chunk(seq);
1260 stream.avail_out = GZ_CHUNK;
1261 stream.next_out = c->buf;
1262 ret = deflate(&stream, Z_FINISH);
1263 c->len = GZ_CHUNK - stream.avail_out;
0c56718d 1264 total += c->len;
aee2ab67 1265 flist_add_tail(&c->list, &list);
0caccfa7
JA
1266 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq,
1267 (unsigned long) c->len);
aee2ab67
JA
1268 } while (ret != Z_STREAM_END);
1269 }
1270
0c56718d
JA
1271 dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total);
1272
aee2ab67
JA
1273 ret = deflateEnd(&stream);
1274 if (ret != Z_OK)
1275 log_err("fio: deflateEnd %d\n", ret);
1276
858bce70 1277 iolog_put_deferred(data->log, data->samples);
aee2ab67
JA
1278
1279 if (!flist_empty(&list)) {
1280 pthread_mutex_lock(&data->log->chunk_lock);
1281 flist_splice_tail(&list, &data->log->chunk_list);
1282 pthread_mutex_unlock(&data->log->chunk_lock);
1283 }
1284
dad95da1
JA
1285 ret = 0;
1286done:
7e419452 1287 if (data->free)
dab41b1c 1288 sfree(data);
dad95da1
JA
1289 return ret;
1290err:
1291 while (!flist_empty(&list)) {
1292 c = flist_first_entry(list.next, struct iolog_compress, list);
1293 flist_del(&c->list);
1294 free_chunk(c);
1295 }
1296 ret = 1;
1297 goto done;
aee2ab67
JA
1298}
1299
78e93aa6
JA
1300/*
1301 * Invoked from our compress helper thread, when logging would have exceeded
1302 * the specified memory limitation. Compresses the previously stored
1303 * entries.
1304 */
1305static int gz_work_async(struct submit_worker *sw, struct workqueue_work *work)
1306{
1307 return gz_work(container_of(work, struct iolog_flush_data, work));
1308}
1309
c08f9fe2
JA
1310static int gz_init_worker(struct submit_worker *sw)
1311{
1312 struct thread_data *td = sw->wq->td;
1313
1314 if (!fio_option_is_set(&td->o, log_gz_cpumask))
1315 return 0;
1316
1317 if (fio_setaffinity(gettid(), td->o.log_gz_cpumask) == -1) {
1318 log_err("gz: failed to set CPU affinity\n");
1319 return 1;
1320 }
1321
1322 return 0;
1323}
1324
155f2f02 1325static struct workqueue_ops log_compress_wq_ops = {
78e93aa6 1326 .fn = gz_work_async,
c08f9fe2 1327 .init_worker_fn = gz_init_worker,
24660963 1328 .nice = 1,
155f2f02
JA
1329};
1330
24660963 1331int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out)
155f2f02
JA
1332{
1333 if (!(td->flags & TD_F_COMPRESS_LOG))
1334 return 0;
1335
24660963 1336 workqueue_init(td, &td->log_compress_wq, &log_compress_wq_ops, 1, sk_out);
155f2f02
JA
1337 return 0;
1338}
1339
1340void iolog_compress_exit(struct thread_data *td)
1341{
1342 if (!(td->flags & TD_F_COMPRESS_LOG))
1343 return;
1344
1345 workqueue_exit(&td->log_compress_wq);
1346}
1347
1a8e7458 1348/*
949ae6dc
JA
1349 * Queue work item to compress the existing log entries. We reset the
1350 * current log to a small size, and reference the existing log in the
1351 * data that we queue for compression. Once compression has been done,
7e419452
JA
1352 * this old log is freed. If called with finish == true, will not return
1353 * until the log compression has completed, and will flush all previous
1354 * logs too
1a8e7458 1355 */
7e419452 1356static int iolog_flush(struct io_log *log)
aee2ab67 1357{
aee2ab67 1358 struct iolog_flush_data *data;
aee2ab67
JA
1359
1360 data = malloc(sizeof(*data));
1361 if (!data)
1362 return 1;
1363
1364 data->log = log;
7e419452 1365 data->free = false;
aee2ab67 1366
7e419452
JA
1367 while (!flist_empty(&log->io_logs)) {
1368 struct io_logs *cur_log;
949ae6dc 1369
7e419452
JA
1370 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1371 flist_del_init(&cur_log->list);
1372
1373 data->samples = cur_log->log;
1374 data->nr_samples = cur_log->nr_samples;
1375
2b7625e2 1376 sfree(cur_log);
aee2ab67 1377
78e93aa6 1378 gz_work(data);
7e419452 1379 }
aee2ab67 1380
7e419452 1381 free(data);
aee2ab67
JA
1382 return 0;
1383}
1384
7e419452
JA
1385int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log)
1386{
1387 struct iolog_flush_data *data;
1388
dab41b1c 1389 data = smalloc(sizeof(*data));
7e419452
JA
1390 if (!data)
1391 return 1;
1392
1393 data->log = log;
1394
1395 data->samples = cur_log->log;
1396 data->nr_samples = cur_log->nr_samples;
1397 data->free = true;
1398
1399 cur_log->nr_samples = cur_log->max_samples = 0;
1400 cur_log->log = NULL;
1401
1402 workqueue_enqueue(&log->td->log_compress_wq, &data->work);
858bce70
JA
1403
1404 iolog_free_deferred(log);
1405
7e419452
JA
1406 return 0;
1407}
aee2ab67
JA
1408#else
1409
7e419452
JA
1410static int iolog_flush(struct io_log *log)
1411{
1412 return 1;
1413}
1414
1415int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log)
aee2ab67
JA
1416{
1417 return 1;
1418}
1419
24660963 1420int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out)
52618a2a
JA
1421{
1422 return 0;
1423}
1424
1425void iolog_compress_exit(struct thread_data *td)
1426{
1427}
1428
aee2ab67
JA
1429#endif
1430
7e419452
JA
1431struct io_logs *iolog_cur_log(struct io_log *log)
1432{
1433 if (flist_empty(&log->io_logs))
1434 return NULL;
1435
1436 return flist_last_entry(&log->io_logs, struct io_logs, list);
1437}
1438
1439uint64_t iolog_nr_samples(struct io_log *iolog)
1440{
1441 struct flist_head *entry;
1442 uint64_t ret = 0;
1443
1444 flist_for_each(entry, &iolog->io_logs) {
1445 struct io_logs *cur_log;
1446
1447 cur_log = flist_entry(entry, struct io_logs, list);
1448 ret += cur_log->nr_samples;
1449 }
1450
1451 return ret;
1452}
1453
1c725e70 1454static int __write_log(struct thread_data *td, struct io_log *log, int try)
905e3d4f 1455{
1c725e70
JA
1456 if (log)
1457 return finish_log(td, log, try);
905e3d4f 1458
1c725e70
JA
1459 return 0;
1460}
905e3d4f 1461
a47591e4 1462static int write_iops_log(struct thread_data *td, int try, bool unit_log)
1c725e70 1463{
a47591e4
JA
1464 int ret;
1465
1466 if (per_unit_log(td->iops_log) != unit_log)
1467 return 0;
1468
1469 ret = __write_log(td, td->iops_log, try);
1470 if (!ret)
1471 td->iops_log = NULL;
1472
1473 return ret;
905e3d4f
JA
1474}
1475
a47591e4 1476static int write_slat_log(struct thread_data *td, int try, bool unit_log)
905e3d4f 1477{
a47591e4
JA
1478 int ret;
1479
1480 if (!unit_log)
1481 return 0;
1482
1483 ret = __write_log(td, td->slat_log, try);
1484 if (!ret)
1485 td->slat_log = NULL;
1486
1487 return ret;
905e3d4f
JA
1488}
1489
a47591e4 1490static int write_clat_log(struct thread_data *td, int try, bool unit_log)
905e3d4f 1491{
a47591e4
JA
1492 int ret;
1493
1494 if (!unit_log)
1495 return 0;
1496
1497 ret = __write_log(td, td->clat_log, try);
1498 if (!ret)
1499 td->clat_log = NULL;
1500
1501 return ret;
905e3d4f
JA
1502}
1503
1e613c9c
KC
1504static int write_clat_hist_log(struct thread_data *td, int try, bool unit_log)
1505{
1506 int ret;
1507
1508 if (!unit_log)
1509 return 0;
1510
1511 ret = __write_log(td, td->clat_hist_log, try);
1512 if (!ret)
1513 td->clat_hist_log = NULL;
1514
1515 return ret;
1516}
1517
a47591e4 1518static int write_lat_log(struct thread_data *td, int try, bool unit_log)
905e3d4f 1519{
a47591e4
JA
1520 int ret;
1521
1522 if (!unit_log)
1523 return 0;
1524
1525 ret = __write_log(td, td->lat_log, try);
1526 if (!ret)
1527 td->lat_log = NULL;
1528
1529 return ret;
905e3d4f
JA
1530}
1531
a47591e4 1532static int write_bandw_log(struct thread_data *td, int try, bool unit_log)
905e3d4f 1533{
a47591e4
JA
1534 int ret;
1535
1536 if (per_unit_log(td->bw_log) != unit_log)
1537 return 0;
1538
1539 ret = __write_log(td, td->bw_log, try);
1540 if (!ret)
1541 td->bw_log = NULL;
1542
1543 return ret;
905e3d4f
JA
1544}
1545
1546enum {
1547 BW_LOG_MASK = 1,
1548 LAT_LOG_MASK = 2,
1549 SLAT_LOG_MASK = 4,
1550 CLAT_LOG_MASK = 8,
1551 IOPS_LOG_MASK = 16,
1e613c9c 1552 CLAT_HIST_LOG_MASK = 32,
905e3d4f 1553
1e613c9c 1554 ALL_LOG_NR = 6,
905e3d4f
JA
1555};
1556
1557struct log_type {
1558 unsigned int mask;
a47591e4 1559 int (*fn)(struct thread_data *, int, bool);
905e3d4f
JA
1560};
1561
1562static struct log_type log_types[] = {
1563 {
1564 .mask = BW_LOG_MASK,
1565 .fn = write_bandw_log,
1566 },
1567 {
1568 .mask = LAT_LOG_MASK,
1569 .fn = write_lat_log,
1570 },
1571 {
1572 .mask = SLAT_LOG_MASK,
1573 .fn = write_slat_log,
1574 },
1575 {
1576 .mask = CLAT_LOG_MASK,
1577 .fn = write_clat_log,
1578 },
1579 {
1580 .mask = IOPS_LOG_MASK,
1581 .fn = write_iops_log,
1582 },
1e613c9c
KC
1583 {
1584 .mask = CLAT_HIST_LOG_MASK,
1585 .fn = write_clat_hist_log,
1586 }
905e3d4f
JA
1587};
1588
a47591e4 1589void td_writeout_logs(struct thread_data *td, bool unit_logs)
905e3d4f 1590{
ea5409f9 1591 unsigned int log_mask = 0;
905e3d4f
JA
1592 unsigned int log_left = ALL_LOG_NR;
1593 int old_state, i;
1594
1595 old_state = td_bump_runstate(td, TD_FINISHING);
1596
a47591e4 1597 finalize_logs(td, unit_logs);
905e3d4f
JA
1598
1599 while (log_left) {
1600 int prev_log_left = log_left;
1601
1602 for (i = 0; i < ALL_LOG_NR && log_left; i++) {
1603 struct log_type *lt = &log_types[i];
1604 int ret;
1605
ea5409f9 1606 if (!(log_mask & lt->mask)) {
a47591e4 1607 ret = lt->fn(td, log_left != 1, unit_logs);
905e3d4f
JA
1608 if (!ret) {
1609 log_left--;
ea5409f9 1610 log_mask |= lt->mask;
905e3d4f
JA
1611 }
1612 }
1613 }
1614
1615 if (prev_log_left == log_left)
1616 usleep(5000);
1617 }
1618
1619 td_restore_runstate(td, old_state);
1620}
a47591e4
JA
1621
1622void fio_writeout_logs(bool unit_logs)
1623{
1624 struct thread_data *td;
1625 int i;
1626
1627 for_each_td(td, i)
1628 td_writeout_logs(td, unit_logs);
1629}