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