blob: 89ece538c570391b8812957f2e4a428d1ab1bb65 [file] [log] [blame]
Jens Axboe10ba5352006-10-20 11:39:27 +02001#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <signal.h>
5#include <time.h>
6#include <assert.h>
7
8#include "fio.h"
9#include "os.h"
10
11/*
12 * The ->file_map[] contains a map of blocks we have or have not done io
13 * to yet. Used to make sure we cover the entire range in a fair fashion.
14 */
15static int random_map_free(struct thread_data *td, struct fio_file *f,
16 unsigned long long block)
17{
18 unsigned int idx = RAND_MAP_IDX(td, f, block);
19 unsigned int bit = RAND_MAP_BIT(td, f, block);
20
21 return (f->file_map[idx] & (1UL << bit)) == 0;
22}
23
24/*
Jens Axboedf415582006-10-20 11:41:03 +020025 * Mark a given offset as used in the map.
26 */
27static void mark_random_map(struct thread_data *td, struct fio_file *f,
28 struct io_u *io_u)
29{
Jens Axboeafdbe582007-02-10 19:01:57 +010030 unsigned int min_bs = td->rw_min_bs;
Jens Axboea00735e2006-11-03 08:58:08 +010031 unsigned long long block;
32 unsigned int blocks;
Jens Axboec685b5b2007-02-10 20:02:28 +010033 unsigned int nr_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020034
Jens Axboea00735e2006-11-03 08:58:08 +010035 block = io_u->offset / (unsigned long long) min_bs;
36 blocks = 0;
Jens Axboec685b5b2007-02-10 20:02:28 +010037 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
38
39 while (blocks < nr_blocks) {
Jens Axboedf415582006-10-20 11:41:03 +020040 unsigned int idx, bit;
41
42 if (!random_map_free(td, f, block))
43 break;
44
45 idx = RAND_MAP_IDX(td, f, block);
46 bit = RAND_MAP_BIT(td, f, block);
47
48 assert(idx < f->num_maps);
49
50 f->file_map[idx] |= (1UL << bit);
51 block++;
52 blocks++;
53 }
54
Jens Axboea00735e2006-11-03 08:58:08 +010055 if ((blocks * min_bs) < io_u->buflen)
56 io_u->buflen = blocks * min_bs;
Jens Axboedf415582006-10-20 11:41:03 +020057}
58
59/*
Jens Axboe10ba5352006-10-20 11:39:27 +020060 * Return the next free block in the map.
61 */
62static int get_next_free_block(struct thread_data *td, struct fio_file *f,
63 unsigned long long *b)
64{
65 int i;
66
Jens Axboec685b5b2007-02-10 20:02:28 +010067 i = f->last_free_lookup;
68 *b = (i * BLOCKS_PER_MAP);
Jens Axboeee884702006-12-20 10:58:07 +010069 while ((*b) * td->rw_min_bs < f->real_file_size) {
Jens Axboe10ba5352006-10-20 11:39:27 +020070 if (f->file_map[i] != -1UL) {
71 *b += ffz(f->file_map[i]);
Jens Axboec685b5b2007-02-10 20:02:28 +010072 f->last_free_lookup = i;
Jens Axboe10ba5352006-10-20 11:39:27 +020073 return 0;
74 }
75
76 *b += BLOCKS_PER_MAP;
77 i++;
78 }
79
80 return 1;
81}
82
83/*
84 * For random io, generate a random new block and see if it's used. Repeat
85 * until we find a free one. For sequential io, just return the end of
86 * the last io issued.
87 */
88static int get_next_offset(struct thread_data *td, struct fio_file *f,
Jens Axboec685b5b2007-02-10 20:02:28 +010089 struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +020090{
Jens Axboec685b5b2007-02-10 20:02:28 +010091 const int ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +020092 unsigned long long b, rb;
93 long r;
94
95 if (!td->sequential) {
Jens Axboef6971252006-11-15 10:00:31 +010096 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
Jens Axboec685b5b2007-02-10 20:02:28 +010097 int loops = 5;
Jens Axboe10ba5352006-10-20 11:39:27 +020098
99 do {
100 r = os_random_long(&td->random_state);
101 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
Jens Axboebb8895e2006-10-30 15:14:48 +0100102 if (td->norandommap)
103 break;
Jens Axboea00735e2006-11-03 08:58:08 +0100104 rb = b + (f->file_offset / td->min_bs[ddir]);
Jens Axboe10ba5352006-10-20 11:39:27 +0200105 loops--;
106 } while (!random_map_free(td, f, rb) && loops);
107
Jens Axboebca4ed42007-02-12 05:13:23 +0100108 /*
109 * if we failed to retrieve a truly random offset within
110 * the loops assigned, see if there are free ones left at all
111 */
112 if (!loops && get_next_free_block(td, f, &b))
113 return 1;
Jens Axboe10ba5352006-10-20 11:39:27 +0200114 } else
Jens Axboea00735e2006-11-03 08:58:08 +0100115 b = f->last_pos / td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200116
Jens Axboec685b5b2007-02-10 20:02:28 +0100117 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
Jens Axboebca4ed42007-02-12 05:13:23 +0100118 if (io_u->offset >= f->real_file_size)
Jens Axboe10ba5352006-10-20 11:39:27 +0200119 return 1;
120
121 return 0;
122}
123
Jens Axboebca4ed42007-02-12 05:13:23 +0100124static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
125 struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200126{
Jens Axboebca4ed42007-02-12 05:13:23 +0100127 const int ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200128 unsigned int buflen;
129 long r;
130
Jens Axboea00735e2006-11-03 08:58:08 +0100131 if (td->min_bs[ddir] == td->max_bs[ddir])
132 buflen = td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200133 else {
134 r = os_random_long(&td->bsrange_state);
Jens Axboe1e97cce2006-12-05 11:44:16 +0100135 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
Jens Axboe690adba2006-10-30 15:25:09 +0100136 if (!td->bs_unaligned)
Jens Axboea00735e2006-11-03 08:58:08 +0100137 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
Jens Axboe10ba5352006-10-20 11:39:27 +0200138 }
139
Jens Axboebca4ed42007-02-12 05:13:23 +0100140 while (buflen + io_u->offset > f->real_file_size) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200141 /*
142 * if using direct/raw io, we may not be able to
143 * shrink the size. so just fail it.
144 */
145 if (td->io_ops->flags & FIO_RAWIO)
146 return 0;
147
Jens Axboebca4ed42007-02-12 05:13:23 +0100148 if (buflen == td->min_bs[ddir])
149 return 0;
150
151 buflen = td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200152 }
153
154 return buflen;
155}
156
157/*
158 * Return the data direction for the next io_u. If the job is a
159 * mixed read/write workload, check the rwmix cycle and switch if
160 * necessary.
161 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100162static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200163{
164 if (td_rw(td)) {
165 struct timeval now;
166 unsigned long elapsed;
167
Jens Axboe02bcaa82006-11-24 10:42:00 +0100168 fio_gettime(&now, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200169 elapsed = mtime_since_now(&td->rwmix_switch);
170
171 /*
172 * Check if it's time to seed a new data direction.
173 */
174 if (elapsed >= td->rwmixcycle) {
175 unsigned int v;
176 long r;
177
178 r = os_random_long(&td->rwmix_state);
179 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
180 if (v < td->rwmixread)
181 td->rwmix_ddir = DDIR_READ;
182 else
183 td->rwmix_ddir = DDIR_WRITE;
184 memcpy(&td->rwmix_switch, &now, sizeof(now));
185 }
186 return td->rwmix_ddir;
187 } else if (td_read(td))
188 return DDIR_READ;
189 else
190 return DDIR_WRITE;
191}
192
Jens Axboe10ba5352006-10-20 11:39:27 +0200193void put_io_u(struct thread_data *td, struct io_u *io_u)
194{
195 io_u->file = NULL;
196 list_del(&io_u->list);
197 list_add(&io_u->list, &td->io_u_freelist);
198 td->cur_depth--;
199}
200
201static int fill_io_u(struct thread_data *td, struct fio_file *f,
202 struct io_u *io_u)
203{
204 /*
205 * If using an iolog, grab next piece if any available.
206 */
207 if (td->read_iolog)
208 return read_iolog_get(td, io_u);
209
210 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200211 * see if it's time to sync
212 */
213 if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks)
214 && should_fsync(td)) {
215 io_u->ddir = DDIR_SYNC;
216 io_u->file = f;
217 return 0;
218 }
219
Jens Axboea00735e2006-11-03 08:58:08 +0100220 io_u->ddir = get_rw_ddir(td);
221
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200222 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100223 * No log, let the seq/rand engine retrieve the next buflen and
224 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200225 */
Jens Axboebca4ed42007-02-12 05:13:23 +0100226 if (get_next_offset(td, f, io_u))
227 return 1;
Jens Axboec685b5b2007-02-10 20:02:28 +0100228
Jens Axboebca4ed42007-02-12 05:13:23 +0100229 io_u->buflen = get_next_buflen(td, f, io_u);
230 if (!io_u->buflen)
231 return 1;
Jens Axboe10ba5352006-10-20 11:39:27 +0200232
Jens Axboebca4ed42007-02-12 05:13:23 +0100233 /*
234 * mark entry before potentially trimming io_u
235 */
236 if (!td->read_iolog && !td->sequential && !td->norandommap)
237 mark_random_map(td, f, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200238
Jens Axboebca4ed42007-02-12 05:13:23 +0100239 /*
240 * If using a write iolog, store this entry.
241 */
242 if (td->write_iolog_file)
243 write_iolog_put(td, io_u);
244
245 io_u->file = f;
246 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200247}
248
Jens Axboe71619dc2007-01-13 23:56:33 +0100249static void io_u_mark_depth(struct thread_data *td)
250{
251 int index = 0;
252
253 switch (td->cur_depth) {
254 default:
255 index++;
256 case 32 ... 63:
257 index++;
258 case 16 ... 31:
259 index++;
260 case 8 ... 15:
261 index++;
262 case 4 ... 7:
263 index++;
264 case 2 ... 3:
265 index++;
266 case 1:
267 break;
268 }
269
270 td->io_u_map[index]++;
271 td->total_io_u++;
272}
273
Jens Axboe10ba5352006-10-20 11:39:27 +0200274struct io_u *__get_io_u(struct thread_data *td)
275{
276 struct io_u *io_u = NULL;
277
278 if (!queue_full(td)) {
279 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
280
Jens Axboe6040dab2006-10-24 19:38:15 +0200281 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200282 io_u->error = 0;
283 io_u->resid = 0;
284 list_del(&io_u->list);
285 list_add(&io_u->list, &td->io_u_busylist);
286 td->cur_depth++;
Jens Axboe71619dc2007-01-13 23:56:33 +0100287 io_u_mark_depth(td);
Jens Axboe10ba5352006-10-20 11:39:27 +0200288 }
289
290 return io_u;
291}
292
293/*
294 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
295 * etc. The returned io_u is fully ready to be prepped and submitted.
296 */
297struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
298{
299 struct io_u *io_u;
300
301 io_u = __get_io_u(td);
302 if (!io_u)
303 return NULL;
304
305 if (td->zone_bytes >= td->zone_size) {
306 td->zone_bytes = 0;
307 f->last_pos += td->zone_skip;
308 }
309
310 if (fill_io_u(td, f, io_u)) {
311 put_io_u(td, io_u);
312 return NULL;
313 }
314
Jens Axboeee884702006-12-20 10:58:07 +0100315 if (io_u->buflen + io_u->offset > f->real_file_size) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200316 if (td->io_ops->flags & FIO_RAWIO) {
317 put_io_u(td, io_u);
318 return NULL;
319 }
320
Jens Axboeee884702006-12-20 10:58:07 +0100321 io_u->buflen = f->real_file_size - io_u->offset;
Jens Axboe10ba5352006-10-20 11:39:27 +0200322 }
323
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200324 if (io_u->ddir != DDIR_SYNC) {
325 if (!io_u->buflen) {
326 put_io_u(td, io_u);
327 return NULL;
328 }
329
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200330 f->last_pos += io_u->buflen;
331
332 if (td->verify != VERIFY_NONE)
333 populate_verify_io_u(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200334 }
335
Jens Axboe10ba5352006-10-20 11:39:27 +0200336 if (td_io_prep(td, io_u)) {
337 put_io_u(td, io_u);
338 return NULL;
339 }
340
Jens Axboe165faf12007-02-07 11:30:37 +0100341 /*
342 * Set io data pointers.
343 */
Jens Axboecec6b552007-02-06 20:15:38 +0100344 io_u->xfer_buf = io_u->buf;
345 io_u->xfer_buflen = io_u->buflen;
Jens Axboe165faf12007-02-07 11:30:37 +0100346
Jens Axboe02bcaa82006-11-24 10:42:00 +0100347 fio_gettime(&io_u->start_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200348 return io_u;
349}
350
351void io_completed(struct thread_data *td, struct io_u *io_u,
352 struct io_completion_data *icd)
353{
Jens Axboe10ba5352006-10-20 11:39:27 +0200354 unsigned long msec;
355
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200356 if (io_u->ddir == DDIR_SYNC) {
357 td->last_was_sync = 1;
358 return;
359 }
360
361 td->last_was_sync = 0;
362
Jens Axboe10ba5352006-10-20 11:39:27 +0200363 if (!io_u->error) {
364 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100365 const enum fio_ddir idx = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200366
367 td->io_blocks[idx]++;
368 td->io_bytes[idx] += bytes;
369 td->zone_bytes += bytes;
370 td->this_io_bytes[idx] += bytes;
371
Jens Axboe02bcaa82006-11-24 10:42:00 +0100372 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
373
374 msec = mtime_since(&io_u->issue_time, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200375
376 add_clat_sample(td, idx, msec);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100377 add_bw_sample(td, idx, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200378
379 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
380 log_io_piece(td, io_u);
381
382 icd->bytes_done[idx] += bytes;
383 } else
384 icd->error = io_u->error;
385}
386
387void ios_completed(struct thread_data *td, struct io_completion_data *icd)
388{
389 struct io_u *io_u;
390 int i;
391
Jens Axboe02bcaa82006-11-24 10:42:00 +0100392 fio_gettime(&icd->time, NULL);
393
Jens Axboe10ba5352006-10-20 11:39:27 +0200394 icd->error = 0;
395 icd->bytes_done[0] = icd->bytes_done[1] = 0;
396
397 for (i = 0; i < icd->nr; i++) {
398 io_u = td->io_ops->event(td, i);
399
400 io_completed(td, io_u, icd);
401 put_io_u(td, io_u);
402 }
403}