2 * Code related to writing an iolog of what a thread is doing, and to
3 * later read that back and replay
22 #include "lib/roundup.h"
24 #include <netinet/in.h>
25 #include <netinet/tcp.h>
26 #include <arpa/inet.h>
28 #include <sys/socket.h>
31 static int iolog_flush(struct io_log *log);
33 static const char iolog_ver2[] = "fio version 2 iolog";
34 static const char iolog_ver3[] = "fio version 3 iolog";
36 void queue_io_piece(struct thread_data *td, struct io_piece *ipo)
38 flist_add_tail(&ipo->list, &td->io_log_list);
39 td->total_io_size += ipo->len;
42 void log_io_u(const struct thread_data *td, const struct io_u *io_u)
46 if (!td->o.write_iolog_file)
49 fio_gettime(&now, NULL);
50 fprintf(td->iolog_f, "%llu %s %s %llu %llu\n",
51 (unsigned long long) utime_since_now(&td->io_log_start_time),
52 io_u->file->file_name, io_ddir_name(io_u->ddir), io_u->offset,
57 void log_file(struct thread_data *td, struct fio_file *f,
58 enum file_log_act what)
60 const char *act[] = { "add", "open", "close" };
65 if (!td->o.write_iolog_file)
70 * this happens on the pre-open/close done before the job starts
75 fio_gettime(&now, NULL);
76 fprintf(td->iolog_f, "%llu %s %s\n",
77 (unsigned long long) utime_since_now(&td->io_log_start_time),
78 f->file_name, act[what]);
81 static void iolog_delay(struct thread_data *td, unsigned long delay)
83 uint64_t usec = utime_since_now(&td->last_issue);
84 unsigned long orig_delay = delay;
88 if (delay < td->time_offset) {
93 delay -= td->time_offset;
99 fio_gettime(&ts, NULL);
101 while (delay && !td->terminate) {
102 ret = io_u_queued_complete(td, 0);
104 td_verror(td, -ret, "io_u_queued_complete");
105 if (td->flags & TD_F_REGROW_LOGS)
107 if (utime_since_now(&ts) > delay)
111 usec = utime_since_now(&ts);
112 if (usec > orig_delay)
113 td->time_offset = usec - orig_delay;
118 static int ipo_special(struct thread_data *td, struct io_piece *ipo)
126 if (ipo->ddir != DDIR_INVAL)
129 f = td->files[ipo->fileno];
132 iolog_delay(td, ipo->delay);
133 if (fio_fill_issue_time(td))
134 fio_gettime(&td->last_issue, NULL);
135 switch (ipo->file_action) {
136 case FIO_LOG_OPEN_FILE:
137 if (td->o.replay_redirect && fio_file_open(f)) {
138 dprint(FD_FILE, "iolog: ignoring re-open of file %s\n",
142 ret = td_io_open_file(td, f);
145 td_verror(td, ret, "iolog open file");
147 case FIO_LOG_CLOSE_FILE:
148 td_io_close_file(td, f);
150 case FIO_LOG_UNLINK_FILE:
151 td_io_unlink_file(td, f);
153 case FIO_LOG_ADD_FILE:
159 log_err("fio: bad file action %d\n", ipo->file_action);
166 static bool read_iolog(struct thread_data *td);
168 unsigned long long delay_since_ttime(const struct thread_data *td,
169 unsigned long long time)
173 const unsigned long long *last_ttime = &td->io_log_last_ttime;
175 if (!*last_ttime || td->o.no_stall || time < *last_ttime)
177 else if (td->o.replay_time_scale == 100)
178 return time - *last_ttime;
181 scale = (double) 100.0 / (double) td->o.replay_time_scale;
182 tmp = time - *last_ttime;
186 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
188 struct io_piece *ipo;
189 unsigned long elapsed;
191 while (!flist_empty(&td->io_log_list)) {
194 if (td->o.read_iolog_chunked) {
195 if (td->io_log_checkmark == td->io_log_current) {
196 if (td->io_log_blktrace) {
197 if (!read_blktrace(td))
204 td->io_log_current--;
206 ipo = flist_first_entry(&td->io_log_list, struct io_piece, list);
207 flist_del(&ipo->list);
208 remove_trim_entry(td, ipo);
210 ret = ipo_special(td, ipo);
214 } else if (ret > 0) {
219 io_u->ddir = ipo->ddir;
220 if (ipo->ddir != DDIR_WAIT) {
221 io_u->offset = ipo->offset;
222 io_u->verify_offset = ipo->offset;
223 io_u->buflen = ipo->len;
224 io_u->file = td->files[ipo->fileno];
225 get_file(io_u->file);
226 dprint(FD_IO, "iolog: get %llu/%llu/%s\n", io_u->offset,
227 io_u->buflen, io_u->file->file_name);
229 iolog_delay(td, ipo->delay);
231 elapsed = mtime_since_genesis();
232 if (ipo->delay > elapsed)
233 usec_sleep(td, (ipo->delay - elapsed) * 1000);
238 if (io_u->ddir != DDIR_WAIT)
246 void prune_io_piece_log(struct thread_data *td)
248 struct io_piece *ipo;
249 struct fio_rb_node *n;
251 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
252 ipo = rb_entry(n, struct io_piece, rb_node);
253 rb_erase(n, &td->io_hist_tree);
254 remove_trim_entry(td, ipo);
259 while (!flist_empty(&td->io_hist_list)) {
260 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
261 flist_del(&ipo->list);
262 remove_trim_entry(td, ipo);
269 * log a successful write, so we can unwind the log for verify
271 void log_io_piece(struct thread_data *td, struct io_u *io_u)
273 struct fio_rb_node **p, *parent;
274 struct io_piece *ipo, *__ipo;
276 ipo = calloc(1, sizeof(struct io_piece));
278 ipo->file = io_u->file;
279 ipo->offset = io_u->offset;
280 ipo->len = io_u->buflen;
281 ipo->numberio = io_u->numberio;
282 ipo->flags = IP_F_IN_FLIGHT;
286 if (io_u_should_trim(td, io_u)) {
287 flist_add_tail(&ipo->trim_list, &td->trim_list);
292 * Only sort writes if we don't have a random map in which case we need
293 * to check for duplicate blocks and drop the old one, which we rely on
294 * the rb insert/lookup for handling.
296 if (file_randommap(td, ipo->file)) {
297 INIT_FLIST_HEAD(&ipo->list);
298 flist_add_tail(&ipo->list, &td->io_hist_list);
299 ipo->flags |= IP_F_ONLIST;
304 RB_CLEAR_NODE(&ipo->rb_node);
307 * Sort the entry into the verification list
310 p = &td->io_hist_tree.rb_node;
316 __ipo = rb_entry(parent, struct io_piece, rb_node);
317 if (ipo->file < __ipo->file)
319 else if (ipo->file > __ipo->file)
321 else if (ipo->offset < __ipo->offset) {
323 overlap = ipo->offset + ipo->len > __ipo->offset;
325 else if (ipo->offset > __ipo->offset) {
327 overlap = __ipo->offset + __ipo->len > ipo->offset;
333 dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu\n",
334 __ipo->offset, __ipo->len,
335 ipo->offset, ipo->len);
337 rb_erase(parent, &td->io_hist_tree);
338 remove_trim_entry(td, __ipo);
339 if (!(__ipo->flags & IP_F_IN_FLIGHT))
345 rb_link_node(&ipo->rb_node, parent, p);
346 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
347 ipo->flags |= IP_F_ONRB;
351 void unlog_io_piece(struct thread_data *td, struct io_u *io_u)
353 struct io_piece *ipo = io_u->ipo;
355 if (td->ts.nr_block_infos) {
356 uint32_t *info = io_u_block_info(td, io_u);
357 if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) {
358 if (io_u->ddir == DDIR_TRIM)
359 *info = BLOCK_INFO_SET_STATE(*info,
360 BLOCK_STATE_TRIM_FAILURE);
361 else if (io_u->ddir == DDIR_WRITE)
362 *info = BLOCK_INFO_SET_STATE(*info,
363 BLOCK_STATE_WRITE_FAILURE);
370 if (ipo->flags & IP_F_ONRB)
371 rb_erase(&ipo->rb_node, &td->io_hist_tree);
372 else if (ipo->flags & IP_F_ONLIST)
373 flist_del(&ipo->list);
380 void trim_io_piece(const struct io_u *io_u)
382 struct io_piece *ipo = io_u->ipo;
387 ipo->len = io_u->xfer_buflen - io_u->resid;
390 void write_iolog_close(struct thread_data *td)
399 td->iolog_buf = NULL;
402 int64_t iolog_items_to_fetch(struct thread_data *td)
407 int64_t items_to_fetch;
409 if (!td->io_log_highmark)
413 fio_gettime(&now, NULL);
414 elapsed = ntime_since(&td->io_log_highmark_time, &now);
416 for_1s = (td->io_log_highmark - td->io_log_current) * 1000000000 / elapsed;
417 items_to_fetch = for_1s - td->io_log_current;
418 if (items_to_fetch < 0)
423 td->io_log_highmark = td->io_log_current + items_to_fetch;
424 td->io_log_checkmark = (td->io_log_highmark + 1) / 2;
425 fio_gettime(&td->io_log_highmark_time, NULL);
427 return items_to_fetch;
430 #define io_act(_td, _r) (((_td)->io_log_version == 3 && (r) == 5) || \
431 ((_td)->io_log_version == 2 && (r) == 4))
432 #define file_act(_td, _r) (((_td)->io_log_version == 3 && (r) == 3) || \
433 ((_td)->io_log_version == 2 && (r) == 2))
436 * Read version 2 and 3 iolog data. It is enhanced to include per-file logging,
439 static bool read_iolog(struct thread_data *td)
441 unsigned long long offset;
443 unsigned long long delay = 0;
444 int reads, writes, trims, waits, fileno = 0, file_action = 0; /* stupid gcc */
445 char *rfname, *fname, *act;
448 bool realloc = false;
449 int64_t items_to_fetch = 0;
452 if (td->o.read_iolog_chunked) {
453 items_to_fetch = iolog_items_to_fetch(td);
459 * Read in the read iolog and store it, reuse the infrastructure
460 * for doing verifications.
463 rfname = fname = malloc(256+16);
464 act = malloc(256+16);
466 syncs = reads = writes = trims = waits = 0;
467 while ((p = fgets(str, 4096, td->io_log_rfile)) != NULL) {
468 struct io_piece *ipo;
470 unsigned long long ttime;
472 if (td->io_log_version == 3) {
473 r = sscanf(p, "%llu %256s %256s %llu %u", &ttime, rfname, act,
475 delay = delay_since_ttime(td, ttime);
476 td->io_log_last_ttime = ttime;
478 * "wait" is not allowed with version 3
480 if (!strcmp(act, "wait")) {
481 log_err("iolog: ignoring wait command with"
482 " version 3 for file %s\n", fname);
485 } else /* version 2 */
486 r = sscanf(p, "%256s %256s %llu %u", rfname, act, &offset, &bytes);
488 if (td->o.replay_redirect)
489 fname = td->o.replay_redirect;
495 if (!strcmp(act, "wait"))
497 else if (!strcmp(act, "read")) {
498 if (td->o.replay_skip & (1u << DDIR_READ))
501 } else if (!strcmp(act, "write")) {
502 if (td->o.replay_skip & (1u << DDIR_WRITE))
505 } else if (!strcmp(act, "sync")) {
506 if (td->o.replay_skip & (1u << DDIR_SYNC))
509 } else if (!strcmp(act, "datasync"))
511 else if (!strcmp(act, "trim")) {
512 if (td->o.replay_skip & (1u << DDIR_TRIM))
516 log_err("fio: bad iolog file action: %s\n",
520 fileno = get_fileno(td, fname);
521 } else if (file_act(td, r)) {
523 if (!strcmp(act, "add")) {
524 if (td->o.replay_redirect &&
525 get_fileno(td, fname) != -1) {
526 dprint(FD_FILE, "iolog: ignoring"
527 " re-add of file %s\n", fname);
529 fileno = add_file(td, fname, td->subjob_number, 1);
530 file_action = FIO_LOG_ADD_FILE;
532 } else if (!strcmp(act, "open")) {
533 fileno = get_fileno(td, fname);
534 file_action = FIO_LOG_OPEN_FILE;
535 } else if (!strcmp(act, "close")) {
536 fileno = get_fileno(td, fname);
537 file_action = FIO_LOG_CLOSE_FILE;
539 log_err("fio: bad iolog file action: %s\n",
544 log_err("bad iolog%d: %s\n", td->io_log_version, p);
550 else if (rw == DDIR_WRITE) {
552 * Don't add a write for ro mode
557 } else if (rw == DDIR_TRIM) {
559 * Don't add a trim for ro mode
564 } else if (rw == DDIR_WAIT) {
568 } else if (rw == DDIR_INVAL) {
569 } else if (ddir_sync(rw)) {
572 log_err("bad ddir: %d\n", rw);
579 ipo = calloc(1, sizeof(*ipo));
582 if (td->io_log_version == 3)
584 if (rw == DDIR_WAIT) {
587 if (td->o.replay_scale)
588 ipo->offset = offset / td->o.replay_scale;
590 ipo->offset = offset;
591 ipo_bytes_align(td->o.replay_align, ipo);
594 if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw]) {
596 td->o.max_bs[rw] = bytes;
598 ipo->fileno = fileno;
599 ipo->file_action = file_action;
603 queue_io_piece(td, ipo);
605 if (td->o.read_iolog_chunked) {
606 td->io_log_current++;
608 if (items_to_fetch == 0)
617 if (td->o.read_iolog_chunked) {
618 td->io_log_highmark = td->io_log_current;
619 td->io_log_checkmark = (td->io_log_highmark + 1) / 2;
620 fio_gettime(&td->io_log_highmark_time, NULL);
623 if (writes && read_only) {
624 log_err("fio: <%s> skips replay of %d writes due to"
625 " read-only\n", td->o.name, writes);
629 td->flags |= TD_F_SYNCS;
631 if (td->o.read_iolog_chunked) {
632 if (td->io_log_current == 0) {
635 td->o.td_ddir = TD_DDIR_RW;
636 if (realloc && td->orig_buffer)
640 if (init_io_u_buffers(td))
646 if (!reads && !writes && !waits && !trims)
651 td->o.td_ddir |= TD_DDIR_READ;
653 td->o.td_ddir |= TD_DDIR_WRITE;
655 td->o.td_ddir |= TD_DDIR_TRIM;
660 static bool is_socket(const char *path)
665 r = stat(path, &buf);
669 return S_ISSOCK(buf.st_mode);
672 static int open_socket(const char *path)
674 struct sockaddr_un addr;
677 fd = socket(AF_UNIX, SOCK_STREAM, 0);
681 addr.sun_family = AF_UNIX;
682 if (snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path) >=
683 sizeof(addr.sun_path)) {
684 log_err("%s: path name %s is too long for a Unix socket\n",
688 ret = connect(fd, (const struct sockaddr *)&addr, strlen(path) + sizeof(addr.sun_family));
697 * open iolog, check version, and call appropriate parser
699 static bool init_iolog_read(struct thread_data *td, char *fname)
701 char buffer[256], *p;
704 dprint(FD_IO, "iolog: name=%s\n", fname);
706 if (is_socket(fname)) {
709 fd = open_socket(fname);
712 } else if (!strcmp(fname, "-")) {
715 f = fopen(fname, "r");
718 perror("fopen read iolog");
722 p = fgets(buffer, sizeof(buffer), f);
724 td_verror(td, errno, "iolog read");
725 log_err("fio: unable to read iolog\n");
731 * versions 2 and 3 of the iolog store a specific string as the
732 * first line, check for that
734 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
735 td->io_log_version = 2;
736 else if (!strncmp(iolog_ver3, buffer, strlen(iolog_ver3)))
737 td->io_log_version = 3;
739 log_err("fio: iolog version 1 is no longer supported\n");
744 free_release_files(td);
745 td->io_log_rfile = f;
746 return read_iolog(td);
750 * Set up a log for storing io patterns.
752 static bool init_iolog_write(struct thread_data *td)
758 f = fopen(td->o.write_iolog_file, "a");
760 perror("fopen write iolog");
765 * That's it for writing, setup a log buffer and we're done.
768 td->iolog_buf = malloc(8192);
769 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
770 fio_gettime(&td->io_log_start_time, NULL);
773 * write our version line
775 if (fprintf(f, "%s\n", iolog_ver3) < 0) {
776 perror("iolog init\n");
781 * add all known files
783 for_each_file(td, ff, i)
784 log_file(td, ff, FIO_LOG_ADD_FILE);
789 bool init_iolog(struct thread_data *td)
793 if (td->o.read_iolog_file) {
795 char * fname = get_name_by_idx(td->o.read_iolog_file, td->subjob_number);
798 * Check if it's a blktrace file and load that if possible.
799 * Otherwise assume it's a normal log file and load that.
801 if (is_blktrace(fname, &need_swap)) {
802 td->io_log_blktrace = 1;
803 ret = init_blktrace_read(td, fname, need_swap);
805 td->io_log_blktrace = 0;
806 ret = init_iolog_read(td, fname);
809 } else if (td->o.write_iolog_file)
810 ret = init_iolog_write(td);
815 td_verror(td, EINVAL, "failed initializing iolog");
820 void setup_log(struct io_log **log, struct log_params *p,
821 const char *filename)
825 struct io_u_plat_entry *entry;
826 struct flist_head *list;
828 l = scalloc(1, sizeof(*l));
829 INIT_FLIST_HEAD(&l->io_logs);
830 l->log_type = p->log_type;
831 l->log_offset = p->log_offset;
832 l->log_prio = p->log_prio;
833 l->log_gz = p->log_gz;
834 l->log_gz_store = p->log_gz_store;
835 l->avg_msec = p->avg_msec;
836 l->hist_msec = p->hist_msec;
837 l->hist_coarseness = p->hist_coarseness;
838 l->filename = strdup(filename);
841 /* Initialize histogram lists for each r/w direction,
842 * with initial io_u_plat of all zeros:
844 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
845 list = &l->hist_window[i].list;
846 INIT_FLIST_HEAD(list);
847 entry = calloc(1, sizeof(struct io_u_plat_entry));
848 flist_add(&entry->list, list);
851 if (l->td && l->td->o.io_submit_mode != IO_MODE_OFFLOAD) {
852 unsigned int def_samples = DEF_LOG_ENTRIES;
855 __p = calloc(1, sizeof(*l->pending));
856 if (l->td->o.iodepth > DEF_LOG_ENTRIES)
857 def_samples = roundup_pow2(l->td->o.iodepth);
858 __p->max_samples = def_samples;
859 __p->log = calloc(__p->max_samples, log_entry_sz(l));
864 l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT;
866 l->log_ddir_mask |= LOG_PRIO_SAMPLE_BIT;
868 * The bandwidth-log option generates agg-read_bw.log,
869 * agg-write_bw.log and agg-trim_bw.log for which l->td is NULL.
870 * Check if l->td is valid before dereferencing it.
872 if (l->td && l->td->o.log_max == IO_LOG_SAMPLE_BOTH)
873 l->log_ddir_mask |= LOG_AVG_MAX_SAMPLE_BIT;
875 INIT_FLIST_HEAD(&l->chunk_list);
877 if (l->log_gz && !p->td)
879 else if (l->log_gz || l->log_gz_store) {
880 mutex_init_pshared(&l->chunk_lock);
881 mutex_init_pshared(&l->deferred_free_lock);
882 p->td->flags |= TD_F_COMPRESS_LOG;
888 #ifdef CONFIG_SETVBUF
889 static void *set_file_buffer(FILE *f)
891 size_t size = 1048576;
895 setvbuf(f, buf, _IOFBF, size);
899 static void clear_file_buffer(void *buf)
904 static void *set_file_buffer(FILE *f)
909 static void clear_file_buffer(void *buf)
914 void free_log(struct io_log *log)
916 while (!flist_empty(&log->io_logs)) {
917 struct io_logs *cur_log;
919 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
920 flist_del_init(&cur_log->list);
926 free(log->pending->log);
936 uint64_t hist_sum(int j, int stride, uint64_t *io_u_plat,
937 uint64_t *io_u_plat_last)
942 if (io_u_plat_last) {
943 for (k = sum = 0; k < stride; k++)
944 sum += io_u_plat[j + k] - io_u_plat_last[j + k];
946 for (k = sum = 0; k < stride; k++)
947 sum += io_u_plat[j + k];
953 static void flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
954 uint64_t sample_size)
958 uint64_t i, j, nr_samples;
959 struct io_u_plat_entry *entry, *entry_before;
961 uint64_t *io_u_plat_before;
963 int stride = 1 << hist_coarseness;
968 s = __get_sample(samples, 0, 0);
969 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
971 nr_samples = sample_size / __log_entry_sz(log_offset);
973 for (i = 0; i < nr_samples; i++) {
974 s = __get_sample(samples, log_offset, i);
976 entry = s->data.plat_entry;
977 io_u_plat = entry->io_u_plat;
979 entry_before = flist_first_entry(&entry->list, struct io_u_plat_entry, list);
980 io_u_plat_before = entry_before->io_u_plat;
982 fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time,
983 io_sample_ddir(s), (unsigned long long) s->bs);
984 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
985 fprintf(f, "%llu, ", (unsigned long long)
986 hist_sum(j, stride, io_u_plat, io_u_plat_before));
988 fprintf(f, "%llu\n", (unsigned long long)
989 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat,
992 flist_del(&entry_before->list);
997 void flush_samples(FILE *f, void *samples, uint64_t sample_size)
1000 int log_offset, log_prio, log_avg_max;
1001 uint64_t i, nr_samples;
1002 unsigned int prio_val;
1008 s = __get_sample(samples, 0, 0);
1009 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
1010 log_prio = (s->__ddir & LOG_PRIO_SAMPLE_BIT) != 0;
1011 log_avg_max = (s->__ddir & LOG_AVG_MAX_SAMPLE_BIT) != 0;
1016 fmt = "%" PRIu64 ", %" PRId64 ", %" PRId64 ", %u, %llu, %llu, 0x%04x\n";
1018 fmt = "%" PRIu64 ", %" PRId64 ", %u, %llu, %llu, 0x%04x\n";
1021 fmt = "%" PRIu64 ", %" PRId64 ", %" PRId64 ", %u, %llu, %llu, %u\n";
1023 fmt = "%" PRIu64 ", %" PRId64 ", %u, %llu, %llu, %u\n";
1028 fmt = "%" PRIu64 ", %" PRId64 ", %" PRId64 ", %u, %llu, 0x%04x\n";
1030 fmt = "%" PRIu64 ", %" PRId64 ", %u, %llu, 0x%04x\n";
1033 fmt = "%" PRIu64 ", %" PRId64 ", %" PRId64 ", %u, %llu, %u\n";
1035 fmt = "%" PRIu64 ", %" PRId64 ", %u, %llu, %u\n";
1039 nr_samples = sample_size / __log_entry_sz(log_offset);
1041 for (i = 0; i < nr_samples; i++) {
1042 s = __get_sample(samples, log_offset, i);
1045 prio_val = s->priority;
1047 prio_val = ioprio_value_is_class_rt(s->priority);
1055 io_sample_ddir(s), (unsigned long long) s->bs,
1061 io_sample_ddir(s), (unsigned long long) s->bs,
1064 struct io_sample_offset *so = (void *) s;
1071 io_sample_ddir(s), (unsigned long long) s->bs,
1072 (unsigned long long) so->offset,
1078 io_sample_ddir(s), (unsigned long long) s->bs,
1079 (unsigned long long) so->offset,
1087 struct iolog_flush_data {
1088 struct workqueue_work work;
1091 uint32_t nr_samples;
1095 #define GZ_CHUNK 131072
1097 static struct iolog_compress *get_new_chunk(unsigned int seq)
1099 struct iolog_compress *c;
1101 c = malloc(sizeof(*c));
1102 INIT_FLIST_HEAD(&c->list);
1103 c->buf = malloc(GZ_CHUNK);
1109 static void free_chunk(struct iolog_compress *ic)
1115 static int z_stream_init(z_stream *stream, int gz_hdr)
1119 memset(stream, 0, sizeof(*stream));
1120 stream->zalloc = Z_NULL;
1121 stream->zfree = Z_NULL;
1122 stream->opaque = Z_NULL;
1123 stream->next_in = Z_NULL;
1126 * zlib magic - add 32 for auto-detection of gz header or not,
1127 * if we decide to store files in a gzip friendly format.
1132 if (inflateInit2(stream, wbits) != Z_OK)
1138 struct inflate_chunk_iter {
1147 static void finish_chunk(z_stream *stream, FILE *f,
1148 struct inflate_chunk_iter *iter)
1152 ret = inflateEnd(stream);
1154 log_err("fio: failed to end log inflation seq %d (%d)\n",
1157 flush_samples(f, iter->buf, iter->buf_used);
1160 iter->buf_size = iter->buf_used = 0;
1164 * Iterative chunk inflation. Handles cases where we cross into a new
1165 * sequence, doing flush finish of previous chunk if needed.
1167 static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f,
1168 z_stream *stream, struct inflate_chunk_iter *iter)
1172 dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u\n",
1173 (unsigned long) ic->len, ic->seq);
1175 if (ic->seq != iter->seq) {
1177 finish_chunk(stream, f, iter);
1179 z_stream_init(stream, gz_hdr);
1180 iter->seq = ic->seq;
1183 stream->avail_in = ic->len;
1184 stream->next_in = ic->buf;
1186 if (!iter->buf_size) {
1187 iter->buf_size = iter->chunk_sz;
1188 iter->buf = malloc(iter->buf_size);
1191 while (stream->avail_in) {
1192 size_t this_out = iter->buf_size - iter->buf_used;
1195 stream->avail_out = this_out;
1196 stream->next_out = iter->buf + iter->buf_used;
1198 err = inflate(stream, Z_NO_FLUSH);
1200 log_err("fio: failed inflating log: %d\n", err);
1205 iter->buf_used += this_out - stream->avail_out;
1207 if (!stream->avail_out) {
1208 iter->buf_size += iter->chunk_sz;
1209 iter->buf = realloc(iter->buf, iter->buf_size);
1213 if (err == Z_STREAM_END)
1217 ret = (void *) stream->next_in - ic->buf;
1219 dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) iter->buf_size);
1225 * Inflate stored compressed chunks, or write them directly to the log
1226 * file if so instructed.
1228 static int inflate_gz_chunks(struct io_log *log, FILE *f)
1230 struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, };
1233 while (!flist_empty(&log->chunk_list)) {
1234 struct iolog_compress *ic;
1236 ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list);
1237 flist_del(&ic->list);
1239 if (log->log_gz_store) {
1242 dprint(FD_COMPRESS, "log write chunk size=%lu, "
1243 "seq=%u\n", (unsigned long) ic->len, ic->seq);
1245 ret = fwrite(ic->buf, ic->len, 1, f);
1246 if (ret != 1 || ferror(f)) {
1248 log_err("fio: error writing compressed log\n");
1251 inflate_chunk(ic, log->log_gz_store, f, &stream, &iter);
1257 finish_chunk(&stream, f, &iter);
1265 * Open compressed log file and decompress the stored chunks and
1266 * write them to stdout. The chunks are stored sequentially in the
1267 * file, so we iterate over them and do them one-by-one.
1269 int iolog_file_inflate(const char *file)
1271 struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, };
1272 struct iolog_compress ic;
1280 f = fopen(file, "rb");
1286 if (stat(file, &sb) < 0) {
1292 ic.buf = buf = malloc(sb.st_size);
1293 ic.len = sb.st_size;
1296 ret = fread(ic.buf, ic.len, 1, f);
1297 if (ret == 0 && ferror(f)) {
1302 } else if (ferror(f) || (!feof(f) && ret != 1)) {
1303 log_err("fio: short read on reading log\n");
1312 * Each chunk will return Z_STREAM_END. We don't know how many
1313 * chunks are in the file, so we just keep looping and incrementing
1314 * the sequence number until we have consumed the whole compressed
1321 iret = inflate_chunk(&ic, 1, stdout, &stream, &iter);
1334 finish_chunk(&stream, stdout, &iter);
1344 static int inflate_gz_chunks(struct io_log *log, FILE *f)
1349 int iolog_file_inflate(const char *file)
1351 log_err("fio: log inflation not possible without zlib\n");
1357 void flush_log(struct io_log *log, bool do_append)
1363 * If log_gz_store is true, we are writing a binary file.
1364 * Set the mode appropriately (on all platforms) to avoid issues
1365 * on windows (line-ending conversions, etc.)
1368 if (log->log_gz_store)
1369 f = fopen(log->filename, "wb");
1371 f = fopen(log->filename, "w");
1373 if (log->log_gz_store)
1374 f = fopen(log->filename, "ab");
1376 f = fopen(log->filename, "a");
1378 perror("fopen log");
1382 buf = set_file_buffer(f);
1384 inflate_gz_chunks(log, f);
1386 while (!flist_empty(&log->io_logs)) {
1387 struct io_logs *cur_log;
1389 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1390 flist_del_init(&cur_log->list);
1392 if (log->td && log == log->td->clat_hist_log)
1393 flush_hist_samples(f, log->hist_coarseness, cur_log->log,
1394 log_sample_sz(log, cur_log));
1396 flush_samples(f, cur_log->log, log_sample_sz(log, cur_log));
1402 clear_file_buffer(buf);
1405 static int finish_log(struct thread_data *td, struct io_log *log, int trylock)
1407 if (td->flags & TD_F_COMPRESS_LOG)
1411 if (fio_trylock_file(log->filename))
1414 fio_lock_file(log->filename);
1416 if (td->client_type == FIO_CLIENT_TYPE_GUI || is_backend)
1417 fio_send_iolog(td, log, log->filename);
1419 flush_log(log, !td->o.per_job_logs);
1421 fio_unlock_file(log->filename);
1426 size_t log_chunk_sizes(struct io_log *log)
1428 struct flist_head *entry;
1431 if (flist_empty(&log->chunk_list))
1435 pthread_mutex_lock(&log->chunk_lock);
1436 flist_for_each(entry, &log->chunk_list) {
1437 struct iolog_compress *c;
1439 c = flist_entry(entry, struct iolog_compress, list);
1442 pthread_mutex_unlock(&log->chunk_lock);
1448 static void iolog_put_deferred(struct io_log *log, void *ptr)
1453 pthread_mutex_lock(&log->deferred_free_lock);
1454 if (log->deferred < IOLOG_MAX_DEFER) {
1455 log->deferred_items[log->deferred] = ptr;
1457 } else if (!fio_did_warn(FIO_WARN_IOLOG_DROP))
1458 log_err("fio: had to drop log entry free\n");
1459 pthread_mutex_unlock(&log->deferred_free_lock);
1462 static void iolog_free_deferred(struct io_log *log)
1469 pthread_mutex_lock(&log->deferred_free_lock);
1471 for (i = 0; i < log->deferred; i++) {
1472 free(log->deferred_items[i]);
1473 log->deferred_items[i] = NULL;
1477 pthread_mutex_unlock(&log->deferred_free_lock);
1480 static int gz_work(struct iolog_flush_data *data)
1482 struct iolog_compress *c = NULL;
1483 struct flist_head list;
1489 INIT_FLIST_HEAD(&list);
1491 memset(&stream, 0, sizeof(stream));
1492 stream.zalloc = Z_NULL;
1493 stream.zfree = Z_NULL;
1494 stream.opaque = Z_NULL;
1496 ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
1498 log_err("fio: failed to init gz stream\n");
1502 seq = ++data->log->chunk_seq;
1504 stream.next_in = (void *) data->samples;
1505 stream.avail_in = data->nr_samples * log_entry_sz(data->log);
1507 dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u, log=%s\n",
1508 (unsigned long) stream.avail_in, seq,
1509 data->log->filename);
1512 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq,
1513 (unsigned long) c->len);
1514 c = get_new_chunk(seq);
1515 stream.avail_out = GZ_CHUNK;
1516 stream.next_out = c->buf;
1517 ret = deflate(&stream, Z_NO_FLUSH);
1519 log_err("fio: deflate log (%d)\n", ret);
1524 c->len = GZ_CHUNK - stream.avail_out;
1525 flist_add_tail(&c->list, &list);
1527 } while (stream.avail_in);
1529 stream.next_out = c->buf + c->len;
1530 stream.avail_out = GZ_CHUNK - c->len;
1532 ret = deflate(&stream, Z_FINISH);
1535 * Z_BUF_ERROR is special, it just means we need more
1536 * output space. We'll handle that below. Treat any other
1539 if (ret != Z_BUF_ERROR) {
1540 log_err("fio: deflate log (%d)\n", ret);
1541 flist_del(&c->list);
1548 c->len = GZ_CHUNK - stream.avail_out;
1550 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, (unsigned long) c->len);
1552 if (ret != Z_STREAM_END) {
1554 c = get_new_chunk(seq);
1555 stream.avail_out = GZ_CHUNK;
1556 stream.next_out = c->buf;
1557 ret = deflate(&stream, Z_FINISH);
1558 c->len = GZ_CHUNK - stream.avail_out;
1560 flist_add_tail(&c->list, &list);
1561 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq,
1562 (unsigned long) c->len);
1563 } while (ret != Z_STREAM_END);
1566 dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total);
1568 ret = deflateEnd(&stream);
1570 log_err("fio: deflateEnd %d\n", ret);
1572 iolog_put_deferred(data->log, data->samples);
1574 if (!flist_empty(&list)) {
1575 pthread_mutex_lock(&data->log->chunk_lock);
1576 flist_splice_tail(&list, &data->log->chunk_list);
1577 pthread_mutex_unlock(&data->log->chunk_lock);
1586 while (!flist_empty(&list)) {
1587 c = flist_first_entry(list.next, struct iolog_compress, list);
1588 flist_del(&c->list);
1596 * Invoked from our compress helper thread, when logging would have exceeded
1597 * the specified memory limitation. Compresses the previously stored
1600 static int gz_work_async(struct submit_worker *sw, struct workqueue_work *work)
1602 return gz_work(container_of(work, struct iolog_flush_data, work));
1605 static int gz_init_worker(struct submit_worker *sw)
1607 struct thread_data *td = sw->wq->td;
1609 if (!fio_option_is_set(&td->o, log_gz_cpumask))
1612 if (fio_setaffinity(gettid(), td->o.log_gz_cpumask) == -1) {
1613 log_err("gz: failed to set CPU affinity\n");
1620 static struct workqueue_ops log_compress_wq_ops = {
1621 .fn = gz_work_async,
1622 .init_worker_fn = gz_init_worker,
1626 int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out)
1628 if (!(td->flags & TD_F_COMPRESS_LOG))
1631 workqueue_init(td, &td->log_compress_wq, &log_compress_wq_ops, 1, sk_out);
1635 void iolog_compress_exit(struct thread_data *td)
1637 if (!(td->flags & TD_F_COMPRESS_LOG))
1640 workqueue_exit(&td->log_compress_wq);
1644 * Queue work item to compress the existing log entries. We reset the
1645 * current log to a small size, and reference the existing log in the
1646 * data that we queue for compression. Once compression has been done,
1647 * this old log is freed. Will not return until the log compression
1648 * has completed, and will flush all previous logs too
1650 static int iolog_flush(struct io_log *log)
1652 struct iolog_flush_data *data;
1654 workqueue_flush(&log->td->log_compress_wq);
1655 data = malloc(sizeof(*data));
1662 while (!flist_empty(&log->io_logs)) {
1663 struct io_logs *cur_log;
1665 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1666 flist_del_init(&cur_log->list);
1668 data->samples = cur_log->log;
1669 data->nr_samples = cur_log->nr_samples;
1680 int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log)
1682 struct iolog_flush_data *data;
1684 data = smalloc(sizeof(*data));
1690 data->samples = cur_log->log;
1691 data->nr_samples = cur_log->nr_samples;
1694 cur_log->nr_samples = cur_log->max_samples = 0;
1695 cur_log->log = NULL;
1697 workqueue_enqueue(&log->td->log_compress_wq, &data->work);
1699 iolog_free_deferred(log);
1705 static int iolog_flush(struct io_log *log)
1710 int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log)
1715 int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out)
1720 void iolog_compress_exit(struct thread_data *td)
1726 struct io_logs *iolog_cur_log(struct io_log *log)
1728 if (flist_empty(&log->io_logs))
1731 return flist_last_entry(&log->io_logs, struct io_logs, list);
1734 uint64_t iolog_nr_samples(struct io_log *iolog)
1736 struct flist_head *entry;
1739 flist_for_each(entry, &iolog->io_logs) {
1740 struct io_logs *cur_log;
1742 cur_log = flist_entry(entry, struct io_logs, list);
1743 ret += cur_log->nr_samples;
1749 static int __write_log(struct thread_data *td, struct io_log *log, int try)
1752 return finish_log(td, log, try);
1757 static int write_iops_log(struct thread_data *td, int try, bool unit_log)
1761 if (per_unit_log(td->iops_log) != unit_log)
1764 ret = __write_log(td, td->iops_log, try);
1766 td->iops_log = NULL;
1771 static int write_slat_log(struct thread_data *td, int try, bool unit_log)
1778 ret = __write_log(td, td->slat_log, try);
1780 td->slat_log = NULL;
1785 static int write_clat_log(struct thread_data *td, int try, bool unit_log)
1792 ret = __write_log(td, td->clat_log, try);
1794 td->clat_log = NULL;
1799 static int write_clat_hist_log(struct thread_data *td, int try, bool unit_log)
1806 ret = __write_log(td, td->clat_hist_log, try);
1808 td->clat_hist_log = NULL;
1813 static int write_lat_log(struct thread_data *td, int try, bool unit_log)
1820 ret = __write_log(td, td->lat_log, try);
1827 static int write_bandw_log(struct thread_data *td, int try, bool unit_log)
1831 if (per_unit_log(td->bw_log) != unit_log)
1834 ret = __write_log(td, td->bw_log, try);
1847 CLAT_HIST_LOG_MASK = 32,
1854 int (*fn)(struct thread_data *, int, bool);
1857 static struct log_type log_types[] = {
1859 .mask = BW_LOG_MASK,
1860 .fn = write_bandw_log,
1863 .mask = LAT_LOG_MASK,
1864 .fn = write_lat_log,
1867 .mask = SLAT_LOG_MASK,
1868 .fn = write_slat_log,
1871 .mask = CLAT_LOG_MASK,
1872 .fn = write_clat_log,
1875 .mask = IOPS_LOG_MASK,
1876 .fn = write_iops_log,
1879 .mask = CLAT_HIST_LOG_MASK,
1880 .fn = write_clat_hist_log,
1884 void td_writeout_logs(struct thread_data *td, bool unit_logs)
1886 unsigned int log_mask = 0;
1887 unsigned int log_left = ALL_LOG_NR;
1890 old_state = td_bump_runstate(td, TD_FINISHING);
1892 finalize_logs(td, unit_logs);
1895 int prev_log_left = log_left;
1897 for (i = 0; i < ALL_LOG_NR && log_left; i++) {
1898 struct log_type *lt = &log_types[i];
1901 if (!(log_mask & lt->mask)) {
1902 ret = lt->fn(td, log_left != 1, unit_logs);
1905 log_mask |= lt->mask;
1910 if (prev_log_left == log_left)
1914 td_restore_runstate(td, old_state);
1917 void fio_writeout_logs(bool unit_logs)
1920 td_writeout_logs(td, unit_logs);