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 log_io_u(struct thread_data *td, struct io_u *io_u)
15 const char *act[] = { "read", "write", "sync" };
17 assert(io_u->ddir < 3);
19 if (!td->o.write_iolog_file)
22 fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name,
23 act[io_u->ddir], io_u->offset,
27 void log_file(struct thread_data *td, struct fio_file *f,
28 enum file_log_act what)
30 const char *act[] = { "add", "open", "close" };
34 if (!td->o.write_iolog_file)
37 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
40 static void iolog_delay(struct thread_data *td, unsigned long delay)
42 unsigned long usec = utime_since_now(&td->last_issue);
50 * less than 100 usec delay, just regard it as noise
55 usec_sleep(td, delay);
58 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
62 while (!list_empty(&td->io_log_list)) {
63 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
67 * invalid ddir, this is a file action
69 if (ipo->ddir == DDIR_INVAL) {
70 struct fio_file *f = td->files[ipo->fileno];
72 if (ipo->file_action == FIO_LOG_OPEN_FILE) {
75 ret = td_io_open_file(td, f);
80 td_verror(td, ret, "iolog open file");
82 } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) {
83 td_io_close_file(td, f);
89 io_u->offset = ipo->offset;
90 io_u->buflen = ipo->len;
91 io_u->ddir = ipo->ddir;
92 io_u->file = td->files[ipo->fileno];
95 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
96 io_u->buflen, io_u->file->file_name);
99 iolog_delay(td, ipo->delay);
109 void prune_io_piece_log(struct thread_data *td)
111 struct io_piece *ipo;
114 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
115 ipo = rb_entry(n, struct io_piece, rb_node);
116 rb_erase(n, &td->io_hist_tree);
120 while (!list_empty(&td->io_hist_list)) {
121 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
122 list_del(&ipo->list);
128 * log a successful write, so we can unwind the log for verify
130 void log_io_piece(struct thread_data *td, struct io_u *io_u)
132 struct rb_node **p, *parent;
133 struct io_piece *ipo, *__ipo;
135 ipo = malloc(sizeof(struct io_piece));
136 ipo->file = io_u->file;
137 ipo->offset = io_u->offset;
138 ipo->len = io_u->buflen;
141 * We don't need to sort the entries, if:
143 * Sequential writes, or
144 * Random writes that lay out the file as it goes along
146 * For both these cases, just reading back data in the order we
147 * wrote it out is the fastest.
149 if (!td_random(td) || !td->o.overwrite) {
150 INIT_LIST_HEAD(&ipo->list);
151 list_add_tail(&ipo->list, &td->io_hist_list);
155 RB_CLEAR_NODE(&ipo->rb_node);
156 p = &td->io_hist_tree.rb_node;
160 * Sort the entry into the verification list
165 __ipo = rb_entry(parent, struct io_piece, rb_node);
166 if (ipo->offset <= __ipo->offset)
172 rb_link_node(&ipo->rb_node, parent, p);
173 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
176 void write_iolog_close(struct thread_data *td)
182 td->iolog_buf = NULL;
186 * Read version 2 iolog data. It is enhanced to include per-file logging,
189 static int read_iolog2(struct thread_data *td, FILE *f)
191 unsigned long long offset;
193 int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
198 free_release_files(td);
201 * Read in the read iolog and store it, reuse the infrastructure
202 * for doing verifications.
205 fname = malloc(256+16);
206 act = malloc(256+16);
209 while ((p = fgets(str, 4096, f)) != NULL) {
210 struct io_piece *ipo;
213 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
219 if (!strcmp(act, "read"))
221 else if (!strcmp(act, "write"))
223 else if (!strcmp(act, "sync"))
226 log_err("fio: bad iolog file action: %s\n",
232 if (!strcmp(act, "add")) {
234 fileno = add_file(td, fname);
235 file_action = FIO_LOG_ADD_FILE;
237 } else if (!strcmp(act, "open")) {
238 fileno = get_fileno(td, fname);
239 file_action = FIO_LOG_OPEN_FILE;
240 } else if (!strcmp(act, "close")) {
241 fileno = get_fileno(td, fname);
242 file_action = FIO_LOG_CLOSE_FILE;
244 log_err("fio: bad iolog file action: %s\n",
249 log_err("bad iolog2: %s", p);
255 else if (rw == DDIR_WRITE) {
257 * Don't add a write for ro mode
262 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
263 log_err("bad ddir: %d\n", rw);
270 ipo = malloc(sizeof(*ipo));
271 memset(ipo, 0, sizeof(*ipo));
272 INIT_LIST_HEAD(&ipo->list);
273 ipo->offset = offset;
276 if (bytes > td->o.max_bs[rw])
277 td->o.max_bs[rw] = bytes;
278 if (rw == DDIR_INVAL) {
279 ipo->fileno = fileno;
280 ipo->file_action = file_action;
282 list_add_tail(&ipo->list, &td->io_log_list);
283 td->total_io_size += bytes;
290 if (writes && read_only) {
291 log_err("fio: <%s> skips replay of %d writes due to"
292 " read-only\n", td->o.name, writes);
296 if (!reads && !writes)
298 else if (reads && !writes)
299 td->o.td_ddir = TD_DDIR_READ;
300 else if (!reads && writes)
301 td->o.td_ddir = TD_DDIR_WRITE;
303 td->o.td_ddir = TD_DDIR_RW;
309 * Read version 1 iolog data.
311 static int read_iolog(struct thread_data *td, FILE *f)
313 unsigned long long offset;
320 * Read in the read iolog and store it, reuse the infrastructure
321 * for doing verifications.
325 while ((p = fgets(str, 4096, f)) != NULL) {
326 struct io_piece *ipo;
328 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
329 log_err("bad iolog: %s\n", p);
334 else if (rw == DDIR_WRITE) {
336 * Don't add a write for ro mode
341 } else if (rw != DDIR_SYNC) {
342 log_err("bad ddir: %d\n", rw);
346 ipo = malloc(sizeof(*ipo));
347 memset(ipo, 0, sizeof(*ipo));
348 INIT_LIST_HEAD(&ipo->list);
349 ipo->offset = offset;
351 ipo->ddir = (enum fio_ddir) rw;
352 if (bytes > td->o.max_bs[rw])
353 td->o.max_bs[rw] = bytes;
354 list_add_tail(&ipo->list, &td->io_log_list);
355 td->total_io_size += bytes;
360 if (writes && read_only) {
361 log_err("fio: <%s> skips replay of %d writes due to"
362 " read-only\n", td->o.name, writes);
366 if (!reads && !writes)
368 else if (reads && !writes)
369 td->o.td_ddir = TD_DDIR_READ;
370 else if (!reads && writes)
371 td->o.td_ddir = TD_DDIR_WRITE;
373 td->o.td_ddir = TD_DDIR_RW;
379 * open iolog, check version, and call appropriate parser
381 static int init_iolog_read(struct thread_data *td)
383 char buffer[256], *p;
387 f = fopen(td->o.read_iolog_file, "r");
389 perror("fopen read iolog");
393 p = fgets(buffer, sizeof(buffer), f);
395 td_verror(td, errno, "iolog read");
396 log_err("fio: unable to read iolog\n");
401 * version 2 of the iolog stores a specific string as the
402 * first line, check for that
404 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
405 ret = read_iolog2(td, f);
408 * seek back to the beginning
410 if (fseek(f, 0, SEEK_SET) < 0) {
411 td_verror(td, errno, "iolog read");
412 log_err("fio: unable to read iolog\n");
416 ret = read_iolog(td, f);
424 * Setup a log for storing io patterns.
426 static int init_iolog_write(struct thread_data *td)
432 f = fopen(td->o.write_iolog_file, "w+");
434 perror("fopen write iolog");
439 * That's it for writing, setup a log buffer and we're done.
442 td->iolog_buf = malloc(8192);
443 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
446 * write our version line
448 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
449 perror("iolog init\n");
454 * add all known files
456 for_each_file(td, ff, i)
457 log_file(td, ff, FIO_LOG_ADD_FILE);
462 int init_iolog(struct thread_data *td)
466 if (td->io_ops->flags & FIO_DISKLESSIO)
469 if (td->o.read_iolog_file) {
471 * Check if it's a blktrace file and load that if possible.
472 * Otherwise assume it's a normal log file and load that.
474 if (is_blktrace(td->o.read_iolog_file))
475 ret = load_blktrace(td, td->o.read_iolog_file);
477 ret = init_iolog_read(td);
478 } else if (td->o.write_iolog_file)
479 ret = init_iolog_write(td);
484 void setup_log(struct io_log **log)
486 struct io_log *l = malloc(sizeof(*l));
489 l->max_samples = 1024;
490 l->log = malloc(l->max_samples * sizeof(struct io_sample));
494 void __finish_log(struct io_log *log, const char *name)
499 f = fopen(name, "w");
505 for (i = 0; i < log->nr_samples; i++) {
506 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val,
515 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
519 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
520 __finish_log(log, file_name);