blob: ee752e1266aaf88350eb10eb3ee4d757945b0066 [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 Axboe97601022007-02-18 12:47:29 +0100622 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100623
Jens Axboe413dd452007-02-23 09:26:09 +0100624 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100625
Jens Axboe97601022007-02-18 12:47:29 +0100626 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100627 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100628 terminate_threads(td->groupid);
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200629 td_verror(td, EIO, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100630 break;
631 }
632
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100633 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100634 unsigned long long b;
635
636 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100637 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100638 int left;
639
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100640 if (td->o.thinktime_spin)
641 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100642
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100643 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100644 if (left)
645 usec_sleep(td, left);
646 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100647 }
Jens Axboeebac4652005-12-08 15:25:21 +0100648 }
649
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100650 if (td->o.fill_device && td->error == ENOSPC) {
651 td->error = 0;
652 td->terminate = 1;
653 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100654 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100655 struct fio_file *f;
656
Jens Axboec01c0392007-02-26 12:43:42 +0100657 i = td->cur_depth;
658 if (i)
659 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100660
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100661 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200662 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100663
664 for_each_file(td, f, i) {
665 if (!(f->flags & FIO_FILE_OPEN))
666 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200667 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100668 }
Jens Axboe84585002006-10-19 20:26:22 +0200669 }
Jens Axboec01c0392007-02-26 12:43:42 +0100670 } else
671 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100672
673 /*
674 * stop job if we failed doing any IO
675 */
676 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
677 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100678}
679
Jens Axboeebac4652005-12-08 15:25:21 +0100680static void cleanup_io_u(struct thread_data *td)
681{
Jens Axboe01743ee2008-06-02 12:19:19 +0200682 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100683 struct io_u *io_u;
684
Jens Axboe01743ee2008-06-02 12:19:19 +0200685 flist_for_each_safe(entry, n, &td->io_u_freelist) {
686 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100687
Jens Axboe01743ee2008-06-02 12:19:19 +0200688 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100689 free(io_u);
690 }
691
Jens Axboe2f9ade32006-10-20 11:25:52 +0200692 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100693}
694
695static int init_io_u(struct thread_data *td)
696{
697 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100698 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100699 int i, max_units;
700 char *p;
701
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100702 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100703 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100704 td->orig_buffer_size = (unsigned long long) max_bs
705 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100706
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100707 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
708 unsigned long bs;
709
710 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
711 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
712 }
Jens Axboeebac4652005-12-08 15:25:21 +0100713
Jens Axboec3990642007-07-19 10:17:09 +0200714 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
715 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
716 return 1;
717 }
718
Jens Axboe2f9ade32006-10-20 11:25:52 +0200719 if (allocate_io_mem(td))
720 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100721
Jens Axboeb3f378e2007-07-20 12:39:22 +0200722 if (td->o.odirect)
723 p = ALIGN(td->orig_buffer);
724 else
725 p = td->orig_buffer;
726
Jens Axboeebac4652005-12-08 15:25:21 +0100727 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200728 if (td->terminate)
729 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100730 io_u = malloc(sizeof(*io_u));
731 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200732 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100733
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200734 if (!(td->io_ops->flags & FIO_NOIO)) {
735 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200736
Jens Axboe5973caf2008-05-21 19:52:35 +0200737 if (td_write(td) && !td->o.refill_buffers)
738 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200739 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100740
Jens Axboeb1ff3402006-02-17 11:35:58 +0100741 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100742 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200743 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100744 }
745
Jens Axboe433afcb2007-02-22 10:39:01 +0100746 io_u_init_timeout();
747
Jens Axboeebac4652005-12-08 15:25:21 +0100748 return 0;
749}
750
Jens Axboeda867742006-06-06 10:39:10 -0700751static int switch_ioscheduler(struct thread_data *td)
752{
753 char tmp[256], tmp2[128];
754 FILE *f;
755 int ret;
756
Jens Axboeba0fbe12007-03-09 14:34:23 +0100757 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200758 return 0;
759
Jens Axboeda867742006-06-06 10:39:10 -0700760 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
761
762 f = fopen(tmp, "r+");
763 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200764 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100765 log_err("fio: os or kernel doesn't support IO scheduler"
766 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200767 return 0;
768 }
769 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700770 return 1;
771 }
772
773 /*
774 * Set io scheduler.
775 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100776 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700777 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100778 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700779 fclose(f);
780 return 1;
781 }
782
783 rewind(f);
784
785 /*
786 * Read back and check that the selected scheduler is now the default.
787 */
788 ret = fread(tmp, 1, sizeof(tmp), f);
789 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100790 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700791 fclose(f);
792 return 1;
793 }
794
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100795 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700796 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100797 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100798 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700799 fclose(f);
800 return 1;
801 }
802
803 fclose(f);
804 return 0;
805}
806
Jens Axboe492158c2007-05-24 10:32:47 +0200807static int keep_running(struct thread_data *td)
808{
809 unsigned long long io_done;
810
Jens Axboe20e354e2007-07-23 14:36:16 +0200811 if (td->done)
812 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200813 if (td->o.time_based)
814 return 1;
815 if (td->o.loops) {
816 td->o.loops--;
817 return 1;
818 }
819
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100820 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
821 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200822 if (io_done < td->o.size)
823 return 1;
824
825 return 0;
826}
827
Jens Axboea978ba62007-03-08 14:29:03 +0100828static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100829{
Jens Axboe53cdc682006-10-18 11:50:58 +0200830 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100831 unsigned int i;
832 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100833
Jens Axboe756867b2007-03-06 15:19:24 +0100834 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100835 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100836 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100837 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100838 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100839 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100840
Jens Axboec1324df2007-02-19 19:06:41 +0100841 td->last_was_sync = 0;
842
Jens Axboe2ba1c292008-02-01 13:16:38 +0100843 /*
844 * reset file done count if we are to start over
845 */
846 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200847 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200848
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100849 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100850
851 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200852 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200853 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100854 ret = td_io_open_file(td, f);
855 if (ret)
856 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200857 }
Jens Axboea978ba62007-03-08 14:29:03 +0100858
859 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100860}
861
Jens Axboe906c8d72006-06-13 09:37:56 +0200862/*
863 * Entry point for the thread based jobs. The process based jobs end up
864 * here as well, after a little setup.
865 */
Jens Axboeebac4652005-12-08 15:25:21 +0100866static void *thread_main(void *data)
867{
Shawn Lewis10927312007-11-21 09:38:13 +0100868 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100869 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100870 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100871
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100872 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100873 setsid();
874
875 td->pid = getpid();
876
Jens Axboe5921e802008-05-30 15:02:38 +0200877 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100878
Jens Axboe01743ee2008-06-02 12:19:19 +0200879 INIT_FLIST_HEAD(&td->io_u_freelist);
880 INIT_FLIST_HEAD(&td->io_u_busylist);
881 INIT_FLIST_HEAD(&td->io_u_requeues);
882 INIT_FLIST_HEAD(&td->io_log_list);
883 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200884 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200885
Jens Axboe75154842006-06-01 13:56:09 +0200886 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100887 fio_mutex_up(startup_mutex);
888 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100889
Jens Axboe37f56872007-03-09 12:42:35 +0100890 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100891 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100892 * eating a file descriptor
893 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100894 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100895
Jens Axboed84f8d42007-05-16 08:47:46 +0200896 /*
897 * May alter parameters that init_io_u() will use, so we need to
898 * do this first.
899 */
900 if (init_iolog(td))
901 goto err;
902
Jens Axboedb1cd902007-04-26 13:47:07 +0200903 if (init_io_u(td))
904 goto err;
905
Jens Axboe375b2692007-05-22 09:13:02 +0200906 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200907 td_verror(td, errno, "cpu_set_affinity");
908 goto err;
909 }
910
Jens Axboeac684782007-11-08 08:29:07 +0100911 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200912 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
913 td_verror(td, errno, "ioprio_set");
914 goto err;
915 }
916 }
917
918 if (nice(td->o.nice) == -1) {
919 td_verror(td, errno, "nice");
920 goto err;
921 }
922
923 if (td->o.ioscheduler && switch_ioscheduler(td))
924 goto err;
925
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100926 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100927 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100928
929 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100930 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100931
Jens Axboeb5af8292007-03-08 12:43:13 +0100932 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100933 goto err;
934
Jens Axboe68727072007-03-15 20:49:25 +0100935 if (init_random_map(td))
936 goto err;
937
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100938 if (td->o.exec_prerun) {
939 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100940 goto err;
941 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200942
Jens Axboe69008992006-11-24 13:12:56 +0100943 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100944 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100945 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100946
947 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100948 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200949 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100950 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100951 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100952
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100953 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100954 memcpy(&td->lastrate, &td->ts.stat_sample_time,
955 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100956
Jens Axboea978ba62007-03-08 14:29:03 +0100957 if (clear_state && clear_io_state(td))
958 break;
959
Jens Axboeebac4652005-12-08 15:25:21 +0100960 prune_io_piece_log(td);
961
Jens Axboeba0fbe12007-03-09 14:34:23 +0100962 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100963
Jens Axboea978ba62007-03-08 14:29:03 +0100964 clear_state = 1;
965
Jens Axboe38d77ca2007-03-21 14:20:20 +0100966 if (td_read(td) && td->io_bytes[DDIR_READ]) {
967 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100968 elapsed = utime_since(&td->start,
969 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100970 else
971 elapsed = utime_since_now(&td->start);
972
973 runtime[DDIR_READ] += elapsed;
974 }
975 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
976 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100977 elapsed = utime_since(&td->start,
978 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100979 else
980 elapsed = utime_since_now(&td->start);
981
982 runtime[DDIR_WRITE] += elapsed;
983 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100984
Jens Axboeebac4652005-12-08 15:25:21 +0100985 if (td->error || td->terminate)
986 break;
987
Shawn Lewise84c73a2007-08-02 22:19:32 +0200988 if (!td->o.do_verify ||
989 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200990 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100991 continue;
992
Jens Axboea978ba62007-03-08 14:29:03 +0100993 if (clear_io_state(td))
994 break;
995
Jens Axboe02bcaa82006-11-24 10:42:00 +0100996 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100997
998 do_verify(td);
999
Jens Axboe69008992006-11-24 13:12:56 +01001000 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001001
1002 if (td->error || td->terminate)
1003 break;
1004 }
1005
Jens Axboe36dff962006-11-24 13:29:20 +01001006 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001007 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1008 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001009 td->ts.total_run_time = mtime_since_now(&td->epoch);
1010 td->ts.io_bytes[0] = td->io_bytes[0];
1011 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001012
Jens Axboe756867b2007-03-06 15:19:24 +01001013 if (td->ts.bw_log)
1014 finish_log(td, td->ts.bw_log, "bw");
1015 if (td->ts.slat_log)
1016 finish_log(td, td->ts.slat_log, "slat");
1017 if (td->ts.clat_log)
1018 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001019 if (td->o.exec_postrun) {
1020 if (system(td->o.exec_postrun) < 0)
1021 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001022 }
Jens Axboeebac4652005-12-08 15:25:21 +01001023
1024 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001025 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001026
1027err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001028 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001029 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001030 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001031 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001032 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001033 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001034
1035 /*
1036 * do this very late, it will log file closing as well
1037 */
1038 if (td->o.write_iolog_file)
1039 write_iolog_close(td);
1040
Jens Axboed23bb322007-03-23 15:57:56 +01001041 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001042 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001043 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001044}
1045
Jens Axboe906c8d72006-06-13 09:37:56 +02001046/*
1047 * We cannot pass the td data into a forked process, so attach the td and
1048 * pass it to the thread worker.
1049 */
Jens Axboea6418142007-02-17 04:47:18 +01001050static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001051{
1052 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001053 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001054
1055 data = shmat(shmid, NULL, 0);
1056 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001057 int __err = errno;
1058
Jens Axboeebac4652005-12-08 15:25:21 +01001059 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001060 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001061 }
1062
1063 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001064 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001065 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001066 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001067}
1068
Jens Axboe906c8d72006-06-13 09:37:56 +02001069/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001070 * Run over the job map and reap the threads that have exited, if any.
1071 */
Jens Axboeebac4652005-12-08 15:25:21 +01001072static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1073{
Jens Axboe34572e22006-10-20 09:33:18 +02001074 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001075 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001076
1077 /*
1078 * reap exited threads (TD_EXITED -> TD_REAPED)
1079 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001080 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001081 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001082 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001083
Jens Axboe84585002006-10-19 20:26:22 +02001084 /*
1085 * ->io_ops is NULL for a thread that has closed its
1086 * io engine
1087 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001088 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001089 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001090 else
1091 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001092
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001093 if (!td->pid) {
1094 pending++;
1095 continue;
1096 }
1097 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001098 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001099 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001100 if (td->runstate == TD_EXITED) {
1101 td_set_runstate(td, TD_REAPED);
1102 goto reaped;
1103 }
1104 continue;
1105 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001106
1107 flags = WNOHANG;
1108 if (td->runstate == TD_EXITED)
1109 flags = 0;
1110
1111 /*
1112 * check if someone quit or got killed in an unusual way
1113 */
1114 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001115 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001116 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001117 log_err("fio: pid=%d disappeared %d\n",
1118 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001119 td_set_runstate(td, TD_REAPED);
1120 goto reaped;
1121 }
1122 perror("waitpid");
1123 } else if (ret == td->pid) {
1124 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001125 int sig = WTERMSIG(status);
1126
Jens Axboed9244942007-03-05 12:32:32 +01001127 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001128 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001129 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001130 td_set_runstate(td, TD_REAPED);
1131 goto reaped;
1132 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001133 if (WIFEXITED(status)) {
1134 if (WEXITSTATUS(status) && !td->error)
1135 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001136
Jens Axboea2f77c92007-02-22 11:53:00 +01001137 td_set_runstate(td, TD_REAPED);
1138 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001139 }
1140 }
Jens Axboeebac4652005-12-08 15:25:21 +01001141
Jens Axboea2f77c92007-02-22 11:53:00 +01001142 /*
1143 * thread is not dead, continue
1144 */
gurudas paie48676b2007-04-11 12:44:27 +02001145 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001146 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001147reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001148 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001149 (*m_rate) -= td->o.ratemin;
1150 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001151 if (!td->pid)
1152 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001153
1154 if (td->error)
1155 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001156
1157 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001158 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001159
Jens Axboe1f809d12007-10-25 18:34:02 +02001160 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001161 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001162}
1163
Jens Axboe906c8d72006-06-13 09:37:56 +02001164/*
1165 * Main function for kicking off and reaping jobs, as needed.
1166 */
Jens Axboeebac4652005-12-08 15:25:21 +01001167static void run_threads(void)
1168{
Jens Axboeebac4652005-12-08 15:25:21 +01001169 struct thread_data *td;
1170 unsigned long spent;
1171 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001172
Jens Axboe2f9ade32006-10-20 11:25:52 +02001173 if (fio_pin_memory())
1174 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001175
Jens Axboec6ae0a52006-06-12 11:04:44 +02001176 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001177 printf("Starting ");
1178 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001179 printf("%d thread%s", nr_thread,
1180 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001181 if (nr_process) {
1182 if (nr_thread)
1183 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001184 printf("%d process%s", nr_process,
1185 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001186 }
1187 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001188 fflush(stdout);
1189 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001190
Jens Axboe697a6062008-05-31 00:04:45 +02001191 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001192
Jens Axboeebac4652005-12-08 15:25:21 +01001193 todo = thread_number;
1194 nr_running = 0;
1195 nr_started = 0;
1196 m_rate = t_rate = 0;
1197
Jens Axboe34572e22006-10-20 09:33:18 +02001198 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001199 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001200
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001201 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001202 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001203 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001204 }
Jens Axboeebac4652005-12-08 15:25:21 +01001205
1206 /*
1207 * do file setup here so it happens sequentially,
1208 * we don't want X number of threads getting their
1209 * client data interspersed on disk
1210 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001211 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001212 exit_value++;
1213 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001214 log_err("fio: pid=%d, err=%d/%s\n",
1215 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001216 td_set_runstate(td, TD_REAPED);
1217 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001218 } else {
1219 struct fio_file *f;
1220 unsigned int i;
1221
1222 /*
1223 * for sharing to work, each job must always open
1224 * its own files. so close them, if we opened them
1225 * for creation
1226 */
1227 for_each_file(td, f, i)
1228 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001229 }
Jens Axboe380cf262007-01-11 14:01:27 +01001230
1231 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001232 }
1233
Jens Axboea2f77c92007-02-22 11:53:00 +01001234 set_genesis_time();
1235
Jens Axboeebac4652005-12-08 15:25:21 +01001236 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001237 struct thread_data *map[MAX_JOBS];
1238 struct timeval this_start;
1239 int this_jobs = 0, left;
1240
Jens Axboeebac4652005-12-08 15:25:21 +01001241 /*
1242 * create threads (TD_NOT_CREATED -> TD_CREATED)
1243 */
Jens Axboe34572e22006-10-20 09:33:18 +02001244 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001245 if (td->runstate != TD_NOT_CREATED)
1246 continue;
1247
1248 /*
1249 * never got a chance to start, killed by other
1250 * thread for some reason
1251 */
1252 if (td->terminate) {
1253 todo--;
1254 continue;
1255 }
1256
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001257 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001258 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001259
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001260 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001261 continue;
1262 }
1263
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001264 if (td->o.stonewall && (nr_started || nr_running)) {
1265 dprint(FD_PROCESS, "%s: stonewall wait\n",
1266 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001267 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001268 }
Jens Axboeebac4652005-12-08 15:25:21 +01001269
Jens Axboe75154842006-06-01 13:56:09 +02001270 /*
1271 * Set state to created. Thread will transition
1272 * to TD_INITIALIZED when it's done setting up.
1273 */
Jens Axboeebac4652005-12-08 15:25:21 +01001274 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001275 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001276 nr_started++;
1277
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001278 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001279 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001280 if (pthread_create(&td->thread, NULL,
1281 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001282 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001283 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001284 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001285 }
Jens Axboef4705a72008-03-10 10:52:22 +01001286 if (pthread_detach(td->thread) < 0)
1287 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001288 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001289 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001290 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001291 pid = fork();
1292 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001293 int ret = fork_main(shm_id, i);
1294
Jens Axboe5e1d3062008-05-23 11:55:53 +02001295 _exit(ret);
1296 } else if (i == fio_debug_jobno)
1297 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001298 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001299 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001300 }
1301
1302 /*
Jens Axboe75154842006-06-01 13:56:09 +02001303 * Wait for the started threads to transition to
1304 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001305 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001306 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001307 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001308 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001309 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1310 break;
1311
1312 usleep(100000);
1313
1314 for (i = 0; i < this_jobs; i++) {
1315 td = map[i];
1316 if (!td)
1317 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001318 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001319 map[i] = NULL;
1320 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001321 } else if (td->runstate >= TD_EXITED) {
1322 map[i] = NULL;
1323 left--;
1324 todo--;
1325 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001326 }
1327 }
1328 }
1329
1330 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001331 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001332 for (i = 0; i < this_jobs; i++) {
1333 td = map[i];
1334 if (!td)
1335 continue;
1336 kill(td->pid, SIGTERM);
1337 }
1338 break;
1339 }
1340
1341 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001342 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001343 */
Jens Axboe34572e22006-10-20 09:33:18 +02001344 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001345 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001346 continue;
1347
1348 td_set_runstate(td, TD_RUNNING);
1349 nr_running++;
1350 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001351 m_rate += td->o.ratemin;
1352 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001353 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001354 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001355 }
1356
1357 reap_threads(&nr_running, &t_rate, &m_rate);
1358
1359 if (todo)
1360 usleep(100000);
1361 }
1362
1363 while (nr_running) {
1364 reap_threads(&nr_running, &t_rate, &m_rate);
1365 usleep(10000);
1366 }
1367
1368 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001369 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001370}
1371
Jens Axboeebac4652005-12-08 15:25:21 +01001372int main(int argc, char *argv[])
1373{
Jens Axboe29d610e2007-02-06 13:25:33 +01001374 long ps;
1375
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001376 sinit();
1377
Jens Axboedbe11252007-02-11 00:19:51 +01001378 /*
1379 * We need locale for number printing, if it isn't set then just
1380 * go with the US format.
1381 */
1382 if (!getenv("LC_NUMERIC"))
1383 setlocale(LC_NUMERIC, "en_US");
1384
Jens Axboeebac4652005-12-08 15:25:21 +01001385 if (parse_options(argc, argv))
1386 return 1;
1387
Jens Axboe4b472fa2007-04-04 11:04:34 +02001388 if (!thread_number)
1389 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001390
Jens Axboe29d610e2007-02-06 13:25:33 +01001391 ps = sysconf(_SC_PAGESIZE);
1392 if (ps < 0) {
1393 log_err("Failed to get page size\n");
1394 return 1;
1395 }
1396
Jens Axboecfc99db2007-03-14 10:34:47 +01001397 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001398 page_mask = ps - 1;
1399
Jens Axboebb3884d2007-01-17 17:23:11 +11001400 if (write_bw_log) {
1401 setup_log(&agg_io_log[DDIR_READ]);
1402 setup_log(&agg_io_log[DDIR_WRITE]);
1403 }
1404
Jens Axboecdd18ad2008-02-27 18:58:00 +01001405 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001406
Jens Axboea2f77c92007-02-22 11:53:00 +01001407 set_genesis_time();
1408
Jens Axboeccb0fa22008-05-30 23:18:00 +02001409 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001410
1411 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001412
Jens Axboebb3884d2007-01-17 17:23:11 +11001413 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001414 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001415 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001416 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1417 __finish_log(agg_io_log[DDIR_WRITE],
1418 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001419 }
1420 }
Jens Axboeebac4652005-12-08 15:25:21 +01001421
Jens Axboecdd18ad2008-02-27 18:58:00 +01001422 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001423 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001424}