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