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