blob: ae92df5d1a4d2559fe5d6c47be5d017529181f8f [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>
Jens Axboe10ba5352006-10-20 11:39:27 +02006
7#include "fio.h"
8#include "os.h"
9
10/*
11 * The ->file_map[] contains a map of blocks we have or have not done io
12 * to yet. Used to make sure we cover the entire range in a fair fashion.
13 */
14static int random_map_free(struct thread_data *td, struct fio_file *f,
15 unsigned long long block)
16{
17 unsigned int idx = RAND_MAP_IDX(td, f, block);
18 unsigned int bit = RAND_MAP_BIT(td, f, block);
19
20 return (f->file_map[idx] & (1UL << bit)) == 0;
21}
22
23/*
Jens Axboedf415582006-10-20 11:41:03 +020024 * Mark a given offset as used in the map.
25 */
26static void mark_random_map(struct thread_data *td, struct fio_file *f,
27 struct io_u *io_u)
28{
Jens Axboeafdbe582007-02-10 19:01:57 +010029 unsigned int min_bs = td->rw_min_bs;
Jens Axboea00735e2006-11-03 08:58:08 +010030 unsigned long long block;
31 unsigned int blocks;
Jens Axboec685b5b2007-02-10 20:02:28 +010032 unsigned int nr_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020033
Jens Axboea00735e2006-11-03 08:58:08 +010034 block = io_u->offset / (unsigned long long) min_bs;
35 blocks = 0;
Jens Axboec685b5b2007-02-10 20:02:28 +010036 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
37
38 while (blocks < nr_blocks) {
Jens Axboedf415582006-10-20 11:41:03 +020039 unsigned int idx, bit;
40
41 if (!random_map_free(td, f, block))
42 break;
43
44 idx = RAND_MAP_IDX(td, f, block);
45 bit = RAND_MAP_BIT(td, f, block);
46
Jens Axboe0032bf92007-02-13 17:39:56 +010047 idx = f->num_maps;
48 fio_assert(td, idx < f->num_maps);
Jens Axboedf415582006-10-20 11:41:03 +020049
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 Axboebca4ed42007-02-12 05:13:23 +0100141 if (buflen == td->min_bs[ddir])
142 return 0;
143
144 buflen = td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200145 }
146
147 return buflen;
148}
149
150/*
151 * Return the data direction for the next io_u. If the job is a
152 * mixed read/write workload, check the rwmix cycle and switch if
153 * necessary.
154 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100155static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200156{
157 if (td_rw(td)) {
158 struct timeval now;
159 unsigned long elapsed;
160
Jens Axboe02bcaa82006-11-24 10:42:00 +0100161 fio_gettime(&now, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200162 elapsed = mtime_since_now(&td->rwmix_switch);
163
164 /*
165 * Check if it's time to seed a new data direction.
166 */
167 if (elapsed >= td->rwmixcycle) {
168 unsigned int v;
169 long r;
170
171 r = os_random_long(&td->rwmix_state);
172 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
173 if (v < td->rwmixread)
174 td->rwmix_ddir = DDIR_READ;
175 else
176 td->rwmix_ddir = DDIR_WRITE;
177 memcpy(&td->rwmix_switch, &now, sizeof(now));
178 }
179 return td->rwmix_ddir;
180 } else if (td_read(td))
181 return DDIR_READ;
182 else
183 return DDIR_WRITE;
184}
185
Jens Axboe10ba5352006-10-20 11:39:27 +0200186void put_io_u(struct thread_data *td, struct io_u *io_u)
187{
188 io_u->file = NULL;
189 list_del(&io_u->list);
190 list_add(&io_u->list, &td->io_u_freelist);
191 td->cur_depth--;
192}
193
194static int fill_io_u(struct thread_data *td, struct fio_file *f,
195 struct io_u *io_u)
196{
197 /*
198 * If using an iolog, grab next piece if any available.
199 */
200 if (td->read_iolog)
201 return read_iolog_get(td, io_u);
202
203 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200204 * see if it's time to sync
205 */
206 if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks)
207 && should_fsync(td)) {
208 io_u->ddir = DDIR_SYNC;
209 io_u->file = f;
210 return 0;
211 }
212
Jens Axboea00735e2006-11-03 08:58:08 +0100213 io_u->ddir = get_rw_ddir(td);
214
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200215 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100216 * No log, let the seq/rand engine retrieve the next buflen and
217 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200218 */
Jens Axboebca4ed42007-02-12 05:13:23 +0100219 if (get_next_offset(td, f, io_u))
220 return 1;
Jens Axboec685b5b2007-02-10 20:02:28 +0100221
Jens Axboebca4ed42007-02-12 05:13:23 +0100222 io_u->buflen = get_next_buflen(td, f, io_u);
223 if (!io_u->buflen)
224 return 1;
Jens Axboe10ba5352006-10-20 11:39:27 +0200225
Jens Axboebca4ed42007-02-12 05:13:23 +0100226 /*
227 * mark entry before potentially trimming io_u
228 */
229 if (!td->read_iolog && !td->sequential && !td->norandommap)
230 mark_random_map(td, f, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200231
Jens Axboebca4ed42007-02-12 05:13:23 +0100232 /*
233 * If using a write iolog, store this entry.
234 */
235 if (td->write_iolog_file)
236 write_iolog_put(td, io_u);
237
238 io_u->file = f;
239 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200240}
241
Jens Axboe71619dc2007-01-13 23:56:33 +0100242static void io_u_mark_depth(struct thread_data *td)
243{
244 int index = 0;
245
246 switch (td->cur_depth) {
247 default:
248 index++;
249 case 32 ... 63:
250 index++;
251 case 16 ... 31:
252 index++;
253 case 8 ... 15:
254 index++;
255 case 4 ... 7:
256 index++;
257 case 2 ... 3:
258 index++;
259 case 1:
260 break;
261 }
262
263 td->io_u_map[index]++;
264 td->total_io_u++;
265}
266
Jens Axboe10ba5352006-10-20 11:39:27 +0200267struct io_u *__get_io_u(struct thread_data *td)
268{
269 struct io_u *io_u = NULL;
270
271 if (!queue_full(td)) {
272 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
273
Jens Axboe6040dab2006-10-24 19:38:15 +0200274 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200275 io_u->error = 0;
276 io_u->resid = 0;
277 list_del(&io_u->list);
278 list_add(&io_u->list, &td->io_u_busylist);
279 td->cur_depth++;
Jens Axboe71619dc2007-01-13 23:56:33 +0100280 io_u_mark_depth(td);
Jens Axboe10ba5352006-10-20 11:39:27 +0200281 }
282
283 return io_u;
284}
285
286/*
287 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
288 * etc. The returned io_u is fully ready to be prepped and submitted.
289 */
290struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
291{
292 struct io_u *io_u;
293
294 io_u = __get_io_u(td);
295 if (!io_u)
296 return NULL;
297
298 if (td->zone_bytes >= td->zone_size) {
299 td->zone_bytes = 0;
300 f->last_pos += td->zone_skip;
301 }
302
303 if (fill_io_u(td, f, io_u)) {
304 put_io_u(td, io_u);
305 return NULL;
306 }
307
Jens Axboeee884702006-12-20 10:58:07 +0100308 if (io_u->buflen + io_u->offset > f->real_file_size) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200309 if (td->io_ops->flags & FIO_RAWIO) {
310 put_io_u(td, io_u);
311 return NULL;
312 }
313
Jens Axboeee884702006-12-20 10:58:07 +0100314 io_u->buflen = f->real_file_size - io_u->offset;
Jens Axboe10ba5352006-10-20 11:39:27 +0200315 }
316
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200317 if (io_u->ddir != DDIR_SYNC) {
318 if (!io_u->buflen) {
319 put_io_u(td, io_u);
320 return NULL;
321 }
322
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200323 f->last_pos += io_u->buflen;
324
325 if (td->verify != VERIFY_NONE)
326 populate_verify_io_u(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200327 }
328
Jens Axboe10ba5352006-10-20 11:39:27 +0200329 if (td_io_prep(td, io_u)) {
330 put_io_u(td, io_u);
331 return NULL;
332 }
333
Jens Axboe165faf12007-02-07 11:30:37 +0100334 /*
335 * Set io data pointers.
336 */
Jens Axboecec6b552007-02-06 20:15:38 +0100337 io_u->xfer_buf = io_u->buf;
338 io_u->xfer_buflen = io_u->buflen;
Jens Axboe165faf12007-02-07 11:30:37 +0100339
Jens Axboe02bcaa82006-11-24 10:42:00 +0100340 fio_gettime(&io_u->start_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200341 return io_u;
342}
343
344void io_completed(struct thread_data *td, struct io_u *io_u,
345 struct io_completion_data *icd)
346{
Jens Axboe10ba5352006-10-20 11:39:27 +0200347 unsigned long msec;
348
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200349 if (io_u->ddir == DDIR_SYNC) {
350 td->last_was_sync = 1;
351 return;
352 }
353
354 td->last_was_sync = 0;
355
Jens Axboe10ba5352006-10-20 11:39:27 +0200356 if (!io_u->error) {
357 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100358 const enum fio_ddir idx = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200359
360 td->io_blocks[idx]++;
361 td->io_bytes[idx] += bytes;
362 td->zone_bytes += bytes;
363 td->this_io_bytes[idx] += bytes;
364
Jens Axboe02bcaa82006-11-24 10:42:00 +0100365 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
366
367 msec = mtime_since(&io_u->issue_time, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200368
369 add_clat_sample(td, idx, msec);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100370 add_bw_sample(td, idx, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200371
372 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
373 log_io_piece(td, io_u);
374
375 icd->bytes_done[idx] += bytes;
376 } else
377 icd->error = io_u->error;
378}
379
380void ios_completed(struct thread_data *td, struct io_completion_data *icd)
381{
382 struct io_u *io_u;
383 int i;
384
Jens Axboe02bcaa82006-11-24 10:42:00 +0100385 fio_gettime(&icd->time, NULL);
386
Jens Axboe10ba5352006-10-20 11:39:27 +0200387 icd->error = 0;
388 icd->bytes_done[0] = icd->bytes_done[1] = 0;
389
390 for (i = 0; i < icd->nr; i++) {
391 io_u = td->io_ops->event(td, i);
392
393 io_completed(td, io_u, icd);
394 put_io_u(td, io_u);
395 }
396}