blob: b0e91e7a1bcc2b52d479df079e671629b8cf3ac8 [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
Jens Axboe97601022007-02-18 12:47:29 +010010struct io_completion_data {
11 int nr; /* input */
12 endio_handler *handler; /* input */
13
14 int error; /* output */
15 unsigned long bytes_done[2]; /* output */
16 struct timeval time; /* output */
17};
18
Jens Axboe10ba5352006-10-20 11:39:27 +020019/*
20 * The ->file_map[] contains a map of blocks we have or have not done io
21 * to yet. Used to make sure we cover the entire range in a fair fashion.
22 */
23static int random_map_free(struct thread_data *td, struct fio_file *f,
24 unsigned long long block)
25{
26 unsigned int idx = RAND_MAP_IDX(td, f, block);
27 unsigned int bit = RAND_MAP_BIT(td, f, block);
28
29 return (f->file_map[idx] & (1UL << bit)) == 0;
30}
31
32/*
Jens Axboedf415582006-10-20 11:41:03 +020033 * Mark a given offset as used in the map.
34 */
35static void mark_random_map(struct thread_data *td, struct fio_file *f,
36 struct io_u *io_u)
37{
Jens Axboeafdbe582007-02-10 19:01:57 +010038 unsigned int min_bs = td->rw_min_bs;
Jens Axboea00735e2006-11-03 08:58:08 +010039 unsigned long long block;
40 unsigned int blocks;
Jens Axboec685b5b2007-02-10 20:02:28 +010041 unsigned int nr_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020042
Jens Axboea00735e2006-11-03 08:58:08 +010043 block = io_u->offset / (unsigned long long) min_bs;
44 blocks = 0;
Jens Axboec685b5b2007-02-10 20:02:28 +010045 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
46
47 while (blocks < nr_blocks) {
Jens Axboedf415582006-10-20 11:41:03 +020048 unsigned int idx, bit;
49
50 if (!random_map_free(td, f, block))
51 break;
52
53 idx = RAND_MAP_IDX(td, f, block);
54 bit = RAND_MAP_BIT(td, f, block);
55
Jens Axboe0032bf92007-02-13 17:39:56 +010056 fio_assert(td, idx < f->num_maps);
Jens Axboedf415582006-10-20 11:41:03 +020057
58 f->file_map[idx] |= (1UL << bit);
59 block++;
60 blocks++;
61 }
62
Jens Axboea00735e2006-11-03 08:58:08 +010063 if ((blocks * min_bs) < io_u->buflen)
64 io_u->buflen = blocks * min_bs;
Jens Axboedf415582006-10-20 11:41:03 +020065}
66
67/*
Jens Axboe10ba5352006-10-20 11:39:27 +020068 * Return the next free block in the map.
69 */
70static int get_next_free_block(struct thread_data *td, struct fio_file *f,
71 unsigned long long *b)
72{
73 int i;
74
Jens Axboec685b5b2007-02-10 20:02:28 +010075 i = f->last_free_lookup;
76 *b = (i * BLOCKS_PER_MAP);
Jens Axboeee884702006-12-20 10:58:07 +010077 while ((*b) * td->rw_min_bs < f->real_file_size) {
Jens Axboe10ba5352006-10-20 11:39:27 +020078 if (f->file_map[i] != -1UL) {
79 *b += ffz(f->file_map[i]);
Jens Axboec685b5b2007-02-10 20:02:28 +010080 f->last_free_lookup = i;
Jens Axboe10ba5352006-10-20 11:39:27 +020081 return 0;
82 }
83
84 *b += BLOCKS_PER_MAP;
85 i++;
86 }
87
88 return 1;
89}
90
91/*
92 * For random io, generate a random new block and see if it's used. Repeat
93 * until we find a free one. For sequential io, just return the end of
94 * the last io issued.
95 */
96static int get_next_offset(struct thread_data *td, struct fio_file *f,
Jens Axboec685b5b2007-02-10 20:02:28 +010097 struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +020098{
Jens Axboec685b5b2007-02-10 20:02:28 +010099 const int ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200100 unsigned long long b, rb;
101 long r;
102
103 if (!td->sequential) {
Jens Axboef6971252006-11-15 10:00:31 +0100104 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
Jens Axboec685b5b2007-02-10 20:02:28 +0100105 int loops = 5;
Jens Axboe10ba5352006-10-20 11:39:27 +0200106
107 do {
108 r = os_random_long(&td->random_state);
109 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
Jens Axboebb8895e2006-10-30 15:14:48 +0100110 if (td->norandommap)
111 break;
Jens Axboea00735e2006-11-03 08:58:08 +0100112 rb = b + (f->file_offset / td->min_bs[ddir]);
Jens Axboe10ba5352006-10-20 11:39:27 +0200113 loops--;
114 } while (!random_map_free(td, f, rb) && loops);
115
Jens Axboebca4ed42007-02-12 05:13:23 +0100116 /*
117 * if we failed to retrieve a truly random offset within
118 * the loops assigned, see if there are free ones left at all
119 */
120 if (!loops && get_next_free_block(td, f, &b))
121 return 1;
Jens Axboe10ba5352006-10-20 11:39:27 +0200122 } else
Jens Axboea00735e2006-11-03 08:58:08 +0100123 b = f->last_pos / td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200124
Jens Axboec685b5b2007-02-10 20:02:28 +0100125 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
Jens Axboebca4ed42007-02-12 05:13:23 +0100126 if (io_u->offset >= f->real_file_size)
Jens Axboe10ba5352006-10-20 11:39:27 +0200127 return 1;
128
129 return 0;
130}
131
Jens Axboebca4ed42007-02-12 05:13:23 +0100132static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
133 struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200134{
Jens Axboebca4ed42007-02-12 05:13:23 +0100135 const int ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200136 unsigned int buflen;
137 long r;
138
Jens Axboea00735e2006-11-03 08:58:08 +0100139 if (td->min_bs[ddir] == td->max_bs[ddir])
140 buflen = td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200141 else {
142 r = os_random_long(&td->bsrange_state);
Jens Axboe1e97cce2006-12-05 11:44:16 +0100143 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
Jens Axboe690adba2006-10-30 15:25:09 +0100144 if (!td->bs_unaligned)
Jens Axboea00735e2006-11-03 08:58:08 +0100145 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
Jens Axboe10ba5352006-10-20 11:39:27 +0200146 }
147
Jens Axboebca4ed42007-02-12 05:13:23 +0100148 while (buflen + io_u->offset > f->real_file_size) {
Jens Axboebca4ed42007-02-12 05:13:23 +0100149 if (buflen == td->min_bs[ddir])
150 return 0;
151
152 buflen = td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200153 }
154
155 return buflen;
156}
157
158/*
159 * Return the data direction for the next io_u. If the job is a
160 * mixed read/write workload, check the rwmix cycle and switch if
161 * necessary.
162 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100163static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200164{
165 if (td_rw(td)) {
166 struct timeval now;
167 unsigned long elapsed;
168
Jens Axboe02bcaa82006-11-24 10:42:00 +0100169 fio_gettime(&now, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200170 elapsed = mtime_since_now(&td->rwmix_switch);
171
172 /*
173 * Check if it's time to seed a new data direction.
174 */
175 if (elapsed >= td->rwmixcycle) {
176 unsigned int v;
177 long r;
178
179 r = os_random_long(&td->rwmix_state);
180 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
181 if (v < td->rwmixread)
182 td->rwmix_ddir = DDIR_READ;
183 else
184 td->rwmix_ddir = DDIR_WRITE;
185 memcpy(&td->rwmix_switch, &now, sizeof(now));
186 }
187 return td->rwmix_ddir;
188 } else if (td_read(td))
189 return DDIR_READ;
190 else
191 return DDIR_WRITE;
192}
193
Jens Axboe10ba5352006-10-20 11:39:27 +0200194void put_io_u(struct thread_data *td, struct io_u *io_u)
195{
196 io_u->file = NULL;
197 list_del(&io_u->list);
198 list_add(&io_u->list, &td->io_u_freelist);
199 td->cur_depth--;
200}
201
202static int fill_io_u(struct thread_data *td, struct fio_file *f,
203 struct io_u *io_u)
204{
205 /*
206 * If using an iolog, grab next piece if any available.
207 */
208 if (td->read_iolog)
209 return read_iolog_get(td, io_u);
210
211 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200212 * see if it's time to sync
213 */
214 if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks)
215 && should_fsync(td)) {
216 io_u->ddir = DDIR_SYNC;
217 io_u->file = f;
218 return 0;
219 }
220
Jens Axboea00735e2006-11-03 08:58:08 +0100221 io_u->ddir = get_rw_ddir(td);
222
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200223 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100224 * No log, let the seq/rand engine retrieve the next buflen and
225 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200226 */
Jens Axboebca4ed42007-02-12 05:13:23 +0100227 if (get_next_offset(td, f, io_u))
228 return 1;
Jens Axboec685b5b2007-02-10 20:02:28 +0100229
Jens Axboebca4ed42007-02-12 05:13:23 +0100230 io_u->buflen = get_next_buflen(td, f, io_u);
231 if (!io_u->buflen)
232 return 1;
Jens Axboe10ba5352006-10-20 11:39:27 +0200233
Jens Axboebca4ed42007-02-12 05:13:23 +0100234 /*
235 * mark entry before potentially trimming io_u
236 */
237 if (!td->read_iolog && !td->sequential && !td->norandommap)
238 mark_random_map(td, f, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200239
Jens Axboebca4ed42007-02-12 05:13:23 +0100240 /*
241 * If using a write iolog, store this entry.
242 */
243 if (td->write_iolog_file)
244 write_iolog_put(td, io_u);
245
246 io_u->file = f;
247 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200248}
249
Jens Axboe71619dc2007-01-13 23:56:33 +0100250static void io_u_mark_depth(struct thread_data *td)
251{
252 int index = 0;
253
254 switch (td->cur_depth) {
255 default:
256 index++;
257 case 32 ... 63:
258 index++;
259 case 16 ... 31:
260 index++;
261 case 8 ... 15:
262 index++;
263 case 4 ... 7:
264 index++;
265 case 2 ... 3:
266 index++;
267 case 1:
268 break;
269 }
270
271 td->io_u_map[index]++;
272 td->total_io_u++;
273}
274
Jens Axboeec118302007-02-17 04:38:20 +0100275static void io_u_mark_latency(struct thread_data *td, unsigned long msec)
276{
277 int index = 0;
278
279 switch (msec) {
280 default:
281 index++;
282 case 1024 ... 2047:
283 index++;
284 case 512 ... 1023:
285 index++;
286 case 256 ... 511:
287 index++;
288 case 128 ... 255:
289 index++;
290 case 64 ... 127:
291 index++;
292 case 32 ... 63:
293 index++;
294 case 16 ... 31:
295 index++;
296 case 8 ... 15:
297 index++;
298 case 4 ... 7:
299 index++;
300 case 2 ... 3:
301 index++;
302 case 0 ... 1:
303 break;
304 }
305
306 td->io_u_lat[index]++;
307}
308
Jens Axboe10ba5352006-10-20 11:39:27 +0200309struct io_u *__get_io_u(struct thread_data *td)
310{
311 struct io_u *io_u = NULL;
312
313 if (!queue_full(td)) {
314 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
315
Jens Axboe6040dab2006-10-24 19:38:15 +0200316 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200317 io_u->error = 0;
318 io_u->resid = 0;
319 list_del(&io_u->list);
320 list_add(&io_u->list, &td->io_u_busylist);
321 td->cur_depth++;
Jens Axboe71619dc2007-01-13 23:56:33 +0100322 io_u_mark_depth(td);
Jens Axboe10ba5352006-10-20 11:39:27 +0200323 }
324
325 return io_u;
326}
327
328/*
329 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
330 * etc. The returned io_u is fully ready to be prepped and submitted.
331 */
332struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
333{
334 struct io_u *io_u;
335
336 io_u = __get_io_u(td);
337 if (!io_u)
338 return NULL;
339
340 if (td->zone_bytes >= td->zone_size) {
341 td->zone_bytes = 0;
342 f->last_pos += td->zone_skip;
343 }
344
345 if (fill_io_u(td, f, io_u)) {
346 put_io_u(td, io_u);
347 return NULL;
348 }
349
Jens Axboeee884702006-12-20 10:58:07 +0100350 if (io_u->buflen + io_u->offset > f->real_file_size) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200351 if (td->io_ops->flags & FIO_RAWIO) {
352 put_io_u(td, io_u);
353 return NULL;
354 }
355
Jens Axboeee884702006-12-20 10:58:07 +0100356 io_u->buflen = f->real_file_size - io_u->offset;
Jens Axboe10ba5352006-10-20 11:39:27 +0200357 }
358
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200359 if (io_u->ddir != DDIR_SYNC) {
360 if (!io_u->buflen) {
361 put_io_u(td, io_u);
362 return NULL;
363 }
364
Jens Axboe36167d82007-02-18 05:41:31 +0100365 f->last_pos = io_u->offset + io_u->buflen;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200366
367 if (td->verify != VERIFY_NONE)
368 populate_verify_io_u(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200369 }
370
Jens Axboe165faf12007-02-07 11:30:37 +0100371 /*
372 * Set io data pointers.
373 */
Jens Axboecec6b552007-02-06 20:15:38 +0100374 io_u->xfer_buf = io_u->buf;
375 io_u->xfer_buflen = io_u->buflen;
Jens Axboe165faf12007-02-07 11:30:37 +0100376
Jens Axboe36167d82007-02-18 05:41:31 +0100377 if (td_io_prep(td, io_u)) {
378 put_io_u(td, io_u);
379 return NULL;
380 }
381
Jens Axboe02bcaa82006-11-24 10:42:00 +0100382 fio_gettime(&io_u->start_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200383 return io_u;
384}
385
Jens Axboe97601022007-02-18 12:47:29 +0100386static void io_completed(struct thread_data *td, struct io_u *io_u,
387 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200388{
Jens Axboe10ba5352006-10-20 11:39:27 +0200389 unsigned long msec;
390
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200391 if (io_u->ddir == DDIR_SYNC) {
392 td->last_was_sync = 1;
393 return;
394 }
395
396 td->last_was_sync = 0;
397
Jens Axboe10ba5352006-10-20 11:39:27 +0200398 if (!io_u->error) {
399 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100400 const enum fio_ddir idx = io_u->ddir;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100401 int ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200402
403 td->io_blocks[idx]++;
404 td->io_bytes[idx] += bytes;
405 td->zone_bytes += bytes;
406 td->this_io_bytes[idx] += bytes;
407
Jens Axboe02bcaa82006-11-24 10:42:00 +0100408 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
409
410 msec = mtime_since(&io_u->issue_time, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200411
412 add_clat_sample(td, idx, msec);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100413 add_bw_sample(td, idx, &icd->time);
Jens Axboeec118302007-02-17 04:38:20 +0100414 io_u_mark_latency(td, msec);
Jens Axboe10ba5352006-10-20 11:39:27 +0200415
416 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
417 log_io_piece(td, io_u);
418
419 icd->bytes_done[idx] += bytes;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100420
421 if (icd->handler) {
422 ret = icd->handler(io_u);
423 if (ret && !icd->error)
424 icd->error = ret;
425 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200426 } else
427 icd->error = io_u->error;
428}
429
Jens Axboe97601022007-02-18 12:47:29 +0100430static void init_icd(struct io_completion_data *icd, endio_handler *handler,
431 int nr)
Jens Axboe36167d82007-02-18 05:41:31 +0100432{
433 fio_gettime(&icd->time, NULL);
434
Jens Axboe3af6ef32007-02-18 06:57:43 +0100435 icd->handler = handler;
436 icd->nr = nr;
437
Jens Axboe36167d82007-02-18 05:41:31 +0100438 icd->error = 0;
439 icd->bytes_done[0] = icd->bytes_done[1] = 0;
440}
441
Jens Axboe97601022007-02-18 12:47:29 +0100442static void ios_completed(struct thread_data *td,
443 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200444{
445 struct io_u *io_u;
446 int i;
447
Jens Axboe10ba5352006-10-20 11:39:27 +0200448 for (i = 0; i < icd->nr; i++) {
449 io_u = td->io_ops->event(td, i);
450
451 io_completed(td, io_u, icd);
452 put_io_u(td, io_u);
453 }
454}
Jens Axboe97601022007-02-18 12:47:29 +0100455
456long io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
457 endio_handler *handler)
458{
459 struct io_completion_data icd;
460
461 init_icd(&icd, handler, 1);
462 io_completed(td, io_u, &icd);
463 put_io_u(td, io_u);
464
465 if (!icd.error)
466 return icd.bytes_done[0] + icd.bytes_done[1];
467
468 td_verror(td, icd.error);
469 return -1;
470}
471
472long io_u_queued_complete(struct thread_data *td, int min_events,
473 endio_handler *handler)
474
475{
476 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
477 struct timespec *tsp = NULL;
478 struct io_completion_data icd;
479 int ret;
480
481 if (min_events > 0)
482 tsp = &ts;
483
484 ret = td_io_getevents(td, min_events, td->cur_depth, tsp);
485 if (ret < 0) {
486 td_verror(td, -ret);
487 return ret;
488 } else if (!ret)
489 return ret;
490
491 init_icd(&icd, handler, ret);
492 ios_completed(td, &icd);
493 if (!icd.error)
494 return icd.bytes_done[0] + icd.bytes_done[1];
495
496 td_verror(td, icd.error);
497 return -1;
498}