b5c6f690d4572770557462cad1b80201317fb814
[fio.git] / log.c
1 /*
2  * Code related to writing an iolog of what a thread is doing, and to
3  * later read that back and replay
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include "list.h"
9 #include "fio.h"
10
11 static const char iolog_ver2[] = "fio version 2 iolog";
12
13 void log_io_u(struct thread_data *td, struct io_u *io_u)
14 {
15         const char *act[] = { "read", "write", "sync" };
16
17         assert(io_u->ddir < 3);
18
19         if (!td->o.write_iolog_file)
20                 return;
21
22         fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name, act[io_u->ddir], io_u->offset, io_u->buflen);
23 }
24
25 void log_file(struct thread_data *td, struct fio_file *f,
26               enum file_log_act what)
27 {
28         const char *act[] = { "add", "open", "close" };
29
30         assert(what < 3);
31
32         if (!td->o.write_iolog_file)
33                 return;
34
35         fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
36 }
37
38 static void iolog_delay(struct thread_data *td, unsigned long delay)
39 {
40         unsigned long usec = utime_since_now(&td->last_issue);
41
42         if (delay < usec)
43                 return;
44
45         delay -= usec;
46
47         /*
48          * less than 100 usec delay, just regard it as noise
49          */
50         if (delay < 100)
51                 return;
52
53         usec_sleep(td, delay);
54 }
55
56 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
57 {
58         struct io_piece *ipo;
59
60 restart:
61         if (!list_empty(&td->io_log_list)) {
62                 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
63                 list_del(&ipo->list);
64
65                 /*
66                  * invalid ddir, this is a file action
67                  */
68                 if (ipo->ddir == DDIR_INVAL) {
69                         struct fio_file *f = &td->files[ipo->fileno];
70
71                         if (ipo->file_action == FIO_LOG_OPEN_FILE) {
72                                 assert(!td_io_open_file(td, f));
73                                 free(ipo);
74                                 goto restart;
75                         } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) {
76                                 td_io_close_file(td, f);
77                                 free(ipo);
78                                 goto restart;
79                         }
80                 }
81
82                 io_u->offset = ipo->offset;
83                 io_u->buflen = ipo->len;
84                 io_u->ddir = ipo->ddir;
85                 io_u->file = &td->files[ipo->fileno];
86                 get_file(io_u->file);
87
88                 if (ipo->delay)
89                         iolog_delay(td, ipo->delay);
90
91                 free(ipo);
92                 return 0;
93         }
94
95         return 1;
96 }
97
98 void prune_io_piece_log(struct thread_data *td)
99 {
100         struct io_piece *ipo;
101         struct rb_node *n;
102
103         while ((n = rb_first(&td->io_hist_tree)) != NULL) {
104                 ipo = rb_entry(n, struct io_piece, rb_node);
105                 rb_erase(n, &td->io_hist_tree);
106                 free(ipo);
107         }
108 }
109
110 /*
111  * log a successful write, so we can unwind the log for verify
112  */
113 void log_io_piece(struct thread_data *td, struct io_u *io_u)
114 {
115         struct rb_node **p, *parent;
116         struct io_piece *ipo, *__ipo;
117
118         ipo = malloc(sizeof(struct io_piece));
119         ipo->file = io_u->file;
120         ipo->offset = io_u->offset;
121         ipo->len = io_u->buflen;
122
123         /*
124          * We don't need to sort the entries, if:
125          *
126          *      Sequential writes, or
127          *      Random writes that lay out the file as it goes along
128          *
129          * For both these cases, just reading back data in the order we
130          * wrote it out is the fastest.
131          */
132         if (!td_random(td) || !td->o.overwrite ||
133              (io_u->file->flags & FIO_FILE_NOSORT)) {
134                 INIT_LIST_HEAD(&ipo->list);
135                 list_add_tail(&ipo->list, &td->io_hist_list);
136                 return;
137         }
138
139         RB_CLEAR_NODE(&ipo->rb_node);
140         p = &td->io_hist_tree.rb_node;
141         parent = NULL;
142
143         /*
144          * Sort the entry into the verification list
145          */
146         while (*p) {
147                 parent = *p;
148
149                 __ipo = rb_entry(parent, struct io_piece, rb_node);
150                 if (ipo->offset <= __ipo->offset)
151                         p = &(*p)->rb_left;
152                 else
153                         p = &(*p)->rb_right;
154         }
155
156         rb_link_node(&ipo->rb_node, parent, p);
157         rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
158 }
159
160 void write_iolog_close(struct thread_data *td)
161 {
162         fflush(td->iolog_f);
163         fclose(td->iolog_f);
164         free(td->iolog_buf);
165         td->iolog_f = NULL;
166         td->iolog_buf = NULL;
167 }
168
169 /*
170  * Read version 2 iolog data. It is enhanced to include per-file logging,
171  * syncs, etc.
172  */
173 static int read_iolog2(struct thread_data *td, FILE *f)
174 {
175         unsigned long long offset;
176         unsigned int bytes;
177         int rw, reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
178         char *fname, *act;
179         char *str, *p;
180
181         free_release_files(td);
182
183         /*
184          * Read in the read iolog and store it, reuse the infrastructure
185          * for doing verifications.
186          */
187         str = malloc(4096);
188         fname = malloc(256+16);
189         act = malloc(256+16);
190
191         reads = writes = 0;
192         while ((p = fgets(str, 4096, f)) != NULL) {
193                 struct io_piece *ipo;
194                 int r;
195
196                 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, &bytes);
197                 if (r == 4) {
198                         /*
199                          * Check action first
200                          */
201                         if (!strcmp(act, "read"))
202                                 rw = DDIR_READ;
203                         else if (!strcmp(act, "write"))
204                                 rw = DDIR_WRITE;
205                         else if (!strcmp(act, "sync"))
206                                 rw = DDIR_SYNC;
207                         else {
208                                 log_err("fio: bad iolog file action: %s\n",act);
209                                 continue;
210                         }
211                 } else if (r == 2) {
212                         rw = DDIR_INVAL;
213                         if (!strcmp(act, "add")) {
214                                 td->o.nr_files++;
215                                 fileno = add_file(td, fname);
216                                 file_action = FIO_LOG_ADD_FILE;
217                                 continue;
218                         } else if (!strcmp(act, "open")) {
219                                 fileno = get_fileno(td, fname);
220                                 file_action = FIO_LOG_OPEN_FILE;
221                         } else if (!strcmp(act, "close")) {
222                                 fileno = get_fileno(td, fname);
223                                 file_action = FIO_LOG_CLOSE_FILE;
224                         } else {
225                                 log_err("fio: bad iolog file action: %s\n",act);
226                                 continue;
227                         }
228                 } else {
229                         log_err("bad iolog2: %s", p);
230                         continue;
231                 }
232                         
233                 if (rw == DDIR_READ)
234                         reads++;
235                 else if (rw == DDIR_WRITE)
236                         writes++;
237                 else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
238                         log_err("bad ddir: %d\n", rw);
239                         continue;
240                 }
241
242                 /*
243                  * Make note of file
244                  */
245                 ipo = malloc(sizeof(*ipo));
246                 memset(ipo, 0, sizeof(*ipo));
247                 INIT_LIST_HEAD(&ipo->list);
248                 ipo->offset = offset;
249                 ipo->len = bytes;
250                 ipo->ddir = (enum fio_ddir) rw;
251                 if (bytes > td->o.max_bs[rw])
252                         td->o.max_bs[rw] = bytes;
253                 if (rw == DDIR_INVAL) {
254                         ipo->fileno = fileno;
255                         ipo->file_action = file_action;
256                 }
257                 list_add_tail(&ipo->list, &td->io_log_list);
258         }
259
260         free(str);
261         free(act);
262         free(fname);
263
264         if (!reads && !writes)
265                 return 1;
266         else if (reads && !writes)
267                 td->o.td_ddir = TD_DDIR_READ;
268         else if (!reads && writes)
269                 td->o.td_ddir = TD_DDIR_WRITE;
270         else
271                 td->o.td_ddir = TD_DDIR_RW;
272
273         return 0;
274 }
275
276 /*
277  * Read version 1 iolog data.
278  */
279 static int read_iolog(struct thread_data *td, FILE *f)
280 {
281         unsigned long long offset;
282         unsigned int bytes;
283         char *str, *p;
284         int rw, reads, writes;
285
286         /*
287          * Read in the read iolog and store it, reuse the infrastructure
288          * for doing verifications.
289          */
290         str = malloc(4096);
291         reads = writes = 0;
292         while ((p = fgets(str, 4096, f)) != NULL) {
293                 struct io_piece *ipo;
294
295                 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
296                         log_err("bad iolog: %s\n", p);
297                         continue;
298                 }
299                 if (rw == DDIR_READ)
300                         reads++;
301                 else if (rw == DDIR_WRITE)
302                         writes++;
303                 else if (rw != DDIR_SYNC) {
304                         log_err("bad ddir: %d\n", rw);
305                         continue;
306                 }
307
308                 ipo = malloc(sizeof(*ipo));
309                 memset(ipo, 0, sizeof(*ipo));
310                 INIT_LIST_HEAD(&ipo->list);
311                 ipo->offset = offset;
312                 ipo->len = bytes;
313                 ipo->ddir = (enum fio_ddir) rw;
314                 if (bytes > td->o.max_bs[rw])
315                         td->o.max_bs[rw] = bytes;
316                 list_add_tail(&ipo->list, &td->io_log_list);
317         }
318
319         free(str);
320
321         if (!reads && !writes)
322                 return 1;
323         else if (reads && !writes)
324                 td->o.td_ddir = TD_DDIR_READ;
325         else if (!reads && writes)
326                 td->o.td_ddir = TD_DDIR_WRITE;
327         else
328                 td->o.td_ddir = TD_DDIR_RW;
329
330         return 0;
331 }
332
333 /*
334  * open iolog, check version, and call appropriate parser
335  */
336 static int init_iolog_read(struct thread_data *td)
337 {
338         char buffer[256], *p;
339         FILE *f;
340         int ret;
341
342         f = fopen(td->o.read_iolog_file, "r");
343         if (!f) {
344                 perror("fopen read iolog");
345                 return 1;
346         }
347
348         p = fgets(buffer, sizeof(buffer), f);
349         if (!p) {
350                 td_verror(td, errno, "iolog read");
351                 log_err("fio: unable to read iolog\n");
352                 return 1;
353         }
354
355         /*
356          * version 2 of the iolog stores a specific string as the
357          * first line, check for that
358          */
359         if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
360                 ret = read_iolog2(td, f);
361         else {
362                 /*
363                  * seek back to the beginning
364                  */
365                 if (fseek(f, 0, SEEK_SET) < 0) {
366                         td_verror(td, errno, "iolog read");
367                         log_err("fio: unable to read iolog\n");
368                         return 1;
369                 }
370
371                 ret = read_iolog(td, f);
372         }
373
374         fclose(f);
375         return ret;
376 }
377
378 /*
379  * Setup a log for storing io patterns.
380  */
381 static int init_iolog_write(struct thread_data *td)
382 {
383         struct fio_file *ff;
384         FILE *f;
385         unsigned int i;
386
387         f = fopen(td->o.write_iolog_file, "w+");
388         if (!f) {
389                 perror("fopen write iolog");
390                 return 1;
391         }
392
393         /*
394          * That's it for writing, setup a log buffer and we're done.
395           */
396         td->iolog_f = f;
397         td->iolog_buf = malloc(8192);
398         setvbuf(f, td->iolog_buf, _IOFBF, 8192);
399
400         /*
401          * write our version line
402          */
403         if (fprintf(f, "%s\n", iolog_ver2) < 0) {
404                 perror("iolog init\n");
405                 return 1;
406         }
407
408         /*
409          * add all known files
410          */
411         for_each_file(td, ff, i)
412                 log_file(td, ff, FIO_LOG_ADD_FILE);
413
414         return 0;
415 }
416
417 int init_iolog(struct thread_data *td)
418 {
419         int ret = 0;
420
421         if (td->io_ops->flags & FIO_DISKLESSIO)
422                 return 0;
423
424         if (td->o.read_iolog_file) {
425                 /*
426                  * Check if it's a blktrace file and load that if possible.
427                  * Otherwise assume it's a normal log file and load that.
428                  */
429                 if (is_blktrace(td->o.read_iolog_file))
430                         ret = load_blktrace(td, td->o.read_iolog_file);
431                 else
432                         ret = init_iolog_read(td);
433         } else if (td->o.write_iolog_file)
434                 ret = init_iolog_write(td);
435
436         return ret;
437 }
438
439 void setup_log(struct io_log **log)
440 {
441         struct io_log *l = malloc(sizeof(*l));
442
443         l->nr_samples = 0;
444         l->max_samples = 1024;
445         l->log = malloc(l->max_samples * sizeof(struct io_sample));
446         *log = l;
447 }
448
449 void __finish_log(struct io_log *log, const char *name)
450 {
451         unsigned int i;
452         FILE *f;
453
454         f = fopen(name, "w");
455         if (!f) {
456                 perror("fopen log");
457                 return;
458         }
459
460         for (i = 0; i < log->nr_samples; i++)
461                 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
462
463         fclose(f);
464         free(log->log);
465         free(log);
466 }
467
468 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
469 {
470         char file_name[256];
471
472         snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
473         __finish_log(log, file_name);
474 }