[PATCH] Split out the verify io parts
[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{
8 fprintf(td->iolog_f, "%d,%llu,%u\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;
53cdc686 21 io_u->file = ipo->file;
3c39a379
JA
22 free(ipo);
23 return 0;
24 }
25
26 return 1;
27}
28
29void prune_io_piece_log(struct thread_data *td)
30{
31 struct io_piece *ipo;
32
33 while (!list_empty(&td->io_hist_list)) {
34 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
35
36 list_del(&ipo->list);
37 free(ipo);
38 }
39}
40
41/*
42 * log a succesful write, so we can unwind the log for verify
43 */
44void log_io_piece(struct thread_data *td, struct io_u *io_u)
45{
46 struct io_piece *ipo = malloc(sizeof(struct io_piece));
47 struct list_head *entry;
48
49 INIT_LIST_HEAD(&ipo->list);
53cdc686 50 ipo->file = io_u->file;
3c39a379
JA
51 ipo->offset = io_u->offset;
52 ipo->len = io_u->buflen;
53
54 /*
55 * for random io where the writes extend the file, it will typically
56 * be laid out with the block scattered as written. it's faster to
57 * read them in in that order again, so don't sort
58 */
59 if (td->sequential || !td->overwrite) {
60 list_add_tail(&ipo->list, &td->io_hist_list);
61 return;
62 }
63
64 /*
65 * for random io, sort the list so verify will run faster
66 */
67 entry = &td->io_hist_list;
68 while ((entry = entry->prev) != &td->io_hist_list) {
69 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
70
71 if (__ipo->offset < ipo->offset)
72 break;
73 }
74
75 list_add(&ipo->list, entry);
76}
77
78void write_iolog_close(struct thread_data *td)
79{
80 fflush(td->iolog_f);
81 fclose(td->iolog_f);
82 free(td->iolog_buf);
83}
84
85int init_iolog(struct thread_data *td)
86{
87 unsigned long long offset;
88 unsigned int bytes;
89 char *str, *p;
90 FILE *f;
91 int rw, i, reads, writes;
92
93 if (!td->read_iolog && !td->write_iolog)
94 return 0;
95
96 if (td->read_iolog)
97 f = fopen(td->iolog_file, "r");
98 else
99 f = fopen(td->iolog_file, "w");
100
101 if (!f) {
102 perror("fopen iolog");
103 printf("file %s, %d/%d\n", td->iolog_file, td->read_iolog, td->write_iolog);
104 return 1;
105 }
106
107 /*
108 * That's it for writing, setup a log buffer and we're done.
109 */
110 if (td->write_iolog) {
111 td->iolog_f = f;
112 td->iolog_buf = malloc(8192);
113 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
114 return 0;
115 }
116
117 /*
118 * Read in the read iolog and store it, reuse the infrastructure
119 * for doing verifications.
120 */
121 str = malloc(4096);
122 reads = writes = i = 0;
123 while ((p = fgets(str, 4096, f)) != NULL) {
124 struct io_piece *ipo;
125
126 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
3b70d7e5 127 log_err("bad iolog: %s\n", p);
3c39a379
JA
128 continue;
129 }
130 if (rw == DDIR_READ)
131 reads++;
132 else if (rw == DDIR_WRITE)
133 writes++;
134 else {
3b70d7e5 135 log_err("bad ddir: %d\n", rw);
3c39a379
JA
136 continue;
137 }
138
139 ipo = malloc(sizeof(*ipo));
140 INIT_LIST_HEAD(&ipo->list);
141 ipo->offset = offset;
142 ipo->len = bytes;
143 if (bytes > td->max_bs)
144 td->max_bs = bytes;
145 ipo->ddir = rw;
146 list_add_tail(&ipo->list, &td->io_log_list);
147 i++;
148 }
149
150 free(str);
151 fclose(f);
152
153 if (!i)
154 return 1;
155
156 if (reads && !writes)
157 td->ddir = DDIR_READ;
158 else if (!reads && writes)
159 td->ddir = DDIR_READ;
160 else
161 td->iomix = 1;
162
163 return 0;
164}
8914a9d8
JA
165
166int setup_rate(struct thread_data *td)
167{
168 int nr_reads_per_sec;
169
170 if (!td->rate)
171 return 0;
172
173 if (td->rate < td->ratemin) {
3b70d7e5 174 log_err("min rate larger than nominal rate\n");
8914a9d8
JA
175 return -1;
176 }
177
178 nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
179 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
180 td->rate_pending_usleep = 0;
181 return 0;
182}
183
184void setup_log(struct io_log **log)
185{
186 struct io_log *l = malloc(sizeof(*l));
187
188 l->nr_samples = 0;
189 l->max_samples = 1024;
190 l->log = malloc(l->max_samples * sizeof(struct io_sample));
191 *log = l;
192}
193
194void finish_log(struct thread_data *td, struct io_log *log, const char *name)
195{
196 char file_name[256];
197 FILE *f;
198 unsigned int i;
199
200 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
201 f = fopen(file_name, "w");
202 if (!f) {
203 perror("fopen log");
204 return;
205 }
206
207 for (i = 0; i < log->nr_samples; i++)
208 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
209
210 fclose(f);
211 free(log->log);
212 free(log);
213}