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