blob: 1aba82ded2aed00396f9b76e9926000de8347f54 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
Jens Axboeaae22ca2006-09-05 10:46:22 +02005 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboeebac4652005-12-08 15:25:21 +01006 *
Jens Axboe8e9fe632006-10-24 15:11:09 +02007 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
Jens Axboeebac4652005-12-08 15:25:21 +010010 * This program is free software; you can redistribute it and/or modify
Jens Axboe8e9fe632006-10-24 15:11:09 +020011 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Jens Axboeebac4652005-12-08 15:25:21 +010013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <signal.h>
28#include <time.h>
Jens Axboedbe11252007-02-11 00:19:51 +010029#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010030#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010031#include <sys/stat.h>
32#include <sys/wait.h>
33#include <sys/ipc.h>
34#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010035#include <sys/mman.h>
36
37#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020038#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010039#include "smalloc.h"
Jens Axboeebac4652005-12-08 15:25:21 +010040
Jens Axboecfc99db2007-03-14 10:34:47 +010041unsigned long page_mask;
42unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010043#define ALIGN(buf) \
44 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010045
46int groupid = 0;
47int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010048int nr_process = 0;
49int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010050int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020051int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020052unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010053
Jens Axboecdd18ad2008-02-27 18:58:00 +010054static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010055static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010056static int exit_value;
Jens Axboeccb0fa22008-05-30 23:18:00 +020057static struct itimerval itimer;
Jens Axboebe4ecfd2008-12-08 14:10:52 +010058static pthread_t gtod_thread;
Jens Axboeebac4652005-12-08 15:25:21 +010059
Jens Axboebb3884d2007-01-17 17:23:11 +110060struct io_log *agg_io_log[2];
61
Jens Axboeebac4652005-12-08 15:25:21 +010062#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020063#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010064
Jens Axboeb29ee5b2008-09-11 10:17:26 +020065void td_set_runstate(struct thread_data *td, int runstate)
Jens Axboe6ce15a32007-01-12 09:04:41 +010066{
Jens Axboed8fab1b2008-03-06 10:25:17 +010067 if (td->runstate == runstate)
68 return;
69
Jens Axboe5921e802008-05-30 15:02:38 +020070 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
71 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010072 td->runstate = runstate;
73}
74
Jens Axboe390c40e2007-03-05 12:35:29 +010075static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010076{
Jens Axboe34572e22006-10-20 09:33:18 +020077 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010078 int i;
79
Jens Axboe6f88bcb2008-03-19 10:29:07 +010080 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
81
Jens Axboe34572e22006-10-20 09:33:18 +020082 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010083 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010084 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +020085 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010086 td->terminate = 1;
87 td->o.start_delay = 0;
88
Jens Axboe7a93ab12007-04-18 12:25:41 +020089 /*
90 * if the thread is running, just let it exit
91 */
92 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010093 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010094 else {
95 struct ioengine_ops *ops = td->io_ops;
96
97 if (ops && (ops->flags & FIO_SIGQUIT))
98 kill(td->pid, SIGQUIT);
99 }
Jens Axboeebac4652005-12-08 15:25:21 +0100100 }
101 }
102}
103
Jens Axboeccb0fa22008-05-30 23:18:00 +0200104static void status_timer_arm(void)
105{
106 itimer.it_value.tv_sec = 0;
107 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
108 setitimer(ITIMER_REAL, &itimer, NULL);
109}
110
Jens Axboe02444ad2008-10-07 11:33:00 +0200111static void sig_alrm(int fio_unused sig)
Jens Axboeccb0fa22008-05-30 23:18:00 +0200112{
Jens Axboe697a6062008-05-31 00:04:45 +0200113 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100114 update_io_ticks();
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100115 print_thread_status();
Jens Axboeccb0fa22008-05-30 23:18:00 +0200116 status_timer_arm();
Jens Axboe697a6062008-05-31 00:04:45 +0200117 }
118}
119
Jens Axboe5f8f9722008-11-18 19:10:21 +0100120/*
121 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
122 */
123static void sig_quit(int sig)
124{
125}
126
Jens Axboe697a6062008-05-31 00:04:45 +0200127static void sig_int(int sig)
128{
129 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100130 printf("\nfio: terminating on signal %d\n", sig);
131 fflush(stdout);
132 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100133 }
134}
135
Jens Axboe02444ad2008-10-07 11:33:00 +0200136static void sig_ill(int fio_unused sig)
Jens Axboede79c912008-08-04 21:43:55 +0200137{
138 if (!threads)
139 return;
140
141 log_err("fio: illegal instruction. your cpu does not support "
142 "the sse4.2 instruction for crc32c\n");
143 terminate_threads(TERMINATE_ALL);
144 exit(4);
145}
146
Jens Axboe697a6062008-05-31 00:04:45 +0200147static void set_sig_handlers(void)
148{
149 struct sigaction act;
150
151 memset(&act, 0, sizeof(act));
152 act.sa_handler = sig_alrm;
153 act.sa_flags = SA_RESTART;
154 sigaction(SIGALRM, &act, NULL);
155
156 memset(&act, 0, sizeof(act));
157 act.sa_handler = sig_int;
158 act.sa_flags = SA_RESTART;
159 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200160
161 memset(&act, 0, sizeof(act));
162 act.sa_handler = sig_ill;
163 act.sa_flags = SA_RESTART;
164 sigaction(SIGILL, &act, NULL);
Jens Axboe5f8f9722008-11-18 19:10:21 +0100165
166 memset(&act, 0, sizeof(act));
167 act.sa_handler = sig_quit;
168 act.sa_flags = SA_RESTART;
169 sigaction(SIGQUIT, &act, NULL);
Jens Axboe697a6062008-05-31 00:04:45 +0200170}
171
Jens Axboe993bf482008-11-14 13:04:53 +0100172static inline int should_check_rate(struct thread_data *td)
173{
Jens Axboee235d5f2009-01-19 12:49:48 +0100174 struct thread_options *o = &td->o;
Jens Axboe993bf482008-11-14 13:04:53 +0100175
Jens Axboee235d5f2009-01-19 12:49:48 +0100176 /*
177 * If some rate setting was given, we need to check it
178 */
179 if (o->rate || o->ratemin || o->rate_iops || o->rate_iops_min)
180 return 1;
181
182 return 0;
Jens Axboe993bf482008-11-14 13:04:53 +0100183}
184
Jens Axboe697a6062008-05-31 00:04:45 +0200185/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200186 * Check if we are above the minimum rate given.
187 */
Jens Axboeebac4652005-12-08 15:25:21 +0100188static int check_min_rate(struct thread_data *td, struct timeval *now)
189{
Jens Axboe09042002007-02-23 10:56:22 +0100190 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100191 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100192 unsigned long spent;
193 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100194
195 /*
196 * allow a 2 second settle period in the beginning
197 */
198 if (mtime_since(&td->start, now) < 2000)
199 return 0;
200
Jens Axboe4e991c22007-03-15 11:41:11 +0100201 if (td_read(td)) {
202 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100203 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100204 }
205 if (td_write(td)) {
206 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100207 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100208 }
Jens Axboe09042002007-02-23 10:56:22 +0100209
Jens Axboeebac4652005-12-08 15:25:21 +0100210 /*
211 * if rate blocks is set, sample is running
212 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100213 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100214 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100215 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100216 return 0;
217
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100218 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100219 /*
220 * check bandwidth specified rate
221 */
222 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100223 log_err("%s: min rate %u not met\n", td->o.name,
224 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100225 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100226 } else {
227 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100228 if (rate < td->o.ratemin ||
229 bytes < td->rate_bytes) {
230 log_err("%s: min rate %u not met, got"
231 " %luKiB/sec\n", td->o.name,
232 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100233 return 1;
234 }
235 }
236 } else {
237 /*
238 * checks iops specified rate
239 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100240 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100241 log_err("%s: min iops rate %u not met\n",
242 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100243 return 1;
244 } else {
245 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100246 if (rate < td->o.rate_iops_min ||
247 iops < td->rate_blocks) {
248 log_err("%s: min iops rate %u not met,"
249 " got %lu\n", td->o.name,
250 td->o.rate_iops_min,
251 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100252 }
Jens Axboe413dd452007-02-23 09:26:09 +0100253 }
Jens Axboeebac4652005-12-08 15:25:21 +0100254 }
255 }
256
Jens Axboe09042002007-02-23 10:56:22 +0100257 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100258 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100259 memcpy(&td->lastrate, now, sizeof(*now));
260 return 0;
261}
262
263static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
264{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100265 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100266 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100267 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100268 return 1;
269
270 return 0;
271}
272
Jens Axboe906c8d72006-06-13 09:37:56 +0200273/*
274 * When job exits, we can cancel the in-flight IO if we are using async
275 * io. Attempt to do so.
276 */
Jens Axboeebac4652005-12-08 15:25:21 +0100277static void cleanup_pending_aio(struct thread_data *td)
278{
Jens Axboe01743ee2008-06-02 12:19:19 +0200279 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100280 struct io_u *io_u;
281 int r;
282
283 /*
284 * get immediately available events, if any
285 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100286 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100287 if (r < 0)
288 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100289
290 /*
291 * now cancel remaining active events
292 */
Jens Axboe2866c822006-10-09 15:57:48 +0200293 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200294 flist_for_each_safe(entry, n, &td->io_u_busylist) {
295 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100296
Jens Axboe0c6e7512007-02-22 11:19:39 +0100297 /*
298 * if the io_u isn't in flight, then that generally
299 * means someone leaked an io_u. complain but fix
300 * it up, so we don't stall here.
301 */
302 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
303 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100304 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100305 } else {
306 r = td->io_ops->cancel(td, io_u);
307 if (!r)
308 put_io_u(td, io_u);
309 }
Jens Axboeebac4652005-12-08 15:25:21 +0100310 }
311 }
312
Jens Axboe97601022007-02-18 12:47:29 +0100313 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100314 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100315}
316
Jens Axboe906c8d72006-06-13 09:37:56 +0200317/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200318 * Helper to handle the final sync of a file. Works just like the normal
319 * io path, just does everything sync.
320 */
321static int fio_io_sync(struct thread_data *td, struct fio_file *f)
322{
323 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200324 int ret;
325
326 if (!io_u)
327 return 1;
328
329 io_u->ddir = DDIR_SYNC;
330 io_u->file = f;
331
332 if (td_io_prep(td, io_u)) {
333 put_io_u(td, io_u);
334 return 1;
335 }
336
Jens Axboe755200a2007-02-19 13:08:12 +0100337requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200338 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100339 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100340 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200341 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200342 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100343 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100344 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100345 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100346 } else if (ret == FIO_Q_COMPLETED) {
347 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100348 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100349 return 1;
350 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200351
Jens Axboed7762cf2007-02-23 12:34:57 +0100352 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100353 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100354 } else if (ret == FIO_Q_BUSY) {
355 if (td_io_commit(td))
356 return 1;
357 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200358 }
359
360 return 0;
361}
362
Jens Axboe993bf482008-11-14 13:04:53 +0100363static inline void update_tv_cache(struct thread_data *td)
364{
365 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
366 fio_gettime(&td->tv_cache, NULL);
367}
368
Jens Axboe858a3d42006-10-24 14:54:44 +0200369/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100370 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200371 * reads the blocks back in, and checks the crc/md5 of the data.
372 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100373static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100374{
Jens Axboe53cdc682006-10-18 11:50:58 +0200375 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100376 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100377 int ret, min_events;
378 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200379
380 /*
381 * sync io first and invalidate cache, to make sure we really
382 * read from disk.
383 */
384 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100385 if (!(f->flags & FIO_FILE_OPEN))
386 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100387 if (fio_io_sync(td, f))
388 break;
389 if (file_invalidate_cache(td, f))
390 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200391 }
Jens Axboeebac4652005-12-08 15:25:21 +0100392
Jens Axboeb2fdda42007-02-20 20:52:51 +0100393 if (td->error)
394 return;
395
Jens Axboeebac4652005-12-08 15:25:21 +0100396 td_set_runstate(td, TD_VERIFYING);
397
Jens Axboe36167d82007-02-18 05:41:31 +0100398 io_u = NULL;
399 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200400 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100401
Jens Axboeebac4652005-12-08 15:25:21 +0100402 io_u = __get_io_u(td);
403 if (!io_u)
404 break;
405
Jens Axboe993bf482008-11-14 13:04:53 +0100406 update_tv_cache(td);
407
408 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe069c2912007-02-21 23:02:39 +0100409 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200410 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200411 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100412 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200413
Jens Axboe069c2912007-02-21 23:02:39 +0100414 if (get_next_verify(td, io_u)) {
415 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100416 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100417 }
Jens Axboeebac4652005-12-08 15:25:21 +0100418
Jens Axboe069c2912007-02-21 23:02:39 +0100419 if (td_io_prep(td, io_u)) {
420 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100421 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100422 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100423
424 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100425
Jens Axboe11786802007-03-05 18:33:22 +0100426 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100427 switch (ret) {
428 case FIO_Q_COMPLETED:
429 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100430 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100431 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100432 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200433 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100434
Jens Axboe9e9d1642007-03-12 09:37:46 +0100435 /*
436 * zero read, fail
437 */
438 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200439 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100440 put_io_u(td, io_u);
441 break;
442 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200443
Jens Axboe36167d82007-02-18 05:41:31 +0100444 io_u->xfer_buflen = io_u->resid;
445 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200446 io_u->offset += bytes;
447
Jens Axboe30061b92007-04-17 13:31:34 +0200448 td->ts.short_io_u[io_u->ddir]++;
449
Jens Axboed460eb32007-04-16 21:39:33 +0200450 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200451 goto sync_done;
452
Jens Axboe11786802007-03-05 18:33:22 +0100453 requeue_io_u(td, &io_u);
454 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200455sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100456 ret = io_u_sync_complete(td, io_u);
457 if (ret < 0)
458 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100459 }
Jens Axboe36167d82007-02-18 05:41:31 +0100460 continue;
461 case FIO_Q_QUEUED:
462 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100463 case FIO_Q_BUSY:
464 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100465 ret2 = td_io_commit(td);
466 if (ret2 < 0)
467 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100468 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100469 default:
470 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100471 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100472 break;
473 }
474
Jens Axboe99784632007-02-19 10:06:17 +0100475 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100476 break;
477
Jens Axboe3af6ef32007-02-18 06:57:43 +0100478 /*
479 * if we can queue more, do so. but check if there are
480 * completed io_u's first.
481 */
Jens Axboe49504212008-06-05 09:03:30 +0200482 full = queue_full(td) || ret == FIO_Q_BUSY;
483 if (full || !td->o.iodepth_batch_complete) {
484 min_events = td->o.iodepth_batch_complete;
485 if (full && !min_events)
Jens Axboe838bc702008-05-22 13:08:23 +0200486 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100487
Jens Axboe49504212008-06-05 09:03:30 +0200488 do {
489 /*
490 * Reap required number of io units, if any,
491 * and do the verification on them through
492 * the callback handler
493 */
494 if (io_u_queued_complete(td, min_events) < 0) {
495 ret = -1;
496 break;
497 }
498 } while (full && (td->cur_depth > td->o.iodepth_low));
499 }
500 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100501 break;
502 }
503
Jens Axboec01c0392007-02-26 12:43:42 +0100504 if (!td->error) {
505 min_events = td->cur_depth;
506
507 if (min_events)
508 ret = io_u_queued_complete(td, min_events);
509 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100510 cleanup_pending_aio(td);
511
512 td_set_runstate(td, TD_RUNNING);
513}
514
Jens Axboe32cd46a2006-06-07 13:40:40 +0200515/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200516 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200517 * and reaps them, checking for rate and errors along the way.
518 */
Jens Axboeebac4652005-12-08 15:25:21 +0100519static void do_io(struct thread_data *td)
520{
Jens Axboeebac4652005-12-08 15:25:21 +0100521 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100522 unsigned int i;
523 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100524
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200525 if (in_ramp_time(td))
526 td_set_runstate(td, TD_RAMP);
527 else
528 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200529
Jens Axboe7bb48f82007-03-27 15:30:28 +0200530 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100531 struct timeval comp_time;
532 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200533 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100534 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200535 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100536
537 if (td->terminate)
538 break;
539
Jens Axboe3d7c3912007-02-19 13:16:12 +0100540 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100541 if (!io_u)
542 break;
543
Jens Axboe993bf482008-11-14 13:04:53 +0100544 update_tv_cache(td);
Jens Axboe97601022007-02-18 12:47:29 +0100545
Jens Axboe993bf482008-11-14 13:04:53 +0100546 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe97601022007-02-18 12:47:29 +0100547 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200548 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100549 break;
550 }
Jens Axboe36167d82007-02-18 05:41:31 +0100551
Jens Axboeb67740d2007-07-26 12:22:30 +0200552 /*
553 * Add verification end_io handler, if asked to verify
554 * a previously written file.
555 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100556 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200557 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100558 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200559 } else if (in_ramp_time(td))
560 td_set_runstate(td, TD_RAMP);
561 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100562 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200563
Jens Axboe11786802007-03-05 18:33:22 +0100564 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100565 switch (ret) {
566 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100567 if (io_u->error)
568 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100569 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100570 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200571 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100572
Jens Axboe9e9d1642007-03-12 09:37:46 +0100573 /*
574 * zero read, fail
575 */
576 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200577 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100578 put_io_u(td, io_u);
579 break;
580 }
581
Jens Axboe36167d82007-02-18 05:41:31 +0100582 io_u->xfer_buflen = io_u->resid;
583 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200584 io_u->offset += bytes;
585
Jens Axboe30061b92007-04-17 13:31:34 +0200586 td->ts.short_io_u[io_u->ddir]++;
587
Jens Axboed460eb32007-04-16 21:39:33 +0200588 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200589 goto sync_done;
590
Jens Axboe11786802007-03-05 18:33:22 +0100591 requeue_io_u(td, &io_u);
592 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200593sync_done:
Jens Axboe993bf482008-11-14 13:04:53 +0100594 if (should_check_rate(td))
595 fio_gettime(&comp_time, NULL);
596
Jens Axboe11786802007-03-05 18:33:22 +0100597 bytes_done = io_u_sync_complete(td, io_u);
598 if (bytes_done < 0)
599 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100600 }
Jens Axboe36167d82007-02-18 05:41:31 +0100601 break;
602 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100603 /*
604 * if the engine doesn't have a commit hook,
605 * the io_u is really queued. if it does have such
606 * a hook, it has to call io_u_queued() itself.
607 */
608 if (td->io_ops->commit == NULL)
609 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100610 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100611 case FIO_Q_BUSY:
612 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100613 ret2 = td_io_commit(td);
614 if (ret2 < 0)
615 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100616 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100617 default:
618 assert(ret < 0);
619 put_io_u(td, io_u);
620 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100621 }
622
Jens Axboe99784632007-02-19 10:06:17 +0100623 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100624 break;
625
Jens Axboe97601022007-02-18 12:47:29 +0100626 /*
627 * See if we need to complete some commands
628 */
Jens Axboe49504212008-06-05 09:03:30 +0200629 full = queue_full(td) || ret == FIO_Q_BUSY;
630 if (full || !td->o.iodepth_batch_complete) {
631 min_evts = td->o.iodepth_batch_complete;
632 if (full && !min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100633 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200634
Jens Axboe993bf482008-11-14 13:04:53 +0100635 if (should_check_rate(td))
636 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200637
638 do {
639 ret = io_u_queued_complete(td, min_evts);
640 if (ret <= 0)
641 break;
642
643 bytes_done += ret;
644 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100645 }
646
Jens Axboe49504212008-06-05 09:03:30 +0200647 if (ret < 0)
648 break;
Jens Axboe97601022007-02-18 12:47:29 +0100649 if (!bytes_done)
650 continue;
651
Jens Axboeebac4652005-12-08 15:25:21 +0100652 /*
653 * the rate is batched for now, it should work for batches
654 * of completions except the very first one which may look
655 * a little bursty
656 */
Jens Axboe993bf482008-11-14 13:04:53 +0100657 if (!in_ramp_time(td) && should_check_rate(td)) {
658 usec = utime_since(&td->tv_cache, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100659
Jens Axboe721938a2008-09-10 09:46:16 +0200660 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100661
Jens Axboe721938a2008-09-10 09:46:16 +0200662 if (check_min_rate(td, &comp_time)) {
663 if (exitall_on_terminate)
664 terminate_threads(td->groupid);
665 td_verror(td, EIO, "check_min_rate");
666 break;
667 }
Jens Axboeebac4652005-12-08 15:25:21 +0100668 }
669
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100670 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100671 unsigned long long b;
672
673 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100674 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100675 int left;
676
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100677 if (td->o.thinktime_spin)
Jens Axboe6dd6f2c2008-12-10 13:24:12 +0100678 usec_spin(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100679
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100680 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100681 if (left)
682 usec_sleep(td, left);
683 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100684 }
Jens Axboeebac4652005-12-08 15:25:21 +0100685 }
686
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100687 if (td->o.fill_device && td->error == ENOSPC) {
688 td->error = 0;
689 td->terminate = 1;
690 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100691 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100692 struct fio_file *f;
693
Jens Axboec01c0392007-02-26 12:43:42 +0100694 i = td->cur_depth;
695 if (i)
696 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100697
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100698 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200699 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100700
701 for_each_file(td, f, i) {
702 if (!(f->flags & FIO_FILE_OPEN))
703 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200704 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100705 }
Jens Axboe84585002006-10-19 20:26:22 +0200706 }
Jens Axboec01c0392007-02-26 12:43:42 +0100707 } else
708 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100709
710 /*
711 * stop job if we failed doing any IO
712 */
713 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
714 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100715}
716
Jens Axboeebac4652005-12-08 15:25:21 +0100717static void cleanup_io_u(struct thread_data *td)
718{
Jens Axboe01743ee2008-06-02 12:19:19 +0200719 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100720 struct io_u *io_u;
721
Jens Axboe01743ee2008-06-02 12:19:19 +0200722 flist_for_each_safe(entry, n, &td->io_u_freelist) {
723 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100724
Jens Axboe01743ee2008-06-02 12:19:19 +0200725 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100726 free(io_u);
727 }
728
Jens Axboe2f9ade32006-10-20 11:25:52 +0200729 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100730}
731
732static int init_io_u(struct thread_data *td)
733{
734 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100735 unsigned int max_bs;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200736 int cl_align, i, max_units;
Jens Axboeebac4652005-12-08 15:25:21 +0100737 char *p;
738
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100739 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100740 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100741 td->orig_buffer_size = (unsigned long long) max_bs
742 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100743
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100744 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
745 unsigned long bs;
746
747 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
748 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
749 }
Jens Axboeebac4652005-12-08 15:25:21 +0100750
Jens Axboec3990642007-07-19 10:17:09 +0200751 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
752 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
753 return 1;
754 }
755
Jens Axboe2f9ade32006-10-20 11:25:52 +0200756 if (allocate_io_mem(td))
757 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100758
Jens Axboeb3f378e2007-07-20 12:39:22 +0200759 if (td->o.odirect)
760 p = ALIGN(td->orig_buffer);
761 else
762 p = td->orig_buffer;
763
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200764 cl_align = os_cache_line_size();
765
Jens Axboeebac4652005-12-08 15:25:21 +0100766 for (i = 0; i < max_units; i++) {
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200767 void *ptr;
768
Jens Axboedb1cd902007-04-26 13:47:07 +0200769 if (td->terminate)
770 return 1;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200771
772 if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) {
773 log_err("fio: posix_memalign=%s\n", strerror(errno));
774 break;
775 }
776
777 io_u = ptr;
Jens Axboeebac4652005-12-08 15:25:21 +0100778 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200779 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100780
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200781 if (!(td->io_ops->flags & FIO_NOIO)) {
782 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200783
Jens Axboe5973caf2008-05-21 19:52:35 +0200784 if (td_write(td) && !td->o.refill_buffers)
785 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200786 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100787
Jens Axboeb1ff3402006-02-17 11:35:58 +0100788 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100789 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200790 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100791 }
792
793 return 0;
794}
795
Jens Axboeda867742006-06-06 10:39:10 -0700796static int switch_ioscheduler(struct thread_data *td)
797{
798 char tmp[256], tmp2[128];
799 FILE *f;
800 int ret;
801
Jens Axboeba0fbe12007-03-09 14:34:23 +0100802 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200803 return 0;
804
Jens Axboeda867742006-06-06 10:39:10 -0700805 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
806
807 f = fopen(tmp, "r+");
808 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200809 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100810 log_err("fio: os or kernel doesn't support IO scheduler"
811 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200812 return 0;
813 }
814 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700815 return 1;
816 }
817
818 /*
819 * Set io scheduler.
820 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100821 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700822 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100823 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700824 fclose(f);
825 return 1;
826 }
827
828 rewind(f);
829
830 /*
831 * Read back and check that the selected scheduler is now the default.
832 */
833 ret = fread(tmp, 1, sizeof(tmp), f);
834 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100835 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700836 fclose(f);
837 return 1;
838 }
839
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100840 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700841 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100842 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100843 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700844 fclose(f);
845 return 1;
846 }
847
848 fclose(f);
849 return 0;
850}
851
Jens Axboe492158c2007-05-24 10:32:47 +0200852static int keep_running(struct thread_data *td)
853{
854 unsigned long long io_done;
855
Jens Axboe20e354e2007-07-23 14:36:16 +0200856 if (td->done)
857 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200858 if (td->o.time_based)
859 return 1;
860 if (td->o.loops) {
861 td->o.loops--;
862 return 1;
863 }
864
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100865 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
866 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200867 if (io_done < td->o.size)
868 return 1;
869
870 return 0;
871}
872
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200873static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100874{
Jens Axboe756867b2007-03-06 15:19:24 +0100875 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100876 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100877 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100878 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100879 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100880 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100881
Jens Axboec1324df2007-02-19 19:06:41 +0100882 td->last_was_sync = 0;
883
Jens Axboe2ba1c292008-02-01 13:16:38 +0100884 /*
885 * reset file done count if we are to start over
886 */
887 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200888 td->nr_done_files = 0;
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200889
890 /*
891 * Set the same seed to get repeatable runs
892 */
893 td_fill_rand_seeds(td);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200894}
895
896void reset_all_stats(struct thread_data *td)
897{
898 struct timeval tv;
899 int i;
900
901 reset_io_counters(td);
902
903 for (i = 0; i < 2; i++) {
904 td->io_bytes[i] = 0;
905 td->io_blocks[i] = 0;
906 td->io_issues[i] = 0;
907 td->ts.total_io_u[i] = 0;
908 }
909
910 fio_gettime(&tv, NULL);
911 memcpy(&td->epoch, &tv, sizeof(tv));
912 memcpy(&td->start, &tv, sizeof(tv));
913}
914
Jens Axboedf9c26b2009-03-05 10:13:58 +0100915static void clear_io_state(struct thread_data *td)
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200916{
917 struct fio_file *f;
918 unsigned int i;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200919
920 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +0200921
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100922 close_files(td);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100923 for_each_file(td, f, i)
Jens Axboe281f9b62007-05-23 09:34:42 +0200924 f->flags &= ~FIO_FILE_DONE;
Jens Axboeebac4652005-12-08 15:25:21 +0100925}
926
Jens Axboe906c8d72006-06-13 09:37:56 +0200927/*
928 * Entry point for the thread based jobs. The process based jobs end up
929 * here as well, after a little setup.
930 */
Jens Axboeebac4652005-12-08 15:25:21 +0100931static void *thread_main(void *data)
932{
Shawn Lewis10927312007-11-21 09:38:13 +0100933 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100934 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100935 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100936
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100937 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100938 setsid();
939
940 td->pid = getpid();
941
Jens Axboe5921e802008-05-30 15:02:38 +0200942 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100943
Jens Axboe01743ee2008-06-02 12:19:19 +0200944 INIT_FLIST_HEAD(&td->io_u_freelist);
945 INIT_FLIST_HEAD(&td->io_u_busylist);
946 INIT_FLIST_HEAD(&td->io_u_requeues);
947 INIT_FLIST_HEAD(&td->io_log_list);
948 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200949 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200950
Jens Axboe75154842006-06-01 13:56:09 +0200951 td_set_runstate(td, TD_INITIALIZED);
Jens Axboe29adda32009-01-05 19:06:39 +0100952 dprint(FD_MUTEX, "up startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +0100953 fio_mutex_up(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +0100954 dprint(FD_MUTEX, "wait on td->mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +0100955 fio_mutex_down(td->mutex);
Jens Axboe29adda32009-01-05 19:06:39 +0100956 dprint(FD_MUTEX, "done waiting on td->mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100957
Jens Axboe37f56872007-03-09 12:42:35 +0100958 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100959 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100960 * eating a file descriptor
961 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100962 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100963
Jens Axboed84f8d42007-05-16 08:47:46 +0200964 /*
965 * May alter parameters that init_io_u() will use, so we need to
966 * do this first.
967 */
968 if (init_iolog(td))
969 goto err;
970
Jens Axboedb1cd902007-04-26 13:47:07 +0200971 if (init_io_u(td))
972 goto err;
973
Jens Axboe375b2692007-05-22 09:13:02 +0200974 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200975 td_verror(td, errno, "cpu_set_affinity");
976 goto err;
977 }
978
Jens Axboef4844702008-12-09 08:04:29 +0100979 /*
980 * If we have a gettimeofday() thread, make sure we exclude that
981 * thread from this job
982 */
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100983 if (td->o.gtod_cpu) {
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100984 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
985 if (fio_setaffinity(td) == -1) {
986 td_verror(td, errno, "cpu_set_affinity");
987 goto err;
988 }
989 }
990
Jens Axboeac684782007-11-08 08:29:07 +0100991 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200992 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
993 td_verror(td, errno, "ioprio_set");
994 goto err;
995 }
996 }
997
998 if (nice(td->o.nice) == -1) {
999 td_verror(td, errno, "nice");
1000 goto err;
1001 }
1002
1003 if (td->o.ioscheduler && switch_ioscheduler(td))
1004 goto err;
1005
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001006 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +01001007 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +01001008
1009 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +01001010 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +01001011
Jens Axboe68727072007-03-15 20:49:25 +01001012 if (init_random_map(td))
1013 goto err;
1014
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001015 if (td->o.exec_prerun) {
1016 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001017 goto err;
1018 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001019
Jens Axboe69008992006-11-24 13:12:56 +01001020 fio_gettime(&td->epoch, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001021 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +01001022
1023 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +01001024 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001025 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +01001026 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001027 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboe993bf482008-11-14 13:04:53 +01001028 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +01001029
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001030 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001031 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1032 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +01001033
Jens Axboedf9c26b2009-03-05 10:13:58 +01001034 if (clear_state)
1035 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001036
Jens Axboeebac4652005-12-08 15:25:21 +01001037 prune_io_piece_log(td);
1038
Jens Axboeba0fbe12007-03-09 14:34:23 +01001039 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001040
Jens Axboea978ba62007-03-08 14:29:03 +01001041 clear_state = 1;
1042
Jens Axboe38d77ca2007-03-21 14:20:20 +01001043 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1044 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001045 elapsed = utime_since(&td->start,
1046 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001047 else
1048 elapsed = utime_since_now(&td->start);
1049
1050 runtime[DDIR_READ] += elapsed;
1051 }
1052 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1053 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001054 elapsed = utime_since(&td->start,
1055 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001056 else
1057 elapsed = utime_since_now(&td->start);
1058
1059 runtime[DDIR_WRITE] += elapsed;
1060 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001061
Jens Axboeebac4652005-12-08 15:25:21 +01001062 if (td->error || td->terminate)
1063 break;
1064
Shawn Lewise84c73a2007-08-02 22:19:32 +02001065 if (!td->o.do_verify ||
1066 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001067 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001068 continue;
1069
Jens Axboedf9c26b2009-03-05 10:13:58 +01001070 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001071
Jens Axboe02bcaa82006-11-24 10:42:00 +01001072 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001073
1074 do_verify(td);
1075
Jens Axboe69008992006-11-24 13:12:56 +01001076 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001077
1078 if (td->error || td->terminate)
1079 break;
1080 }
1081
Jens Axboe36dff962006-11-24 13:29:20 +01001082 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001083 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1084 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001085 td->ts.total_run_time = mtime_since_now(&td->epoch);
1086 td->ts.io_bytes[0] = td->io_bytes[0];
1087 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001088
Jens Axboee3cedca2008-11-19 19:57:52 +01001089 if (td->ts.bw_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001090 if (td->o.bw_log_file) {
1091 finish_log_named(td, td->ts.bw_log,
1092 td->o.bw_log_file, "bw");
1093 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001094 finish_log(td, td->ts.bw_log, "bw");
1095 }
1096 if (td->ts.slat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001097 if (td->o.lat_log_file) {
1098 finish_log_named(td, td->ts.slat_log,
1099 td->o.lat_log_file, "clat");
1100 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001101 finish_log(td, td->ts.slat_log, "slat");
1102 }
1103 if (td->ts.clat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001104 if (td->o.lat_log_file) {
1105 finish_log_named(td, td->ts.clat_log,
1106 td->o.lat_log_file, "clat");
1107 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001108 finish_log(td, td->ts.clat_log, "clat");
1109 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001110 if (td->o.exec_postrun) {
1111 if (system(td->o.exec_postrun) < 0)
1112 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001113 }
Jens Axboeebac4652005-12-08 15:25:21 +01001114
1115 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001116 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001117
1118err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001119 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001120 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001121 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001122 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001123 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001124 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001125
Jens Axboed2ce18b2008-12-12 20:51:40 +01001126 if (td->o.cpumask_set) {
1127 int ret = fio_cpuset_exit(&td->o.cpumask);
1128
1129 td_verror(td, ret, "fio_cpuset_exit");
1130 }
Jens Axboe4e78e402008-12-12 20:40:27 +01001131
Jens Axboef29b25a2007-07-23 08:56:43 +02001132 /*
1133 * do this very late, it will log file closing as well
1134 */
1135 if (td->o.write_iolog_file)
1136 write_iolog_close(td);
1137
Jens Axboed23bb322007-03-23 15:57:56 +01001138 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001139 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001140 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001141}
1142
Jens Axboe906c8d72006-06-13 09:37:56 +02001143/*
1144 * We cannot pass the td data into a forked process, so attach the td and
1145 * pass it to the thread worker.
1146 */
Jens Axboea6418142007-02-17 04:47:18 +01001147static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001148{
1149 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001150 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001151
1152 data = shmat(shmid, NULL, 0);
1153 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001154 int __err = errno;
1155
Jens Axboeebac4652005-12-08 15:25:21 +01001156 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001157 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001158 }
1159
1160 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001161 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001162 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001163 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001164}
1165
Jens Axboe906c8d72006-06-13 09:37:56 +02001166/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001167 * Run over the job map and reap the threads that have exited, if any.
1168 */
Jens Axboeebac4652005-12-08 15:25:21 +01001169static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1170{
Jens Axboe34572e22006-10-20 09:33:18 +02001171 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001172 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001173
1174 /*
1175 * reap exited threads (TD_EXITED -> TD_REAPED)
1176 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001177 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001178 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001179 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001180
Jens Axboe84585002006-10-19 20:26:22 +02001181 /*
1182 * ->io_ops is NULL for a thread that has closed its
1183 * io engine
1184 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001185 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001186 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001187 else
1188 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001189
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001190 if (!td->pid) {
1191 pending++;
1192 continue;
1193 }
1194 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001195 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001196 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001197 if (td->runstate == TD_EXITED) {
1198 td_set_runstate(td, TD_REAPED);
1199 goto reaped;
1200 }
1201 continue;
1202 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001203
1204 flags = WNOHANG;
1205 if (td->runstate == TD_EXITED)
1206 flags = 0;
1207
1208 /*
1209 * check if someone quit or got killed in an unusual way
1210 */
1211 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001212 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001213 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001214 log_err("fio: pid=%d disappeared %d\n",
1215 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001216 td_set_runstate(td, TD_REAPED);
1217 goto reaped;
1218 }
1219 perror("waitpid");
1220 } else if (ret == td->pid) {
1221 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001222 int sig = WTERMSIG(status);
1223
Jens Axboed9244942007-03-05 12:32:32 +01001224 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001225 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001226 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001227 td_set_runstate(td, TD_REAPED);
1228 goto reaped;
1229 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001230 if (WIFEXITED(status)) {
1231 if (WEXITSTATUS(status) && !td->error)
1232 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001233
Jens Axboea2f77c92007-02-22 11:53:00 +01001234 td_set_runstate(td, TD_REAPED);
1235 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001236 }
1237 }
Jens Axboeebac4652005-12-08 15:25:21 +01001238
Jens Axboea2f77c92007-02-22 11:53:00 +01001239 /*
1240 * thread is not dead, continue
1241 */
gurudas paie48676b2007-04-11 12:44:27 +02001242 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001243 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001244reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001245 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001246 (*m_rate) -= td->o.ratemin;
1247 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001248 if (!td->pid)
1249 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001250
1251 if (td->error)
1252 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001253
1254 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001255 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001256
Jens Axboe1f809d12007-10-25 18:34:02 +02001257 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001258 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001259}
1260
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001261static void *gtod_thread_main(void *data)
1262{
1263 fio_mutex_up(startup_mutex);
1264
1265 /*
1266 * As long as we have jobs around, update the clock. It would be nice
1267 * to have some way of NOT hammering that CPU with gettimeofday(),
1268 * but I'm not sure what to use outside of a simple CPU nop to relax
1269 * it - we don't want to lose precision.
1270 */
1271 while (threads) {
1272 fio_gtod_update();
1273 nop;
1274 }
1275
1276 return NULL;
1277}
1278
1279static int fio_start_gtod_thread(void)
1280{
Jens Axboef42c1532009-01-05 19:07:13 +01001281 int ret;
1282
1283 ret = pthread_create(&gtod_thread, NULL, gtod_thread_main, NULL);
1284 if (ret) {
1285 log_err("Can't create gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001286 return 1;
1287 }
Jens Axboef42c1532009-01-05 19:07:13 +01001288
1289 ret = pthread_detach(gtod_thread);
1290 if (ret) {
1291 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001292 return 1;
1293 }
1294
Jens Axboe29adda32009-01-05 19:06:39 +01001295 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001296 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001297 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001298 return 0;
1299}
1300
Jens Axboe906c8d72006-06-13 09:37:56 +02001301/*
1302 * Main function for kicking off and reaping jobs, as needed.
1303 */
Jens Axboeebac4652005-12-08 15:25:21 +01001304static void run_threads(void)
1305{
Jens Axboeebac4652005-12-08 15:25:21 +01001306 struct thread_data *td;
1307 unsigned long spent;
1308 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001309
Jens Axboe2f9ade32006-10-20 11:25:52 +02001310 if (fio_pin_memory())
1311 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001312
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001313 if (fio_gtod_offload && fio_start_gtod_thread())
1314 return;
1315
Jens Axboec6ae0a52006-06-12 11:04:44 +02001316 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001317 printf("Starting ");
1318 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001319 printf("%d thread%s", nr_thread,
1320 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001321 if (nr_process) {
1322 if (nr_thread)
1323 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001324 printf("%d process%s", nr_process,
1325 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001326 }
1327 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001328 fflush(stdout);
1329 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001330
Jens Axboe697a6062008-05-31 00:04:45 +02001331 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001332
Jens Axboeebac4652005-12-08 15:25:21 +01001333 todo = thread_number;
1334 nr_running = 0;
1335 nr_started = 0;
1336 m_rate = t_rate = 0;
1337
Jens Axboe34572e22006-10-20 09:33:18 +02001338 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001339 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001340
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001341 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001342 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001343 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001344 }
Jens Axboeebac4652005-12-08 15:25:21 +01001345
1346 /*
1347 * do file setup here so it happens sequentially,
1348 * we don't want X number of threads getting their
1349 * client data interspersed on disk
1350 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001351 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001352 exit_value++;
1353 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001354 log_err("fio: pid=%d, err=%d/%s\n",
1355 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001356 td_set_runstate(td, TD_REAPED);
1357 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001358 } else {
1359 struct fio_file *f;
1360 unsigned int i;
1361
1362 /*
1363 * for sharing to work, each job must always open
1364 * its own files. so close them, if we opened them
1365 * for creation
1366 */
1367 for_each_file(td, f, i)
1368 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001369 }
Jens Axboe380cf262007-01-11 14:01:27 +01001370
1371 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001372 }
1373
Jens Axboea2f77c92007-02-22 11:53:00 +01001374 set_genesis_time();
1375
Jens Axboeebac4652005-12-08 15:25:21 +01001376 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001377 struct thread_data *map[MAX_JOBS];
1378 struct timeval this_start;
1379 int this_jobs = 0, left;
1380
Jens Axboeebac4652005-12-08 15:25:21 +01001381 /*
1382 * create threads (TD_NOT_CREATED -> TD_CREATED)
1383 */
Jens Axboe34572e22006-10-20 09:33:18 +02001384 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001385 if (td->runstate != TD_NOT_CREATED)
1386 continue;
1387
1388 /*
1389 * never got a chance to start, killed by other
1390 * thread for some reason
1391 */
1392 if (td->terminate) {
1393 todo--;
1394 continue;
1395 }
1396
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001397 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001398 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001399
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001400 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001401 continue;
1402 }
1403
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001404 if (td->o.stonewall && (nr_started || nr_running)) {
1405 dprint(FD_PROCESS, "%s: stonewall wait\n",
1406 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001407 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001408 }
Jens Axboeebac4652005-12-08 15:25:21 +01001409
Jens Axboe75154842006-06-01 13:56:09 +02001410 /*
1411 * Set state to created. Thread will transition
1412 * to TD_INITIALIZED when it's done setting up.
1413 */
Jens Axboeebac4652005-12-08 15:25:21 +01001414 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001415 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001416 nr_started++;
1417
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001418 if (td->o.use_thread) {
Jens Axboef42c1532009-01-05 19:07:13 +01001419 int ret;
1420
Jens Axboeee56ad52008-02-01 10:30:20 +01001421 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboef42c1532009-01-05 19:07:13 +01001422 ret = pthread_create(&td->thread, NULL,
1423 thread_main, td);
1424 if (ret) {
1425 log_err("pthread_create: %s\n",
1426 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001427 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001428 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001429 }
Jens Axboef42c1532009-01-05 19:07:13 +01001430 ret = pthread_detach(td->thread);
1431 if (ret)
1432 log_err("pthread_detach: %s",
1433 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001434 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001435 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001436 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001437 pid = fork();
1438 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001439 int ret = fork_main(shm_id, i);
1440
Jens Axboe5e1d3062008-05-23 11:55:53 +02001441 _exit(ret);
1442 } else if (i == fio_debug_jobno)
1443 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001444 }
Jens Axboe29adda32009-01-05 19:06:39 +01001445 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +01001446 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001447 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001448 }
1449
1450 /*
Jens Axboe75154842006-06-01 13:56:09 +02001451 * Wait for the started threads to transition to
1452 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001453 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001454 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001455 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001456 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001457 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1458 break;
1459
1460 usleep(100000);
1461
1462 for (i = 0; i < this_jobs; i++) {
1463 td = map[i];
1464 if (!td)
1465 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001466 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001467 map[i] = NULL;
1468 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001469 } else if (td->runstate >= TD_EXITED) {
1470 map[i] = NULL;
1471 left--;
1472 todo--;
1473 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001474 }
1475 }
1476 }
1477
1478 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001479 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001480 for (i = 0; i < this_jobs; i++) {
1481 td = map[i];
1482 if (!td)
1483 continue;
1484 kill(td->pid, SIGTERM);
1485 }
1486 break;
1487 }
1488
1489 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001490 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001491 */
Jens Axboe34572e22006-10-20 09:33:18 +02001492 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001493 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001494 continue;
1495
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001496 if (in_ramp_time(td))
1497 td_set_runstate(td, TD_RAMP);
1498 else
1499 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001500 nr_running++;
1501 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001502 m_rate += td->o.ratemin;
1503 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001504 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001505 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001506 }
1507
1508 reap_threads(&nr_running, &t_rate, &m_rate);
1509
1510 if (todo)
1511 usleep(100000);
1512 }
1513
1514 while (nr_running) {
1515 reap_threads(&nr_running, &t_rate, &m_rate);
1516 usleep(10000);
1517 }
1518
1519 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001520 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001521}
1522
Jens Axboeebac4652005-12-08 15:25:21 +01001523int main(int argc, char *argv[])
1524{
Jens Axboe29d610e2007-02-06 13:25:33 +01001525 long ps;
1526
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001527 sinit();
1528
Jens Axboedbe11252007-02-11 00:19:51 +01001529 /*
1530 * We need locale for number printing, if it isn't set then just
1531 * go with the US format.
1532 */
1533 if (!getenv("LC_NUMERIC"))
1534 setlocale(LC_NUMERIC, "en_US");
1535
Jens Axboeebac4652005-12-08 15:25:21 +01001536 if (parse_options(argc, argv))
1537 return 1;
1538
Jens Axboe4b472fa2007-04-04 11:04:34 +02001539 if (!thread_number)
1540 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001541
Jens Axboe29d610e2007-02-06 13:25:33 +01001542 ps = sysconf(_SC_PAGESIZE);
1543 if (ps < 0) {
1544 log_err("Failed to get page size\n");
1545 return 1;
1546 }
1547
Jens Axboecfc99db2007-03-14 10:34:47 +01001548 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001549 page_mask = ps - 1;
1550
Jens Axboebb3884d2007-01-17 17:23:11 +11001551 if (write_bw_log) {
1552 setup_log(&agg_io_log[DDIR_READ]);
1553 setup_log(&agg_io_log[DDIR_WRITE]);
1554 }
1555
Jens Axboecdd18ad2008-02-27 18:58:00 +01001556 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001557
Jens Axboea2f77c92007-02-22 11:53:00 +01001558 set_genesis_time();
1559
Jens Axboeccb0fa22008-05-30 23:18:00 +02001560 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001561
1562 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001563
Jens Axboebb3884d2007-01-17 17:23:11 +11001564 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001565 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001566 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001567 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1568 __finish_log(agg_io_log[DDIR_WRITE],
1569 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001570 }
1571 }
Jens Axboeebac4652005-12-08 15:25:21 +01001572
Jens Axboecdd18ad2008-02-27 18:58:00 +01001573 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001574 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001575}