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