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