blob: 8c1502ec80319775240d3300b2b565eba718a562 [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 Axboe4f5af7b2009-06-03 08:45:40 +020040#include "verify.h"
Jens Axboeebac4652005-12-08 15:25:21 +010041
Jens Axboecfc99db2007-03-14 10:34:47 +010042unsigned long page_mask;
43unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010044#define ALIGN(buf) \
45 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010046
47int groupid = 0;
48int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010049int nr_process = 0;
50int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010051int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020052int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020053unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010054
Jens Axboecdd18ad2008-02-27 18:58:00 +010055static struct fio_mutex *startup_mutex;
Fabio Checconid2bbb8d2009-05-18 12:54:50 +020056static struct fio_mutex *writeout_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010057static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010058static int exit_value;
Jens Axboeccb0fa22008-05-30 23:18:00 +020059static struct itimerval itimer;
Jens Axboebe4ecfd2008-12-08 14:10:52 +010060static pthread_t gtod_thread;
Jens Axboeebac4652005-12-08 15:25:21 +010061
Jens Axboebb3884d2007-01-17 17:23:11 +110062struct io_log *agg_io_log[2];
63
Jens Axboeebac4652005-12-08 15:25:21 +010064#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020065#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010066
Jens Axboeb29ee5b2008-09-11 10:17:26 +020067void td_set_runstate(struct thread_data *td, int runstate)
Jens Axboe6ce15a32007-01-12 09:04:41 +010068{
Jens Axboed8fab1b2008-03-06 10:25:17 +010069 if (td->runstate == runstate)
70 return;
71
Jens Axboe5921e802008-05-30 15:02:38 +020072 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
73 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010074 td->runstate = runstate;
75}
76
Jens Axboe390c40e2007-03-05 12:35:29 +010077static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010078{
Jens Axboe34572e22006-10-20 09:33:18 +020079 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010080 int i;
81
Jens Axboe6f88bcb2008-03-19 10:29:07 +010082 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
83
Jens Axboe34572e22006-10-20 09:33:18 +020084 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010085 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010086 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +020087 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010088 td->terminate = 1;
89 td->o.start_delay = 0;
90
Jens Axboe7a93ab12007-04-18 12:25:41 +020091 /*
92 * if the thread is running, just let it exit
93 */
94 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010095 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010096 else {
97 struct ioengine_ops *ops = td->io_ops;
98
99 if (ops && (ops->flags & FIO_SIGQUIT))
100 kill(td->pid, SIGQUIT);
101 }
Jens Axboeebac4652005-12-08 15:25:21 +0100102 }
103 }
104}
105
Jens Axboeccb0fa22008-05-30 23:18:00 +0200106static void status_timer_arm(void)
107{
108 itimer.it_value.tv_sec = 0;
109 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
110 setitimer(ITIMER_REAL, &itimer, NULL);
111}
112
Jens Axboe02444ad2008-10-07 11:33:00 +0200113static void sig_alrm(int fio_unused sig)
Jens Axboeccb0fa22008-05-30 23:18:00 +0200114{
Jens Axboe697a6062008-05-31 00:04:45 +0200115 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100116 update_io_ticks();
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100117 print_thread_status();
Jens Axboeccb0fa22008-05-30 23:18:00 +0200118 status_timer_arm();
Jens Axboe697a6062008-05-31 00:04:45 +0200119 }
120}
121
Jens Axboe5f8f9722008-11-18 19:10:21 +0100122/*
123 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
124 */
125static void sig_quit(int sig)
126{
127}
128
Jens Axboe697a6062008-05-31 00:04:45 +0200129static void sig_int(int sig)
130{
131 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100132 printf("\nfio: terminating on signal %d\n", sig);
133 fflush(stdout);
134 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100135 }
136}
137
Jens Axboe02444ad2008-10-07 11:33:00 +0200138static void sig_ill(int fio_unused sig)
Jens Axboede79c912008-08-04 21:43:55 +0200139{
140 if (!threads)
141 return;
142
143 log_err("fio: illegal instruction. your cpu does not support "
144 "the sse4.2 instruction for crc32c\n");
145 terminate_threads(TERMINATE_ALL);
146 exit(4);
147}
148
Jens Axboe697a6062008-05-31 00:04:45 +0200149static void set_sig_handlers(void)
150{
151 struct sigaction act;
152
153 memset(&act, 0, sizeof(act));
154 act.sa_handler = sig_alrm;
155 act.sa_flags = SA_RESTART;
156 sigaction(SIGALRM, &act, NULL);
157
158 memset(&act, 0, sizeof(act));
159 act.sa_handler = sig_int;
160 act.sa_flags = SA_RESTART;
161 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200162
163 memset(&act, 0, sizeof(act));
164 act.sa_handler = sig_ill;
165 act.sa_flags = SA_RESTART;
166 sigaction(SIGILL, &act, NULL);
Jens Axboe5f8f9722008-11-18 19:10:21 +0100167
168 memset(&act, 0, sizeof(act));
169 act.sa_handler = sig_quit;
170 act.sa_flags = SA_RESTART;
171 sigaction(SIGQUIT, &act, NULL);
Jens Axboe697a6062008-05-31 00:04:45 +0200172}
173
Jens Axboe993bf482008-11-14 13:04:53 +0100174static inline int should_check_rate(struct thread_data *td)
175{
Jens Axboee235d5f2009-01-19 12:49:48 +0100176 struct thread_options *o = &td->o;
Jens Axboe993bf482008-11-14 13:04:53 +0100177
Jens Axboee235d5f2009-01-19 12:49:48 +0100178 /*
179 * If some rate setting was given, we need to check it
180 */
181 if (o->rate || o->ratemin || o->rate_iops || o->rate_iops_min)
182 return 1;
183
184 return 0;
Jens Axboe993bf482008-11-14 13:04:53 +0100185}
186
Jens Axboe697a6062008-05-31 00:04:45 +0200187/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200188 * Check if we are above the minimum rate given.
189 */
Jens Axboeebac4652005-12-08 15:25:21 +0100190static int check_min_rate(struct thread_data *td, struct timeval *now)
191{
Jens Axboe09042002007-02-23 10:56:22 +0100192 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100193 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100194 unsigned long spent;
195 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100196
197 /*
198 * allow a 2 second settle period in the beginning
199 */
200 if (mtime_since(&td->start, now) < 2000)
201 return 0;
202
Jens Axboe4e991c22007-03-15 11:41:11 +0100203 if (td_read(td)) {
204 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100205 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100206 }
207 if (td_write(td)) {
208 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100209 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100210 }
Jens Axboe09042002007-02-23 10:56:22 +0100211
Jens Axboeebac4652005-12-08 15:25:21 +0100212 /*
213 * if rate blocks is set, sample is running
214 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100215 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100216 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100217 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100218 return 0;
219
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100220 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100221 /*
222 * check bandwidth specified rate
223 */
224 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100225 log_err("%s: min rate %u not met\n", td->o.name,
226 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100227 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100228 } else {
229 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100230 if (rate < td->o.ratemin ||
231 bytes < td->rate_bytes) {
232 log_err("%s: min rate %u not met, got"
233 " %luKiB/sec\n", td->o.name,
234 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100235 return 1;
236 }
237 }
238 } else {
239 /*
240 * checks iops specified rate
241 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100242 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100243 log_err("%s: min iops rate %u not met\n",
244 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100245 return 1;
246 } else {
247 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100248 if (rate < td->o.rate_iops_min ||
249 iops < td->rate_blocks) {
250 log_err("%s: min iops rate %u not met,"
251 " got %lu\n", td->o.name,
252 td->o.rate_iops_min,
253 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100254 }
Jens Axboe413dd452007-02-23 09:26:09 +0100255 }
Jens Axboeebac4652005-12-08 15:25:21 +0100256 }
257 }
258
Jens Axboe09042002007-02-23 10:56:22 +0100259 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100260 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100261 memcpy(&td->lastrate, now, sizeof(*now));
262 return 0;
263}
264
265static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
266{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100267 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100268 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100269 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100270 return 1;
271
272 return 0;
273}
274
Jens Axboe906c8d72006-06-13 09:37:56 +0200275/*
276 * When job exits, we can cancel the in-flight IO if we are using async
277 * io. Attempt to do so.
278 */
Jens Axboeebac4652005-12-08 15:25:21 +0100279static void cleanup_pending_aio(struct thread_data *td)
280{
Jens Axboe01743ee2008-06-02 12:19:19 +0200281 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100282 struct io_u *io_u;
283 int r;
284
285 /*
286 * get immediately available events, if any
287 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100288 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100289 if (r < 0)
290 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100291
292 /*
293 * now cancel remaining active events
294 */
Jens Axboe2866c822006-10-09 15:57:48 +0200295 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200296 flist_for_each_safe(entry, n, &td->io_u_busylist) {
297 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100298
Jens Axboe0c6e7512007-02-22 11:19:39 +0100299 /*
300 * if the io_u isn't in flight, then that generally
301 * means someone leaked an io_u. complain but fix
302 * it up, so we don't stall here.
303 */
304 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
305 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100306 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100307 } else {
308 r = td->io_ops->cancel(td, io_u);
309 if (!r)
310 put_io_u(td, io_u);
311 }
Jens Axboeebac4652005-12-08 15:25:21 +0100312 }
313 }
314
Jens Axboe97601022007-02-18 12:47:29 +0100315 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100316 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100317}
318
Jens Axboe906c8d72006-06-13 09:37:56 +0200319/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200320 * Helper to handle the final sync of a file. Works just like the normal
321 * io path, just does everything sync.
322 */
323static int fio_io_sync(struct thread_data *td, struct fio_file *f)
324{
325 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200326 int ret;
327
328 if (!io_u)
329 return 1;
330
331 io_u->ddir = DDIR_SYNC;
332 io_u->file = f;
333
334 if (td_io_prep(td, io_u)) {
335 put_io_u(td, io_u);
336 return 1;
337 }
338
Jens Axboe755200a2007-02-19 13:08:12 +0100339requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200340 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100341 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100342 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200343 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200344 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100345 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100346 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100347 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100348 } else if (ret == FIO_Q_COMPLETED) {
349 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100350 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100351 return 1;
352 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200353
Jens Axboed7762cf2007-02-23 12:34:57 +0100354 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100355 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100356 } else if (ret == FIO_Q_BUSY) {
357 if (td_io_commit(td))
358 return 1;
359 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200360 }
361
362 return 0;
363}
364
Jens Axboe993bf482008-11-14 13:04:53 +0100365static inline void update_tv_cache(struct thread_data *td)
366{
367 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
368 fio_gettime(&td->tv_cache, NULL);
369}
370
Jens Axboe858a3d42006-10-24 14:54:44 +0200371/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100372 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200373 * reads the blocks back in, and checks the crc/md5 of the data.
374 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100375static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100376{
Jens Axboe53cdc682006-10-18 11:50:58 +0200377 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100378 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100379 int ret, min_events;
380 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200381
382 /*
383 * sync io first and invalidate cache, to make sure we really
384 * read from disk.
385 */
386 for_each_file(td, f, i) {
Jens Axboed6aed792009-06-03 08:41:15 +0200387 if (!fio_file_open(f))
Jens Axboe3d7b4852007-03-13 14:50:28 +0100388 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100389 if (fio_io_sync(td, f))
390 break;
391 if (file_invalidate_cache(td, f))
392 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200393 }
Jens Axboeebac4652005-12-08 15:25:21 +0100394
Jens Axboeb2fdda42007-02-20 20:52:51 +0100395 if (td->error)
396 return;
397
Jens Axboeebac4652005-12-08 15:25:21 +0100398 td_set_runstate(td, TD_VERIFYING);
399
Jens Axboe36167d82007-02-18 05:41:31 +0100400 io_u = NULL;
401 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200402 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100403
Jens Axboeebac4652005-12-08 15:25:21 +0100404 io_u = __get_io_u(td);
405 if (!io_u)
406 break;
407
Jens Axboe993bf482008-11-14 13:04:53 +0100408 update_tv_cache(td);
409
410 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe069c2912007-02-21 23:02:39 +0100411 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200412 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200413 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100414 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200415
Jens Axboe069c2912007-02-21 23:02:39 +0100416 if (get_next_verify(td, io_u)) {
417 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100418 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100419 }
Jens Axboeebac4652005-12-08 15:25:21 +0100420
Jens Axboe069c2912007-02-21 23:02:39 +0100421 if (td_io_prep(td, io_u)) {
422 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100423 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100424 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100425
426 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100427
Jens Axboe11786802007-03-05 18:33:22 +0100428 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100429 switch (ret) {
430 case FIO_Q_COMPLETED:
431 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100432 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100433 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100434 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200435 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100436
Jens Axboe9e9d1642007-03-12 09:37:46 +0100437 /*
438 * zero read, fail
439 */
440 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200441 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100442 put_io_u(td, io_u);
443 break;
444 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200445
Jens Axboe36167d82007-02-18 05:41:31 +0100446 io_u->xfer_buflen = io_u->resid;
447 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200448 io_u->offset += bytes;
449
Jens Axboe30061b92007-04-17 13:31:34 +0200450 td->ts.short_io_u[io_u->ddir]++;
451
Jens Axboed460eb32007-04-16 21:39:33 +0200452 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200453 goto sync_done;
454
Jens Axboe11786802007-03-05 18:33:22 +0100455 requeue_io_u(td, &io_u);
456 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200457sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100458 ret = io_u_sync_complete(td, io_u);
459 if (ret < 0)
460 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100461 }
Jens Axboe36167d82007-02-18 05:41:31 +0100462 continue;
463 case FIO_Q_QUEUED:
464 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100465 case FIO_Q_BUSY:
466 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100467 ret2 = td_io_commit(td);
468 if (ret2 < 0)
469 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100470 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100471 default:
472 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100473 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100474 break;
475 }
476
Jens Axboe99784632007-02-19 10:06:17 +0100477 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100478 break;
479
Jens Axboe3af6ef32007-02-18 06:57:43 +0100480 /*
481 * if we can queue more, do so. but check if there are
482 * completed io_u's first.
483 */
Jens Axboe49504212008-06-05 09:03:30 +0200484 full = queue_full(td) || ret == FIO_Q_BUSY;
485 if (full || !td->o.iodepth_batch_complete) {
486 min_events = td->o.iodepth_batch_complete;
487 if (full && !min_events)
Jens Axboe838bc702008-05-22 13:08:23 +0200488 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100489
Jens Axboe49504212008-06-05 09:03:30 +0200490 do {
491 /*
492 * Reap required number of io units, if any,
493 * and do the verification on them through
494 * the callback handler
495 */
496 if (io_u_queued_complete(td, min_events) < 0) {
497 ret = -1;
498 break;
499 }
500 } while (full && (td->cur_depth > td->o.iodepth_low));
501 }
502 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100503 break;
504 }
505
Jens Axboec01c0392007-02-26 12:43:42 +0100506 if (!td->error) {
507 min_events = td->cur_depth;
508
509 if (min_events)
510 ret = io_u_queued_complete(td, min_events);
511 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100512 cleanup_pending_aio(td);
513
514 td_set_runstate(td, TD_RUNNING);
515}
516
Jens Axboe32cd46a2006-06-07 13:40:40 +0200517/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200518 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200519 * and reaps them, checking for rate and errors along the way.
520 */
Jens Axboeebac4652005-12-08 15:25:21 +0100521static void do_io(struct thread_data *td)
522{
Jens Axboeebac4652005-12-08 15:25:21 +0100523 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100524 unsigned int i;
525 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100526
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200527 if (in_ramp_time(td))
528 td_set_runstate(td, TD_RAMP);
529 else
530 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200531
Jens Axboe7bb48f82007-03-27 15:30:28 +0200532 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100533 struct timeval comp_time;
534 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200535 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100536 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200537 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100538
539 if (td->terminate)
540 break;
541
Jens Axboe3d7c3912007-02-19 13:16:12 +0100542 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100543 if (!io_u)
544 break;
545
Jens Axboe993bf482008-11-14 13:04:53 +0100546 update_tv_cache(td);
Jens Axboe97601022007-02-18 12:47:29 +0100547
Jens Axboe993bf482008-11-14 13:04:53 +0100548 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe97601022007-02-18 12:47:29 +0100549 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200550 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100551 break;
552 }
Jens Axboe36167d82007-02-18 05:41:31 +0100553
Jens Axboeb67740d2007-07-26 12:22:30 +0200554 /*
555 * Add verification end_io handler, if asked to verify
556 * a previously written file.
557 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100558 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200559 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100560 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200561 } else if (in_ramp_time(td))
562 td_set_runstate(td, TD_RAMP);
563 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100564 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200565
Jens Axboe11786802007-03-05 18:33:22 +0100566 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100567 switch (ret) {
568 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100569 if (io_u->error)
570 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100571 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100572 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200573 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100574
Jens Axboe9e9d1642007-03-12 09:37:46 +0100575 /*
576 * zero read, fail
577 */
578 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200579 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100580 put_io_u(td, io_u);
581 break;
582 }
583
Jens Axboe36167d82007-02-18 05:41:31 +0100584 io_u->xfer_buflen = io_u->resid;
585 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200586 io_u->offset += bytes;
587
Jens Axboe30061b92007-04-17 13:31:34 +0200588 td->ts.short_io_u[io_u->ddir]++;
589
Jens Axboed460eb32007-04-16 21:39:33 +0200590 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200591 goto sync_done;
592
Jens Axboe11786802007-03-05 18:33:22 +0100593 requeue_io_u(td, &io_u);
594 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200595sync_done:
Jens Axboe993bf482008-11-14 13:04:53 +0100596 if (should_check_rate(td))
597 fio_gettime(&comp_time, NULL);
598
Jens Axboe11786802007-03-05 18:33:22 +0100599 bytes_done = io_u_sync_complete(td, io_u);
600 if (bytes_done < 0)
601 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100602 }
Jens Axboe36167d82007-02-18 05:41:31 +0100603 break;
604 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100605 /*
606 * if the engine doesn't have a commit hook,
607 * the io_u is really queued. if it does have such
608 * a hook, it has to call io_u_queued() itself.
609 */
610 if (td->io_ops->commit == NULL)
611 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100612 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100613 case FIO_Q_BUSY:
614 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100615 ret2 = td_io_commit(td);
616 if (ret2 < 0)
617 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100618 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100619 default:
620 assert(ret < 0);
621 put_io_u(td, io_u);
622 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100623 }
624
Jens Axboe99784632007-02-19 10:06:17 +0100625 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100626 break;
627
Jens Axboe97601022007-02-18 12:47:29 +0100628 /*
629 * See if we need to complete some commands
630 */
Jens Axboe49504212008-06-05 09:03:30 +0200631 full = queue_full(td) || ret == FIO_Q_BUSY;
632 if (full || !td->o.iodepth_batch_complete) {
633 min_evts = td->o.iodepth_batch_complete;
634 if (full && !min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100635 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200636
Jens Axboe993bf482008-11-14 13:04:53 +0100637 if (should_check_rate(td))
638 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200639
640 do {
641 ret = io_u_queued_complete(td, min_evts);
642 if (ret <= 0)
643 break;
644
645 bytes_done += ret;
646 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100647 }
648
Jens Axboe49504212008-06-05 09:03:30 +0200649 if (ret < 0)
650 break;
Jens Axboe97601022007-02-18 12:47:29 +0100651 if (!bytes_done)
652 continue;
653
Jens Axboeebac4652005-12-08 15:25:21 +0100654 /*
655 * the rate is batched for now, it should work for batches
656 * of completions except the very first one which may look
657 * a little bursty
658 */
Jens Axboe993bf482008-11-14 13:04:53 +0100659 if (!in_ramp_time(td) && should_check_rate(td)) {
660 usec = utime_since(&td->tv_cache, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100661
Jens Axboe721938a2008-09-10 09:46:16 +0200662 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100663
Jens Axboe721938a2008-09-10 09:46:16 +0200664 if (check_min_rate(td, &comp_time)) {
665 if (exitall_on_terminate)
666 terminate_threads(td->groupid);
667 td_verror(td, EIO, "check_min_rate");
668 break;
669 }
Jens Axboeebac4652005-12-08 15:25:21 +0100670 }
671
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100672 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100673 unsigned long long b;
674
675 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100676 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100677 int left;
678
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100679 if (td->o.thinktime_spin)
Jens Axboe6dd6f2c2008-12-10 13:24:12 +0100680 usec_spin(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100681
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100682 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100683 if (left)
684 usec_sleep(td, left);
685 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100686 }
Jens Axboeebac4652005-12-08 15:25:21 +0100687 }
688
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100689 if (td->o.fill_device && td->error == ENOSPC) {
690 td->error = 0;
691 td->terminate = 1;
692 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100693 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100694 struct fio_file *f;
695
Jens Axboec01c0392007-02-26 12:43:42 +0100696 i = td->cur_depth;
697 if (i)
698 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100699
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100700 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200701 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100702
703 for_each_file(td, f, i) {
Jens Axboed6aed792009-06-03 08:41:15 +0200704 if (!fio_file_open(f))
Jens Axboe3d7b4852007-03-13 14:50:28 +0100705 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200706 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100707 }
Jens Axboe84585002006-10-19 20:26:22 +0200708 }
Jens Axboec01c0392007-02-26 12:43:42 +0100709 } else
710 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100711
712 /*
713 * stop job if we failed doing any IO
714 */
715 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
716 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100717}
718
Jens Axboeebac4652005-12-08 15:25:21 +0100719static void cleanup_io_u(struct thread_data *td)
720{
Jens Axboe01743ee2008-06-02 12:19:19 +0200721 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100722 struct io_u *io_u;
723
Jens Axboe01743ee2008-06-02 12:19:19 +0200724 flist_for_each_safe(entry, n, &td->io_u_freelist) {
725 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100726
Jens Axboe01743ee2008-06-02 12:19:19 +0200727 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100728 free(io_u);
729 }
730
Jens Axboe2f9ade32006-10-20 11:25:52 +0200731 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100732}
733
734static int init_io_u(struct thread_data *td)
735{
736 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100737 unsigned int max_bs;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200738 int cl_align, i, max_units;
Jens Axboeebac4652005-12-08 15:25:21 +0100739 char *p;
740
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100741 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100742 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100743 td->orig_buffer_size = (unsigned long long) max_bs
744 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100745
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100746 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
747 unsigned long bs;
748
749 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
750 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
751 }
Jens Axboeebac4652005-12-08 15:25:21 +0100752
Jens Axboec3990642007-07-19 10:17:09 +0200753 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
754 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
755 return 1;
756 }
757
Jens Axboe2f9ade32006-10-20 11:25:52 +0200758 if (allocate_io_mem(td))
759 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100760
Jens Axboeb3f378e2007-07-20 12:39:22 +0200761 if (td->o.odirect)
762 p = ALIGN(td->orig_buffer);
763 else
764 p = td->orig_buffer;
765
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200766 cl_align = os_cache_line_size();
767
Jens Axboeebac4652005-12-08 15:25:21 +0100768 for (i = 0; i < max_units; i++) {
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200769 void *ptr;
770
Jens Axboedb1cd902007-04-26 13:47:07 +0200771 if (td->terminate)
772 return 1;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200773
774 if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) {
775 log_err("fio: posix_memalign=%s\n", strerror(errno));
776 break;
777 }
778
779 io_u = ptr;
Jens Axboeebac4652005-12-08 15:25:21 +0100780 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200781 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100782
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200783 if (!(td->io_ops->flags & FIO_NOIO)) {
784 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200785
Jens Axboe5973caf2008-05-21 19:52:35 +0200786 if (td_write(td) && !td->o.refill_buffers)
787 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200788 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100789
Jens Axboeb1ff3402006-02-17 11:35:58 +0100790 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100791 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200792 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100793 }
794
795 return 0;
796}
797
Jens Axboeda867742006-06-06 10:39:10 -0700798static int switch_ioscheduler(struct thread_data *td)
799{
800 char tmp[256], tmp2[128];
801 FILE *f;
802 int ret;
803
Jens Axboeba0fbe12007-03-09 14:34:23 +0100804 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200805 return 0;
806
Jens Axboeda867742006-06-06 10:39:10 -0700807 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
808
809 f = fopen(tmp, "r+");
810 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200811 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100812 log_err("fio: os or kernel doesn't support IO scheduler"
813 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200814 return 0;
815 }
816 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700817 return 1;
818 }
819
820 /*
821 * Set io scheduler.
822 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100823 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700824 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100825 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700826 fclose(f);
827 return 1;
828 }
829
830 rewind(f);
831
832 /*
833 * Read back and check that the selected scheduler is now the default.
834 */
835 ret = fread(tmp, 1, sizeof(tmp), f);
836 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100837 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700838 fclose(f);
839 return 1;
840 }
841
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100842 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700843 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100844 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100845 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700846 fclose(f);
847 return 1;
848 }
849
850 fclose(f);
851 return 0;
852}
853
Jens Axboe492158c2007-05-24 10:32:47 +0200854static int keep_running(struct thread_data *td)
855{
856 unsigned long long io_done;
857
Jens Axboe20e354e2007-07-23 14:36:16 +0200858 if (td->done)
859 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200860 if (td->o.time_based)
861 return 1;
862 if (td->o.loops) {
863 td->o.loops--;
864 return 1;
865 }
866
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100867 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
868 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200869 if (io_done < td->o.size)
870 return 1;
871
872 return 0;
873}
874
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200875static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100876{
Jens Axboe756867b2007-03-06 15:19:24 +0100877 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100878 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100879 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100880 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100881 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100882 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100883
Jens Axboec1324df2007-02-19 19:06:41 +0100884 td->last_was_sync = 0;
885
Jens Axboe2ba1c292008-02-01 13:16:38 +0100886 /*
887 * reset file done count if we are to start over
888 */
889 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200890 td->nr_done_files = 0;
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200891
892 /*
893 * Set the same seed to get repeatable runs
894 */
895 td_fill_rand_seeds(td);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200896}
897
898void reset_all_stats(struct thread_data *td)
899{
900 struct timeval tv;
901 int i;
902
903 reset_io_counters(td);
904
905 for (i = 0; i < 2; i++) {
906 td->io_bytes[i] = 0;
907 td->io_blocks[i] = 0;
908 td->io_issues[i] = 0;
909 td->ts.total_io_u[i] = 0;
910 }
911
912 fio_gettime(&tv, NULL);
913 memcpy(&td->epoch, &tv, sizeof(tv));
914 memcpy(&td->start, &tv, sizeof(tv));
915}
916
Jens Axboedf9c26b2009-03-05 10:13:58 +0100917static void clear_io_state(struct thread_data *td)
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200918{
919 struct fio_file *f;
920 unsigned int i;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200921
922 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +0200923
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100924 close_files(td);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100925 for_each_file(td, f, i)
Jens Axboed6aed792009-06-03 08:41:15 +0200926 fio_file_clear_done(f);
Jens Axboeebac4652005-12-08 15:25:21 +0100927}
928
Jens Axboe398e8c42009-05-20 12:10:02 +0200929static int exec_string(const char *string)
930{
931 int ret, newlen = strlen(string) + 1 + 8;
932 char *str;
933
934 str = malloc(newlen);
935 sprintf(str, "sh -c %s", string);
936
937 ret = system(str);
938 if (ret == -1)
939 log_err("fio: exec of cmd <%s> failed\n", str);
940
941 free(str);
942 return ret;
943}
944
Jens Axboe906c8d72006-06-13 09:37:56 +0200945/*
946 * Entry point for the thread based jobs. The process based jobs end up
947 * here as well, after a little setup.
948 */
Jens Axboeebac4652005-12-08 15:25:21 +0100949static void *thread_main(void *data)
950{
Shawn Lewis10927312007-11-21 09:38:13 +0100951 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100952 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100953 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100954
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100955 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100956 setsid();
957
958 td->pid = getpid();
959
Jens Axboe5921e802008-05-30 15:02:38 +0200960 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100961
Jens Axboe01743ee2008-06-02 12:19:19 +0200962 INIT_FLIST_HEAD(&td->io_u_freelist);
963 INIT_FLIST_HEAD(&td->io_u_busylist);
964 INIT_FLIST_HEAD(&td->io_u_requeues);
965 INIT_FLIST_HEAD(&td->io_log_list);
966 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200967 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200968
Jens Axboe75154842006-06-01 13:56:09 +0200969 td_set_runstate(td, TD_INITIALIZED);
Jens Axboe29adda32009-01-05 19:06:39 +0100970 dprint(FD_MUTEX, "up startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +0100971 fio_mutex_up(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +0100972 dprint(FD_MUTEX, "wait on td->mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +0100973 fio_mutex_down(td->mutex);
Jens Axboe29adda32009-01-05 19:06:39 +0100974 dprint(FD_MUTEX, "done waiting on td->mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100975
Jens Axboe37f56872007-03-09 12:42:35 +0100976 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100977 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100978 * eating a file descriptor
979 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100980 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100981
Jens Axboed84f8d42007-05-16 08:47:46 +0200982 /*
983 * May alter parameters that init_io_u() will use, so we need to
984 * do this first.
985 */
986 if (init_iolog(td))
987 goto err;
988
Jens Axboedb1cd902007-04-26 13:47:07 +0200989 if (init_io_u(td))
990 goto err;
991
Jens Axboe375b2692007-05-22 09:13:02 +0200992 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200993 td_verror(td, errno, "cpu_set_affinity");
994 goto err;
995 }
996
Jens Axboef4844702008-12-09 08:04:29 +0100997 /*
998 * If we have a gettimeofday() thread, make sure we exclude that
999 * thread from this job
1000 */
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001001 if (td->o.gtod_cpu) {
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001002 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1003 if (fio_setaffinity(td) == -1) {
1004 td_verror(td, errno, "cpu_set_affinity");
1005 goto err;
1006 }
1007 }
1008
Jens Axboeac684782007-11-08 08:29:07 +01001009 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +02001010 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1011 td_verror(td, errno, "ioprio_set");
1012 goto err;
1013 }
1014 }
1015
1016 if (nice(td->o.nice) == -1) {
1017 td_verror(td, errno, "nice");
1018 goto err;
1019 }
1020
1021 if (td->o.ioscheduler && switch_ioscheduler(td))
1022 goto err;
1023
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001024 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +01001025 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +01001026
1027 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +01001028 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +01001029
Jens Axboe68727072007-03-15 20:49:25 +01001030 if (init_random_map(td))
1031 goto err;
1032
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001033 if (td->o.exec_prerun) {
Jens Axboe398e8c42009-05-20 12:10:02 +02001034 if (exec_string(td->o.exec_prerun))
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001035 goto err;
1036 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001037
Zhang, Yanminafad68f2009-05-20 11:30:55 +02001038 if (td->o.pre_read) {
1039 if (pre_read_files(td) < 0)
1040 goto err;
1041 }
1042
Jens Axboe69008992006-11-24 13:12:56 +01001043 fio_gettime(&td->epoch, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001044 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +01001045
1046 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +01001047 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001048 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +01001049 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001050 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboe993bf482008-11-14 13:04:53 +01001051 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +01001052
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001053 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001054 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1055 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +01001056
Jens Axboedf9c26b2009-03-05 10:13:58 +01001057 if (clear_state)
1058 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001059
Jens Axboeebac4652005-12-08 15:25:21 +01001060 prune_io_piece_log(td);
1061
Jens Axboeba0fbe12007-03-09 14:34:23 +01001062 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001063
Jens Axboea978ba62007-03-08 14:29:03 +01001064 clear_state = 1;
1065
Jens Axboe38d77ca2007-03-21 14:20:20 +01001066 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1067 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001068 elapsed = utime_since(&td->start,
1069 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001070 else
1071 elapsed = utime_since_now(&td->start);
1072
1073 runtime[DDIR_READ] += elapsed;
1074 }
1075 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1076 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001077 elapsed = utime_since(&td->start,
1078 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001079 else
1080 elapsed = utime_since_now(&td->start);
1081
1082 runtime[DDIR_WRITE] += elapsed;
1083 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001084
Jens Axboeebac4652005-12-08 15:25:21 +01001085 if (td->error || td->terminate)
1086 break;
1087
Shawn Lewise84c73a2007-08-02 22:19:32 +02001088 if (!td->o.do_verify ||
1089 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001090 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001091 continue;
1092
Jens Axboedf9c26b2009-03-05 10:13:58 +01001093 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001094
Jens Axboe02bcaa82006-11-24 10:42:00 +01001095 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001096
1097 do_verify(td);
1098
Jens Axboe69008992006-11-24 13:12:56 +01001099 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001100
1101 if (td->error || td->terminate)
1102 break;
1103 }
1104
Jens Axboe36dff962006-11-24 13:29:20 +01001105 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001106 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1107 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001108 td->ts.total_run_time = mtime_since_now(&td->epoch);
1109 td->ts.io_bytes[0] = td->io_bytes[0];
1110 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001111
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001112 fio_mutex_down(writeout_mutex);
Jens Axboee3cedca2008-11-19 19:57:52 +01001113 if (td->ts.bw_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001114 if (td->o.bw_log_file) {
1115 finish_log_named(td, td->ts.bw_log,
1116 td->o.bw_log_file, "bw");
1117 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001118 finish_log(td, td->ts.bw_log, "bw");
1119 }
1120 if (td->ts.slat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001121 if (td->o.lat_log_file) {
1122 finish_log_named(td, td->ts.slat_log,
Radha Ramachandran1ca95732009-04-29 07:23:26 +02001123 td->o.lat_log_file, "slat");
Jens Axboef4844702008-12-09 08:04:29 +01001124 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001125 finish_log(td, td->ts.slat_log, "slat");
1126 }
1127 if (td->ts.clat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001128 if (td->o.lat_log_file) {
1129 finish_log_named(td, td->ts.clat_log,
1130 td->o.lat_log_file, "clat");
1131 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001132 finish_log(td, td->ts.clat_log, "clat");
1133 }
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001134 fio_mutex_up(writeout_mutex);
Jens Axboe398e8c42009-05-20 12:10:02 +02001135 if (td->o.exec_postrun)
1136 exec_string(td->o.exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +01001137
1138 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001139 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001140
1141err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001142 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001143 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001144 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001145 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001146 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001147 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001148
Jens Axboed2ce18b2008-12-12 20:51:40 +01001149 if (td->o.cpumask_set) {
1150 int ret = fio_cpuset_exit(&td->o.cpumask);
1151
1152 td_verror(td, ret, "fio_cpuset_exit");
1153 }
Jens Axboe4e78e402008-12-12 20:40:27 +01001154
Jens Axboef29b25a2007-07-23 08:56:43 +02001155 /*
1156 * do this very late, it will log file closing as well
1157 */
1158 if (td->o.write_iolog_file)
1159 write_iolog_close(td);
1160
Jens Axboed23bb322007-03-23 15:57:56 +01001161 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001162 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001163 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001164}
1165
Jens Axboe906c8d72006-06-13 09:37:56 +02001166/*
1167 * We cannot pass the td data into a forked process, so attach the td and
1168 * pass it to the thread worker.
1169 */
Jens Axboea6418142007-02-17 04:47:18 +01001170static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001171{
1172 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001173 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001174
1175 data = shmat(shmid, NULL, 0);
1176 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001177 int __err = errno;
1178
Jens Axboeebac4652005-12-08 15:25:21 +01001179 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001180 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001181 }
1182
1183 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001184 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001185 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001186 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001187}
1188
Jens Axboe906c8d72006-06-13 09:37:56 +02001189/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001190 * Run over the job map and reap the threads that have exited, if any.
1191 */
Jens Axboeebac4652005-12-08 15:25:21 +01001192static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1193{
Jens Axboe34572e22006-10-20 09:33:18 +02001194 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001195 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001196
1197 /*
1198 * reap exited threads (TD_EXITED -> TD_REAPED)
1199 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001200 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001201 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001202 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001203
Jens Axboe84585002006-10-19 20:26:22 +02001204 /*
1205 * ->io_ops is NULL for a thread that has closed its
1206 * io engine
1207 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001208 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001209 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001210 else
1211 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001212
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001213 if (!td->pid) {
1214 pending++;
1215 continue;
1216 }
1217 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001218 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001219 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001220 if (td->runstate == TD_EXITED) {
1221 td_set_runstate(td, TD_REAPED);
1222 goto reaped;
1223 }
1224 continue;
1225 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001226
1227 flags = WNOHANG;
1228 if (td->runstate == TD_EXITED)
1229 flags = 0;
1230
1231 /*
1232 * check if someone quit or got killed in an unusual way
1233 */
1234 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001235 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001236 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001237 log_err("fio: pid=%d disappeared %d\n",
1238 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001239 td_set_runstate(td, TD_REAPED);
1240 goto reaped;
1241 }
1242 perror("waitpid");
1243 } else if (ret == td->pid) {
1244 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001245 int sig = WTERMSIG(status);
1246
Jens Axboed9244942007-03-05 12:32:32 +01001247 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001248 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001249 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001250 td_set_runstate(td, TD_REAPED);
1251 goto reaped;
1252 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001253 if (WIFEXITED(status)) {
1254 if (WEXITSTATUS(status) && !td->error)
1255 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001256
Jens Axboea2f77c92007-02-22 11:53:00 +01001257 td_set_runstate(td, TD_REAPED);
1258 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001259 }
1260 }
Jens Axboeebac4652005-12-08 15:25:21 +01001261
Jens Axboea2f77c92007-02-22 11:53:00 +01001262 /*
1263 * thread is not dead, continue
1264 */
gurudas paie48676b2007-04-11 12:44:27 +02001265 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001266 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001267reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001268 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001269 (*m_rate) -= td->o.ratemin;
1270 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001271 if (!td->pid)
1272 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001273
1274 if (td->error)
1275 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001276
1277 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001278 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001279
Jens Axboe1f809d12007-10-25 18:34:02 +02001280 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001281 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001282}
1283
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001284static void *gtod_thread_main(void *data)
1285{
1286 fio_mutex_up(startup_mutex);
1287
1288 /*
1289 * As long as we have jobs around, update the clock. It would be nice
1290 * to have some way of NOT hammering that CPU with gettimeofday(),
1291 * but I'm not sure what to use outside of a simple CPU nop to relax
1292 * it - we don't want to lose precision.
1293 */
1294 while (threads) {
1295 fio_gtod_update();
1296 nop;
1297 }
1298
1299 return NULL;
1300}
1301
1302static int fio_start_gtod_thread(void)
1303{
Jens Axboef42c1532009-01-05 19:07:13 +01001304 int ret;
1305
1306 ret = pthread_create(&gtod_thread, NULL, gtod_thread_main, NULL);
1307 if (ret) {
1308 log_err("Can't create gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001309 return 1;
1310 }
Jens Axboef42c1532009-01-05 19:07:13 +01001311
1312 ret = pthread_detach(gtod_thread);
1313 if (ret) {
1314 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001315 return 1;
1316 }
1317
Jens Axboe29adda32009-01-05 19:06:39 +01001318 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001319 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001320 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001321 return 0;
1322}
1323
Jens Axboe906c8d72006-06-13 09:37:56 +02001324/*
1325 * Main function for kicking off and reaping jobs, as needed.
1326 */
Jens Axboeebac4652005-12-08 15:25:21 +01001327static void run_threads(void)
1328{
Jens Axboeebac4652005-12-08 15:25:21 +01001329 struct thread_data *td;
1330 unsigned long spent;
1331 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001332
Jens Axboe2f9ade32006-10-20 11:25:52 +02001333 if (fio_pin_memory())
1334 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001335
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001336 if (fio_gtod_offload && fio_start_gtod_thread())
1337 return;
1338
Jens Axboec6ae0a52006-06-12 11:04:44 +02001339 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001340 printf("Starting ");
1341 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001342 printf("%d thread%s", nr_thread,
1343 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001344 if (nr_process) {
1345 if (nr_thread)
1346 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001347 printf("%d process%s", nr_process,
1348 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001349 }
1350 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001351 fflush(stdout);
1352 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001353
Jens Axboe697a6062008-05-31 00:04:45 +02001354 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001355
Jens Axboeebac4652005-12-08 15:25:21 +01001356 todo = thread_number;
1357 nr_running = 0;
1358 nr_started = 0;
1359 m_rate = t_rate = 0;
1360
Jens Axboe34572e22006-10-20 09:33:18 +02001361 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001362 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001363
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001364 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001365 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001366 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001367 }
Jens Axboeebac4652005-12-08 15:25:21 +01001368
1369 /*
1370 * do file setup here so it happens sequentially,
1371 * we don't want X number of threads getting their
1372 * client data interspersed on disk
1373 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001374 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001375 exit_value++;
1376 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001377 log_err("fio: pid=%d, err=%d/%s\n",
1378 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001379 td_set_runstate(td, TD_REAPED);
1380 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001381 } else {
1382 struct fio_file *f;
1383 unsigned int i;
1384
1385 /*
1386 * for sharing to work, each job must always open
1387 * its own files. so close them, if we opened them
1388 * for creation
1389 */
1390 for_each_file(td, f, i)
1391 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001392 }
Jens Axboe380cf262007-01-11 14:01:27 +01001393
1394 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001395 }
1396
Jens Axboea2f77c92007-02-22 11:53:00 +01001397 set_genesis_time();
1398
Jens Axboeebac4652005-12-08 15:25:21 +01001399 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001400 struct thread_data *map[MAX_JOBS];
1401 struct timeval this_start;
1402 int this_jobs = 0, left;
1403
Jens Axboeebac4652005-12-08 15:25:21 +01001404 /*
1405 * create threads (TD_NOT_CREATED -> TD_CREATED)
1406 */
Jens Axboe34572e22006-10-20 09:33:18 +02001407 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001408 if (td->runstate != TD_NOT_CREATED)
1409 continue;
1410
1411 /*
1412 * never got a chance to start, killed by other
1413 * thread for some reason
1414 */
1415 if (td->terminate) {
1416 todo--;
1417 continue;
1418 }
1419
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001420 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001421 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001422
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001423 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001424 continue;
1425 }
1426
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001427 if (td->o.stonewall && (nr_started || nr_running)) {
1428 dprint(FD_PROCESS, "%s: stonewall wait\n",
1429 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001430 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001431 }
Jens Axboeebac4652005-12-08 15:25:21 +01001432
Jens Axboe75154842006-06-01 13:56:09 +02001433 /*
1434 * Set state to created. Thread will transition
1435 * to TD_INITIALIZED when it's done setting up.
1436 */
Jens Axboeebac4652005-12-08 15:25:21 +01001437 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001438 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001439 nr_started++;
1440
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001441 if (td->o.use_thread) {
Jens Axboef42c1532009-01-05 19:07:13 +01001442 int ret;
1443
Jens Axboeee56ad52008-02-01 10:30:20 +01001444 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboef42c1532009-01-05 19:07:13 +01001445 ret = pthread_create(&td->thread, NULL,
1446 thread_main, td);
1447 if (ret) {
1448 log_err("pthread_create: %s\n",
1449 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001450 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001451 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001452 }
Jens Axboef42c1532009-01-05 19:07:13 +01001453 ret = pthread_detach(td->thread);
1454 if (ret)
1455 log_err("pthread_detach: %s",
1456 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001457 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001458 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001459 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001460 pid = fork();
1461 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001462 int ret = fork_main(shm_id, i);
1463
Jens Axboe5e1d3062008-05-23 11:55:53 +02001464 _exit(ret);
1465 } else if (i == fio_debug_jobno)
1466 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001467 }
Jens Axboe29adda32009-01-05 19:06:39 +01001468 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +01001469 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001470 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001471 }
1472
1473 /*
Jens Axboe75154842006-06-01 13:56:09 +02001474 * Wait for the started threads to transition to
1475 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001476 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001477 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001478 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001479 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001480 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1481 break;
1482
1483 usleep(100000);
1484
1485 for (i = 0; i < this_jobs; i++) {
1486 td = map[i];
1487 if (!td)
1488 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001489 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001490 map[i] = NULL;
1491 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001492 } else if (td->runstate >= TD_EXITED) {
1493 map[i] = NULL;
1494 left--;
1495 todo--;
1496 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001497 }
1498 }
1499 }
1500
1501 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001502 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001503 for (i = 0; i < this_jobs; i++) {
1504 td = map[i];
1505 if (!td)
1506 continue;
1507 kill(td->pid, SIGTERM);
1508 }
1509 break;
1510 }
1511
1512 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001513 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001514 */
Jens Axboe34572e22006-10-20 09:33:18 +02001515 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001516 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001517 continue;
1518
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001519 if (in_ramp_time(td))
1520 td_set_runstate(td, TD_RAMP);
1521 else
1522 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001523 nr_running++;
1524 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001525 m_rate += td->o.ratemin;
1526 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001527 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001528 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001529 }
1530
1531 reap_threads(&nr_running, &t_rate, &m_rate);
1532
1533 if (todo)
1534 usleep(100000);
1535 }
1536
1537 while (nr_running) {
1538 reap_threads(&nr_running, &t_rate, &m_rate);
1539 usleep(10000);
1540 }
1541
1542 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001543 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001544}
1545
Jens Axboeebac4652005-12-08 15:25:21 +01001546int main(int argc, char *argv[])
1547{
Jens Axboe29d610e2007-02-06 13:25:33 +01001548 long ps;
1549
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001550 sinit();
1551
Jens Axboedbe11252007-02-11 00:19:51 +01001552 /*
1553 * We need locale for number printing, if it isn't set then just
1554 * go with the US format.
1555 */
1556 if (!getenv("LC_NUMERIC"))
1557 setlocale(LC_NUMERIC, "en_US");
1558
Jens Axboeebac4652005-12-08 15:25:21 +01001559 if (parse_options(argc, argv))
1560 return 1;
1561
Jens Axboe4b472fa2007-04-04 11:04:34 +02001562 if (!thread_number)
1563 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001564
Jens Axboe29d610e2007-02-06 13:25:33 +01001565 ps = sysconf(_SC_PAGESIZE);
1566 if (ps < 0) {
1567 log_err("Failed to get page size\n");
1568 return 1;
1569 }
1570
Jens Axboecfc99db2007-03-14 10:34:47 +01001571 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001572 page_mask = ps - 1;
1573
Jens Axboebb3884d2007-01-17 17:23:11 +11001574 if (write_bw_log) {
1575 setup_log(&agg_io_log[DDIR_READ]);
1576 setup_log(&agg_io_log[DDIR_WRITE]);
1577 }
1578
Jens Axboecdd18ad2008-02-27 18:58:00 +01001579 startup_mutex = fio_mutex_init(0);
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001580 writeout_mutex = fio_mutex_init(1);
Jens Axboe07739b52007-03-08 20:25:46 +01001581
Jens Axboea2f77c92007-02-22 11:53:00 +01001582 set_genesis_time();
1583
Jens Axboeccb0fa22008-05-30 23:18:00 +02001584 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001585
1586 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001587
Jens Axboebb3884d2007-01-17 17:23:11 +11001588 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001589 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001590 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001591 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1592 __finish_log(agg_io_log[DDIR_WRITE],
1593 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001594 }
1595 }
Jens Axboeebac4652005-12-08 15:25:21 +01001596
Jens Axboecdd18ad2008-02-27 18:58:00 +01001597 fio_mutex_remove(startup_mutex);
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001598 fio_mutex_remove(writeout_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001599 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001600}