blob: 2a09fbe2d71b68fe40eaa0f7fb4f77056219d81f [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 Axboe6ce15a32007-01-12 09:04:41 +010064static inline void td_set_runstate(struct thread_data *td, int runstate)
65{
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 Axboe5853e5a2006-06-08 14:41:05 +0200498 td_set_runstate(td, TD_RUNNING);
499
Jens Axboe7bb48f82007-03-27 15:30:28 +0200500 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100501 struct timeval comp_time;
502 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200503 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100504 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200505 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100506
507 if (td->terminate)
508 break;
509
Jens Axboe3d7c3912007-02-19 13:16:12 +0100510 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100511 if (!io_u)
512 break;
513
514 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100515
516 if (runtime_exceeded(td, &s)) {
517 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200518 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100519 break;
520 }
Jens Axboe36167d82007-02-18 05:41:31 +0100521
Jens Axboeb67740d2007-07-26 12:22:30 +0200522 /*
523 * Add verification end_io handler, if asked to verify
524 * a previously written file.
525 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100526 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200527 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100528 td_set_runstate(td, TD_VERIFYING);
529 } else
530 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200531
Jens Axboe11786802007-03-05 18:33:22 +0100532 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100533 switch (ret) {
534 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100535 if (io_u->error)
536 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100537 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100538 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200539 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100540
Jens Axboe9e9d1642007-03-12 09:37:46 +0100541 /*
542 * zero read, fail
543 */
544 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200545 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100546 put_io_u(td, io_u);
547 break;
548 }
549
Jens Axboe36167d82007-02-18 05:41:31 +0100550 io_u->xfer_buflen = io_u->resid;
551 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200552 io_u->offset += bytes;
553
Jens Axboe30061b92007-04-17 13:31:34 +0200554 td->ts.short_io_u[io_u->ddir]++;
555
Jens Axboed460eb32007-04-16 21:39:33 +0200556 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200557 goto sync_done;
558
Jens Axboe11786802007-03-05 18:33:22 +0100559 requeue_io_u(td, &io_u);
560 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200561sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100562 fio_gettime(&comp_time, NULL);
563 bytes_done = io_u_sync_complete(td, io_u);
564 if (bytes_done < 0)
565 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100566 }
Jens Axboe36167d82007-02-18 05:41:31 +0100567 break;
568 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100569 /*
570 * if the engine doesn't have a commit hook,
571 * the io_u is really queued. if it does have such
572 * a hook, it has to call io_u_queued() itself.
573 */
574 if (td->io_ops->commit == NULL)
575 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100576 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100577 case FIO_Q_BUSY:
578 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100579 ret2 = td_io_commit(td);
580 if (ret2 < 0)
581 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100582 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100583 default:
584 assert(ret < 0);
585 put_io_u(td, io_u);
586 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100587 }
588
Jens Axboe99784632007-02-19 10:06:17 +0100589 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100590 break;
591
Jens Axboe97601022007-02-18 12:47:29 +0100592 /*
593 * See if we need to complete some commands
594 */
Jens Axboe49504212008-06-05 09:03:30 +0200595 full = queue_full(td) || ret == FIO_Q_BUSY;
596 if (full || !td->o.iodepth_batch_complete) {
597 min_evts = td->o.iodepth_batch_complete;
598 if (full && !min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100599 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200600
Jens Axboe97601022007-02-18 12:47:29 +0100601 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200602
603 do {
604 ret = io_u_queued_complete(td, min_evts);
605 if (ret <= 0)
606 break;
607
608 bytes_done += ret;
609 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100610 }
611
Jens Axboe49504212008-06-05 09:03:30 +0200612 if (ret < 0)
613 break;
Jens Axboe97601022007-02-18 12:47:29 +0100614 if (!bytes_done)
615 continue;
616
Jens Axboeebac4652005-12-08 15:25:21 +0100617 /*
618 * the rate is batched for now, it should work for batches
619 * of completions except the very first one which may look
620 * a little bursty
621 */
Jens Axboe721938a2008-09-10 09:46:16 +0200622 if (ramp_time_over(td)) {
623 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100624
Jens Axboe721938a2008-09-10 09:46:16 +0200625 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100626
Jens Axboe721938a2008-09-10 09:46:16 +0200627 if (check_min_rate(td, &comp_time)) {
628 if (exitall_on_terminate)
629 terminate_threads(td->groupid);
630 td_verror(td, EIO, "check_min_rate");
631 break;
632 }
Jens Axboeebac4652005-12-08 15:25:21 +0100633 }
634
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100635 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100636 unsigned long long b;
637
638 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100639 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100640 int left;
641
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100642 if (td->o.thinktime_spin)
643 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100644
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100645 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100646 if (left)
647 usec_sleep(td, left);
648 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100649 }
Jens Axboeebac4652005-12-08 15:25:21 +0100650 }
651
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100652 if (td->o.fill_device && td->error == ENOSPC) {
653 td->error = 0;
654 td->terminate = 1;
655 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100656 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100657 struct fio_file *f;
658
Jens Axboec01c0392007-02-26 12:43:42 +0100659 i = td->cur_depth;
660 if (i)
661 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100662
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100663 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200664 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100665
666 for_each_file(td, f, i) {
667 if (!(f->flags & FIO_FILE_OPEN))
668 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200669 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100670 }
Jens Axboe84585002006-10-19 20:26:22 +0200671 }
Jens Axboec01c0392007-02-26 12:43:42 +0100672 } else
673 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100674
675 /*
676 * stop job if we failed doing any IO
677 */
678 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
679 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100680}
681
Jens Axboeebac4652005-12-08 15:25:21 +0100682static void cleanup_io_u(struct thread_data *td)
683{
Jens Axboe01743ee2008-06-02 12:19:19 +0200684 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100685 struct io_u *io_u;
686
Jens Axboe01743ee2008-06-02 12:19:19 +0200687 flist_for_each_safe(entry, n, &td->io_u_freelist) {
688 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100689
Jens Axboe01743ee2008-06-02 12:19:19 +0200690 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100691 free(io_u);
692 }
693
Jens Axboe2f9ade32006-10-20 11:25:52 +0200694 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100695}
696
697static int init_io_u(struct thread_data *td)
698{
699 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100700 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100701 int i, max_units;
702 char *p;
703
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100704 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100705 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100706 td->orig_buffer_size = (unsigned long long) max_bs
707 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100708
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100709 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
710 unsigned long bs;
711
712 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
713 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
714 }
Jens Axboeebac4652005-12-08 15:25:21 +0100715
Jens Axboec3990642007-07-19 10:17:09 +0200716 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
717 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
718 return 1;
719 }
720
Jens Axboe2f9ade32006-10-20 11:25:52 +0200721 if (allocate_io_mem(td))
722 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100723
Jens Axboeb3f378e2007-07-20 12:39:22 +0200724 if (td->o.odirect)
725 p = ALIGN(td->orig_buffer);
726 else
727 p = td->orig_buffer;
728
Jens Axboeebac4652005-12-08 15:25:21 +0100729 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200730 if (td->terminate)
731 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100732 io_u = malloc(sizeof(*io_u));
733 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200734 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100735
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200736 if (!(td->io_ops->flags & FIO_NOIO)) {
737 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200738
Jens Axboe5973caf2008-05-21 19:52:35 +0200739 if (td_write(td) && !td->o.refill_buffers)
740 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200741 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100742
Jens Axboeb1ff3402006-02-17 11:35:58 +0100743 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100744 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200745 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100746 }
747
Jens Axboe433afcb2007-02-22 10:39:01 +0100748 io_u_init_timeout();
749
Jens Axboeebac4652005-12-08 15:25:21 +0100750 return 0;
751}
752
Jens Axboeda867742006-06-06 10:39:10 -0700753static int switch_ioscheduler(struct thread_data *td)
754{
755 char tmp[256], tmp2[128];
756 FILE *f;
757 int ret;
758
Jens Axboeba0fbe12007-03-09 14:34:23 +0100759 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200760 return 0;
761
Jens Axboeda867742006-06-06 10:39:10 -0700762 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
763
764 f = fopen(tmp, "r+");
765 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200766 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100767 log_err("fio: os or kernel doesn't support IO scheduler"
768 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200769 return 0;
770 }
771 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700772 return 1;
773 }
774
775 /*
776 * Set io scheduler.
777 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100778 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700779 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100780 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700781 fclose(f);
782 return 1;
783 }
784
785 rewind(f);
786
787 /*
788 * Read back and check that the selected scheduler is now the default.
789 */
790 ret = fread(tmp, 1, sizeof(tmp), f);
791 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100792 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700793 fclose(f);
794 return 1;
795 }
796
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100797 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700798 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100799 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100800 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700801 fclose(f);
802 return 1;
803 }
804
805 fclose(f);
806 return 0;
807}
808
Jens Axboe492158c2007-05-24 10:32:47 +0200809static int keep_running(struct thread_data *td)
810{
811 unsigned long long io_done;
812
Jens Axboe20e354e2007-07-23 14:36:16 +0200813 if (td->done)
814 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200815 if (td->o.time_based)
816 return 1;
817 if (td->o.loops) {
818 td->o.loops--;
819 return 1;
820 }
821
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100822 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
823 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200824 if (io_done < td->o.size)
825 return 1;
826
827 return 0;
828}
829
Jens Axboea978ba62007-03-08 14:29:03 +0100830static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100831{
Jens Axboe53cdc682006-10-18 11:50:58 +0200832 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100833 unsigned int i;
834 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100835
Jens Axboe756867b2007-03-06 15:19:24 +0100836 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100837 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100838 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100839 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100840 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100841 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100842
Jens Axboec1324df2007-02-19 19:06:41 +0100843 td->last_was_sync = 0;
844
Jens Axboe2ba1c292008-02-01 13:16:38 +0100845 /*
846 * reset file done count if we are to start over
847 */
848 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200849 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200850
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100851 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100852
853 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200854 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200855 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100856 ret = td_io_open_file(td, f);
857 if (ret)
858 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200859 }
Jens Axboea978ba62007-03-08 14:29:03 +0100860
861 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100862}
863
Jens Axboe906c8d72006-06-13 09:37:56 +0200864/*
865 * Entry point for the thread based jobs. The process based jobs end up
866 * here as well, after a little setup.
867 */
Jens Axboeebac4652005-12-08 15:25:21 +0100868static void *thread_main(void *data)
869{
Shawn Lewis10927312007-11-21 09:38:13 +0100870 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100871 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100872 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100873
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100874 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100875 setsid();
876
877 td->pid = getpid();
878
Jens Axboe5921e802008-05-30 15:02:38 +0200879 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100880
Jens Axboe01743ee2008-06-02 12:19:19 +0200881 INIT_FLIST_HEAD(&td->io_u_freelist);
882 INIT_FLIST_HEAD(&td->io_u_busylist);
883 INIT_FLIST_HEAD(&td->io_u_requeues);
884 INIT_FLIST_HEAD(&td->io_log_list);
885 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200886 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200887
Jens Axboe75154842006-06-01 13:56:09 +0200888 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100889 fio_mutex_up(startup_mutex);
890 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100891
Jens Axboe37f56872007-03-09 12:42:35 +0100892 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100893 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100894 * eating a file descriptor
895 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100896 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100897
Jens Axboed84f8d42007-05-16 08:47:46 +0200898 /*
899 * May alter parameters that init_io_u() will use, so we need to
900 * do this first.
901 */
902 if (init_iolog(td))
903 goto err;
904
Jens Axboedb1cd902007-04-26 13:47:07 +0200905 if (init_io_u(td))
906 goto err;
907
Jens Axboe375b2692007-05-22 09:13:02 +0200908 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200909 td_verror(td, errno, "cpu_set_affinity");
910 goto err;
911 }
912
Jens Axboeac684782007-11-08 08:29:07 +0100913 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200914 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
915 td_verror(td, errno, "ioprio_set");
916 goto err;
917 }
918 }
919
920 if (nice(td->o.nice) == -1) {
921 td_verror(td, errno, "nice");
922 goto err;
923 }
924
925 if (td->o.ioscheduler && switch_ioscheduler(td))
926 goto err;
927
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100928 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100929 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100930
931 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100932 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100933
Jens Axboeb5af8292007-03-08 12:43:13 +0100934 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100935 goto err;
936
Jens Axboe68727072007-03-15 20:49:25 +0100937 if (init_random_map(td))
938 goto err;
939
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100940 if (td->o.exec_prerun) {
941 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100942 goto err;
943 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200944
Jens Axboe69008992006-11-24 13:12:56 +0100945 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100946 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100947 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100948
949 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100950 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200951 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100952 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100953 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100954
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100955 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100956 memcpy(&td->lastrate, &td->ts.stat_sample_time,
957 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100958
Jens Axboea978ba62007-03-08 14:29:03 +0100959 if (clear_state && clear_io_state(td))
960 break;
961
Jens Axboeebac4652005-12-08 15:25:21 +0100962 prune_io_piece_log(td);
963
Jens Axboeba0fbe12007-03-09 14:34:23 +0100964 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100965
Jens Axboea978ba62007-03-08 14:29:03 +0100966 clear_state = 1;
967
Jens Axboe38d77ca2007-03-21 14:20:20 +0100968 if (td_read(td) && td->io_bytes[DDIR_READ]) {
969 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100970 elapsed = utime_since(&td->start,
971 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100972 else
973 elapsed = utime_since_now(&td->start);
974
975 runtime[DDIR_READ] += elapsed;
976 }
977 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
978 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100979 elapsed = utime_since(&td->start,
980 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100981 else
982 elapsed = utime_since_now(&td->start);
983
984 runtime[DDIR_WRITE] += elapsed;
985 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100986
Jens Axboeebac4652005-12-08 15:25:21 +0100987 if (td->error || td->terminate)
988 break;
989
Shawn Lewise84c73a2007-08-02 22:19:32 +0200990 if (!td->o.do_verify ||
991 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200992 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100993 continue;
994
Jens Axboea978ba62007-03-08 14:29:03 +0100995 if (clear_io_state(td))
996 break;
997
Jens Axboe02bcaa82006-11-24 10:42:00 +0100998 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100999
1000 do_verify(td);
1001
Jens Axboe69008992006-11-24 13:12:56 +01001002 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001003
1004 if (td->error || td->terminate)
1005 break;
1006 }
1007
Jens Axboe36dff962006-11-24 13:29:20 +01001008 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001009 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1010 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001011 td->ts.total_run_time = mtime_since_now(&td->epoch);
1012 td->ts.io_bytes[0] = td->io_bytes[0];
1013 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001014
Jens Axboe756867b2007-03-06 15:19:24 +01001015 if (td->ts.bw_log)
1016 finish_log(td, td->ts.bw_log, "bw");
1017 if (td->ts.slat_log)
1018 finish_log(td, td->ts.slat_log, "slat");
1019 if (td->ts.clat_log)
1020 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001021 if (td->o.exec_postrun) {
1022 if (system(td->o.exec_postrun) < 0)
1023 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001024 }
Jens Axboeebac4652005-12-08 15:25:21 +01001025
1026 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001027 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001028
1029err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001030 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001031 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001032 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001033 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001034 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001035 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001036
1037 /*
1038 * do this very late, it will log file closing as well
1039 */
1040 if (td->o.write_iolog_file)
1041 write_iolog_close(td);
1042
Jens Axboed23bb322007-03-23 15:57:56 +01001043 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001044 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001045 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001046}
1047
Jens Axboe906c8d72006-06-13 09:37:56 +02001048/*
1049 * We cannot pass the td data into a forked process, so attach the td and
1050 * pass it to the thread worker.
1051 */
Jens Axboea6418142007-02-17 04:47:18 +01001052static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001053{
1054 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001055 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001056
1057 data = shmat(shmid, NULL, 0);
1058 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001059 int __err = errno;
1060
Jens Axboeebac4652005-12-08 15:25:21 +01001061 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001062 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001063 }
1064
1065 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001066 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001067 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001068 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001069}
1070
Jens Axboe906c8d72006-06-13 09:37:56 +02001071/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001072 * Run over the job map and reap the threads that have exited, if any.
1073 */
Jens Axboeebac4652005-12-08 15:25:21 +01001074static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1075{
Jens Axboe34572e22006-10-20 09:33:18 +02001076 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001077 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001078
1079 /*
1080 * reap exited threads (TD_EXITED -> TD_REAPED)
1081 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001082 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001083 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001084 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001085
Jens Axboe84585002006-10-19 20:26:22 +02001086 /*
1087 * ->io_ops is NULL for a thread that has closed its
1088 * io engine
1089 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001090 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001091 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001092 else
1093 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001094
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001095 if (!td->pid) {
1096 pending++;
1097 continue;
1098 }
1099 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001100 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001101 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001102 if (td->runstate == TD_EXITED) {
1103 td_set_runstate(td, TD_REAPED);
1104 goto reaped;
1105 }
1106 continue;
1107 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001108
1109 flags = WNOHANG;
1110 if (td->runstate == TD_EXITED)
1111 flags = 0;
1112
1113 /*
1114 * check if someone quit or got killed in an unusual way
1115 */
1116 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001117 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001118 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001119 log_err("fio: pid=%d disappeared %d\n",
1120 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001121 td_set_runstate(td, TD_REAPED);
1122 goto reaped;
1123 }
1124 perror("waitpid");
1125 } else if (ret == td->pid) {
1126 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001127 int sig = WTERMSIG(status);
1128
Jens Axboed9244942007-03-05 12:32:32 +01001129 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001130 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001131 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001132 td_set_runstate(td, TD_REAPED);
1133 goto reaped;
1134 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001135 if (WIFEXITED(status)) {
1136 if (WEXITSTATUS(status) && !td->error)
1137 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001138
Jens Axboea2f77c92007-02-22 11:53:00 +01001139 td_set_runstate(td, TD_REAPED);
1140 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001141 }
1142 }
Jens Axboeebac4652005-12-08 15:25:21 +01001143
Jens Axboea2f77c92007-02-22 11:53:00 +01001144 /*
1145 * thread is not dead, continue
1146 */
gurudas paie48676b2007-04-11 12:44:27 +02001147 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001148 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001149reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001150 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001151 (*m_rate) -= td->o.ratemin;
1152 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001153 if (!td->pid)
1154 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001155
1156 if (td->error)
1157 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001158
1159 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001160 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001161
Jens Axboe1f809d12007-10-25 18:34:02 +02001162 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001163 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001164}
1165
Jens Axboe906c8d72006-06-13 09:37:56 +02001166/*
1167 * Main function for kicking off and reaping jobs, as needed.
1168 */
Jens Axboeebac4652005-12-08 15:25:21 +01001169static void run_threads(void)
1170{
Jens Axboeebac4652005-12-08 15:25:21 +01001171 struct thread_data *td;
1172 unsigned long spent;
1173 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001174
Jens Axboe2f9ade32006-10-20 11:25:52 +02001175 if (fio_pin_memory())
1176 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001177
Jens Axboec6ae0a52006-06-12 11:04:44 +02001178 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001179 printf("Starting ");
1180 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001181 printf("%d thread%s", nr_thread,
1182 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001183 if (nr_process) {
1184 if (nr_thread)
1185 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001186 printf("%d process%s", nr_process,
1187 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001188 }
1189 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001190 fflush(stdout);
1191 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001192
Jens Axboe697a6062008-05-31 00:04:45 +02001193 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001194
Jens Axboeebac4652005-12-08 15:25:21 +01001195 todo = thread_number;
1196 nr_running = 0;
1197 nr_started = 0;
1198 m_rate = t_rate = 0;
1199
Jens Axboe34572e22006-10-20 09:33:18 +02001200 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001201 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001202
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001203 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001204 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001205 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001206 }
Jens Axboeebac4652005-12-08 15:25:21 +01001207
1208 /*
1209 * do file setup here so it happens sequentially,
1210 * we don't want X number of threads getting their
1211 * client data interspersed on disk
1212 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001213 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001214 exit_value++;
1215 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001216 log_err("fio: pid=%d, err=%d/%s\n",
1217 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001218 td_set_runstate(td, TD_REAPED);
1219 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001220 } else {
1221 struct fio_file *f;
1222 unsigned int i;
1223
1224 /*
1225 * for sharing to work, each job must always open
1226 * its own files. so close them, if we opened them
1227 * for creation
1228 */
1229 for_each_file(td, f, i)
1230 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001231 }
Jens Axboe380cf262007-01-11 14:01:27 +01001232
1233 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001234 }
1235
Jens Axboea2f77c92007-02-22 11:53:00 +01001236 set_genesis_time();
1237
Jens Axboeebac4652005-12-08 15:25:21 +01001238 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001239 struct thread_data *map[MAX_JOBS];
1240 struct timeval this_start;
1241 int this_jobs = 0, left;
1242
Jens Axboeebac4652005-12-08 15:25:21 +01001243 /*
1244 * create threads (TD_NOT_CREATED -> TD_CREATED)
1245 */
Jens Axboe34572e22006-10-20 09:33:18 +02001246 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001247 if (td->runstate != TD_NOT_CREATED)
1248 continue;
1249
1250 /*
1251 * never got a chance to start, killed by other
1252 * thread for some reason
1253 */
1254 if (td->terminate) {
1255 todo--;
1256 continue;
1257 }
1258
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001259 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001260 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001261
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001262 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001263 continue;
1264 }
1265
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001266 if (td->o.stonewall && (nr_started || nr_running)) {
1267 dprint(FD_PROCESS, "%s: stonewall wait\n",
1268 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001269 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001270 }
Jens Axboeebac4652005-12-08 15:25:21 +01001271
Jens Axboe75154842006-06-01 13:56:09 +02001272 /*
1273 * Set state to created. Thread will transition
1274 * to TD_INITIALIZED when it's done setting up.
1275 */
Jens Axboeebac4652005-12-08 15:25:21 +01001276 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001277 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001278 nr_started++;
1279
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001280 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001281 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001282 if (pthread_create(&td->thread, NULL,
1283 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001284 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001285 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001286 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001287 }
Jens Axboef4705a72008-03-10 10:52:22 +01001288 if (pthread_detach(td->thread) < 0)
1289 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001290 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001291 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001292 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001293 pid = fork();
1294 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001295 int ret = fork_main(shm_id, i);
1296
Jens Axboe5e1d3062008-05-23 11:55:53 +02001297 _exit(ret);
1298 } else if (i == fio_debug_jobno)
1299 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001300 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001301 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001302 }
1303
1304 /*
Jens Axboe75154842006-06-01 13:56:09 +02001305 * Wait for the started threads to transition to
1306 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001307 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001308 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001309 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001310 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001311 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1312 break;
1313
1314 usleep(100000);
1315
1316 for (i = 0; i < this_jobs; i++) {
1317 td = map[i];
1318 if (!td)
1319 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001320 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001321 map[i] = NULL;
1322 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001323 } else if (td->runstate >= TD_EXITED) {
1324 map[i] = NULL;
1325 left--;
1326 todo--;
1327 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001328 }
1329 }
1330 }
1331
1332 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001333 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001334 for (i = 0; i < this_jobs; i++) {
1335 td = map[i];
1336 if (!td)
1337 continue;
1338 kill(td->pid, SIGTERM);
1339 }
1340 break;
1341 }
1342
1343 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001344 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001345 */
Jens Axboe34572e22006-10-20 09:33:18 +02001346 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001347 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001348 continue;
1349
1350 td_set_runstate(td, TD_RUNNING);
1351 nr_running++;
1352 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001353 m_rate += td->o.ratemin;
1354 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001355 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001356 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001357 }
1358
1359 reap_threads(&nr_running, &t_rate, &m_rate);
1360
1361 if (todo)
1362 usleep(100000);
1363 }
1364
1365 while (nr_running) {
1366 reap_threads(&nr_running, &t_rate, &m_rate);
1367 usleep(10000);
1368 }
1369
1370 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001371 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001372}
1373
Jens Axboeebac4652005-12-08 15:25:21 +01001374int main(int argc, char *argv[])
1375{
Jens Axboe29d610e2007-02-06 13:25:33 +01001376 long ps;
1377
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001378 sinit();
1379
Jens Axboedbe11252007-02-11 00:19:51 +01001380 /*
1381 * We need locale for number printing, if it isn't set then just
1382 * go with the US format.
1383 */
1384 if (!getenv("LC_NUMERIC"))
1385 setlocale(LC_NUMERIC, "en_US");
1386
Jens Axboeebac4652005-12-08 15:25:21 +01001387 if (parse_options(argc, argv))
1388 return 1;
1389
Jens Axboe4b472fa2007-04-04 11:04:34 +02001390 if (!thread_number)
1391 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001392
Jens Axboe29d610e2007-02-06 13:25:33 +01001393 ps = sysconf(_SC_PAGESIZE);
1394 if (ps < 0) {
1395 log_err("Failed to get page size\n");
1396 return 1;
1397 }
1398
Jens Axboecfc99db2007-03-14 10:34:47 +01001399 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001400 page_mask = ps - 1;
1401
Jens Axboebb3884d2007-01-17 17:23:11 +11001402 if (write_bw_log) {
1403 setup_log(&agg_io_log[DDIR_READ]);
1404 setup_log(&agg_io_log[DDIR_WRITE]);
1405 }
1406
Jens Axboecdd18ad2008-02-27 18:58:00 +01001407 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001408
Jens Axboea2f77c92007-02-22 11:53:00 +01001409 set_genesis_time();
1410
Jens Axboeccb0fa22008-05-30 23:18:00 +02001411 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001412
1413 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001414
Jens Axboebb3884d2007-01-17 17:23:11 +11001415 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001416 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001417 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001418 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1419 __finish_log(agg_io_log[DDIR_WRITE],
1420 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001421 }
1422 }
Jens Axboeebac4652005-12-08 15:25:21 +01001423
Jens Axboecdd18ad2008-02-27 18:58:00 +01001424 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001425 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001426}