verify: escape '/' in state file name to '.'
[fio.git] / iolog.c
CommitLineData
ac9b9101
JA
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 <libgen.h>
8#include <assert.h>
b26317c9
JA
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <unistd.h>
aee2ab67
JA
12#ifdef CONFIG_ZLIB
13#include <zlib.h>
14#endif
15
ac9b9101
JA
16#include "flist.h"
17#include "fio.h"
18#include "verify.h"
19#include "trim.h"
243bfe19 20#include "filelock.h"
a47591e4 21#include "smalloc.h"
ac9b9101
JA
22
23static const char iolog_ver2[] = "fio version 2 iolog";
24
25void queue_io_piece(struct thread_data *td, struct io_piece *ipo)
26{
27 flist_add_tail(&ipo->list, &td->io_log_list);
28 td->total_io_size += ipo->len;
29}
30
1f440ece 31void log_io_u(const struct thread_data *td, const struct io_u *io_u)
ac9b9101 32{
ac9b9101
JA
33 if (!td->o.write_iolog_file)
34 return;
35
36 fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name,
173375c2
JA
37 io_ddir_name(io_u->ddir),
38 io_u->offset, io_u->buflen);
ac9b9101
JA
39}
40
41void log_file(struct thread_data *td, struct fio_file *f,
42 enum file_log_act what)
43{
44 const char *act[] = { "add", "open", "close" };
45
46 assert(what < 3);
47
48 if (!td->o.write_iolog_file)
49 return;
50
51
52 /*
53 * this happens on the pre-open/close done before the job starts
54 */
55 if (!td->iolog_f)
56 return;
57
58 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
59}
60
61static void iolog_delay(struct thread_data *td, unsigned long delay)
62{
df8fb609
JA
63 uint64_t usec = utime_since_now(&td->last_issue);
64 uint64_t this_delay;
65 struct timeval tv;
ac9b9101 66
df8fb609
JA
67 if (delay < td->time_offset) {
68 td->time_offset = 0;
69 return;
70 }
71
72 delay -= td->time_offset;
ac9b9101
JA
73 if (delay < usec)
74 return;
75
76 delay -= usec;
77
df8fb609 78 fio_gettime(&tv, NULL);
30b18672
JA
79 while (delay && !td->terminate) {
80 this_delay = delay;
81 if (this_delay > 500000)
82 this_delay = 500000;
83
84 usec_sleep(td, this_delay);
85 delay -= this_delay;
86 }
df8fb609
JA
87
88 usec = utime_since_now(&tv);
89 if (usec > delay)
90 td->time_offset = usec - delay;
91 else
92 td->time_offset = 0;
ac9b9101
JA
93}
94
95static int ipo_special(struct thread_data *td, struct io_piece *ipo)
96{
97 struct fio_file *f;
98 int ret;
99
100 /*
101 * Not a special ipo
102 */
103 if (ipo->ddir != DDIR_INVAL)
104 return 0;
105
106 f = td->files[ipo->fileno];
107
108 switch (ipo->file_action) {
109 case FIO_LOG_OPEN_FILE:
110 ret = td_io_open_file(td, f);
111 if (!ret)
112 break;
113 td_verror(td, ret, "iolog open file");
114 return -1;
115 case FIO_LOG_CLOSE_FILE:
116 td_io_close_file(td, f);
117 break;
118 case FIO_LOG_UNLINK_FILE:
38ef9c90 119 td_io_unlink_file(td, f);
ac9b9101
JA
120 break;
121 default:
122 log_err("fio: bad file action %d\n", ipo->file_action);
123 break;
124 }
125
126 return 1;
127}
128
129int read_iolog_get(struct thread_data *td, struct io_u *io_u)
130{
131 struct io_piece *ipo;
132 unsigned long elapsed;
3c3ed070 133
ac9b9101
JA
134 while (!flist_empty(&td->io_log_list)) {
135 int ret;
136
9342d5f8 137 ipo = flist_first_entry(&td->io_log_list, struct io_piece, list);
ac9b9101
JA
138 flist_del(&ipo->list);
139 remove_trim_entry(td, ipo);
140
141 ret = ipo_special(td, ipo);
142 if (ret < 0) {
143 free(ipo);
144 break;
145 } else if (ret > 0) {
146 free(ipo);
147 continue;
148 }
149
150 io_u->ddir = ipo->ddir;
151 if (ipo->ddir != DDIR_WAIT) {
152 io_u->offset = ipo->offset;
153 io_u->buflen = ipo->len;
154 io_u->file = td->files[ipo->fileno];
155 get_file(io_u->file);
156 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
157 io_u->buflen, io_u->file->file_name);
158 if (ipo->delay)
159 iolog_delay(td, ipo->delay);
160 } else {
161 elapsed = mtime_since_genesis();
162 if (ipo->delay > elapsed)
163 usec_sleep(td, (ipo->delay - elapsed) * 1000);
ac9b9101
JA
164 }
165
166 free(ipo);
3c3ed070 167
ac9b9101
JA
168 if (io_u->ddir != DDIR_WAIT)
169 return 0;
170 }
171
172 td->done = 1;
173 return 1;
174}
175
176void prune_io_piece_log(struct thread_data *td)
177{
178 struct io_piece *ipo;
179 struct rb_node *n;
180
181 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
182 ipo = rb_entry(n, struct io_piece, rb_node);
183 rb_erase(n, &td->io_hist_tree);
184 remove_trim_entry(td, ipo);
185 td->io_hist_len--;
186 free(ipo);
187 }
188
189 while (!flist_empty(&td->io_hist_list)) {
4e1020f8 190 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
ac9b9101
JA
191 flist_del(&ipo->list);
192 remove_trim_entry(td, ipo);
193 td->io_hist_len--;
194 free(ipo);
195 }
196}
197
198/*
199 * log a successful write, so we can unwind the log for verify
200 */
201void log_io_piece(struct thread_data *td, struct io_u *io_u)
202{
203 struct rb_node **p, *parent;
204 struct io_piece *ipo, *__ipo;
205
206 ipo = malloc(sizeof(struct io_piece));
207 init_ipo(ipo);
208 ipo->file = io_u->file;
209 ipo->offset = io_u->offset;
210 ipo->len = io_u->buflen;
da0a7bd2 211 ipo->numberio = io_u->numberio;
f9401285
JA
212 ipo->flags = IP_F_IN_FLIGHT;
213
214 io_u->ipo = ipo;
ac9b9101
JA
215
216 if (io_u_should_trim(td, io_u)) {
217 flist_add_tail(&ipo->trim_list, &td->trim_list);
218 td->trim_entries++;
219 }
220
221 /*
222 * We don't need to sort the entries, if:
223 *
224 * Sequential writes, or
225 * Random writes that lay out the file as it goes along
226 *
227 * For both these cases, just reading back data in the order we
228 * wrote it out is the fastest.
229 *
230 * One exception is if we don't have a random map AND we are doing
231 * verifies, in that case we need to check for duplicate blocks and
232 * drop the old one, which we rely on the rb insert/lookup for
233 * handling.
234 */
c4b6117b 235 if (((!td->o.verifysort) || !td_random(td) || !td->o.overwrite) &&
ac9b9101
JA
236 (file_randommap(td, ipo->file) || td->o.verify == VERIFY_NONE)) {
237 INIT_FLIST_HEAD(&ipo->list);
238 flist_add_tail(&ipo->list, &td->io_hist_list);
239 ipo->flags |= IP_F_ONLIST;
240 td->io_hist_len++;
241 return;
242 }
243
244 RB_CLEAR_NODE(&ipo->rb_node);
245
246 /*
247 * Sort the entry into the verification list
248 */
249restart:
250 p = &td->io_hist_tree.rb_node;
251 parent = NULL;
252 while (*p) {
3b1ff869 253 int overlap = 0;
ac9b9101
JA
254 parent = *p;
255
256 __ipo = rb_entry(parent, struct io_piece, rb_node);
257 if (ipo->file < __ipo->file)
258 p = &(*p)->rb_left;
259 else if (ipo->file > __ipo->file)
260 p = &(*p)->rb_right;
3b1ff869 261 else if (ipo->offset < __ipo->offset) {
ac9b9101 262 p = &(*p)->rb_left;
3b1ff869
JE
263 overlap = ipo->offset + ipo->len > __ipo->offset;
264 }
265 else if (ipo->offset > __ipo->offset) {
ac9b9101 266 p = &(*p)->rb_right;
3b1ff869
JE
267 overlap = __ipo->offset + __ipo->len > ipo->offset;
268 }
269 else
270 overlap = 1;
271
272 if (overlap) {
885ac623
JA
273 dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu",
274 __ipo->offset, __ipo->len,
275 ipo->offset, ipo->len);
ac9b9101
JA
276 td->io_hist_len--;
277 rb_erase(parent, &td->io_hist_tree);
278 remove_trim_entry(td, __ipo);
279 free(__ipo);
280 goto restart;
281 }
282 }
283
284 rb_link_node(&ipo->rb_node, parent, p);
285 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
286 ipo->flags |= IP_F_ONRB;
287 td->io_hist_len++;
288}
289
890b6656
JA
290void unlog_io_piece(struct thread_data *td, struct io_u *io_u)
291{
292 struct io_piece *ipo = io_u->ipo;
293
66347cfa
DE
294 if (td->ts.nr_block_infos) {
295 uint32_t *info = io_u_block_info(td, io_u);
296 if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) {
297 if (io_u->ddir == DDIR_TRIM)
298 *info = BLOCK_INFO_SET_STATE(*info,
299 BLOCK_STATE_TRIM_FAILURE);
300 else if (io_u->ddir == DDIR_WRITE)
301 *info = BLOCK_INFO_SET_STATE(*info,
302 BLOCK_STATE_WRITE_FAILURE);
303 }
304 }
305
890b6656
JA
306 if (!ipo)
307 return;
308
309 if (ipo->flags & IP_F_ONRB)
310 rb_erase(&ipo->rb_node, &td->io_hist_tree);
311 else if (ipo->flags & IP_F_ONLIST)
312 flist_del(&ipo->list);
313
314 free(ipo);
315 io_u->ipo = NULL;
316 td->io_hist_len--;
317}
318
1f440ece 319void trim_io_piece(struct thread_data *td, const struct io_u *io_u)
890b6656
JA
320{
321 struct io_piece *ipo = io_u->ipo;
322
323 if (!ipo)
324 return;
325
326 ipo->len = io_u->xfer_buflen - io_u->resid;
327}
328
ac9b9101
JA
329void write_iolog_close(struct thread_data *td)
330{
331 fflush(td->iolog_f);
332 fclose(td->iolog_f);
333 free(td->iolog_buf);
334 td->iolog_f = NULL;
335 td->iolog_buf = NULL;
336}
337
338/*
339 * Read version 2 iolog data. It is enhanced to include per-file logging,
340 * syncs, etc.
341 */
342static int read_iolog2(struct thread_data *td, FILE *f)
343{
344 unsigned long long offset;
345 unsigned int bytes;
346 int reads, writes, waits, fileno = 0, file_action = 0; /* stupid gcc */
347 char *fname, *act;
348 char *str, *p;
349 enum fio_ddir rw;
350
351 free_release_files(td);
352
353 /*
354 * Read in the read iolog and store it, reuse the infrastructure
355 * for doing verifications.
356 */
357 str = malloc(4096);
358 fname = malloc(256+16);
359 act = malloc(256+16);
360
361 reads = writes = waits = 0;
362 while ((p = fgets(str, 4096, f)) != NULL) {
363 struct io_piece *ipo;
364 int r;
365
366 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
367 &bytes);
368 if (r == 4) {
369 /*
370 * Check action first
371 */
372 if (!strcmp(act, "wait"))
373 rw = DDIR_WAIT;
374 else if (!strcmp(act, "read"))
375 rw = DDIR_READ;
376 else if (!strcmp(act, "write"))
377 rw = DDIR_WRITE;
378 else if (!strcmp(act, "sync"))
379 rw = DDIR_SYNC;
380 else if (!strcmp(act, "datasync"))
381 rw = DDIR_DATASYNC;
382 else if (!strcmp(act, "trim"))
383 rw = DDIR_TRIM;
384 else {
385 log_err("fio: bad iolog file action: %s\n",
386 act);
387 continue;
388 }
033ace1e 389 fileno = get_fileno(td, fname);
ac9b9101
JA
390 } else if (r == 2) {
391 rw = DDIR_INVAL;
392 if (!strcmp(act, "add")) {
5903e7b7 393 fileno = add_file(td, fname, 0, 1);
ac9b9101
JA
394 file_action = FIO_LOG_ADD_FILE;
395 continue;
396 } else if (!strcmp(act, "open")) {
397 fileno = get_fileno(td, fname);
398 file_action = FIO_LOG_OPEN_FILE;
399 } else if (!strcmp(act, "close")) {
400 fileno = get_fileno(td, fname);
401 file_action = FIO_LOG_CLOSE_FILE;
402 } else {
403 log_err("fio: bad iolog file action: %s\n",
404 act);
405 continue;
406 }
407 } else {
408 log_err("bad iolog2: %s", p);
409 continue;
410 }
411
412 if (rw == DDIR_READ)
413 reads++;
414 else if (rw == DDIR_WRITE) {
415 /*
416 * Don't add a write for ro mode
417 */
418 if (read_only)
419 continue;
420 writes++;
421 } else if (rw == DDIR_WAIT) {
422 waits++;
423 } else if (rw == DDIR_INVAL) {
424 } else if (!ddir_sync(rw)) {
425 log_err("bad ddir: %d\n", rw);
426 continue;
427 }
428
429 /*
430 * Make note of file
431 */
432 ipo = malloc(sizeof(*ipo));
433 init_ipo(ipo);
434 ipo->ddir = rw;
435 if (rw == DDIR_WAIT) {
436 ipo->delay = offset;
437 } else {
438 ipo->offset = offset;
439 ipo->len = bytes;
42793d94 440 if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw])
ac9b9101
JA
441 td->o.max_bs[rw] = bytes;
442 ipo->fileno = fileno;
443 ipo->file_action = file_action;
70afff59 444 td->o.size += bytes;
ac9b9101 445 }
3c3ed070 446
ac9b9101
JA
447 queue_io_piece(td, ipo);
448 }
449
450 free(str);
451 free(act);
452 free(fname);
453
454 if (writes && read_only) {
455 log_err("fio: <%s> skips replay of %d writes due to"
456 " read-only\n", td->o.name, writes);
457 writes = 0;
458 }
459
460 if (!reads && !writes && !waits)
461 return 1;
462 else if (reads && !writes)
463 td->o.td_ddir = TD_DDIR_READ;
464 else if (!reads && writes)
465 td->o.td_ddir = TD_DDIR_WRITE;
466 else
467 td->o.td_ddir = TD_DDIR_RW;
468
469 return 0;
470}
471
472/*
473 * open iolog, check version, and call appropriate parser
474 */
475static int init_iolog_read(struct thread_data *td)
476{
477 char buffer[256], *p;
478 FILE *f;
479 int ret;
480
481 f = fopen(td->o.read_iolog_file, "r");
482 if (!f) {
483 perror("fopen read iolog");
484 return 1;
485 }
486
487 p = fgets(buffer, sizeof(buffer), f);
488 if (!p) {
489 td_verror(td, errno, "iolog read");
490 log_err("fio: unable to read iolog\n");
491 fclose(f);
492 return 1;
493 }
494
495 /*
496 * version 2 of the iolog stores a specific string as the
497 * first line, check for that
498 */
499 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
500 ret = read_iolog2(td, f);
501 else {
502 log_err("fio: iolog version 1 is no longer supported\n");
503 ret = 1;
504 }
505
506 fclose(f);
507 return ret;
508}
509
510/*
511 * Set up a log for storing io patterns.
512 */
513static int init_iolog_write(struct thread_data *td)
514{
515 struct fio_file *ff;
516 FILE *f;
517 unsigned int i;
518
519 f = fopen(td->o.write_iolog_file, "a");
520 if (!f) {
521 perror("fopen write iolog");
522 return 1;
523 }
524
525 /*
526 * That's it for writing, setup a log buffer and we're done.
527 */
528 td->iolog_f = f;
529 td->iolog_buf = malloc(8192);
530 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
531
532 /*
533 * write our version line
534 */
535 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
536 perror("iolog init\n");
537 return 1;
538 }
539
540 /*
541 * add all known files
542 */
543 for_each_file(td, ff, i)
544 log_file(td, ff, FIO_LOG_ADD_FILE);
545
546 return 0;
547}
548
549int init_iolog(struct thread_data *td)
550{
551 int ret = 0;
552
553 if (td->o.read_iolog_file) {
d95b34a6
JA
554 int need_swap;
555
ac9b9101
JA
556 /*
557 * Check if it's a blktrace file and load that if possible.
558 * Otherwise assume it's a normal log file and load that.
559 */
d95b34a6
JA
560 if (is_blktrace(td->o.read_iolog_file, &need_swap))
561 ret = load_blktrace(td, td->o.read_iolog_file, need_swap);
ac9b9101
JA
562 else
563 ret = init_iolog_read(td);
564 } else if (td->o.write_iolog_file)
565 ret = init_iolog_write(td);
566
f01b34ae
JA
567 if (ret)
568 td_verror(td, EINVAL, "failed initializing iolog");
569
ac9b9101
JA
570 return ret;
571}
572
aee2ab67
JA
573void setup_log(struct io_log **log, struct log_params *p,
574 const char *filename)
ac9b9101 575{
e75cc8f3 576 struct io_log *l;
ac9b9101 577
a47591e4 578 l = smalloc(sizeof(*l));
ac9b9101 579 l->nr_samples = 0;
aee2ab67
JA
580 l->log_type = p->log_type;
581 l->log_offset = p->log_offset;
582 l->log_gz = p->log_gz;
b26317c9 583 l->log_gz_store = p->log_gz_store;
aee2ab67 584 l->avg_msec = p->avg_msec;
cb7e0ace 585 l->filename = strdup(filename);
aee2ab67
JA
586 l->td = p->td;
587
b26317c9 588 if (l->log_offset)
49e98daa 589 l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT;
b26317c9 590
aee2ab67
JA
591 INIT_FLIST_HEAD(&l->chunk_list);
592
593 if (l->log_gz && !p->td)
594 l->log_gz = 0;
19417eda 595 else if (l->log_gz || l->log_gz_store) {
aee2ab67
JA
596 pthread_mutex_init(&l->chunk_lock, NULL);
597 p->td->flags |= TD_F_COMPRESS_LOG;
598 }
599
ac9b9101
JA
600 *log = l;
601}
602
2e802282
JA
603#ifdef CONFIG_SETVBUF
604static void *set_file_buffer(FILE *f)
605{
606 size_t size = 1048576;
607 void *buf;
608
609 buf = malloc(size);
610 setvbuf(f, buf, _IOFBF, size);
611 return buf;
612}
613
614static void clear_file_buffer(void *buf)
615{
616 free(buf);
617}
618#else
619static void *set_file_buffer(FILE *f)
620{
621 return NULL;
622}
623
624static void clear_file_buffer(void *buf)
625{
626}
627#endif
628
518dac09 629void free_log(struct io_log *log)
cb7e0ace
JA
630{
631 free(log->log);
632 free(log->filename);
a47591e4 633 sfree(log);
cb7e0ace
JA
634}
635
bead01d7 636void flush_samples(FILE *f, void *samples, uint64_t sample_size)
ac9b9101 637{
b26317c9
JA
638 struct io_sample *s;
639 int log_offset;
640 uint64_t i, nr_samples;
641
642 if (!sample_size)
643 return;
644
645 s = __get_sample(samples, 0, 0);
49e98daa 646 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
b26317c9
JA
647
648 nr_samples = sample_size / __log_entry_sz(log_offset);
ac9b9101 649
aee2ab67 650 for (i = 0; i < nr_samples; i++) {
b26317c9 651 s = __get_sample(samples, log_offset, i);
ac9b9101 652
aee2ab67 653 if (!log_offset) {
ae588852
JA
654 fprintf(f, "%lu, %lu, %u, %u\n",
655 (unsigned long) s->time,
656 (unsigned long) s->val,
b26317c9 657 io_sample_ddir(s), s->bs);
ae588852
JA
658 } else {
659 struct io_sample_offset *so = (void *) s;
660
661 fprintf(f, "%lu, %lu, %u, %u, %llu\n",
662 (unsigned long) s->time,
663 (unsigned long) s->val,
b26317c9 664 io_sample_ddir(s), s->bs,
ae588852
JA
665 (unsigned long long) so->offset);
666 }
ac9b9101 667 }
aee2ab67
JA
668}
669
670#ifdef CONFIG_ZLIB
97eabb2a
JA
671
672struct iolog_flush_data {
155f2f02 673 struct workqueue_work work;
97eabb2a
JA
674 struct io_log *log;
675 void *samples;
676 uint64_t nr_samples;
677};
678
97eabb2a
JA
679#define GZ_CHUNK 131072
680
681static struct iolog_compress *get_new_chunk(unsigned int seq)
682{
683 struct iolog_compress *c;
684
685 c = malloc(sizeof(*c));
686 INIT_FLIST_HEAD(&c->list);
687 c->buf = malloc(GZ_CHUNK);
688 c->len = 0;
689 c->seq = seq;
97eabb2a
JA
690 return c;
691}
692
693static void free_chunk(struct iolog_compress *ic)
694{
c07826cd
JA
695 free(ic->buf);
696 free(ic);
97eabb2a
JA
697}
698
b26317c9 699static int z_stream_init(z_stream *stream, int gz_hdr)
aee2ab67 700{
b26317c9
JA
701 int wbits = 15;
702
aee2ab67
JA
703 stream->zalloc = Z_NULL;
704 stream->zfree = Z_NULL;
705 stream->opaque = Z_NULL;
706 stream->next_in = Z_NULL;
707
1a8e7458
JA
708 /*
709 * zlib magic - add 32 for auto-detection of gz header or not,
710 * if we decide to store files in a gzip friendly format.
711 */
b26317c9
JA
712 if (gz_hdr)
713 wbits += 32;
714
715 if (inflateInit2(stream, wbits) != Z_OK)
aee2ab67
JA
716 return 1;
717
718 return 0;
719}
720
1a8e7458 721struct inflate_chunk_iter {
aee2ab67 722 unsigned int seq;
dad95da1 723 int err;
aee2ab67
JA
724 void *buf;
725 size_t buf_size;
726 size_t buf_used;
727 size_t chunk_sz;
728};
729
b26317c9 730static void finish_chunk(z_stream *stream, FILE *f,
1a8e7458 731 struct inflate_chunk_iter *iter)
aee2ab67 732{
aee2ab67
JA
733 int ret;
734
735 ret = inflateEnd(stream);
736 if (ret != Z_OK)
737 log_err("fio: failed to end log inflation (%d)\n", ret);
738
b26317c9 739 flush_samples(f, iter->buf, iter->buf_used);
aee2ab67
JA
740 free(iter->buf);
741 iter->buf = NULL;
742 iter->buf_size = iter->buf_used = 0;
743}
744
1a8e7458
JA
745/*
746 * Iterative chunk inflation. Handles cases where we cross into a new
747 * sequence, doing flush finish of previous chunk if needed.
748 */
749static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f,
750 z_stream *stream, struct inflate_chunk_iter *iter)
aee2ab67 751{
0c56718d
JA
752 size_t ret;
753
754 dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u",
755 (unsigned long) ic->len, ic->seq);
756
aee2ab67
JA
757 if (ic->seq != iter->seq) {
758 if (iter->seq)
b26317c9 759 finish_chunk(stream, f, iter);
aee2ab67 760
b26317c9 761 z_stream_init(stream, gz_hdr);
aee2ab67
JA
762 iter->seq = ic->seq;
763 }
764
765 stream->avail_in = ic->len;
766 stream->next_in = ic->buf;
767
768 if (!iter->buf_size) {
769 iter->buf_size = iter->chunk_sz;
770 iter->buf = malloc(iter->buf_size);
771 }
772
773 while (stream->avail_in) {
b26317c9 774 size_t this_out = iter->buf_size - iter->buf_used;
aee2ab67
JA
775 int err;
776
b26317c9 777 stream->avail_out = this_out;
aee2ab67
JA
778 stream->next_out = iter->buf + iter->buf_used;
779
780 err = inflate(stream, Z_NO_FLUSH);
781 if (err < 0) {
782 log_err("fio: failed inflating log: %d\n", err);
dad95da1 783 iter->err = err;
aee2ab67
JA
784 break;
785 }
786
b26317c9
JA
787 iter->buf_used += this_out - stream->avail_out;
788
789 if (!stream->avail_out) {
790 iter->buf_size += iter->chunk_sz;
791 iter->buf = realloc(iter->buf, iter->buf_size);
792 continue;
793 }
794
795 if (err == Z_STREAM_END)
796 break;
aee2ab67
JA
797 }
798
0c56718d
JA
799 ret = (void *) stream->next_in - ic->buf;
800
801 dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) ret);
802
803 return ret;
aee2ab67
JA
804}
805
1a8e7458
JA
806/*
807 * Inflate stored compressed chunks, or write them directly to the log
808 * file if so instructed.
809 */
dad95da1 810static int inflate_gz_chunks(struct io_log *log, FILE *f)
aee2ab67 811{
1a8e7458 812 struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, };
aee2ab67
JA
813 z_stream stream;
814
815 while (!flist_empty(&log->chunk_list)) {
816 struct iolog_compress *ic;
817
9342d5f8 818 ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list);
aee2ab67 819 flist_del(&ic->list);
b26317c9 820
1a8e7458
JA
821 if (log->log_gz_store) {
822 size_t ret;
823
0c56718d
JA
824 dprint(FD_COMPRESS, "log write chunk size=%lu, "
825 "seq=%u\n", (unsigned long) ic->len, ic->seq);
826
1a8e7458 827 ret = fwrite(ic->buf, ic->len, 1, f);
dad95da1
JA
828 if (ret != 1 || ferror(f)) {
829 iter.err = errno;
1a8e7458 830 log_err("fio: error writing compressed log\n");
dad95da1 831 }
1a8e7458
JA
832 } else
833 inflate_chunk(ic, log->log_gz_store, f, &stream, &iter);
c07826cd
JA
834
835 free_chunk(ic);
aee2ab67
JA
836 }
837
838 if (iter.seq) {
b26317c9 839 finish_chunk(&stream, f, &iter);
aee2ab67
JA
840 free(iter.buf);
841 }
dad95da1
JA
842
843 return iter.err;
aee2ab67
JA
844}
845
1a8e7458
JA
846/*
847 * Open compressed log file and decompress the stored chunks and
848 * write them to stdout. The chunks are stored sequentially in the
849 * file, so we iterate over them and do them one-by-one.
850 */
b26317c9
JA
851int iolog_file_inflate(const char *file)
852{
1a8e7458 853 struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, };
b26317c9
JA
854 struct iolog_compress ic;
855 z_stream stream;
856 struct stat sb;
fd771971 857 ssize_t ret;
f302710c
JA
858 size_t total;
859 void *buf;
b26317c9
JA
860 FILE *f;
861
b26317c9
JA
862 f = fopen(file, "r");
863 if (!f) {
864 perror("fopen");
865 return 1;
866 }
867
fd771971
JA
868 if (stat(file, &sb) < 0) {
869 fclose(f);
870 perror("stat");
871 return 1;
872 }
873
f302710c 874 ic.buf = buf = malloc(sb.st_size);
b26317c9 875 ic.len = sb.st_size;
b26317c9
JA
876 ic.seq = 1;
877
878 ret = fread(ic.buf, ic.len, 1, f);
879 if (ret < 0) {
880 perror("fread");
881 fclose(f);
bb1116fe 882 free(buf);
b26317c9
JA
883 return 1;
884 } else if (ret != 1) {
885 log_err("fio: short read on reading log\n");
886 fclose(f);
bb1116fe 887 free(buf);
b26317c9
JA
888 return 1;
889 }
890
891 fclose(f);
892
1a8e7458
JA
893 /*
894 * Each chunk will return Z_STREAM_END. We don't know how many
895 * chunks are in the file, so we just keep looping and incrementing
896 * the sequence number until we have consumed the whole compressed
897 * file.
898 */
f302710c
JA
899 total = ic.len;
900 do {
8a1db9a1 901 size_t iret;
f302710c 902
8a1db9a1
JA
903 iret = inflate_chunk(&ic, 1, stdout, &stream, &iter);
904 total -= iret;
f302710c
JA
905 if (!total)
906 break;
dad95da1
JA
907 if (iter.err)
908 break;
f302710c
JA
909
910 ic.seq++;
8a1db9a1
JA
911 ic.len -= iret;
912 ic.buf += iret;
f302710c 913 } while (1);
b26317c9
JA
914
915 if (iter.seq) {
916 finish_chunk(&stream, stdout, &iter);
917 free(iter.buf);
918 }
919
f302710c 920 free(buf);
dad95da1 921 return iter.err;
b26317c9
JA
922}
923
aee2ab67
JA
924#else
925
dad95da1 926static int inflate_gz_chunks(struct io_log *log, FILE *f)
aee2ab67 927{
b7364e21 928 return 0;
aee2ab67
JA
929}
930
a68c66b2
JA
931int iolog_file_inflate(const char *file)
932{
933 log_err("fio: log inflation not possible without zlib\n");
934 return 1;
935}
936
aee2ab67
JA
937#endif
938
3a5db920 939void flush_log(struct io_log *log, int do_append)
aee2ab67
JA
940{
941 void *buf;
942 FILE *f;
943
3a5db920
JA
944 if (!do_append)
945 f = fopen(log->filename, "w");
946 else
947 f = fopen(log->filename, "a");
aee2ab67
JA
948 if (!f) {
949 perror("fopen log");
950 return;
951 }
952
953 buf = set_file_buffer(f);
954
1a8e7458 955 inflate_gz_chunks(log, f);
aee2ab67 956
b26317c9 957 flush_samples(f, log->log, log->nr_samples * log_entry_sz(log));
ac9b9101
JA
958
959 fclose(f);
2e802282 960 clear_file_buffer(buf);
ac9b9101
JA
961}
962
cb7e0ace 963static int finish_log(struct thread_data *td, struct io_log *log, int trylock)
ac9b9101 964{
155f2f02 965 if (td->flags & TD_F_COMPRESS_LOG)
aee2ab67
JA
966 iolog_flush(log, 1);
967
243bfe19 968 if (trylock) {
cb7e0ace 969 if (fio_trylock_file(log->filename))
243bfe19
JA
970 return 1;
971 } else
cb7e0ace 972 fio_lock_file(log->filename);
243bfe19 973
0cba0f91 974 if (td->client_type == FIO_CLIENT_TYPE_GUI || is_backend)
cb7e0ace 975 fio_send_iolog(td, log, log->filename);
aee2ab67 976 else
3a5db920 977 flush_log(log, !td->o.per_job_logs);
243bfe19 978
cb7e0ace 979 fio_unlock_file(log->filename);
518dac09 980 free_log(log);
243bfe19 981 return 0;
ac9b9101
JA
982}
983
0cba0f91
JA
984size_t log_chunk_sizes(struct io_log *log)
985{
986 struct flist_head *entry;
987 size_t ret;
988
989 if (flist_empty(&log->chunk_list))
990 return 0;
991
992 ret = 0;
993 pthread_mutex_lock(&log->chunk_lock);
994 flist_for_each(entry, &log->chunk_list) {
995 struct iolog_compress *c;
996
997 c = flist_entry(entry, struct iolog_compress, list);
998 ret += c->len;
999 }
1000 pthread_mutex_unlock(&log->chunk_lock);
1001 return ret;
1002}
1003
aee2ab67
JA
1004#ifdef CONFIG_ZLIB
1005
78e93aa6 1006static int gz_work(struct iolog_flush_data *data)
c143980a 1007{
aee2ab67
JA
1008 struct iolog_compress *c;
1009 struct flist_head list;
1010 unsigned int seq;
1011 z_stream stream;
1012 size_t total = 0;
d73ac887 1013 int ret;
aee2ab67
JA
1014
1015 INIT_FLIST_HEAD(&list);
1016
aee2ab67
JA
1017 stream.zalloc = Z_NULL;
1018 stream.zfree = Z_NULL;
1019 stream.opaque = Z_NULL;
1020
b26317c9 1021 ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
b26317c9 1022 if (ret != Z_OK) {
aee2ab67 1023 log_err("fio: failed to init gz stream\n");
152ea035 1024 goto err;
aee2ab67
JA
1025 }
1026
1027 seq = ++data->log->chunk_seq;
b26317c9 1028
aee2ab67
JA
1029 stream.next_in = (void *) data->samples;
1030 stream.avail_in = data->nr_samples * log_entry_sz(data->log);
1031
0c56718d
JA
1032 dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u\n",
1033 (unsigned long) stream.avail_in, seq);
aee2ab67
JA
1034 do {
1035 c = get_new_chunk(seq);
1036 stream.avail_out = GZ_CHUNK;
1037 stream.next_out = c->buf;
1038 ret = deflate(&stream, Z_NO_FLUSH);
1039 if (ret < 0) {
1040 log_err("fio: deflate log (%d)\n", ret);
dad95da1
JA
1041 free_chunk(c);
1042 goto err;
aee2ab67
JA
1043 }
1044
1045 c->len = GZ_CHUNK - stream.avail_out;
1046 flist_add_tail(&c->list, &list);
1047 total += c->len;
1048 } while (stream.avail_in);
1049
1050 stream.next_out = c->buf + c->len;
1051 stream.avail_out = GZ_CHUNK - c->len;
1052
1053 ret = deflate(&stream, Z_FINISH);
1054 if (ret == Z_STREAM_END)
1055 c->len = GZ_CHUNK - stream.avail_out;
1056 else {
1057 do {
1058 c = get_new_chunk(seq);
1059 stream.avail_out = GZ_CHUNK;
1060 stream.next_out = c->buf;
1061 ret = deflate(&stream, Z_FINISH);
1062 c->len = GZ_CHUNK - stream.avail_out;
0c56718d 1063 total += c->len;
aee2ab67
JA
1064 flist_add_tail(&c->list, &list);
1065 } while (ret != Z_STREAM_END);
1066 }
1067
0c56718d
JA
1068 dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total);
1069
aee2ab67
JA
1070 ret = deflateEnd(&stream);
1071 if (ret != Z_OK)
1072 log_err("fio: deflateEnd %d\n", ret);
1073
1074 free(data->samples);
1075
1076 if (!flist_empty(&list)) {
1077 pthread_mutex_lock(&data->log->chunk_lock);
1078 flist_splice_tail(&list, &data->log->chunk_list);
1079 pthread_mutex_unlock(&data->log->chunk_lock);
1080 }
1081
dad95da1
JA
1082 ret = 0;
1083done:
78e93aa6 1084 free(data);
dad95da1
JA
1085 return ret;
1086err:
1087 while (!flist_empty(&list)) {
1088 c = flist_first_entry(list.next, struct iolog_compress, list);
1089 flist_del(&c->list);
1090 free_chunk(c);
1091 }
1092 ret = 1;
1093 goto done;
aee2ab67
JA
1094}
1095
78e93aa6
JA
1096/*
1097 * Invoked from our compress helper thread, when logging would have exceeded
1098 * the specified memory limitation. Compresses the previously stored
1099 * entries.
1100 */
1101static int gz_work_async(struct submit_worker *sw, struct workqueue_work *work)
1102{
1103 return gz_work(container_of(work, struct iolog_flush_data, work));
1104}
1105
c08f9fe2
JA
1106static int gz_init_worker(struct submit_worker *sw)
1107{
1108 struct thread_data *td = sw->wq->td;
1109
1110 if (!fio_option_is_set(&td->o, log_gz_cpumask))
1111 return 0;
1112
1113 if (fio_setaffinity(gettid(), td->o.log_gz_cpumask) == -1) {
1114 log_err("gz: failed to set CPU affinity\n");
1115 return 1;
1116 }
1117
1118 return 0;
1119}
1120
155f2f02 1121static struct workqueue_ops log_compress_wq_ops = {
78e93aa6 1122 .fn = gz_work_async,
c08f9fe2 1123 .init_worker_fn = gz_init_worker,
24660963 1124 .nice = 1,
155f2f02
JA
1125};
1126
24660963 1127int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out)
155f2f02
JA
1128{
1129 if (!(td->flags & TD_F_COMPRESS_LOG))
1130 return 0;
1131
24660963 1132 workqueue_init(td, &td->log_compress_wq, &log_compress_wq_ops, 1, sk_out);
155f2f02
JA
1133 return 0;
1134}
1135
1136void iolog_compress_exit(struct thread_data *td)
1137{
1138 if (!(td->flags & TD_F_COMPRESS_LOG))
1139 return;
1140
1141 workqueue_exit(&td->log_compress_wq);
1142}
1143
1a8e7458 1144/*
949ae6dc
JA
1145 * Queue work item to compress the existing log entries. We reset the
1146 * current log to a small size, and reference the existing log in the
1147 * data that we queue for compression. Once compression has been done,
1148 * this old log is freed. If called with wait == 1, will not return until
1149 * the log compression has completed.
1a8e7458 1150 */
aee2ab67
JA
1151int iolog_flush(struct io_log *log, int wait)
1152{
aee2ab67 1153 struct iolog_flush_data *data;
aee2ab67 1154
f77d4b73
JA
1155 io_u_quiesce(log->td);
1156
aee2ab67
JA
1157 data = malloc(sizeof(*data));
1158 if (!data)
1159 return 1;
1160
1161 data->log = log;
1162
949ae6dc 1163 data->samples = log->log;
aee2ab67 1164 data->nr_samples = log->nr_samples;
949ae6dc 1165
aee2ab67 1166 log->nr_samples = 0;
6d37f67f 1167 log->max_samples = DEF_LOG_ENTRIES;
949ae6dc 1168 log->log = malloc(log->max_samples * log_entry_sz(log));
aee2ab67 1169
78e93aa6
JA
1170 if (!wait)
1171 workqueue_enqueue(&log->td->log_compress_wq, &data->work);
1172 else
1173 gz_work(data);
aee2ab67
JA
1174
1175 return 0;
1176}
1177
1178#else
1179
1180int iolog_flush(struct io_log *log, int wait)
1181{
1182 return 1;
1183}
1184
24660963 1185int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out)
52618a2a
JA
1186{
1187 return 0;
1188}
1189
1190void iolog_compress_exit(struct thread_data *td)
1191{
1192}
1193
aee2ab67
JA
1194#endif
1195
1c725e70 1196static int __write_log(struct thread_data *td, struct io_log *log, int try)
905e3d4f 1197{
1c725e70
JA
1198 if (log)
1199 return finish_log(td, log, try);
905e3d4f 1200
1c725e70
JA
1201 return 0;
1202}
905e3d4f 1203
a47591e4 1204static int write_iops_log(struct thread_data *td, int try, bool unit_log)
1c725e70 1205{
a47591e4
JA
1206 int ret;
1207
1208 if (per_unit_log(td->iops_log) != unit_log)
1209 return 0;
1210
1211 ret = __write_log(td, td->iops_log, try);
1212 if (!ret)
1213 td->iops_log = NULL;
1214
1215 return ret;
905e3d4f
JA
1216}
1217
a47591e4 1218static int write_slat_log(struct thread_data *td, int try, bool unit_log)
905e3d4f 1219{
a47591e4
JA
1220 int ret;
1221
1222 if (!unit_log)
1223 return 0;
1224
1225 ret = __write_log(td, td->slat_log, try);
1226 if (!ret)
1227 td->slat_log = NULL;
1228
1229 return ret;
905e3d4f
JA
1230}
1231
a47591e4 1232static int write_clat_log(struct thread_data *td, int try, bool unit_log)
905e3d4f 1233{
a47591e4
JA
1234 int ret;
1235
1236 if (!unit_log)
1237 return 0;
1238
1239 ret = __write_log(td, td->clat_log, try);
1240 if (!ret)
1241 td->clat_log = NULL;
1242
1243 return ret;
905e3d4f
JA
1244}
1245
a47591e4 1246static int write_lat_log(struct thread_data *td, int try, bool unit_log)
905e3d4f 1247{
a47591e4
JA
1248 int ret;
1249
1250 if (!unit_log)
1251 return 0;
1252
1253 ret = __write_log(td, td->lat_log, try);
1254 if (!ret)
1255 td->lat_log = NULL;
1256
1257 return ret;
905e3d4f
JA
1258}
1259
a47591e4 1260static int write_bandw_log(struct thread_data *td, int try, bool unit_log)
905e3d4f 1261{
a47591e4
JA
1262 int ret;
1263
1264 if (per_unit_log(td->bw_log) != unit_log)
1265 return 0;
1266
1267 ret = __write_log(td, td->bw_log, try);
1268 if (!ret)
1269 td->bw_log = NULL;
1270
1271 return ret;
905e3d4f
JA
1272}
1273
1274enum {
1275 BW_LOG_MASK = 1,
1276 LAT_LOG_MASK = 2,
1277 SLAT_LOG_MASK = 4,
1278 CLAT_LOG_MASK = 8,
1279 IOPS_LOG_MASK = 16,
1280
905e3d4f
JA
1281 ALL_LOG_NR = 5,
1282};
1283
1284struct log_type {
1285 unsigned int mask;
a47591e4 1286 int (*fn)(struct thread_data *, int, bool);
905e3d4f
JA
1287};
1288
1289static struct log_type log_types[] = {
1290 {
1291 .mask = BW_LOG_MASK,
1292 .fn = write_bandw_log,
1293 },
1294 {
1295 .mask = LAT_LOG_MASK,
1296 .fn = write_lat_log,
1297 },
1298 {
1299 .mask = SLAT_LOG_MASK,
1300 .fn = write_slat_log,
1301 },
1302 {
1303 .mask = CLAT_LOG_MASK,
1304 .fn = write_clat_log,
1305 },
1306 {
1307 .mask = IOPS_LOG_MASK,
1308 .fn = write_iops_log,
1309 },
1310};
1311
a47591e4 1312void td_writeout_logs(struct thread_data *td, bool unit_logs)
905e3d4f 1313{
ea5409f9 1314 unsigned int log_mask = 0;
905e3d4f
JA
1315 unsigned int log_left = ALL_LOG_NR;
1316 int old_state, i;
1317
1318 old_state = td_bump_runstate(td, TD_FINISHING);
1319
a47591e4 1320 finalize_logs(td, unit_logs);
905e3d4f
JA
1321
1322 while (log_left) {
1323 int prev_log_left = log_left;
1324
1325 for (i = 0; i < ALL_LOG_NR && log_left; i++) {
1326 struct log_type *lt = &log_types[i];
1327 int ret;
1328
ea5409f9 1329 if (!(log_mask & lt->mask)) {
a47591e4 1330 ret = lt->fn(td, log_left != 1, unit_logs);
905e3d4f
JA
1331 if (!ret) {
1332 log_left--;
ea5409f9 1333 log_mask |= lt->mask;
905e3d4f
JA
1334 }
1335 }
1336 }
1337
1338 if (prev_log_left == log_left)
1339 usleep(5000);
1340 }
1341
1342 td_restore_runstate(td, old_state);
1343}
a47591e4
JA
1344
1345void fio_writeout_logs(bool unit_logs)
1346{
1347 struct thread_data *td;
1348 int i;
1349
1350 for_each_td(td, i)
1351 td_writeout_logs(td, unit_logs);
1352}