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