blob: bbfc41e50d3d845da3ef7693a11499e05a1fcb82 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
Jens Axboeaae22ca2006-09-05 10:46:22 +02005 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboeebac4652005-12-08 15:25:21 +01006 *
Jens Axboe8e9fe632006-10-24 15:11:09 +02007 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
Jens Axboeebac4652005-12-08 15:25:21 +010010 * This program is free software; you can redistribute it and/or modify
Jens Axboe8e9fe632006-10-24 15:11:09 +020011 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Jens Axboeebac4652005-12-08 15:25:21 +010013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <signal.h>
28#include <time.h>
Jens Axboedbe11252007-02-11 00:19:51 +010029#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010030#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010031#include <sys/stat.h>
32#include <sys/wait.h>
33#include <sys/ipc.h>
34#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010035#include <sys/mman.h>
36
37#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020038#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010039#include "smalloc.h"
Jens Axboeebac4652005-12-08 15:25:21 +010040
Jens Axboecfc99db2007-03-14 10:34:47 +010041unsigned long page_mask;
42unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010043#define ALIGN(buf) \
44 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010045
46int groupid = 0;
47int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010048int nr_process = 0;
49int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010050int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020051int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020052unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010053
Jens Axboecdd18ad2008-02-27 18:58:00 +010054static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010055static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010056static int exit_value;
Jens Axboeccb0fa22008-05-30 23:18:00 +020057static struct itimerval itimer;
Jens Axboeebac4652005-12-08 15:25:21 +010058
Jens Axboebb3884d2007-01-17 17:23:11 +110059struct io_log *agg_io_log[2];
60
Jens Axboeebac4652005-12-08 15:25:21 +010061#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020062#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010063
Jens Axboe6ce15a32007-01-12 09:04:41 +010064static inline void td_set_runstate(struct thread_data *td, int runstate)
65{
Jens Axboed8fab1b2008-03-06 10:25:17 +010066 if (td->runstate == runstate)
67 return;
68
Jens Axboe5921e802008-05-30 15:02:38 +020069 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
70 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010071 td->runstate = runstate;
72}
73
Jens Axboe390c40e2007-03-05 12:35:29 +010074static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010075{
Jens Axboe34572e22006-10-20 09:33:18 +020076 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010077 int i;
78
Jens Axboe6f88bcb2008-03-19 10:29:07 +010079 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
80
Jens Axboe34572e22006-10-20 09:33:18 +020081 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010082 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010083 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +020084 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010085 td->terminate = 1;
86 td->o.start_delay = 0;
87
Jens Axboe7a93ab12007-04-18 12:25:41 +020088 /*
89 * if the thread is running, just let it exit
90 */
91 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010092 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010093 else {
94 struct ioengine_ops *ops = td->io_ops;
95
96 if (ops && (ops->flags & FIO_SIGQUIT))
97 kill(td->pid, SIGQUIT);
98 }
Jens Axboeebac4652005-12-08 15:25:21 +010099 }
100 }
101}
102
Jens Axboeccb0fa22008-05-30 23:18:00 +0200103static void status_timer_arm(void)
104{
105 itimer.it_value.tv_sec = 0;
106 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
107 setitimer(ITIMER_REAL, &itimer, NULL);
108}
109
Jens Axboe697a6062008-05-31 00:04:45 +0200110static void sig_alrm(int sig)
Jens Axboeccb0fa22008-05-30 23:18:00 +0200111{
Jens Axboe697a6062008-05-31 00:04:45 +0200112 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100113 update_io_ticks();
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100114 print_thread_status();
Jens Axboeccb0fa22008-05-30 23:18:00 +0200115 status_timer_arm();
Jens Axboe697a6062008-05-31 00:04:45 +0200116 }
117}
118
119static void sig_int(int sig)
120{
121 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100122 printf("\nfio: terminating on signal %d\n", sig);
123 fflush(stdout);
124 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100125 }
126}
127
Jens Axboe906c8d72006-06-13 09:37:56 +0200128/*
Jens Axboe697a6062008-05-31 00:04:45 +0200129 * We need to rearm on BSD/solaris. Switch this to sigaction in the future...
130 */
131static void set_sig_handlers(void)
132{
133 struct sigaction act;
134
135 memset(&act, 0, sizeof(act));
136 act.sa_handler = sig_alrm;
137 act.sa_flags = SA_RESTART;
138 sigaction(SIGALRM, &act, NULL);
139
140 memset(&act, 0, sizeof(act));
141 act.sa_handler = sig_int;
142 act.sa_flags = SA_RESTART;
143 sigaction(SIGINT, &act, NULL);
144}
145
146/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200147 * Check if we are above the minimum rate given.
148 */
Jens Axboeebac4652005-12-08 15:25:21 +0100149static int check_min_rate(struct thread_data *td, struct timeval *now)
150{
Jens Axboe09042002007-02-23 10:56:22 +0100151 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100152 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100153 unsigned long spent;
154 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100155
156 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100157 * No minimum rate set, always ok
158 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100159 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100160 return 0;
161
162 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100163 * allow a 2 second settle period in the beginning
164 */
165 if (mtime_since(&td->start, now) < 2000)
166 return 0;
167
Jens Axboe4e991c22007-03-15 11:41:11 +0100168 if (td_read(td)) {
169 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100170 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100171 }
172 if (td_write(td)) {
173 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100174 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100175 }
Jens Axboe09042002007-02-23 10:56:22 +0100176
Jens Axboeebac4652005-12-08 15:25:21 +0100177 /*
178 * if rate blocks is set, sample is running
179 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100180 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100181 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100182 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100183 return 0;
184
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100185 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100186 /*
187 * check bandwidth specified rate
188 */
189 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100190 log_err("%s: min rate %u not met\n", td->o.name,
191 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100192 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100193 } else {
194 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100195 if (rate < td->o.ratemin ||
196 bytes < td->rate_bytes) {
197 log_err("%s: min rate %u not met, got"
198 " %luKiB/sec\n", td->o.name,
199 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100200 return 1;
201 }
202 }
203 } else {
204 /*
205 * checks iops specified rate
206 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100207 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100208 log_err("%s: min iops rate %u not met\n",
209 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100210 return 1;
211 } else {
212 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100213 if (rate < td->o.rate_iops_min ||
214 iops < td->rate_blocks) {
215 log_err("%s: min iops rate %u not met,"
216 " got %lu\n", td->o.name,
217 td->o.rate_iops_min,
218 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100219 }
Jens Axboe413dd452007-02-23 09:26:09 +0100220 }
Jens Axboeebac4652005-12-08 15:25:21 +0100221 }
222 }
223
Jens Axboe09042002007-02-23 10:56:22 +0100224 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100225 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100226 memcpy(&td->lastrate, now, sizeof(*now));
227 return 0;
228}
229
230static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
231{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100232 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100233 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100234 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100235 return 1;
236
237 return 0;
238}
239
Jens Axboe906c8d72006-06-13 09:37:56 +0200240/*
241 * When job exits, we can cancel the in-flight IO if we are using async
242 * io. Attempt to do so.
243 */
Jens Axboeebac4652005-12-08 15:25:21 +0100244static void cleanup_pending_aio(struct thread_data *td)
245{
Jens Axboe01743ee2008-06-02 12:19:19 +0200246 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100247 struct io_u *io_u;
248 int r;
249
250 /*
251 * get immediately available events, if any
252 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100253 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100254 if (r < 0)
255 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100256
257 /*
258 * now cancel remaining active events
259 */
Jens Axboe2866c822006-10-09 15:57:48 +0200260 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200261 flist_for_each_safe(entry, n, &td->io_u_busylist) {
262 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100263
Jens Axboe0c6e7512007-02-22 11:19:39 +0100264 /*
265 * if the io_u isn't in flight, then that generally
266 * means someone leaked an io_u. complain but fix
267 * it up, so we don't stall here.
268 */
269 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
270 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100271 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100272 } else {
273 r = td->io_ops->cancel(td, io_u);
274 if (!r)
275 put_io_u(td, io_u);
276 }
Jens Axboeebac4652005-12-08 15:25:21 +0100277 }
278 }
279
Jens Axboe97601022007-02-18 12:47:29 +0100280 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100281 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100282}
283
Jens Axboe906c8d72006-06-13 09:37:56 +0200284/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200285 * Helper to handle the final sync of a file. Works just like the normal
286 * io path, just does everything sync.
287 */
288static int fio_io_sync(struct thread_data *td, struct fio_file *f)
289{
290 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200291 int ret;
292
293 if (!io_u)
294 return 1;
295
296 io_u->ddir = DDIR_SYNC;
297 io_u->file = f;
298
299 if (td_io_prep(td, io_u)) {
300 put_io_u(td, io_u);
301 return 1;
302 }
303
Jens Axboe755200a2007-02-19 13:08:12 +0100304requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200305 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100306 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100307 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200308 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200309 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100310 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100311 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100312 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100313 } else if (ret == FIO_Q_COMPLETED) {
314 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100315 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100316 return 1;
317 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200318
Jens Axboed7762cf2007-02-23 12:34:57 +0100319 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100320 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100321 } else if (ret == FIO_Q_BUSY) {
322 if (td_io_commit(td))
323 return 1;
324 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200325 }
326
327 return 0;
328}
329
330/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100331 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200332 * reads the blocks back in, and checks the crc/md5 of the data.
333 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100334static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100335{
Jens Axboe53cdc682006-10-18 11:50:58 +0200336 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100337 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100338 int ret, min_events;
339 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200340
341 /*
342 * sync io first and invalidate cache, to make sure we really
343 * read from disk.
344 */
345 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100346 if (!(f->flags & FIO_FILE_OPEN))
347 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100348 if (fio_io_sync(td, f))
349 break;
350 if (file_invalidate_cache(td, f))
351 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200352 }
Jens Axboeebac4652005-12-08 15:25:21 +0100353
Jens Axboeb2fdda42007-02-20 20:52:51 +0100354 if (td->error)
355 return;
356
Jens Axboeebac4652005-12-08 15:25:21 +0100357 td_set_runstate(td, TD_VERIFYING);
358
Jens Axboe36167d82007-02-18 05:41:31 +0100359 io_u = NULL;
360 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100361 int ret2;
362
Jens Axboeebac4652005-12-08 15:25:21 +0100363 io_u = __get_io_u(td);
364 if (!io_u)
365 break;
366
Jens Axboe069c2912007-02-21 23:02:39 +0100367 if (runtime_exceeded(td, &io_u->start_time)) {
368 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200369 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200370 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100371 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200372
Jens Axboe069c2912007-02-21 23:02:39 +0100373 if (get_next_verify(td, io_u)) {
374 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100375 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100376 }
Jens Axboeebac4652005-12-08 15:25:21 +0100377
Jens Axboe069c2912007-02-21 23:02:39 +0100378 if (td_io_prep(td, io_u)) {
379 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100380 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100381 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100382
383 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100384
Jens Axboe11786802007-03-05 18:33:22 +0100385 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100386 switch (ret) {
387 case FIO_Q_COMPLETED:
388 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100389 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100390 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100391 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200392 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100393
Jens Axboe9e9d1642007-03-12 09:37:46 +0100394 /*
395 * zero read, fail
396 */
397 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200398 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100399 put_io_u(td, io_u);
400 break;
401 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200402
Jens Axboe36167d82007-02-18 05:41:31 +0100403 io_u->xfer_buflen = io_u->resid;
404 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200405 io_u->offset += bytes;
406
Jens Axboe30061b92007-04-17 13:31:34 +0200407 td->ts.short_io_u[io_u->ddir]++;
408
Jens Axboed460eb32007-04-16 21:39:33 +0200409 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200410 goto sync_done;
411
Jens Axboe11786802007-03-05 18:33:22 +0100412 requeue_io_u(td, &io_u);
413 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200414sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100415 ret = io_u_sync_complete(td, io_u);
416 if (ret < 0)
417 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100418 }
Jens Axboe36167d82007-02-18 05:41:31 +0100419 continue;
420 case FIO_Q_QUEUED:
421 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100422 case FIO_Q_BUSY:
423 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100424 ret2 = td_io_commit(td);
425 if (ret2 < 0)
426 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100427 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100428 default:
429 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100430 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100431 break;
432 }
433
Jens Axboe99784632007-02-19 10:06:17 +0100434 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100435 break;
436
Jens Axboe3af6ef32007-02-18 06:57:43 +0100437 /*
438 * if we can queue more, do so. but check if there are
439 * completed io_u's first.
440 */
Jens Axboe97601022007-02-18 12:47:29 +0100441 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100442 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe838bc702008-05-22 13:08:23 +0200443 if (td->cur_depth >= td->o.iodepth_low)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100444 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboe838bc702008-05-22 13:08:23 +0200445 if (!min_events)
446 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100447 }
448
Jens Axboe3af6ef32007-02-18 06:57:43 +0100449 /*
450 * Reap required number of io units, if any, and do the
451 * verification on them through the callback handler
452 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100453 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100454 break;
455 }
456
Jens Axboec01c0392007-02-26 12:43:42 +0100457 if (!td->error) {
458 min_events = td->cur_depth;
459
460 if (min_events)
461 ret = io_u_queued_complete(td, min_events);
462 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100463 cleanup_pending_aio(td);
464
465 td_set_runstate(td, TD_RUNNING);
466}
467
Jens Axboe32cd46a2006-06-07 13:40:40 +0200468/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200469 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200470 * and reaps them, checking for rate and errors along the way.
471 */
Jens Axboeebac4652005-12-08 15:25:21 +0100472static void do_io(struct thread_data *td)
473{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100474 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100475 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100476 unsigned int i;
477 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100478
Jens Axboe5853e5a2006-06-08 14:41:05 +0200479 td_set_runstate(td, TD_RUNNING);
480
Jens Axboe7bb48f82007-03-27 15:30:28 +0200481 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100482 struct timeval comp_time;
483 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200484 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100485 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100486 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100487
488 if (td->terminate)
489 break;
490
Jens Axboe3d7c3912007-02-19 13:16:12 +0100491 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100492 if (!io_u)
493 break;
494
495 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100496
497 if (runtime_exceeded(td, &s)) {
498 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200499 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100500 break;
501 }
Jens Axboe36167d82007-02-18 05:41:31 +0100502
Jens Axboeb67740d2007-07-26 12:22:30 +0200503 /*
504 * Add verification end_io handler, if asked to verify
505 * a previously written file.
506 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100507 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200508 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100509 td_set_runstate(td, TD_VERIFYING);
510 } else
511 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200512
Jens Axboe11786802007-03-05 18:33:22 +0100513 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100514 switch (ret) {
515 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100516 if (io_u->error)
517 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100518 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100519 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200520 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100521
Jens Axboe9e9d1642007-03-12 09:37:46 +0100522 /*
523 * zero read, fail
524 */
525 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200526 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100527 put_io_u(td, io_u);
528 break;
529 }
530
Jens Axboe36167d82007-02-18 05:41:31 +0100531 io_u->xfer_buflen = io_u->resid;
532 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200533 io_u->offset += bytes;
534
Jens Axboe30061b92007-04-17 13:31:34 +0200535 td->ts.short_io_u[io_u->ddir]++;
536
Jens Axboed460eb32007-04-16 21:39:33 +0200537 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200538 goto sync_done;
539
Jens Axboe11786802007-03-05 18:33:22 +0100540 requeue_io_u(td, &io_u);
541 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200542sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100543 fio_gettime(&comp_time, NULL);
544 bytes_done = io_u_sync_complete(td, io_u);
545 if (bytes_done < 0)
546 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100547 }
Jens Axboe36167d82007-02-18 05:41:31 +0100548 break;
549 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100550 /*
551 * if the engine doesn't have a commit hook,
552 * the io_u is really queued. if it does have such
553 * a hook, it has to call io_u_queued() itself.
554 */
555 if (td->io_ops->commit == NULL)
556 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100557 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100558 case FIO_Q_BUSY:
559 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100560 ret2 = td_io_commit(td);
561 if (ret2 < 0)
562 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100563 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100564 default:
565 assert(ret < 0);
566 put_io_u(td, io_u);
567 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100568 }
569
Jens Axboe99784632007-02-19 10:06:17 +0100570 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100571 break;
572
Jens Axboe97601022007-02-18 12:47:29 +0100573 /*
574 * See if we need to complete some commands
575 */
Jens Axboe838bc702008-05-22 13:08:23 +0200576 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100577 min_evts = 0;
Jens Axboe838bc702008-05-22 13:08:23 +0200578 if (td->cur_depth >= td->o.iodepth_low)
579 min_evts = td->cur_depth - td->o.iodepth_low;
580 if (!min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100581 min_evts = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100582 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100583 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100584 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100585 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100586 }
587
Jens Axboe97601022007-02-18 12:47:29 +0100588 if (!bytes_done)
589 continue;
590
Jens Axboeebac4652005-12-08 15:25:21 +0100591 /*
592 * the rate is batched for now, it should work for batches
593 * of completions except the very first one which may look
594 * a little bursty
595 */
Jens Axboe97601022007-02-18 12:47:29 +0100596 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100597
Jens Axboe413dd452007-02-23 09:26:09 +0100598 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100599
Jens Axboe97601022007-02-18 12:47:29 +0100600 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100601 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100602 terminate_threads(td->groupid);
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200603 td_verror(td, EIO, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100604 break;
605 }
606
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100607 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100608 unsigned long long b;
609
610 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100611 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100612 int left;
613
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100614 if (td->o.thinktime_spin)
615 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100616
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100617 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100618 if (left)
619 usec_sleep(td, left);
620 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100621 }
Jens Axboeebac4652005-12-08 15:25:21 +0100622 }
623
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100624 if (td->o.fill_device && td->error == ENOSPC) {
625 td->error = 0;
626 td->terminate = 1;
627 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100628 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100629 struct fio_file *f;
630
Jens Axboec01c0392007-02-26 12:43:42 +0100631 i = td->cur_depth;
632 if (i)
633 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100634
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100635 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200636 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100637
638 for_each_file(td, f, i) {
639 if (!(f->flags & FIO_FILE_OPEN))
640 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200641 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100642 }
Jens Axboe84585002006-10-19 20:26:22 +0200643 }
Jens Axboec01c0392007-02-26 12:43:42 +0100644 } else
645 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100646
647 /*
648 * stop job if we failed doing any IO
649 */
650 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
651 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100652}
653
Jens Axboeebac4652005-12-08 15:25:21 +0100654static void cleanup_io_u(struct thread_data *td)
655{
Jens Axboe01743ee2008-06-02 12:19:19 +0200656 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100657 struct io_u *io_u;
658
Jens Axboe01743ee2008-06-02 12:19:19 +0200659 flist_for_each_safe(entry, n, &td->io_u_freelist) {
660 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100661
Jens Axboe01743ee2008-06-02 12:19:19 +0200662 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100663 free(io_u);
664 }
665
Jens Axboe2f9ade32006-10-20 11:25:52 +0200666 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100667}
668
669static int init_io_u(struct thread_data *td)
670{
671 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100672 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100673 int i, max_units;
674 char *p;
675
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100676 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100677 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100678 td->orig_buffer_size = (unsigned long long) max_bs
679 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100680
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100681 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
682 unsigned long bs;
683
684 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
685 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
686 }
Jens Axboeebac4652005-12-08 15:25:21 +0100687
Jens Axboec3990642007-07-19 10:17:09 +0200688 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
689 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
690 return 1;
691 }
692
Jens Axboe2f9ade32006-10-20 11:25:52 +0200693 if (allocate_io_mem(td))
694 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100695
Jens Axboeb3f378e2007-07-20 12:39:22 +0200696 if (td->o.odirect)
697 p = ALIGN(td->orig_buffer);
698 else
699 p = td->orig_buffer;
700
Jens Axboeebac4652005-12-08 15:25:21 +0100701 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200702 if (td->terminate)
703 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100704 io_u = malloc(sizeof(*io_u));
705 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200706 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100707
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200708 if (!(td->io_ops->flags & FIO_NOIO)) {
709 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200710
Jens Axboe5973caf2008-05-21 19:52:35 +0200711 if (td_write(td) && !td->o.refill_buffers)
712 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200713 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100714
Jens Axboeb1ff3402006-02-17 11:35:58 +0100715 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100716 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200717 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100718 }
719
Jens Axboe433afcb2007-02-22 10:39:01 +0100720 io_u_init_timeout();
721
Jens Axboeebac4652005-12-08 15:25:21 +0100722 return 0;
723}
724
Jens Axboeda867742006-06-06 10:39:10 -0700725static int switch_ioscheduler(struct thread_data *td)
726{
727 char tmp[256], tmp2[128];
728 FILE *f;
729 int ret;
730
Jens Axboeba0fbe12007-03-09 14:34:23 +0100731 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200732 return 0;
733
Jens Axboeda867742006-06-06 10:39:10 -0700734 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
735
736 f = fopen(tmp, "r+");
737 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200738 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100739 log_err("fio: os or kernel doesn't support IO scheduler"
740 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200741 return 0;
742 }
743 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700744 return 1;
745 }
746
747 /*
748 * Set io scheduler.
749 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100750 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700751 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100752 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700753 fclose(f);
754 return 1;
755 }
756
757 rewind(f);
758
759 /*
760 * Read back and check that the selected scheduler is now the default.
761 */
762 ret = fread(tmp, 1, sizeof(tmp), f);
763 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100764 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700765 fclose(f);
766 return 1;
767 }
768
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100769 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700770 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100771 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100772 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700773 fclose(f);
774 return 1;
775 }
776
777 fclose(f);
778 return 0;
779}
780
Jens Axboe492158c2007-05-24 10:32:47 +0200781static int keep_running(struct thread_data *td)
782{
783 unsigned long long io_done;
784
Jens Axboe20e354e2007-07-23 14:36:16 +0200785 if (td->done)
786 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200787 if (td->o.time_based)
788 return 1;
789 if (td->o.loops) {
790 td->o.loops--;
791 return 1;
792 }
793
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100794 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
795 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200796 if (io_done < td->o.size)
797 return 1;
798
799 return 0;
800}
801
Jens Axboea978ba62007-03-08 14:29:03 +0100802static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100803{
Jens Axboe53cdc682006-10-18 11:50:58 +0200804 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100805 unsigned int i;
806 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100807
Jens Axboe756867b2007-03-06 15:19:24 +0100808 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100809 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100810 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100811 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100812 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100813 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100814
Jens Axboec1324df2007-02-19 19:06:41 +0100815 td->last_was_sync = 0;
816
Jens Axboe2ba1c292008-02-01 13:16:38 +0100817 /*
818 * reset file done count if we are to start over
819 */
820 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200821 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200822
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100823 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100824
825 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200826 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200827 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100828 ret = td_io_open_file(td, f);
829 if (ret)
830 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200831 }
Jens Axboea978ba62007-03-08 14:29:03 +0100832
833 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100834}
835
Jens Axboe906c8d72006-06-13 09:37:56 +0200836/*
837 * Entry point for the thread based jobs. The process based jobs end up
838 * here as well, after a little setup.
839 */
Jens Axboeebac4652005-12-08 15:25:21 +0100840static void *thread_main(void *data)
841{
Shawn Lewis10927312007-11-21 09:38:13 +0100842 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100843 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100844 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100845
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100846 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100847 setsid();
848
849 td->pid = getpid();
850
Jens Axboe5921e802008-05-30 15:02:38 +0200851 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100852
Jens Axboe01743ee2008-06-02 12:19:19 +0200853 INIT_FLIST_HEAD(&td->io_u_freelist);
854 INIT_FLIST_HEAD(&td->io_u_busylist);
855 INIT_FLIST_HEAD(&td->io_u_requeues);
856 INIT_FLIST_HEAD(&td->io_log_list);
857 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200858 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200859
Jens Axboe75154842006-06-01 13:56:09 +0200860 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100861 fio_mutex_up(startup_mutex);
862 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100863
Jens Axboe37f56872007-03-09 12:42:35 +0100864 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100865 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100866 * eating a file descriptor
867 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100868 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100869
Jens Axboed84f8d42007-05-16 08:47:46 +0200870 /*
871 * May alter parameters that init_io_u() will use, so we need to
872 * do this first.
873 */
874 if (init_iolog(td))
875 goto err;
876
Jens Axboedb1cd902007-04-26 13:47:07 +0200877 if (init_io_u(td))
878 goto err;
879
Jens Axboe375b2692007-05-22 09:13:02 +0200880 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200881 td_verror(td, errno, "cpu_set_affinity");
882 goto err;
883 }
884
Jens Axboeac684782007-11-08 08:29:07 +0100885 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200886 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
887 td_verror(td, errno, "ioprio_set");
888 goto err;
889 }
890 }
891
892 if (nice(td->o.nice) == -1) {
893 td_verror(td, errno, "nice");
894 goto err;
895 }
896
897 if (td->o.ioscheduler && switch_ioscheduler(td))
898 goto err;
899
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100900 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100901 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100902
903 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100904 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100905
Jens Axboeb5af8292007-03-08 12:43:13 +0100906 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100907 goto err;
908
Jens Axboe68727072007-03-15 20:49:25 +0100909 if (init_random_map(td))
910 goto err;
911
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100912 if (td->o.exec_prerun) {
913 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100914 goto err;
915 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200916
Jens Axboe69008992006-11-24 13:12:56 +0100917 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100918 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100919 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100920
921 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100922 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200923 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100924 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100925 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100926
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100927 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100928 memcpy(&td->lastrate, &td->ts.stat_sample_time,
929 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100930
Jens Axboea978ba62007-03-08 14:29:03 +0100931 if (clear_state && clear_io_state(td))
932 break;
933
Jens Axboeebac4652005-12-08 15:25:21 +0100934 prune_io_piece_log(td);
935
Jens Axboeba0fbe12007-03-09 14:34:23 +0100936 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100937
Jens Axboea978ba62007-03-08 14:29:03 +0100938 clear_state = 1;
939
Jens Axboe38d77ca2007-03-21 14:20:20 +0100940 if (td_read(td) && td->io_bytes[DDIR_READ]) {
941 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100942 elapsed = utime_since(&td->start,
943 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100944 else
945 elapsed = utime_since_now(&td->start);
946
947 runtime[DDIR_READ] += elapsed;
948 }
949 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
950 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100951 elapsed = utime_since(&td->start,
952 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100953 else
954 elapsed = utime_since_now(&td->start);
955
956 runtime[DDIR_WRITE] += elapsed;
957 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100958
Jens Axboeebac4652005-12-08 15:25:21 +0100959 if (td->error || td->terminate)
960 break;
961
Shawn Lewise84c73a2007-08-02 22:19:32 +0200962 if (!td->o.do_verify ||
963 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200964 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100965 continue;
966
Jens Axboea978ba62007-03-08 14:29:03 +0100967 if (clear_io_state(td))
968 break;
969
Jens Axboe02bcaa82006-11-24 10:42:00 +0100970 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100971
972 do_verify(td);
973
Jens Axboe69008992006-11-24 13:12:56 +0100974 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100975
976 if (td->error || td->terminate)
977 break;
978 }
979
Jens Axboe36dff962006-11-24 13:29:20 +0100980 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200981 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
982 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100983 td->ts.total_run_time = mtime_since_now(&td->epoch);
984 td->ts.io_bytes[0] = td->io_bytes[0];
985 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100986
Jens Axboe756867b2007-03-06 15:19:24 +0100987 if (td->ts.bw_log)
988 finish_log(td, td->ts.bw_log, "bw");
989 if (td->ts.slat_log)
990 finish_log(td, td->ts.slat_log, "slat");
991 if (td->ts.clat_log)
992 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100993 if (td->o.exec_postrun) {
994 if (system(td->o.exec_postrun) < 0)
995 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100996 }
Jens Axboeebac4652005-12-08 15:25:21 +0100997
998 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100999 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001000
1001err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001002 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001003 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001004 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001005 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001006 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001007 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001008
1009 /*
1010 * do this very late, it will log file closing as well
1011 */
1012 if (td->o.write_iolog_file)
1013 write_iolog_close(td);
1014
Jens Axboed23bb322007-03-23 15:57:56 +01001015 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001016 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001017 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001018}
1019
Jens Axboe906c8d72006-06-13 09:37:56 +02001020/*
1021 * We cannot pass the td data into a forked process, so attach the td and
1022 * pass it to the thread worker.
1023 */
Jens Axboea6418142007-02-17 04:47:18 +01001024static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001025{
1026 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001027 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001028
1029 data = shmat(shmid, NULL, 0);
1030 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001031 int __err = errno;
1032
Jens Axboeebac4652005-12-08 15:25:21 +01001033 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001034 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001035 }
1036
1037 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001038 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001039 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001040 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001041}
1042
Jens Axboe906c8d72006-06-13 09:37:56 +02001043/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001044 * Run over the job map and reap the threads that have exited, if any.
1045 */
Jens Axboeebac4652005-12-08 15:25:21 +01001046static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1047{
Jens Axboe34572e22006-10-20 09:33:18 +02001048 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001049 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001050
1051 /*
1052 * reap exited threads (TD_EXITED -> TD_REAPED)
1053 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001054 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001055 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001056 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001057
Jens Axboe84585002006-10-19 20:26:22 +02001058 /*
1059 * ->io_ops is NULL for a thread that has closed its
1060 * io engine
1061 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001062 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001063 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001064 else
1065 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001066
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001067 if (!td->pid) {
1068 pending++;
1069 continue;
1070 }
1071 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001072 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001073 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001074 if (td->runstate == TD_EXITED) {
1075 td_set_runstate(td, TD_REAPED);
1076 goto reaped;
1077 }
1078 continue;
1079 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001080
1081 flags = WNOHANG;
1082 if (td->runstate == TD_EXITED)
1083 flags = 0;
1084
1085 /*
1086 * check if someone quit or got killed in an unusual way
1087 */
1088 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001089 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001090 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001091 log_err("fio: pid=%d disappeared %d\n",
1092 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001093 td_set_runstate(td, TD_REAPED);
1094 goto reaped;
1095 }
1096 perror("waitpid");
1097 } else if (ret == td->pid) {
1098 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001099 int sig = WTERMSIG(status);
1100
Jens Axboed9244942007-03-05 12:32:32 +01001101 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001102 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001103 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001104 td_set_runstate(td, TD_REAPED);
1105 goto reaped;
1106 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001107 if (WIFEXITED(status)) {
1108 if (WEXITSTATUS(status) && !td->error)
1109 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001110
Jens Axboea2f77c92007-02-22 11:53:00 +01001111 td_set_runstate(td, TD_REAPED);
1112 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001113 }
1114 }
Jens Axboeebac4652005-12-08 15:25:21 +01001115
Jens Axboea2f77c92007-02-22 11:53:00 +01001116 /*
1117 * thread is not dead, continue
1118 */
gurudas paie48676b2007-04-11 12:44:27 +02001119 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001120 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001121reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001122 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001123 (*m_rate) -= td->o.ratemin;
1124 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001125 if (!td->pid)
1126 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001127
1128 if (td->error)
1129 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001130
1131 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001132 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001133
Jens Axboe1f809d12007-10-25 18:34:02 +02001134 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001135 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001136}
1137
Jens Axboe906c8d72006-06-13 09:37:56 +02001138/*
1139 * Main function for kicking off and reaping jobs, as needed.
1140 */
Jens Axboeebac4652005-12-08 15:25:21 +01001141static void run_threads(void)
1142{
Jens Axboeebac4652005-12-08 15:25:21 +01001143 struct thread_data *td;
1144 unsigned long spent;
1145 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001146
Jens Axboe2f9ade32006-10-20 11:25:52 +02001147 if (fio_pin_memory())
1148 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001149
Jens Axboec6ae0a52006-06-12 11:04:44 +02001150 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001151 printf("Starting ");
1152 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001153 printf("%d thread%s", nr_thread,
1154 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001155 if (nr_process) {
1156 if (nr_thread)
1157 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001158 printf("%d process%s", nr_process,
1159 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001160 }
1161 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001162 fflush(stdout);
1163 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001164
Jens Axboe697a6062008-05-31 00:04:45 +02001165 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001166
Jens Axboeebac4652005-12-08 15:25:21 +01001167 todo = thread_number;
1168 nr_running = 0;
1169 nr_started = 0;
1170 m_rate = t_rate = 0;
1171
Jens Axboe34572e22006-10-20 09:33:18 +02001172 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001173 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001174
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001175 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001176 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001177 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001178 }
Jens Axboeebac4652005-12-08 15:25:21 +01001179
1180 /*
1181 * do file setup here so it happens sequentially,
1182 * we don't want X number of threads getting their
1183 * client data interspersed on disk
1184 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001185 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001186 exit_value++;
1187 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001188 log_err("fio: pid=%d, err=%d/%s\n",
1189 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001190 td_set_runstate(td, TD_REAPED);
1191 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001192 } else {
1193 struct fio_file *f;
1194 unsigned int i;
1195
1196 /*
1197 * for sharing to work, each job must always open
1198 * its own files. so close them, if we opened them
1199 * for creation
1200 */
1201 for_each_file(td, f, i)
1202 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001203 }
Jens Axboe380cf262007-01-11 14:01:27 +01001204
1205 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001206 }
1207
Jens Axboea2f77c92007-02-22 11:53:00 +01001208 set_genesis_time();
1209
Jens Axboeebac4652005-12-08 15:25:21 +01001210 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001211 struct thread_data *map[MAX_JOBS];
1212 struct timeval this_start;
1213 int this_jobs = 0, left;
1214
Jens Axboeebac4652005-12-08 15:25:21 +01001215 /*
1216 * create threads (TD_NOT_CREATED -> TD_CREATED)
1217 */
Jens Axboe34572e22006-10-20 09:33:18 +02001218 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001219 if (td->runstate != TD_NOT_CREATED)
1220 continue;
1221
1222 /*
1223 * never got a chance to start, killed by other
1224 * thread for some reason
1225 */
1226 if (td->terminate) {
1227 todo--;
1228 continue;
1229 }
1230
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001231 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001232 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001233
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001234 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001235 continue;
1236 }
1237
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001238 if (td->o.stonewall && (nr_started || nr_running)) {
1239 dprint(FD_PROCESS, "%s: stonewall wait\n",
1240 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001241 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001242 }
Jens Axboeebac4652005-12-08 15:25:21 +01001243
Jens Axboe75154842006-06-01 13:56:09 +02001244 /*
1245 * Set state to created. Thread will transition
1246 * to TD_INITIALIZED when it's done setting up.
1247 */
Jens Axboeebac4652005-12-08 15:25:21 +01001248 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001249 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001250 nr_started++;
1251
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001252 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001253 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001254 if (pthread_create(&td->thread, NULL,
1255 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001256 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001257 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001258 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001259 }
Jens Axboef4705a72008-03-10 10:52:22 +01001260 if (pthread_detach(td->thread) < 0)
1261 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001262 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001263 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001264 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001265 pid = fork();
1266 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001267 int ret = fork_main(shm_id, i);
1268
Jens Axboe5e1d3062008-05-23 11:55:53 +02001269 _exit(ret);
1270 } else if (i == fio_debug_jobno)
1271 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001272 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001273 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001274 }
1275
1276 /*
Jens Axboe75154842006-06-01 13:56:09 +02001277 * Wait for the started threads to transition to
1278 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001279 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001280 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001281 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001282 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001283 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1284 break;
1285
1286 usleep(100000);
1287
1288 for (i = 0; i < this_jobs; i++) {
1289 td = map[i];
1290 if (!td)
1291 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001292 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001293 map[i] = NULL;
1294 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001295 } else if (td->runstate >= TD_EXITED) {
1296 map[i] = NULL;
1297 left--;
1298 todo--;
1299 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001300 }
1301 }
1302 }
1303
1304 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001305 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001306 for (i = 0; i < this_jobs; i++) {
1307 td = map[i];
1308 if (!td)
1309 continue;
1310 kill(td->pid, SIGTERM);
1311 }
1312 break;
1313 }
1314
1315 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001316 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001317 */
Jens Axboe34572e22006-10-20 09:33:18 +02001318 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001319 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001320 continue;
1321
1322 td_set_runstate(td, TD_RUNNING);
1323 nr_running++;
1324 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001325 m_rate += td->o.ratemin;
1326 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001327 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001328 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001329 }
1330
1331 reap_threads(&nr_running, &t_rate, &m_rate);
1332
1333 if (todo)
1334 usleep(100000);
1335 }
1336
1337 while (nr_running) {
1338 reap_threads(&nr_running, &t_rate, &m_rate);
1339 usleep(10000);
1340 }
1341
1342 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001343 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001344}
1345
Jens Axboeebac4652005-12-08 15:25:21 +01001346int main(int argc, char *argv[])
1347{
Jens Axboe29d610e2007-02-06 13:25:33 +01001348 long ps;
1349
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001350 sinit();
1351
Jens Axboedbe11252007-02-11 00:19:51 +01001352 /*
1353 * We need locale for number printing, if it isn't set then just
1354 * go with the US format.
1355 */
1356 if (!getenv("LC_NUMERIC"))
1357 setlocale(LC_NUMERIC, "en_US");
1358
Jens Axboeebac4652005-12-08 15:25:21 +01001359 if (parse_options(argc, argv))
1360 return 1;
1361
Jens Axboe4b472fa2007-04-04 11:04:34 +02001362 if (!thread_number)
1363 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001364
Jens Axboe29d610e2007-02-06 13:25:33 +01001365 ps = sysconf(_SC_PAGESIZE);
1366 if (ps < 0) {
1367 log_err("Failed to get page size\n");
1368 return 1;
1369 }
1370
Jens Axboecfc99db2007-03-14 10:34:47 +01001371 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001372 page_mask = ps - 1;
1373
Jens Axboebb3884d2007-01-17 17:23:11 +11001374 if (write_bw_log) {
1375 setup_log(&agg_io_log[DDIR_READ]);
1376 setup_log(&agg_io_log[DDIR_WRITE]);
1377 }
1378
Jens Axboecdd18ad2008-02-27 18:58:00 +01001379 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001380
Jens Axboea2f77c92007-02-22 11:53:00 +01001381 set_genesis_time();
1382
Jens Axboeccb0fa22008-05-30 23:18:00 +02001383 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001384
1385 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001386
Jens Axboebb3884d2007-01-17 17:23:11 +11001387 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001388 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001389 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001390 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1391 __finish_log(agg_io_log[DDIR_WRITE],
1392 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001393 }
1394 }
Jens Axboeebac4652005-12-08 15:25:21 +01001395
Jens Axboecdd18ad2008-02-27 18:58:00 +01001396 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001397 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001398}