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