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