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