blob: dbb9ef65502c0e136785d22902099719e8304173 [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 Axboedbe11252007-02-11 00:19:51 +010029#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010030#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010031#include <sys/stat.h>
32#include <sys/wait.h>
33#include <sys/ipc.h>
34#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010035#include <sys/mman.h>
36
37#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020038#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010039#include "smalloc.h"
Jens Axboeebac4652005-12-08 15:25:21 +010040
Jens Axboecfc99db2007-03-14 10:34:47 +010041unsigned long page_mask;
42unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010043#define ALIGN(buf) \
44 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010045
46int groupid = 0;
47int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010048int nr_process = 0;
49int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010050int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020051int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020052unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010053
Jens Axboecdd18ad2008-02-27 18:58:00 +010054static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010055static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010056static int exit_value;
Jens Axboeccb0fa22008-05-30 23:18:00 +020057static struct itimerval itimer;
Jens Axboeebac4652005-12-08 15:25:21 +010058
Jens Axboebb3884d2007-01-17 17:23:11 +110059struct io_log *agg_io_log[2];
60
Jens Axboeebac4652005-12-08 15:25:21 +010061#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020062#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010063
Jens Axboeb29ee5b2008-09-11 10:17:26 +020064void td_set_runstate(struct thread_data *td, int runstate)
Jens Axboe6ce15a32007-01-12 09:04:41 +010065{
Jens Axboed8fab1b2008-03-06 10:25:17 +010066 if (td->runstate == runstate)
67 return;
68
Jens Axboe5921e802008-05-30 15:02:38 +020069 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
70 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010071 td->runstate = runstate;
72}
73
Jens Axboe390c40e2007-03-05 12:35:29 +010074static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010075{
Jens Axboe34572e22006-10-20 09:33:18 +020076 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010077 int i;
78
Jens Axboe6f88bcb2008-03-19 10:29:07 +010079 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
80
Jens Axboe34572e22006-10-20 09:33:18 +020081 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010082 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010083 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +020084 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010085 td->terminate = 1;
86 td->o.start_delay = 0;
87
Jens Axboe7a93ab12007-04-18 12:25:41 +020088 /*
89 * if the thread is running, just let it exit
90 */
91 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010092 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010093 else {
94 struct ioengine_ops *ops = td->io_ops;
95
96 if (ops && (ops->flags & FIO_SIGQUIT))
97 kill(td->pid, SIGQUIT);
98 }
Jens Axboeebac4652005-12-08 15:25:21 +010099 }
100 }
101}
102
Jens Axboeccb0fa22008-05-30 23:18:00 +0200103static void status_timer_arm(void)
104{
105 itimer.it_value.tv_sec = 0;
106 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
107 setitimer(ITIMER_REAL, &itimer, NULL);
108}
109
Jens Axboe697a6062008-05-31 00:04:45 +0200110static void sig_alrm(int sig)
Jens Axboeccb0fa22008-05-30 23:18:00 +0200111{
Jens Axboe697a6062008-05-31 00:04:45 +0200112 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100113 update_io_ticks();
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100114 print_thread_status();
Jens Axboeccb0fa22008-05-30 23:18:00 +0200115 status_timer_arm();
Jens Axboe697a6062008-05-31 00:04:45 +0200116 }
117}
118
119static void sig_int(int sig)
120{
121 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100122 printf("\nfio: terminating on signal %d\n", sig);
123 fflush(stdout);
124 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100125 }
126}
127
Jens Axboede79c912008-08-04 21:43:55 +0200128static void sig_ill(int sig)
129{
130 if (!threads)
131 return;
132
133 log_err("fio: illegal instruction. your cpu does not support "
134 "the sse4.2 instruction for crc32c\n");
135 terminate_threads(TERMINATE_ALL);
136 exit(4);
137}
138
Jens Axboe697a6062008-05-31 00:04:45 +0200139static void set_sig_handlers(void)
140{
141 struct sigaction act;
142
143 memset(&act, 0, sizeof(act));
144 act.sa_handler = sig_alrm;
145 act.sa_flags = SA_RESTART;
146 sigaction(SIGALRM, &act, NULL);
147
148 memset(&act, 0, sizeof(act));
149 act.sa_handler = sig_int;
150 act.sa_flags = SA_RESTART;
151 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200152
153 memset(&act, 0, sizeof(act));
154 act.sa_handler = sig_ill;
155 act.sa_flags = SA_RESTART;
156 sigaction(SIGILL, &act, NULL);
Jens Axboe697a6062008-05-31 00:04:45 +0200157}
158
159/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200160 * Check if we are above the minimum rate given.
161 */
Jens Axboeebac4652005-12-08 15:25:21 +0100162static int check_min_rate(struct thread_data *td, struct timeval *now)
163{
Jens Axboe09042002007-02-23 10:56:22 +0100164 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100165 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100166 unsigned long spent;
167 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100168
169 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100170 * No minimum rate set, always ok
171 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100172 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100173 return 0;
174
175 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100176 * allow a 2 second settle period in the beginning
177 */
178 if (mtime_since(&td->start, now) < 2000)
179 return 0;
180
Jens Axboe4e991c22007-03-15 11:41:11 +0100181 if (td_read(td)) {
182 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100183 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100184 }
185 if (td_write(td)) {
186 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100187 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100188 }
Jens Axboe09042002007-02-23 10:56:22 +0100189
Jens Axboeebac4652005-12-08 15:25:21 +0100190 /*
191 * if rate blocks is set, sample is running
192 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100193 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100194 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100195 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100196 return 0;
197
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100198 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100199 /*
200 * check bandwidth specified rate
201 */
202 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100203 log_err("%s: min rate %u not met\n", td->o.name,
204 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100205 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100206 } else {
207 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100208 if (rate < td->o.ratemin ||
209 bytes < td->rate_bytes) {
210 log_err("%s: min rate %u not met, got"
211 " %luKiB/sec\n", td->o.name,
212 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100213 return 1;
214 }
215 }
216 } else {
217 /*
218 * checks iops specified rate
219 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100220 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100221 log_err("%s: min iops rate %u not met\n",
222 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100223 return 1;
224 } else {
225 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100226 if (rate < td->o.rate_iops_min ||
227 iops < td->rate_blocks) {
228 log_err("%s: min iops rate %u not met,"
229 " got %lu\n", td->o.name,
230 td->o.rate_iops_min,
231 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100232 }
Jens Axboe413dd452007-02-23 09:26:09 +0100233 }
Jens Axboeebac4652005-12-08 15:25:21 +0100234 }
235 }
236
Jens Axboe09042002007-02-23 10:56:22 +0100237 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100238 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100239 memcpy(&td->lastrate, now, sizeof(*now));
240 return 0;
241}
242
243static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
244{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100245 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100246 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100247 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100248 return 1;
249
250 return 0;
251}
252
Jens Axboe906c8d72006-06-13 09:37:56 +0200253/*
254 * When job exits, we can cancel the in-flight IO if we are using async
255 * io. Attempt to do so.
256 */
Jens Axboeebac4652005-12-08 15:25:21 +0100257static void cleanup_pending_aio(struct thread_data *td)
258{
Jens Axboe01743ee2008-06-02 12:19:19 +0200259 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100260 struct io_u *io_u;
261 int r;
262
263 /*
264 * get immediately available events, if any
265 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100266 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100267 if (r < 0)
268 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100269
270 /*
271 * now cancel remaining active events
272 */
Jens Axboe2866c822006-10-09 15:57:48 +0200273 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200274 flist_for_each_safe(entry, n, &td->io_u_busylist) {
275 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100276
Jens Axboe0c6e7512007-02-22 11:19:39 +0100277 /*
278 * if the io_u isn't in flight, then that generally
279 * means someone leaked an io_u. complain but fix
280 * it up, so we don't stall here.
281 */
282 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
283 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100284 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100285 } else {
286 r = td->io_ops->cancel(td, io_u);
287 if (!r)
288 put_io_u(td, io_u);
289 }
Jens Axboeebac4652005-12-08 15:25:21 +0100290 }
291 }
292
Jens Axboe97601022007-02-18 12:47:29 +0100293 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100294 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100295}
296
Jens Axboe906c8d72006-06-13 09:37:56 +0200297/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200298 * Helper to handle the final sync of a file. Works just like the normal
299 * io path, just does everything sync.
300 */
301static int fio_io_sync(struct thread_data *td, struct fio_file *f)
302{
303 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200304 int ret;
305
306 if (!io_u)
307 return 1;
308
309 io_u->ddir = DDIR_SYNC;
310 io_u->file = f;
311
312 if (td_io_prep(td, io_u)) {
313 put_io_u(td, io_u);
314 return 1;
315 }
316
Jens Axboe755200a2007-02-19 13:08:12 +0100317requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200318 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100319 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100320 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200321 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200322 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100323 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100324 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100325 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100326 } else if (ret == FIO_Q_COMPLETED) {
327 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100328 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100329 return 1;
330 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200331
Jens Axboed7762cf2007-02-23 12:34:57 +0100332 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100333 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100334 } else if (ret == FIO_Q_BUSY) {
335 if (td_io_commit(td))
336 return 1;
337 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200338 }
339
340 return 0;
341}
342
343/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100344 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200345 * reads the blocks back in, and checks the crc/md5 of the data.
346 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100347static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100348{
Jens Axboe53cdc682006-10-18 11:50:58 +0200349 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100350 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100351 int ret, min_events;
352 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200353
354 /*
355 * sync io first and invalidate cache, to make sure we really
356 * read from disk.
357 */
358 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100359 if (!(f->flags & FIO_FILE_OPEN))
360 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100361 if (fio_io_sync(td, f))
362 break;
363 if (file_invalidate_cache(td, f))
364 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200365 }
Jens Axboeebac4652005-12-08 15:25:21 +0100366
Jens Axboeb2fdda42007-02-20 20:52:51 +0100367 if (td->error)
368 return;
369
Jens Axboeebac4652005-12-08 15:25:21 +0100370 td_set_runstate(td, TD_VERIFYING);
371
Jens Axboe36167d82007-02-18 05:41:31 +0100372 io_u = NULL;
373 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200374 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100375
Jens Axboeebac4652005-12-08 15:25:21 +0100376 io_u = __get_io_u(td);
377 if (!io_u)
378 break;
379
Jens Axboe069c2912007-02-21 23:02:39 +0100380 if (runtime_exceeded(td, &io_u->start_time)) {
381 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200382 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200383 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100384 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200385
Jens Axboe069c2912007-02-21 23:02:39 +0100386 if (get_next_verify(td, io_u)) {
387 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100388 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100389 }
Jens Axboeebac4652005-12-08 15:25:21 +0100390
Jens Axboe069c2912007-02-21 23:02:39 +0100391 if (td_io_prep(td, io_u)) {
392 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100393 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100394 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100395
396 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100397
Jens Axboe11786802007-03-05 18:33:22 +0100398 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100399 switch (ret) {
400 case FIO_Q_COMPLETED:
401 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100402 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100403 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100404 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200405 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100406
Jens Axboe9e9d1642007-03-12 09:37:46 +0100407 /*
408 * zero read, fail
409 */
410 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200411 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100412 put_io_u(td, io_u);
413 break;
414 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200415
Jens Axboe36167d82007-02-18 05:41:31 +0100416 io_u->xfer_buflen = io_u->resid;
417 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200418 io_u->offset += bytes;
419
Jens Axboe30061b92007-04-17 13:31:34 +0200420 td->ts.short_io_u[io_u->ddir]++;
421
Jens Axboed460eb32007-04-16 21:39:33 +0200422 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200423 goto sync_done;
424
Jens Axboe11786802007-03-05 18:33:22 +0100425 requeue_io_u(td, &io_u);
426 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200427sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100428 ret = io_u_sync_complete(td, io_u);
429 if (ret < 0)
430 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100431 }
Jens Axboe36167d82007-02-18 05:41:31 +0100432 continue;
433 case FIO_Q_QUEUED:
434 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100435 case FIO_Q_BUSY:
436 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100437 ret2 = td_io_commit(td);
438 if (ret2 < 0)
439 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100440 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100441 default:
442 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100443 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100444 break;
445 }
446
Jens Axboe99784632007-02-19 10:06:17 +0100447 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100448 break;
449
Jens Axboe3af6ef32007-02-18 06:57:43 +0100450 /*
451 * if we can queue more, do so. but check if there are
452 * completed io_u's first.
453 */
Jens Axboe49504212008-06-05 09:03:30 +0200454 full = queue_full(td) || ret == FIO_Q_BUSY;
455 if (full || !td->o.iodepth_batch_complete) {
456 min_events = td->o.iodepth_batch_complete;
457 if (full && !min_events)
Jens Axboe838bc702008-05-22 13:08:23 +0200458 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100459
Jens Axboe49504212008-06-05 09:03:30 +0200460 do {
461 /*
462 * Reap required number of io units, if any,
463 * and do the verification on them through
464 * the callback handler
465 */
466 if (io_u_queued_complete(td, min_events) < 0) {
467 ret = -1;
468 break;
469 }
470 } while (full && (td->cur_depth > td->o.iodepth_low));
471 }
472 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100473 break;
474 }
475
Jens Axboec01c0392007-02-26 12:43:42 +0100476 if (!td->error) {
477 min_events = td->cur_depth;
478
479 if (min_events)
480 ret = io_u_queued_complete(td, min_events);
481 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100482 cleanup_pending_aio(td);
483
484 td_set_runstate(td, TD_RUNNING);
485}
486
Jens Axboe32cd46a2006-06-07 13:40:40 +0200487/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200488 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200489 * and reaps them, checking for rate and errors along the way.
490 */
Jens Axboeebac4652005-12-08 15:25:21 +0100491static void do_io(struct thread_data *td)
492{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100493 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100494 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100495 unsigned int i;
496 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100497
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200498 if (in_ramp_time(td))
499 td_set_runstate(td, TD_RAMP);
500 else
501 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200502
Jens Axboe7bb48f82007-03-27 15:30:28 +0200503 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100504 struct timeval comp_time;
505 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200506 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100507 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200508 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100509
510 if (td->terminate)
511 break;
512
Jens Axboe3d7c3912007-02-19 13:16:12 +0100513 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100514 if (!io_u)
515 break;
516
517 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100518
519 if (runtime_exceeded(td, &s)) {
520 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200521 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100522 break;
523 }
Jens Axboe36167d82007-02-18 05:41:31 +0100524
Jens Axboeb67740d2007-07-26 12:22:30 +0200525 /*
526 * Add verification end_io handler, if asked to verify
527 * a previously written file.
528 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100529 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200530 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100531 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200532 } else if (in_ramp_time(td))
533 td_set_runstate(td, TD_RAMP);
534 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100535 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200536
Jens Axboe11786802007-03-05 18:33:22 +0100537 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100538 switch (ret) {
539 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100540 if (io_u->error)
541 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100542 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100543 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200544 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100545
Jens Axboe9e9d1642007-03-12 09:37:46 +0100546 /*
547 * zero read, fail
548 */
549 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200550 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100551 put_io_u(td, io_u);
552 break;
553 }
554
Jens Axboe36167d82007-02-18 05:41:31 +0100555 io_u->xfer_buflen = io_u->resid;
556 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200557 io_u->offset += bytes;
558
Jens Axboe30061b92007-04-17 13:31:34 +0200559 td->ts.short_io_u[io_u->ddir]++;
560
Jens Axboed460eb32007-04-16 21:39:33 +0200561 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200562 goto sync_done;
563
Jens Axboe11786802007-03-05 18:33:22 +0100564 requeue_io_u(td, &io_u);
565 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200566sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100567 fio_gettime(&comp_time, NULL);
568 bytes_done = io_u_sync_complete(td, io_u);
569 if (bytes_done < 0)
570 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100571 }
Jens Axboe36167d82007-02-18 05:41:31 +0100572 break;
573 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100574 /*
575 * if the engine doesn't have a commit hook,
576 * the io_u is really queued. if it does have such
577 * a hook, it has to call io_u_queued() itself.
578 */
579 if (td->io_ops->commit == NULL)
580 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100581 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100582 case FIO_Q_BUSY:
583 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100584 ret2 = td_io_commit(td);
585 if (ret2 < 0)
586 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100587 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100588 default:
589 assert(ret < 0);
590 put_io_u(td, io_u);
591 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100592 }
593
Jens Axboe99784632007-02-19 10:06:17 +0100594 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100595 break;
596
Jens Axboe97601022007-02-18 12:47:29 +0100597 /*
598 * See if we need to complete some commands
599 */
Jens Axboe49504212008-06-05 09:03:30 +0200600 full = queue_full(td) || ret == FIO_Q_BUSY;
601 if (full || !td->o.iodepth_batch_complete) {
602 min_evts = td->o.iodepth_batch_complete;
603 if (full && !min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100604 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200605
Jens Axboe97601022007-02-18 12:47:29 +0100606 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200607
608 do {
609 ret = io_u_queued_complete(td, min_evts);
610 if (ret <= 0)
611 break;
612
613 bytes_done += ret;
614 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100615 }
616
Jens Axboe49504212008-06-05 09:03:30 +0200617 if (ret < 0)
618 break;
Jens Axboe97601022007-02-18 12:47:29 +0100619 if (!bytes_done)
620 continue;
621
Jens Axboeebac4652005-12-08 15:25:21 +0100622 /*
623 * the rate is batched for now, it should work for batches
624 * of completions except the very first one which may look
625 * a little bursty
626 */
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200627 if (!in_ramp_time(td)) {
Jens Axboe721938a2008-09-10 09:46:16 +0200628 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100629
Jens Axboe721938a2008-09-10 09:46:16 +0200630 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100631
Jens Axboe721938a2008-09-10 09:46:16 +0200632 if (check_min_rate(td, &comp_time)) {
633 if (exitall_on_terminate)
634 terminate_threads(td->groupid);
635 td_verror(td, EIO, "check_min_rate");
636 break;
637 }
Jens Axboeebac4652005-12-08 15:25:21 +0100638 }
639
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100640 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100641 unsigned long long b;
642
643 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100644 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100645 int left;
646
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100647 if (td->o.thinktime_spin)
648 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100649
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100650 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100651 if (left)
652 usec_sleep(td, left);
653 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100654 }
Jens Axboeebac4652005-12-08 15:25:21 +0100655 }
656
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100657 if (td->o.fill_device && td->error == ENOSPC) {
658 td->error = 0;
659 td->terminate = 1;
660 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100661 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100662 struct fio_file *f;
663
Jens Axboec01c0392007-02-26 12:43:42 +0100664 i = td->cur_depth;
665 if (i)
666 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100667
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100668 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200669 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100670
671 for_each_file(td, f, i) {
672 if (!(f->flags & FIO_FILE_OPEN))
673 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200674 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100675 }
Jens Axboe84585002006-10-19 20:26:22 +0200676 }
Jens Axboec01c0392007-02-26 12:43:42 +0100677 } else
678 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100679
680 /*
681 * stop job if we failed doing any IO
682 */
683 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
684 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100685}
686
Jens Axboeebac4652005-12-08 15:25:21 +0100687static void cleanup_io_u(struct thread_data *td)
688{
Jens Axboe01743ee2008-06-02 12:19:19 +0200689 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100690 struct io_u *io_u;
691
Jens Axboe01743ee2008-06-02 12:19:19 +0200692 flist_for_each_safe(entry, n, &td->io_u_freelist) {
693 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100694
Jens Axboe01743ee2008-06-02 12:19:19 +0200695 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100696 free(io_u);
697 }
698
Jens Axboe2f9ade32006-10-20 11:25:52 +0200699 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100700}
701
702static int init_io_u(struct thread_data *td)
703{
704 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100705 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100706 int i, max_units;
707 char *p;
708
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100709 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100710 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100711 td->orig_buffer_size = (unsigned long long) max_bs
712 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100713
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100714 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
715 unsigned long bs;
716
717 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
718 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
719 }
Jens Axboeebac4652005-12-08 15:25:21 +0100720
Jens Axboec3990642007-07-19 10:17:09 +0200721 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
722 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
723 return 1;
724 }
725
Jens Axboe2f9ade32006-10-20 11:25:52 +0200726 if (allocate_io_mem(td))
727 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100728
Jens Axboeb3f378e2007-07-20 12:39:22 +0200729 if (td->o.odirect)
730 p = ALIGN(td->orig_buffer);
731 else
732 p = td->orig_buffer;
733
Jens Axboeebac4652005-12-08 15:25:21 +0100734 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200735 if (td->terminate)
736 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100737 io_u = malloc(sizeof(*io_u));
738 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200739 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100740
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200741 if (!(td->io_ops->flags & FIO_NOIO)) {
742 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200743
Jens Axboe5973caf2008-05-21 19:52:35 +0200744 if (td_write(td) && !td->o.refill_buffers)
745 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200746 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100747
Jens Axboeb1ff3402006-02-17 11:35:58 +0100748 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100749 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200750 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100751 }
752
Jens Axboe433afcb2007-02-22 10:39:01 +0100753 io_u_init_timeout();
754
Jens Axboeebac4652005-12-08 15:25:21 +0100755 return 0;
756}
757
Jens Axboeda867742006-06-06 10:39:10 -0700758static int switch_ioscheduler(struct thread_data *td)
759{
760 char tmp[256], tmp2[128];
761 FILE *f;
762 int ret;
763
Jens Axboeba0fbe12007-03-09 14:34:23 +0100764 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200765 return 0;
766
Jens Axboeda867742006-06-06 10:39:10 -0700767 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
768
769 f = fopen(tmp, "r+");
770 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200771 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100772 log_err("fio: os or kernel doesn't support IO scheduler"
773 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200774 return 0;
775 }
776 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700777 return 1;
778 }
779
780 /*
781 * Set io scheduler.
782 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100783 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700784 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100785 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700786 fclose(f);
787 return 1;
788 }
789
790 rewind(f);
791
792 /*
793 * Read back and check that the selected scheduler is now the default.
794 */
795 ret = fread(tmp, 1, sizeof(tmp), f);
796 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100797 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700798 fclose(f);
799 return 1;
800 }
801
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100802 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700803 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100804 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100805 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700806 fclose(f);
807 return 1;
808 }
809
810 fclose(f);
811 return 0;
812}
813
Jens Axboe492158c2007-05-24 10:32:47 +0200814static int keep_running(struct thread_data *td)
815{
816 unsigned long long io_done;
817
Jens Axboe20e354e2007-07-23 14:36:16 +0200818 if (td->done)
819 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200820 if (td->o.time_based)
821 return 1;
822 if (td->o.loops) {
823 td->o.loops--;
824 return 1;
825 }
826
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100827 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
828 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200829 if (io_done < td->o.size)
830 return 1;
831
832 return 0;
833}
834
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200835static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100836{
Jens Axboe756867b2007-03-06 15:19:24 +0100837 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100838 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100839 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100840 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100841 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100842 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100843
Jens Axboec1324df2007-02-19 19:06:41 +0100844 td->last_was_sync = 0;
845
Jens Axboe2ba1c292008-02-01 13:16:38 +0100846 /*
847 * reset file done count if we are to start over
848 */
849 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200850 td->nr_done_files = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200851}
852
853void reset_all_stats(struct thread_data *td)
854{
855 struct timeval tv;
856 int i;
857
858 reset_io_counters(td);
859
860 for (i = 0; i < 2; i++) {
861 td->io_bytes[i] = 0;
862 td->io_blocks[i] = 0;
863 td->io_issues[i] = 0;
864 td->ts.total_io_u[i] = 0;
865 }
866
867 fio_gettime(&tv, NULL);
868 memcpy(&td->epoch, &tv, sizeof(tv));
869 memcpy(&td->start, &tv, sizeof(tv));
870}
871
872static int clear_io_state(struct thread_data *td)
873{
874 struct fio_file *f;
875 unsigned int i;
876 int ret;
877
878 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +0200879
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100880 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100881
882 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200883 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200884 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100885 ret = td_io_open_file(td, f);
886 if (ret)
887 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200888 }
Jens Axboea978ba62007-03-08 14:29:03 +0100889
890 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100891}
892
Jens Axboe906c8d72006-06-13 09:37:56 +0200893/*
894 * Entry point for the thread based jobs. The process based jobs end up
895 * here as well, after a little setup.
896 */
Jens Axboeebac4652005-12-08 15:25:21 +0100897static void *thread_main(void *data)
898{
Shawn Lewis10927312007-11-21 09:38:13 +0100899 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100900 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100901 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100902
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100903 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100904 setsid();
905
906 td->pid = getpid();
907
Jens Axboe5921e802008-05-30 15:02:38 +0200908 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100909
Jens Axboe01743ee2008-06-02 12:19:19 +0200910 INIT_FLIST_HEAD(&td->io_u_freelist);
911 INIT_FLIST_HEAD(&td->io_u_busylist);
912 INIT_FLIST_HEAD(&td->io_u_requeues);
913 INIT_FLIST_HEAD(&td->io_log_list);
914 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200915 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200916
Jens Axboe75154842006-06-01 13:56:09 +0200917 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100918 fio_mutex_up(startup_mutex);
919 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100920
Jens Axboe37f56872007-03-09 12:42:35 +0100921 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100922 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100923 * eating a file descriptor
924 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100925 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100926
Jens Axboed84f8d42007-05-16 08:47:46 +0200927 /*
928 * May alter parameters that init_io_u() will use, so we need to
929 * do this first.
930 */
931 if (init_iolog(td))
932 goto err;
933
Jens Axboedb1cd902007-04-26 13:47:07 +0200934 if (init_io_u(td))
935 goto err;
936
Jens Axboe375b2692007-05-22 09:13:02 +0200937 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200938 td_verror(td, errno, "cpu_set_affinity");
939 goto err;
940 }
941
Jens Axboeac684782007-11-08 08:29:07 +0100942 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200943 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
944 td_verror(td, errno, "ioprio_set");
945 goto err;
946 }
947 }
948
949 if (nice(td->o.nice) == -1) {
950 td_verror(td, errno, "nice");
951 goto err;
952 }
953
954 if (td->o.ioscheduler && switch_ioscheduler(td))
955 goto err;
956
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100957 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100958 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100959
960 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100961 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100962
Jens Axboeb5af8292007-03-08 12:43:13 +0100963 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100964 goto err;
965
Jens Axboe68727072007-03-15 20:49:25 +0100966 if (init_random_map(td))
967 goto err;
968
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100969 if (td->o.exec_prerun) {
970 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100971 goto err;
972 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200973
Jens Axboe69008992006-11-24 13:12:56 +0100974 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100975 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100976 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100977
978 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100979 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200980 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100981 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100982 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100983
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100984 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100985 memcpy(&td->lastrate, &td->ts.stat_sample_time,
986 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100987
Jens Axboea978ba62007-03-08 14:29:03 +0100988 if (clear_state && clear_io_state(td))
989 break;
990
Jens Axboeebac4652005-12-08 15:25:21 +0100991 prune_io_piece_log(td);
992
Jens Axboeba0fbe12007-03-09 14:34:23 +0100993 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100994
Jens Axboea978ba62007-03-08 14:29:03 +0100995 clear_state = 1;
996
Jens Axboe38d77ca2007-03-21 14:20:20 +0100997 if (td_read(td) && td->io_bytes[DDIR_READ]) {
998 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100999 elapsed = utime_since(&td->start,
1000 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001001 else
1002 elapsed = utime_since_now(&td->start);
1003
1004 runtime[DDIR_READ] += elapsed;
1005 }
1006 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1007 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001008 elapsed = utime_since(&td->start,
1009 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001010 else
1011 elapsed = utime_since_now(&td->start);
1012
1013 runtime[DDIR_WRITE] += elapsed;
1014 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001015
Jens Axboeebac4652005-12-08 15:25:21 +01001016 if (td->error || td->terminate)
1017 break;
1018
Shawn Lewise84c73a2007-08-02 22:19:32 +02001019 if (!td->o.do_verify ||
1020 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001021 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001022 continue;
1023
Jens Axboea978ba62007-03-08 14:29:03 +01001024 if (clear_io_state(td))
1025 break;
1026
Jens Axboe02bcaa82006-11-24 10:42:00 +01001027 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001028
1029 do_verify(td);
1030
Jens Axboe69008992006-11-24 13:12:56 +01001031 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001032
1033 if (td->error || td->terminate)
1034 break;
1035 }
1036
Jens Axboe36dff962006-11-24 13:29:20 +01001037 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001038 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1039 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001040 td->ts.total_run_time = mtime_since_now(&td->epoch);
1041 td->ts.io_bytes[0] = td->io_bytes[0];
1042 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001043
Jens Axboe756867b2007-03-06 15:19:24 +01001044 if (td->ts.bw_log)
1045 finish_log(td, td->ts.bw_log, "bw");
1046 if (td->ts.slat_log)
1047 finish_log(td, td->ts.slat_log, "slat");
1048 if (td->ts.clat_log)
1049 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001050 if (td->o.exec_postrun) {
1051 if (system(td->o.exec_postrun) < 0)
1052 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001053 }
Jens Axboeebac4652005-12-08 15:25:21 +01001054
1055 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001056 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001057
1058err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001059 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001060 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001061 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001062 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001063 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001064 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001065
1066 /*
1067 * do this very late, it will log file closing as well
1068 */
1069 if (td->o.write_iolog_file)
1070 write_iolog_close(td);
1071
Jens Axboed23bb322007-03-23 15:57:56 +01001072 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001073 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001074 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001075}
1076
Jens Axboe906c8d72006-06-13 09:37:56 +02001077/*
1078 * We cannot pass the td data into a forked process, so attach the td and
1079 * pass it to the thread worker.
1080 */
Jens Axboea6418142007-02-17 04:47:18 +01001081static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001082{
1083 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001084 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001085
1086 data = shmat(shmid, NULL, 0);
1087 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001088 int __err = errno;
1089
Jens Axboeebac4652005-12-08 15:25:21 +01001090 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001091 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001092 }
1093
1094 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001095 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001096 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001097 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001098}
1099
Jens Axboe906c8d72006-06-13 09:37:56 +02001100/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001101 * Run over the job map and reap the threads that have exited, if any.
1102 */
Jens Axboeebac4652005-12-08 15:25:21 +01001103static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1104{
Jens Axboe34572e22006-10-20 09:33:18 +02001105 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001106 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001107
1108 /*
1109 * reap exited threads (TD_EXITED -> TD_REAPED)
1110 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001111 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001112 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001113 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001114
Jens Axboe84585002006-10-19 20:26:22 +02001115 /*
1116 * ->io_ops is NULL for a thread that has closed its
1117 * io engine
1118 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001119 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001120 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001121 else
1122 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001123
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001124 if (!td->pid) {
1125 pending++;
1126 continue;
1127 }
1128 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001129 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001130 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001131 if (td->runstate == TD_EXITED) {
1132 td_set_runstate(td, TD_REAPED);
1133 goto reaped;
1134 }
1135 continue;
1136 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001137
1138 flags = WNOHANG;
1139 if (td->runstate == TD_EXITED)
1140 flags = 0;
1141
1142 /*
1143 * check if someone quit or got killed in an unusual way
1144 */
1145 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001146 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001147 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001148 log_err("fio: pid=%d disappeared %d\n",
1149 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001150 td_set_runstate(td, TD_REAPED);
1151 goto reaped;
1152 }
1153 perror("waitpid");
1154 } else if (ret == td->pid) {
1155 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001156 int sig = WTERMSIG(status);
1157
Jens Axboed9244942007-03-05 12:32:32 +01001158 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001159 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001160 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001161 td_set_runstate(td, TD_REAPED);
1162 goto reaped;
1163 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001164 if (WIFEXITED(status)) {
1165 if (WEXITSTATUS(status) && !td->error)
1166 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001167
Jens Axboea2f77c92007-02-22 11:53:00 +01001168 td_set_runstate(td, TD_REAPED);
1169 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001170 }
1171 }
Jens Axboeebac4652005-12-08 15:25:21 +01001172
Jens Axboea2f77c92007-02-22 11:53:00 +01001173 /*
1174 * thread is not dead, continue
1175 */
gurudas paie48676b2007-04-11 12:44:27 +02001176 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001177 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001178reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001179 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001180 (*m_rate) -= td->o.ratemin;
1181 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001182 if (!td->pid)
1183 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001184
1185 if (td->error)
1186 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001187
1188 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001189 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001190
Jens Axboe1f809d12007-10-25 18:34:02 +02001191 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001192 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001193}
1194
Jens Axboe906c8d72006-06-13 09:37:56 +02001195/*
1196 * Main function for kicking off and reaping jobs, as needed.
1197 */
Jens Axboeebac4652005-12-08 15:25:21 +01001198static void run_threads(void)
1199{
Jens Axboeebac4652005-12-08 15:25:21 +01001200 struct thread_data *td;
1201 unsigned long spent;
1202 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001203
Jens Axboe2f9ade32006-10-20 11:25:52 +02001204 if (fio_pin_memory())
1205 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001206
Jens Axboec6ae0a52006-06-12 11:04:44 +02001207 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001208 printf("Starting ");
1209 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001210 printf("%d thread%s", nr_thread,
1211 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001212 if (nr_process) {
1213 if (nr_thread)
1214 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001215 printf("%d process%s", nr_process,
1216 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001217 }
1218 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001219 fflush(stdout);
1220 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001221
Jens Axboe697a6062008-05-31 00:04:45 +02001222 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001223
Jens Axboeebac4652005-12-08 15:25:21 +01001224 todo = thread_number;
1225 nr_running = 0;
1226 nr_started = 0;
1227 m_rate = t_rate = 0;
1228
Jens Axboe34572e22006-10-20 09:33:18 +02001229 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001230 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001231
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001232 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001233 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001234 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001235 }
Jens Axboeebac4652005-12-08 15:25:21 +01001236
1237 /*
1238 * do file setup here so it happens sequentially,
1239 * we don't want X number of threads getting their
1240 * client data interspersed on disk
1241 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001242 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001243 exit_value++;
1244 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001245 log_err("fio: pid=%d, err=%d/%s\n",
1246 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001247 td_set_runstate(td, TD_REAPED);
1248 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001249 } else {
1250 struct fio_file *f;
1251 unsigned int i;
1252
1253 /*
1254 * for sharing to work, each job must always open
1255 * its own files. so close them, if we opened them
1256 * for creation
1257 */
1258 for_each_file(td, f, i)
1259 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001260 }
Jens Axboe380cf262007-01-11 14:01:27 +01001261
1262 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001263 }
1264
Jens Axboea2f77c92007-02-22 11:53:00 +01001265 set_genesis_time();
1266
Jens Axboeebac4652005-12-08 15:25:21 +01001267 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001268 struct thread_data *map[MAX_JOBS];
1269 struct timeval this_start;
1270 int this_jobs = 0, left;
1271
Jens Axboeebac4652005-12-08 15:25:21 +01001272 /*
1273 * create threads (TD_NOT_CREATED -> TD_CREATED)
1274 */
Jens Axboe34572e22006-10-20 09:33:18 +02001275 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001276 if (td->runstate != TD_NOT_CREATED)
1277 continue;
1278
1279 /*
1280 * never got a chance to start, killed by other
1281 * thread for some reason
1282 */
1283 if (td->terminate) {
1284 todo--;
1285 continue;
1286 }
1287
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001288 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001289 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001290
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001291 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001292 continue;
1293 }
1294
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001295 if (td->o.stonewall && (nr_started || nr_running)) {
1296 dprint(FD_PROCESS, "%s: stonewall wait\n",
1297 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001298 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001299 }
Jens Axboeebac4652005-12-08 15:25:21 +01001300
Jens Axboe75154842006-06-01 13:56:09 +02001301 /*
1302 * Set state to created. Thread will transition
1303 * to TD_INITIALIZED when it's done setting up.
1304 */
Jens Axboeebac4652005-12-08 15:25:21 +01001305 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001306 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001307 nr_started++;
1308
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001309 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001310 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001311 if (pthread_create(&td->thread, NULL,
1312 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001313 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001314 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001315 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001316 }
Jens Axboef4705a72008-03-10 10:52:22 +01001317 if (pthread_detach(td->thread) < 0)
1318 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001319 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001320 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001321 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001322 pid = fork();
1323 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001324 int ret = fork_main(shm_id, i);
1325
Jens Axboe5e1d3062008-05-23 11:55:53 +02001326 _exit(ret);
1327 } else if (i == fio_debug_jobno)
1328 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001329 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001330 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001331 }
1332
1333 /*
Jens Axboe75154842006-06-01 13:56:09 +02001334 * Wait for the started threads to transition to
1335 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001336 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001337 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001338 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001339 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001340 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1341 break;
1342
1343 usleep(100000);
1344
1345 for (i = 0; i < this_jobs; i++) {
1346 td = map[i];
1347 if (!td)
1348 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001349 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001350 map[i] = NULL;
1351 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001352 } else if (td->runstate >= TD_EXITED) {
1353 map[i] = NULL;
1354 left--;
1355 todo--;
1356 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001357 }
1358 }
1359 }
1360
1361 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001362 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001363 for (i = 0; i < this_jobs; i++) {
1364 td = map[i];
1365 if (!td)
1366 continue;
1367 kill(td->pid, SIGTERM);
1368 }
1369 break;
1370 }
1371
1372 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001373 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001374 */
Jens Axboe34572e22006-10-20 09:33:18 +02001375 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001376 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001377 continue;
1378
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001379 if (in_ramp_time(td))
1380 td_set_runstate(td, TD_RAMP);
1381 else
1382 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001383 nr_running++;
1384 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001385 m_rate += td->o.ratemin;
1386 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001387 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001388 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001389 }
1390
1391 reap_threads(&nr_running, &t_rate, &m_rate);
1392
1393 if (todo)
1394 usleep(100000);
1395 }
1396
1397 while (nr_running) {
1398 reap_threads(&nr_running, &t_rate, &m_rate);
1399 usleep(10000);
1400 }
1401
1402 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001403 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001404}
1405
Jens Axboeebac4652005-12-08 15:25:21 +01001406int main(int argc, char *argv[])
1407{
Jens Axboe29d610e2007-02-06 13:25:33 +01001408 long ps;
1409
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001410 sinit();
1411
Jens Axboedbe11252007-02-11 00:19:51 +01001412 /*
1413 * We need locale for number printing, if it isn't set then just
1414 * go with the US format.
1415 */
1416 if (!getenv("LC_NUMERIC"))
1417 setlocale(LC_NUMERIC, "en_US");
1418
Jens Axboeebac4652005-12-08 15:25:21 +01001419 if (parse_options(argc, argv))
1420 return 1;
1421
Jens Axboe4b472fa2007-04-04 11:04:34 +02001422 if (!thread_number)
1423 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001424
Jens Axboe29d610e2007-02-06 13:25:33 +01001425 ps = sysconf(_SC_PAGESIZE);
1426 if (ps < 0) {
1427 log_err("Failed to get page size\n");
1428 return 1;
1429 }
1430
Jens Axboecfc99db2007-03-14 10:34:47 +01001431 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001432 page_mask = ps - 1;
1433
Jens Axboebb3884d2007-01-17 17:23:11 +11001434 if (write_bw_log) {
1435 setup_log(&agg_io_log[DDIR_READ]);
1436 setup_log(&agg_io_log[DDIR_WRITE]);
1437 }
1438
Jens Axboecdd18ad2008-02-27 18:58:00 +01001439 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001440
Jens Axboea2f77c92007-02-22 11:53:00 +01001441 set_genesis_time();
1442
Jens Axboeccb0fa22008-05-30 23:18:00 +02001443 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001444
1445 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001446
Jens Axboebb3884d2007-01-17 17:23:11 +11001447 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001448 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001449 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001450 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1451 __finish_log(agg_io_log[DDIR_WRITE],
1452 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001453 }
1454 }
Jens Axboeebac4652005-12-08 15:25:21 +01001455
Jens Axboecdd18ad2008-02-27 18:58:00 +01001456 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001457 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001458}