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