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