netsplice: fix a few typos
[fio.git] / log.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "list.h"
4 #include "fio.h"
5
6 void 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
11 static 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
29 int 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;
39                 io_u->file = ipo->file;
40
41                 if (ipo->delay)
42                         iolog_delay(td, ipo->delay);
43
44                 /*
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];
49                 free(ipo);
50                 return 0;
51         }
52
53         return 1;
54 }
55
56 void prune_io_piece_log(struct thread_data *td)
57 {
58         struct io_piece *ipo;
59         struct rb_node *n;
60
61         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);
64                 free(ipo);
65         }
66 }
67
68 /*
69  * log a successful write, so we can unwind the log for verify
70  */
71 void log_io_piece(struct thread_data *td, struct io_u *io_u)
72 {
73         struct rb_node **p, *parent;
74         struct io_piece *ipo, *__ipo;
75
76         ipo = malloc(sizeof(struct io_piece));
77         ipo->file = io_u->file;
78         ipo->offset = io_u->offset;
79         ipo->len = io_u->buflen;
80
81         /*
82          * 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          */
90         if (!td_random(td) || !td->o.overwrite ||
91              (io_u->file->flags & FIO_FILE_NOSORT)) {
92                 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         /*
102          * Sort the entry into the verification list
103          */
104         while (*p) {
105                 parent = *p;
106
107                 __ipo = rb_entry(parent, struct io_piece, rb_node);
108                 if (ipo->offset <= __ipo->offset)
109                         p = &(*p)->rb_left;
110                 else
111                         p = &(*p)->rb_right;
112         }
113
114         rb_link_node(&ipo->rb_node, parent, p);
115         rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
116 }
117
118 void 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
125 /*
126  * Open a stored log and read in the entries.
127  */
128 static int init_iolog_read(struct thread_data *td)
129 {
130         unsigned long long offset;
131         unsigned int bytes;
132         char *str, *p;
133         FILE *f;
134         int rw, reads, writes;
135
136         f = fopen(td->o.read_iolog_file, "r");
137         if (!f) {
138                 perror("fopen read iolog");
139                 return 1;
140         }
141
142         /*
143          * Read in the read iolog and store it, reuse the infrastructure
144          * for doing verifications.
145          */
146         str = malloc(4096);
147         reads = writes = 0;
148         while ((p = fgets(str, 4096, f)) != NULL) {
149                 struct io_piece *ipo;
150
151                 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
152                         log_err("bad iolog: %s\n", p);
153                         continue;
154                 }
155                 if (rw == DDIR_READ)
156                         reads++;
157                 else if (rw == DDIR_WRITE)
158                         writes++;
159                 else if (rw != DDIR_SYNC) {
160                         log_err("bad ddir: %d\n", rw);
161                         continue;
162                 }
163
164                 ipo = malloc(sizeof(*ipo));
165                 memset(ipo, 0, sizeof(*ipo));
166                 INIT_LIST_HEAD(&ipo->list);
167                 ipo->offset = offset;
168                 ipo->len = bytes;
169                 ipo->ddir = (enum fio_ddir) rw;
170                 if (bytes > td->o.max_bs[rw])
171                         td->o.max_bs[rw] = bytes;
172                 list_add_tail(&ipo->list, &td->io_log_list);
173         }
174
175         free(str);
176         fclose(f);
177
178         if (!reads && !writes)
179                 return 1;
180         else if (reads && !writes)
181                 td->o.td_ddir = TD_DDIR_READ;
182         else if (!reads && writes)
183                 td->o.td_ddir = TD_DDIR_WRITE;
184         else
185                 td->o.td_ddir = TD_DDIR_RW;
186
187         return 0;
188 }
189
190 /*
191  * Setup a log for storing io patterns.
192  */
193 static int init_iolog_write(struct thread_data *td)
194 {
195         FILE *f;
196
197         if (td->o.nr_files > 1) {
198                 log_err("fio: write_iolog only works with 1 file currently\n");
199                 return 1;
200         }
201
202         f = fopen(td->o.write_iolog_file, "w+");
203         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
217 int init_iolog(struct thread_data *td)
218 {
219         int ret = 0;
220
221         if (td->io_ops->flags & FIO_DISKLESSIO)
222                 return 0;
223
224         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)
234                 ret = init_iolog_write(td);
235
236         return ret;
237 }
238
239 void 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
249 void __finish_log(struct io_log *log, const char *name)
250 {
251         unsigned int i;
252         FILE *f;
253
254         f = fopen(name, "w");
255         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 }
267
268 void 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 }