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