Add clue for ENOMEM hugepage allocation
[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
a61eddec
JA
11static void iolog_delay(struct thread_data *td, unsigned long delay)
12{
13 unsigned long usec = utime_since_now(&td->last_issue);
14
15 if (delay < usec)
16 return;
17
18 delay -= usec;
19
20 /*
21 * less than 100 usec delay, just regard it as noise
22 */
23 if (delay < 100)
24 return;
25
26 usec_sleep(td, delay);
27}
28
3c39a379
JA
29int read_iolog_get(struct thread_data *td, struct io_u *io_u)
30{
31 struct io_piece *ipo;
32
33 if (!list_empty(&td->io_log_list)) {
34 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
35 list_del(&ipo->list);
36 io_u->offset = ipo->offset;
37 io_u->buflen = ipo->len;
38 io_u->ddir = ipo->ddir;
53cdc686 39 io_u->file = ipo->file;
a61eddec
JA
40
41 if (ipo->delay)
42 iolog_delay(td, ipo->delay);
43
fcb11708
JA
44 /*
45 * work around, this needs a format change to work for > 1 file
46 */
47 if (!io_u->file)
48 io_u->file = &td->files[0];
3c39a379
JA
49 free(ipo);
50 return 0;
51 }
52
53 return 1;
54}
55
56void prune_io_piece_log(struct thread_data *td)
57{
58 struct io_piece *ipo;
4b87898e 59 struct rb_node *n;
3c39a379 60
4b87898e
JA
61 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
62 ipo = rb_entry(n, struct io_piece, rb_node);
63 rb_erase(n, &td->io_hist_tree);
3c39a379
JA
64 free(ipo);
65 }
66}
67
68/*
34403fb1 69 * log a successful write, so we can unwind the log for verify
3c39a379
JA
70 */
71void log_io_piece(struct thread_data *td, struct io_u *io_u)
72{
8de8f047 73 struct rb_node **p, *parent;
4b87898e 74 struct io_piece *ipo, *__ipo;
3c39a379 75
4b87898e 76 ipo = malloc(sizeof(struct io_piece));
53cdc686 77 ipo->file = io_u->file;
3c39a379
JA
78 ipo->offset = io_u->offset;
79 ipo->len = io_u->buflen;
80
8de8f047
JA
81 /*
82 * We don't need to sort the entries, if:
83 *
84 * Sequential writes, or
85 * Random writes that lay out the file as it goes along
86 *
87 * For both these cases, just reading back data in the order we
88 * wrote it out is the fastest.
89 */
160b966d
JA
90 if (!td_random(td) || !td->o.overwrite ||
91 (io_u->file->flags & FIO_FILE_NOSORT)) {
8de8f047
JA
92 INIT_LIST_HEAD(&ipo->list);
93 list_add_tail(&ipo->list, &td->io_hist_list);
94 return;
95 }
96
97 RB_CLEAR_NODE(&ipo->rb_node);
98 p = &td->io_hist_tree.rb_node;
99 parent = NULL;
100
3c39a379 101 /*
4b87898e 102 * Sort the entry into the verification list
3c39a379 103 */
4b87898e
JA
104 while (*p) {
105 parent = *p;
106
107 __ipo = rb_entry(parent, struct io_piece, rb_node);
bb5d7d0b 108 if (ipo->offset <= __ipo->offset)
4b87898e 109 p = &(*p)->rb_left;
4b87898e 110 else
bb5d7d0b 111 p = &(*p)->rb_right;
3c39a379
JA
112 }
113
4b87898e
JA
114 rb_link_node(&ipo->rb_node, parent, p);
115 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
3c39a379
JA
116}
117
118void write_iolog_close(struct thread_data *td)
119{
120 fflush(td->iolog_f);
121 fclose(td->iolog_f);
122 free(td->iolog_buf);
123}
124
fb71fbd7
JA
125/*
126 * Open a stored log and read in the entries.
127 */
128static int init_iolog_read(struct thread_data *td)
3c39a379
JA
129{
130 unsigned long long offset;
131 unsigned int bytes;
132 char *str, *p;
133 FILE *f;
fb71fbd7 134 int rw, reads, writes;
3c39a379 135
2dc1bbeb 136 f = fopen(td->o.read_iolog_file, "r");
3c39a379 137 if (!f) {
fb71fbd7 138 perror("fopen read iolog");
3c39a379
JA
139 return 1;
140 }
141
3c39a379
JA
142 /*
143 * Read in the read iolog and store it, reuse the infrastructure
144 * for doing verifications.
145 */
146 str = malloc(4096);
fb71fbd7 147 reads = writes = 0;
3c39a379
JA
148 while ((p = fgets(str, 4096, f)) != NULL) {
149 struct io_piece *ipo;
150
151 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
3b70d7e5 152 log_err("bad iolog: %s\n", p);
3c39a379
JA
153 continue;
154 }
155 if (rw == DDIR_READ)
156 reads++;
157 else if (rw == DDIR_WRITE)
158 writes++;
c38e9468 159 else if (rw != DDIR_SYNC) {
3b70d7e5 160 log_err("bad ddir: %d\n", rw);
3c39a379
JA
161 continue;
162 }
163
164 ipo = malloc(sizeof(*ipo));
733ed597 165 memset(ipo, 0, sizeof(*ipo));
3c39a379
JA
166 INIT_LIST_HEAD(&ipo->list);
167 ipo->offset = offset;
168 ipo->len = bytes;
1e97cce9 169 ipo->ddir = (enum fio_ddir) rw;
2dc1bbeb
JA
170 if (bytes > td->o.max_bs[rw])
171 td->o.max_bs[rw] = bytes;
3c39a379 172 list_add_tail(&ipo->list, &td->io_log_list);
3c39a379
JA
173 }
174
175 free(str);
176 fclose(f);
177
fb71fbd7 178 if (!reads && !writes)
3c39a379 179 return 1;
fb71fbd7 180 else if (reads && !writes)
2dc1bbeb 181 td->o.td_ddir = TD_DDIR_READ;
3c39a379 182 else if (!reads && writes)
36361ebb 183 td->o.td_ddir = TD_DDIR_WRITE;
3c39a379 184 else
2dc1bbeb 185 td->o.td_ddir = TD_DDIR_RW;
3c39a379
JA
186
187 return 0;
188}
8914a9d8 189
fb71fbd7
JA
190/*
191 * Setup a log for storing io patterns.
192 */
193static int init_iolog_write(struct thread_data *td)
194{
076efc7c 195 FILE *f;
fb71fbd7 196
733ed597
JA
197 if (td->o.nr_files > 1) {
198 log_err("fio: write_iolog only works with 1 file currently\n");
199 return 1;
200 }
201
2dc1bbeb 202 f = fopen(td->o.write_iolog_file, "w+");
fb71fbd7
JA
203 if (!f) {
204 perror("fopen write iolog");
205 return 1;
206 }
207
208 /*
209 * That's it for writing, setup a log buffer and we're done.
210 */
211 td->iolog_f = f;
212 td->iolog_buf = malloc(8192);
213 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
214 return 0;
215}
216
217int init_iolog(struct thread_data *td)
218{
b4a6a59a
JA
219 int ret = 0;
220
ba0fbe10 221 if (td->io_ops->flags & FIO_DISKLESSIO)
f48b467c
JA
222 return 0;
223
fb7b71a3
JA
224 if (td->o.read_iolog_file) {
225 /*
226 * Check if it's a blktrace file and load that if possible.
227 * Otherwise assume it's a normal log file and load that.
228 */
229 if (is_blktrace(td->o.read_iolog_file))
230 ret = load_blktrace(td, td->o.read_iolog_file);
231 else
232 ret = init_iolog_read(td);
233 } else if (td->o.write_iolog_file)
b4a6a59a 234 ret = init_iolog_write(td);
fb71fbd7 235
1e97cce9 236 return ret;
fb71fbd7
JA
237}
238
8914a9d8
JA
239void setup_log(struct io_log **log)
240{
241 struct io_log *l = malloc(sizeof(*l));
242
243 l->nr_samples = 0;
244 l->max_samples = 1024;
245 l->log = malloc(l->max_samples * sizeof(struct io_sample));
246 *log = l;
247}
248
bb3884d8 249void __finish_log(struct io_log *log, const char *name)
8914a9d8 250{
8914a9d8 251 unsigned int i;
bb3884d8 252 FILE *f;
8914a9d8 253
bb3884d8 254 f = fopen(name, "w");
8914a9d8
JA
255 if (!f) {
256 perror("fopen log");
257 return;
258 }
259
260 for (i = 0; i < log->nr_samples; i++)
261 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
262
263 fclose(f);
264 free(log->log);
265 free(log);
266}
bb3884d8
JA
267
268void finish_log(struct thread_data *td, struct io_log *log, const char *name)
269{
270 char file_name[256];
271
272 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
273 __finish_log(log, file_name);
274}