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, act[io_u->ddir], io_u->offset, io_u->buflen);
25 void log_file(struct thread_data *td, struct fio_file *f,
26 enum file_log_act what)
28 const char *act[] = { "add", "open", "close" };
32 if (!td->o.write_iolog_file)
35 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
38 static void iolog_delay(struct thread_data *td, unsigned long delay)
40 unsigned long usec = utime_since_now(&td->last_issue);
48 * less than 100 usec delay, just regard it as noise
53 usec_sleep(td, delay);
56 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
60 while (!list_empty(&td->io_log_list)) {
61 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
65 * invalid ddir, this is a file action
67 if (ipo->ddir == DDIR_INVAL) {
68 struct fio_file *f = &td->files[ipo->fileno];
70 if (ipo->file_action == FIO_LOG_OPEN_FILE) {
71 assert(!td_io_open_file(td, f));
74 } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) {
75 td_io_close_file(td, f);
81 io_u->offset = ipo->offset;
82 io_u->buflen = ipo->len;
83 io_u->ddir = ipo->ddir;
84 io_u->file = &td->files[ipo->fileno];
87 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
88 io_u->buflen, io_u->file->file_name);
91 iolog_delay(td, ipo->delay);
101 void prune_io_piece_log(struct thread_data *td)
103 struct io_piece *ipo;
106 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
107 ipo = rb_entry(n, struct io_piece, rb_node);
108 rb_erase(n, &td->io_hist_tree);
112 while (!list_empty(&td->io_hist_list)) {
113 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
114 list_del(&ipo->list);
120 * log a successful write, so we can unwind the log for verify
122 void log_io_piece(struct thread_data *td, struct io_u *io_u)
124 struct rb_node **p, *parent;
125 struct io_piece *ipo, *__ipo;
127 ipo = malloc(sizeof(struct io_piece));
128 ipo->file = io_u->file;
129 ipo->offset = io_u->offset;
130 ipo->len = io_u->buflen;
133 * We don't need to sort the entries, if:
135 * Sequential writes, or
136 * Random writes that lay out the file as it goes along
138 * For both these cases, just reading back data in the order we
139 * wrote it out is the fastest.
141 if (!td_random(td) || !td->o.overwrite) {
142 INIT_LIST_HEAD(&ipo->list);
143 list_add_tail(&ipo->list, &td->io_hist_list);
147 RB_CLEAR_NODE(&ipo->rb_node);
148 p = &td->io_hist_tree.rb_node;
152 * Sort the entry into the verification list
157 __ipo = rb_entry(parent, struct io_piece, rb_node);
158 if (ipo->offset <= __ipo->offset)
164 rb_link_node(&ipo->rb_node, parent, p);
165 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
168 void write_iolog_close(struct thread_data *td)
174 td->iolog_buf = NULL;
178 * Read version 2 iolog data. It is enhanced to include per-file logging,
181 static int read_iolog2(struct thread_data *td, FILE *f)
183 unsigned long long offset;
185 int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
190 free_release_files(td);
193 * Read in the read iolog and store it, reuse the infrastructure
194 * for doing verifications.
197 fname = malloc(256+16);
198 act = malloc(256+16);
201 while ((p = fgets(str, 4096, f)) != NULL) {
202 struct io_piece *ipo;
205 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, &bytes);
210 if (!strcmp(act, "read"))
212 else if (!strcmp(act, "write"))
214 else if (!strcmp(act, "sync"))
217 log_err("fio: bad iolog file action: %s\n",act);
222 if (!strcmp(act, "add")) {
224 fileno = add_file(td, fname);
225 file_action = FIO_LOG_ADD_FILE;
227 } else if (!strcmp(act, "open")) {
228 fileno = get_fileno(td, fname);
229 file_action = FIO_LOG_OPEN_FILE;
230 } else if (!strcmp(act, "close")) {
231 fileno = get_fileno(td, fname);
232 file_action = FIO_LOG_CLOSE_FILE;
234 log_err("fio: bad iolog file action: %s\n",act);
238 log_err("bad iolog2: %s", p);
244 else if (rw == DDIR_WRITE) {
247 * Don't add a write for ro mode
251 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
252 log_err("bad ddir: %d\n", rw);
259 ipo = malloc(sizeof(*ipo));
260 memset(ipo, 0, sizeof(*ipo));
261 INIT_LIST_HEAD(&ipo->list);
262 ipo->offset = offset;
265 if (bytes > td->o.max_bs[rw])
266 td->o.max_bs[rw] = bytes;
267 if (rw == DDIR_INVAL) {
268 ipo->fileno = fileno;
269 ipo->file_action = file_action;
271 list_add_tail(&ipo->list, &td->io_log_list);
272 td->total_io_size += bytes;
279 if (writes && read_only) {
280 log_err("fio: <%s> skips replay of %d writes due to read-only\n", td->o.name, writes);
284 if (!reads && !writes)
286 else if (reads && !writes)
287 td->o.td_ddir = TD_DDIR_READ;
288 else if (!reads && writes)
289 td->o.td_ddir = TD_DDIR_WRITE;
291 td->o.td_ddir = TD_DDIR_RW;
297 * Read version 1 iolog data.
299 static int read_iolog(struct thread_data *td, FILE *f)
301 unsigned long long offset;
308 * Read in the read iolog and store it, reuse the infrastructure
309 * for doing verifications.
313 while ((p = fgets(str, 4096, f)) != NULL) {
314 struct io_piece *ipo;
316 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
317 log_err("bad iolog: %s\n", p);
322 else if (rw == DDIR_WRITE) {
325 * Don't add a write for ro mode
329 } else if (rw != DDIR_SYNC) {
330 log_err("bad ddir: %d\n", rw);
334 ipo = malloc(sizeof(*ipo));
335 memset(ipo, 0, sizeof(*ipo));
336 INIT_LIST_HEAD(&ipo->list);
337 ipo->offset = offset;
339 ipo->ddir = (enum fio_ddir) rw;
340 if (bytes > td->o.max_bs[rw])
341 td->o.max_bs[rw] = bytes;
342 list_add_tail(&ipo->list, &td->io_log_list);
343 td->total_io_size += bytes;
348 if (writes && read_only) {
349 log_err("fio: <%s> skips replay of %d writes due to read-only\n", td->o.name, writes);
353 if (!reads && !writes)
355 else if (reads && !writes)
356 td->o.td_ddir = TD_DDIR_READ;
357 else if (!reads && writes)
358 td->o.td_ddir = TD_DDIR_WRITE;
360 td->o.td_ddir = TD_DDIR_RW;
366 * open iolog, check version, and call appropriate parser
368 static int init_iolog_read(struct thread_data *td)
370 char buffer[256], *p;
374 f = fopen(td->o.read_iolog_file, "r");
376 perror("fopen read iolog");
380 p = fgets(buffer, sizeof(buffer), f);
382 td_verror(td, errno, "iolog read");
383 log_err("fio: unable to read iolog\n");
388 * version 2 of the iolog stores a specific string as the
389 * first line, check for that
391 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
392 ret = read_iolog2(td, f);
395 * seek back to the beginning
397 if (fseek(f, 0, SEEK_SET) < 0) {
398 td_verror(td, errno, "iolog read");
399 log_err("fio: unable to read iolog\n");
403 ret = read_iolog(td, f);
411 * Setup a log for storing io patterns.
413 static int init_iolog_write(struct thread_data *td)
419 f = fopen(td->o.write_iolog_file, "w+");
421 perror("fopen write iolog");
426 * That's it for writing, setup a log buffer and we're done.
429 td->iolog_buf = malloc(8192);
430 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
433 * write our version line
435 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
436 perror("iolog init\n");
441 * add all known files
443 for_each_file(td, ff, i)
444 log_file(td, ff, FIO_LOG_ADD_FILE);
449 int init_iolog(struct thread_data *td)
453 if (td->io_ops->flags & FIO_DISKLESSIO)
456 if (td->o.read_iolog_file) {
458 * Check if it's a blktrace file and load that if possible.
459 * Otherwise assume it's a normal log file and load that.
461 if (is_blktrace(td->o.read_iolog_file))
462 ret = load_blktrace(td, td->o.read_iolog_file);
464 ret = init_iolog_read(td);
465 } else if (td->o.write_iolog_file)
466 ret = init_iolog_write(td);
471 void setup_log(struct io_log **log)
473 struct io_log *l = malloc(sizeof(*l));
476 l->max_samples = 1024;
477 l->log = malloc(l->max_samples * sizeof(struct io_sample));
481 void __finish_log(struct io_log *log, const char *name)
486 f = fopen(name, "w");
492 for (i = 0; i < log->nr_samples; i++)
493 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
500 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
504 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
505 __finish_log(log, file_name);