blob: 5bd1baefc29849ec58c225e4443f8a829b95df5d [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;
Fabio Checconid2bbb8d2009-05-18 12:54:50 +020055static struct fio_mutex *writeout_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010056static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010057static int exit_value;
Jens Axboeccb0fa22008-05-30 23:18:00 +020058static struct itimerval itimer;
Jens Axboebe4ecfd2008-12-08 14:10:52 +010059static pthread_t gtod_thread;
Jens Axboeebac4652005-12-08 15:25:21 +010060
Jens Axboebb3884d2007-01-17 17:23:11 +110061struct io_log *agg_io_log[2];
62
Jens Axboeebac4652005-12-08 15:25:21 +010063#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020064#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010065
Jens Axboeb29ee5b2008-09-11 10:17:26 +020066void td_set_runstate(struct thread_data *td, int runstate)
Jens Axboe6ce15a32007-01-12 09:04:41 +010067{
Jens Axboed8fab1b2008-03-06 10:25:17 +010068 if (td->runstate == runstate)
69 return;
70
Jens Axboe5921e802008-05-30 15:02:38 +020071 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
72 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010073 td->runstate = runstate;
74}
75
Jens Axboe390c40e2007-03-05 12:35:29 +010076static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010077{
Jens Axboe34572e22006-10-20 09:33:18 +020078 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010079 int i;
80
Jens Axboe6f88bcb2008-03-19 10:29:07 +010081 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
82
Jens Axboe34572e22006-10-20 09:33:18 +020083 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010084 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010085 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +020086 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010087 td->terminate = 1;
88 td->o.start_delay = 0;
89
Jens Axboe7a93ab12007-04-18 12:25:41 +020090 /*
91 * if the thread is running, just let it exit
92 */
93 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010094 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010095 else {
96 struct ioengine_ops *ops = td->io_ops;
97
98 if (ops && (ops->flags & FIO_SIGQUIT))
99 kill(td->pid, SIGQUIT);
100 }
Jens Axboeebac4652005-12-08 15:25:21 +0100101 }
102 }
103}
104
Jens Axboeccb0fa22008-05-30 23:18:00 +0200105static void status_timer_arm(void)
106{
107 itimer.it_value.tv_sec = 0;
108 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
109 setitimer(ITIMER_REAL, &itimer, NULL);
110}
111
Jens Axboe02444ad2008-10-07 11:33:00 +0200112static void sig_alrm(int fio_unused sig)
Jens Axboeccb0fa22008-05-30 23:18:00 +0200113{
Jens Axboe697a6062008-05-31 00:04:45 +0200114 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100115 update_io_ticks();
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100116 print_thread_status();
Jens Axboeccb0fa22008-05-30 23:18:00 +0200117 status_timer_arm();
Jens Axboe697a6062008-05-31 00:04:45 +0200118 }
119}
120
Jens Axboe5f8f9722008-11-18 19:10:21 +0100121/*
122 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
123 */
124static void sig_quit(int sig)
125{
126}
127
Jens Axboe697a6062008-05-31 00:04:45 +0200128static void sig_int(int sig)
129{
130 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100131 printf("\nfio: terminating on signal %d\n", sig);
132 fflush(stdout);
133 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100134 }
135}
136
Jens Axboe02444ad2008-10-07 11:33:00 +0200137static void sig_ill(int fio_unused sig)
Jens Axboede79c912008-08-04 21:43:55 +0200138{
139 if (!threads)
140 return;
141
142 log_err("fio: illegal instruction. your cpu does not support "
143 "the sse4.2 instruction for crc32c\n");
144 terminate_threads(TERMINATE_ALL);
145 exit(4);
146}
147
Jens Axboe697a6062008-05-31 00:04:45 +0200148static void set_sig_handlers(void)
149{
150 struct sigaction act;
151
152 memset(&act, 0, sizeof(act));
153 act.sa_handler = sig_alrm;
154 act.sa_flags = SA_RESTART;
155 sigaction(SIGALRM, &act, NULL);
156
157 memset(&act, 0, sizeof(act));
158 act.sa_handler = sig_int;
159 act.sa_flags = SA_RESTART;
160 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200161
162 memset(&act, 0, sizeof(act));
163 act.sa_handler = sig_ill;
164 act.sa_flags = SA_RESTART;
165 sigaction(SIGILL, &act, NULL);
Jens Axboe5f8f9722008-11-18 19:10:21 +0100166
167 memset(&act, 0, sizeof(act));
168 act.sa_handler = sig_quit;
169 act.sa_flags = SA_RESTART;
170 sigaction(SIGQUIT, &act, NULL);
Jens Axboe697a6062008-05-31 00:04:45 +0200171}
172
Jens Axboe993bf482008-11-14 13:04:53 +0100173static inline int should_check_rate(struct thread_data *td)
174{
Jens Axboee235d5f2009-01-19 12:49:48 +0100175 struct thread_options *o = &td->o;
Jens Axboe993bf482008-11-14 13:04:53 +0100176
Jens Axboee235d5f2009-01-19 12:49:48 +0100177 /*
178 * If some rate setting was given, we need to check it
179 */
180 if (o->rate || o->ratemin || o->rate_iops || o->rate_iops_min)
181 return 1;
182
183 return 0;
Jens Axboe993bf482008-11-14 13:04:53 +0100184}
185
Jens Axboe697a6062008-05-31 00:04:45 +0200186/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200187 * Check if we are above the minimum rate given.
188 */
Jens Axboeebac4652005-12-08 15:25:21 +0100189static int check_min_rate(struct thread_data *td, struct timeval *now)
190{
Jens Axboe09042002007-02-23 10:56:22 +0100191 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100192 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100193 unsigned long spent;
194 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100195
196 /*
197 * allow a 2 second settle period in the beginning
198 */
199 if (mtime_since(&td->start, now) < 2000)
200 return 0;
201
Jens Axboe4e991c22007-03-15 11:41:11 +0100202 if (td_read(td)) {
203 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100204 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100205 }
206 if (td_write(td)) {
207 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100208 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100209 }
Jens Axboe09042002007-02-23 10:56:22 +0100210
Jens Axboeebac4652005-12-08 15:25:21 +0100211 /*
212 * if rate blocks is set, sample is running
213 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100214 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100215 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100216 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100217 return 0;
218
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100219 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100220 /*
221 * check bandwidth specified rate
222 */
223 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100224 log_err("%s: min rate %u not met\n", td->o.name,
225 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100226 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100227 } else {
228 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100229 if (rate < td->o.ratemin ||
230 bytes < td->rate_bytes) {
231 log_err("%s: min rate %u not met, got"
232 " %luKiB/sec\n", td->o.name,
233 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100234 return 1;
235 }
236 }
237 } else {
238 /*
239 * checks iops specified rate
240 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100241 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100242 log_err("%s: min iops rate %u not met\n",
243 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100244 return 1;
245 } else {
246 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100247 if (rate < td->o.rate_iops_min ||
248 iops < td->rate_blocks) {
249 log_err("%s: min iops rate %u not met,"
250 " got %lu\n", td->o.name,
251 td->o.rate_iops_min,
252 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100253 }
Jens Axboe413dd452007-02-23 09:26:09 +0100254 }
Jens Axboeebac4652005-12-08 15:25:21 +0100255 }
256 }
257
Jens Axboe09042002007-02-23 10:56:22 +0100258 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100259 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100260 memcpy(&td->lastrate, now, sizeof(*now));
261 return 0;
262}
263
264static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
265{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100266 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100267 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100268 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100269 return 1;
270
271 return 0;
272}
273
Jens Axboe906c8d72006-06-13 09:37:56 +0200274/*
275 * When job exits, we can cancel the in-flight IO if we are using async
276 * io. Attempt to do so.
277 */
Jens Axboeebac4652005-12-08 15:25:21 +0100278static void cleanup_pending_aio(struct thread_data *td)
279{
Jens Axboe01743ee2008-06-02 12:19:19 +0200280 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100281 struct io_u *io_u;
282 int r;
283
284 /*
285 * get immediately available events, if any
286 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100287 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100288 if (r < 0)
289 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100290
291 /*
292 * now cancel remaining active events
293 */
Jens Axboe2866c822006-10-09 15:57:48 +0200294 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200295 flist_for_each_safe(entry, n, &td->io_u_busylist) {
296 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100297
Jens Axboe0c6e7512007-02-22 11:19:39 +0100298 /*
299 * if the io_u isn't in flight, then that generally
300 * means someone leaked an io_u. complain but fix
301 * it up, so we don't stall here.
302 */
303 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
304 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100305 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100306 } else {
307 r = td->io_ops->cancel(td, io_u);
308 if (!r)
309 put_io_u(td, io_u);
310 }
Jens Axboeebac4652005-12-08 15:25:21 +0100311 }
312 }
313
Jens Axboe97601022007-02-18 12:47:29 +0100314 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100315 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100316}
317
Jens Axboe906c8d72006-06-13 09:37:56 +0200318/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200319 * Helper to handle the final sync of a file. Works just like the normal
320 * io path, just does everything sync.
321 */
322static int fio_io_sync(struct thread_data *td, struct fio_file *f)
323{
324 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200325 int ret;
326
327 if (!io_u)
328 return 1;
329
330 io_u->ddir = DDIR_SYNC;
331 io_u->file = f;
332
333 if (td_io_prep(td, io_u)) {
334 put_io_u(td, io_u);
335 return 1;
336 }
337
Jens Axboe755200a2007-02-19 13:08:12 +0100338requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200339 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100340 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100341 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200342 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200343 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100344 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100345 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100346 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100347 } else if (ret == FIO_Q_COMPLETED) {
348 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100349 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100350 return 1;
351 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200352
Jens Axboed7762cf2007-02-23 12:34:57 +0100353 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100354 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100355 } else if (ret == FIO_Q_BUSY) {
356 if (td_io_commit(td))
357 return 1;
358 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200359 }
360
361 return 0;
362}
363
Jens Axboe993bf482008-11-14 13:04:53 +0100364static inline void update_tv_cache(struct thread_data *td)
365{
366 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
367 fio_gettime(&td->tv_cache, NULL);
368}
369
Jens Axboe858a3d42006-10-24 14:54:44 +0200370/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100371 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200372 * reads the blocks back in, and checks the crc/md5 of the data.
373 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100374static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100375{
Jens Axboe53cdc682006-10-18 11:50:58 +0200376 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100377 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100378 int ret, min_events;
379 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200380
381 /*
382 * sync io first and invalidate cache, to make sure we really
383 * read from disk.
384 */
385 for_each_file(td, f, i) {
Jens Axboed6aed792009-06-03 08:41:15 +0200386 if (!fio_file_open(f))
Jens Axboe3d7b4852007-03-13 14:50:28 +0100387 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100388 if (fio_io_sync(td, f))
389 break;
390 if (file_invalidate_cache(td, f))
391 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200392 }
Jens Axboeebac4652005-12-08 15:25:21 +0100393
Jens Axboeb2fdda42007-02-20 20:52:51 +0100394 if (td->error)
395 return;
396
Jens Axboeebac4652005-12-08 15:25:21 +0100397 td_set_runstate(td, TD_VERIFYING);
398
Jens Axboe36167d82007-02-18 05:41:31 +0100399 io_u = NULL;
400 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200401 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100402
Jens Axboeebac4652005-12-08 15:25:21 +0100403 io_u = __get_io_u(td);
404 if (!io_u)
405 break;
406
Jens Axboe993bf482008-11-14 13:04:53 +0100407 update_tv_cache(td);
408
409 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe069c2912007-02-21 23:02:39 +0100410 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200411 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200412 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100413 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200414
Jens Axboe069c2912007-02-21 23:02:39 +0100415 if (get_next_verify(td, io_u)) {
416 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100417 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100418 }
Jens Axboeebac4652005-12-08 15:25:21 +0100419
Jens Axboe069c2912007-02-21 23:02:39 +0100420 if (td_io_prep(td, io_u)) {
421 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100422 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100423 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100424
425 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100426
Jens Axboe11786802007-03-05 18:33:22 +0100427 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100428 switch (ret) {
429 case FIO_Q_COMPLETED:
430 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100431 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100432 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100433 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200434 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100435
Jens Axboe9e9d1642007-03-12 09:37:46 +0100436 /*
437 * zero read, fail
438 */
439 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200440 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100441 put_io_u(td, io_u);
442 break;
443 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200444
Jens Axboe36167d82007-02-18 05:41:31 +0100445 io_u->xfer_buflen = io_u->resid;
446 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200447 io_u->offset += bytes;
448
Jens Axboe30061b92007-04-17 13:31:34 +0200449 td->ts.short_io_u[io_u->ddir]++;
450
Jens Axboed460eb32007-04-16 21:39:33 +0200451 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200452 goto sync_done;
453
Jens Axboe11786802007-03-05 18:33:22 +0100454 requeue_io_u(td, &io_u);
455 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200456sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100457 ret = io_u_sync_complete(td, io_u);
458 if (ret < 0)
459 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100460 }
Jens Axboe36167d82007-02-18 05:41:31 +0100461 continue;
462 case FIO_Q_QUEUED:
463 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100464 case FIO_Q_BUSY:
465 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100466 ret2 = td_io_commit(td);
467 if (ret2 < 0)
468 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100469 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100470 default:
471 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100472 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100473 break;
474 }
475
Jens Axboe99784632007-02-19 10:06:17 +0100476 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100477 break;
478
Jens Axboe3af6ef32007-02-18 06:57:43 +0100479 /*
480 * if we can queue more, do so. but check if there are
481 * completed io_u's first.
482 */
Jens Axboe49504212008-06-05 09:03:30 +0200483 full = queue_full(td) || ret == FIO_Q_BUSY;
484 if (full || !td->o.iodepth_batch_complete) {
485 min_events = td->o.iodepth_batch_complete;
486 if (full && !min_events)
Jens Axboe838bc702008-05-22 13:08:23 +0200487 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100488
Jens Axboe49504212008-06-05 09:03:30 +0200489 do {
490 /*
491 * Reap required number of io units, if any,
492 * and do the verification on them through
493 * the callback handler
494 */
495 if (io_u_queued_complete(td, min_events) < 0) {
496 ret = -1;
497 break;
498 }
499 } while (full && (td->cur_depth > td->o.iodepth_low));
500 }
501 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100502 break;
503 }
504
Jens Axboec01c0392007-02-26 12:43:42 +0100505 if (!td->error) {
506 min_events = td->cur_depth;
507
508 if (min_events)
509 ret = io_u_queued_complete(td, min_events);
510 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100511 cleanup_pending_aio(td);
512
513 td_set_runstate(td, TD_RUNNING);
514}
515
Jens Axboe32cd46a2006-06-07 13:40:40 +0200516/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200517 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200518 * and reaps them, checking for rate and errors along the way.
519 */
Jens Axboeebac4652005-12-08 15:25:21 +0100520static void do_io(struct thread_data *td)
521{
Jens Axboeebac4652005-12-08 15:25:21 +0100522 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100523 unsigned int i;
524 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100525
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200526 if (in_ramp_time(td))
527 td_set_runstate(td, TD_RAMP);
528 else
529 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200530
Jens Axboe7bb48f82007-03-27 15:30:28 +0200531 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100532 struct timeval comp_time;
533 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200534 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100535 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200536 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100537
538 if (td->terminate)
539 break;
540
Jens Axboe3d7c3912007-02-19 13:16:12 +0100541 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100542 if (!io_u)
543 break;
544
Jens Axboe993bf482008-11-14 13:04:53 +0100545 update_tv_cache(td);
Jens Axboe97601022007-02-18 12:47:29 +0100546
Jens Axboe993bf482008-11-14 13:04:53 +0100547 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe97601022007-02-18 12:47:29 +0100548 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200549 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100550 break;
551 }
Jens Axboe36167d82007-02-18 05:41:31 +0100552
Jens Axboeb67740d2007-07-26 12:22:30 +0200553 /*
554 * Add verification end_io handler, if asked to verify
555 * a previously written file.
556 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100557 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200558 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100559 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200560 } else if (in_ramp_time(td))
561 td_set_runstate(td, TD_RAMP);
562 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100563 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200564
Jens Axboe11786802007-03-05 18:33:22 +0100565 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100566 switch (ret) {
567 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100568 if (io_u->error)
569 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100570 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100571 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200572 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100573
Jens Axboe9e9d1642007-03-12 09:37:46 +0100574 /*
575 * zero read, fail
576 */
577 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200578 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100579 put_io_u(td, io_u);
580 break;
581 }
582
Jens Axboe36167d82007-02-18 05:41:31 +0100583 io_u->xfer_buflen = io_u->resid;
584 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200585 io_u->offset += bytes;
586
Jens Axboe30061b92007-04-17 13:31:34 +0200587 td->ts.short_io_u[io_u->ddir]++;
588
Jens Axboed460eb32007-04-16 21:39:33 +0200589 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200590 goto sync_done;
591
Jens Axboe11786802007-03-05 18:33:22 +0100592 requeue_io_u(td, &io_u);
593 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200594sync_done:
Jens Axboe993bf482008-11-14 13:04:53 +0100595 if (should_check_rate(td))
596 fio_gettime(&comp_time, NULL);
597
Jens Axboe11786802007-03-05 18:33:22 +0100598 bytes_done = io_u_sync_complete(td, io_u);
599 if (bytes_done < 0)
600 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100601 }
Jens Axboe36167d82007-02-18 05:41:31 +0100602 break;
603 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100604 /*
605 * if the engine doesn't have a commit hook,
606 * the io_u is really queued. if it does have such
607 * a hook, it has to call io_u_queued() itself.
608 */
609 if (td->io_ops->commit == NULL)
610 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100611 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100612 case FIO_Q_BUSY:
613 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100614 ret2 = td_io_commit(td);
615 if (ret2 < 0)
616 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100617 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100618 default:
619 assert(ret < 0);
620 put_io_u(td, io_u);
621 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100622 }
623
Jens Axboe99784632007-02-19 10:06:17 +0100624 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100625 break;
626
Jens Axboe97601022007-02-18 12:47:29 +0100627 /*
628 * See if we need to complete some commands
629 */
Jens Axboe49504212008-06-05 09:03:30 +0200630 full = queue_full(td) || ret == FIO_Q_BUSY;
631 if (full || !td->o.iodepth_batch_complete) {
632 min_evts = td->o.iodepth_batch_complete;
633 if (full && !min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100634 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200635
Jens Axboe993bf482008-11-14 13:04:53 +0100636 if (should_check_rate(td))
637 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200638
639 do {
640 ret = io_u_queued_complete(td, min_evts);
641 if (ret <= 0)
642 break;
643
644 bytes_done += ret;
645 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100646 }
647
Jens Axboe49504212008-06-05 09:03:30 +0200648 if (ret < 0)
649 break;
Jens Axboe97601022007-02-18 12:47:29 +0100650 if (!bytes_done)
651 continue;
652
Jens Axboeebac4652005-12-08 15:25:21 +0100653 /*
654 * the rate is batched for now, it should work for batches
655 * of completions except the very first one which may look
656 * a little bursty
657 */
Jens Axboe993bf482008-11-14 13:04:53 +0100658 if (!in_ramp_time(td) && should_check_rate(td)) {
659 usec = utime_since(&td->tv_cache, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100660
Jens Axboe721938a2008-09-10 09:46:16 +0200661 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100662
Jens Axboe721938a2008-09-10 09:46:16 +0200663 if (check_min_rate(td, &comp_time)) {
664 if (exitall_on_terminate)
665 terminate_threads(td->groupid);
666 td_verror(td, EIO, "check_min_rate");
667 break;
668 }
Jens Axboeebac4652005-12-08 15:25:21 +0100669 }
670
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100671 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100672 unsigned long long b;
673
674 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100675 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100676 int left;
677
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100678 if (td->o.thinktime_spin)
Jens Axboe6dd6f2c2008-12-10 13:24:12 +0100679 usec_spin(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100680
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100681 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100682 if (left)
683 usec_sleep(td, left);
684 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100685 }
Jens Axboeebac4652005-12-08 15:25:21 +0100686 }
687
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100688 if (td->o.fill_device && td->error == ENOSPC) {
689 td->error = 0;
690 td->terminate = 1;
691 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100692 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100693 struct fio_file *f;
694
Jens Axboec01c0392007-02-26 12:43:42 +0100695 i = td->cur_depth;
696 if (i)
697 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100698
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100699 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200700 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100701
702 for_each_file(td, f, i) {
Jens Axboed6aed792009-06-03 08:41:15 +0200703 if (!fio_file_open(f))
Jens Axboe3d7b4852007-03-13 14:50:28 +0100704 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200705 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100706 }
Jens Axboe84585002006-10-19 20:26:22 +0200707 }
Jens Axboec01c0392007-02-26 12:43:42 +0100708 } else
709 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100710
711 /*
712 * stop job if we failed doing any IO
713 */
714 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
715 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100716}
717
Jens Axboeebac4652005-12-08 15:25:21 +0100718static void cleanup_io_u(struct thread_data *td)
719{
Jens Axboe01743ee2008-06-02 12:19:19 +0200720 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100721 struct io_u *io_u;
722
Jens Axboe01743ee2008-06-02 12:19:19 +0200723 flist_for_each_safe(entry, n, &td->io_u_freelist) {
724 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100725
Jens Axboe01743ee2008-06-02 12:19:19 +0200726 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100727 free(io_u);
728 }
729
Jens Axboe2f9ade32006-10-20 11:25:52 +0200730 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100731}
732
733static int init_io_u(struct thread_data *td)
734{
735 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100736 unsigned int max_bs;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200737 int cl_align, i, max_units;
Jens Axboeebac4652005-12-08 15:25:21 +0100738 char *p;
739
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100740 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100741 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100742 td->orig_buffer_size = (unsigned long long) max_bs
743 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100744
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100745 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
746 unsigned long bs;
747
748 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
749 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
750 }
Jens Axboeebac4652005-12-08 15:25:21 +0100751
Jens Axboec3990642007-07-19 10:17:09 +0200752 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
753 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
754 return 1;
755 }
756
Jens Axboe2f9ade32006-10-20 11:25:52 +0200757 if (allocate_io_mem(td))
758 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100759
Jens Axboeb3f378e2007-07-20 12:39:22 +0200760 if (td->o.odirect)
761 p = ALIGN(td->orig_buffer);
762 else
763 p = td->orig_buffer;
764
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200765 cl_align = os_cache_line_size();
766
Jens Axboeebac4652005-12-08 15:25:21 +0100767 for (i = 0; i < max_units; i++) {
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200768 void *ptr;
769
Jens Axboedb1cd902007-04-26 13:47:07 +0200770 if (td->terminate)
771 return 1;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200772
773 if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) {
774 log_err("fio: posix_memalign=%s\n", strerror(errno));
775 break;
776 }
777
778 io_u = ptr;
Jens Axboeebac4652005-12-08 15:25:21 +0100779 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200780 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100781
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200782 if (!(td->io_ops->flags & FIO_NOIO)) {
783 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200784
Jens Axboe5973caf2008-05-21 19:52:35 +0200785 if (td_write(td) && !td->o.refill_buffers)
786 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200787 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100788
Jens Axboeb1ff3402006-02-17 11:35:58 +0100789 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100790 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200791 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100792 }
793
794 return 0;
795}
796
Jens Axboeda867742006-06-06 10:39:10 -0700797static int switch_ioscheduler(struct thread_data *td)
798{
799 char tmp[256], tmp2[128];
800 FILE *f;
801 int ret;
802
Jens Axboeba0fbe12007-03-09 14:34:23 +0100803 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200804 return 0;
805
Jens Axboeda867742006-06-06 10:39:10 -0700806 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
807
808 f = fopen(tmp, "r+");
809 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200810 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100811 log_err("fio: os or kernel doesn't support IO scheduler"
812 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200813 return 0;
814 }
815 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700816 return 1;
817 }
818
819 /*
820 * Set io scheduler.
821 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100822 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700823 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100824 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700825 fclose(f);
826 return 1;
827 }
828
829 rewind(f);
830
831 /*
832 * Read back and check that the selected scheduler is now the default.
833 */
834 ret = fread(tmp, 1, sizeof(tmp), f);
835 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100836 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700837 fclose(f);
838 return 1;
839 }
840
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100841 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700842 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100843 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100844 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700845 fclose(f);
846 return 1;
847 }
848
849 fclose(f);
850 return 0;
851}
852
Jens Axboe492158c2007-05-24 10:32:47 +0200853static int keep_running(struct thread_data *td)
854{
855 unsigned long long io_done;
856
Jens Axboe20e354e2007-07-23 14:36:16 +0200857 if (td->done)
858 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200859 if (td->o.time_based)
860 return 1;
861 if (td->o.loops) {
862 td->o.loops--;
863 return 1;
864 }
865
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100866 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
867 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200868 if (io_done < td->o.size)
869 return 1;
870
871 return 0;
872}
873
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200874static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100875{
Jens Axboe756867b2007-03-06 15:19:24 +0100876 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100877 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100878 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100879 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100880 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100881 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100882
Jens Axboec1324df2007-02-19 19:06:41 +0100883 td->last_was_sync = 0;
884
Jens Axboe2ba1c292008-02-01 13:16:38 +0100885 /*
886 * reset file done count if we are to start over
887 */
888 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200889 td->nr_done_files = 0;
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200890
891 /*
892 * Set the same seed to get repeatable runs
893 */
894 td_fill_rand_seeds(td);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200895}
896
897void reset_all_stats(struct thread_data *td)
898{
899 struct timeval tv;
900 int i;
901
902 reset_io_counters(td);
903
904 for (i = 0; i < 2; i++) {
905 td->io_bytes[i] = 0;
906 td->io_blocks[i] = 0;
907 td->io_issues[i] = 0;
908 td->ts.total_io_u[i] = 0;
909 }
910
911 fio_gettime(&tv, NULL);
912 memcpy(&td->epoch, &tv, sizeof(tv));
913 memcpy(&td->start, &tv, sizeof(tv));
914}
915
Jens Axboedf9c26b2009-03-05 10:13:58 +0100916static void clear_io_state(struct thread_data *td)
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200917{
918 struct fio_file *f;
919 unsigned int i;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200920
921 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +0200922
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100923 close_files(td);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100924 for_each_file(td, f, i)
Jens Axboed6aed792009-06-03 08:41:15 +0200925 fio_file_clear_done(f);
Jens Axboeebac4652005-12-08 15:25:21 +0100926}
927
Jens Axboe398e8c42009-05-20 12:10:02 +0200928static int exec_string(const char *string)
929{
930 int ret, newlen = strlen(string) + 1 + 8;
931 char *str;
932
933 str = malloc(newlen);
934 sprintf(str, "sh -c %s", string);
935
936 ret = system(str);
937 if (ret == -1)
938 log_err("fio: exec of cmd <%s> failed\n", str);
939
940 free(str);
941 return ret;
942}
943
Jens Axboe906c8d72006-06-13 09:37:56 +0200944/*
945 * Entry point for the thread based jobs. The process based jobs end up
946 * here as well, after a little setup.
947 */
Jens Axboeebac4652005-12-08 15:25:21 +0100948static void *thread_main(void *data)
949{
Shawn Lewis10927312007-11-21 09:38:13 +0100950 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100951 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100952 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100953
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100954 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100955 setsid();
956
957 td->pid = getpid();
958
Jens Axboe5921e802008-05-30 15:02:38 +0200959 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100960
Jens Axboe01743ee2008-06-02 12:19:19 +0200961 INIT_FLIST_HEAD(&td->io_u_freelist);
962 INIT_FLIST_HEAD(&td->io_u_busylist);
963 INIT_FLIST_HEAD(&td->io_u_requeues);
964 INIT_FLIST_HEAD(&td->io_log_list);
965 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200966 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200967
Jens Axboe75154842006-06-01 13:56:09 +0200968 td_set_runstate(td, TD_INITIALIZED);
Jens Axboe29adda32009-01-05 19:06:39 +0100969 dprint(FD_MUTEX, "up startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +0100970 fio_mutex_up(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +0100971 dprint(FD_MUTEX, "wait on td->mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +0100972 fio_mutex_down(td->mutex);
Jens Axboe29adda32009-01-05 19:06:39 +0100973 dprint(FD_MUTEX, "done waiting on td->mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100974
Jens Axboe37f56872007-03-09 12:42:35 +0100975 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100976 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100977 * eating a file descriptor
978 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100979 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100980
Jens Axboed84f8d42007-05-16 08:47:46 +0200981 /*
982 * May alter parameters that init_io_u() will use, so we need to
983 * do this first.
984 */
985 if (init_iolog(td))
986 goto err;
987
Jens Axboedb1cd902007-04-26 13:47:07 +0200988 if (init_io_u(td))
989 goto err;
990
Jens Axboe375b2692007-05-22 09:13:02 +0200991 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200992 td_verror(td, errno, "cpu_set_affinity");
993 goto err;
994 }
995
Jens Axboef4844702008-12-09 08:04:29 +0100996 /*
997 * If we have a gettimeofday() thread, make sure we exclude that
998 * thread from this job
999 */
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001000 if (td->o.gtod_cpu) {
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001001 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1002 if (fio_setaffinity(td) == -1) {
1003 td_verror(td, errno, "cpu_set_affinity");
1004 goto err;
1005 }
1006 }
1007
Jens Axboeac684782007-11-08 08:29:07 +01001008 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +02001009 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1010 td_verror(td, errno, "ioprio_set");
1011 goto err;
1012 }
1013 }
1014
1015 if (nice(td->o.nice) == -1) {
1016 td_verror(td, errno, "nice");
1017 goto err;
1018 }
1019
1020 if (td->o.ioscheduler && switch_ioscheduler(td))
1021 goto err;
1022
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001023 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +01001024 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +01001025
1026 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +01001027 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +01001028
Jens Axboe68727072007-03-15 20:49:25 +01001029 if (init_random_map(td))
1030 goto err;
1031
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001032 if (td->o.exec_prerun) {
Jens Axboe398e8c42009-05-20 12:10:02 +02001033 if (exec_string(td->o.exec_prerun))
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001034 goto err;
1035 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001036
Zhang, Yanminafad68f2009-05-20 11:30:55 +02001037 if (td->o.pre_read) {
1038 if (pre_read_files(td) < 0)
1039 goto err;
1040 }
1041
Jens Axboe69008992006-11-24 13:12:56 +01001042 fio_gettime(&td->epoch, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001043 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +01001044
1045 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +01001046 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001047 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +01001048 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001049 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboe993bf482008-11-14 13:04:53 +01001050 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +01001051
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001052 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001053 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1054 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +01001055
Jens Axboedf9c26b2009-03-05 10:13:58 +01001056 if (clear_state)
1057 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001058
Jens Axboeebac4652005-12-08 15:25:21 +01001059 prune_io_piece_log(td);
1060
Jens Axboeba0fbe12007-03-09 14:34:23 +01001061 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001062
Jens Axboea978ba62007-03-08 14:29:03 +01001063 clear_state = 1;
1064
Jens Axboe38d77ca2007-03-21 14:20:20 +01001065 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1066 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001067 elapsed = utime_since(&td->start,
1068 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001069 else
1070 elapsed = utime_since_now(&td->start);
1071
1072 runtime[DDIR_READ] += elapsed;
1073 }
1074 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1075 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001076 elapsed = utime_since(&td->start,
1077 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001078 else
1079 elapsed = utime_since_now(&td->start);
1080
1081 runtime[DDIR_WRITE] += elapsed;
1082 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001083
Jens Axboeebac4652005-12-08 15:25:21 +01001084 if (td->error || td->terminate)
1085 break;
1086
Shawn Lewise84c73a2007-08-02 22:19:32 +02001087 if (!td->o.do_verify ||
1088 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001089 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001090 continue;
1091
Jens Axboedf9c26b2009-03-05 10:13:58 +01001092 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001093
Jens Axboe02bcaa82006-11-24 10:42:00 +01001094 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001095
1096 do_verify(td);
1097
Jens Axboe69008992006-11-24 13:12:56 +01001098 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001099
1100 if (td->error || td->terminate)
1101 break;
1102 }
1103
Jens Axboe36dff962006-11-24 13:29:20 +01001104 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001105 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1106 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001107 td->ts.total_run_time = mtime_since_now(&td->epoch);
1108 td->ts.io_bytes[0] = td->io_bytes[0];
1109 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001110
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001111 fio_mutex_down(writeout_mutex);
Jens Axboee3cedca2008-11-19 19:57:52 +01001112 if (td->ts.bw_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001113 if (td->o.bw_log_file) {
1114 finish_log_named(td, td->ts.bw_log,
1115 td->o.bw_log_file, "bw");
1116 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001117 finish_log(td, td->ts.bw_log, "bw");
1118 }
1119 if (td->ts.slat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001120 if (td->o.lat_log_file) {
1121 finish_log_named(td, td->ts.slat_log,
Radha Ramachandran1ca95732009-04-29 07:23:26 +02001122 td->o.lat_log_file, "slat");
Jens Axboef4844702008-12-09 08:04:29 +01001123 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001124 finish_log(td, td->ts.slat_log, "slat");
1125 }
1126 if (td->ts.clat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001127 if (td->o.lat_log_file) {
1128 finish_log_named(td, td->ts.clat_log,
1129 td->o.lat_log_file, "clat");
1130 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001131 finish_log(td, td->ts.clat_log, "clat");
1132 }
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001133 fio_mutex_up(writeout_mutex);
Jens Axboe398e8c42009-05-20 12:10:02 +02001134 if (td->o.exec_postrun)
1135 exec_string(td->o.exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +01001136
1137 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001138 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001139
1140err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001141 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001142 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001143 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001144 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001145 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001146 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001147
Jens Axboed2ce18b2008-12-12 20:51:40 +01001148 if (td->o.cpumask_set) {
1149 int ret = fio_cpuset_exit(&td->o.cpumask);
1150
1151 td_verror(td, ret, "fio_cpuset_exit");
1152 }
Jens Axboe4e78e402008-12-12 20:40:27 +01001153
Jens Axboef29b25a2007-07-23 08:56:43 +02001154 /*
1155 * do this very late, it will log file closing as well
1156 */
1157 if (td->o.write_iolog_file)
1158 write_iolog_close(td);
1159
Jens Axboed23bb322007-03-23 15:57:56 +01001160 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001161 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001162 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001163}
1164
Jens Axboe906c8d72006-06-13 09:37:56 +02001165/*
1166 * We cannot pass the td data into a forked process, so attach the td and
1167 * pass it to the thread worker.
1168 */
Jens Axboea6418142007-02-17 04:47:18 +01001169static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001170{
1171 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001172 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001173
1174 data = shmat(shmid, NULL, 0);
1175 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001176 int __err = errno;
1177
Jens Axboeebac4652005-12-08 15:25:21 +01001178 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001179 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001180 }
1181
1182 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001183 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001184 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001185 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001186}
1187
Jens Axboe906c8d72006-06-13 09:37:56 +02001188/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001189 * Run over the job map and reap the threads that have exited, if any.
1190 */
Jens Axboeebac4652005-12-08 15:25:21 +01001191static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1192{
Jens Axboe34572e22006-10-20 09:33:18 +02001193 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001194 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001195
1196 /*
1197 * reap exited threads (TD_EXITED -> TD_REAPED)
1198 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001199 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001200 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001201 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001202
Jens Axboe84585002006-10-19 20:26:22 +02001203 /*
1204 * ->io_ops is NULL for a thread that has closed its
1205 * io engine
1206 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001207 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001208 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001209 else
1210 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001211
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001212 if (!td->pid) {
1213 pending++;
1214 continue;
1215 }
1216 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001217 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001218 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001219 if (td->runstate == TD_EXITED) {
1220 td_set_runstate(td, TD_REAPED);
1221 goto reaped;
1222 }
1223 continue;
1224 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001225
1226 flags = WNOHANG;
1227 if (td->runstate == TD_EXITED)
1228 flags = 0;
1229
1230 /*
1231 * check if someone quit or got killed in an unusual way
1232 */
1233 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001234 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001235 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001236 log_err("fio: pid=%d disappeared %d\n",
1237 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001238 td_set_runstate(td, TD_REAPED);
1239 goto reaped;
1240 }
1241 perror("waitpid");
1242 } else if (ret == td->pid) {
1243 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001244 int sig = WTERMSIG(status);
1245
Jens Axboed9244942007-03-05 12:32:32 +01001246 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001247 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001248 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001249 td_set_runstate(td, TD_REAPED);
1250 goto reaped;
1251 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001252 if (WIFEXITED(status)) {
1253 if (WEXITSTATUS(status) && !td->error)
1254 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001255
Jens Axboea2f77c92007-02-22 11:53:00 +01001256 td_set_runstate(td, TD_REAPED);
1257 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001258 }
1259 }
Jens Axboeebac4652005-12-08 15:25:21 +01001260
Jens Axboea2f77c92007-02-22 11:53:00 +01001261 /*
1262 * thread is not dead, continue
1263 */
gurudas paie48676b2007-04-11 12:44:27 +02001264 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001265 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001266reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001267 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001268 (*m_rate) -= td->o.ratemin;
1269 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001270 if (!td->pid)
1271 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001272
1273 if (td->error)
1274 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001275
1276 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001277 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001278
Jens Axboe1f809d12007-10-25 18:34:02 +02001279 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001280 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001281}
1282
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001283static void *gtod_thread_main(void *data)
1284{
1285 fio_mutex_up(startup_mutex);
1286
1287 /*
1288 * As long as we have jobs around, update the clock. It would be nice
1289 * to have some way of NOT hammering that CPU with gettimeofday(),
1290 * but I'm not sure what to use outside of a simple CPU nop to relax
1291 * it - we don't want to lose precision.
1292 */
1293 while (threads) {
1294 fio_gtod_update();
1295 nop;
1296 }
1297
1298 return NULL;
1299}
1300
1301static int fio_start_gtod_thread(void)
1302{
Jens Axboef42c1532009-01-05 19:07:13 +01001303 int ret;
1304
1305 ret = pthread_create(&gtod_thread, NULL, gtod_thread_main, NULL);
1306 if (ret) {
1307 log_err("Can't create gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001308 return 1;
1309 }
Jens Axboef42c1532009-01-05 19:07:13 +01001310
1311 ret = pthread_detach(gtod_thread);
1312 if (ret) {
1313 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001314 return 1;
1315 }
1316
Jens Axboe29adda32009-01-05 19:06:39 +01001317 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001318 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001319 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001320 return 0;
1321}
1322
Jens Axboe906c8d72006-06-13 09:37:56 +02001323/*
1324 * Main function for kicking off and reaping jobs, as needed.
1325 */
Jens Axboeebac4652005-12-08 15:25:21 +01001326static void run_threads(void)
1327{
Jens Axboeebac4652005-12-08 15:25:21 +01001328 struct thread_data *td;
1329 unsigned long spent;
1330 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001331
Jens Axboe2f9ade32006-10-20 11:25:52 +02001332 if (fio_pin_memory())
1333 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001334
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001335 if (fio_gtod_offload && fio_start_gtod_thread())
1336 return;
1337
Jens Axboec6ae0a52006-06-12 11:04:44 +02001338 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001339 printf("Starting ");
1340 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001341 printf("%d thread%s", nr_thread,
1342 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001343 if (nr_process) {
1344 if (nr_thread)
1345 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001346 printf("%d process%s", nr_process,
1347 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001348 }
1349 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001350 fflush(stdout);
1351 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001352
Jens Axboe697a6062008-05-31 00:04:45 +02001353 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001354
Jens Axboeebac4652005-12-08 15:25:21 +01001355 todo = thread_number;
1356 nr_running = 0;
1357 nr_started = 0;
1358 m_rate = t_rate = 0;
1359
Jens Axboe34572e22006-10-20 09:33:18 +02001360 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001361 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001362
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001363 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001364 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001365 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001366 }
Jens Axboeebac4652005-12-08 15:25:21 +01001367
1368 /*
1369 * do file setup here so it happens sequentially,
1370 * we don't want X number of threads getting their
1371 * client data interspersed on disk
1372 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001373 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001374 exit_value++;
1375 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001376 log_err("fio: pid=%d, err=%d/%s\n",
1377 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001378 td_set_runstate(td, TD_REAPED);
1379 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001380 } else {
1381 struct fio_file *f;
1382 unsigned int i;
1383
1384 /*
1385 * for sharing to work, each job must always open
1386 * its own files. so close them, if we opened them
1387 * for creation
1388 */
1389 for_each_file(td, f, i)
1390 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001391 }
Jens Axboe380cf262007-01-11 14:01:27 +01001392
1393 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001394 }
1395
Jens Axboea2f77c92007-02-22 11:53:00 +01001396 set_genesis_time();
1397
Jens Axboeebac4652005-12-08 15:25:21 +01001398 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001399 struct thread_data *map[MAX_JOBS];
1400 struct timeval this_start;
1401 int this_jobs = 0, left;
1402
Jens Axboeebac4652005-12-08 15:25:21 +01001403 /*
1404 * create threads (TD_NOT_CREATED -> TD_CREATED)
1405 */
Jens Axboe34572e22006-10-20 09:33:18 +02001406 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001407 if (td->runstate != TD_NOT_CREATED)
1408 continue;
1409
1410 /*
1411 * never got a chance to start, killed by other
1412 * thread for some reason
1413 */
1414 if (td->terminate) {
1415 todo--;
1416 continue;
1417 }
1418
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001419 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001420 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001421
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001422 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001423 continue;
1424 }
1425
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001426 if (td->o.stonewall && (nr_started || nr_running)) {
1427 dprint(FD_PROCESS, "%s: stonewall wait\n",
1428 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001429 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001430 }
Jens Axboeebac4652005-12-08 15:25:21 +01001431
Jens Axboe75154842006-06-01 13:56:09 +02001432 /*
1433 * Set state to created. Thread will transition
1434 * to TD_INITIALIZED when it's done setting up.
1435 */
Jens Axboeebac4652005-12-08 15:25:21 +01001436 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001437 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001438 nr_started++;
1439
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001440 if (td->o.use_thread) {
Jens Axboef42c1532009-01-05 19:07:13 +01001441 int ret;
1442
Jens Axboeee56ad52008-02-01 10:30:20 +01001443 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboef42c1532009-01-05 19:07:13 +01001444 ret = pthread_create(&td->thread, NULL,
1445 thread_main, td);
1446 if (ret) {
1447 log_err("pthread_create: %s\n",
1448 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001449 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001450 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001451 }
Jens Axboef42c1532009-01-05 19:07:13 +01001452 ret = pthread_detach(td->thread);
1453 if (ret)
1454 log_err("pthread_detach: %s",
1455 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001456 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001457 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001458 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001459 pid = fork();
1460 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001461 int ret = fork_main(shm_id, i);
1462
Jens Axboe5e1d3062008-05-23 11:55:53 +02001463 _exit(ret);
1464 } else if (i == fio_debug_jobno)
1465 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001466 }
Jens Axboe29adda32009-01-05 19:06:39 +01001467 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +01001468 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001469 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001470 }
1471
1472 /*
Jens Axboe75154842006-06-01 13:56:09 +02001473 * Wait for the started threads to transition to
1474 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001475 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001476 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001477 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001478 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001479 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1480 break;
1481
1482 usleep(100000);
1483
1484 for (i = 0; i < this_jobs; i++) {
1485 td = map[i];
1486 if (!td)
1487 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001488 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001489 map[i] = NULL;
1490 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001491 } else if (td->runstate >= TD_EXITED) {
1492 map[i] = NULL;
1493 left--;
1494 todo--;
1495 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001496 }
1497 }
1498 }
1499
1500 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001501 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001502 for (i = 0; i < this_jobs; i++) {
1503 td = map[i];
1504 if (!td)
1505 continue;
1506 kill(td->pid, SIGTERM);
1507 }
1508 break;
1509 }
1510
1511 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001512 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001513 */
Jens Axboe34572e22006-10-20 09:33:18 +02001514 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001515 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001516 continue;
1517
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001518 if (in_ramp_time(td))
1519 td_set_runstate(td, TD_RAMP);
1520 else
1521 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001522 nr_running++;
1523 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001524 m_rate += td->o.ratemin;
1525 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001526 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001527 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001528 }
1529
1530 reap_threads(&nr_running, &t_rate, &m_rate);
1531
1532 if (todo)
1533 usleep(100000);
1534 }
1535
1536 while (nr_running) {
1537 reap_threads(&nr_running, &t_rate, &m_rate);
1538 usleep(10000);
1539 }
1540
1541 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001542 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001543}
1544
Jens Axboeebac4652005-12-08 15:25:21 +01001545int main(int argc, char *argv[])
1546{
Jens Axboe29d610e2007-02-06 13:25:33 +01001547 long ps;
1548
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001549 sinit();
1550
Jens Axboedbe11252007-02-11 00:19:51 +01001551 /*
1552 * We need locale for number printing, if it isn't set then just
1553 * go with the US format.
1554 */
1555 if (!getenv("LC_NUMERIC"))
1556 setlocale(LC_NUMERIC, "en_US");
1557
Jens Axboeebac4652005-12-08 15:25:21 +01001558 if (parse_options(argc, argv))
1559 return 1;
1560
Jens Axboe4b472fa2007-04-04 11:04:34 +02001561 if (!thread_number)
1562 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001563
Jens Axboe29d610e2007-02-06 13:25:33 +01001564 ps = sysconf(_SC_PAGESIZE);
1565 if (ps < 0) {
1566 log_err("Failed to get page size\n");
1567 return 1;
1568 }
1569
Jens Axboecfc99db2007-03-14 10:34:47 +01001570 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001571 page_mask = ps - 1;
1572
Jens Axboebb3884d2007-01-17 17:23:11 +11001573 if (write_bw_log) {
1574 setup_log(&agg_io_log[DDIR_READ]);
1575 setup_log(&agg_io_log[DDIR_WRITE]);
1576 }
1577
Jens Axboecdd18ad2008-02-27 18:58:00 +01001578 startup_mutex = fio_mutex_init(0);
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001579 writeout_mutex = fio_mutex_init(1);
Jens Axboe07739b52007-03-08 20:25:46 +01001580
Jens Axboea2f77c92007-02-22 11:53:00 +01001581 set_genesis_time();
1582
Jens Axboeccb0fa22008-05-30 23:18:00 +02001583 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001584
1585 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001586
Jens Axboebb3884d2007-01-17 17:23:11 +11001587 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001588 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001589 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001590 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1591 __finish_log(agg_io_log[DDIR_WRITE],
1592 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001593 }
1594 }
Jens Axboeebac4652005-12-08 15:25:21 +01001595
Jens Axboecdd18ad2008-02-27 18:58:00 +01001596 fio_mutex_remove(startup_mutex);
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001597 fio_mutex_remove(writeout_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001598 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001599}