blob: 45be5fb6fdb94f718d3603bd769226f50abf7802 [file] [log] [blame]
Jens Axboe3c39a372006-06-06 20:56:12 +02001#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{
Jens Axboea4f4fdd2007-02-14 01:16:39 +01008 fprintf(td->iolog_f, "%u,%llu,%lu\n", io_u->ddir, io_u->offset, io_u->buflen);
Jens Axboe3c39a372006-06-06 20:56:12 +02009}
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;
Jens Axboe53cdc682006-10-18 11:50:58 +020021 io_u->file = ipo->file;
Jens Axboe3c39a372006-06-06 20:56:12 +020022 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;
Jens Axboe4b878982007-03-26 09:32:22 +020032 struct rb_node *n;
Jens Axboe3c39a372006-06-06 20:56:12 +020033
Jens Axboe4b878982007-03-26 09:32:22 +020034 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
35 ipo = rb_entry(n, struct io_piece, rb_node);
36 rb_erase(n, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +020037 free(ipo);
38 }
39}
40
41/*
Jens Axboe34403fb2007-03-02 21:43:25 +010042 * log a successful write, so we can unwind the log for verify
Jens Axboe3c39a372006-06-06 20:56:12 +020043 */
44void log_io_piece(struct thread_data *td, struct io_u *io_u)
45{
Jens Axboe8de8f042007-03-27 10:36:12 +020046 struct rb_node **p, *parent;
Jens Axboe4b878982007-03-26 09:32:22 +020047 struct io_piece *ipo, *__ipo;
Jens Axboe3c39a372006-06-06 20:56:12 +020048
Jens Axboe4b878982007-03-26 09:32:22 +020049 ipo = malloc(sizeof(struct io_piece));
Jens Axboe53cdc682006-10-18 11:50:58 +020050 ipo->file = io_u->file;
Jens Axboe3c39a372006-06-06 20:56:12 +020051 ipo->offset = io_u->offset;
52 ipo->len = io_u->buflen;
53
54 /*
Jens Axboe8de8f042007-03-27 10:36:12 +020055 * We don't need to sort the entries, if:
56 *
57 * Sequential writes, or
58 * Random writes that lay out the file as it goes along
59 *
60 * For both these cases, just reading back data in the order we
61 * wrote it out is the fastest.
62 */
Jens Axboe160b9662007-03-27 10:59:49 +020063 if (!td_random(td) || !td->o.overwrite ||
64 (io_u->file->flags & FIO_FILE_NOSORT)) {
Jens Axboe8de8f042007-03-27 10:36:12 +020065 INIT_LIST_HEAD(&ipo->list);
66 list_add_tail(&ipo->list, &td->io_hist_list);
67 return;
68 }
69
70 RB_CLEAR_NODE(&ipo->rb_node);
71 p = &td->io_hist_tree.rb_node;
72 parent = NULL;
73
74 /*
Jens Axboe4b878982007-03-26 09:32:22 +020075 * Sort the entry into the verification list
Jens Axboe3c39a372006-06-06 20:56:12 +020076 */
Jens Axboe4b878982007-03-26 09:32:22 +020077 while (*p) {
78 parent = *p;
Jens Axboe3c39a372006-06-06 20:56:12 +020079
Jens Axboe4b878982007-03-26 09:32:22 +020080 __ipo = rb_entry(parent, struct io_piece, rb_node);
Jens Axboebb5d7d02007-03-27 10:21:25 +020081 if (ipo->offset <= __ipo->offset)
Jens Axboe4b878982007-03-26 09:32:22 +020082 p = &(*p)->rb_left;
Jens Axboe4b878982007-03-26 09:32:22 +020083 else
Jens Axboebb5d7d02007-03-27 10:21:25 +020084 p = &(*p)->rb_right;
Jens Axboe3c39a372006-06-06 20:56:12 +020085 }
86
Jens Axboe4b878982007-03-26 09:32:22 +020087 rb_link_node(&ipo->rb_node, parent, p);
88 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +020089}
90
91void write_iolog_close(struct thread_data *td)
92{
93 fflush(td->iolog_f);
94 fclose(td->iolog_f);
95 free(td->iolog_buf);
96}
97
Jens Axboefb71fbd2006-10-20 09:15:46 +020098/*
99 * Open a stored log and read in the entries.
100 */
101static int init_iolog_read(struct thread_data *td)
Jens Axboe3c39a372006-06-06 20:56:12 +0200102{
103 unsigned long long offset;
104 unsigned int bytes;
105 char *str, *p;
106 FILE *f;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200107 int rw, reads, writes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200108
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100109 f = fopen(td->o.read_iolog_file, "r");
Jens Axboe3c39a372006-06-06 20:56:12 +0200110 if (!f) {
Jens Axboefb71fbd2006-10-20 09:15:46 +0200111 perror("fopen read iolog");
Jens Axboe3c39a372006-06-06 20:56:12 +0200112 return 1;
113 }
114
115 /*
Jens Axboe3c39a372006-06-06 20:56:12 +0200116 * Read in the read iolog and store it, reuse the infrastructure
117 * for doing verifications.
118 */
119 str = malloc(4096);
Jens Axboefb71fbd2006-10-20 09:15:46 +0200120 reads = writes = 0;
Jens Axboe3c39a372006-06-06 20:56:12 +0200121 while ((p = fgets(str, 4096, f)) != NULL) {
122 struct io_piece *ipo;
123
124 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200125 log_err("bad iolog: %s\n", p);
Jens Axboe3c39a372006-06-06 20:56:12 +0200126 continue;
127 }
128 if (rw == DDIR_READ)
129 reads++;
130 else if (rw == DDIR_WRITE)
131 writes++;
Jens Axboec38e9462007-03-27 08:48:48 +0200132 else if (rw != DDIR_SYNC) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200133 log_err("bad ddir: %d\n", rw);
Jens Axboe3c39a372006-06-06 20:56:12 +0200134 continue;
135 }
136
137 ipo = malloc(sizeof(*ipo));
138 INIT_LIST_HEAD(&ipo->list);
139 ipo->offset = offset;
140 ipo->len = bytes;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100141 ipo->ddir = (enum fio_ddir) rw;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100142 if (bytes > td->o.max_bs[rw])
143 td->o.max_bs[rw] = bytes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200144 list_add_tail(&ipo->list, &td->io_log_list);
Jens Axboe3c39a372006-06-06 20:56:12 +0200145 }
146
147 free(str);
148 fclose(f);
149
Jens Axboefb71fbd2006-10-20 09:15:46 +0200150 if (!reads && !writes)
Jens Axboe3c39a372006-06-06 20:56:12 +0200151 return 1;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200152 else if (reads && !writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100153 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200154 else if (!reads && writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100155 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200156 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100157 td->o.td_ddir = TD_DDIR_RW;
Jens Axboe3c39a372006-06-06 20:56:12 +0200158
159 return 0;
160}
Jens Axboe8914a9d2006-06-07 11:14:56 +0200161
Jens Axboefb71fbd2006-10-20 09:15:46 +0200162/*
163 * Setup a log for storing io patterns.
164 */
165static int init_iolog_write(struct thread_data *td)
166{
Jens Axboe076efc72006-10-27 11:24:25 +0200167 FILE *f;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200168
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100169 f = fopen(td->o.write_iolog_file, "w+");
Jens Axboefb71fbd2006-10-20 09:15:46 +0200170 if (!f) {
171 perror("fopen write iolog");
172 return 1;
173 }
174
175 /*
176 * That's it for writing, setup a log buffer and we're done.
177 */
178 td->iolog_f = f;
179 td->iolog_buf = malloc(8192);
180 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
181 return 0;
182}
183
184int init_iolog(struct thread_data *td)
185{
Jens Axboeb4a6a592006-10-20 13:54:47 +0200186 int ret = 0;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200187
Jens Axboeba0fbe12007-03-09 14:34:23 +0100188 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200189 return 0;
190
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100191 if (td->o.read_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200192 ret = init_iolog_read(td);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100193 else if (td->o.write_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200194 ret = init_iolog_write(td);
195
Jens Axboe1e97cce2006-12-05 11:44:16 +0100196 return ret;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200197}
198
Jens Axboe8914a9d2006-06-07 11:14:56 +0200199void setup_log(struct io_log **log)
200{
201 struct io_log *l = malloc(sizeof(*l));
202
203 l->nr_samples = 0;
204 l->max_samples = 1024;
205 l->log = malloc(l->max_samples * sizeof(struct io_sample));
206 *log = l;
207}
208
Jens Axboebb3884d2007-01-17 17:23:11 +1100209void __finish_log(struct io_log *log, const char *name)
Jens Axboe8914a9d2006-06-07 11:14:56 +0200210{
Jens Axboe8914a9d2006-06-07 11:14:56 +0200211 unsigned int i;
Jens Axboebb3884d2007-01-17 17:23:11 +1100212 FILE *f;
Jens Axboe8914a9d2006-06-07 11:14:56 +0200213
Jens Axboebb3884d2007-01-17 17:23:11 +1100214 f = fopen(name, "w");
Jens Axboe8914a9d2006-06-07 11:14:56 +0200215 if (!f) {
216 perror("fopen log");
217 return;
218 }
219
220 for (i = 0; i < log->nr_samples; i++)
221 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
222
223 fclose(f);
224 free(log->log);
225 free(log);
226}
Jens Axboebb3884d2007-01-17 17:23:11 +1100227
228void finish_log(struct thread_data *td, struct io_log *log, const char *name)
229{
230 char file_name[256];
231
232 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
233 __finish_log(log, file_name);
234}