2 * Code related to writing an iolog of what a thread is doing, and to
3 * later read that back and replay
11 static const char iolog_ver2[] = "fio version 2 iolog";
13 void queue_io_piece(struct thread_data *td, struct io_piece *ipo)
15 list_add_tail(&ipo->list, &td->io_log_list);
16 td->total_io_size += ipo->len;
19 void log_io_u(struct thread_data *td, struct io_u *io_u)
21 const char *act[] = { "read", "write", "sync" };
23 assert(io_u->ddir < 3);
25 if (!td->o.write_iolog_file)
28 fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name,
29 act[io_u->ddir], io_u->offset,
33 void log_file(struct thread_data *td, struct fio_file *f,
34 enum file_log_act what)
36 const char *act[] = { "add", "open", "close" };
40 if (!td->o.write_iolog_file)
45 * this happens on the pre-open/close done before the job starts
50 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
53 static void iolog_delay(struct thread_data *td, unsigned long delay)
55 unsigned long usec = utime_since_now(&td->last_issue);
63 * less than 100 usec delay, just regard it as noise
68 usec_sleep(td, delay);
71 static int ipo_special(struct thread_data *td, struct io_piece *ipo)
79 if (ipo->ddir != DDIR_INVAL)
82 f = td->files[ipo->fileno];
84 switch (ipo->file_action) {
85 case FIO_LOG_OPEN_FILE:
86 ret = td_io_open_file(td, f);
91 td_verror(td, ret, "iolog open file");
93 case FIO_LOG_CLOSE_FILE:
94 td_io_close_file(td, f);
96 case FIO_LOG_UNLINK_FILE:
100 log_err("fio: bad file action %d\n", ipo->file_action);
107 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
109 struct io_piece *ipo;
111 while (!list_empty(&td->io_log_list)) {
114 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
115 list_del(&ipo->list);
117 ret = ipo_special(td, ipo);
121 } else if (ret > 0) {
126 io_u->offset = ipo->offset;
127 io_u->buflen = ipo->len;
128 io_u->ddir = ipo->ddir;
129 io_u->file = td->files[ipo->fileno];
130 get_file(io_u->file);
132 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
133 io_u->buflen, io_u->file->file_name);
136 iolog_delay(td, ipo->delay);
146 void prune_io_piece_log(struct thread_data *td)
148 struct io_piece *ipo;
151 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
152 ipo = rb_entry(n, struct io_piece, rb_node);
153 rb_erase(n, &td->io_hist_tree);
157 while (!list_empty(&td->io_hist_list)) {
158 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
159 list_del(&ipo->list);
165 * log a successful write, so we can unwind the log for verify
167 void log_io_piece(struct thread_data *td, struct io_u *io_u)
169 struct rb_node **p, *parent;
170 struct io_piece *ipo, *__ipo;
172 ipo = malloc(sizeof(struct io_piece));
173 ipo->file = io_u->file;
174 ipo->offset = io_u->offset;
175 ipo->len = io_u->buflen;
178 * We don't need to sort the entries, if:
180 * Sequential writes, or
181 * Random writes that lay out the file as it goes along
183 * For both these cases, just reading back data in the order we
184 * wrote it out is the fastest.
186 if (!td_random(td) || !td->o.overwrite) {
187 INIT_LIST_HEAD(&ipo->list);
188 list_add_tail(&ipo->list, &td->io_hist_list);
192 RB_CLEAR_NODE(&ipo->rb_node);
193 p = &td->io_hist_tree.rb_node;
197 * Sort the entry into the verification list
202 __ipo = rb_entry(parent, struct io_piece, rb_node);
203 if (ipo->offset <= __ipo->offset)
209 rb_link_node(&ipo->rb_node, parent, p);
210 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
213 void write_iolog_close(struct thread_data *td)
219 td->iolog_buf = NULL;
223 * Read version 2 iolog data. It is enhanced to include per-file logging,
226 static int read_iolog2(struct thread_data *td, FILE *f)
228 unsigned long long offset;
230 int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
235 free_release_files(td);
238 * Read in the read iolog and store it, reuse the infrastructure
239 * for doing verifications.
242 fname = malloc(256+16);
243 act = malloc(256+16);
246 while ((p = fgets(str, 4096, f)) != NULL) {
247 struct io_piece *ipo;
250 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
256 if (!strcmp(act, "read"))
258 else if (!strcmp(act, "write"))
260 else if (!strcmp(act, "sync"))
263 log_err("fio: bad iolog file action: %s\n",
269 if (!strcmp(act, "add")) {
271 fileno = add_file(td, fname);
272 file_action = FIO_LOG_ADD_FILE;
274 } else if (!strcmp(act, "open")) {
275 fileno = get_fileno(td, fname);
276 file_action = FIO_LOG_OPEN_FILE;
277 } else if (!strcmp(act, "close")) {
278 fileno = get_fileno(td, fname);
279 file_action = FIO_LOG_CLOSE_FILE;
281 log_err("fio: bad iolog file action: %s\n",
286 log_err("bad iolog2: %s", p);
292 else if (rw == DDIR_WRITE) {
294 * Don't add a write for ro mode
299 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
300 log_err("bad ddir: %d\n", rw);
307 ipo = malloc(sizeof(*ipo));
308 memset(ipo, 0, sizeof(*ipo));
309 INIT_LIST_HEAD(&ipo->list);
310 ipo->offset = offset;
313 if (bytes > td->o.max_bs[rw])
314 td->o.max_bs[rw] = bytes;
315 if (rw == DDIR_INVAL) {
316 ipo->fileno = fileno;
317 ipo->file_action = file_action;
319 queue_io_piece(td, ipo);
326 if (writes && read_only) {
327 log_err("fio: <%s> skips replay of %d writes due to"
328 " read-only\n", td->o.name, writes);
332 if (!reads && !writes)
334 else if (reads && !writes)
335 td->o.td_ddir = TD_DDIR_READ;
336 else if (!reads && writes)
337 td->o.td_ddir = TD_DDIR_WRITE;
339 td->o.td_ddir = TD_DDIR_RW;
345 * open iolog, check version, and call appropriate parser
347 static int init_iolog_read(struct thread_data *td)
349 char buffer[256], *p;
353 f = fopen(td->o.read_iolog_file, "r");
355 perror("fopen read iolog");
359 p = fgets(buffer, sizeof(buffer), f);
361 td_verror(td, errno, "iolog read");
362 log_err("fio: unable to read iolog\n");
367 * version 2 of the iolog stores a specific string as the
368 * first line, check for that
370 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
371 ret = read_iolog2(td, f);
373 log_err("fio: iolog version 1 is no longer supported\n");
382 * Setup a log for storing io patterns.
384 static int init_iolog_write(struct thread_data *td)
390 f = fopen(td->o.write_iolog_file, "w+");
392 perror("fopen write iolog");
397 * That's it for writing, setup a log buffer and we're done.
400 td->iolog_buf = malloc(8192);
401 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
404 * write our version line
406 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
407 perror("iolog init\n");
412 * add all known files
414 for_each_file(td, ff, i)
415 log_file(td, ff, FIO_LOG_ADD_FILE);
420 int init_iolog(struct thread_data *td)
424 if (td->io_ops->flags & FIO_DISKLESSIO)
427 if (td->o.read_iolog_file) {
429 * Check if it's a blktrace file and load that if possible.
430 * Otherwise assume it's a normal log file and load that.
432 if (is_blktrace(td->o.read_iolog_file))
433 ret = load_blktrace(td, td->o.read_iolog_file);
435 ret = init_iolog_read(td);
436 } else if (td->o.write_iolog_file)
437 ret = init_iolog_write(td);
442 void setup_log(struct io_log **log)
444 struct io_log *l = malloc(sizeof(*l));
447 l->max_samples = 1024;
448 l->log = malloc(l->max_samples * sizeof(struct io_sample));
452 void __finish_log(struct io_log *log, const char *name)
457 f = fopen(name, "w");
463 for (i = 0; i < log->nr_samples; i++) {
464 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val,
473 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
475 char file_name[256], *p;
477 snprintf(file_name, 200, "%s_%s.log", td->o.name, name);
478 p = basename(file_name);
479 __finish_log(log, p);