blob: 9669b4a7c336eb510a510679c7933d9e96c45bd1 [file] [log] [blame]
Jens Axboe3c39a372006-06-06 20:56:12 +02001#include <stdio.h>
2#include <stdlib.h>
3#include "list.h"
4#include "fio.h"
5
6void write_iolog_put(struct thread_data *td, struct io_u *io_u)
7{
Jens Axboea4f4fdd2007-02-14 01:16:39 +01008 fprintf(td->iolog_f, "%u,%llu,%lu\n", io_u->ddir, io_u->offset, io_u->buflen);
Jens Axboe3c39a372006-06-06 20:56:12 +02009}
10
11int read_iolog_get(struct thread_data *td, struct io_u *io_u)
12{
13 struct io_piece *ipo;
14
15 if (!list_empty(&td->io_log_list)) {
16 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
17 list_del(&ipo->list);
18 io_u->offset = ipo->offset;
19 io_u->buflen = ipo->len;
20 io_u->ddir = ipo->ddir;
Jens Axboe53cdc682006-10-18 11:50:58 +020021 io_u->file = ipo->file;
Jens Axboefcb11702007-04-25 14:11:18 +020022 /*
23 * work around, this needs a format change to work for > 1 file
24 */
25 if (!io_u->file)
26 io_u->file = &td->files[0];
Jens Axboe3c39a372006-06-06 20:56:12 +020027 free(ipo);
28 return 0;
29 }
30
31 return 1;
32}
33
34void prune_io_piece_log(struct thread_data *td)
35{
36 struct io_piece *ipo;
Jens Axboe4b878982007-03-26 09:32:22 +020037 struct rb_node *n;
Jens Axboe3c39a372006-06-06 20:56:12 +020038
Jens Axboe4b878982007-03-26 09:32:22 +020039 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
40 ipo = rb_entry(n, struct io_piece, rb_node);
41 rb_erase(n, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +020042 free(ipo);
43 }
44}
45
46/*
Jens Axboe34403fb2007-03-02 21:43:25 +010047 * log a successful write, so we can unwind the log for verify
Jens Axboe3c39a372006-06-06 20:56:12 +020048 */
49void log_io_piece(struct thread_data *td, struct io_u *io_u)
50{
Jens Axboe8de8f042007-03-27 10:36:12 +020051 struct rb_node **p, *parent;
Jens Axboe4b878982007-03-26 09:32:22 +020052 struct io_piece *ipo, *__ipo;
Jens Axboe3c39a372006-06-06 20:56:12 +020053
Jens Axboe4b878982007-03-26 09:32:22 +020054 ipo = malloc(sizeof(struct io_piece));
Jens Axboe53cdc682006-10-18 11:50:58 +020055 ipo->file = io_u->file;
Jens Axboe3c39a372006-06-06 20:56:12 +020056 ipo->offset = io_u->offset;
57 ipo->len = io_u->buflen;
58
59 /*
Jens Axboe8de8f042007-03-27 10:36:12 +020060 * We don't need to sort the entries, if:
61 *
62 * Sequential writes, or
63 * Random writes that lay out the file as it goes along
64 *
65 * For both these cases, just reading back data in the order we
66 * wrote it out is the fastest.
67 */
Jens Axboe160b9662007-03-27 10:59:49 +020068 if (!td_random(td) || !td->o.overwrite ||
69 (io_u->file->flags & FIO_FILE_NOSORT)) {
Jens Axboe8de8f042007-03-27 10:36:12 +020070 INIT_LIST_HEAD(&ipo->list);
71 list_add_tail(&ipo->list, &td->io_hist_list);
72 return;
73 }
74
75 RB_CLEAR_NODE(&ipo->rb_node);
76 p = &td->io_hist_tree.rb_node;
77 parent = NULL;
78
79 /*
Jens Axboe4b878982007-03-26 09:32:22 +020080 * Sort the entry into the verification list
Jens Axboe3c39a372006-06-06 20:56:12 +020081 */
Jens Axboe4b878982007-03-26 09:32:22 +020082 while (*p) {
83 parent = *p;
Jens Axboe3c39a372006-06-06 20:56:12 +020084
Jens Axboe4b878982007-03-26 09:32:22 +020085 __ipo = rb_entry(parent, struct io_piece, rb_node);
Jens Axboebb5d7d02007-03-27 10:21:25 +020086 if (ipo->offset <= __ipo->offset)
Jens Axboe4b878982007-03-26 09:32:22 +020087 p = &(*p)->rb_left;
Jens Axboe4b878982007-03-26 09:32:22 +020088 else
Jens Axboebb5d7d02007-03-27 10:21:25 +020089 p = &(*p)->rb_right;
Jens Axboe3c39a372006-06-06 20:56:12 +020090 }
91
Jens Axboe4b878982007-03-26 09:32:22 +020092 rb_link_node(&ipo->rb_node, parent, p);
93 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +020094}
95
96void write_iolog_close(struct thread_data *td)
97{
98 fflush(td->iolog_f);
99 fclose(td->iolog_f);
100 free(td->iolog_buf);
101}
102
Jens Axboefb71fbd2006-10-20 09:15:46 +0200103/*
104 * Open a stored log and read in the entries.
105 */
106static int init_iolog_read(struct thread_data *td)
Jens Axboe3c39a372006-06-06 20:56:12 +0200107{
108 unsigned long long offset;
109 unsigned int bytes;
110 char *str, *p;
111 FILE *f;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200112 int rw, reads, writes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200113
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100114 f = fopen(td->o.read_iolog_file, "r");
Jens Axboe3c39a372006-06-06 20:56:12 +0200115 if (!f) {
Jens Axboefb71fbd2006-10-20 09:15:46 +0200116 perror("fopen read iolog");
Jens Axboe3c39a372006-06-06 20:56:12 +0200117 return 1;
118 }
119
120 /*
Jens Axboe3c39a372006-06-06 20:56:12 +0200121 * Read in the read iolog and store it, reuse the infrastructure
122 * for doing verifications.
123 */
124 str = malloc(4096);
Jens Axboefb71fbd2006-10-20 09:15:46 +0200125 reads = writes = 0;
Jens Axboe3c39a372006-06-06 20:56:12 +0200126 while ((p = fgets(str, 4096, f)) != NULL) {
127 struct io_piece *ipo;
128
129 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200130 log_err("bad iolog: %s\n", p);
Jens Axboe3c39a372006-06-06 20:56:12 +0200131 continue;
132 }
133 if (rw == DDIR_READ)
134 reads++;
135 else if (rw == DDIR_WRITE)
136 writes++;
Jens Axboec38e9462007-03-27 08:48:48 +0200137 else if (rw != DDIR_SYNC) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200138 log_err("bad ddir: %d\n", rw);
Jens Axboe3c39a372006-06-06 20:56:12 +0200139 continue;
140 }
141
142 ipo = malloc(sizeof(*ipo));
Jens Axboe733ed592007-04-25 14:24:12 +0200143 memset(ipo, 0, sizeof(*ipo));
Jens Axboe3c39a372006-06-06 20:56:12 +0200144 INIT_LIST_HEAD(&ipo->list);
145 ipo->offset = offset;
146 ipo->len = bytes;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100147 ipo->ddir = (enum fio_ddir) rw;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100148 if (bytes > td->o.max_bs[rw])
149 td->o.max_bs[rw] = bytes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200150 list_add_tail(&ipo->list, &td->io_log_list);
Jens Axboe3c39a372006-06-06 20:56:12 +0200151 }
152
153 free(str);
154 fclose(f);
155
Jens Axboefb71fbd2006-10-20 09:15:46 +0200156 if (!reads && !writes)
Jens Axboe3c39a372006-06-06 20:56:12 +0200157 return 1;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200158 else if (reads && !writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100159 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200160 else if (!reads && writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100161 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200162 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100163 td->o.td_ddir = TD_DDIR_RW;
Jens Axboe3c39a372006-06-06 20:56:12 +0200164
165 return 0;
166}
Jens Axboe8914a9d2006-06-07 11:14:56 +0200167
Jens Axboefb71fbd2006-10-20 09:15:46 +0200168/*
169 * Setup a log for storing io patterns.
170 */
171static int init_iolog_write(struct thread_data *td)
172{
Jens Axboe076efc72006-10-27 11:24:25 +0200173 FILE *f;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200174
Jens Axboe733ed592007-04-25 14:24:12 +0200175 if (td->o.nr_files > 1) {
176 log_err("fio: write_iolog only works with 1 file currently\n");
177 return 1;
178 }
179
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100180 f = fopen(td->o.write_iolog_file, "w+");
Jens Axboefb71fbd2006-10-20 09:15:46 +0200181 if (!f) {
182 perror("fopen write iolog");
183 return 1;
184 }
185
186 /*
187 * That's it for writing, setup a log buffer and we're done.
188 */
189 td->iolog_f = f;
190 td->iolog_buf = malloc(8192);
191 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
192 return 0;
193}
194
195int init_iolog(struct thread_data *td)
196{
Jens Axboeb4a6a592006-10-20 13:54:47 +0200197 int ret = 0;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200198
Jens Axboeba0fbe12007-03-09 14:34:23 +0100199 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200200 return 0;
201
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100202 if (td->o.read_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200203 ret = init_iolog_read(td);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100204 else if (td->o.write_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200205 ret = init_iolog_write(td);
206
Jens Axboe1e97cce2006-12-05 11:44:16 +0100207 return ret;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200208}
209
Jens Axboe8914a9d2006-06-07 11:14:56 +0200210void setup_log(struct io_log **log)
211{
212 struct io_log *l = malloc(sizeof(*l));
213
214 l->nr_samples = 0;
215 l->max_samples = 1024;
216 l->log = malloc(l->max_samples * sizeof(struct io_sample));
217 *log = l;
218}
219
Jens Axboebb3884d2007-01-17 17:23:11 +1100220void __finish_log(struct io_log *log, const char *name)
Jens Axboe8914a9d2006-06-07 11:14:56 +0200221{
Jens Axboe8914a9d2006-06-07 11:14:56 +0200222 unsigned int i;
Jens Axboebb3884d2007-01-17 17:23:11 +1100223 FILE *f;
Jens Axboe8914a9d2006-06-07 11:14:56 +0200224
Jens Axboebb3884d2007-01-17 17:23:11 +1100225 f = fopen(name, "w");
Jens Axboe8914a9d2006-06-07 11:14:56 +0200226 if (!f) {
227 perror("fopen log");
228 return;
229 }
230
231 for (i = 0; i < log->nr_samples; i++)
232 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
233
234 fclose(f);
235 free(log->log);
236 free(log);
237}
Jens Axboebb3884d2007-01-17 17:23:11 +1100238
239void finish_log(struct thread_data *td, struct io_log *log, const char *name)
240{
241 char file_name[256];
242
243 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
244 __finish_log(log, file_name);
245}