blob: 08011c94b82480bf7a875b4a3d193733e8e954cd [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 Axboeb29ee5b2008-09-11 10:17:26 +020064void td_set_runstate(struct thread_data *td, int runstate)
Jens Axboe6ce15a32007-01-12 09:04:41 +010065{
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 Axboe02444ad2008-10-07 11:33:00 +0200110static void sig_alrm(int fio_unused 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 Axboe02444ad2008-10-07 11:33:00 +0200128static void sig_ill(int fio_unused sig)
Jens Axboede79c912008-08-04 21:43:55 +0200129{
130 if (!threads)
131 return;
132
133 log_err("fio: illegal instruction. your cpu does not support "
134 "the sse4.2 instruction for crc32c\n");
135 terminate_threads(TERMINATE_ALL);
136 exit(4);
137}
138
Jens Axboe697a6062008-05-31 00:04:45 +0200139static void set_sig_handlers(void)
140{
141 struct sigaction act;
142
143 memset(&act, 0, sizeof(act));
144 act.sa_handler = sig_alrm;
145 act.sa_flags = SA_RESTART;
146 sigaction(SIGALRM, &act, NULL);
147
148 memset(&act, 0, sizeof(act));
149 act.sa_handler = sig_int;
150 act.sa_flags = SA_RESTART;
151 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200152
153 memset(&act, 0, sizeof(act));
154 act.sa_handler = sig_ill;
155 act.sa_flags = SA_RESTART;
156 sigaction(SIGILL, &act, NULL);
Jens Axboe697a6062008-05-31 00:04:45 +0200157}
158
Jens Axboe993bf482008-11-14 13:04:53 +0100159static inline int should_check_rate(struct thread_data *td)
160{
161 /*
162 * No minimum rate set, always ok
163 */
164 if (!td->o.ratemin && !td->o.rate_iops_min)
165 return 0;
166
167 return 1;
168}
169
Jens Axboe697a6062008-05-31 00:04:45 +0200170/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200171 * Check if we are above the minimum rate given.
172 */
Jens Axboeebac4652005-12-08 15:25:21 +0100173static int check_min_rate(struct thread_data *td, struct timeval *now)
174{
Jens Axboe09042002007-02-23 10:56:22 +0100175 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100176 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100177 unsigned long spent;
178 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100179
180 /*
181 * allow a 2 second settle period in the beginning
182 */
183 if (mtime_since(&td->start, now) < 2000)
184 return 0;
185
Jens Axboe4e991c22007-03-15 11:41:11 +0100186 if (td_read(td)) {
187 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100188 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100189 }
190 if (td_write(td)) {
191 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100192 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100193 }
Jens Axboe09042002007-02-23 10:56:22 +0100194
Jens Axboeebac4652005-12-08 15:25:21 +0100195 /*
196 * if rate blocks is set, sample is running
197 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100198 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100199 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100200 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100201 return 0;
202
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100203 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100204 /*
205 * check bandwidth specified rate
206 */
207 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100208 log_err("%s: min rate %u not met\n", td->o.name,
209 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100210 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100211 } else {
212 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100213 if (rate < td->o.ratemin ||
214 bytes < td->rate_bytes) {
215 log_err("%s: min rate %u not met, got"
216 " %luKiB/sec\n", td->o.name,
217 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100218 return 1;
219 }
220 }
221 } else {
222 /*
223 * checks iops specified rate
224 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100225 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100226 log_err("%s: min iops rate %u not met\n",
227 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100228 return 1;
229 } else {
230 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100231 if (rate < td->o.rate_iops_min ||
232 iops < td->rate_blocks) {
233 log_err("%s: min iops rate %u not met,"
234 " got %lu\n", td->o.name,
235 td->o.rate_iops_min,
236 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100237 }
Jens Axboe413dd452007-02-23 09:26:09 +0100238 }
Jens Axboeebac4652005-12-08 15:25:21 +0100239 }
240 }
241
Jens Axboe09042002007-02-23 10:56:22 +0100242 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100243 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100244 memcpy(&td->lastrate, now, sizeof(*now));
245 return 0;
246}
247
248static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
249{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100250 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100251 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100252 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100253 return 1;
254
255 return 0;
256}
257
Jens Axboe906c8d72006-06-13 09:37:56 +0200258/*
259 * When job exits, we can cancel the in-flight IO if we are using async
260 * io. Attempt to do so.
261 */
Jens Axboeebac4652005-12-08 15:25:21 +0100262static void cleanup_pending_aio(struct thread_data *td)
263{
Jens Axboe01743ee2008-06-02 12:19:19 +0200264 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100265 struct io_u *io_u;
266 int r;
267
268 /*
269 * get immediately available events, if any
270 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100271 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100272 if (r < 0)
273 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100274
275 /*
276 * now cancel remaining active events
277 */
Jens Axboe2866c822006-10-09 15:57:48 +0200278 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200279 flist_for_each_safe(entry, n, &td->io_u_busylist) {
280 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100281
Jens Axboe0c6e7512007-02-22 11:19:39 +0100282 /*
283 * if the io_u isn't in flight, then that generally
284 * means someone leaked an io_u. complain but fix
285 * it up, so we don't stall here.
286 */
287 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
288 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100289 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100290 } else {
291 r = td->io_ops->cancel(td, io_u);
292 if (!r)
293 put_io_u(td, io_u);
294 }
Jens Axboeebac4652005-12-08 15:25:21 +0100295 }
296 }
297
Jens Axboe97601022007-02-18 12:47:29 +0100298 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100299 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100300}
301
Jens Axboe906c8d72006-06-13 09:37:56 +0200302/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200303 * Helper to handle the final sync of a file. Works just like the normal
304 * io path, just does everything sync.
305 */
306static int fio_io_sync(struct thread_data *td, struct fio_file *f)
307{
308 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200309 int ret;
310
311 if (!io_u)
312 return 1;
313
314 io_u->ddir = DDIR_SYNC;
315 io_u->file = f;
316
317 if (td_io_prep(td, io_u)) {
318 put_io_u(td, io_u);
319 return 1;
320 }
321
Jens Axboe755200a2007-02-19 13:08:12 +0100322requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200323 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100324 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100325 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200326 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200327 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100328 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100329 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100330 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100331 } else if (ret == FIO_Q_COMPLETED) {
332 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100333 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100334 return 1;
335 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200336
Jens Axboed7762cf2007-02-23 12:34:57 +0100337 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100338 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100339 } else if (ret == FIO_Q_BUSY) {
340 if (td_io_commit(td))
341 return 1;
342 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200343 }
344
345 return 0;
346}
347
Jens Axboe993bf482008-11-14 13:04:53 +0100348static inline void update_tv_cache(struct thread_data *td)
349{
350 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
351 fio_gettime(&td->tv_cache, NULL);
352}
353
Jens Axboe858a3d42006-10-24 14:54:44 +0200354/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100355 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200356 * reads the blocks back in, and checks the crc/md5 of the data.
357 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100358static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100359{
Jens Axboe53cdc682006-10-18 11:50:58 +0200360 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100361 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100362 int ret, min_events;
363 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200364
365 /*
366 * sync io first and invalidate cache, to make sure we really
367 * read from disk.
368 */
369 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100370 if (!(f->flags & FIO_FILE_OPEN))
371 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100372 if (fio_io_sync(td, f))
373 break;
374 if (file_invalidate_cache(td, f))
375 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200376 }
Jens Axboeebac4652005-12-08 15:25:21 +0100377
Jens Axboeb2fdda42007-02-20 20:52:51 +0100378 if (td->error)
379 return;
380
Jens Axboeebac4652005-12-08 15:25:21 +0100381 td_set_runstate(td, TD_VERIFYING);
382
Jens Axboe36167d82007-02-18 05:41:31 +0100383 io_u = NULL;
384 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200385 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100386
Jens Axboeebac4652005-12-08 15:25:21 +0100387 io_u = __get_io_u(td);
388 if (!io_u)
389 break;
390
Jens Axboe993bf482008-11-14 13:04:53 +0100391 update_tv_cache(td);
392
393 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe069c2912007-02-21 23:02:39 +0100394 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200395 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200396 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100397 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200398
Jens Axboe069c2912007-02-21 23:02:39 +0100399 if (get_next_verify(td, io_u)) {
400 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100401 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100402 }
Jens Axboeebac4652005-12-08 15:25:21 +0100403
Jens Axboe069c2912007-02-21 23:02:39 +0100404 if (td_io_prep(td, io_u)) {
405 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100406 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100407 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100408
409 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100410
Jens Axboe11786802007-03-05 18:33:22 +0100411 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100412 switch (ret) {
413 case FIO_Q_COMPLETED:
414 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100415 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100416 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100417 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200418 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100419
Jens Axboe9e9d1642007-03-12 09:37:46 +0100420 /*
421 * zero read, fail
422 */
423 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200424 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100425 put_io_u(td, io_u);
426 break;
427 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200428
Jens Axboe36167d82007-02-18 05:41:31 +0100429 io_u->xfer_buflen = io_u->resid;
430 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200431 io_u->offset += bytes;
432
Jens Axboe30061b92007-04-17 13:31:34 +0200433 td->ts.short_io_u[io_u->ddir]++;
434
Jens Axboed460eb32007-04-16 21:39:33 +0200435 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200436 goto sync_done;
437
Jens Axboe11786802007-03-05 18:33:22 +0100438 requeue_io_u(td, &io_u);
439 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200440sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100441 ret = io_u_sync_complete(td, io_u);
442 if (ret < 0)
443 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100444 }
Jens Axboe36167d82007-02-18 05:41:31 +0100445 continue;
446 case FIO_Q_QUEUED:
447 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100448 case FIO_Q_BUSY:
449 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100450 ret2 = td_io_commit(td);
451 if (ret2 < 0)
452 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100453 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100454 default:
455 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100456 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100457 break;
458 }
459
Jens Axboe99784632007-02-19 10:06:17 +0100460 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100461 break;
462
Jens Axboe3af6ef32007-02-18 06:57:43 +0100463 /*
464 * if we can queue more, do so. but check if there are
465 * completed io_u's first.
466 */
Jens Axboe49504212008-06-05 09:03:30 +0200467 full = queue_full(td) || ret == FIO_Q_BUSY;
468 if (full || !td->o.iodepth_batch_complete) {
469 min_events = td->o.iodepth_batch_complete;
470 if (full && !min_events)
Jens Axboe838bc702008-05-22 13:08:23 +0200471 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100472
Jens Axboe49504212008-06-05 09:03:30 +0200473 do {
474 /*
475 * Reap required number of io units, if any,
476 * and do the verification on them through
477 * the callback handler
478 */
479 if (io_u_queued_complete(td, min_events) < 0) {
480 ret = -1;
481 break;
482 }
483 } while (full && (td->cur_depth > td->o.iodepth_low));
484 }
485 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100486 break;
487 }
488
Jens Axboec01c0392007-02-26 12:43:42 +0100489 if (!td->error) {
490 min_events = td->cur_depth;
491
492 if (min_events)
493 ret = io_u_queued_complete(td, min_events);
494 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100495 cleanup_pending_aio(td);
496
497 td_set_runstate(td, TD_RUNNING);
498}
499
Jens Axboe32cd46a2006-06-07 13:40:40 +0200500/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200501 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200502 * and reaps them, checking for rate and errors along the way.
503 */
Jens Axboeebac4652005-12-08 15:25:21 +0100504static void do_io(struct thread_data *td)
505{
Jens Axboeebac4652005-12-08 15:25:21 +0100506 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100507 unsigned int i;
508 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100509
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200510 if (in_ramp_time(td))
511 td_set_runstate(td, TD_RAMP);
512 else
513 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200514
Jens Axboe7bb48f82007-03-27 15:30:28 +0200515 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100516 struct timeval comp_time;
517 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200518 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100519 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200520 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100521
522 if (td->terminate)
523 break;
524
Jens Axboe3d7c3912007-02-19 13:16:12 +0100525 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100526 if (!io_u)
527 break;
528
Jens Axboe993bf482008-11-14 13:04:53 +0100529 update_tv_cache(td);
Jens Axboe97601022007-02-18 12:47:29 +0100530
Jens Axboe993bf482008-11-14 13:04:53 +0100531 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe97601022007-02-18 12:47:29 +0100532 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200533 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100534 break;
535 }
Jens Axboe36167d82007-02-18 05:41:31 +0100536
Jens Axboeb67740d2007-07-26 12:22:30 +0200537 /*
538 * Add verification end_io handler, if asked to verify
539 * a previously written file.
540 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100541 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200542 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100543 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200544 } else if (in_ramp_time(td))
545 td_set_runstate(td, TD_RAMP);
546 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100547 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200548
Jens Axboe11786802007-03-05 18:33:22 +0100549 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100550 switch (ret) {
551 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100552 if (io_u->error)
553 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100554 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100555 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200556 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100557
Jens Axboe9e9d1642007-03-12 09:37:46 +0100558 /*
559 * zero read, fail
560 */
561 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200562 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100563 put_io_u(td, io_u);
564 break;
565 }
566
Jens Axboe36167d82007-02-18 05:41:31 +0100567 io_u->xfer_buflen = io_u->resid;
568 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200569 io_u->offset += bytes;
570
Jens Axboe30061b92007-04-17 13:31:34 +0200571 td->ts.short_io_u[io_u->ddir]++;
572
Jens Axboed460eb32007-04-16 21:39:33 +0200573 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200574 goto sync_done;
575
Jens Axboe11786802007-03-05 18:33:22 +0100576 requeue_io_u(td, &io_u);
577 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200578sync_done:
Jens Axboe993bf482008-11-14 13:04:53 +0100579 if (should_check_rate(td))
580 fio_gettime(&comp_time, NULL);
581
Jens Axboe11786802007-03-05 18:33:22 +0100582 bytes_done = io_u_sync_complete(td, io_u);
583 if (bytes_done < 0)
584 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100585 }
Jens Axboe36167d82007-02-18 05:41:31 +0100586 break;
587 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100588 /*
589 * if the engine doesn't have a commit hook,
590 * the io_u is really queued. if it does have such
591 * a hook, it has to call io_u_queued() itself.
592 */
593 if (td->io_ops->commit == NULL)
594 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100595 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100596 case FIO_Q_BUSY:
597 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100598 ret2 = td_io_commit(td);
599 if (ret2 < 0)
600 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100601 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100602 default:
603 assert(ret < 0);
604 put_io_u(td, io_u);
605 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100606 }
607
Jens Axboe99784632007-02-19 10:06:17 +0100608 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100609 break;
610
Jens Axboe97601022007-02-18 12:47:29 +0100611 /*
612 * See if we need to complete some commands
613 */
Jens Axboe49504212008-06-05 09:03:30 +0200614 full = queue_full(td) || ret == FIO_Q_BUSY;
615 if (full || !td->o.iodepth_batch_complete) {
616 min_evts = td->o.iodepth_batch_complete;
617 if (full && !min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100618 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200619
Jens Axboe993bf482008-11-14 13:04:53 +0100620 if (should_check_rate(td))
621 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200622
623 do {
624 ret = io_u_queued_complete(td, min_evts);
625 if (ret <= 0)
626 break;
627
628 bytes_done += ret;
629 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100630 }
631
Jens Axboe49504212008-06-05 09:03:30 +0200632 if (ret < 0)
633 break;
Jens Axboe97601022007-02-18 12:47:29 +0100634 if (!bytes_done)
635 continue;
636
Jens Axboeebac4652005-12-08 15:25:21 +0100637 /*
638 * the rate is batched for now, it should work for batches
639 * of completions except the very first one which may look
640 * a little bursty
641 */
Jens Axboe993bf482008-11-14 13:04:53 +0100642 if (!in_ramp_time(td) && should_check_rate(td)) {
643 usec = utime_since(&td->tv_cache, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100644
Jens Axboe721938a2008-09-10 09:46:16 +0200645 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100646
Jens Axboe721938a2008-09-10 09:46:16 +0200647 if (check_min_rate(td, &comp_time)) {
648 if (exitall_on_terminate)
649 terminate_threads(td->groupid);
650 td_verror(td, EIO, "check_min_rate");
651 break;
652 }
Jens Axboeebac4652005-12-08 15:25:21 +0100653 }
654
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100655 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100656 unsigned long long b;
657
658 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100659 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100660 int left;
661
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100662 if (td->o.thinktime_spin)
663 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100664
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100665 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100666 if (left)
667 usec_sleep(td, left);
668 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100669 }
Jens Axboeebac4652005-12-08 15:25:21 +0100670 }
671
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100672 if (td->o.fill_device && td->error == ENOSPC) {
673 td->error = 0;
674 td->terminate = 1;
675 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100676 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100677 struct fio_file *f;
678
Jens Axboec01c0392007-02-26 12:43:42 +0100679 i = td->cur_depth;
680 if (i)
681 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100682
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100683 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200684 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100685
686 for_each_file(td, f, i) {
687 if (!(f->flags & FIO_FILE_OPEN))
688 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200689 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100690 }
Jens Axboe84585002006-10-19 20:26:22 +0200691 }
Jens Axboec01c0392007-02-26 12:43:42 +0100692 } else
693 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100694
695 /*
696 * stop job if we failed doing any IO
697 */
698 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
699 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100700}
701
Jens Axboeebac4652005-12-08 15:25:21 +0100702static void cleanup_io_u(struct thread_data *td)
703{
Jens Axboe01743ee2008-06-02 12:19:19 +0200704 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100705 struct io_u *io_u;
706
Jens Axboe01743ee2008-06-02 12:19:19 +0200707 flist_for_each_safe(entry, n, &td->io_u_freelist) {
708 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100709
Jens Axboe01743ee2008-06-02 12:19:19 +0200710 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100711 free(io_u);
712 }
713
Jens Axboe2f9ade32006-10-20 11:25:52 +0200714 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100715}
716
717static int init_io_u(struct thread_data *td)
718{
719 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100720 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100721 int i, max_units;
722 char *p;
723
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100724 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100725 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100726 td->orig_buffer_size = (unsigned long long) max_bs
727 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100728
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100729 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
730 unsigned long bs;
731
732 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
733 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
734 }
Jens Axboeebac4652005-12-08 15:25:21 +0100735
Jens Axboec3990642007-07-19 10:17:09 +0200736 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
737 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
738 return 1;
739 }
740
Jens Axboe2f9ade32006-10-20 11:25:52 +0200741 if (allocate_io_mem(td))
742 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100743
Jens Axboeb3f378e2007-07-20 12:39:22 +0200744 if (td->o.odirect)
745 p = ALIGN(td->orig_buffer);
746 else
747 p = td->orig_buffer;
748
Jens Axboeebac4652005-12-08 15:25:21 +0100749 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200750 if (td->terminate)
751 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100752 io_u = malloc(sizeof(*io_u));
753 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200754 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100755
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200756 if (!(td->io_ops->flags & FIO_NOIO)) {
757 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200758
Jens Axboe5973caf2008-05-21 19:52:35 +0200759 if (td_write(td) && !td->o.refill_buffers)
760 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200761 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100762
Jens Axboeb1ff3402006-02-17 11:35:58 +0100763 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100764 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200765 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100766 }
767
768 return 0;
769}
770
Jens Axboeda867742006-06-06 10:39:10 -0700771static int switch_ioscheduler(struct thread_data *td)
772{
773 char tmp[256], tmp2[128];
774 FILE *f;
775 int ret;
776
Jens Axboeba0fbe12007-03-09 14:34:23 +0100777 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200778 return 0;
779
Jens Axboeda867742006-06-06 10:39:10 -0700780 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
781
782 f = fopen(tmp, "r+");
783 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200784 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100785 log_err("fio: os or kernel doesn't support IO scheduler"
786 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200787 return 0;
788 }
789 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700790 return 1;
791 }
792
793 /*
794 * Set io scheduler.
795 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100796 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700797 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100798 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700799 fclose(f);
800 return 1;
801 }
802
803 rewind(f);
804
805 /*
806 * Read back and check that the selected scheduler is now the default.
807 */
808 ret = fread(tmp, 1, sizeof(tmp), f);
809 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100810 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700811 fclose(f);
812 return 1;
813 }
814
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100815 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700816 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100817 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100818 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700819 fclose(f);
820 return 1;
821 }
822
823 fclose(f);
824 return 0;
825}
826
Jens Axboe492158c2007-05-24 10:32:47 +0200827static int keep_running(struct thread_data *td)
828{
829 unsigned long long io_done;
830
Jens Axboe20e354e2007-07-23 14:36:16 +0200831 if (td->done)
832 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200833 if (td->o.time_based)
834 return 1;
835 if (td->o.loops) {
836 td->o.loops--;
837 return 1;
838 }
839
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100840 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
841 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200842 if (io_done < td->o.size)
843 return 1;
844
845 return 0;
846}
847
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200848static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100849{
Jens Axboe756867b2007-03-06 15:19:24 +0100850 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100851 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100852 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100853 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100854 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100855 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100856
Jens Axboec1324df2007-02-19 19:06:41 +0100857 td->last_was_sync = 0;
858
Jens Axboe2ba1c292008-02-01 13:16:38 +0100859 /*
860 * reset file done count if we are to start over
861 */
862 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200863 td->nr_done_files = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200864}
865
866void reset_all_stats(struct thread_data *td)
867{
868 struct timeval tv;
869 int i;
870
871 reset_io_counters(td);
872
873 for (i = 0; i < 2; i++) {
874 td->io_bytes[i] = 0;
875 td->io_blocks[i] = 0;
876 td->io_issues[i] = 0;
877 td->ts.total_io_u[i] = 0;
878 }
879
880 fio_gettime(&tv, NULL);
881 memcpy(&td->epoch, &tv, sizeof(tv));
882 memcpy(&td->start, &tv, sizeof(tv));
883}
884
885static int clear_io_state(struct thread_data *td)
886{
887 struct fio_file *f;
888 unsigned int i;
889 int ret;
890
891 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +0200892
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100893 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100894
895 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200896 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200897 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100898 ret = td_io_open_file(td, f);
899 if (ret)
900 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200901 }
Jens Axboea978ba62007-03-08 14:29:03 +0100902
903 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100904}
905
Jens Axboe906c8d72006-06-13 09:37:56 +0200906/*
907 * Entry point for the thread based jobs. The process based jobs end up
908 * here as well, after a little setup.
909 */
Jens Axboeebac4652005-12-08 15:25:21 +0100910static void *thread_main(void *data)
911{
Shawn Lewis10927312007-11-21 09:38:13 +0100912 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100913 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100914 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100915
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100916 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100917 setsid();
918
919 td->pid = getpid();
920
Jens Axboe5921e802008-05-30 15:02:38 +0200921 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100922
Jens Axboe01743ee2008-06-02 12:19:19 +0200923 INIT_FLIST_HEAD(&td->io_u_freelist);
924 INIT_FLIST_HEAD(&td->io_u_busylist);
925 INIT_FLIST_HEAD(&td->io_u_requeues);
926 INIT_FLIST_HEAD(&td->io_log_list);
927 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200928 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200929
Jens Axboe75154842006-06-01 13:56:09 +0200930 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100931 fio_mutex_up(startup_mutex);
932 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100933
Jens Axboe37f56872007-03-09 12:42:35 +0100934 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100935 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100936 * eating a file descriptor
937 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100938 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100939
Jens Axboed84f8d42007-05-16 08:47:46 +0200940 /*
941 * May alter parameters that init_io_u() will use, so we need to
942 * do this first.
943 */
944 if (init_iolog(td))
945 goto err;
946
Jens Axboedb1cd902007-04-26 13:47:07 +0200947 if (init_io_u(td))
948 goto err;
949
Jens Axboe375b2692007-05-22 09:13:02 +0200950 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200951 td_verror(td, errno, "cpu_set_affinity");
952 goto err;
953 }
954
Jens Axboeac684782007-11-08 08:29:07 +0100955 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200956 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
957 td_verror(td, errno, "ioprio_set");
958 goto err;
959 }
960 }
961
962 if (nice(td->o.nice) == -1) {
963 td_verror(td, errno, "nice");
964 goto err;
965 }
966
967 if (td->o.ioscheduler && switch_ioscheduler(td))
968 goto err;
969
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100970 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100971 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100972
973 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100974 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100975
Jens Axboeb5af8292007-03-08 12:43:13 +0100976 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100977 goto err;
978
Jens Axboe68727072007-03-15 20:49:25 +0100979 if (init_random_map(td))
980 goto err;
981
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100982 if (td->o.exec_prerun) {
983 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100984 goto err;
985 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200986
Jens Axboe69008992006-11-24 13:12:56 +0100987 fio_gettime(&td->epoch, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100988 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100989
990 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100991 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200992 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100993 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100994 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboe993bf482008-11-14 13:04:53 +0100995 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100996
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100997 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100998 memcpy(&td->lastrate, &td->ts.stat_sample_time,
999 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +01001000
Jens Axboea978ba62007-03-08 14:29:03 +01001001 if (clear_state && clear_io_state(td))
1002 break;
1003
Jens Axboeebac4652005-12-08 15:25:21 +01001004 prune_io_piece_log(td);
1005
Jens Axboeba0fbe12007-03-09 14:34:23 +01001006 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001007
Jens Axboea978ba62007-03-08 14:29:03 +01001008 clear_state = 1;
1009
Jens Axboe38d77ca2007-03-21 14:20:20 +01001010 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1011 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001012 elapsed = utime_since(&td->start,
1013 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001014 else
1015 elapsed = utime_since_now(&td->start);
1016
1017 runtime[DDIR_READ] += elapsed;
1018 }
1019 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1020 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001021 elapsed = utime_since(&td->start,
1022 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001023 else
1024 elapsed = utime_since_now(&td->start);
1025
1026 runtime[DDIR_WRITE] += elapsed;
1027 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001028
Jens Axboeebac4652005-12-08 15:25:21 +01001029 if (td->error || td->terminate)
1030 break;
1031
Shawn Lewise84c73a2007-08-02 22:19:32 +02001032 if (!td->o.do_verify ||
1033 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001034 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001035 continue;
1036
Jens Axboea978ba62007-03-08 14:29:03 +01001037 if (clear_io_state(td))
1038 break;
1039
Jens Axboe02bcaa82006-11-24 10:42:00 +01001040 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001041
1042 do_verify(td);
1043
Jens Axboe69008992006-11-24 13:12:56 +01001044 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001045
1046 if (td->error || td->terminate)
1047 break;
1048 }
1049
Jens Axboe36dff962006-11-24 13:29:20 +01001050 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001051 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1052 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001053 td->ts.total_run_time = mtime_since_now(&td->epoch);
1054 td->ts.io_bytes[0] = td->io_bytes[0];
1055 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001056
Jens Axboe756867b2007-03-06 15:19:24 +01001057 if (td->ts.bw_log)
1058 finish_log(td, td->ts.bw_log, "bw");
1059 if (td->ts.slat_log)
1060 finish_log(td, td->ts.slat_log, "slat");
1061 if (td->ts.clat_log)
1062 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001063 if (td->o.exec_postrun) {
1064 if (system(td->o.exec_postrun) < 0)
1065 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001066 }
Jens Axboeebac4652005-12-08 15:25:21 +01001067
1068 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001069 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001070
1071err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001072 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001073 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001074 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001075 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001076 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001077 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001078
1079 /*
1080 * do this very late, it will log file closing as well
1081 */
1082 if (td->o.write_iolog_file)
1083 write_iolog_close(td);
1084
Jens Axboed23bb322007-03-23 15:57:56 +01001085 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001086 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001087 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001088}
1089
Jens Axboe906c8d72006-06-13 09:37:56 +02001090/*
1091 * We cannot pass the td data into a forked process, so attach the td and
1092 * pass it to the thread worker.
1093 */
Jens Axboea6418142007-02-17 04:47:18 +01001094static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001095{
1096 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001097 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001098
1099 data = shmat(shmid, NULL, 0);
1100 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001101 int __err = errno;
1102
Jens Axboeebac4652005-12-08 15:25:21 +01001103 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001104 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001105 }
1106
1107 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001108 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001109 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001110 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001111}
1112
Jens Axboe906c8d72006-06-13 09:37:56 +02001113/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001114 * Run over the job map and reap the threads that have exited, if any.
1115 */
Jens Axboeebac4652005-12-08 15:25:21 +01001116static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1117{
Jens Axboe34572e22006-10-20 09:33:18 +02001118 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001119 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001120
1121 /*
1122 * reap exited threads (TD_EXITED -> TD_REAPED)
1123 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001124 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001125 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001126 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001127
Jens Axboe84585002006-10-19 20:26:22 +02001128 /*
1129 * ->io_ops is NULL for a thread that has closed its
1130 * io engine
1131 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001132 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001133 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001134 else
1135 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001136
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001137 if (!td->pid) {
1138 pending++;
1139 continue;
1140 }
1141 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001142 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001143 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001144 if (td->runstate == TD_EXITED) {
1145 td_set_runstate(td, TD_REAPED);
1146 goto reaped;
1147 }
1148 continue;
1149 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001150
1151 flags = WNOHANG;
1152 if (td->runstate == TD_EXITED)
1153 flags = 0;
1154
1155 /*
1156 * check if someone quit or got killed in an unusual way
1157 */
1158 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001159 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001160 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001161 log_err("fio: pid=%d disappeared %d\n",
1162 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001163 td_set_runstate(td, TD_REAPED);
1164 goto reaped;
1165 }
1166 perror("waitpid");
1167 } else if (ret == td->pid) {
1168 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001169 int sig = WTERMSIG(status);
1170
Jens Axboed9244942007-03-05 12:32:32 +01001171 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001172 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001173 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001174 td_set_runstate(td, TD_REAPED);
1175 goto reaped;
1176 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001177 if (WIFEXITED(status)) {
1178 if (WEXITSTATUS(status) && !td->error)
1179 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001180
Jens Axboea2f77c92007-02-22 11:53:00 +01001181 td_set_runstate(td, TD_REAPED);
1182 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001183 }
1184 }
Jens Axboeebac4652005-12-08 15:25:21 +01001185
Jens Axboea2f77c92007-02-22 11:53:00 +01001186 /*
1187 * thread is not dead, continue
1188 */
gurudas paie48676b2007-04-11 12:44:27 +02001189 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001190 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001191reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001192 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001193 (*m_rate) -= td->o.ratemin;
1194 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001195 if (!td->pid)
1196 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001197
1198 if (td->error)
1199 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001200
1201 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001202 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001203
Jens Axboe1f809d12007-10-25 18:34:02 +02001204 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001205 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001206}
1207
Jens Axboe906c8d72006-06-13 09:37:56 +02001208/*
1209 * Main function for kicking off and reaping jobs, as needed.
1210 */
Jens Axboeebac4652005-12-08 15:25:21 +01001211static void run_threads(void)
1212{
Jens Axboeebac4652005-12-08 15:25:21 +01001213 struct thread_data *td;
1214 unsigned long spent;
1215 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001216
Jens Axboe2f9ade32006-10-20 11:25:52 +02001217 if (fio_pin_memory())
1218 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001219
Jens Axboec6ae0a52006-06-12 11:04:44 +02001220 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001221 printf("Starting ");
1222 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001223 printf("%d thread%s", nr_thread,
1224 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001225 if (nr_process) {
1226 if (nr_thread)
1227 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001228 printf("%d process%s", nr_process,
1229 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001230 }
1231 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001232 fflush(stdout);
1233 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001234
Jens Axboe697a6062008-05-31 00:04:45 +02001235 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001236
Jens Axboeebac4652005-12-08 15:25:21 +01001237 todo = thread_number;
1238 nr_running = 0;
1239 nr_started = 0;
1240 m_rate = t_rate = 0;
1241
Jens Axboe34572e22006-10-20 09:33:18 +02001242 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001243 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001244
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001245 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001246 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001247 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001248 }
Jens Axboeebac4652005-12-08 15:25:21 +01001249
1250 /*
1251 * do file setup here so it happens sequentially,
1252 * we don't want X number of threads getting their
1253 * client data interspersed on disk
1254 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001255 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001256 exit_value++;
1257 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001258 log_err("fio: pid=%d, err=%d/%s\n",
1259 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001260 td_set_runstate(td, TD_REAPED);
1261 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001262 } else {
1263 struct fio_file *f;
1264 unsigned int i;
1265
1266 /*
1267 * for sharing to work, each job must always open
1268 * its own files. so close them, if we opened them
1269 * for creation
1270 */
1271 for_each_file(td, f, i)
1272 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001273 }
Jens Axboe380cf262007-01-11 14:01:27 +01001274
1275 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001276 }
1277
Jens Axboea2f77c92007-02-22 11:53:00 +01001278 set_genesis_time();
1279
Jens Axboeebac4652005-12-08 15:25:21 +01001280 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001281 struct thread_data *map[MAX_JOBS];
1282 struct timeval this_start;
1283 int this_jobs = 0, left;
1284
Jens Axboeebac4652005-12-08 15:25:21 +01001285 /*
1286 * create threads (TD_NOT_CREATED -> TD_CREATED)
1287 */
Jens Axboe34572e22006-10-20 09:33:18 +02001288 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001289 if (td->runstate != TD_NOT_CREATED)
1290 continue;
1291
1292 /*
1293 * never got a chance to start, killed by other
1294 * thread for some reason
1295 */
1296 if (td->terminate) {
1297 todo--;
1298 continue;
1299 }
1300
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001301 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001302 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001303
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001304 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001305 continue;
1306 }
1307
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001308 if (td->o.stonewall && (nr_started || nr_running)) {
1309 dprint(FD_PROCESS, "%s: stonewall wait\n",
1310 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001311 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001312 }
Jens Axboeebac4652005-12-08 15:25:21 +01001313
Jens Axboe75154842006-06-01 13:56:09 +02001314 /*
1315 * Set state to created. Thread will transition
1316 * to TD_INITIALIZED when it's done setting up.
1317 */
Jens Axboeebac4652005-12-08 15:25:21 +01001318 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001319 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001320 nr_started++;
1321
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001322 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001323 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001324 if (pthread_create(&td->thread, NULL,
1325 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001326 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001327 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001328 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001329 }
Jens Axboef4705a72008-03-10 10:52:22 +01001330 if (pthread_detach(td->thread) < 0)
1331 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001332 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001333 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001334 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001335 pid = fork();
1336 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001337 int ret = fork_main(shm_id, i);
1338
Jens Axboe5e1d3062008-05-23 11:55:53 +02001339 _exit(ret);
1340 } else if (i == fio_debug_jobno)
1341 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001342 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001343 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001344 }
1345
1346 /*
Jens Axboe75154842006-06-01 13:56:09 +02001347 * Wait for the started threads to transition to
1348 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001349 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001350 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001351 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001352 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001353 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1354 break;
1355
1356 usleep(100000);
1357
1358 for (i = 0; i < this_jobs; i++) {
1359 td = map[i];
1360 if (!td)
1361 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001362 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001363 map[i] = NULL;
1364 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001365 } else if (td->runstate >= TD_EXITED) {
1366 map[i] = NULL;
1367 left--;
1368 todo--;
1369 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001370 }
1371 }
1372 }
1373
1374 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001375 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001376 for (i = 0; i < this_jobs; i++) {
1377 td = map[i];
1378 if (!td)
1379 continue;
1380 kill(td->pid, SIGTERM);
1381 }
1382 break;
1383 }
1384
1385 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001386 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001387 */
Jens Axboe34572e22006-10-20 09:33:18 +02001388 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001389 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001390 continue;
1391
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001392 if (in_ramp_time(td))
1393 td_set_runstate(td, TD_RAMP);
1394 else
1395 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001396 nr_running++;
1397 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001398 m_rate += td->o.ratemin;
1399 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001400 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001401 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001402 }
1403
1404 reap_threads(&nr_running, &t_rate, &m_rate);
1405
1406 if (todo)
1407 usleep(100000);
1408 }
1409
1410 while (nr_running) {
1411 reap_threads(&nr_running, &t_rate, &m_rate);
1412 usleep(10000);
1413 }
1414
1415 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001416 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001417}
1418
Jens Axboeebac4652005-12-08 15:25:21 +01001419int main(int argc, char *argv[])
1420{
Jens Axboe29d610e2007-02-06 13:25:33 +01001421 long ps;
1422
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001423 sinit();
1424
Jens Axboedbe11252007-02-11 00:19:51 +01001425 /*
1426 * We need locale for number printing, if it isn't set then just
1427 * go with the US format.
1428 */
1429 if (!getenv("LC_NUMERIC"))
1430 setlocale(LC_NUMERIC, "en_US");
1431
Jens Axboeebac4652005-12-08 15:25:21 +01001432 if (parse_options(argc, argv))
1433 return 1;
1434
Jens Axboe4b472fa2007-04-04 11:04:34 +02001435 if (!thread_number)
1436 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001437
Jens Axboe29d610e2007-02-06 13:25:33 +01001438 ps = sysconf(_SC_PAGESIZE);
1439 if (ps < 0) {
1440 log_err("Failed to get page size\n");
1441 return 1;
1442 }
1443
Jens Axboecfc99db2007-03-14 10:34:47 +01001444 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001445 page_mask = ps - 1;
1446
Jens Axboebb3884d2007-01-17 17:23:11 +11001447 if (write_bw_log) {
1448 setup_log(&agg_io_log[DDIR_READ]);
1449 setup_log(&agg_io_log[DDIR_WRITE]);
1450 }
1451
Jens Axboecdd18ad2008-02-27 18:58:00 +01001452 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001453
Jens Axboea2f77c92007-02-22 11:53:00 +01001454 set_genesis_time();
1455
Jens Axboeccb0fa22008-05-30 23:18:00 +02001456 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001457
1458 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001459
Jens Axboebb3884d2007-01-17 17:23:11 +11001460 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001461 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001462 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001463 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1464 __finish_log(agg_io_log[DDIR_WRITE],
1465 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001466 }
1467 }
Jens Axboeebac4652005-12-08 15:25:21 +01001468
Jens Axboecdd18ad2008-02-27 18:58:00 +01001469 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001470 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001471}