blob: d59c38fd4e7cee46aa8f199b7f570e8c93b23501 [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 Axboe3c39a372006-06-06 20:56:12 +020022 free(ipo);
23 return 0;
24 }
25
26 return 1;
27}
28
29void prune_io_piece_log(struct thread_data *td)
30{
31 struct io_piece *ipo;
Jens Axboe4b878982007-03-26 09:32:22 +020032 struct rb_node *n;
Jens Axboe3c39a372006-06-06 20:56:12 +020033
Jens Axboe4b878982007-03-26 09:32:22 +020034 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
35 ipo = rb_entry(n, struct io_piece, rb_node);
36 rb_erase(n, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +020037 free(ipo);
38 }
39}
40
41/*
Jens Axboe34403fb2007-03-02 21:43:25 +010042 * log a successful write, so we can unwind the log for verify
Jens Axboe3c39a372006-06-06 20:56:12 +020043 */
44void log_io_piece(struct thread_data *td, struct io_u *io_u)
45{
Jens Axboe4b878982007-03-26 09:32:22 +020046 struct rb_node **p = &td->io_hist_tree.rb_node;
47 struct rb_node *parent = NULL;
48 struct io_piece *ipo, *__ipo;
Jens Axboe3c39a372006-06-06 20:56:12 +020049
Jens Axboe4b878982007-03-26 09:32:22 +020050 ipo = malloc(sizeof(struct io_piece));
51 memset(&ipo->rb_node, 0, sizeof(ipo->rb_node));
Jens Axboe53cdc682006-10-18 11:50:58 +020052 ipo->file = io_u->file;
Jens Axboe3c39a372006-06-06 20:56:12 +020053 ipo->offset = io_u->offset;
54 ipo->len = io_u->buflen;
55
56 /*
Jens Axboe4b878982007-03-26 09:32:22 +020057 * Sort the entry into the verification list
Jens Axboe3c39a372006-06-06 20:56:12 +020058 */
Jens Axboe4b878982007-03-26 09:32:22 +020059 while (*p) {
60 parent = *p;
Jens Axboe3c39a372006-06-06 20:56:12 +020061
Jens Axboe4b878982007-03-26 09:32:22 +020062 __ipo = rb_entry(parent, struct io_piece, rb_node);
63 if (ipo->offset < __ipo->offset)
64 p = &(*p)->rb_left;
65 else if (ipo->offset > __ipo->offset)
66 p = &(*p)->rb_right;
67 else
Jens Axboe3c39a372006-06-06 20:56:12 +020068 break;
69 }
70
Jens Axboe4b878982007-03-26 09:32:22 +020071 rb_link_node(&ipo->rb_node, parent, p);
72 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +020073}
74
75void write_iolog_close(struct thread_data *td)
76{
77 fflush(td->iolog_f);
78 fclose(td->iolog_f);
79 free(td->iolog_buf);
80}
81
Jens Axboefb71fbd2006-10-20 09:15:46 +020082/*
83 * Open a stored log and read in the entries.
84 */
85static int init_iolog_read(struct thread_data *td)
Jens Axboe3c39a372006-06-06 20:56:12 +020086{
87 unsigned long long offset;
88 unsigned int bytes;
89 char *str, *p;
90 FILE *f;
Jens Axboefb71fbd2006-10-20 09:15:46 +020091 int rw, reads, writes;
Jens Axboe3c39a372006-06-06 20:56:12 +020092
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010093 f = fopen(td->o.read_iolog_file, "r");
Jens Axboe3c39a372006-06-06 20:56:12 +020094 if (!f) {
Jens Axboefb71fbd2006-10-20 09:15:46 +020095 perror("fopen read iolog");
Jens Axboe3c39a372006-06-06 20:56:12 +020096 return 1;
97 }
98
99 /*
Jens Axboe3c39a372006-06-06 20:56:12 +0200100 * Read in the read iolog and store it, reuse the infrastructure
101 * for doing verifications.
102 */
103 str = malloc(4096);
Jens Axboefb71fbd2006-10-20 09:15:46 +0200104 reads = writes = 0;
Jens Axboe3c39a372006-06-06 20:56:12 +0200105 while ((p = fgets(str, 4096, f)) != NULL) {
106 struct io_piece *ipo;
107
108 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200109 log_err("bad iolog: %s\n", p);
Jens Axboe3c39a372006-06-06 20:56:12 +0200110 continue;
111 }
112 if (rw == DDIR_READ)
113 reads++;
114 else if (rw == DDIR_WRITE)
115 writes++;
Jens Axboec38e9462007-03-27 08:48:48 +0200116 else if (rw != DDIR_SYNC) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200117 log_err("bad ddir: %d\n", rw);
Jens Axboe3c39a372006-06-06 20:56:12 +0200118 continue;
119 }
120
121 ipo = malloc(sizeof(*ipo));
122 INIT_LIST_HEAD(&ipo->list);
123 ipo->offset = offset;
124 ipo->len = bytes;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100125 ipo->ddir = (enum fio_ddir) rw;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100126 if (bytes > td->o.max_bs[rw])
127 td->o.max_bs[rw] = bytes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200128 list_add_tail(&ipo->list, &td->io_log_list);
Jens Axboe3c39a372006-06-06 20:56:12 +0200129 }
130
131 free(str);
132 fclose(f);
133
Jens Axboefb71fbd2006-10-20 09:15:46 +0200134 if (!reads && !writes)
Jens Axboe3c39a372006-06-06 20:56:12 +0200135 return 1;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200136 else if (reads && !writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100137 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200138 else if (!reads && writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100139 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200140 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100141 td->o.td_ddir = TD_DDIR_RW;
Jens Axboe3c39a372006-06-06 20:56:12 +0200142
143 return 0;
144}
Jens Axboe8914a9d2006-06-07 11:14:56 +0200145
Jens Axboefb71fbd2006-10-20 09:15:46 +0200146/*
147 * Setup a log for storing io patterns.
148 */
149static int init_iolog_write(struct thread_data *td)
150{
Jens Axboe076efc72006-10-27 11:24:25 +0200151 FILE *f;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200152
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100153 f = fopen(td->o.write_iolog_file, "w+");
Jens Axboefb71fbd2006-10-20 09:15:46 +0200154 if (!f) {
155 perror("fopen write iolog");
156 return 1;
157 }
158
159 /*
160 * That's it for writing, setup a log buffer and we're done.
161 */
162 td->iolog_f = f;
163 td->iolog_buf = malloc(8192);
164 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
165 return 0;
166}
167
168int init_iolog(struct thread_data *td)
169{
Jens Axboeb4a6a592006-10-20 13:54:47 +0200170 int ret = 0;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200171
Jens Axboeba0fbe12007-03-09 14:34:23 +0100172 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200173 return 0;
174
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100175 if (td->o.read_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200176 ret = init_iolog_read(td);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100177 else if (td->o.write_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200178 ret = init_iolog_write(td);
179
Jens Axboe1e97cce2006-12-05 11:44:16 +0100180 return ret;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200181}
182
Jens Axboe8914a9d2006-06-07 11:14:56 +0200183void setup_log(struct io_log **log)
184{
185 struct io_log *l = malloc(sizeof(*l));
186
187 l->nr_samples = 0;
188 l->max_samples = 1024;
189 l->log = malloc(l->max_samples * sizeof(struct io_sample));
190 *log = l;
191}
192
Jens Axboebb3884d2007-01-17 17:23:11 +1100193void __finish_log(struct io_log *log, const char *name)
Jens Axboe8914a9d2006-06-07 11:14:56 +0200194{
Jens Axboe8914a9d2006-06-07 11:14:56 +0200195 unsigned int i;
Jens Axboebb3884d2007-01-17 17:23:11 +1100196 FILE *f;
Jens Axboe8914a9d2006-06-07 11:14:56 +0200197
Jens Axboebb3884d2007-01-17 17:23:11 +1100198 f = fopen(name, "w");
Jens Axboe8914a9d2006-06-07 11:14:56 +0200199 if (!f) {
200 perror("fopen log");
201 return;
202 }
203
204 for (i = 0; i < log->nr_samples; i++)
205 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
206
207 fclose(f);
208 free(log->log);
209 free(log);
210}
Jens Axboebb3884d2007-01-17 17:23:11 +1100211
212void finish_log(struct thread_data *td, struct io_log *log, const char *name)
213{
214 char file_name[256];
215
216 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
217 __finish_log(log, file_name);
218}