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