-/*
- * Code related to writing an iolog of what a thread is doing, and to
- * later read that back and replay
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include "list.h"
-#include "fio.h"
-
-static const char iolog_ver2[] = "fio version 2 iolog";
-
-void log_io_u(struct thread_data *td, struct io_u *io_u)
-{
- const char *act[] = { "read", "write", "sync" };
-
- assert(io_u->ddir < 3);
-
- if (!td->o.write_iolog_file)
- return;
-
- fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name, act[io_u->ddir], io_u->offset, io_u->buflen);
-}
-
-void log_file(struct thread_data *td, struct fio_file *f,
- enum file_log_act what)
-{
- const char *act[] = { "add", "open", "close" };
-
- assert(what < 3);
-
- if (!td->o.write_iolog_file)
- return;
-
- fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
-}
-
-static void iolog_delay(struct thread_data *td, unsigned long delay)
-{
- unsigned long usec = utime_since_now(&td->last_issue);
-
- if (delay < usec)
- return;
-
- delay -= usec;
-
- /*
- * less than 100 usec delay, just regard it as noise
- */
- if (delay < 100)
- return;
-
- usec_sleep(td, delay);
-}
-
-int read_iolog_get(struct thread_data *td, struct io_u *io_u)
-{
- struct io_piece *ipo;
-
- while (!list_empty(&td->io_log_list)) {
- ipo = list_entry(td->io_log_list.next, struct io_piece, list);
- list_del(&ipo->list);
-
- /*
- * invalid ddir, this is a file action
- */
- if (ipo->ddir == DDIR_INVAL) {
- struct fio_file *f = &td->files[ipo->fileno];
-
- if (ipo->file_action == FIO_LOG_OPEN_FILE) {
- assert(!td_io_open_file(td, f));
- free(ipo);
- continue;
- } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) {
- td_io_close_file(td, f);
- free(ipo);
- continue;
- }
- }
-
- io_u->offset = ipo->offset;
- io_u->buflen = ipo->len;
- io_u->ddir = ipo->ddir;
- io_u->file = &td->files[ipo->fileno];
- get_file(io_u->file);
-
- if (ipo->delay)
- iolog_delay(td, ipo->delay);
-
- free(ipo);
- return 0;
- }
-
- td->done = 1;
- return 1;
-}
-
-void prune_io_piece_log(struct thread_data *td)
-{
- struct io_piece *ipo;
- struct rb_node *n;
-
- while ((n = rb_first(&td->io_hist_tree)) != NULL) {
- ipo = rb_entry(n, struct io_piece, rb_node);
- rb_erase(n, &td->io_hist_tree);
- free(ipo);
- }
-}