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