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