Fix read_iolog
[fio.git] / log.c
... / ...
CommitLineData
1#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{
8 fprintf(td->iolog_f, "%u,%llu,%lu\n", io_u->ddir, io_u->offset, io_u->buflen);
9}
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;
21 io_u->file = ipo->file;
22 /*
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];
27 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;
37 struct rb_node *n;
38
39 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);
42 free(ipo);
43 }
44}
45
46/*
47 * log a successful write, so we can unwind the log for verify
48 */
49void log_io_piece(struct thread_data *td, struct io_u *io_u)
50{
51 struct rb_node **p, *parent;
52 struct io_piece *ipo, *__ipo;
53
54 ipo = malloc(sizeof(struct io_piece));
55 ipo->file = io_u->file;
56 ipo->offset = io_u->offset;
57 ipo->len = io_u->buflen;
58
59 /*
60 * 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 */
68 if (!td_random(td) || !td->o.overwrite ||
69 (io_u->file->flags & FIO_FILE_NOSORT)) {
70 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 /*
80 * Sort the entry into the verification list
81 */
82 while (*p) {
83 parent = *p;
84
85 __ipo = rb_entry(parent, struct io_piece, rb_node);
86 if (ipo->offset <= __ipo->offset)
87 p = &(*p)->rb_left;
88 else
89 p = &(*p)->rb_right;
90 }
91
92 rb_link_node(&ipo->rb_node, parent, p);
93 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
94}
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
103/*
104 * Open a stored log and read in the entries.
105 */
106static int init_iolog_read(struct thread_data *td)
107{
108 unsigned long long offset;
109 unsigned int bytes;
110 char *str, *p;
111 FILE *f;
112 int rw, reads, writes;
113
114 f = fopen(td->o.read_iolog_file, "r");
115 if (!f) {
116 perror("fopen read iolog");
117 return 1;
118 }
119
120 /*
121 * Read in the read iolog and store it, reuse the infrastructure
122 * for doing verifications.
123 */
124 str = malloc(4096);
125 reads = writes = 0;
126 while ((p = fgets(str, 4096, f)) != NULL) {
127 struct io_piece *ipo;
128
129 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
130 log_err("bad iolog: %s\n", p);
131 continue;
132 }
133 if (rw == DDIR_READ)
134 reads++;
135 else if (rw == DDIR_WRITE)
136 writes++;
137 else if (rw != DDIR_SYNC) {
138 log_err("bad ddir: %d\n", rw);
139 continue;
140 }
141
142 ipo = malloc(sizeof(*ipo));
143 INIT_LIST_HEAD(&ipo->list);
144 ipo->offset = offset;
145 ipo->len = bytes;
146 ipo->ddir = (enum fio_ddir) rw;
147 if (bytes > td->o.max_bs[rw])
148 td->o.max_bs[rw] = bytes;
149 list_add_tail(&ipo->list, &td->io_log_list);
150 }
151
152 free(str);
153 fclose(f);
154
155 if (!reads && !writes)
156 return 1;
157 else if (reads && !writes)
158 td->o.td_ddir = TD_DDIR_READ;
159 else if (!reads && writes)
160 td->o.td_ddir = TD_DDIR_READ;
161 else
162 td->o.td_ddir = TD_DDIR_RW;
163
164 return 0;
165}
166
167/*
168 * Setup a log for storing io patterns.
169 */
170static int init_iolog_write(struct thread_data *td)
171{
172 FILE *f;
173
174 f = fopen(td->o.write_iolog_file, "w+");
175 if (!f) {
176 perror("fopen write iolog");
177 return 1;
178 }
179
180 /*
181 * That's it for writing, setup a log buffer and we're done.
182 */
183 td->iolog_f = f;
184 td->iolog_buf = malloc(8192);
185 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
186 return 0;
187}
188
189int init_iolog(struct thread_data *td)
190{
191 int ret = 0;
192
193 if (td->io_ops->flags & FIO_DISKLESSIO)
194 return 0;
195
196 if (td->o.read_iolog_file)
197 ret = init_iolog_read(td);
198 else if (td->o.write_iolog_file)
199 ret = init_iolog_write(td);
200
201 return ret;
202}
203
204void setup_log(struct io_log **log)
205{
206 struct io_log *l = malloc(sizeof(*l));
207
208 l->nr_samples = 0;
209 l->max_samples = 1024;
210 l->log = malloc(l->max_samples * sizeof(struct io_sample));
211 *log = l;
212}
213
214void __finish_log(struct io_log *log, const char *name)
215{
216 unsigned int i;
217 FILE *f;
218
219 f = fopen(name, "w");
220 if (!f) {
221 perror("fopen log");
222 return;
223 }
224
225 for (i = 0; i < log->nr_samples; i++)
226 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
227
228 fclose(f);
229 free(log->log);
230 free(log);
231}
232
233void finish_log(struct thread_data *td, struct io_log *log, const char *name)
234{
235 char file_name[256];
236
237 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
238 __finish_log(log, file_name);
239}