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