blob: b452c5f03b3934b8b0e40198df525980578735bc [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
Jens Axboeebac4652005-12-08 15:25:21 +010021#include <unistd.h>
22#include <fcntl.h>
23#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <signal.h>
25#include <time.h>
Jens Axboeebac4652005-12-08 15:25:21 +010026#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <sys/stat.h>
28#include <sys/wait.h>
29#include <sys/ipc.h>
30#include <sys/shm.h>
31#include <sys/ioctl.h>
32#include <sys/mman.h>
33
34#include "fio.h"
35#include "os.h"
36
37#define MASK (4095)
38
39#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
40
41int groupid = 0;
42int thread_number = 0;
43static char run_str[MAX_JOBS + 1];
44int shm_id = 0;
Jens Axboe5289b842005-12-08 20:47:28 +010045static struct timeval genesis;
Jens Axboeebac4652005-12-08 15:25:21 +010046
Jens Axboeebac4652005-12-08 15:25:21 +010047static void print_thread_status(void);
48
Jens Axboec04f7ec2006-05-31 10:13:16 +020049extern unsigned long long mlock_size;
50
Jens Axboeebac4652005-12-08 15:25:21 +010051/*
52 * thread life cycle
53 */
54enum {
55 TD_NOT_CREATED = 0,
56 TD_CREATED,
Jens Axboe75154842006-06-01 13:56:09 +020057 TD_INITIALIZED,
Jens Axboeebac4652005-12-08 15:25:21 +010058 TD_RUNNING,
59 TD_VERIFYING,
Jens Axboe5853e5a2006-06-08 14:41:05 +020060 TD_FSYNCING,
Jens Axboeebac4652005-12-08 15:25:21 +010061 TD_EXITED,
62 TD_REAPED,
63};
64
Jens Axboe3d60d1e2006-05-25 06:31:06 +020065#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
Jens Axboeebac4652005-12-08 15:25:21 +010066
Jens Axboebbfd6b02006-06-07 19:42:54 +020067static volatile int startup_sem;
Jens Axboeebac4652005-12-08 15:25:21 +010068
69#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020070#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010071
72static void terminate_threads(int group_id)
73{
74 int i;
75
76 for (i = 0; i < thread_number; i++) {
77 struct thread_data *td = &threads[i];
78
79 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
80 td->terminate = 1;
81 td->start_delay = 0;
82 }
83 }
84}
85
86static void sig_handler(int sig)
87{
88 switch (sig) {
89 case SIGALRM:
90 update_io_ticks();
91 disk_util_timer_arm();
92 print_thread_status();
93 break;
94 default:
95 printf("\nfio: terminating on signal\n");
96 fflush(stdout);
97 terminate_threads(TERMINATE_ALL);
98 break;
99 }
100}
101
Jens Axboeebac4652005-12-08 15:25:21 +0100102static int random_map_free(struct thread_data *td, unsigned long long block)
103{
104 unsigned int idx = RAND_MAP_IDX(td, block);
105 unsigned int bit = RAND_MAP_BIT(td, block);
106
107 return (td->file_map[idx] & (1UL << bit)) == 0;
108}
109
110static int get_next_free_block(struct thread_data *td, unsigned long long *b)
111{
112 int i;
113
114 *b = 0;
115 i = 0;
116 while ((*b) * td->min_bs < td->io_size) {
117 if (td->file_map[i] != -1UL) {
118 *b += ffz(td->file_map[i]);
119 return 0;
120 }
121
122 *b += BLOCKS_PER_MAP;
123 i++;
124 }
125
126 return 1;
127}
128
129static void mark_random_map(struct thread_data *td, struct io_u *io_u)
130{
Jens Axboe200bc852006-06-01 16:10:12 +0200131 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100132 unsigned int blocks = 0;
133
134 while (blocks < (io_u->buflen / td->min_bs)) {
135 unsigned int idx, bit;
136
137 if (!random_map_free(td, block))
138 break;
139
140 idx = RAND_MAP_IDX(td, block);
141 bit = RAND_MAP_BIT(td, block);
142
143 assert(idx < td->num_maps);
144
145 td->file_map[idx] |= (1UL << bit);
146 block++;
147 blocks++;
148 }
149
150 if ((blocks * td->min_bs) < io_u->buflen)
151 io_u->buflen = blocks * td->min_bs;
152}
153
Jens Axboe20dc95c2005-12-09 10:29:35 +0100154static int get_next_offset(struct thread_data *td, unsigned long long *offset)
155{
156 unsigned long long b, rb;
157 long r;
158
159 if (!td->sequential) {
Jens Axboe085227a2006-06-01 15:54:09 -0700160 unsigned long long max_blocks = td->io_size / td->min_bs;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100161 int loops = 50;
162
163 do {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200164 r = os_random_long(&td->random_state);
Jens Axboe085227a2006-06-01 15:54:09 -0700165 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
Jens Axboe20dc95c2005-12-09 10:29:35 +0100166 rb = b + (td->file_offset / td->min_bs);
167 loops--;
168 } while (!random_map_free(td, rb) && loops);
169
170 if (!loops) {
171 if (get_next_free_block(td, &b))
172 return 1;
173 }
174 } else
175 b = td->last_pos / td->min_bs;
176
177 *offset = (b * td->min_bs) + td->file_offset;
Jens Axboe838a3cd2006-01-20 12:38:29 +0100178 if (*offset > td->real_file_size)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100179 return 1;
180
181 return 0;
182}
183
184static unsigned int get_next_buflen(struct thread_data *td)
185{
186 unsigned int buflen;
187 long r;
188
189 if (td->min_bs == td->max_bs)
190 buflen = td->min_bs;
191 else {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200192 r = os_random_long(&td->bsrange_state);
Jens Axboe20dc95c2005-12-09 10:29:35 +0100193 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
194 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
195 }
196
197 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
198 buflen = td->io_size - td->this_io_bytes[td->ddir];
199
200 return buflen;
201}
202
Jens Axboeebac4652005-12-08 15:25:21 +0100203static int check_min_rate(struct thread_data *td, struct timeval *now)
204{
205 unsigned long spent;
206 unsigned long rate;
207 int ddir = td->ddir;
208
209 /*
210 * allow a 2 second settle period in the beginning
211 */
212 if (mtime_since(&td->start, now) < 2000)
213 return 0;
214
215 /*
216 * if rate blocks is set, sample is running
217 */
218 if (td->rate_bytes) {
219 spent = mtime_since(&td->lastrate, now);
220 if (spent < td->ratecycle)
221 return 0;
222
223 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
224 if (rate < td->ratemin) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200225 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
Jens Axboeebac4652005-12-08 15:25:21 +0100226 if (rate_quit)
227 terminate_threads(td->groupid);
228 return 1;
229 }
230 }
231
232 td->rate_bytes = td->this_io_bytes[ddir];
233 memcpy(&td->lastrate, now, sizeof(*now));
234 return 0;
235}
236
237static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
238{
239 if (!td->timeout)
240 return 0;
241 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
242 return 1;
243
244 return 0;
245}
246
247static void fill_random_bytes(struct thread_data *td,
248 unsigned char *p, unsigned int len)
249{
250 unsigned int todo;
251 double r;
252
253 while (len) {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200254 r = os_random_double(&td->verify_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100255
256 /*
257 * lrand48_r seems to be broken and only fill the bottom
258 * 32-bits, even on 64-bit archs with 64-bit longs
259 */
260 todo = sizeof(r);
261 if (todo > len)
262 todo = len;
263
264 memcpy(p, &r, todo);
265
266 len -= todo;
267 p += todo;
268 }
269}
270
271static void hexdump(void *buffer, int len)
272{
273 unsigned char *p = buffer;
274 int i;
275
276 for (i = 0; i < len; i++)
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200277 fprintf(f_out, "%02x", p[i]);
278 fprintf(f_out, "\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100279}
280
281static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
282{
283 unsigned char *p = (unsigned char *) io_u->buf;
284 unsigned long c;
Jens Axboeebac4652005-12-08 15:25:21 +0100285
286 p += sizeof(*hdr);
287 c = crc32(p, hdr->len - sizeof(*hdr));
Jens Axboeebac4652005-12-08 15:25:21 +0100288
Jens Axboe22f78b32006-06-07 11:30:07 +0200289 if (c != hdr->crc32) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200290 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
291 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
Jens Axboe22f78b32006-06-07 11:30:07 +0200292 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100293 }
294
Jens Axboe22f78b32006-06-07 11:30:07 +0200295 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100296}
297
298static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
299{
300 unsigned char *p = (unsigned char *) io_u->buf;
301 struct md5_ctx md5_ctx;
Jens Axboeebac4652005-12-08 15:25:21 +0100302
303 memset(&md5_ctx, 0, sizeof(md5_ctx));
304 p += sizeof(*hdr);
305 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
306
Jens Axboe22f78b32006-06-07 11:30:07 +0200307 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200308 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
Jens Axboeebac4652005-12-08 15:25:21 +0100309 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
310 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
Jens Axboe22f78b32006-06-07 11:30:07 +0200311 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100312 }
313
Jens Axboe22f78b32006-06-07 11:30:07 +0200314 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100315}
316
317static int verify_io_u(struct io_u *io_u)
318{
319 struct verify_header *hdr = (struct verify_header *) io_u->buf;
320 int ret;
321
322 if (hdr->fio_magic != FIO_HDR_MAGIC)
323 return 1;
324
325 if (hdr->verify_type == VERIFY_MD5)
326 ret = verify_io_u_md5(hdr, io_u);
327 else if (hdr->verify_type == VERIFY_CRC32)
328 ret = verify_io_u_crc32(hdr, io_u);
329 else {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200330 log_err("Bad verify type %d\n", hdr->verify_type);
Jens Axboeebac4652005-12-08 15:25:21 +0100331 ret = 1;
332 }
333
334 return ret;
335}
336
337static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
338{
339 hdr->crc32 = crc32(p, len);
340}
341
342static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
343{
344 struct md5_ctx md5_ctx;
345
346 memset(&md5_ctx, 0, sizeof(md5_ctx));
347 md5_update(&md5_ctx, p, len);
348 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
349}
350
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200351static int get_rw_ddir(struct thread_data *td)
352{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200353 if (td_rw(td)) {
354 struct timeval now;
355 unsigned long elapsed;
356
357 gettimeofday(&now, NULL);
358 elapsed = mtime_since_now(&td->rwmix_switch);
359
360 /*
361 * Check if it's time to seed a new data direction.
362 */
363 if (elapsed >= td->rwmixcycle) {
Jens Axboec1ee2ca2006-06-07 22:42:37 +0200364 int v;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200365 long r;
366
Jens Axboec1ee2ca2006-06-07 22:42:37 +0200367 r = os_random_long(&td->rwmix_state);
368 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200369 if (v < td->rwmixread)
370 td->rwmix_ddir = DDIR_READ;
371 else
372 td->rwmix_ddir = DDIR_WRITE;
373 memcpy(&td->rwmix_switch, &now, sizeof(now));
374 }
375 return td->rwmix_ddir;
376 } else if (td_read(td))
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200377 return DDIR_READ;
378 else
379 return DDIR_WRITE;
380}
381
Jens Axboeebac4652005-12-08 15:25:21 +0100382/*
383 * fill body of io_u->buf with random data and add a header with the
Jens Axboe22f78b32006-06-07 11:30:07 +0200384 * crc32 or md5 sum of that data.
Jens Axboeebac4652005-12-08 15:25:21 +0100385 */
386static void populate_io_u(struct thread_data *td, struct io_u *io_u)
387{
388 unsigned char *p = (unsigned char *) io_u->buf;
389 struct verify_header hdr;
390
391 hdr.fio_magic = FIO_HDR_MAGIC;
392 hdr.len = io_u->buflen;
393 p += sizeof(hdr);
394 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
395
396 if (td->verify == VERIFY_MD5) {
397 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
398 hdr.verify_type = VERIFY_MD5;
399 } else {
400 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
401 hdr.verify_type = VERIFY_CRC32;
402 }
403
404 memcpy(io_u->buf, &hdr, sizeof(hdr));
405}
406
Jens Axboeaea47d42006-05-26 19:27:29 +0200407static int td_io_prep(struct thread_data *td, struct io_u *io_u)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100408{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100409 if (td->io_prep && td->io_prep(td, io_u))
410 return 1;
411
412 return 0;
413}
414
Jens Axboeb1ff3402006-02-17 11:35:58 +0100415void put_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100416{
417 list_del(&io_u->list);
418 list_add(&io_u->list, &td->io_u_freelist);
419 td->cur_depth--;
420}
421
Jens Axboe843a7412006-06-01 21:14:21 -0700422static int fill_io_u(struct thread_data *td, struct io_u *io_u)
423{
424 /*
425 * If using an iolog, grab next piece if any available.
426 */
427 if (td->read_iolog)
428 return read_iolog_get(td, io_u);
429
Jens Axboeaea47d42006-05-26 19:27:29 +0200430 /*
431 * No log, let the seq/rand engine retrieve the next position.
432 */
433 if (!get_next_offset(td, &io_u->offset)) {
434 io_u->buflen = get_next_buflen(td);
435
436 if (io_u->buflen) {
437 io_u->ddir = get_rw_ddir(td);
Jens Axboe843a7412006-06-01 21:14:21 -0700438
439 /*
440 * If using a write iolog, store this entry.
441 */
442 if (td->write_iolog)
443 write_iolog_put(td, io_u);
444
Jens Axboeaea47d42006-05-26 19:27:29 +0200445 return 0;
446 }
447 }
448
449 return 1;
450}
451
Jens Axboe22f78b32006-06-07 11:30:07 +0200452#define queue_full(td) list_empty(&(td)->io_u_freelist)
Jens Axboeebac4652005-12-08 15:25:21 +0100453
Jens Axboeb1ff3402006-02-17 11:35:58 +0100454struct io_u *__get_io_u(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100455{
Jens Axboe22f78b32006-06-07 11:30:07 +0200456 struct io_u *io_u = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100457
Jens Axboe22f78b32006-06-07 11:30:07 +0200458 if (!queue_full(td)) {
459 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100460
Jens Axboe22f78b32006-06-07 11:30:07 +0200461 io_u->error = 0;
462 io_u->resid = 0;
463 list_del(&io_u->list);
464 list_add(&io_u->list, &td->io_u_busylist);
465 td->cur_depth++;
466 }
467
Jens Axboeebac4652005-12-08 15:25:21 +0100468 return io_u;
469}
470
Jens Axboeebac4652005-12-08 15:25:21 +0100471static struct io_u *get_io_u(struct thread_data *td)
472{
473 struct io_u *io_u;
474
475 io_u = __get_io_u(td);
476 if (!io_u)
477 return NULL;
478
Jens Axboe20dc95c2005-12-09 10:29:35 +0100479 if (td->zone_bytes >= td->zone_size) {
480 td->zone_bytes = 0;
481 td->last_pos += td->zone_skip;
482 }
483
Jens Axboeaea47d42006-05-26 19:27:29 +0200484 if (fill_io_u(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100485 put_io_u(td, io_u);
486 return NULL;
487 }
488
Jens Axboe838a3cd2006-01-20 12:38:29 +0100489 if (io_u->buflen + io_u->offset > td->real_file_size)
490 io_u->buflen = td->real_file_size - io_u->offset;
Jens Axboeebac4652005-12-08 15:25:21 +0100491
492 if (!io_u->buflen) {
493 put_io_u(td, io_u);
494 return NULL;
495 }
496
Jens Axboe843a7412006-06-01 21:14:21 -0700497 if (!td->read_iolog && !td->sequential)
Jens Axboeebac4652005-12-08 15:25:21 +0100498 mark_random_map(td, io_u);
499
Jens Axboe20dc95c2005-12-09 10:29:35 +0100500 td->last_pos += io_u->buflen;
Jens Axboeebac4652005-12-08 15:25:21 +0100501
502 if (td->verify != VERIFY_NONE)
503 populate_io_u(td, io_u);
504
Jens Axboeaea47d42006-05-26 19:27:29 +0200505 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100506 put_io_u(td, io_u);
507 return NULL;
508 }
509
510 gettimeofday(&io_u->start_time, NULL);
511 return io_u;
512}
513
514static inline void td_set_runstate(struct thread_data *td, int runstate)
515{
Jens Axboeebac4652005-12-08 15:25:21 +0100516 td->runstate = runstate;
517}
518
Jens Axboeaea47d42006-05-26 19:27:29 +0200519static int get_next_verify(struct thread_data *td, struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100520{
521 struct io_piece *ipo;
522
Jens Axboe22f78b32006-06-07 11:30:07 +0200523 if (!list_empty(&td->io_hist_list)) {
524 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100525
Jens Axboe22f78b32006-06-07 11:30:07 +0200526 list_del(&ipo->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100527
Jens Axboe22f78b32006-06-07 11:30:07 +0200528 io_u->offset = ipo->offset;
529 io_u->buflen = ipo->len;
530 io_u->ddir = DDIR_READ;
531 free(ipo);
532 return 0;
533 }
534
535 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100536}
537
Jens Axboeebac4652005-12-08 15:25:21 +0100538static int sync_td(struct thread_data *td)
539{
540 if (td->io_sync)
541 return td->io_sync(td);
542
543 return 0;
544}
545
546static int io_u_getevents(struct thread_data *td, int min, int max,
547 struct timespec *t)
548{
549 return td->io_getevents(td, min, max, t);
550}
551
552static int io_u_queue(struct thread_data *td, struct io_u *io_u)
553{
554 gettimeofday(&io_u->issue_time, NULL);
555
556 return td->io_queue(td, io_u);
557}
558
559#define iocb_time(iocb) ((unsigned long) (iocb)->data)
560
561static void io_completed(struct thread_data *td, struct io_u *io_u,
562 struct io_completion_data *icd)
563{
564 struct timeval e;
565 unsigned long msec;
566
567 gettimeofday(&e, NULL);
568
569 if (!io_u->error) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100570 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200571 const int idx = io_u->ddir;
Jens Axboeebac4652005-12-08 15:25:21 +0100572
573 td->io_blocks[idx]++;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100574 td->io_bytes[idx] += bytes;
575 td->zone_bytes += bytes;
576 td->this_io_bytes[idx] += bytes;
Jens Axboeebac4652005-12-08 15:25:21 +0100577
578 msec = mtime_since(&io_u->issue_time, &e);
579
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200580 add_clat_sample(td, idx, msec);
581 add_bw_sample(td, idx);
Jens Axboeebac4652005-12-08 15:25:21 +0100582
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200583 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
Jens Axboeebac4652005-12-08 15:25:21 +0100584 log_io_piece(td, io_u);
585
Jens Axboe20dc95c2005-12-09 10:29:35 +0100586 icd->bytes_done[idx] += bytes;
Jens Axboeebac4652005-12-08 15:25:21 +0100587 } else
588 icd->error = io_u->error;
589}
590
591static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
592{
593 struct io_u *io_u;
594 int i;
595
596 icd->error = 0;
597 icd->bytes_done[0] = icd->bytes_done[1] = 0;
598
599 for (i = 0; i < icd->nr; i++) {
600 io_u = td->io_event(td, i);
601
602 io_completed(td, io_u, icd);
603 put_io_u(td, io_u);
604 }
605}
606
607static void cleanup_pending_aio(struct thread_data *td)
608{
609 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
610 struct list_head *entry, *n;
611 struct io_completion_data icd;
612 struct io_u *io_u;
613 int r;
614
615 /*
616 * get immediately available events, if any
617 */
618 r = io_u_getevents(td, 0, td->cur_depth, &ts);
619 if (r > 0) {
620 icd.nr = r;
621 ios_completed(td, &icd);
622 }
623
624 /*
625 * now cancel remaining active events
626 */
627 if (td->io_cancel) {
628 list_for_each_safe(entry, n, &td->io_u_busylist) {
629 io_u = list_entry(entry, struct io_u, list);
630
631 r = td->io_cancel(td, io_u);
632 if (!r)
633 put_io_u(td, io_u);
634 }
635 }
636
637 if (td->cur_depth) {
638 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
639 if (r > 0) {
640 icd.nr = r;
641 ios_completed(td, &icd);
642 }
643 }
644}
645
646static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
647{
648 struct io_u *v_io_u = *io_u;
649 int ret = 0;
650
651 if (v_io_u) {
652 ret = verify_io_u(v_io_u);
653 put_io_u(td, v_io_u);
654 *io_u = NULL;
655 }
656
657 return ret;
658}
659
660static void do_verify(struct thread_data *td)
661{
662 struct timeval t;
663 struct io_u *io_u, *v_io_u = NULL;
664 struct io_completion_data icd;
665 int ret;
666
667 td_set_runstate(td, TD_VERIFYING);
668
669 do {
670 if (td->terminate)
671 break;
672
673 gettimeofday(&t, NULL);
674 if (runtime_exceeded(td, &t))
675 break;
676
677 io_u = __get_io_u(td);
678 if (!io_u)
679 break;
680
Jens Axboeaea47d42006-05-26 19:27:29 +0200681 if (get_next_verify(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100682 put_io_u(td, io_u);
683 break;
684 }
685
Jens Axboeaea47d42006-05-26 19:27:29 +0200686 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100687 put_io_u(td, io_u);
688 break;
689 }
690
691 ret = io_u_queue(td, io_u);
692 if (ret) {
693 put_io_u(td, io_u);
694 td_verror(td, ret);
695 break;
696 }
697
698 /*
699 * we have one pending to verify, do that while
700 * we are doing io on the next one
701 */
702 if (do_io_u_verify(td, &v_io_u))
703 break;
704
705 ret = io_u_getevents(td, 1, 1, NULL);
706 if (ret != 1) {
707 if (ret < 0)
708 td_verror(td, ret);
709 break;
710 }
711
712 v_io_u = td->io_event(td, 0);
713 icd.nr = 1;
714 icd.error = 0;
715 io_completed(td, v_io_u, &icd);
716
717 if (icd.error) {
718 td_verror(td, icd.error);
719 put_io_u(td, v_io_u);
720 v_io_u = NULL;
721 break;
722 }
723
Jens Axboeebac4652005-12-08 15:25:21 +0100724 /*
725 * if we can't submit more io, we need to verify now
726 */
727 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
728 break;
729
730 } while (1);
731
732 do_io_u_verify(td, &v_io_u);
733
734 if (td->cur_depth)
735 cleanup_pending_aio(td);
736
737 td_set_runstate(td, TD_RUNNING);
738}
739
Jens Axboe32cd46a2006-06-07 13:40:40 +0200740/*
741 * Main IO worker functions. It retrieves io_u's to process and queues
742 * and reaps them, checking for rate and errors along the way.
743 */
Jens Axboeebac4652005-12-08 15:25:21 +0100744static void do_io(struct thread_data *td)
745{
746 struct io_completion_data icd;
747 struct timeval s, e;
748 unsigned long usec;
749
Jens Axboe5853e5a2006-06-08 14:41:05 +0200750 td_set_runstate(td, TD_RUNNING);
751
Jens Axboeebac4652005-12-08 15:25:21 +0100752 while (td->this_io_bytes[td->ddir] < td->io_size) {
753 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
754 struct timespec *timeout;
755 int ret, min_evts = 0;
756 struct io_u *io_u;
757
758 if (td->terminate)
759 break;
760
761 io_u = get_io_u(td);
762 if (!io_u)
763 break;
764
765 memcpy(&s, &io_u->start_time, sizeof(s));
766
767 ret = io_u_queue(td, io_u);
768 if (ret) {
769 put_io_u(td, io_u);
770 td_verror(td, ret);
771 break;
772 }
773
774 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
775
776 if (td->cur_depth < td->iodepth) {
777 timeout = &ts;
778 min_evts = 0;
779 } else {
780 timeout = NULL;
781 min_evts = 1;
782 }
783
784 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
785 if (ret < 0) {
786 td_verror(td, ret);
787 break;
788 } else if (!ret)
789 continue;
790
791 icd.nr = ret;
792 ios_completed(td, &icd);
793 if (icd.error) {
794 td_verror(td, icd.error);
795 break;
796 }
797
798 /*
799 * the rate is batched for now, it should work for batches
800 * of completions except the very first one which may look
801 * a little bursty
802 */
803 gettimeofday(&e, NULL);
804 usec = utime_since(&s, &e);
805
806 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
807
808 if (check_min_rate(td, &e)) {
809 td_verror(td, ENOMEM);
810 break;
811 }
812
813 if (runtime_exceeded(td, &e))
814 break;
815
816 if (td->thinktime)
817 usec_sleep(td, td->thinktime);
818
819 if (should_fsync(td) && td->fsync_blocks &&
820 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
821 sync_td(td);
822 }
823
824 if (td->cur_depth)
825 cleanup_pending_aio(td);
826
Jens Axboe5853e5a2006-06-08 14:41:05 +0200827 if (should_fsync(td) && td->end_fsync) {
828 td_set_runstate(td, TD_FSYNCING);
Jens Axboeebac4652005-12-08 15:25:21 +0100829 sync_td(td);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200830 }
Jens Axboeebac4652005-12-08 15:25:21 +0100831}
832
833static void cleanup_io(struct thread_data *td)
834{
835 if (td->io_cleanup)
836 td->io_cleanup(td);
837}
838
839static int init_io(struct thread_data *td)
840{
841 if (td->io_engine == FIO_SYNCIO)
842 return fio_syncio_init(td);
843 else if (td->io_engine == FIO_MMAPIO)
844 return fio_mmapio_init(td);
845 else if (td->io_engine == FIO_LIBAIO)
846 return fio_libaio_init(td);
847 else if (td->io_engine == FIO_POSIXAIO)
848 return fio_posixaio_init(td);
849 else if (td->io_engine == FIO_SGIO)
850 return fio_sgio_init(td);
Jens Axboe8756e4d2006-05-27 20:24:53 +0200851 else if (td->io_engine == FIO_SPLICEIO)
852 return fio_spliceio_init(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100853 else {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200854 log_err("bad io_engine %d\n", td->io_engine);
Jens Axboeebac4652005-12-08 15:25:21 +0100855 return 1;
856 }
857}
858
859static void cleanup_io_u(struct thread_data *td)
860{
861 struct list_head *entry, *n;
862 struct io_u *io_u;
863
864 list_for_each_safe(entry, n, &td->io_u_freelist) {
865 io_u = list_entry(entry, struct io_u, list);
866
867 list_del(&io_u->list);
868 free(io_u);
869 }
870
871 if (td->mem_type == MEM_MALLOC)
872 free(td->orig_buffer);
873 else if (td->mem_type == MEM_SHM) {
874 struct shmid_ds sbuf;
875
876 shmdt(td->orig_buffer);
877 shmctl(td->shm_id, IPC_RMID, &sbuf);
878 } else if (td->mem_type == MEM_MMAP)
879 munmap(td->orig_buffer, td->orig_buffer_size);
880 else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200881 log_err("Bad memory type %d\n", td->mem_type);
Jens Axboeebac4652005-12-08 15:25:21 +0100882
883 td->orig_buffer = NULL;
884}
885
886static int init_io_u(struct thread_data *td)
887{
888 struct io_u *io_u;
889 int i, max_units;
890 char *p;
891
892 if (td->io_engine & FIO_SYNCIO)
893 max_units = 1;
894 else
895 max_units = td->iodepth;
896
897 td->orig_buffer_size = td->max_bs * max_units + MASK;
898
899 if (td->mem_type == MEM_MALLOC)
900 td->orig_buffer = malloc(td->orig_buffer_size);
901 else if (td->mem_type == MEM_SHM) {
902 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
903 if (td->shm_id < 0) {
904 td_verror(td, errno);
905 perror("shmget");
906 return 1;
907 }
908
909 td->orig_buffer = shmat(td->shm_id, NULL, 0);
910 if (td->orig_buffer == (void *) -1) {
911 td_verror(td, errno);
912 perror("shmat");
913 td->orig_buffer = NULL;
914 return 1;
915 }
916 } else if (td->mem_type == MEM_MMAP) {
917 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
918 if (td->orig_buffer == MAP_FAILED) {
919 td_verror(td, errno);
920 perror("mmap");
921 td->orig_buffer = NULL;
922 return 1;
923 }
924 }
925
Jens Axboeebac4652005-12-08 15:25:21 +0100926 p = ALIGN(td->orig_buffer);
927 for (i = 0; i < max_units; i++) {
928 io_u = malloc(sizeof(*io_u));
929 memset(io_u, 0, sizeof(*io_u));
930 INIT_LIST_HEAD(&io_u->list);
931
932 io_u->buf = p + td->max_bs * i;
Jens Axboeb1ff3402006-02-17 11:35:58 +0100933 io_u->index = i;
Jens Axboeebac4652005-12-08 15:25:21 +0100934 list_add(&io_u->list, &td->io_u_freelist);
935 }
936
937 return 0;
938}
939
940static int create_file(struct thread_data *td, unsigned long long size,
941 int extend)
942{
943 unsigned long long left;
944 unsigned int bs;
945 int r, oflags;
946 char *b;
947
948 /*
949 * unless specifically asked for overwrite, let normal io extend it
950 */
951 if (td_write(td) && !td->overwrite)
952 return 0;
953
954 if (!size) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200955 log_err("Need size for create\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100956 td_verror(td, EINVAL);
957 return 1;
958 }
959
960 if (!extend) {
961 oflags = O_CREAT | O_TRUNC;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200962 fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
Jens Axboeebac4652005-12-08 15:25:21 +0100963 } else {
964 oflags = O_APPEND;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200965 fprintf(f_out, "%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20);
Jens Axboeebac4652005-12-08 15:25:21 +0100966 }
967
968 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
969 if (td->fd < 0) {
970 td_verror(td, errno);
971 return 1;
972 }
973
974 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
975 td_verror(td, errno);
976 return 1;
977 }
978
979 td->io_size = td->file_size;
980 b = malloc(td->max_bs);
981 memset(b, 0, td->max_bs);
982
983 left = size;
984 while (left && !td->terminate) {
985 bs = td->max_bs;
986 if (bs > left)
987 bs = left;
988
989 r = write(td->fd, b, bs);
990
991 if (r == (int) bs) {
992 left -= bs;
993 continue;
994 } else {
995 if (r < 0)
996 td_verror(td, errno);
997 else
998 td_verror(td, EIO);
999
1000 break;
1001 }
1002 }
1003
1004 if (td->terminate)
1005 unlink(td->file_name);
1006 else if (td->create_fsync)
1007 fsync(td->fd);
1008
1009 close(td->fd);
1010 td->fd = -1;
1011 free(b);
1012 return 0;
1013}
1014
1015static int file_size(struct thread_data *td)
1016{
1017 struct stat st;
1018
1019 if (fstat(td->fd, &st) == -1) {
1020 td_verror(td, errno);
1021 return 1;
1022 }
1023
Jens Axboe838a3cd2006-01-20 12:38:29 +01001024 td->real_file_size = st.st_size;
1025
1026 if (!td->file_size || td->file_size > td->real_file_size)
1027 td->file_size = td->real_file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001028
Jens Axboee8e387c2006-05-05 11:11:22 +02001029 td->file_size -= td->file_offset;
Jens Axboeebac4652005-12-08 15:25:21 +01001030 return 0;
1031}
1032
1033static int bdev_size(struct thread_data *td)
1034{
Jens Axboe9104f872005-12-28 23:50:45 +01001035 unsigned long long bytes;
Jens Axboeebac4652005-12-08 15:25:21 +01001036 int r;
1037
1038 r = blockdev_size(td->fd, &bytes);
1039 if (r) {
1040 td_verror(td, r);
1041 return 1;
1042 }
1043
Jens Axboe838a3cd2006-01-20 12:38:29 +01001044 td->real_file_size = bytes;
1045
Jens Axboeebac4652005-12-08 15:25:21 +01001046 /*
1047 * no extend possibilities, so limit size to device size if too large
1048 */
Jens Axboe838a3cd2006-01-20 12:38:29 +01001049 if (!td->file_size || td->file_size > td->real_file_size)
1050 td->file_size = td->real_file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001051
Jens Axboee8e387c2006-05-05 11:11:22 +02001052 td->file_size -= td->file_offset;
Jens Axboeebac4652005-12-08 15:25:21 +01001053 return 0;
1054}
1055
1056static int get_file_size(struct thread_data *td)
1057{
Jens Axboe0af7b542006-02-17 10:10:12 +01001058 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001059
1060 if (td->filetype == FIO_TYPE_FILE)
1061 ret = file_size(td);
Jens Axboe0af7b542006-02-17 10:10:12 +01001062 else if (td->filetype == FIO_TYPE_BD)
Jens Axboeebac4652005-12-08 15:25:21 +01001063 ret = bdev_size(td);
Jens Axboe0af7b542006-02-17 10:10:12 +01001064 else
1065 td->real_file_size = -1;
Jens Axboeebac4652005-12-08 15:25:21 +01001066
1067 if (ret)
1068 return ret;
1069
Jens Axboee8e387c2006-05-05 11:11:22 +02001070 if (td->file_offset > td->real_file_size) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001071 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
Jens Axboeebac4652005-12-08 15:25:21 +01001072 return 1;
1073 }
1074
Jens Axboe838a3cd2006-01-20 12:38:29 +01001075 td->io_size = td->file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001076 if (td->io_size == 0) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001077 log_err("%s: no io blocks\n", td->name);
Jens Axboeebac4652005-12-08 15:25:21 +01001078 td_verror(td, EINVAL);
1079 return 1;
1080 }
1081
Jens Axboe20dc95c2005-12-09 10:29:35 +01001082 if (!td->zone_size)
1083 td->zone_size = td->io_size;
1084
Jens Axboeebac4652005-12-08 15:25:21 +01001085 td->total_io_size = td->io_size * td->loops;
1086 return 0;
1087}
1088
1089static int setup_file_mmap(struct thread_data *td)
1090{
1091 int flags;
1092
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001093 if (td_rw(td))
1094 flags = PROT_READ | PROT_WRITE;
1095 else if (td_write(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001096 flags = PROT_WRITE;
1097
1098 if (td->verify != VERIFY_NONE)
1099 flags |= PROT_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001100 } else
1101 flags = PROT_READ;
Jens Axboeebac4652005-12-08 15:25:21 +01001102
1103 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1104 if (td->mmap == MAP_FAILED) {
1105 td->mmap = NULL;
1106 td_verror(td, errno);
1107 return 1;
1108 }
1109
1110 if (td->invalidate_cache) {
1111 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1112 td_verror(td, errno);
1113 return 1;
1114 }
1115 }
1116
1117 if (td->sequential) {
1118 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1119 td_verror(td, errno);
1120 return 1;
1121 }
1122 } else {
1123 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1124 td_verror(td, errno);
1125 return 1;
1126 }
1127 }
1128
1129 return 0;
1130}
1131
1132static int setup_file_plain(struct thread_data *td)
1133{
1134 if (td->invalidate_cache) {
1135 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1136 td_verror(td, errno);
1137 return 1;
1138 }
1139 }
1140
1141 if (td->sequential) {
1142 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1143 td_verror(td, errno);
1144 return 1;
1145 }
1146 } else {
1147 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1148 td_verror(td, errno);
1149 return 1;
1150 }
1151 }
1152
1153 return 0;
1154}
1155
1156static int setup_file(struct thread_data *td)
1157{
1158 struct stat st;
1159 int flags = 0;
1160
1161 if (stat(td->file_name, &st) == -1) {
1162 if (errno != ENOENT) {
1163 td_verror(td, errno);
1164 return 1;
1165 }
1166 if (!td->create_file) {
1167 td_verror(td, ENOENT);
1168 return 1;
1169 }
1170 if (create_file(td, td->file_size, 0))
1171 return 1;
1172 } else if (td->filetype == FIO_TYPE_FILE) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001173 if (st.st_size < (off_t) td->file_size) {
Jens Axboeebac4652005-12-08 15:25:21 +01001174 if (create_file(td, td->file_size - st.st_size, 1))
1175 return 1;
1176 }
1177 }
1178
1179 if (td->odirect)
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001180 flags |= OS_O_DIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001181
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001182 if (td_write(td) || td_rw(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001183 if (td->filetype == FIO_TYPE_FILE) {
1184 if (!td->overwrite)
1185 flags |= O_TRUNC;
1186
1187 flags |= O_CREAT;
1188 }
1189 if (td->sync_io)
1190 flags |= O_SYNC;
1191
1192 flags |= O_RDWR;
1193
1194 td->fd = open(td->file_name, flags, 0600);
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001195 } else {
1196 if (td->filetype == FIO_TYPE_CHAR)
1197 flags |= O_RDWR;
1198 else
1199 flags |= O_RDONLY;
1200
1201 td->fd = open(td->file_name, flags);
Jens Axboeebac4652005-12-08 15:25:21 +01001202 }
1203
1204 if (td->fd == -1) {
1205 td_verror(td, errno);
1206 return 1;
1207 }
1208
1209 if (get_file_size(td))
1210 return 1;
1211
1212 if (td->io_engine != FIO_MMAPIO)
1213 return setup_file_plain(td);
1214 else
1215 return setup_file_mmap(td);
1216}
1217
Jens Axboeda867742006-06-06 10:39:10 -07001218static int switch_ioscheduler(struct thread_data *td)
1219{
1220 char tmp[256], tmp2[128];
1221 FILE *f;
1222 int ret;
1223
1224 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1225
1226 f = fopen(tmp, "r+");
1227 if (!f) {
1228 td_verror(td, errno);
1229 return 1;
1230 }
1231
1232 /*
1233 * Set io scheduler.
1234 */
1235 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1236 if (ferror(f) || ret != 1) {
1237 td_verror(td, errno);
1238 fclose(f);
1239 return 1;
1240 }
1241
1242 rewind(f);
1243
1244 /*
1245 * Read back and check that the selected scheduler is now the default.
1246 */
1247 ret = fread(tmp, 1, sizeof(tmp), f);
1248 if (ferror(f) || ret < 0) {
1249 td_verror(td, errno);
1250 fclose(f);
1251 return 1;
1252 }
1253
1254 sprintf(tmp2, "[%s]", td->ioscheduler);
1255 if (!strstr(tmp, tmp2)) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001256 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -07001257 td_verror(td, EINVAL);
1258 fclose(f);
1259 return 1;
1260 }
1261
1262 fclose(f);
1263 return 0;
1264}
1265
Jens Axboeebac4652005-12-08 15:25:21 +01001266static void clear_io_state(struct thread_data *td)
1267{
1268 if (td->io_engine == FIO_SYNCIO)
1269 lseek(td->fd, SEEK_SET, 0);
1270
Jens Axboe20dc95c2005-12-09 10:29:35 +01001271 td->last_pos = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001272 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1273 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +01001274 td->zone_bytes = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001275
1276 if (td->file_map)
1277 memset(td->file_map, 0, td->num_maps * sizeof(long));
1278}
1279
Jens Axboeebac4652005-12-08 15:25:21 +01001280static void *thread_main(void *data)
1281{
1282 struct thread_data *td = data;
Jens Axboeebac4652005-12-08 15:25:21 +01001283
1284 if (!td->use_thread)
1285 setsid();
1286
1287 td->pid = getpid();
1288
Jens Axboeaea47d42006-05-26 19:27:29 +02001289 INIT_LIST_HEAD(&td->io_u_freelist);
1290 INIT_LIST_HEAD(&td->io_u_busylist);
1291 INIT_LIST_HEAD(&td->io_hist_list);
1292 INIT_LIST_HEAD(&td->io_log_list);
1293
Jens Axboeebac4652005-12-08 15:25:21 +01001294 if (init_io_u(td))
1295 goto err;
1296
1297 if (fio_setaffinity(td) == -1) {
1298 td_verror(td, errno);
1299 goto err;
1300 }
1301
1302 if (init_io(td))
1303 goto err;
1304
Jens Axboeaea47d42006-05-26 19:27:29 +02001305 if (init_iolog(td))
1306 goto err;
1307
Jens Axboeebac4652005-12-08 15:25:21 +01001308 if (td->ioprio) {
1309 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1310 td_verror(td, errno);
1311 goto err;
1312 }
1313 }
1314
Jens Axboeb6f4d882006-06-02 10:32:51 +02001315 if (nice(td->nice) < 0) {
1316 td_verror(td, errno);
1317 goto err;
1318 }
1319
Jens Axboe75154842006-06-01 13:56:09 +02001320 if (init_random_state(td))
1321 goto err;
1322
Jens Axboeda867742006-06-06 10:39:10 -07001323 if (td->ioscheduler && switch_ioscheduler(td))
1324 goto err;
1325
Jens Axboe75154842006-06-01 13:56:09 +02001326 td_set_runstate(td, TD_INITIALIZED);
Jens Axboebbfd6b02006-06-07 19:42:54 +02001327 fio_sem_up(&startup_sem);
1328 fio_sem_down(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001329
1330 if (!td->create_serialize && setup_file(td))
1331 goto err;
1332
Jens Axboeebac4652005-12-08 15:25:21 +01001333 gettimeofday(&td->epoch, NULL);
1334
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001335 if (td->exec_prerun)
1336 system(td->exec_prerun);
1337
Jens Axboeebac4652005-12-08 15:25:21 +01001338 while (td->loops--) {
1339 getrusage(RUSAGE_SELF, &td->ru_start);
1340 gettimeofday(&td->start, NULL);
1341 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1342
1343 if (td->ratemin)
1344 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1345
1346 clear_io_state(td);
1347 prune_io_piece_log(td);
1348
1349 do_io(td);
1350
1351 td->runtime[td->ddir] += mtime_since_now(&td->start);
Jens Axboeaea47d42006-05-26 19:27:29 +02001352 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001353 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1354
Jens Axboeebac4652005-12-08 15:25:21 +01001355 update_rusage_stat(td);
1356
1357 if (td->error || td->terminate)
1358 break;
1359
1360 if (td->verify == VERIFY_NONE)
1361 continue;
1362
1363 clear_io_state(td);
1364 gettimeofday(&td->start, NULL);
1365
1366 do_verify(td);
1367
1368 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1369
1370 if (td->error || td->terminate)
1371 break;
1372 }
1373
Jens Axboeebac4652005-12-08 15:25:21 +01001374 if (td->bw_log)
1375 finish_log(td, td->bw_log, "bw");
1376 if (td->slat_log)
1377 finish_log(td, td->slat_log, "slat");
1378 if (td->clat_log)
1379 finish_log(td, td->clat_log, "clat");
Jens Axboe843a7412006-06-01 21:14:21 -07001380 if (td->write_iolog)
1381 write_iolog_close(td);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001382 if (td->exec_postrun)
1383 system(td->exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +01001384
1385 if (exitall_on_terminate)
1386 terminate_threads(td->groupid);
1387
1388err:
1389 if (td->fd != -1) {
1390 close(td->fd);
1391 td->fd = -1;
1392 }
1393 if (td->mmap)
1394 munmap(td->mmap, td->file_size);
1395 cleanup_io(td);
1396 cleanup_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001397 td_set_runstate(td, TD_EXITED);
1398 return NULL;
1399
1400}
1401
1402static void *fork_main(int shmid, int offset)
1403{
1404 struct thread_data *td;
1405 void *data;
1406
1407 data = shmat(shmid, NULL, 0);
1408 if (data == (void *) -1) {
1409 perror("shmat");
1410 return NULL;
1411 }
1412
1413 td = data + offset * sizeof(struct thread_data);
1414 thread_main(td);
1415 shmdt(data);
1416 return NULL;
1417}
1418
Jens Axboeebac4652005-12-08 15:25:21 +01001419static void check_str_update(struct thread_data *td)
1420{
1421 char c = run_str[td->thread_number - 1];
1422
Jens Axboeebac4652005-12-08 15:25:21 +01001423 switch (td->runstate) {
1424 case TD_REAPED:
1425 c = '_';
1426 break;
1427 case TD_EXITED:
1428 c = 'E';
1429 break;
1430 case TD_RUNNING:
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001431 if (td_rw(td)) {
1432 if (td->sequential)
1433 c = 'M';
1434 else
1435 c = 'm';
1436 } else if (td_read(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001437 if (td->sequential)
1438 c = 'R';
1439 else
1440 c = 'r';
1441 } else {
1442 if (td->sequential)
1443 c = 'W';
1444 else
1445 c = 'w';
1446 }
1447 break;
1448 case TD_VERIFYING:
1449 c = 'V';
1450 break;
Jens Axboe5853e5a2006-06-08 14:41:05 +02001451 case TD_FSYNCING:
1452 c = 'F';
1453 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001454 case TD_CREATED:
1455 c = 'C';
1456 break;
Jens Axboe75154842006-06-01 13:56:09 +02001457 case TD_INITIALIZED:
1458 c = 'I';
1459 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001460 case TD_NOT_CREATED:
1461 c = 'P';
1462 break;
1463 default:
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001464 log_err("state %d\n", td->runstate);
Jens Axboeebac4652005-12-08 15:25:21 +01001465 }
1466
1467 run_str[td->thread_number - 1] = c;
Jens Axboeebac4652005-12-08 15:25:21 +01001468}
1469
Jens Axboe5289b842005-12-08 20:47:28 +01001470static void eta_to_str(char *str, int eta_sec)
1471{
1472 unsigned int d, h, m, s;
1473 static int always_d, always_h;
1474
1475 d = h = m = s = 0;
1476
1477 s = eta_sec % 60;
1478 eta_sec /= 60;
1479 m = eta_sec % 60;
1480 eta_sec /= 60;
1481 h = eta_sec % 24;
1482 eta_sec /= 24;
1483 d = eta_sec;
1484
1485 if (d || always_d) {
1486 always_d = 1;
1487 str += sprintf(str, "%02dd:", d);
1488 }
1489 if (h || always_h) {
1490 always_h = 1;
1491 str += sprintf(str, "%02dh:", h);
1492 }
1493
1494 str += sprintf(str, "%02dm:", m);
1495 str += sprintf(str, "%02ds", s);
1496}
1497
Jens Axboe6a0106a2006-05-05 10:34:43 +02001498static int thread_eta(struct thread_data *td, unsigned long elapsed)
1499{
1500 unsigned long long bytes_total, bytes_done;
1501 unsigned int eta_sec = 0;
1502
1503 bytes_total = td->total_io_size;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001504
1505 /*
1506 * if writing, bytes_total will be twice the size. If mixing,
1507 * assume a 50/50 split and thus bytes_total will be 50% larger.
1508 */
1509 if (td->verify) {
1510 if (td_rw(td))
1511 bytes_total = bytes_total * 3 / 2;
1512 else
1513 bytes_total <<= 1;
1514 }
Jens Axboe6a0106a2006-05-05 10:34:43 +02001515 if (td->zone_size && td->zone_skip)
1516 bytes_total /= (td->zone_skip / td->zone_size);
1517
1518 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1519 double perc;
Jens Axboe8b611c32006-05-25 01:46:59 +02001520
Jens Axboe6a0106a2006-05-05 10:34:43 +02001521 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1522 perc = (double) bytes_done / (double) bytes_total;
1523 if (perc > 1.0)
1524 perc = 1.0;
1525
1526 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1527
Jens Axboe8b611c32006-05-25 01:46:59 +02001528 if (td->timeout && eta_sec > (td->timeout - elapsed))
Jens Axboe6a0106a2006-05-05 10:34:43 +02001529 eta_sec = td->timeout - elapsed;
Jens Axboe75154842006-06-01 13:56:09 +02001530 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1531 || td->runstate == TD_INITIALIZED) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001532 int t_eta = 0, r_eta = 0;
1533
1534 /*
1535 * We can only guess - assume it'll run the full timeout
1536 * if given, otherwise assume it'll run at the specified rate.
1537 */
1538 if (td->timeout)
1539 t_eta = td->timeout + td->start_delay - elapsed;
1540 if (td->rate) {
1541 r_eta = (bytes_total / 1024) / td->rate;
1542 r_eta += td->start_delay - elapsed;
1543 }
1544
1545 if (r_eta && t_eta)
1546 eta_sec = min(r_eta, t_eta);
1547 else if (r_eta)
1548 eta_sec = r_eta;
1549 else if (t_eta)
1550 eta_sec = t_eta;
1551 else
1552 eta_sec = INT_MAX;
1553 } else {
1554 /*
Jens Axboe5853e5a2006-06-08 14:41:05 +02001555 * thread is already done or waiting for fsync
Jens Axboe6a0106a2006-05-05 10:34:43 +02001556 */
1557 eta_sec = 0;
1558 }
1559
1560 return eta_sec;
1561}
1562
Jens Axboeebac4652005-12-08 15:25:21 +01001563static void print_thread_status(void)
1564{
Jens Axboe6a0106a2006-05-05 10:34:43 +02001565 unsigned long elapsed = time_since_now(&genesis);
Jens Axboe71a751c2006-06-08 14:48:47 +02001566 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
Jens Axboe5289b842005-12-08 20:47:28 +01001567 char eta_str[32];
Jens Axboe6a0106a2006-05-05 10:34:43 +02001568 double perc = 0.0;
Jens Axboeebac4652005-12-08 15:25:21 +01001569
Jens Axboe6a0106a2006-05-05 10:34:43 +02001570 eta_secs = malloc(thread_number * sizeof(int));
1571 memset(eta_secs, 0, thread_number * sizeof(int));
1572
Jens Axboe71a751c2006-06-08 14:48:47 +02001573 nr_pending = nr_running = t_rate = m_rate = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001574 for (i = 0; i < thread_number; i++) {
1575 struct thread_data *td = &threads[i];
1576
Jens Axboe5853e5a2006-06-08 14:41:05 +02001577 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1578 td->runstate == TD_FSYNCING) {
Jens Axboeebac4652005-12-08 15:25:21 +01001579 nr_running++;
1580 t_rate += td->rate;
1581 m_rate += td->ratemin;
Jens Axboe71a751c2006-06-08 14:48:47 +02001582 } else if (td->runstate < TD_RUNNING)
1583 nr_pending++;
Jens Axboeebac4652005-12-08 15:25:21 +01001584
Jens Axboe6a0106a2006-05-05 10:34:43 +02001585 if (elapsed >= 3)
1586 eta_secs[i] = thread_eta(td, elapsed);
Jens Axboe8b611c32006-05-25 01:46:59 +02001587 else
1588 eta_secs[i] = INT_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +01001589
1590 check_str_update(td);
1591 }
1592
Jens Axboe6a0106a2006-05-05 10:34:43 +02001593 if (exitall_on_terminate)
1594 eta_sec = INT_MAX;
1595 else
1596 eta_sec = 0;
Jens Axboe5289b842005-12-08 20:47:28 +01001597
Jens Axboe6a0106a2006-05-05 10:34:43 +02001598 for (i = 0; i < thread_number; i++) {
1599 if (exitall_on_terminate) {
1600 if (eta_secs[i] < eta_sec)
1601 eta_sec = eta_secs[i];
1602 } else {
1603 if (eta_secs[i] > eta_sec)
1604 eta_sec = eta_secs[i];
Jens Axboe5289b842005-12-08 20:47:28 +01001605 }
Jens Axboe6a0106a2006-05-05 10:34:43 +02001606 }
Jens Axboe5289b842005-12-08 20:47:28 +01001607
Jens Axboe8b611c32006-05-25 01:46:59 +02001608 if (eta_sec != INT_MAX && elapsed) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001609 perc = (double) elapsed / (double) (elapsed + eta_sec);
1610 eta_to_str(eta_str, eta_sec);
Jens Axboeebac4652005-12-08 15:25:21 +01001611 }
1612
Jens Axboe71a751c2006-06-08 14:48:47 +02001613 if (!nr_running && !nr_pending)
Jens Axboef44c1d42006-06-08 14:46:24 +02001614 return;
1615
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001616 printf("Threads running: %d", nr_running);
Jens Axboeebac4652005-12-08 15:25:21 +01001617 if (m_rate || t_rate)
1618 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
Jens Axboe8b611c32006-05-25 01:46:59 +02001619 if (eta_sec != INT_MAX) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001620 perc *= 100.0;
1621 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1622 }
Jens Axboe5289b842005-12-08 20:47:28 +01001623 printf("\r");
Jens Axboeebac4652005-12-08 15:25:21 +01001624 fflush(stdout);
Jens Axboe6a0106a2006-05-05 10:34:43 +02001625 free(eta_secs);
Jens Axboeebac4652005-12-08 15:25:21 +01001626}
1627
1628static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1629{
1630 int i;
1631
1632 /*
1633 * reap exited threads (TD_EXITED -> TD_REAPED)
1634 */
1635 for (i = 0; i < thread_number; i++) {
1636 struct thread_data *td = &threads[i];
1637
1638 if (td->runstate != TD_EXITED)
1639 continue;
1640
1641 td_set_runstate(td, TD_REAPED);
1642
1643 if (td->use_thread) {
1644 long ret;
1645
1646 if (pthread_join(td->thread, (void *) &ret))
1647 perror("thread_join");
1648 } else
1649 waitpid(td->pid, NULL, 0);
1650
1651 (*nr_running)--;
1652 (*m_rate) -= td->ratemin;
1653 (*t_rate) -= td->rate;
1654 }
1655}
1656
Jens Axboefcb6ade2006-05-31 12:14:35 +02001657static void fio_unpin_memory(void *pinned)
1658{
1659 if (pinned) {
1660 if (munlock(pinned, mlock_size) < 0)
1661 perror("munlock");
1662 munmap(pinned, mlock_size);
1663 }
1664}
1665
1666static void *fio_pin_memory(void)
1667{
Jens Axboe32cd46a2006-06-07 13:40:40 +02001668 unsigned long long phys_mem;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001669 void *ptr;
1670
1671 if (!mlock_size)
1672 return NULL;
1673
1674 /*
1675 * Don't allow mlock of more than real_mem-128MB
1676 */
Jens Axboe32cd46a2006-06-07 13:40:40 +02001677 phys_mem = os_phys_mem();
1678 if (phys_mem) {
1679 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1680 mlock_size = phys_mem - 128 * 1024 * 1024;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001681 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
Jens Axboefcb6ade2006-05-31 12:14:35 +02001682 }
1683 }
1684
1685 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1686 if (!ptr) {
1687 perror("malloc locked mem");
1688 return NULL;
1689 }
1690 if (mlock(ptr, mlock_size) < 0) {
1691 munmap(ptr, mlock_size);
1692 perror("mlock");
1693 return NULL;
1694 }
1695
1696 return ptr;
1697}
1698
Jens Axboeebac4652005-12-08 15:25:21 +01001699static void run_threads(void)
1700{
Jens Axboeebac4652005-12-08 15:25:21 +01001701 struct thread_data *td;
1702 unsigned long spent;
1703 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001704 void *mlocked_mem;
1705
1706 mlocked_mem = fio_pin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001707
1708 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
Jens Axboefcb6ade2006-05-31 12:14:35 +02001709 fflush(stdout);
Jens Axboec04f7ec2006-05-31 10:13:16 +02001710
Jens Axboe4efa9702006-05-31 11:56:49 +02001711 signal(SIGINT, sig_handler);
1712 signal(SIGALRM, sig_handler);
1713
Jens Axboeebac4652005-12-08 15:25:21 +01001714 todo = thread_number;
1715 nr_running = 0;
1716 nr_started = 0;
1717 m_rate = t_rate = 0;
1718
1719 for (i = 0; i < thread_number; i++) {
1720 td = &threads[i];
1721
1722 run_str[td->thread_number - 1] = 'P';
1723
1724 init_disk_util(td);
1725
1726 if (!td->create_serialize)
1727 continue;
1728
1729 /*
1730 * do file setup here so it happens sequentially,
1731 * we don't want X number of threads getting their
1732 * client data interspersed on disk
1733 */
1734 if (setup_file(td)) {
1735 td_set_runstate(td, TD_REAPED);
1736 todo--;
1737 }
1738 }
1739
1740 gettimeofday(&genesis, NULL);
1741
1742 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001743 struct thread_data *map[MAX_JOBS];
1744 struct timeval this_start;
1745 int this_jobs = 0, left;
1746
Jens Axboeebac4652005-12-08 15:25:21 +01001747 /*
1748 * create threads (TD_NOT_CREATED -> TD_CREATED)
1749 */
1750 for (i = 0; i < thread_number; i++) {
1751 td = &threads[i];
1752
1753 if (td->runstate != TD_NOT_CREATED)
1754 continue;
1755
1756 /*
1757 * never got a chance to start, killed by other
1758 * thread for some reason
1759 */
1760 if (td->terminate) {
1761 todo--;
1762 continue;
1763 }
1764
1765 if (td->start_delay) {
1766 spent = mtime_since_now(&genesis);
1767
1768 if (td->start_delay * 1000 > spent)
1769 continue;
1770 }
1771
1772 if (td->stonewall && (nr_started || nr_running))
1773 break;
1774
Jens Axboe75154842006-06-01 13:56:09 +02001775 /*
1776 * Set state to created. Thread will transition
1777 * to TD_INITIALIZED when it's done setting up.
1778 */
Jens Axboeebac4652005-12-08 15:25:21 +01001779 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001780 map[this_jobs++] = td;
Jens Axboebbfd6b02006-06-07 19:42:54 +02001781 fio_sem_init(&startup_sem, 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001782 nr_started++;
1783
1784 if (td->use_thread) {
1785 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1786 perror("thread_create");
1787 nr_started--;
1788 }
1789 } else {
1790 if (fork())
Jens Axboebbfd6b02006-06-07 19:42:54 +02001791 fio_sem_down(&startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +01001792 else {
1793 fork_main(shm_id, i);
1794 exit(0);
1795 }
1796 }
1797 }
1798
1799 /*
Jens Axboe75154842006-06-01 13:56:09 +02001800 * Wait for the started threads to transition to
1801 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001802 */
Jens Axboe75154842006-06-01 13:56:09 +02001803 printf("fio: Waiting for threads to initialize...\n");
1804 gettimeofday(&this_start, NULL);
1805 left = this_jobs;
1806 while (left) {
1807 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1808 break;
1809
1810 usleep(100000);
1811
1812 for (i = 0; i < this_jobs; i++) {
1813 td = map[i];
1814 if (!td)
1815 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001816 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001817 map[i] = NULL;
1818 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001819 } else if (td->runstate >= TD_EXITED) {
1820 map[i] = NULL;
1821 left--;
1822 todo--;
1823 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001824 }
1825 }
1826 }
1827
1828 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001829 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001830 for (i = 0; i < this_jobs; i++) {
1831 td = map[i];
1832 if (!td)
1833 continue;
1834 kill(td->pid, SIGTERM);
1835 }
1836 break;
1837 }
1838
1839 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001840 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001841 */
1842 printf("fio: Go for launch\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001843 for (i = 0; i < thread_number; i++) {
1844 td = &threads[i];
1845
Jens Axboe75154842006-06-01 13:56:09 +02001846 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001847 continue;
1848
1849 td_set_runstate(td, TD_RUNNING);
1850 nr_running++;
1851 nr_started--;
1852 m_rate += td->ratemin;
1853 t_rate += td->rate;
Jens Axboe75154842006-06-01 13:56:09 +02001854 todo--;
Jens Axboebbfd6b02006-06-07 19:42:54 +02001855 fio_sem_up(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001856 }
1857
1858 reap_threads(&nr_running, &t_rate, &m_rate);
1859
1860 if (todo)
1861 usleep(100000);
1862 }
1863
1864 while (nr_running) {
1865 reap_threads(&nr_running, &t_rate, &m_rate);
1866 usleep(10000);
1867 }
1868
1869 update_io_ticks();
Jens Axboefcb6ade2006-05-31 12:14:35 +02001870 fio_unpin_memory(mlocked_mem);
Jens Axboeebac4652005-12-08 15:25:21 +01001871}
1872
Jens Axboeebac4652005-12-08 15:25:21 +01001873int main(int argc, char *argv[])
1874{
1875 if (parse_options(argc, argv))
1876 return 1;
1877
1878 if (!thread_number) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001879 log_err("Nothing to do\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001880 return 1;
1881 }
1882
1883 disk_util_timer_arm();
1884
1885 run_threads();
1886 show_run_stats();
1887
1888 return 0;
1889}