blob: 5ed30eb6f103efb8ed6150d04a807a9dc018ef63 [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
Jens Axboea61edde2007-05-15 14:29:58 +020011static void iolog_delay(struct thread_data *td, unsigned long delay)
12{
13 unsigned long usec = utime_since_now(&td->last_issue);
14
15 if (delay < usec)
16 return;
17
18 delay -= usec;
19
20 /*
21 * less than 100 usec delay, just regard it as noise
22 */
23 if (delay < 100)
24 return;
25
26 usec_sleep(td, delay);
27}
28
Jens Axboe3c39a372006-06-06 20:56:12 +020029int read_iolog_get(struct thread_data *td, struct io_u *io_u)
30{
31 struct io_piece *ipo;
32
33 if (!list_empty(&td->io_log_list)) {
34 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
35 list_del(&ipo->list);
36 io_u->offset = ipo->offset;
37 io_u->buflen = ipo->len;
38 io_u->ddir = ipo->ddir;
Jens Axboe53cdc682006-10-18 11:50:58 +020039 io_u->file = ipo->file;
Jens Axboea61edde2007-05-15 14:29:58 +020040
41 if (ipo->delay)
42 iolog_delay(td, ipo->delay);
43
Jens Axboefcb11702007-04-25 14:11:18 +020044 /*
45 * work around, this needs a format change to work for > 1 file
46 */
47 if (!io_u->file)
48 io_u->file = &td->files[0];
Jens Axboe3c39a372006-06-06 20:56:12 +020049 free(ipo);
50 return 0;
51 }
52
53 return 1;
54}
55
56void prune_io_piece_log(struct thread_data *td)
57{
58 struct io_piece *ipo;
Jens Axboe4b878982007-03-26 09:32:22 +020059 struct rb_node *n;
Jens Axboe3c39a372006-06-06 20:56:12 +020060
Jens Axboe4b878982007-03-26 09:32:22 +020061 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
62 ipo = rb_entry(n, struct io_piece, rb_node);
63 rb_erase(n, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +020064 free(ipo);
65 }
66}
67
68/*
Jens Axboe34403fb2007-03-02 21:43:25 +010069 * log a successful write, so we can unwind the log for verify
Jens Axboe3c39a372006-06-06 20:56:12 +020070 */
71void log_io_piece(struct thread_data *td, struct io_u *io_u)
72{
Jens Axboe8de8f042007-03-27 10:36:12 +020073 struct rb_node **p, *parent;
Jens Axboe4b878982007-03-26 09:32:22 +020074 struct io_piece *ipo, *__ipo;
Jens Axboe3c39a372006-06-06 20:56:12 +020075
Jens Axboe4b878982007-03-26 09:32:22 +020076 ipo = malloc(sizeof(struct io_piece));
Jens Axboe53cdc682006-10-18 11:50:58 +020077 ipo->file = io_u->file;
Jens Axboe3c39a372006-06-06 20:56:12 +020078 ipo->offset = io_u->offset;
79 ipo->len = io_u->buflen;
80
81 /*
Jens Axboe8de8f042007-03-27 10:36:12 +020082 * We don't need to sort the entries, if:
83 *
84 * Sequential writes, or
85 * Random writes that lay out the file as it goes along
86 *
87 * For both these cases, just reading back data in the order we
88 * wrote it out is the fastest.
89 */
Jens Axboe160b9662007-03-27 10:59:49 +020090 if (!td_random(td) || !td->o.overwrite ||
91 (io_u->file->flags & FIO_FILE_NOSORT)) {
Jens Axboe8de8f042007-03-27 10:36:12 +020092 INIT_LIST_HEAD(&ipo->list);
93 list_add_tail(&ipo->list, &td->io_hist_list);
94 return;
95 }
96
97 RB_CLEAR_NODE(&ipo->rb_node);
98 p = &td->io_hist_tree.rb_node;
99 parent = NULL;
100
101 /*
Jens Axboe4b878982007-03-26 09:32:22 +0200102 * Sort the entry into the verification list
Jens Axboe3c39a372006-06-06 20:56:12 +0200103 */
Jens Axboe4b878982007-03-26 09:32:22 +0200104 while (*p) {
105 parent = *p;
Jens Axboe3c39a372006-06-06 20:56:12 +0200106
Jens Axboe4b878982007-03-26 09:32:22 +0200107 __ipo = rb_entry(parent, struct io_piece, rb_node);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200108 if (ipo->offset <= __ipo->offset)
Jens Axboe4b878982007-03-26 09:32:22 +0200109 p = &(*p)->rb_left;
Jens Axboe4b878982007-03-26 09:32:22 +0200110 else
Jens Axboebb5d7d02007-03-27 10:21:25 +0200111 p = &(*p)->rb_right;
Jens Axboe3c39a372006-06-06 20:56:12 +0200112 }
113
Jens Axboe4b878982007-03-26 09:32:22 +0200114 rb_link_node(&ipo->rb_node, parent, p);
115 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +0200116}
117
118void write_iolog_close(struct thread_data *td)
119{
120 fflush(td->iolog_f);
121 fclose(td->iolog_f);
122 free(td->iolog_buf);
123}
124
Jens Axboefb71fbd2006-10-20 09:15:46 +0200125/*
126 * Open a stored log and read in the entries.
127 */
128static int init_iolog_read(struct thread_data *td)
Jens Axboe3c39a372006-06-06 20:56:12 +0200129{
130 unsigned long long offset;
131 unsigned int bytes;
132 char *str, *p;
133 FILE *f;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200134 int rw, reads, writes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200135
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100136 f = fopen(td->o.read_iolog_file, "r");
Jens Axboe3c39a372006-06-06 20:56:12 +0200137 if (!f) {
Jens Axboefb71fbd2006-10-20 09:15:46 +0200138 perror("fopen read iolog");
Jens Axboe3c39a372006-06-06 20:56:12 +0200139 return 1;
140 }
141
142 /*
Jens Axboe3c39a372006-06-06 20:56:12 +0200143 * Read in the read iolog and store it, reuse the infrastructure
144 * for doing verifications.
145 */
146 str = malloc(4096);
Jens Axboefb71fbd2006-10-20 09:15:46 +0200147 reads = writes = 0;
Jens Axboe3c39a372006-06-06 20:56:12 +0200148 while ((p = fgets(str, 4096, f)) != NULL) {
149 struct io_piece *ipo;
150
151 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200152 log_err("bad iolog: %s\n", p);
Jens Axboe3c39a372006-06-06 20:56:12 +0200153 continue;
154 }
155 if (rw == DDIR_READ)
156 reads++;
157 else if (rw == DDIR_WRITE)
158 writes++;
Jens Axboec38e9462007-03-27 08:48:48 +0200159 else if (rw != DDIR_SYNC) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200160 log_err("bad ddir: %d\n", rw);
Jens Axboe3c39a372006-06-06 20:56:12 +0200161 continue;
162 }
163
164 ipo = malloc(sizeof(*ipo));
Jens Axboe733ed592007-04-25 14:24:12 +0200165 memset(ipo, 0, sizeof(*ipo));
Jens Axboe3c39a372006-06-06 20:56:12 +0200166 INIT_LIST_HEAD(&ipo->list);
167 ipo->offset = offset;
168 ipo->len = bytes;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100169 ipo->ddir = (enum fio_ddir) rw;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100170 if (bytes > td->o.max_bs[rw])
171 td->o.max_bs[rw] = bytes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200172 list_add_tail(&ipo->list, &td->io_log_list);
Jens Axboe3c39a372006-06-06 20:56:12 +0200173 }
174
175 free(str);
176 fclose(f);
177
Jens Axboefb71fbd2006-10-20 09:15:46 +0200178 if (!reads && !writes)
Jens Axboe3c39a372006-06-06 20:56:12 +0200179 return 1;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200180 else if (reads && !writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100181 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200182 else if (!reads && writes)
Jens Axboe36361eb2007-05-15 11:12:19 +0200183 td->o.td_ddir = TD_DDIR_WRITE;
Jens Axboe3c39a372006-06-06 20:56:12 +0200184 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100185 td->o.td_ddir = TD_DDIR_RW;
Jens Axboe3c39a372006-06-06 20:56:12 +0200186
187 return 0;
188}
Jens Axboe8914a9d2006-06-07 11:14:56 +0200189
Jens Axboefb71fbd2006-10-20 09:15:46 +0200190/*
191 * Setup a log for storing io patterns.
192 */
193static int init_iolog_write(struct thread_data *td)
194{
Jens Axboe076efc72006-10-27 11:24:25 +0200195 FILE *f;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200196
Jens Axboe733ed592007-04-25 14:24:12 +0200197 if (td->o.nr_files > 1) {
198 log_err("fio: write_iolog only works with 1 file currently\n");
199 return 1;
200 }
201
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100202 f = fopen(td->o.write_iolog_file, "w+");
Jens Axboefb71fbd2006-10-20 09:15:46 +0200203 if (!f) {
204 perror("fopen write iolog");
205 return 1;
206 }
207
208 /*
209 * That's it for writing, setup a log buffer and we're done.
210 */
211 td->iolog_f = f;
212 td->iolog_buf = malloc(8192);
213 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
214 return 0;
215}
216
217int init_iolog(struct thread_data *td)
218{
Jens Axboeb4a6a592006-10-20 13:54:47 +0200219 int ret = 0;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200220
Jens Axboeba0fbe12007-03-09 14:34:23 +0100221 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200222 return 0;
223
Jens Axboefb7b71a2007-05-15 08:44:04 +0200224 if (td->o.read_iolog_file) {
225 /*
226 * Check if it's a blktrace file and load that if possible.
227 * Otherwise assume it's a normal log file and load that.
228 */
229 if (is_blktrace(td->o.read_iolog_file))
230 ret = load_blktrace(td, td->o.read_iolog_file);
231 else
232 ret = init_iolog_read(td);
233 } else if (td->o.write_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200234 ret = init_iolog_write(td);
235
Jens Axboe1e97cce2006-12-05 11:44:16 +0100236 return ret;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200237}
238
Jens Axboe8914a9d2006-06-07 11:14:56 +0200239void setup_log(struct io_log **log)
240{
241 struct io_log *l = malloc(sizeof(*l));
242
243 l->nr_samples = 0;
244 l->max_samples = 1024;
245 l->log = malloc(l->max_samples * sizeof(struct io_sample));
246 *log = l;
247}
248
Jens Axboebb3884d2007-01-17 17:23:11 +1100249void __finish_log(struct io_log *log, const char *name)
Jens Axboe8914a9d2006-06-07 11:14:56 +0200250{
Jens Axboe8914a9d2006-06-07 11:14:56 +0200251 unsigned int i;
Jens Axboebb3884d2007-01-17 17:23:11 +1100252 FILE *f;
Jens Axboe8914a9d2006-06-07 11:14:56 +0200253
Jens Axboebb3884d2007-01-17 17:23:11 +1100254 f = fopen(name, "w");
Jens Axboe8914a9d2006-06-07 11:14:56 +0200255 if (!f) {
256 perror("fopen log");
257 return;
258 }
259
260 for (i = 0; i < log->nr_samples; i++)
261 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
262
263 fclose(f);
264 free(log->log);
265 free(log);
266}
Jens Axboebb3884d2007-01-17 17:23:11 +1100267
268void finish_log(struct thread_data *td, struct io_log *log, const char *name)
269{
270 char file_name[256];
271
272 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
273 __finish_log(log, file_name);
274}