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