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