blob: f9e835d4b087aa17755d41f76a59d6cf0bb95c7e [file] [log] [blame]
Jens Axboeac9b9102011-10-01 15:01:08 -04001/*
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>
7#include <libgen.h>
8#include <assert.h>
Jens Axboebac4af12014-07-03 13:42:28 -06009#include <sys/types.h>
10#include <sys/stat.h>
11#include <unistd.h>
Jens Axboe38a812d2014-07-03 09:10:39 -060012#ifdef CONFIG_ZLIB
13#include <zlib.h>
14#endif
15
Jens Axboeac9b9102011-10-01 15:01:08 -040016#include "flist.h"
17#include "fio.h"
18#include "verify.h"
19#include "trim.h"
Jens Axboe243bfe12014-04-02 15:46:58 -060020#include "filelock.h"
Jens Axboe65f93ca2014-07-03 14:16:25 -060021#include "lib/tp.h"
Jens Axboeac9b9102011-10-01 15:01:08 -040022
23static const char iolog_ver2[] = "fio version 2 iolog";
24
25void queue_io_piece(struct thread_data *td, struct io_piece *ipo)
26{
27 flist_add_tail(&ipo->list, &td->io_log_list);
28 td->total_io_size += ipo->len;
29}
30
31void log_io_u(struct thread_data *td, struct io_u *io_u)
32{
33 const char *act[] = { "read", "write", "sync", "datasync",
34 "sync_file_range", "wait", "trim" };
35
36 assert(io_u->ddir <= 6);
37
38 if (!td->o.write_iolog_file)
39 return;
40
41 fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name,
42 act[io_u->ddir], io_u->offset,
43 io_u->buflen);
44}
45
46void log_file(struct thread_data *td, struct fio_file *f,
47 enum file_log_act what)
48{
49 const char *act[] = { "add", "open", "close" };
50
51 assert(what < 3);
52
53 if (!td->o.write_iolog_file)
54 return;
55
56
57 /*
58 * this happens on the pre-open/close done before the job starts
59 */
60 if (!td->iolog_f)
61 return;
62
63 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
64}
65
66static void iolog_delay(struct thread_data *td, unsigned long delay)
67{
68 unsigned long usec = utime_since_now(&td->last_issue);
Jens Axboe30b18672014-04-11 00:03:32 +020069 unsigned long this_delay;
Jens Axboeac9b9102011-10-01 15:01:08 -040070
71 if (delay < usec)
72 return;
73
74 delay -= usec;
75
Jens Axboe30b18672014-04-11 00:03:32 +020076 while (delay && !td->terminate) {
77 this_delay = delay;
78 if (this_delay > 500000)
79 this_delay = 500000;
80
81 usec_sleep(td, this_delay);
82 delay -= this_delay;
83 }
Jens Axboeac9b9102011-10-01 15:01:08 -040084}
85
86static int ipo_special(struct thread_data *td, struct io_piece *ipo)
87{
88 struct fio_file *f;
89 int ret;
90
91 /*
92 * Not a special ipo
93 */
94 if (ipo->ddir != DDIR_INVAL)
95 return 0;
96
97 f = td->files[ipo->fileno];
98
99 switch (ipo->file_action) {
100 case FIO_LOG_OPEN_FILE:
101 ret = td_io_open_file(td, f);
102 if (!ret)
103 break;
104 td_verror(td, ret, "iolog open file");
105 return -1;
106 case FIO_LOG_CLOSE_FILE:
107 td_io_close_file(td, f);
108 break;
109 case FIO_LOG_UNLINK_FILE:
Castor Fu9187a262014-08-19 09:28:53 -0700110 td_io_unlink_file(td, f);
Jens Axboeac9b9102011-10-01 15:01:08 -0400111 break;
112 default:
113 log_err("fio: bad file action %d\n", ipo->file_action);
114 break;
115 }
116
117 return 1;
118}
119
120int read_iolog_get(struct thread_data *td, struct io_u *io_u)
121{
122 struct io_piece *ipo;
123 unsigned long elapsed;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200124
Jens Axboeac9b9102011-10-01 15:01:08 -0400125 while (!flist_empty(&td->io_log_list)) {
126 int ret;
127
Jens Axboe12dbd062014-07-03 21:19:57 -0600128 ipo = flist_first_entry(&td->io_log_list, struct io_piece, list);
Jens Axboeac9b9102011-10-01 15:01:08 -0400129 flist_del(&ipo->list);
130 remove_trim_entry(td, ipo);
131
132 ret = ipo_special(td, ipo);
133 if (ret < 0) {
134 free(ipo);
135 break;
136 } else if (ret > 0) {
137 free(ipo);
138 continue;
139 }
140
141 io_u->ddir = ipo->ddir;
142 if (ipo->ddir != DDIR_WAIT) {
143 io_u->offset = ipo->offset;
144 io_u->buflen = ipo->len;
145 io_u->file = td->files[ipo->fileno];
146 get_file(io_u->file);
147 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
148 io_u->buflen, io_u->file->file_name);
149 if (ipo->delay)
150 iolog_delay(td, ipo->delay);
151 } else {
152 elapsed = mtime_since_genesis();
153 if (ipo->delay > elapsed)
154 usec_sleep(td, (ipo->delay - elapsed) * 1000);
Jens Axboeac9b9102011-10-01 15:01:08 -0400155 }
156
157 free(ipo);
Jens Axboe3c3ed072012-03-27 09:12:39 +0200158
Jens Axboeac9b9102011-10-01 15:01:08 -0400159 if (io_u->ddir != DDIR_WAIT)
160 return 0;
161 }
162
163 td->done = 1;
164 return 1;
165}
166
167void prune_io_piece_log(struct thread_data *td)
168{
169 struct io_piece *ipo;
170 struct rb_node *n;
171
172 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
173 ipo = rb_entry(n, struct io_piece, rb_node);
174 rb_erase(n, &td->io_hist_tree);
175 remove_trim_entry(td, ipo);
176 td->io_hist_len--;
177 free(ipo);
178 }
179
180 while (!flist_empty(&td->io_hist_list)) {
Jens Axboe12dbd062014-07-03 21:19:57 -0600181 ipo = flist_entry(&td->io_hist_list, struct io_piece, list);
Jens Axboeac9b9102011-10-01 15:01:08 -0400182 flist_del(&ipo->list);
183 remove_trim_entry(td, ipo);
184 td->io_hist_len--;
185 free(ipo);
186 }
187}
188
189/*
190 * log a successful write, so we can unwind the log for verify
191 */
192void log_io_piece(struct thread_data *td, struct io_u *io_u)
193{
194 struct rb_node **p, *parent;
195 struct io_piece *ipo, *__ipo;
196
197 ipo = malloc(sizeof(struct io_piece));
198 init_ipo(ipo);
199 ipo->file = io_u->file;
200 ipo->offset = io_u->offset;
201 ipo->len = io_u->buflen;
Juan Casseda0a7bd2013-09-17 14:06:12 -0700202 ipo->numberio = io_u->numberio;
Jens Axboef9401282014-02-06 12:17:37 -0700203 ipo->flags = IP_F_IN_FLIGHT;
204
205 io_u->ipo = ipo;
Jens Axboeac9b9102011-10-01 15:01:08 -0400206
207 if (io_u_should_trim(td, io_u)) {
208 flist_add_tail(&ipo->trim_list, &td->trim_list);
209 td->trim_entries++;
210 }
211
212 /*
213 * We don't need to sort the entries, if:
214 *
215 * Sequential writes, or
216 * Random writes that lay out the file as it goes along
217 *
218 * For both these cases, just reading back data in the order we
219 * wrote it out is the fastest.
220 *
221 * One exception is if we don't have a random map AND we are doing
222 * verifies, in that case we need to check for duplicate blocks and
223 * drop the old one, which we rely on the rb insert/lookup for
224 * handling.
225 */
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -0700226 if (((!td->o.verifysort) || !td_random(td) || !td->o.overwrite) &&
Jens Axboeac9b9102011-10-01 15:01:08 -0400227 (file_randommap(td, ipo->file) || td->o.verify == VERIFY_NONE)) {
228 INIT_FLIST_HEAD(&ipo->list);
229 flist_add_tail(&ipo->list, &td->io_hist_list);
230 ipo->flags |= IP_F_ONLIST;
231 td->io_hist_len++;
232 return;
233 }
234
235 RB_CLEAR_NODE(&ipo->rb_node);
236
237 /*
238 * Sort the entry into the verification list
239 */
240restart:
241 p = &td->io_hist_tree.rb_node;
242 parent = NULL;
243 while (*p) {
244 parent = *p;
245
246 __ipo = rb_entry(parent, struct io_piece, rb_node);
247 if (ipo->file < __ipo->file)
248 p = &(*p)->rb_left;
249 else if (ipo->file > __ipo->file)
250 p = &(*p)->rb_right;
251 else if (ipo->offset < __ipo->offset)
252 p = &(*p)->rb_left;
253 else if (ipo->offset > __ipo->offset)
254 p = &(*p)->rb_right;
255 else {
Jens Axboe885ac622012-04-04 14:11:58 -0600256 dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu",
257 __ipo->offset, __ipo->len,
258 ipo->offset, ipo->len);
Jens Axboeac9b9102011-10-01 15:01:08 -0400259 td->io_hist_len--;
260 rb_erase(parent, &td->io_hist_tree);
261 remove_trim_entry(td, __ipo);
262 free(__ipo);
263 goto restart;
264 }
265 }
266
267 rb_link_node(&ipo->rb_node, parent, p);
268 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
269 ipo->flags |= IP_F_ONRB;
270 td->io_hist_len++;
271}
272
Jens Axboe890b6652014-05-06 19:06:51 -0600273void unlog_io_piece(struct thread_data *td, struct io_u *io_u)
274{
275 struct io_piece *ipo = io_u->ipo;
276
277 if (!ipo)
278 return;
279
280 if (ipo->flags & IP_F_ONRB)
281 rb_erase(&ipo->rb_node, &td->io_hist_tree);
282 else if (ipo->flags & IP_F_ONLIST)
283 flist_del(&ipo->list);
284
285 free(ipo);
286 io_u->ipo = NULL;
287 td->io_hist_len--;
288}
289
290void trim_io_piece(struct thread_data *td, struct io_u *io_u)
291{
292 struct io_piece *ipo = io_u->ipo;
293
294 if (!ipo)
295 return;
296
297 ipo->len = io_u->xfer_buflen - io_u->resid;
298}
299
Jens Axboeac9b9102011-10-01 15:01:08 -0400300void write_iolog_close(struct thread_data *td)
301{
302 fflush(td->iolog_f);
303 fclose(td->iolog_f);
304 free(td->iolog_buf);
305 td->iolog_f = NULL;
306 td->iolog_buf = NULL;
307}
308
309/*
310 * Read version 2 iolog data. It is enhanced to include per-file logging,
311 * syncs, etc.
312 */
313static int read_iolog2(struct thread_data *td, FILE *f)
314{
315 unsigned long long offset;
316 unsigned int bytes;
317 int reads, writes, waits, fileno = 0, file_action = 0; /* stupid gcc */
318 char *fname, *act;
319 char *str, *p;
320 enum fio_ddir rw;
321
322 free_release_files(td);
323
324 /*
325 * Read in the read iolog and store it, reuse the infrastructure
326 * for doing verifications.
327 */
328 str = malloc(4096);
329 fname = malloc(256+16);
330 act = malloc(256+16);
331
332 reads = writes = waits = 0;
333 while ((p = fgets(str, 4096, f)) != NULL) {
334 struct io_piece *ipo;
335 int r;
336
337 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
338 &bytes);
339 if (r == 4) {
340 /*
341 * Check action first
342 */
343 if (!strcmp(act, "wait"))
344 rw = DDIR_WAIT;
345 else if (!strcmp(act, "read"))
346 rw = DDIR_READ;
347 else if (!strcmp(act, "write"))
348 rw = DDIR_WRITE;
349 else if (!strcmp(act, "sync"))
350 rw = DDIR_SYNC;
351 else if (!strcmp(act, "datasync"))
352 rw = DDIR_DATASYNC;
353 else if (!strcmp(act, "trim"))
354 rw = DDIR_TRIM;
355 else {
356 log_err("fio: bad iolog file action: %s\n",
357 act);
358 continue;
359 }
Nikolaus Jeremic033ace12013-02-18 19:44:41 +0100360 fileno = get_fileno(td, fname);
Jens Axboeac9b9102011-10-01 15:01:08 -0400361 } else if (r == 2) {
362 rw = DDIR_INVAL;
363 if (!strcmp(act, "add")) {
Jens Axboe5903e7b2014-02-26 13:42:13 -0800364 fileno = add_file(td, fname, 0, 1);
Jens Axboeac9b9102011-10-01 15:01:08 -0400365 file_action = FIO_LOG_ADD_FILE;
366 continue;
367 } else if (!strcmp(act, "open")) {
368 fileno = get_fileno(td, fname);
369 file_action = FIO_LOG_OPEN_FILE;
370 } else if (!strcmp(act, "close")) {
371 fileno = get_fileno(td, fname);
372 file_action = FIO_LOG_CLOSE_FILE;
373 } else {
374 log_err("fio: bad iolog file action: %s\n",
375 act);
376 continue;
377 }
378 } else {
379 log_err("bad iolog2: %s", p);
380 continue;
381 }
382
383 if (rw == DDIR_READ)
384 reads++;
385 else if (rw == DDIR_WRITE) {
386 /*
387 * Don't add a write for ro mode
388 */
389 if (read_only)
390 continue;
391 writes++;
392 } else if (rw == DDIR_WAIT) {
393 waits++;
394 } else if (rw == DDIR_INVAL) {
395 } else if (!ddir_sync(rw)) {
396 log_err("bad ddir: %d\n", rw);
397 continue;
398 }
399
400 /*
401 * Make note of file
402 */
403 ipo = malloc(sizeof(*ipo));
404 init_ipo(ipo);
405 ipo->ddir = rw;
406 if (rw == DDIR_WAIT) {
407 ipo->delay = offset;
408 } else {
409 ipo->offset = offset;
410 ipo->len = bytes;
Jens Axboe42793d92014-04-08 21:18:37 -0600411 if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw])
Jens Axboeac9b9102011-10-01 15:01:08 -0400412 td->o.max_bs[rw] = bytes;
413 ipo->fileno = fileno;
414 ipo->file_action = file_action;
Jens Axboeecf53692014-06-09 19:56:51 -0600415 td->o.size += bytes;
Jens Axboeac9b9102011-10-01 15:01:08 -0400416 }
Jens Axboe3c3ed072012-03-27 09:12:39 +0200417
Jens Axboeac9b9102011-10-01 15:01:08 -0400418 queue_io_piece(td, ipo);
419 }
420
421 free(str);
422 free(act);
423 free(fname);
424
425 if (writes && read_only) {
426 log_err("fio: <%s> skips replay of %d writes due to"
427 " read-only\n", td->o.name, writes);
428 writes = 0;
429 }
430
431 if (!reads && !writes && !waits)
432 return 1;
433 else if (reads && !writes)
434 td->o.td_ddir = TD_DDIR_READ;
435 else if (!reads && writes)
436 td->o.td_ddir = TD_DDIR_WRITE;
437 else
438 td->o.td_ddir = TD_DDIR_RW;
439
440 return 0;
441}
442
443/*
444 * open iolog, check version, and call appropriate parser
445 */
446static int init_iolog_read(struct thread_data *td)
447{
448 char buffer[256], *p;
449 FILE *f;
450 int ret;
451
452 f = fopen(td->o.read_iolog_file, "r");
453 if (!f) {
454 perror("fopen read iolog");
455 return 1;
456 }
457
458 p = fgets(buffer, sizeof(buffer), f);
459 if (!p) {
460 td_verror(td, errno, "iolog read");
461 log_err("fio: unable to read iolog\n");
462 fclose(f);
463 return 1;
464 }
465
466 /*
467 * version 2 of the iolog stores a specific string as the
468 * first line, check for that
469 */
470 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
471 ret = read_iolog2(td, f);
472 else {
473 log_err("fio: iolog version 1 is no longer supported\n");
474 ret = 1;
475 }
476
477 fclose(f);
478 return ret;
479}
480
481/*
482 * Set up a log for storing io patterns.
483 */
484static int init_iolog_write(struct thread_data *td)
485{
486 struct fio_file *ff;
487 FILE *f;
488 unsigned int i;
489
490 f = fopen(td->o.write_iolog_file, "a");
491 if (!f) {
492 perror("fopen write iolog");
493 return 1;
494 }
495
496 /*
497 * That's it for writing, setup a log buffer and we're done.
498 */
499 td->iolog_f = f;
500 td->iolog_buf = malloc(8192);
501 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
502
503 /*
504 * write our version line
505 */
506 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
507 perror("iolog init\n");
508 return 1;
509 }
510
511 /*
512 * add all known files
513 */
514 for_each_file(td, ff, i)
515 log_file(td, ff, FIO_LOG_ADD_FILE);
516
517 return 0;
518}
519
520int init_iolog(struct thread_data *td)
521{
522 int ret = 0;
523
524 if (td->o.read_iolog_file) {
Jens Axboed95b34a2013-11-21 09:55:49 -0700525 int need_swap;
526
Jens Axboeac9b9102011-10-01 15:01:08 -0400527 /*
528 * Check if it's a blktrace file and load that if possible.
529 * Otherwise assume it's a normal log file and load that.
530 */
Jens Axboed95b34a2013-11-21 09:55:49 -0700531 if (is_blktrace(td->o.read_iolog_file, &need_swap))
532 ret = load_blktrace(td, td->o.read_iolog_file, need_swap);
Jens Axboeac9b9102011-10-01 15:01:08 -0400533 else
534 ret = init_iolog_read(td);
535 } else if (td->o.write_iolog_file)
536 ret = init_iolog_write(td);
537
Jens Axboef01b34a2013-11-21 11:13:12 -0700538 if (ret)
539 td_verror(td, EINVAL, "failed initializing iolog");
540
Jens Axboeac9b9102011-10-01 15:01:08 -0400541 return ret;
542}
543
Jens Axboe38a812d2014-07-03 09:10:39 -0600544void setup_log(struct io_log **log, struct log_params *p,
545 const char *filename)
Jens Axboeac9b9102011-10-01 15:01:08 -0400546{
547 struct io_log *l = malloc(sizeof(*l));
548
Jens Axboeb8bc8cb2011-12-01 09:04:31 +0100549 memset(l, 0, sizeof(*l));
Jens Axboeac9b9102011-10-01 15:01:08 -0400550 l->nr_samples = 0;
551 l->max_samples = 1024;
Jens Axboe38a812d2014-07-03 09:10:39 -0600552 l->log_type = p->log_type;
553 l->log_offset = p->log_offset;
554 l->log_gz = p->log_gz;
Jens Axboebac4af12014-07-03 13:42:28 -0600555 l->log_gz_store = p->log_gz_store;
Jens Axboeccefd5f2014-06-30 20:59:03 -0600556 l->log = malloc(l->max_samples * log_entry_sz(l));
Jens Axboe38a812d2014-07-03 09:10:39 -0600557 l->avg_msec = p->avg_msec;
Jens Axboe987c4f42014-07-01 16:29:12 -0600558 l->filename = strdup(filename);
Jens Axboe38a812d2014-07-03 09:10:39 -0600559 l->td = p->td;
560
Jens Axboebac4af12014-07-03 13:42:28 -0600561 if (l->log_offset)
Jens Axboe238934c2014-07-03 18:01:11 -0600562 l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT;
Jens Axboebac4af12014-07-03 13:42:28 -0600563
Jens Axboe38a812d2014-07-03 09:10:39 -0600564 INIT_FLIST_HEAD(&l->chunk_list);
565
566 if (l->log_gz && !p->td)
567 l->log_gz = 0;
568 else if (l->log_gz) {
569 pthread_mutex_init(&l->chunk_lock, NULL);
570 p->td->flags |= TD_F_COMPRESS_LOG;
571 }
572
Jens Axboeac9b9102011-10-01 15:01:08 -0400573 *log = l;
574}
575
Jens Axboe2e802282014-04-02 21:47:27 -0600576#ifdef CONFIG_SETVBUF
577static void *set_file_buffer(FILE *f)
578{
579 size_t size = 1048576;
580 void *buf;
581
582 buf = malloc(size);
583 setvbuf(f, buf, _IOFBF, size);
584 return buf;
585}
586
587static void clear_file_buffer(void *buf)
588{
589 free(buf);
590}
591#else
592static void *set_file_buffer(FILE *f)
593{
594 return NULL;
595}
596
597static void clear_file_buffer(void *buf)
598{
599}
600#endif
601
Jens Axboe633d8252014-07-02 13:36:34 -0600602void free_log(struct io_log *log)
Jens Axboe987c4f42014-07-01 16:29:12 -0600603{
604 free(log->log);
605 free(log->filename);
606 free(log);
607}
608
Jens Axboebac4af12014-07-03 13:42:28 -0600609static void flush_samples(FILE *f, void *samples, uint64_t sample_size)
Jens Axboeac9b9102011-10-01 15:01:08 -0400610{
Jens Axboebac4af12014-07-03 13:42:28 -0600611 struct io_sample *s;
612 int log_offset;
613 uint64_t i, nr_samples;
614
615 if (!sample_size)
616 return;
617
618 s = __get_sample(samples, 0, 0);
Jens Axboe238934c2014-07-03 18:01:11 -0600619 log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0;
Jens Axboebac4af12014-07-03 13:42:28 -0600620
621 nr_samples = sample_size / __log_entry_sz(log_offset);
Jens Axboeac9b9102011-10-01 15:01:08 -0400622
Jens Axboe38a812d2014-07-03 09:10:39 -0600623 for (i = 0; i < nr_samples; i++) {
Jens Axboebac4af12014-07-03 13:42:28 -0600624 s = __get_sample(samples, log_offset, i);
Jens Axboeac9b9102011-10-01 15:01:08 -0400625
Jens Axboe38a812d2014-07-03 09:10:39 -0600626 if (!log_offset) {
Jens Axboeccefd5f2014-06-30 20:59:03 -0600627 fprintf(f, "%lu, %lu, %u, %u\n",
628 (unsigned long) s->time,
629 (unsigned long) s->val,
Jens Axboebac4af12014-07-03 13:42:28 -0600630 io_sample_ddir(s), s->bs);
Jens Axboeccefd5f2014-06-30 20:59:03 -0600631 } else {
632 struct io_sample_offset *so = (void *) s;
633
634 fprintf(f, "%lu, %lu, %u, %u, %llu\n",
635 (unsigned long) s->time,
636 (unsigned long) s->val,
Jens Axboebac4af12014-07-03 13:42:28 -0600637 io_sample_ddir(s), s->bs,
Jens Axboeccefd5f2014-06-30 20:59:03 -0600638 (unsigned long long) so->offset);
639 }
Jens Axboeac9b9102011-10-01 15:01:08 -0400640 }
Jens Axboe38a812d2014-07-03 09:10:39 -0600641}
642
643#ifdef CONFIG_ZLIB
Jens Axboe8ef51332014-07-03 17:56:55 -0600644
645struct iolog_flush_data {
646 struct tp_work work;
647 struct io_log *log;
648 void *samples;
649 uint64_t nr_samples;
650};
651
652struct iolog_compress {
653 struct flist_head list;
654 void *buf;
655 size_t len;
656 unsigned int seq;
Jens Axboe8ef51332014-07-03 17:56:55 -0600657};
658
659#define GZ_CHUNK 131072
660
661static struct iolog_compress *get_new_chunk(unsigned int seq)
662{
663 struct iolog_compress *c;
664
665 c = malloc(sizeof(*c));
666 INIT_FLIST_HEAD(&c->list);
667 c->buf = malloc(GZ_CHUNK);
668 c->len = 0;
669 c->seq = seq;
Jens Axboe8ef51332014-07-03 17:56:55 -0600670 return c;
671}
672
673static void free_chunk(struct iolog_compress *ic)
674{
Jens Axboe375daae2014-07-03 17:58:49 -0600675 free(ic->buf);
676 free(ic);
Jens Axboe8ef51332014-07-03 17:56:55 -0600677}
678
Jens Axboebac4af12014-07-03 13:42:28 -0600679static int z_stream_init(z_stream *stream, int gz_hdr)
Jens Axboe38a812d2014-07-03 09:10:39 -0600680{
Jens Axboebac4af12014-07-03 13:42:28 -0600681 int wbits = 15;
682
Jens Axboe38a812d2014-07-03 09:10:39 -0600683 stream->zalloc = Z_NULL;
684 stream->zfree = Z_NULL;
685 stream->opaque = Z_NULL;
686 stream->next_in = Z_NULL;
687
Jens Axboec5971cd2014-07-08 09:44:51 +0200688 /*
689 * zlib magic - add 32 for auto-detection of gz header or not,
690 * if we decide to store files in a gzip friendly format.
691 */
Jens Axboebac4af12014-07-03 13:42:28 -0600692 if (gz_hdr)
693 wbits += 32;
694
695 if (inflateInit2(stream, wbits) != Z_OK)
Jens Axboe38a812d2014-07-03 09:10:39 -0600696 return 1;
697
698 return 0;
699}
700
Jens Axboec5971cd2014-07-08 09:44:51 +0200701struct inflate_chunk_iter {
Jens Axboe38a812d2014-07-03 09:10:39 -0600702 unsigned int seq;
Jens Axboed4650642014-07-08 14:14:23 +0200703 int err;
Jens Axboe38a812d2014-07-03 09:10:39 -0600704 void *buf;
705 size_t buf_size;
706 size_t buf_used;
707 size_t chunk_sz;
708};
709
Jens Axboebac4af12014-07-03 13:42:28 -0600710static void finish_chunk(z_stream *stream, FILE *f,
Jens Axboec5971cd2014-07-08 09:44:51 +0200711 struct inflate_chunk_iter *iter)
Jens Axboe38a812d2014-07-03 09:10:39 -0600712{
Jens Axboe38a812d2014-07-03 09:10:39 -0600713 int ret;
714
715 ret = inflateEnd(stream);
716 if (ret != Z_OK)
717 log_err("fio: failed to end log inflation (%d)\n", ret);
718
Jens Axboebac4af12014-07-03 13:42:28 -0600719 flush_samples(f, iter->buf, iter->buf_used);
Jens Axboe38a812d2014-07-03 09:10:39 -0600720 free(iter->buf);
721 iter->buf = NULL;
722 iter->buf_size = iter->buf_used = 0;
723}
724
Jens Axboec5971cd2014-07-08 09:44:51 +0200725/*
726 * Iterative chunk inflation. Handles cases where we cross into a new
727 * sequence, doing flush finish of previous chunk if needed.
728 */
729static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f,
730 z_stream *stream, struct inflate_chunk_iter *iter)
Jens Axboe38a812d2014-07-03 09:10:39 -0600731{
Jens Axboe4f3fe6c2014-07-09 23:01:03 +0200732 size_t ret;
733
734 dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u",
735 (unsigned long) ic->len, ic->seq);
736
Jens Axboe38a812d2014-07-03 09:10:39 -0600737 if (ic->seq != iter->seq) {
738 if (iter->seq)
Jens Axboebac4af12014-07-03 13:42:28 -0600739 finish_chunk(stream, f, iter);
Jens Axboe38a812d2014-07-03 09:10:39 -0600740
Jens Axboebac4af12014-07-03 13:42:28 -0600741 z_stream_init(stream, gz_hdr);
Jens Axboe38a812d2014-07-03 09:10:39 -0600742 iter->seq = ic->seq;
743 }
744
745 stream->avail_in = ic->len;
746 stream->next_in = ic->buf;
747
748 if (!iter->buf_size) {
749 iter->buf_size = iter->chunk_sz;
750 iter->buf = malloc(iter->buf_size);
751 }
752
753 while (stream->avail_in) {
Jens Axboebac4af12014-07-03 13:42:28 -0600754 size_t this_out = iter->buf_size - iter->buf_used;
Jens Axboe38a812d2014-07-03 09:10:39 -0600755 int err;
756
Jens Axboebac4af12014-07-03 13:42:28 -0600757 stream->avail_out = this_out;
Jens Axboe38a812d2014-07-03 09:10:39 -0600758 stream->next_out = iter->buf + iter->buf_used;
759
760 err = inflate(stream, Z_NO_FLUSH);
761 if (err < 0) {
762 log_err("fio: failed inflating log: %d\n", err);
Jens Axboed4650642014-07-08 14:14:23 +0200763 iter->err = err;
Jens Axboe38a812d2014-07-03 09:10:39 -0600764 break;
765 }
766
Jens Axboebac4af12014-07-03 13:42:28 -0600767 iter->buf_used += this_out - stream->avail_out;
768
769 if (!stream->avail_out) {
770 iter->buf_size += iter->chunk_sz;
771 iter->buf = realloc(iter->buf, iter->buf_size);
772 continue;
773 }
774
775 if (err == Z_STREAM_END)
776 break;
Jens Axboe38a812d2014-07-03 09:10:39 -0600777 }
778
Jens Axboe4f3fe6c2014-07-09 23:01:03 +0200779 ret = (void *) stream->next_in - ic->buf;
780
781 dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) ret);
782
783 return ret;
Jens Axboe38a812d2014-07-03 09:10:39 -0600784}
785
Jens Axboec5971cd2014-07-08 09:44:51 +0200786/*
787 * Inflate stored compressed chunks, or write them directly to the log
788 * file if so instructed.
789 */
Jens Axboed4650642014-07-08 14:14:23 +0200790static int inflate_gz_chunks(struct io_log *log, FILE *f)
Jens Axboe38a812d2014-07-03 09:10:39 -0600791{
Jens Axboec5971cd2014-07-08 09:44:51 +0200792 struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, };
Jens Axboe38a812d2014-07-03 09:10:39 -0600793 z_stream stream;
794
795 while (!flist_empty(&log->chunk_list)) {
796 struct iolog_compress *ic;
797
Jens Axboe12dbd062014-07-03 21:19:57 -0600798 ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list);
Jens Axboe38a812d2014-07-03 09:10:39 -0600799 flist_del(&ic->list);
Jens Axboebac4af12014-07-03 13:42:28 -0600800
Jens Axboec5971cd2014-07-08 09:44:51 +0200801 if (log->log_gz_store) {
802 size_t ret;
803
Jens Axboe4f3fe6c2014-07-09 23:01:03 +0200804 dprint(FD_COMPRESS, "log write chunk size=%lu, "
805 "seq=%u\n", (unsigned long) ic->len, ic->seq);
806
Jens Axboec5971cd2014-07-08 09:44:51 +0200807 ret = fwrite(ic->buf, ic->len, 1, f);
Jens Axboed4650642014-07-08 14:14:23 +0200808 if (ret != 1 || ferror(f)) {
809 iter.err = errno;
Jens Axboec5971cd2014-07-08 09:44:51 +0200810 log_err("fio: error writing compressed log\n");
Jens Axboed4650642014-07-08 14:14:23 +0200811 }
Jens Axboec5971cd2014-07-08 09:44:51 +0200812 } else
813 inflate_chunk(ic, log->log_gz_store, f, &stream, &iter);
Jens Axboe375daae2014-07-03 17:58:49 -0600814
815 free_chunk(ic);
Jens Axboe38a812d2014-07-03 09:10:39 -0600816 }
817
818 if (iter.seq) {
Jens Axboebac4af12014-07-03 13:42:28 -0600819 finish_chunk(&stream, f, &iter);
Jens Axboe38a812d2014-07-03 09:10:39 -0600820 free(iter.buf);
821 }
Jens Axboed4650642014-07-08 14:14:23 +0200822
823 return iter.err;
Jens Axboe38a812d2014-07-03 09:10:39 -0600824}
825
Jens Axboec5971cd2014-07-08 09:44:51 +0200826/*
827 * Open compressed log file and decompress the stored chunks and
828 * write them to stdout. The chunks are stored sequentially in the
829 * file, so we iterate over them and do them one-by-one.
830 */
Jens Axboebac4af12014-07-03 13:42:28 -0600831int iolog_file_inflate(const char *file)
832{
Jens Axboec5971cd2014-07-08 09:44:51 +0200833 struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, };
Jens Axboebac4af12014-07-03 13:42:28 -0600834 struct iolog_compress ic;
835 z_stream stream;
836 struct stat sb;
Jens Axboe51e73842014-07-03 15:32:07 -0600837 ssize_t ret;
Jens Axboe66cf52d2014-07-03 21:57:29 -0600838 size_t total;
839 void *buf;
Jens Axboebac4af12014-07-03 13:42:28 -0600840 FILE *f;
841
Jens Axboebac4af12014-07-03 13:42:28 -0600842 f = fopen(file, "r");
843 if (!f) {
844 perror("fopen");
845 return 1;
846 }
847
Jens Axboe51e73842014-07-03 15:32:07 -0600848 if (stat(file, &sb) < 0) {
849 fclose(f);
850 perror("stat");
851 return 1;
852 }
853
Jens Axboe66cf52d2014-07-03 21:57:29 -0600854 ic.buf = buf = malloc(sb.st_size);
Jens Axboebac4af12014-07-03 13:42:28 -0600855 ic.len = sb.st_size;
Jens Axboebac4af12014-07-03 13:42:28 -0600856 ic.seq = 1;
857
858 ret = fread(ic.buf, ic.len, 1, f);
859 if (ret < 0) {
860 perror("fread");
861 fclose(f);
862 return 1;
863 } else if (ret != 1) {
864 log_err("fio: short read on reading log\n");
865 fclose(f);
866 return 1;
867 }
868
869 fclose(f);
870
Jens Axboec5971cd2014-07-08 09:44:51 +0200871 /*
872 * Each chunk will return Z_STREAM_END. We don't know how many
873 * chunks are in the file, so we just keep looping and incrementing
874 * the sequence number until we have consumed the whole compressed
875 * file.
876 */
Jens Axboe66cf52d2014-07-03 21:57:29 -0600877 total = ic.len;
878 do {
879 size_t ret;
880
Jens Axboec5971cd2014-07-08 09:44:51 +0200881 ret = inflate_chunk(&ic, 1, stdout, &stream, &iter);
Jens Axboe66cf52d2014-07-03 21:57:29 -0600882 total -= ret;
883 if (!total)
884 break;
Jens Axboed4650642014-07-08 14:14:23 +0200885 if (iter.err)
886 break;
Jens Axboe66cf52d2014-07-03 21:57:29 -0600887
888 ic.seq++;
889 ic.len -= ret;
890 ic.buf += ret;
891 } while (1);
Jens Axboebac4af12014-07-03 13:42:28 -0600892
893 if (iter.seq) {
894 finish_chunk(&stream, stdout, &iter);
895 free(iter.buf);
896 }
897
Jens Axboe66cf52d2014-07-03 21:57:29 -0600898 free(buf);
Jens Axboed4650642014-07-08 14:14:23 +0200899 return iter.err;
Jens Axboebac4af12014-07-03 13:42:28 -0600900}
901
Jens Axboe38a812d2014-07-03 09:10:39 -0600902#else
903
Jens Axboed4650642014-07-08 14:14:23 +0200904static int inflate_gz_chunks(struct io_log *log, FILE *f)
Jens Axboe38a812d2014-07-03 09:10:39 -0600905{
Jens Axboe1540fa72014-07-08 14:18:13 +0200906 return 0;
Jens Axboe38a812d2014-07-03 09:10:39 -0600907}
908
Jens Axboeb0adca32014-07-08 14:24:54 +0200909int iolog_file_inflate(const char *file)
910{
911 log_err("fio: log inflation not possible without zlib\n");
912 return 1;
913}
914
Jens Axboe38a812d2014-07-03 09:10:39 -0600915#endif
916
917void flush_log(struct io_log *log)
918{
919 void *buf;
920 FILE *f;
921
922 f = fopen(log->filename, "w");
923 if (!f) {
924 perror("fopen log");
925 return;
926 }
927
928 buf = set_file_buffer(f);
929
Jens Axboec5971cd2014-07-08 09:44:51 +0200930 inflate_gz_chunks(log, f);
Jens Axboe38a812d2014-07-03 09:10:39 -0600931
Jens Axboebac4af12014-07-03 13:42:28 -0600932 flush_samples(f, log->log, log->nr_samples * log_entry_sz(log));
Jens Axboeac9b9102011-10-01 15:01:08 -0400933
934 fclose(f);
Jens Axboe2e802282014-04-02 21:47:27 -0600935 clear_file_buffer(buf);
Jens Axboeac9b9102011-10-01 15:01:08 -0400936}
937
Jens Axboe987c4f42014-07-01 16:29:12 -0600938static int finish_log(struct thread_data *td, struct io_log *log, int trylock)
Jens Axboeac9b9102011-10-01 15:01:08 -0400939{
Jens Axboe38a812d2014-07-03 09:10:39 -0600940 if (td->tp_data)
941 iolog_flush(log, 1);
942
Jens Axboe243bfe12014-04-02 15:46:58 -0600943 if (trylock) {
Jens Axboe987c4f42014-07-01 16:29:12 -0600944 if (fio_trylock_file(log->filename))
Jens Axboe243bfe12014-04-02 15:46:58 -0600945 return 1;
946 } else
Jens Axboe987c4f42014-07-01 16:29:12 -0600947 fio_lock_file(log->filename);
Jens Axboe243bfe12014-04-02 15:46:58 -0600948
Jens Axboe38a812d2014-07-03 09:10:39 -0600949 if (td->client_type == FIO_CLIENT_TYPE_GUI)
Jens Axboe987c4f42014-07-01 16:29:12 -0600950 fio_send_iolog(td, log, log->filename);
Jens Axboe38a812d2014-07-03 09:10:39 -0600951 else
952 flush_log(log);
Jens Axboe243bfe12014-04-02 15:46:58 -0600953
Jens Axboe987c4f42014-07-01 16:29:12 -0600954 fio_unlock_file(log->filename);
Jens Axboe633d8252014-07-02 13:36:34 -0600955 free_log(log);
Jens Axboe243bfe12014-04-02 15:46:58 -0600956 return 0;
Jens Axboeac9b9102011-10-01 15:01:08 -0400957}
958
Jens Axboe38a812d2014-07-03 09:10:39 -0600959#ifdef CONFIG_ZLIB
960
Jens Axboec5971cd2014-07-08 09:44:51 +0200961/*
962 * Invoked from our compress helper thread, when logging would have exceeded
963 * the specified memory limitation. Compresses the previously stored
964 * entries.
965 */
Jens Axboe38a812d2014-07-03 09:10:39 -0600966static int gz_work(struct tp_work *work)
967{
968 struct iolog_flush_data *data;
969 struct iolog_compress *c;
970 struct flist_head list;
971 unsigned int seq;
972 z_stream stream;
973 size_t total = 0;
Jens Axboeaba6b602014-07-03 14:17:45 -0600974 int ret;
Jens Axboe38a812d2014-07-03 09:10:39 -0600975
976 INIT_FLIST_HEAD(&list);
977
978 data = container_of(work, struct iolog_flush_data, work);
979
980 stream.zalloc = Z_NULL;
981 stream.zfree = Z_NULL;
982 stream.opaque = Z_NULL;
983
Jens Axboebac4af12014-07-03 13:42:28 -0600984 ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
Jens Axboebac4af12014-07-03 13:42:28 -0600985 if (ret != Z_OK) {
Jens Axboe38a812d2014-07-03 09:10:39 -0600986 log_err("fio: failed to init gz stream\n");
987 return 0;
988 }
989
990 seq = ++data->log->chunk_seq;
Jens Axboebac4af12014-07-03 13:42:28 -0600991
Jens Axboe38a812d2014-07-03 09:10:39 -0600992 stream.next_in = (void *) data->samples;
993 stream.avail_in = data->nr_samples * log_entry_sz(data->log);
994
Jens Axboe4f3fe6c2014-07-09 23:01:03 +0200995 dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u\n",
996 (unsigned long) stream.avail_in, seq);
Jens Axboe38a812d2014-07-03 09:10:39 -0600997 do {
998 c = get_new_chunk(seq);
999 stream.avail_out = GZ_CHUNK;
1000 stream.next_out = c->buf;
1001 ret = deflate(&stream, Z_NO_FLUSH);
1002 if (ret < 0) {
1003 log_err("fio: deflate log (%d)\n", ret);
Jens Axboed4650642014-07-08 14:14:23 +02001004 free_chunk(c);
1005 goto err;
Jens Axboe38a812d2014-07-03 09:10:39 -06001006 }
1007
1008 c->len = GZ_CHUNK - stream.avail_out;
1009 flist_add_tail(&c->list, &list);
1010 total += c->len;
1011 } while (stream.avail_in);
1012
1013 stream.next_out = c->buf + c->len;
1014 stream.avail_out = GZ_CHUNK - c->len;
1015
1016 ret = deflate(&stream, Z_FINISH);
1017 if (ret == Z_STREAM_END)
1018 c->len = GZ_CHUNK - stream.avail_out;
1019 else {
1020 do {
1021 c = get_new_chunk(seq);
1022 stream.avail_out = GZ_CHUNK;
1023 stream.next_out = c->buf;
1024 ret = deflate(&stream, Z_FINISH);
1025 c->len = GZ_CHUNK - stream.avail_out;
Jens Axboe4f3fe6c2014-07-09 23:01:03 +02001026 total += c->len;
Jens Axboe38a812d2014-07-03 09:10:39 -06001027 flist_add_tail(&c->list, &list);
1028 } while (ret != Z_STREAM_END);
1029 }
1030
Jens Axboe4f3fe6c2014-07-09 23:01:03 +02001031 dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total);
1032
Jens Axboe38a812d2014-07-03 09:10:39 -06001033 ret = deflateEnd(&stream);
1034 if (ret != Z_OK)
1035 log_err("fio: deflateEnd %d\n", ret);
1036
1037 free(data->samples);
1038
1039 if (!flist_empty(&list)) {
1040 pthread_mutex_lock(&data->log->chunk_lock);
1041 flist_splice_tail(&list, &data->log->chunk_list);
1042 pthread_mutex_unlock(&data->log->chunk_lock);
1043 }
1044
Jens Axboed4650642014-07-08 14:14:23 +02001045 ret = 0;
1046done:
Jens Axboe38a812d2014-07-03 09:10:39 -06001047 if (work->wait) {
1048 work->done = 1;
1049 pthread_cond_signal(&work->cv);
1050 } else
1051 free(data);
1052
Jens Axboed4650642014-07-08 14:14:23 +02001053 return ret;
1054err:
1055 while (!flist_empty(&list)) {
1056 c = flist_first_entry(list.next, struct iolog_compress, list);
1057 flist_del(&c->list);
1058 free_chunk(c);
1059 }
1060 ret = 1;
1061 goto done;
Jens Axboe38a812d2014-07-03 09:10:39 -06001062}
1063
Jens Axboec5971cd2014-07-08 09:44:51 +02001064/*
1065 * Queue work item to compress the existing log entries. We copy the
1066 * samples, and reset the log sample count to 0 (so the logging will
1067 * continue to use the memory associated with the log). If called with
1068 * wait == 1, will not return until the log compression has completed.
1069 */
Jens Axboe38a812d2014-07-03 09:10:39 -06001070int iolog_flush(struct io_log *log, int wait)
1071{
Jens Axboebac4af12014-07-03 13:42:28 -06001072 struct tp_data *tdat = log->td->tp_data;
Jens Axboe38a812d2014-07-03 09:10:39 -06001073 struct iolog_flush_data *data;
1074 size_t sample_size;
1075
1076 data = malloc(sizeof(*data));
1077 if (!data)
1078 return 1;
1079
1080 data->log = log;
1081
1082 sample_size = log->nr_samples * log_entry_sz(log);
1083 data->samples = malloc(sample_size);
1084 if (!data->samples) {
1085 free(data);
1086 return 1;
1087 }
1088
1089 memcpy(data->samples, log->log, sample_size);
1090 data->nr_samples = log->nr_samples;
1091 data->work.fn = gz_work;
1092 log->nr_samples = 0;
1093
1094 if (wait) {
1095 pthread_mutex_init(&data->work.lock, NULL);
1096 pthread_cond_init(&data->work.cv, NULL);
1097 data->work.wait = 1;
1098 } else
1099 data->work.wait = 0;
1100
Jens Axboec9a5f392014-07-09 09:52:20 +02001101 data->work.prio = 1;
Jens Axboebac4af12014-07-03 13:42:28 -06001102 tp_queue_work(tdat, &data->work);
Jens Axboe38a812d2014-07-03 09:10:39 -06001103
1104 if (wait) {
1105 pthread_mutex_lock(&data->work.lock);
1106 while (!data->work.done)
1107 pthread_cond_wait(&data->work.cv, &data->work.lock);
1108 pthread_mutex_unlock(&data->work.lock);
1109 free(data);
1110 }
1111
1112 return 0;
1113}
1114
1115#else
1116
1117int iolog_flush(struct io_log *log, int wait)
1118{
1119 return 1;
1120}
1121
1122#endif
1123
Jens Axboe905e3d42014-04-02 20:01:27 -06001124static int write_iops_log(struct thread_data *td, int try)
1125{
Jens Axboe987c4f42014-07-01 16:29:12 -06001126 struct io_log *log = td->iops_log;
Jens Axboe905e3d42014-04-02 20:01:27 -06001127
Jens Axboe987c4f42014-07-01 16:29:12 -06001128 if (!log)
1129 return 0;
1130
1131 return finish_log(td, log, try);
Jens Axboe905e3d42014-04-02 20:01:27 -06001132}
1133
1134static int write_slat_log(struct thread_data *td, int try)
1135{
Jens Axboe987c4f42014-07-01 16:29:12 -06001136 struct io_log *log = td->slat_log;
Jens Axboe905e3d42014-04-02 20:01:27 -06001137
Jens Axboe987c4f42014-07-01 16:29:12 -06001138 if (!log)
1139 return 0;
1140
1141 return finish_log(td, log, try);
Jens Axboe905e3d42014-04-02 20:01:27 -06001142}
1143
1144static int write_clat_log(struct thread_data *td, int try)
1145{
Jens Axboe987c4f42014-07-01 16:29:12 -06001146 struct io_log *log = td->clat_log;
Jens Axboe905e3d42014-04-02 20:01:27 -06001147
Jens Axboe987c4f42014-07-01 16:29:12 -06001148 if (!log)
1149 return 0;
1150
1151 return finish_log(td, log, try);
Jens Axboe905e3d42014-04-02 20:01:27 -06001152}
1153
1154static int write_lat_log(struct thread_data *td, int try)
1155{
Jens Axboe987c4f42014-07-01 16:29:12 -06001156 struct io_log *log = td->lat_log;
Jens Axboe905e3d42014-04-02 20:01:27 -06001157
Jens Axboe987c4f42014-07-01 16:29:12 -06001158 if (!log)
1159 return 0;
1160
1161 return finish_log(td, log, try);
Jens Axboe905e3d42014-04-02 20:01:27 -06001162}
1163
1164static int write_bandw_log(struct thread_data *td, int try)
1165{
Jens Axboe987c4f42014-07-01 16:29:12 -06001166 struct io_log *log = td->bw_log;
Jens Axboe905e3d42014-04-02 20:01:27 -06001167
Jens Axboe987c4f42014-07-01 16:29:12 -06001168 if (!log)
1169 return 0;
1170
1171 return finish_log(td, log, try);
Jens Axboe905e3d42014-04-02 20:01:27 -06001172}
1173
1174enum {
1175 BW_LOG_MASK = 1,
1176 LAT_LOG_MASK = 2,
1177 SLAT_LOG_MASK = 4,
1178 CLAT_LOG_MASK = 8,
1179 IOPS_LOG_MASK = 16,
1180
Jens Axboe905e3d42014-04-02 20:01:27 -06001181 ALL_LOG_NR = 5,
1182};
1183
1184struct log_type {
1185 unsigned int mask;
1186 int (*fn)(struct thread_data *, int);
1187};
1188
1189static struct log_type log_types[] = {
1190 {
1191 .mask = BW_LOG_MASK,
1192 .fn = write_bandw_log,
1193 },
1194 {
1195 .mask = LAT_LOG_MASK,
1196 .fn = write_lat_log,
1197 },
1198 {
1199 .mask = SLAT_LOG_MASK,
1200 .fn = write_slat_log,
1201 },
1202 {
1203 .mask = CLAT_LOG_MASK,
1204 .fn = write_clat_log,
1205 },
1206 {
1207 .mask = IOPS_LOG_MASK,
1208 .fn = write_iops_log,
1209 },
1210};
1211
1212void fio_writeout_logs(struct thread_data *td)
1213{
Jens Axboeea5409f2014-04-02 20:07:28 -06001214 unsigned int log_mask = 0;
Jens Axboe905e3d42014-04-02 20:01:27 -06001215 unsigned int log_left = ALL_LOG_NR;
1216 int old_state, i;
1217
1218 old_state = td_bump_runstate(td, TD_FINISHING);
1219
1220 finalize_logs(td);
1221
1222 while (log_left) {
1223 int prev_log_left = log_left;
1224
1225 for (i = 0; i < ALL_LOG_NR && log_left; i++) {
1226 struct log_type *lt = &log_types[i];
1227 int ret;
1228
Jens Axboeea5409f2014-04-02 20:07:28 -06001229 if (!(log_mask & lt->mask)) {
Jens Axboe905e3d42014-04-02 20:01:27 -06001230 ret = lt->fn(td, log_left != 1);
1231 if (!ret) {
1232 log_left--;
Jens Axboeea5409f2014-04-02 20:07:28 -06001233 log_mask |= lt->mask;
Jens Axboe905e3d42014-04-02 20:01:27 -06001234 }
1235 }
1236 }
1237
1238 if (prev_log_left == log_left)
1239 usleep(5000);
1240 }
1241
1242 td_restore_runstate(td, old_state);
1243}