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