[PATCH] file descriptor is int
[fio.git] / log.c
... / ...
CommitLineData
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, "%u,%llu,%lu\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;
21 io_u->file = ipo->file;
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);
50 ipo->file = io_u->file;
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
85/*
86 * Open a stored log and read in the entries.
87 */
88static int init_iolog_read(struct thread_data *td)
89{
90 unsigned long long offset;
91 unsigned int bytes;
92 char *str, *p;
93 FILE *f;
94 int rw, reads, writes;
95
96 f = fopen(td->read_iolog_file, "r");
97 if (!f) {
98 perror("fopen read iolog");
99 return 1;
100 }
101
102 /*
103 * Read in the read iolog and store it, reuse the infrastructure
104 * for doing verifications.
105 */
106 str = malloc(4096);
107 reads = writes = 0;
108 while ((p = fgets(str, 4096, f)) != NULL) {
109 struct io_piece *ipo;
110
111 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
112 log_err("bad iolog: %s\n", p);
113 continue;
114 }
115 if (rw == DDIR_READ)
116 reads++;
117 else if (rw == DDIR_WRITE)
118 writes++;
119 else {
120 log_err("bad ddir: %d\n", rw);
121 continue;
122 }
123
124 ipo = malloc(sizeof(*ipo));
125 INIT_LIST_HEAD(&ipo->list);
126 ipo->offset = offset;
127 ipo->len = bytes;
128 ipo->ddir = (enum fio_ddir) rw;
129 if (bytes > td->max_bs[rw])
130 td->max_bs[rw] = bytes;
131 list_add_tail(&ipo->list, &td->io_log_list);
132 }
133
134 free(str);
135 fclose(f);
136
137 if (!reads && !writes)
138 return 1;
139 else if (reads && !writes)
140 td->ddir = DDIR_READ;
141 else if (!reads && writes)
142 td->ddir = DDIR_READ;
143 else
144 td->iomix = 1;
145
146 return 0;
147}
148
149/*
150 * Setup a log for storing io patterns.
151 */
152static int init_iolog_write(struct thread_data *td)
153{
154 FILE *f;
155
156 f = fopen(td->write_iolog_file, "w+");
157 if (!f) {
158 perror("fopen write iolog");
159 return 1;
160 }
161
162 /*
163 * That's it for writing, setup a log buffer and we're done.
164 */
165 td->iolog_f = f;
166 td->iolog_buf = malloc(8192);
167 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
168 return 0;
169}
170
171int init_iolog(struct thread_data *td)
172{
173 int ret = 0;
174
175 if (td->io_ops->flags & FIO_CPUIO)
176 return 0;
177
178 if (td->read_iolog_file)
179 ret = init_iolog_read(td);
180 else if (td->write_iolog_file)
181 ret = init_iolog_write(td);
182
183 return ret;
184}
185
186int setup_rate(struct thread_data *td)
187{
188 unsigned long long rate;
189 int nr_reads_per_msec;
190
191 if (!td->rate)
192 return 0;
193
194 if (td->rate < td->ratemin) {
195 log_err("min rate larger than nominal rate\n");
196 return -1;
197 }
198
199 rate = td->rate;
200 nr_reads_per_msec = (rate * 1024 * 1000) / td->min_bs[DDIR_READ];
201 if (!nr_reads_per_msec) {
202 log_err("rate lower than supported\n");
203 return -1;
204 }
205
206 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
207 td->rate_pending_usleep = 0;
208 return 0;
209}
210
211void setup_log(struct io_log **log)
212{
213 struct io_log *l = malloc(sizeof(*l));
214
215 l->nr_samples = 0;
216 l->max_samples = 1024;
217 l->log = malloc(l->max_samples * sizeof(struct io_sample));
218 *log = l;
219}
220
221void __finish_log(struct io_log *log, const char *name)
222{
223 unsigned int i;
224 FILE *f;
225
226 f = fopen(name, "w");
227 if (!f) {
228 perror("fopen log");
229 return;
230 }
231
232 for (i = 0; i < log->nr_samples; i++)
233 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
234
235 fclose(f);
236 free(log->log);
237 free(log);
238}
239
240void finish_log(struct thread_data *td, struct io_log *log, const char *name)
241{
242 char file_name[256];
243
244 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
245 __finish_log(log, file_name);
246}