blob: 76710e19fba08166902c52cdb8218cda45235928 [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>
Jens Axboeaae22ca2006-09-05 10:46:22 +02005 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboeebac4652005-12-08 15:25:21 +01006 *
Jens Axboe8e9fe632006-10-24 15:11:09 +02007 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
Jens Axboeebac4652005-12-08 15:25:21 +010010 * This program is free software; you can redistribute it and/or modify
Jens Axboe8e9fe632006-10-24 15:11:09 +020011 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Jens Axboeebac4652005-12-08 15:25:21 +010013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <signal.h>
28#include <time.h>
Jens Axboeebac4652005-12-08 15:25:21 +010029#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010030#include <sys/stat.h>
31#include <sys/wait.h>
32#include <sys/ipc.h>
33#include <sys/shm.h>
34#include <sys/ioctl.h>
35#include <sys/mman.h>
36
37#include "fio.h"
38#include "os.h"
39
40#define MASK (4095)
41
42#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
43
44int groupid = 0;
45int thread_number = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010046int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020047int temp_stall_ts;
Jens Axboec1d57252006-10-09 19:56:04 +020048char *fio_inst_prefix = _INST_PREFIX;
Jens Axboeebac4652005-12-08 15:25:21 +010049
Jens Axboebbfd6b02006-06-07 19:42:54 +020050static volatile int startup_sem;
Jens Axboeebac4652005-12-08 15:25:21 +010051
52#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020053#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010054
55static void terminate_threads(int group_id)
56{
Jens Axboe34572e22006-10-20 09:33:18 +020057 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010058 int i;
59
Jens Axboe34572e22006-10-20 09:33:18 +020060 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010061 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
62 td->terminate = 1;
63 td->start_delay = 0;
64 }
65 }
66}
67
68static void sig_handler(int sig)
69{
70 switch (sig) {
71 case SIGALRM:
72 update_io_ticks();
73 disk_util_timer_arm();
74 print_thread_status();
75 break;
76 default:
77 printf("\nfio: terminating on signal\n");
78 fflush(stdout);
79 terminate_threads(TERMINATE_ALL);
80 break;
81 }
82}
83
Jens Axboe906c8d72006-06-13 09:37:56 +020084/*
Jens Axboe906c8d72006-06-13 09:37:56 +020085 * Check if we are above the minimum rate given.
86 */
Jens Axboeebac4652005-12-08 15:25:21 +010087static int check_min_rate(struct thread_data *td, struct timeval *now)
88{
89 unsigned long spent;
90 unsigned long rate;
91 int ddir = td->ddir;
92
93 /*
94 * allow a 2 second settle period in the beginning
95 */
96 if (mtime_since(&td->start, now) < 2000)
97 return 0;
98
99 /*
100 * if rate blocks is set, sample is running
101 */
102 if (td->rate_bytes) {
103 spent = mtime_since(&td->lastrate, now);
104 if (spent < td->ratecycle)
105 return 0;
106
107 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
108 if (rate < td->ratemin) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200109 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 +0100110 return 1;
111 }
112 }
113
114 td->rate_bytes = td->this_io_bytes[ddir];
115 memcpy(&td->lastrate, now, sizeof(*now));
116 return 0;
117}
118
119static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
120{
121 if (!td->timeout)
122 return 0;
123 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
124 return 1;
125
126 return 0;
127}
128
Jens Axboeebac4652005-12-08 15:25:21 +0100129static inline void td_set_runstate(struct thread_data *td, int runstate)
130{
Jens Axboeebac4652005-12-08 15:25:21 +0100131 td->runstate = runstate;
132}
133
Jens Axboe53cdc682006-10-18 11:50:58 +0200134static struct fio_file *get_next_file(struct thread_data *td)
135{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200136 unsigned int old_next_file = td->next_file;
Jens Axboeb2a15192006-10-18 14:10:42 +0200137 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200138
Jens Axboeb2a15192006-10-18 14:10:42 +0200139 do {
140 f = &td->files[td->next_file];
141
142 td->next_file++;
143 if (td->next_file >= td->nr_files)
144 td->next_file = 0;
145
146 if (f->fd != -1)
147 break;
148
149 f = NULL;
150 } while (td->next_file != old_next_file);
Jens Axboe53cdc682006-10-18 11:50:58 +0200151
152 return f;
153}
154
Jens Axboe906c8d72006-06-13 09:37:56 +0200155/*
156 * When job exits, we can cancel the in-flight IO if we are using async
157 * io. Attempt to do so.
158 */
Jens Axboeebac4652005-12-08 15:25:21 +0100159static void cleanup_pending_aio(struct thread_data *td)
160{
161 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
162 struct list_head *entry, *n;
163 struct io_completion_data icd;
164 struct io_u *io_u;
165 int r;
166
167 /*
168 * get immediately available events, if any
169 */
Jens Axboe45bee282006-10-18 15:26:44 +0200170 r = td_io_getevents(td, 0, td->cur_depth, &ts);
Jens Axboeebac4652005-12-08 15:25:21 +0100171 if (r > 0) {
172 icd.nr = r;
173 ios_completed(td, &icd);
174 }
175
176 /*
177 * now cancel remaining active events
178 */
Jens Axboe2866c822006-10-09 15:57:48 +0200179 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100180 list_for_each_safe(entry, n, &td->io_u_busylist) {
181 io_u = list_entry(entry, struct io_u, list);
182
Jens Axboe2866c822006-10-09 15:57:48 +0200183 r = td->io_ops->cancel(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100184 if (!r)
185 put_io_u(td, io_u);
186 }
187 }
188
189 if (td->cur_depth) {
Jens Axboe45bee282006-10-18 15:26:44 +0200190 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100191 if (r > 0) {
192 icd.nr = r;
193 ios_completed(td, &icd);
194 }
195 }
196}
197
Jens Axboe906c8d72006-06-13 09:37:56 +0200198/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200199 * Helper to handle the final sync of a file. Works just like the normal
200 * io path, just does everything sync.
201 */
202static int fio_io_sync(struct thread_data *td, struct fio_file *f)
203{
204 struct io_u *io_u = __get_io_u(td);
205 struct io_completion_data icd;
206 int ret;
207
208 if (!io_u)
209 return 1;
210
211 io_u->ddir = DDIR_SYNC;
212 io_u->file = f;
213
214 if (td_io_prep(td, io_u)) {
215 put_io_u(td, io_u);
216 return 1;
217 }
218
219 ret = td_io_queue(td, io_u);
220 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200221 td_verror(td, io_u->error);
Jens Axboe858a3d42006-10-24 14:54:44 +0200222 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200223 return 1;
224 }
225
226 ret = td_io_getevents(td, 1, td->cur_depth, NULL);
227 if (ret < 0) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200228 td_verror(td, ret);
Jens Axboe858a3d42006-10-24 14:54:44 +0200229 return 1;
230 }
231
232 icd.nr = ret;
233 ios_completed(td, &icd);
234 if (icd.error) {
235 td_verror(td, icd.error);
236 return 1;
237 }
238
239 return 0;
240}
241
242/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200243 * The main verify engine. Runs over the writes we previusly submitted,
244 * reads the blocks back in, and checks the crc/md5 of the data.
245 */
Jens Axboea9619d42006-10-18 15:47:42 +0200246void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100247{
248 struct timeval t;
249 struct io_u *io_u, *v_io_u = NULL;
250 struct io_completion_data icd;
Jens Axboe53cdc682006-10-18 11:50:58 +0200251 struct fio_file *f;
Jens Axboee5b401d2006-10-18 16:03:40 +0200252 int ret, i;
253
254 /*
255 * sync io first and invalidate cache, to make sure we really
256 * read from disk.
257 */
258 for_each_file(td, f, i) {
Jens Axboe858a3d42006-10-24 14:54:44 +0200259 fio_io_sync(td, f);
Jens Axboee5b401d2006-10-18 16:03:40 +0200260 file_invalidate_cache(td, f);
261 }
Jens Axboeebac4652005-12-08 15:25:21 +0100262
263 td_set_runstate(td, TD_VERIFYING);
264
265 do {
266 if (td->terminate)
267 break;
268
269 gettimeofday(&t, NULL);
270 if (runtime_exceeded(td, &t))
271 break;
272
273 io_u = __get_io_u(td);
274 if (!io_u)
275 break;
276
Jens Axboeaea47d42006-05-26 19:27:29 +0200277 if (get_next_verify(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100278 put_io_u(td, io_u);
279 break;
280 }
281
Jens Axboe53cdc682006-10-18 11:50:58 +0200282 f = get_next_file(td);
283 if (!f)
284 break;
285
286 io_u->file = f;
287
Jens Axboeaea47d42006-05-26 19:27:29 +0200288 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100289 put_io_u(td, io_u);
290 break;
291 }
292
Jens Axboe45bee282006-10-18 15:26:44 +0200293 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100294 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200295 td_verror(td, io_u->error);
Jens Axboeebac4652005-12-08 15:25:21 +0100296 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100297 break;
298 }
299
300 /*
301 * we have one pending to verify, do that while
302 * we are doing io on the next one
303 */
304 if (do_io_u_verify(td, &v_io_u))
305 break;
306
Jens Axboe45bee282006-10-18 15:26:44 +0200307 ret = td_io_getevents(td, 1, 1, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100308 if (ret != 1) {
309 if (ret < 0)
310 td_verror(td, ret);
311 break;
312 }
313
Jens Axboe2866c822006-10-09 15:57:48 +0200314 v_io_u = td->io_ops->event(td, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100315 icd.nr = 1;
316 icd.error = 0;
317 io_completed(td, v_io_u, &icd);
318
319 if (icd.error) {
320 td_verror(td, icd.error);
321 put_io_u(td, v_io_u);
322 v_io_u = NULL;
323 break;
324 }
325
Jens Axboeebac4652005-12-08 15:25:21 +0100326 /*
327 * if we can't submit more io, we need to verify now
328 */
329 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
330 break;
331
332 } while (1);
333
334 do_io_u_verify(td, &v_io_u);
335
336 if (td->cur_depth)
337 cleanup_pending_aio(td);
338
339 td_set_runstate(td, TD_RUNNING);
340}
341
Jens Axboe32cd46a2006-06-07 13:40:40 +0200342/*
Jens Axboeb990b5c2006-09-14 09:48:22 +0200343 * Not really an io thread, all it does is burn CPU cycles in the specified
344 * manner.
345 */
346static void do_cpuio(struct thread_data *td)
347{
348 struct timeval e;
349 int split = 100 / td->cpuload;
350 int i = 0;
351
352 while (!td->terminate) {
353 gettimeofday(&e, NULL);
354
355 if (runtime_exceeded(td, &e))
356 break;
357
358 if (!(i % split))
359 __usec_sleep(10000);
360 else
361 usec_sleep(td, 10000);
362
363 i++;
364 }
365}
366
367/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200368 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200369 * and reaps them, checking for rate and errors along the way.
370 */
Jens Axboeebac4652005-12-08 15:25:21 +0100371static void do_io(struct thread_data *td)
372{
373 struct io_completion_data icd;
374 struct timeval s, e;
375 unsigned long usec;
Jens Axboe53cdc682006-10-18 11:50:58 +0200376 struct fio_file *f;
Jens Axboe84585002006-10-19 20:26:22 +0200377 int i, ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100378
Jens Axboe5853e5a2006-06-08 14:41:05 +0200379 td_set_runstate(td, TD_RUNNING);
380
Jens Axboeebac4652005-12-08 15:25:21 +0100381 while (td->this_io_bytes[td->ddir] < td->io_size) {
382 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
383 struct timespec *timeout;
Jens Axboe84585002006-10-19 20:26:22 +0200384 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100385 struct io_u *io_u;
386
387 if (td->terminate)
388 break;
389
Jens Axboe53cdc682006-10-18 11:50:58 +0200390 f = get_next_file(td);
391 if (!f)
392 break;
393
394 io_u = get_io_u(td, f);
Jens Axboeebac4652005-12-08 15:25:21 +0100395 if (!io_u)
396 break;
397
398 memcpy(&s, &io_u->start_time, sizeof(s));
399
Jens Axboe45bee282006-10-18 15:26:44 +0200400 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100401 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200402 td_verror(td, io_u->error);
Jens Axboeebac4652005-12-08 15:25:21 +0100403 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100404 break;
405 }
406
407 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
408
409 if (td->cur_depth < td->iodepth) {
410 timeout = &ts;
411 min_evts = 0;
412 } else {
413 timeout = NULL;
414 min_evts = 1;
415 }
416
Jens Axboe45bee282006-10-18 15:26:44 +0200417 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
Jens Axboeebac4652005-12-08 15:25:21 +0100418 if (ret < 0) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200419 td_verror(td, ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100420 break;
421 } else if (!ret)
422 continue;
423
424 icd.nr = ret;
425 ios_completed(td, &icd);
426 if (icd.error) {
427 td_verror(td, icd.error);
428 break;
429 }
430
431 /*
432 * the rate is batched for now, it should work for batches
433 * of completions except the very first one which may look
434 * a little bursty
435 */
436 gettimeofday(&e, NULL);
437 usec = utime_since(&s, &e);
438
439 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
440
441 if (check_min_rate(td, &e)) {
Jens Axboe2f9ade32006-10-20 11:25:52 +0200442 if (rate_quit)
443 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100444 td_verror(td, ENOMEM);
445 break;
446 }
447
448 if (runtime_exceeded(td, &e))
449 break;
450
451 if (td->thinktime)
452 usec_sleep(td, td->thinktime);
Jens Axboeebac4652005-12-08 15:25:21 +0100453 }
454
Jens Axboe84585002006-10-19 20:26:22 +0200455 if (!ret) {
456 if (td->cur_depth)
457 cleanup_pending_aio(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100458
Jens Axboe84585002006-10-19 20:26:22 +0200459 if (should_fsync(td) && td->end_fsync) {
460 td_set_runstate(td, TD_FSYNCING);
461 for_each_file(td, f, i)
Jens Axboe858a3d42006-10-24 14:54:44 +0200462 fio_io_sync(td, f);
Jens Axboe84585002006-10-19 20:26:22 +0200463 }
Jens Axboe5853e5a2006-06-08 14:41:05 +0200464 }
Jens Axboeebac4652005-12-08 15:25:21 +0100465}
466
Jens Axboeebac4652005-12-08 15:25:21 +0100467static void cleanup_io_u(struct thread_data *td)
468{
469 struct list_head *entry, *n;
470 struct io_u *io_u;
471
472 list_for_each_safe(entry, n, &td->io_u_freelist) {
473 io_u = list_entry(entry, struct io_u, list);
474
475 list_del(&io_u->list);
476 free(io_u);
477 }
478
Jens Axboe2f9ade32006-10-20 11:25:52 +0200479 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100480}
481
Jens Axboe6b9cea22006-10-30 17:00:24 +0100482/*
483 * "randomly" fill the buffer contents
484 */
Jens Axboe66eeb292006-11-01 08:35:44 +0100485static void fill_rand_buf(struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100486{
Jens Axboe66eeb292006-11-01 08:35:44 +0100487 int *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100488
489 while ((void *) ptr - io_u->buf < max_bs) {
490 *ptr = rand() * 0x9e370001;
491 ptr++;
492 }
493}
494
Jens Axboeebac4652005-12-08 15:25:21 +0100495static int init_io_u(struct thread_data *td)
496{
497 struct io_u *io_u;
498 int i, max_units;
499 char *p;
500
Jens Axboe2866c822006-10-09 15:57:48 +0200501 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200502 return 0;
503
Jens Axboe2866c822006-10-09 15:57:48 +0200504 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100505 max_units = 1;
506 else
507 max_units = td->iodepth;
508
509 td->orig_buffer_size = td->max_bs * max_units + MASK;
510
Jens Axboe2f9ade32006-10-20 11:25:52 +0200511 if (allocate_io_mem(td))
512 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100513
Jens Axboeebac4652005-12-08 15:25:21 +0100514 p = ALIGN(td->orig_buffer);
515 for (i = 0; i < max_units; i++) {
516 io_u = malloc(sizeof(*io_u));
517 memset(io_u, 0, sizeof(*io_u));
518 INIT_LIST_HEAD(&io_u->list);
519
520 io_u->buf = p + td->max_bs * i;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100521 if (td_write(td) || td_rw(td))
522 fill_rand_buf(io_u, td->max_bs);
523
Jens Axboeb1ff3402006-02-17 11:35:58 +0100524 io_u->index = i;
Jens Axboeebac4652005-12-08 15:25:21 +0100525 list_add(&io_u->list, &td->io_u_freelist);
526 }
527
528 return 0;
529}
530
Jens Axboeda867742006-06-06 10:39:10 -0700531static int switch_ioscheduler(struct thread_data *td)
532{
533 char tmp[256], tmp2[128];
534 FILE *f;
535 int ret;
536
Jens Axboef48b4672006-10-27 11:30:07 +0200537 if (td->io_ops->flags & FIO_CPUIO)
538 return 0;
539
Jens Axboeda867742006-06-06 10:39:10 -0700540 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
541
542 f = fopen(tmp, "r+");
543 if (!f) {
544 td_verror(td, errno);
545 return 1;
546 }
547
548 /*
549 * Set io scheduler.
550 */
551 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
552 if (ferror(f) || ret != 1) {
553 td_verror(td, errno);
554 fclose(f);
555 return 1;
556 }
557
558 rewind(f);
559
560 /*
561 * Read back and check that the selected scheduler is now the default.
562 */
563 ret = fread(tmp, 1, sizeof(tmp), f);
564 if (ferror(f) || ret < 0) {
565 td_verror(td, errno);
566 fclose(f);
567 return 1;
568 }
569
570 sprintf(tmp2, "[%s]", td->ioscheduler);
571 if (!strstr(tmp, tmp2)) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200572 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700573 td_verror(td, EINVAL);
574 fclose(f);
575 return 1;
576 }
577
578 fclose(f);
579 return 0;
580}
581
Jens Axboeebac4652005-12-08 15:25:21 +0100582static void clear_io_state(struct thread_data *td)
583{
Jens Axboe53cdc682006-10-18 11:50:58 +0200584 struct fio_file *f;
585 int i;
Jens Axboeebac4652005-12-08 15:25:21 +0100586
Jens Axboeebac4652005-12-08 15:25:21 +0100587 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
588 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100589 td->zone_bytes = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100590
Jens Axboe53cdc682006-10-18 11:50:58 +0200591 for_each_file(td, f, i) {
592 f->last_pos = 0;
593 if (td->io_ops->flags & FIO_SYNCIO)
594 lseek(f->fd, SEEK_SET, 0);
595
596 if (f->file_map)
597 memset(f->file_map, 0, f->num_maps * sizeof(long));
598 }
Jens Axboeebac4652005-12-08 15:25:21 +0100599}
600
Jens Axboe906c8d72006-06-13 09:37:56 +0200601/*
602 * Entry point for the thread based jobs. The process based jobs end up
603 * here as well, after a little setup.
604 */
Jens Axboeebac4652005-12-08 15:25:21 +0100605static void *thread_main(void *data)
606{
607 struct thread_data *td = data;
Jens Axboeebac4652005-12-08 15:25:21 +0100608
609 if (!td->use_thread)
610 setsid();
611
612 td->pid = getpid();
613
Jens Axboeaea47d42006-05-26 19:27:29 +0200614 INIT_LIST_HEAD(&td->io_u_freelist);
615 INIT_LIST_HEAD(&td->io_u_busylist);
616 INIT_LIST_HEAD(&td->io_hist_list);
617 INIT_LIST_HEAD(&td->io_log_list);
618
Jens Axboeebac4652005-12-08 15:25:21 +0100619 if (init_io_u(td))
620 goto err;
621
622 if (fio_setaffinity(td) == -1) {
623 td_verror(td, errno);
624 goto err;
625 }
626
Jens Axboe45bee282006-10-18 15:26:44 +0200627 if (td_io_init(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100628 goto err;
629
Jens Axboeaea47d42006-05-26 19:27:29 +0200630 if (init_iolog(td))
631 goto err;
632
Jens Axboeebac4652005-12-08 15:25:21 +0100633 if (td->ioprio) {
634 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
635 td_verror(td, errno);
636 goto err;
637 }
638 }
639
Jens Axboe1056eaa2006-07-13 10:33:45 -0700640 if (nice(td->nice) == -1) {
Jens Axboeb6f4d882006-06-02 10:32:51 +0200641 td_verror(td, errno);
642 goto err;
643 }
644
Jens Axboe75154842006-06-01 13:56:09 +0200645 if (init_random_state(td))
646 goto err;
647
Jens Axboe56f94982006-10-24 09:33:42 +0200648 if (td->ioscheduler && switch_ioscheduler(td))
649 goto err;
Jens Axboeda867742006-06-06 10:39:10 -0700650
Jens Axboe75154842006-06-01 13:56:09 +0200651 td_set_runstate(td, TD_INITIALIZED);
Jens Axboebbfd6b02006-06-07 19:42:54 +0200652 fio_sem_up(&startup_sem);
653 fio_sem_down(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100654
Jens Axboe53cdc682006-10-18 11:50:58 +0200655 if (!td->create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100656 goto err;
657
Jens Axboeebac4652005-12-08 15:25:21 +0100658 gettimeofday(&td->epoch, NULL);
659
Jens Axboe56f94982006-10-24 09:33:42 +0200660 if (td->exec_prerun)
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200661 system(td->exec_prerun);
662
Jens Axboeebac4652005-12-08 15:25:21 +0100663 while (td->loops--) {
664 getrusage(RUSAGE_SELF, &td->ru_start);
665 gettimeofday(&td->start, NULL);
666 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
667
668 if (td->ratemin)
669 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
670
671 clear_io_state(td);
672 prune_io_piece_log(td);
673
Jens Axboe2866c822006-10-09 15:57:48 +0200674 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200675 do_cpuio(td);
676 else
677 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100678
679 td->runtime[td->ddir] += mtime_since_now(&td->start);
Jens Axboeaea47d42006-05-26 19:27:29 +0200680 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200681 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
682
Jens Axboeebac4652005-12-08 15:25:21 +0100683 update_rusage_stat(td);
684
685 if (td->error || td->terminate)
686 break;
687
688 if (td->verify == VERIFY_NONE)
689 continue;
690
691 clear_io_state(td);
692 gettimeofday(&td->start, NULL);
693
694 do_verify(td);
695
696 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
697
698 if (td->error || td->terminate)
699 break;
700 }
701
Jens Axboeebac4652005-12-08 15:25:21 +0100702 if (td->bw_log)
703 finish_log(td, td->bw_log, "bw");
704 if (td->slat_log)
705 finish_log(td, td->slat_log, "slat");
706 if (td->clat_log)
707 finish_log(td, td->clat_log, "clat");
Jens Axboe076efc72006-10-27 11:24:25 +0200708 if (td->write_iolog_file)
Jens Axboe843a7412006-06-01 21:14:21 -0700709 write_iolog_close(td);
Jens Axboe56f94982006-10-24 09:33:42 +0200710 if (td->exec_postrun)
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200711 system(td->exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +0100712
713 if (exitall_on_terminate)
714 terminate_threads(td->groupid);
715
716err:
Jens Axboe53cdc682006-10-18 11:50:58 +0200717 close_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200718 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100719 cleanup_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100720 td_set_runstate(td, TD_EXITED);
721 return NULL;
722
723}
724
Jens Axboe906c8d72006-06-13 09:37:56 +0200725/*
726 * We cannot pass the td data into a forked process, so attach the td and
727 * pass it to the thread worker.
728 */
Jens Axboeebac4652005-12-08 15:25:21 +0100729static void *fork_main(int shmid, int offset)
730{
731 struct thread_data *td;
732 void *data;
733
734 data = shmat(shmid, NULL, 0);
735 if (data == (void *) -1) {
736 perror("shmat");
737 return NULL;
738 }
739
740 td = data + offset * sizeof(struct thread_data);
741 thread_main(td);
742 shmdt(data);
743 return NULL;
744}
745
Jens Axboe906c8d72006-06-13 09:37:56 +0200746/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200747 * Run over the job map and reap the threads that have exited, if any.
748 */
Jens Axboeebac4652005-12-08 15:25:21 +0100749static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
750{
Jens Axboe34572e22006-10-20 09:33:18 +0200751 struct thread_data *td;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200752 int i, cputhreads;
Jens Axboeebac4652005-12-08 15:25:21 +0100753
754 /*
755 * reap exited threads (TD_EXITED -> TD_REAPED)
756 */
Jens Axboe34572e22006-10-20 09:33:18 +0200757 cputhreads = 0;
758 for_each_td(td, i) {
Jens Axboe84585002006-10-19 20:26:22 +0200759 /*
760 * ->io_ops is NULL for a thread that has closed its
761 * io engine
762 */
763 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200764 cputhreads++;
765
Jens Axboeebac4652005-12-08 15:25:21 +0100766 if (td->runstate != TD_EXITED)
767 continue;
768
769 td_set_runstate(td, TD_REAPED);
770
771 if (td->use_thread) {
772 long ret;
773
774 if (pthread_join(td->thread, (void *) &ret))
775 perror("thread_join");
776 } else
777 waitpid(td->pid, NULL, 0);
778
779 (*nr_running)--;
780 (*m_rate) -= td->ratemin;
781 (*t_rate) -= td->rate;
782 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200783
784 if (*nr_running == cputhreads)
785 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100786}
787
Jens Axboe906c8d72006-06-13 09:37:56 +0200788/*
789 * Main function for kicking off and reaping jobs, as needed.
790 */
Jens Axboeebac4652005-12-08 15:25:21 +0100791static void run_threads(void)
792{
Jens Axboeebac4652005-12-08 15:25:21 +0100793 struct thread_data *td;
794 unsigned long spent;
795 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +0200796
Jens Axboe2f9ade32006-10-20 11:25:52 +0200797 if (fio_pin_memory())
798 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100799
Jens Axboec6ae0a52006-06-12 11:04:44 +0200800 if (!terse_output) {
801 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
802 fflush(stdout);
803 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200804
Jens Axboe4efa9702006-05-31 11:56:49 +0200805 signal(SIGINT, sig_handler);
806 signal(SIGALRM, sig_handler);
807
Jens Axboeebac4652005-12-08 15:25:21 +0100808 todo = thread_number;
809 nr_running = 0;
810 nr_started = 0;
811 m_rate = t_rate = 0;
812
Jens Axboe34572e22006-10-20 09:33:18 +0200813 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +0200814 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100815
816 init_disk_util(td);
817
818 if (!td->create_serialize)
819 continue;
820
821 /*
822 * do file setup here so it happens sequentially,
823 * we don't want X number of threads getting their
824 * client data interspersed on disk
825 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200826 if (setup_files(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100827 td_set_runstate(td, TD_REAPED);
828 todo--;
829 }
830 }
831
Jens Axboe263e5292006-10-18 15:37:01 +0200832 time_init();
Jens Axboeebac4652005-12-08 15:25:21 +0100833
834 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +0200835 struct thread_data *map[MAX_JOBS];
836 struct timeval this_start;
837 int this_jobs = 0, left;
838
Jens Axboeebac4652005-12-08 15:25:21 +0100839 /*
840 * create threads (TD_NOT_CREATED -> TD_CREATED)
841 */
Jens Axboe34572e22006-10-20 09:33:18 +0200842 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +0100843 if (td->runstate != TD_NOT_CREATED)
844 continue;
845
846 /*
847 * never got a chance to start, killed by other
848 * thread for some reason
849 */
850 if (td->terminate) {
851 todo--;
852 continue;
853 }
854
855 if (td->start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +0200856 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +0100857
858 if (td->start_delay * 1000 > spent)
859 continue;
860 }
861
862 if (td->stonewall && (nr_started || nr_running))
863 break;
864
Jens Axboe75154842006-06-01 13:56:09 +0200865 /*
866 * Set state to created. Thread will transition
867 * to TD_INITIALIZED when it's done setting up.
868 */
Jens Axboeebac4652005-12-08 15:25:21 +0100869 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +0200870 map[this_jobs++] = td;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200871 fio_sem_init(&startup_sem, 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100872 nr_started++;
873
874 if (td->use_thread) {
875 if (pthread_create(&td->thread, NULL, thread_main, td)) {
876 perror("thread_create");
877 nr_started--;
878 }
879 } else {
880 if (fork())
Jens Axboebbfd6b02006-06-07 19:42:54 +0200881 fio_sem_down(&startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +0100882 else {
883 fork_main(shm_id, i);
884 exit(0);
885 }
886 }
887 }
888
889 /*
Jens Axboe75154842006-06-01 13:56:09 +0200890 * Wait for the started threads to transition to
891 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +0100892 */
Jens Axboe75154842006-06-01 13:56:09 +0200893 gettimeofday(&this_start, NULL);
894 left = this_jobs;
895 while (left) {
896 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
897 break;
898
899 usleep(100000);
900
901 for (i = 0; i < this_jobs; i++) {
902 td = map[i];
903 if (!td)
904 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200905 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +0200906 map[i] = NULL;
907 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200908 } else if (td->runstate >= TD_EXITED) {
909 map[i] = NULL;
910 left--;
911 todo--;
912 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +0200913 }
914 }
915 }
916
917 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200918 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +0200919 for (i = 0; i < this_jobs; i++) {
920 td = map[i];
921 if (!td)
922 continue;
923 kill(td->pid, SIGTERM);
924 }
925 break;
926 }
927
928 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +0200929 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +0200930 */
Jens Axboe34572e22006-10-20 09:33:18 +0200931 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +0200932 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +0100933 continue;
934
935 td_set_runstate(td, TD_RUNNING);
936 nr_running++;
937 nr_started--;
938 m_rate += td->ratemin;
939 t_rate += td->rate;
Jens Axboe75154842006-06-01 13:56:09 +0200940 todo--;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200941 fio_sem_up(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100942 }
943
944 reap_threads(&nr_running, &t_rate, &m_rate);
945
946 if (todo)
947 usleep(100000);
948 }
949
950 while (nr_running) {
951 reap_threads(&nr_running, &t_rate, &m_rate);
952 usleep(10000);
953 }
954
955 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +0200956 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +0100957}
958
Jens Axboeebac4652005-12-08 15:25:21 +0100959int main(int argc, char *argv[])
960{
961 if (parse_options(argc, argv))
962 return 1;
963
964 if (!thread_number) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200965 log_err("Nothing to do\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100966 return 1;
967 }
968
969 disk_util_timer_arm();
970
971 run_threads();
972 show_run_stats();
973
974 return 0;
975}