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)
61 if (!list_empty(&td->io_log_list)) {
62 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
64 io_u->offset = ipo->offset;
65 io_u->buflen = ipo->len;
66 io_u->ddir = ipo->ddir;
67 io_u->file = ipo->file;
70 * invalid ddir, this is a file action
72 if (io_u->ddir == DDIR_INVAL) {
73 struct fio_file *f = &td->files[ipo->fileno];
75 if (ipo->file_action == FIO_LOG_OPEN_FILE) {
76 assert(!td_io_open_file(td, f));
79 } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) {
80 td_io_close_file(td, f);
87 iolog_delay(td, ipo->delay);
90 * work around, this needs a format change to work for > 1 file
93 io_u->file = &td->files[0];
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);
114 * log a successful write, so we can unwind the log for verify
116 void log_io_piece(struct thread_data *td, struct io_u *io_u)
118 struct rb_node **p, *parent;
119 struct io_piece *ipo, *__ipo;
121 ipo = malloc(sizeof(struct io_piece));
122 ipo->file = io_u->file;
123 ipo->offset = io_u->offset;
124 ipo->len = io_u->buflen;
127 * We don't need to sort the entries, if:
129 * Sequential writes, or
130 * Random writes that lay out the file as it goes along
132 * For both these cases, just reading back data in the order we
133 * wrote it out is the fastest.
135 if (!td_random(td) || !td->o.overwrite ||
136 (io_u->file->flags & FIO_FILE_NOSORT)) {
137 INIT_LIST_HEAD(&ipo->list);
138 list_add_tail(&ipo->list, &td->io_hist_list);
142 RB_CLEAR_NODE(&ipo->rb_node);
143 p = &td->io_hist_tree.rb_node;
147 * Sort the entry into the verification list
152 __ipo = rb_entry(parent, struct io_piece, rb_node);
153 if (ipo->offset <= __ipo->offset)
159 rb_link_node(&ipo->rb_node, parent, p);
160 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
163 void write_iolog_close(struct thread_data *td)
169 td->iolog_buf = NULL;
173 * Read version 2 iolog data. It is enhanced to include per-file logging,
176 static int read_iolog2(struct thread_data *td, FILE *f)
178 unsigned long long offset;
180 int rw, reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
184 free_release_files(td);
187 * Read in the read iolog and store it, reuse the infrastructure
188 * for doing verifications.
191 fname = malloc(256+16);
192 act = malloc(256+16);
195 while ((p = fgets(str, 4096, f)) != NULL) {
196 struct io_piece *ipo;
199 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, &bytes);
204 if (!strcmp(act, "read"))
206 else if (!strcmp(act, "write"))
208 else if (!strcmp(act, "sync"))
211 log_err("fio: bad iolog file action: %s\n",act);
216 if (!strcmp(act, "add")) {
218 fileno = add_file(td, fname);
219 file_action = FIO_LOG_ADD_FILE;
221 } else if (!strcmp(act, "open")) {
222 fileno = get_fileno(td, fname);
223 file_action = FIO_LOG_OPEN_FILE;
224 } else if (!strcmp(act, "close")) {
225 fileno = get_fileno(td, fname);
226 file_action = FIO_LOG_CLOSE_FILE;
228 log_err("fio: bad iolog file action: %s\n",act);
232 log_err("bad iolog2: %s", p);
238 else if (rw == DDIR_WRITE)
240 else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
241 log_err("bad ddir: %d\n", rw);
248 ipo = malloc(sizeof(*ipo));
249 memset(ipo, 0, sizeof(*ipo));
250 INIT_LIST_HEAD(&ipo->list);
251 ipo->offset = offset;
253 ipo->ddir = (enum fio_ddir) rw;
254 if (bytes > td->o.max_bs[rw])
255 td->o.max_bs[rw] = bytes;
256 if (rw == DDIR_INVAL) {
257 ipo->fileno = fileno;
258 ipo->file_action = file_action;
260 list_add_tail(&ipo->list, &td->io_log_list);
267 if (!reads && !writes)
269 else if (reads && !writes)
270 td->o.td_ddir = TD_DDIR_READ;
271 else if (!reads && writes)
272 td->o.td_ddir = TD_DDIR_WRITE;
274 td->o.td_ddir = TD_DDIR_RW;
280 * Read version 1 iolog data.
282 static int read_iolog(struct thread_data *td, FILE *f)
284 unsigned long long offset;
287 int rw, reads, writes;
290 * Read in the read iolog and store it, reuse the infrastructure
291 * for doing verifications.
295 while ((p = fgets(str, 4096, f)) != NULL) {
296 struct io_piece *ipo;
298 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
299 log_err("bad iolog: %s\n", p);
304 else if (rw == DDIR_WRITE)
306 else if (rw != DDIR_SYNC) {
307 log_err("bad ddir: %d\n", rw);
311 ipo = malloc(sizeof(*ipo));
312 memset(ipo, 0, sizeof(*ipo));
313 INIT_LIST_HEAD(&ipo->list);
314 ipo->offset = offset;
316 ipo->ddir = (enum fio_ddir) rw;
317 if (bytes > td->o.max_bs[rw])
318 td->o.max_bs[rw] = bytes;
319 list_add_tail(&ipo->list, &td->io_log_list);
324 if (!reads && !writes)
326 else if (reads && !writes)
327 td->o.td_ddir = TD_DDIR_READ;
328 else if (!reads && writes)
329 td->o.td_ddir = TD_DDIR_WRITE;
331 td->o.td_ddir = TD_DDIR_RW;
337 * open iolog, check version, and call appropriate parser
339 static int init_iolog_read(struct thread_data *td)
341 char buffer[256], *p;
345 f = fopen(td->o.read_iolog_file, "r");
347 perror("fopen read iolog");
351 p = fgets(buffer, sizeof(buffer), f);
353 td_verror(td, errno, "iolog read");
354 log_err("fio: unable to read iolog\n");
359 * version 2 of the iolog stores a specific string as the
360 * first line, check for that
362 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
363 ret = read_iolog2(td, f);
366 * seek back to the beginning
368 if (fseek(f, 0, SEEK_SET) < 0) {
369 td_verror(td, errno, "iolog read");
370 log_err("fio: unable to read iolog\n");
374 ret = read_iolog(td, f);
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, log->log[i].ddir);
471 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
475 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
476 __finish_log(log, file_name);