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