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