2 * Code related to writing an iolog of what a thread is doing, and to
3 * later read that back and replay
23 static int iolog_flush(struct io_log *log);
25 static const char iolog_ver2[] = "fio version 2 iolog";
27 void queue_io_piece(struct thread_data *td, struct io_piece *ipo)
29 flist_add_tail(&ipo->list, &td->io_log_list);
30 td->total_io_size += ipo->len;
33 void log_io_u(const struct thread_data *td, const struct io_u *io_u)
35 if (!td->o.write_iolog_file)
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);
43 void log_file(struct thread_data *td, struct fio_file *f,
44 enum file_log_act what)
46 const char *act[] = { "add", "open", "close" };
50 if (!td->o.write_iolog_file)
55 * this happens on the pre-open/close done before the job starts
60 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
63 static void iolog_delay(struct thread_data *td, unsigned long delay)
65 uint64_t usec = utime_since_now(&td->last_issue);
69 if (delay < td->time_offset) {
74 delay -= td->time_offset;
80 fio_gettime(&tv, NULL);
81 while (delay && !td->terminate) {
83 if (this_delay > 500000)
86 usec_sleep(td, this_delay);
90 usec = utime_since_now(&tv);
92 td->time_offset = usec - delay;
97 static int ipo_special(struct thread_data *td, struct io_piece *ipo)
105 if (ipo->ddir != DDIR_INVAL)
108 f = td->files[ipo->fileno];
110 switch (ipo->file_action) {
111 case FIO_LOG_OPEN_FILE:
112 ret = td_io_open_file(td, f);
115 td_verror(td, ret, "iolog open file");
117 case FIO_LOG_CLOSE_FILE:
118 td_io_close_file(td, f);
120 case FIO_LOG_UNLINK_FILE:
121 td_io_unlink_file(td, f);
124 log_err("fio: bad file action %d\n", ipo->file_action);
131 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
133 struct io_piece *ipo;
134 unsigned long elapsed;
136 while (!flist_empty(&td->io_log_list)) {
139 ipo = flist_first_entry(&td->io_log_list, struct io_piece, list);
140 flist_del(&ipo->list);
141 remove_trim_entry(td, ipo);
143 ret = ipo_special(td, ipo);
147 } else if (ret > 0) {
152 io_u->ddir = ipo->ddir;
153 if (ipo->ddir != DDIR_WAIT) {
154 io_u->offset = ipo->offset;
155 io_u->buflen = ipo->len;
156 io_u->file = td->files[ipo->fileno];
157 get_file(io_u->file);
158 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
159 io_u->buflen, io_u->file->file_name);
161 iolog_delay(td, ipo->delay);
163 elapsed = mtime_since_genesis();
164 if (ipo->delay > elapsed)
165 usec_sleep(td, (ipo->delay - elapsed) * 1000);
170 if (io_u->ddir != DDIR_WAIT)
178 void prune_io_piece_log(struct thread_data *td)
180 struct io_piece *ipo;
183 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
184 ipo = rb_entry(n, struct io_piece, rb_node);
185 rb_erase(n, &td->io_hist_tree);
186 remove_trim_entry(td, ipo);
191 while (!flist_empty(&td->io_hist_list)) {
192 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
193 flist_del(&ipo->list);
194 remove_trim_entry(td, ipo);
201 * log a successful write, so we can unwind the log for verify
203 void log_io_piece(struct thread_data *td, struct io_u *io_u)
205 struct rb_node **p, *parent;
206 struct io_piece *ipo, *__ipo;
208 ipo = malloc(sizeof(struct io_piece));
210 ipo->file = io_u->file;
211 ipo->offset = io_u->offset;
212 ipo->len = io_u->buflen;
213 ipo->numberio = io_u->numberio;
214 ipo->flags = IP_F_IN_FLIGHT;
218 if (io_u_should_trim(td, io_u)) {
219 flist_add_tail(&ipo->trim_list, &td->trim_list);
224 * We don't need to sort the entries, if:
226 * Sequential writes, or
227 * Random writes that lay out the file as it goes along
229 * For both these cases, just reading back data in the order we
230 * wrote it out is the fastest.
232 * One exception is if we don't have a random map AND we are doing
233 * verifies, in that case we need to check for duplicate blocks and
234 * drop the old one, which we rely on the rb insert/lookup for
237 if (((!td->o.verifysort) || !td_random(td) || !td->o.overwrite) &&
238 (file_randommap(td, ipo->file) || td->o.verify == VERIFY_NONE)) {
239 INIT_FLIST_HEAD(&ipo->list);
240 flist_add_tail(&ipo->list, &td->io_hist_list);
241 ipo->flags |= IP_F_ONLIST;
246 RB_CLEAR_NODE(&ipo->rb_node);
249 * Sort the entry into the verification list
252 p = &td->io_hist_tree.rb_node;
258 __ipo = rb_entry(parent, struct io_piece, rb_node);
259 if (ipo->file < __ipo->file)
261 else if (ipo->file > __ipo->file)
263 else if (ipo->offset < __ipo->offset) {
265 overlap = ipo->offset + ipo->len > __ipo->offset;
267 else if (ipo->offset > __ipo->offset) {
269 overlap = __ipo->offset + __ipo->len > ipo->offset;
275 dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu",
276 __ipo->offset, __ipo->len,
277 ipo->offset, ipo->len);
279 rb_erase(parent, &td->io_hist_tree);
280 remove_trim_entry(td, __ipo);
286 rb_link_node(&ipo->rb_node, parent, p);
287 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
288 ipo->flags |= IP_F_ONRB;
292 void unlog_io_piece(struct thread_data *td, struct io_u *io_u)
294 struct io_piece *ipo = io_u->ipo;
296 if (td->ts.nr_block_infos) {
297 uint32_t *info = io_u_block_info(td, io_u);
298 if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) {
299 if (io_u->ddir == DDIR_TRIM)
300 *info = BLOCK_INFO_SET_STATE(*info,
301 BLOCK_STATE_TRIM_FAILURE);
302 else if (io_u->ddir == DDIR_WRITE)
303 *info = BLOCK_INFO_SET_STATE(*info,
304 BLOCK_STATE_WRITE_FAILURE);
311 if (ipo->flags & IP_F_ONRB)
312 rb_erase(&ipo->rb_node, &td->io_hist_tree);
313 else if (ipo->flags & IP_F_ONLIST)
314 flist_del(&ipo->list);
321 void trim_io_piece(struct thread_data *td, const struct io_u *io_u)
323 struct io_piece *ipo = io_u->ipo;
328 ipo->len = io_u->xfer_buflen - io_u->resid;
331 void write_iolog_close(struct thread_data *td)
337 td->iolog_buf = NULL;
341 * Read version 2 iolog data. It is enhanced to include per-file logging,
344 static int read_iolog2(struct thread_data *td, FILE *f)
346 unsigned long long offset;
348 int reads, writes, waits, fileno = 0, file_action = 0; /* stupid gcc */
353 free_release_files(td);
356 * Read in the read iolog and store it, reuse the infrastructure
357 * for doing verifications.
360 fname = malloc(256+16);
361 act = malloc(256+16);
363 reads = writes = waits = 0;
364 while ((p = fgets(str, 4096, f)) != NULL) {
365 struct io_piece *ipo;
368 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
374 if (!strcmp(act, "wait"))
376 else if (!strcmp(act, "read"))
378 else if (!strcmp(act, "write"))
380 else if (!strcmp(act, "sync"))
382 else if (!strcmp(act, "datasync"))
384 else if (!strcmp(act, "trim"))
387 log_err("fio: bad iolog file action: %s\n",
391 fileno = get_fileno(td, fname);
394 if (!strcmp(act, "add")) {
395 fileno = add_file(td, fname, 0, 1);
396 file_action = FIO_LOG_ADD_FILE;
398 } else if (!strcmp(act, "open")) {
399 fileno = get_fileno(td, fname);
400 file_action = FIO_LOG_OPEN_FILE;
401 } else if (!strcmp(act, "close")) {
402 fileno = get_fileno(td, fname);
403 file_action = FIO_LOG_CLOSE_FILE;
405 log_err("fio: bad iolog file action: %s\n",
410 log_err("bad iolog2: %s", p);
416 else if (rw == DDIR_WRITE) {
418 * Don't add a write for ro mode
423 } else if (rw == DDIR_WAIT) {
425 } else if (rw == DDIR_INVAL) {
426 } else if (!ddir_sync(rw)) {
427 log_err("bad ddir: %d\n", rw);
434 ipo = malloc(sizeof(*ipo));
437 if (rw == DDIR_WAIT) {
440 ipo->offset = offset;
442 if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw])
443 td->o.max_bs[rw] = bytes;
444 ipo->fileno = fileno;
445 ipo->file_action = file_action;
449 queue_io_piece(td, ipo);
456 if (writes && read_only) {
457 log_err("fio: <%s> skips replay of %d writes due to"
458 " read-only\n", td->o.name, writes);
462 if (!reads && !writes && !waits)
464 else if (reads && !writes)
465 td->o.td_ddir = TD_DDIR_READ;
466 else if (!reads && writes)
467 td->o.td_ddir = TD_DDIR_WRITE;
469 td->o.td_ddir = TD_DDIR_RW;
475 * open iolog, check version, and call appropriate parser
477 static int init_iolog_read(struct thread_data *td)
479 char buffer[256], *p;
483 f = fopen(td->o.read_iolog_file, "r");
485 perror("fopen read iolog");
489 p = fgets(buffer, sizeof(buffer), f);
491 td_verror(td, errno, "iolog read");
492 log_err("fio: unable to read iolog\n");
498 * version 2 of the iolog stores a specific string as the
499 * first line, check for that
501 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
502 ret = read_iolog2(td, f);
504 log_err("fio: iolog version 1 is no longer supported\n");
513 * Set up a log for storing io patterns.
515 static int init_iolog_write(struct thread_data *td)
521 f = fopen(td->o.write_iolog_file, "a");
523 perror("fopen write iolog");
528 * That's it for writing, setup a log buffer and we're done.
531 td->iolog_buf = malloc(8192);
532 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
535 * write our version line
537 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
538 perror("iolog init\n");
543 * add all known files
545 for_each_file(td, ff, i)
546 log_file(td, ff, FIO_LOG_ADD_FILE);
551 int init_iolog(struct thread_data *td)
555 if (td->o.read_iolog_file) {
559 * Check if it's a blktrace file and load that if possible.
560 * Otherwise assume it's a normal log file and load that.
562 if (is_blktrace(td->o.read_iolog_file, &need_swap))
563 ret = load_blktrace(td, td->o.read_iolog_file, need_swap);
565 ret = init_iolog_read(td);
566 } else if (td->o.write_iolog_file)
567 ret = init_iolog_write(td);
570 td_verror(td, EINVAL, "failed initializing iolog");
575 void setup_log(struct io_log **log, struct log_params *p,
576 const char *filename)
580 l = scalloc(1, sizeof(*l));
581 INIT_FLIST_HEAD(&l->io_logs);
582 l->log_type = p->log_type;
583 l->log_offset = p->log_offset;
584 l->log_gz = p->log_gz;
585 l->log_gz_store = p->log_gz_store;
586 l->avg_msec = p->avg_msec;
587 l->hist_msec = p->hist_msec;
588 l->hist_coarseness = p->hist_coarseness;
589 l->filename = strdup(filename);
592 if (l->td && l->td->o.io_submit_mode != IO_MODE_OFFLOAD) {
595 p = calloc(1, sizeof(*l->pending));
596 p->max_samples = DEF_LOG_ENTRIES;
597 p->log = calloc(p->max_samples, log_entry_sz(l));
602 l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT;
604 INIT_FLIST_HEAD(&l->chunk_list);
606 if (l->log_gz && !p->td)
608 else if (l->log_gz || l->log_gz_store) {
609 mutex_init_pshared(&l->chunk_lock);
610 p->td->flags |= TD_F_COMPRESS_LOG;
616 #ifdef CONFIG_SETVBUF
617 static void *set_file_buffer(FILE *f)
619 size_t size = 1048576;
623 setvbuf(f, buf, _IOFBF, size);
627 static void clear_file_buffer(void *buf)
632 static void *set_file_buffer(FILE *f)
637 static void clear_file_buffer(void *buf)
642 void free_log(struct io_log *log)
644 while (!flist_empty(&log->io_logs)) {
645 struct io_logs *cur_log;
647 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
648 flist_del_init(&cur_log->list);
654 free(log->pending->log);
664 static inline int hist_sum(int j, int stride, unsigned int *io_u_plat)
668 for (k = sum = 0; k < stride; k++)
669 sum += io_u_plat[j + k];
674 void flush_hist_samples(FILE *f, int hist_coarseness, void *samples,
675 uint64_t sample_size)
679 uint64_t i, j, nr_samples;
680 unsigned int *io_u_plat;
682 int stride = 1 << hist_coarseness;
687 s = __get_sample(samples, 0, 0);
688 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
690 nr_samples = sample_size / __log_entry_sz(log_offset);
692 for (i = 0; i < nr_samples; i++) {
693 s = __get_sample(samples, log_offset, i);
694 io_u_plat = (unsigned int *) s->val;
695 fprintf(f, "%lu, %u, %u, ", (unsigned long)s->time,
696 io_sample_ddir(s), s->bs);
697 for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) {
698 fprintf(f, "%lu, ", (unsigned long) hist_sum(j, stride, io_u_plat));
700 fprintf(f, "%lu\n", (unsigned long)
701 hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat));
706 void flush_samples(FILE *f, void *samples, uint64_t sample_size)
710 uint64_t i, nr_samples;
715 s = __get_sample(samples, 0, 0);
716 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
718 nr_samples = sample_size / __log_entry_sz(log_offset);
720 for (i = 0; i < nr_samples; i++) {
721 s = __get_sample(samples, log_offset, i);
724 fprintf(f, "%lu, %lu, %u, %u\n",
725 (unsigned long) s->time,
726 (unsigned long) s->val,
727 io_sample_ddir(s), s->bs);
729 struct io_sample_offset *so = (void *) s;
731 fprintf(f, "%lu, %lu, %u, %u, %llu\n",
732 (unsigned long) s->time,
733 (unsigned long) s->val,
734 io_sample_ddir(s), s->bs,
735 (unsigned long long) so->offset);
742 struct iolog_flush_data {
743 struct workqueue_work work;
750 #define GZ_CHUNK 131072
752 static struct iolog_compress *get_new_chunk(unsigned int seq)
754 struct iolog_compress *c;
756 c = malloc(sizeof(*c));
757 INIT_FLIST_HEAD(&c->list);
758 c->buf = malloc(GZ_CHUNK);
764 static void free_chunk(struct iolog_compress *ic)
770 static int z_stream_init(z_stream *stream, int gz_hdr)
774 memset(stream, 0, sizeof(*stream));
775 stream->zalloc = Z_NULL;
776 stream->zfree = Z_NULL;
777 stream->opaque = Z_NULL;
778 stream->next_in = Z_NULL;
781 * zlib magic - add 32 for auto-detection of gz header or not,
782 * if we decide to store files in a gzip friendly format.
787 if (inflateInit2(stream, wbits) != Z_OK)
793 struct inflate_chunk_iter {
802 static void finish_chunk(z_stream *stream, FILE *f,
803 struct inflate_chunk_iter *iter)
807 ret = inflateEnd(stream);
809 log_err("fio: failed to end log inflation seq %d (%d)\n",
812 flush_samples(f, iter->buf, iter->buf_used);
815 iter->buf_size = iter->buf_used = 0;
819 * Iterative chunk inflation. Handles cases where we cross into a new
820 * sequence, doing flush finish of previous chunk if needed.
822 static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f,
823 z_stream *stream, struct inflate_chunk_iter *iter)
827 dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u\n",
828 (unsigned long) ic->len, ic->seq);
830 if (ic->seq != iter->seq) {
832 finish_chunk(stream, f, iter);
834 z_stream_init(stream, gz_hdr);
838 stream->avail_in = ic->len;
839 stream->next_in = ic->buf;
841 if (!iter->buf_size) {
842 iter->buf_size = iter->chunk_sz;
843 iter->buf = malloc(iter->buf_size);
846 while (stream->avail_in) {
847 size_t this_out = iter->buf_size - iter->buf_used;
850 stream->avail_out = this_out;
851 stream->next_out = iter->buf + iter->buf_used;
853 err = inflate(stream, Z_NO_FLUSH);
855 log_err("fio: failed inflating log: %d\n", err);
860 iter->buf_used += this_out - stream->avail_out;
862 if (!stream->avail_out) {
863 iter->buf_size += iter->chunk_sz;
864 iter->buf = realloc(iter->buf, iter->buf_size);
868 if (err == Z_STREAM_END)
872 ret = (void *) stream->next_in - ic->buf;
874 dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) iter->buf_size);
880 * Inflate stored compressed chunks, or write them directly to the log
881 * file if so instructed.
883 static int inflate_gz_chunks(struct io_log *log, FILE *f)
885 struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, };
888 while (!flist_empty(&log->chunk_list)) {
889 struct iolog_compress *ic;
891 ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list);
892 flist_del(&ic->list);
894 if (log->log_gz_store) {
897 dprint(FD_COMPRESS, "log write chunk size=%lu, "
898 "seq=%u\n", (unsigned long) ic->len, ic->seq);
900 ret = fwrite(ic->buf, ic->len, 1, f);
901 if (ret != 1 || ferror(f)) {
903 log_err("fio: error writing compressed log\n");
906 inflate_chunk(ic, log->log_gz_store, f, &stream, &iter);
912 finish_chunk(&stream, f, &iter);
920 * Open compressed log file and decompress the stored chunks and
921 * write them to stdout. The chunks are stored sequentially in the
922 * file, so we iterate over them and do them one-by-one.
924 int iolog_file_inflate(const char *file)
926 struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, };
927 struct iolog_compress ic;
935 f = fopen(file, "r");
941 if (stat(file, &sb) < 0) {
947 ic.buf = buf = malloc(sb.st_size);
951 ret = fread(ic.buf, ic.len, 1, f);
957 } else if (ret != 1) {
958 log_err("fio: short read on reading log\n");
967 * Each chunk will return Z_STREAM_END. We don't know how many
968 * chunks are in the file, so we just keep looping and incrementing
969 * the sequence number until we have consumed the whole compressed
976 iret = inflate_chunk(&ic, 1, stdout, &stream, &iter);
989 finish_chunk(&stream, stdout, &iter);
999 static int inflate_gz_chunks(struct io_log *log, FILE *f)
1004 int iolog_file_inflate(const char *file)
1006 log_err("fio: log inflation not possible without zlib\n");
1012 void flush_log(struct io_log *log, bool do_append)
1018 f = fopen(log->filename, "w");
1020 f = fopen(log->filename, "a");
1022 perror("fopen log");
1026 buf = set_file_buffer(f);
1028 inflate_gz_chunks(log, f);
1030 while (!flist_empty(&log->io_logs)) {
1031 struct io_logs *cur_log;
1033 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1034 flist_del_init(&cur_log->list);
1036 if (log == log->td->clat_hist_log)
1037 flush_hist_samples(f, log->hist_coarseness, cur_log->log,
1038 cur_log->nr_samples * log_entry_sz(log));
1040 flush_samples(f, cur_log->log, cur_log->nr_samples * log_entry_sz(log));
1046 clear_file_buffer(buf);
1049 static int finish_log(struct thread_data *td, struct io_log *log, int trylock)
1051 if (td->flags & TD_F_COMPRESS_LOG)
1055 if (fio_trylock_file(log->filename))
1058 fio_lock_file(log->filename);
1060 if (td->client_type == FIO_CLIENT_TYPE_GUI || is_backend)
1061 fio_send_iolog(td, log, log->filename);
1063 flush_log(log, !td->o.per_job_logs);
1065 fio_unlock_file(log->filename);
1070 size_t log_chunk_sizes(struct io_log *log)
1072 struct flist_head *entry;
1075 if (flist_empty(&log->chunk_list))
1079 pthread_mutex_lock(&log->chunk_lock);
1080 flist_for_each(entry, &log->chunk_list) {
1081 struct iolog_compress *c;
1083 c = flist_entry(entry, struct iolog_compress, list);
1086 pthread_mutex_unlock(&log->chunk_lock);
1092 static int gz_work(struct iolog_flush_data *data)
1094 struct iolog_compress *c = NULL;
1095 struct flist_head list;
1101 INIT_FLIST_HEAD(&list);
1103 memset(&stream, 0, sizeof(stream));
1104 stream.zalloc = Z_NULL;
1105 stream.zfree = Z_NULL;
1106 stream.opaque = Z_NULL;
1108 ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
1110 log_err("fio: failed to init gz stream\n");
1114 seq = ++data->log->chunk_seq;
1116 stream.next_in = (void *) data->samples;
1117 stream.avail_in = data->nr_samples * log_entry_sz(data->log);
1119 dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u, log=%s\n",
1120 (unsigned long) stream.avail_in, seq,
1121 data->log->filename);
1124 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, c->len);
1125 c = get_new_chunk(seq);
1126 stream.avail_out = GZ_CHUNK;
1127 stream.next_out = c->buf;
1128 ret = deflate(&stream, Z_NO_FLUSH);
1130 log_err("fio: deflate log (%d)\n", ret);
1135 c->len = GZ_CHUNK - stream.avail_out;
1136 flist_add_tail(&c->list, &list);
1138 } while (stream.avail_in);
1140 stream.next_out = c->buf + c->len;
1141 stream.avail_out = GZ_CHUNK - c->len;
1143 ret = deflate(&stream, Z_FINISH);
1146 * Z_BUF_ERROR is special, it just means we need more
1147 * output space. We'll handle that below. Treat any other
1150 if (ret != Z_BUF_ERROR) {
1151 log_err("fio: deflate log (%d)\n", ret);
1152 flist_del(&c->list);
1159 c->len = GZ_CHUNK - stream.avail_out;
1161 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, c->len);
1163 if (ret != Z_STREAM_END) {
1165 c = get_new_chunk(seq);
1166 stream.avail_out = GZ_CHUNK;
1167 stream.next_out = c->buf;
1168 ret = deflate(&stream, Z_FINISH);
1169 c->len = GZ_CHUNK - stream.avail_out;
1171 flist_add_tail(&c->list, &list);
1172 dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, c->len);
1173 } while (ret != Z_STREAM_END);
1176 dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total);
1178 ret = deflateEnd(&stream);
1180 log_err("fio: deflateEnd %d\n", ret);
1182 free(data->samples);
1184 if (!flist_empty(&list)) {
1185 pthread_mutex_lock(&data->log->chunk_lock);
1186 flist_splice_tail(&list, &data->log->chunk_list);
1187 pthread_mutex_unlock(&data->log->chunk_lock);
1196 while (!flist_empty(&list)) {
1197 c = flist_first_entry(list.next, struct iolog_compress, list);
1198 flist_del(&c->list);
1206 * Invoked from our compress helper thread, when logging would have exceeded
1207 * the specified memory limitation. Compresses the previously stored
1210 static int gz_work_async(struct submit_worker *sw, struct workqueue_work *work)
1212 return gz_work(container_of(work, struct iolog_flush_data, work));
1215 static int gz_init_worker(struct submit_worker *sw)
1217 struct thread_data *td = sw->wq->td;
1219 if (!fio_option_is_set(&td->o, log_gz_cpumask))
1222 if (fio_setaffinity(gettid(), td->o.log_gz_cpumask) == -1) {
1223 log_err("gz: failed to set CPU affinity\n");
1230 static struct workqueue_ops log_compress_wq_ops = {
1231 .fn = gz_work_async,
1232 .init_worker_fn = gz_init_worker,
1236 int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out)
1238 if (!(td->flags & TD_F_COMPRESS_LOG))
1241 workqueue_init(td, &td->log_compress_wq, &log_compress_wq_ops, 1, sk_out);
1245 void iolog_compress_exit(struct thread_data *td)
1247 if (!(td->flags & TD_F_COMPRESS_LOG))
1250 workqueue_exit(&td->log_compress_wq);
1254 * Queue work item to compress the existing log entries. We reset the
1255 * current log to a small size, and reference the existing log in the
1256 * data that we queue for compression. Once compression has been done,
1257 * this old log is freed. If called with finish == true, will not return
1258 * until the log compression has completed, and will flush all previous
1261 static int iolog_flush(struct io_log *log)
1263 struct iolog_flush_data *data;
1265 data = malloc(sizeof(*data));
1272 while (!flist_empty(&log->io_logs)) {
1273 struct io_logs *cur_log;
1275 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1276 flist_del_init(&cur_log->list);
1278 data->samples = cur_log->log;
1279 data->nr_samples = cur_log->nr_samples;
1290 int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log)
1292 struct iolog_flush_data *data;
1294 data = malloc(sizeof(*data));
1300 data->samples = cur_log->log;
1301 data->nr_samples = cur_log->nr_samples;
1304 cur_log->nr_samples = cur_log->max_samples = 0;
1305 cur_log->log = NULL;
1307 workqueue_enqueue(&log->td->log_compress_wq, &data->work);
1312 static int iolog_flush(struct io_log *log)
1317 int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log)
1322 int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out)
1327 void iolog_compress_exit(struct thread_data *td)
1333 struct io_logs *iolog_cur_log(struct io_log *log)
1335 if (flist_empty(&log->io_logs))
1338 return flist_last_entry(&log->io_logs, struct io_logs, list);
1341 uint64_t iolog_nr_samples(struct io_log *iolog)
1343 struct flist_head *entry;
1346 flist_for_each(entry, &iolog->io_logs) {
1347 struct io_logs *cur_log;
1349 cur_log = flist_entry(entry, struct io_logs, list);
1350 ret += cur_log->nr_samples;
1356 static int __write_log(struct thread_data *td, struct io_log *log, int try)
1359 return finish_log(td, log, try);
1364 static int write_iops_log(struct thread_data *td, int try, bool unit_log)
1368 if (per_unit_log(td->iops_log) != unit_log)
1371 ret = __write_log(td, td->iops_log, try);
1373 td->iops_log = NULL;
1378 static int write_slat_log(struct thread_data *td, int try, bool unit_log)
1385 ret = __write_log(td, td->slat_log, try);
1387 td->slat_log = NULL;
1392 static int write_clat_log(struct thread_data *td, int try, bool unit_log)
1399 ret = __write_log(td, td->clat_log, try);
1401 td->clat_log = NULL;
1406 static int write_clat_hist_log(struct thread_data *td, int try, bool unit_log)
1413 ret = __write_log(td, td->clat_hist_log, try);
1415 td->clat_hist_log = NULL;
1420 static int write_lat_log(struct thread_data *td, int try, bool unit_log)
1427 ret = __write_log(td, td->lat_log, try);
1434 static int write_bandw_log(struct thread_data *td, int try, bool unit_log)
1438 if (per_unit_log(td->bw_log) != unit_log)
1441 ret = __write_log(td, td->bw_log, try);
1454 CLAT_HIST_LOG_MASK = 32,
1461 int (*fn)(struct thread_data *, int, bool);
1464 static struct log_type log_types[] = {
1466 .mask = BW_LOG_MASK,
1467 .fn = write_bandw_log,
1470 .mask = LAT_LOG_MASK,
1471 .fn = write_lat_log,
1474 .mask = SLAT_LOG_MASK,
1475 .fn = write_slat_log,
1478 .mask = CLAT_LOG_MASK,
1479 .fn = write_clat_log,
1482 .mask = IOPS_LOG_MASK,
1483 .fn = write_iops_log,
1486 .mask = CLAT_HIST_LOG_MASK,
1487 .fn = write_clat_hist_log,
1491 void td_writeout_logs(struct thread_data *td, bool unit_logs)
1493 unsigned int log_mask = 0;
1494 unsigned int log_left = ALL_LOG_NR;
1497 old_state = td_bump_runstate(td, TD_FINISHING);
1499 finalize_logs(td, unit_logs);
1502 int prev_log_left = log_left;
1504 for (i = 0; i < ALL_LOG_NR && log_left; i++) {
1505 struct log_type *lt = &log_types[i];
1508 if (!(log_mask & lt->mask)) {
1509 ret = lt->fn(td, log_left != 1, unit_logs);
1512 log_mask |= lt->mask;
1517 if (prev_log_left == log_left)
1521 td_restore_runstate(td, old_state);
1524 void fio_writeout_logs(bool unit_logs)
1526 struct thread_data *td;
1530 td_writeout_logs(td, unit_logs);