blob: 5a87ae4b000fe377ad9970fa7971f725c287c020 [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
Jens Axboe5f8f9722008-11-18 19:10:21 +0100119/*
120 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
121 */
122static void sig_quit(int sig)
123{
124}
125
Jens Axboe697a6062008-05-31 00:04:45 +0200126static void sig_int(int sig)
127{
128 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100129 printf("\nfio: terminating on signal %d\n", sig);
130 fflush(stdout);
131 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100132 }
133}
134
Jens Axboe02444ad2008-10-07 11:33:00 +0200135static void sig_ill(int fio_unused sig)
Jens Axboede79c912008-08-04 21:43:55 +0200136{
137 if (!threads)
138 return;
139
140 log_err("fio: illegal instruction. your cpu does not support "
141 "the sse4.2 instruction for crc32c\n");
142 terminate_threads(TERMINATE_ALL);
143 exit(4);
144}
145
Jens Axboe697a6062008-05-31 00:04:45 +0200146static void set_sig_handlers(void)
147{
148 struct sigaction act;
149
150 memset(&act, 0, sizeof(act));
151 act.sa_handler = sig_alrm;
152 act.sa_flags = SA_RESTART;
153 sigaction(SIGALRM, &act, NULL);
154
155 memset(&act, 0, sizeof(act));
156 act.sa_handler = sig_int;
157 act.sa_flags = SA_RESTART;
158 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200159
160 memset(&act, 0, sizeof(act));
161 act.sa_handler = sig_ill;
162 act.sa_flags = SA_RESTART;
163 sigaction(SIGILL, &act, NULL);
Jens Axboe5f8f9722008-11-18 19:10:21 +0100164
165 memset(&act, 0, sizeof(act));
166 act.sa_handler = sig_quit;
167 act.sa_flags = SA_RESTART;
168 sigaction(SIGQUIT, &act, NULL);
Jens Axboe697a6062008-05-31 00:04:45 +0200169}
170
Jens Axboe993bf482008-11-14 13:04:53 +0100171static inline int should_check_rate(struct thread_data *td)
172{
173 /*
174 * No minimum rate set, always ok
175 */
176 if (!td->o.ratemin && !td->o.rate_iops_min)
177 return 0;
178
179 return 1;
180}
181
Jens Axboe697a6062008-05-31 00:04:45 +0200182/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200183 * Check if we are above the minimum rate given.
184 */
Jens Axboeebac4652005-12-08 15:25:21 +0100185static int check_min_rate(struct thread_data *td, struct timeval *now)
186{
Jens Axboe09042002007-02-23 10:56:22 +0100187 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100188 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100189 unsigned long spent;
190 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100191
192 /*
193 * allow a 2 second settle period in the beginning
194 */
195 if (mtime_since(&td->start, now) < 2000)
196 return 0;
197
Jens Axboe4e991c22007-03-15 11:41:11 +0100198 if (td_read(td)) {
199 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100200 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100201 }
202 if (td_write(td)) {
203 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100204 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100205 }
Jens Axboe09042002007-02-23 10:56:22 +0100206
Jens Axboeebac4652005-12-08 15:25:21 +0100207 /*
208 * if rate blocks is set, sample is running
209 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100210 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100211 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100212 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100213 return 0;
214
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100215 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100216 /*
217 * check bandwidth specified rate
218 */
219 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100220 log_err("%s: min rate %u not met\n", td->o.name,
221 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100222 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100223 } else {
224 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100225 if (rate < td->o.ratemin ||
226 bytes < td->rate_bytes) {
227 log_err("%s: min rate %u not met, got"
228 " %luKiB/sec\n", td->o.name,
229 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100230 return 1;
231 }
232 }
233 } else {
234 /*
235 * checks iops specified rate
236 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100237 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100238 log_err("%s: min iops rate %u not met\n",
239 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100240 return 1;
241 } else {
242 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100243 if (rate < td->o.rate_iops_min ||
244 iops < td->rate_blocks) {
245 log_err("%s: min iops rate %u not met,"
246 " got %lu\n", td->o.name,
247 td->o.rate_iops_min,
248 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100249 }
Jens Axboe413dd452007-02-23 09:26:09 +0100250 }
Jens Axboeebac4652005-12-08 15:25:21 +0100251 }
252 }
253
Jens Axboe09042002007-02-23 10:56:22 +0100254 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100255 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100256 memcpy(&td->lastrate, now, sizeof(*now));
257 return 0;
258}
259
260static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
261{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100262 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100263 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100264 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100265 return 1;
266
267 return 0;
268}
269
Jens Axboe906c8d72006-06-13 09:37:56 +0200270/*
271 * When job exits, we can cancel the in-flight IO if we are using async
272 * io. Attempt to do so.
273 */
Jens Axboeebac4652005-12-08 15:25:21 +0100274static void cleanup_pending_aio(struct thread_data *td)
275{
Jens Axboe01743ee2008-06-02 12:19:19 +0200276 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100277 struct io_u *io_u;
278 int r;
279
280 /*
281 * get immediately available events, if any
282 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100283 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100284 if (r < 0)
285 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100286
287 /*
288 * now cancel remaining active events
289 */
Jens Axboe2866c822006-10-09 15:57:48 +0200290 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200291 flist_for_each_safe(entry, n, &td->io_u_busylist) {
292 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100293
Jens Axboe0c6e7512007-02-22 11:19:39 +0100294 /*
295 * if the io_u isn't in flight, then that generally
296 * means someone leaked an io_u. complain but fix
297 * it up, so we don't stall here.
298 */
299 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
300 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100301 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100302 } else {
303 r = td->io_ops->cancel(td, io_u);
304 if (!r)
305 put_io_u(td, io_u);
306 }
Jens Axboeebac4652005-12-08 15:25:21 +0100307 }
308 }
309
Jens Axboe97601022007-02-18 12:47:29 +0100310 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100311 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100312}
313
Jens Axboe906c8d72006-06-13 09:37:56 +0200314/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200315 * Helper to handle the final sync of a file. Works just like the normal
316 * io path, just does everything sync.
317 */
318static int fio_io_sync(struct thread_data *td, struct fio_file *f)
319{
320 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200321 int ret;
322
323 if (!io_u)
324 return 1;
325
326 io_u->ddir = DDIR_SYNC;
327 io_u->file = f;
328
329 if (td_io_prep(td, io_u)) {
330 put_io_u(td, io_u);
331 return 1;
332 }
333
Jens Axboe755200a2007-02-19 13:08:12 +0100334requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200335 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100336 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100337 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200338 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200339 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100340 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100341 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100342 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100343 } else if (ret == FIO_Q_COMPLETED) {
344 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100345 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100346 return 1;
347 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200348
Jens Axboed7762cf2007-02-23 12:34:57 +0100349 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100350 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100351 } else if (ret == FIO_Q_BUSY) {
352 if (td_io_commit(td))
353 return 1;
354 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200355 }
356
357 return 0;
358}
359
Jens Axboe993bf482008-11-14 13:04:53 +0100360static inline void update_tv_cache(struct thread_data *td)
361{
362 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
363 fio_gettime(&td->tv_cache, NULL);
364}
365
Jens Axboe858a3d42006-10-24 14:54:44 +0200366/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100367 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200368 * reads the blocks back in, and checks the crc/md5 of the data.
369 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100370static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100371{
Jens Axboe53cdc682006-10-18 11:50:58 +0200372 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100373 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100374 int ret, min_events;
375 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200376
377 /*
378 * sync io first and invalidate cache, to make sure we really
379 * read from disk.
380 */
381 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100382 if (!(f->flags & FIO_FILE_OPEN))
383 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100384 if (fio_io_sync(td, f))
385 break;
386 if (file_invalidate_cache(td, f))
387 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200388 }
Jens Axboeebac4652005-12-08 15:25:21 +0100389
Jens Axboeb2fdda42007-02-20 20:52:51 +0100390 if (td->error)
391 return;
392
Jens Axboeebac4652005-12-08 15:25:21 +0100393 td_set_runstate(td, TD_VERIFYING);
394
Jens Axboe36167d82007-02-18 05:41:31 +0100395 io_u = NULL;
396 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200397 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100398
Jens Axboeebac4652005-12-08 15:25:21 +0100399 io_u = __get_io_u(td);
400 if (!io_u)
401 break;
402
Jens Axboe993bf482008-11-14 13:04:53 +0100403 update_tv_cache(td);
404
405 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe069c2912007-02-21 23:02:39 +0100406 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200407 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200408 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100409 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200410
Jens Axboe069c2912007-02-21 23:02:39 +0100411 if (get_next_verify(td, io_u)) {
412 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100413 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100414 }
Jens Axboeebac4652005-12-08 15:25:21 +0100415
Jens Axboe069c2912007-02-21 23:02:39 +0100416 if (td_io_prep(td, io_u)) {
417 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100418 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100419 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100420
421 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100422
Jens Axboe11786802007-03-05 18:33:22 +0100423 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100424 switch (ret) {
425 case FIO_Q_COMPLETED:
426 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100427 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100428 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100429 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200430 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100431
Jens Axboe9e9d1642007-03-12 09:37:46 +0100432 /*
433 * zero read, fail
434 */
435 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200436 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100437 put_io_u(td, io_u);
438 break;
439 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200440
Jens Axboe36167d82007-02-18 05:41:31 +0100441 io_u->xfer_buflen = io_u->resid;
442 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200443 io_u->offset += bytes;
444
Jens Axboe30061b92007-04-17 13:31:34 +0200445 td->ts.short_io_u[io_u->ddir]++;
446
Jens Axboed460eb32007-04-16 21:39:33 +0200447 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200448 goto sync_done;
449
Jens Axboe11786802007-03-05 18:33:22 +0100450 requeue_io_u(td, &io_u);
451 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200452sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100453 ret = io_u_sync_complete(td, io_u);
454 if (ret < 0)
455 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100456 }
Jens Axboe36167d82007-02-18 05:41:31 +0100457 continue;
458 case FIO_Q_QUEUED:
459 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100460 case FIO_Q_BUSY:
461 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100462 ret2 = td_io_commit(td);
463 if (ret2 < 0)
464 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100465 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100466 default:
467 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100468 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100469 break;
470 }
471
Jens Axboe99784632007-02-19 10:06:17 +0100472 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100473 break;
474
Jens Axboe3af6ef32007-02-18 06:57:43 +0100475 /*
476 * if we can queue more, do so. but check if there are
477 * completed io_u's first.
478 */
Jens Axboe49504212008-06-05 09:03:30 +0200479 full = queue_full(td) || ret == FIO_Q_BUSY;
480 if (full || !td->o.iodepth_batch_complete) {
481 min_events = td->o.iodepth_batch_complete;
482 if (full && !min_events)
Jens Axboe838bc702008-05-22 13:08:23 +0200483 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100484
Jens Axboe49504212008-06-05 09:03:30 +0200485 do {
486 /*
487 * Reap required number of io units, if any,
488 * and do the verification on them through
489 * the callback handler
490 */
491 if (io_u_queued_complete(td, min_events) < 0) {
492 ret = -1;
493 break;
494 }
495 } while (full && (td->cur_depth > td->o.iodepth_low));
496 }
497 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100498 break;
499 }
500
Jens Axboec01c0392007-02-26 12:43:42 +0100501 if (!td->error) {
502 min_events = td->cur_depth;
503
504 if (min_events)
505 ret = io_u_queued_complete(td, min_events);
506 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100507 cleanup_pending_aio(td);
508
509 td_set_runstate(td, TD_RUNNING);
510}
511
Jens Axboe32cd46a2006-06-07 13:40:40 +0200512/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200513 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200514 * and reaps them, checking for rate and errors along the way.
515 */
Jens Axboeebac4652005-12-08 15:25:21 +0100516static void do_io(struct thread_data *td)
517{
Jens Axboeebac4652005-12-08 15:25:21 +0100518 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100519 unsigned int i;
520 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100521
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200522 if (in_ramp_time(td))
523 td_set_runstate(td, TD_RAMP);
524 else
525 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200526
Jens Axboe7bb48f82007-03-27 15:30:28 +0200527 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100528 struct timeval comp_time;
529 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200530 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100531 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200532 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100533
534 if (td->terminate)
535 break;
536
Jens Axboe3d7c3912007-02-19 13:16:12 +0100537 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100538 if (!io_u)
539 break;
540
Jens Axboe993bf482008-11-14 13:04:53 +0100541 update_tv_cache(td);
Jens Axboe97601022007-02-18 12:47:29 +0100542
Jens Axboe993bf482008-11-14 13:04:53 +0100543 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe97601022007-02-18 12:47:29 +0100544 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200545 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100546 break;
547 }
Jens Axboe36167d82007-02-18 05:41:31 +0100548
Jens Axboeb67740d2007-07-26 12:22:30 +0200549 /*
550 * Add verification end_io handler, if asked to verify
551 * a previously written file.
552 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100553 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200554 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100555 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200556 } else if (in_ramp_time(td))
557 td_set_runstate(td, TD_RAMP);
558 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100559 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200560
Jens Axboe11786802007-03-05 18:33:22 +0100561 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100562 switch (ret) {
563 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100564 if (io_u->error)
565 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100566 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100567 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200568 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100569
Jens Axboe9e9d1642007-03-12 09:37:46 +0100570 /*
571 * zero read, fail
572 */
573 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200574 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100575 put_io_u(td, io_u);
576 break;
577 }
578
Jens Axboe36167d82007-02-18 05:41:31 +0100579 io_u->xfer_buflen = io_u->resid;
580 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200581 io_u->offset += bytes;
582
Jens Axboe30061b92007-04-17 13:31:34 +0200583 td->ts.short_io_u[io_u->ddir]++;
584
Jens Axboed460eb32007-04-16 21:39:33 +0200585 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200586 goto sync_done;
587
Jens Axboe11786802007-03-05 18:33:22 +0100588 requeue_io_u(td, &io_u);
589 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200590sync_done:
Jens Axboe993bf482008-11-14 13:04:53 +0100591 if (should_check_rate(td))
592 fio_gettime(&comp_time, NULL);
593
Jens Axboe11786802007-03-05 18:33:22 +0100594 bytes_done = io_u_sync_complete(td, io_u);
595 if (bytes_done < 0)
596 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100597 }
Jens Axboe36167d82007-02-18 05:41:31 +0100598 break;
599 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100600 /*
601 * if the engine doesn't have a commit hook,
602 * the io_u is really queued. if it does have such
603 * a hook, it has to call io_u_queued() itself.
604 */
605 if (td->io_ops->commit == NULL)
606 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100607 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100608 case FIO_Q_BUSY:
609 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100610 ret2 = td_io_commit(td);
611 if (ret2 < 0)
612 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100613 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100614 default:
615 assert(ret < 0);
616 put_io_u(td, io_u);
617 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100618 }
619
Jens Axboe99784632007-02-19 10:06:17 +0100620 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100621 break;
622
Jens Axboe97601022007-02-18 12:47:29 +0100623 /*
624 * See if we need to complete some commands
625 */
Jens Axboe49504212008-06-05 09:03:30 +0200626 full = queue_full(td) || ret == FIO_Q_BUSY;
627 if (full || !td->o.iodepth_batch_complete) {
628 min_evts = td->o.iodepth_batch_complete;
629 if (full && !min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100630 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200631
Jens Axboe993bf482008-11-14 13:04:53 +0100632 if (should_check_rate(td))
633 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200634
635 do {
636 ret = io_u_queued_complete(td, min_evts);
637 if (ret <= 0)
638 break;
639
640 bytes_done += ret;
641 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100642 }
643
Jens Axboe49504212008-06-05 09:03:30 +0200644 if (ret < 0)
645 break;
Jens Axboe97601022007-02-18 12:47:29 +0100646 if (!bytes_done)
647 continue;
648
Jens Axboeebac4652005-12-08 15:25:21 +0100649 /*
650 * the rate is batched for now, it should work for batches
651 * of completions except the very first one which may look
652 * a little bursty
653 */
Jens Axboe993bf482008-11-14 13:04:53 +0100654 if (!in_ramp_time(td) && should_check_rate(td)) {
655 usec = utime_since(&td->tv_cache, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100656
Jens Axboe721938a2008-09-10 09:46:16 +0200657 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100658
Jens Axboe721938a2008-09-10 09:46:16 +0200659 if (check_min_rate(td, &comp_time)) {
660 if (exitall_on_terminate)
661 terminate_threads(td->groupid);
662 td_verror(td, EIO, "check_min_rate");
663 break;
664 }
Jens Axboeebac4652005-12-08 15:25:21 +0100665 }
666
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100667 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100668 unsigned long long b;
669
670 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100671 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100672 int left;
673
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100674 if (td->o.thinktime_spin)
675 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100676
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100677 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100678 if (left)
679 usec_sleep(td, left);
680 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100681 }
Jens Axboeebac4652005-12-08 15:25:21 +0100682 }
683
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100684 if (td->o.fill_device && td->error == ENOSPC) {
685 td->error = 0;
686 td->terminate = 1;
687 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100688 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100689 struct fio_file *f;
690
Jens Axboec01c0392007-02-26 12:43:42 +0100691 i = td->cur_depth;
692 if (i)
693 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100694
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100695 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200696 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100697
698 for_each_file(td, f, i) {
699 if (!(f->flags & FIO_FILE_OPEN))
700 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200701 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100702 }
Jens Axboe84585002006-10-19 20:26:22 +0200703 }
Jens Axboec01c0392007-02-26 12:43:42 +0100704 } else
705 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100706
707 /*
708 * stop job if we failed doing any IO
709 */
710 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
711 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100712}
713
Jens Axboeebac4652005-12-08 15:25:21 +0100714static void cleanup_io_u(struct thread_data *td)
715{
Jens Axboe01743ee2008-06-02 12:19:19 +0200716 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100717 struct io_u *io_u;
718
Jens Axboe01743ee2008-06-02 12:19:19 +0200719 flist_for_each_safe(entry, n, &td->io_u_freelist) {
720 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100721
Jens Axboe01743ee2008-06-02 12:19:19 +0200722 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100723 free(io_u);
724 }
725
Jens Axboe2f9ade32006-10-20 11:25:52 +0200726 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100727}
728
729static int init_io_u(struct thread_data *td)
730{
731 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100732 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100733 int i, max_units;
734 char *p;
735
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100736 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100737 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100738 td->orig_buffer_size = (unsigned long long) max_bs
739 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100740
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100741 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
742 unsigned long bs;
743
744 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
745 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
746 }
Jens Axboeebac4652005-12-08 15:25:21 +0100747
Jens Axboec3990642007-07-19 10:17:09 +0200748 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
749 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
750 return 1;
751 }
752
Jens Axboe2f9ade32006-10-20 11:25:52 +0200753 if (allocate_io_mem(td))
754 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100755
Jens Axboeb3f378e2007-07-20 12:39:22 +0200756 if (td->o.odirect)
757 p = ALIGN(td->orig_buffer);
758 else
759 p = td->orig_buffer;
760
Jens Axboeebac4652005-12-08 15:25:21 +0100761 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200762 if (td->terminate)
763 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100764 io_u = malloc(sizeof(*io_u));
765 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200766 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100767
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200768 if (!(td->io_ops->flags & FIO_NOIO)) {
769 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200770
Jens Axboe5973caf2008-05-21 19:52:35 +0200771 if (td_write(td) && !td->o.refill_buffers)
772 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200773 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100774
Jens Axboeb1ff3402006-02-17 11:35:58 +0100775 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100776 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200777 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100778 }
779
780 return 0;
781}
782
Jens Axboeda867742006-06-06 10:39:10 -0700783static int switch_ioscheduler(struct thread_data *td)
784{
785 char tmp[256], tmp2[128];
786 FILE *f;
787 int ret;
788
Jens Axboeba0fbe12007-03-09 14:34:23 +0100789 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200790 return 0;
791
Jens Axboeda867742006-06-06 10:39:10 -0700792 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
793
794 f = fopen(tmp, "r+");
795 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200796 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100797 log_err("fio: os or kernel doesn't support IO scheduler"
798 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200799 return 0;
800 }
801 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700802 return 1;
803 }
804
805 /*
806 * Set io scheduler.
807 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100808 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700809 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100810 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700811 fclose(f);
812 return 1;
813 }
814
815 rewind(f);
816
817 /*
818 * Read back and check that the selected scheduler is now the default.
819 */
820 ret = fread(tmp, 1, sizeof(tmp), f);
821 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100822 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700823 fclose(f);
824 return 1;
825 }
826
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100827 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700828 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100829 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100830 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700831 fclose(f);
832 return 1;
833 }
834
835 fclose(f);
836 return 0;
837}
838
Jens Axboe492158c2007-05-24 10:32:47 +0200839static int keep_running(struct thread_data *td)
840{
841 unsigned long long io_done;
842
Jens Axboe20e354e2007-07-23 14:36:16 +0200843 if (td->done)
844 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200845 if (td->o.time_based)
846 return 1;
847 if (td->o.loops) {
848 td->o.loops--;
849 return 1;
850 }
851
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100852 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
853 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200854 if (io_done < td->o.size)
855 return 1;
856
857 return 0;
858}
859
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200860static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100861{
Jens Axboe756867b2007-03-06 15:19:24 +0100862 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100863 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100864 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100865 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100866 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100867 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100868
Jens Axboec1324df2007-02-19 19:06:41 +0100869 td->last_was_sync = 0;
870
Jens Axboe2ba1c292008-02-01 13:16:38 +0100871 /*
872 * reset file done count if we are to start over
873 */
874 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200875 td->nr_done_files = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200876}
877
878void reset_all_stats(struct thread_data *td)
879{
880 struct timeval tv;
881 int i;
882
883 reset_io_counters(td);
884
885 for (i = 0; i < 2; i++) {
886 td->io_bytes[i] = 0;
887 td->io_blocks[i] = 0;
888 td->io_issues[i] = 0;
889 td->ts.total_io_u[i] = 0;
890 }
891
892 fio_gettime(&tv, NULL);
893 memcpy(&td->epoch, &tv, sizeof(tv));
894 memcpy(&td->start, &tv, sizeof(tv));
895}
896
897static int clear_io_state(struct thread_data *td)
898{
899 struct fio_file *f;
900 unsigned int i;
901 int ret;
902
903 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +0200904
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100905 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100906
907 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200908 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200909 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100910 ret = td_io_open_file(td, f);
911 if (ret)
912 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200913 }
Jens Axboea978ba62007-03-08 14:29:03 +0100914
915 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100916}
917
Jens Axboe906c8d72006-06-13 09:37:56 +0200918/*
919 * Entry point for the thread based jobs. The process based jobs end up
920 * here as well, after a little setup.
921 */
Jens Axboeebac4652005-12-08 15:25:21 +0100922static void *thread_main(void *data)
923{
Shawn Lewis10927312007-11-21 09:38:13 +0100924 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100925 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100926 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100927
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100928 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100929 setsid();
930
931 td->pid = getpid();
932
Jens Axboe5921e802008-05-30 15:02:38 +0200933 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100934
Jens Axboe01743ee2008-06-02 12:19:19 +0200935 INIT_FLIST_HEAD(&td->io_u_freelist);
936 INIT_FLIST_HEAD(&td->io_u_busylist);
937 INIT_FLIST_HEAD(&td->io_u_requeues);
938 INIT_FLIST_HEAD(&td->io_log_list);
939 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200940 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200941
Jens Axboe75154842006-06-01 13:56:09 +0200942 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100943 fio_mutex_up(startup_mutex);
944 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100945
Jens Axboe37f56872007-03-09 12:42:35 +0100946 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100947 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100948 * eating a file descriptor
949 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100950 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100951
Jens Axboed84f8d42007-05-16 08:47:46 +0200952 /*
953 * May alter parameters that init_io_u() will use, so we need to
954 * do this first.
955 */
956 if (init_iolog(td))
957 goto err;
958
Jens Axboedb1cd902007-04-26 13:47:07 +0200959 if (init_io_u(td))
960 goto err;
961
Jens Axboe375b2692007-05-22 09:13:02 +0200962 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200963 td_verror(td, errno, "cpu_set_affinity");
964 goto err;
965 }
966
Jens Axboeac684782007-11-08 08:29:07 +0100967 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200968 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
969 td_verror(td, errno, "ioprio_set");
970 goto err;
971 }
972 }
973
974 if (nice(td->o.nice) == -1) {
975 td_verror(td, errno, "nice");
976 goto err;
977 }
978
979 if (td->o.ioscheduler && switch_ioscheduler(td))
980 goto err;
981
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100982 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100983 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100984
985 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100986 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100987
Jens Axboeb5af8292007-03-08 12:43:13 +0100988 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100989 goto err;
990
Jens Axboe68727072007-03-15 20:49:25 +0100991 if (init_random_map(td))
992 goto err;
993
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100994 if (td->o.exec_prerun) {
995 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100996 goto err;
997 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200998
Jens Axboe69008992006-11-24 13:12:56 +0100999 fio_gettime(&td->epoch, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001000 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +01001001
1002 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +01001003 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001004 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +01001005 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001006 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboe993bf482008-11-14 13:04:53 +01001007 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +01001008
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001009 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001010 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1011 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +01001012
Jens Axboea978ba62007-03-08 14:29:03 +01001013 if (clear_state && clear_io_state(td))
1014 break;
1015
Jens Axboeebac4652005-12-08 15:25:21 +01001016 prune_io_piece_log(td);
1017
Jens Axboeba0fbe12007-03-09 14:34:23 +01001018 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001019
Jens Axboea978ba62007-03-08 14:29:03 +01001020 clear_state = 1;
1021
Jens Axboe38d77ca2007-03-21 14:20:20 +01001022 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1023 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001024 elapsed = utime_since(&td->start,
1025 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001026 else
1027 elapsed = utime_since_now(&td->start);
1028
1029 runtime[DDIR_READ] += elapsed;
1030 }
1031 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1032 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001033 elapsed = utime_since(&td->start,
1034 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001035 else
1036 elapsed = utime_since_now(&td->start);
1037
1038 runtime[DDIR_WRITE] += elapsed;
1039 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001040
Jens Axboeebac4652005-12-08 15:25:21 +01001041 if (td->error || td->terminate)
1042 break;
1043
Shawn Lewise84c73a2007-08-02 22:19:32 +02001044 if (!td->o.do_verify ||
1045 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001046 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001047 continue;
1048
Jens Axboea978ba62007-03-08 14:29:03 +01001049 if (clear_io_state(td))
1050 break;
1051
Jens Axboe02bcaa82006-11-24 10:42:00 +01001052 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001053
1054 do_verify(td);
1055
Jens Axboe69008992006-11-24 13:12:56 +01001056 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001057
1058 if (td->error || td->terminate)
1059 break;
1060 }
1061
Jens Axboe36dff962006-11-24 13:29:20 +01001062 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001063 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1064 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001065 td->ts.total_run_time = mtime_since_now(&td->epoch);
1066 td->ts.io_bytes[0] = td->io_bytes[0];
1067 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001068
Jens Axboee3cedca2008-11-19 19:57:52 +01001069 if (td->ts.bw_log) {
1070 if (td->o.bw_log_file)
1071 finish_log_named(td, td->ts.bw_log, td->o.bw_log_file, "bw");
1072 else
1073 finish_log(td, td->ts.bw_log, "bw");
1074 }
1075 if (td->ts.slat_log) {
1076 if (td->o.lat_log_file)
1077 finish_log_named(td, td->ts.slat_log, td->o.lat_log_file, "clat");
1078 else
1079 finish_log(td, td->ts.slat_log, "slat");
1080 }
1081 if (td->ts.clat_log) {
1082 if (td->o.lat_log_file)
1083 finish_log_named(td, td->ts.clat_log, td->o.lat_log_file, "clat");
1084 else
1085 finish_log(td, td->ts.clat_log, "clat");
1086 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001087 if (td->o.exec_postrun) {
1088 if (system(td->o.exec_postrun) < 0)
1089 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001090 }
Jens Axboeebac4652005-12-08 15:25:21 +01001091
1092 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001093 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001094
1095err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001096 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001097 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001098 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001099 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001100 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001101 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001102
1103 /*
1104 * do this very late, it will log file closing as well
1105 */
1106 if (td->o.write_iolog_file)
1107 write_iolog_close(td);
1108
Jens Axboed23bb322007-03-23 15:57:56 +01001109 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001110 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001111 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001112}
1113
Jens Axboe906c8d72006-06-13 09:37:56 +02001114/*
1115 * We cannot pass the td data into a forked process, so attach the td and
1116 * pass it to the thread worker.
1117 */
Jens Axboea6418142007-02-17 04:47:18 +01001118static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001119{
1120 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001121 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001122
1123 data = shmat(shmid, NULL, 0);
1124 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001125 int __err = errno;
1126
Jens Axboeebac4652005-12-08 15:25:21 +01001127 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001128 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001129 }
1130
1131 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001132 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001133 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001134 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001135}
1136
Jens Axboe906c8d72006-06-13 09:37:56 +02001137/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001138 * Run over the job map and reap the threads that have exited, if any.
1139 */
Jens Axboeebac4652005-12-08 15:25:21 +01001140static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1141{
Jens Axboe34572e22006-10-20 09:33:18 +02001142 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001143 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001144
1145 /*
1146 * reap exited threads (TD_EXITED -> TD_REAPED)
1147 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001148 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001149 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001150 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001151
Jens Axboe84585002006-10-19 20:26:22 +02001152 /*
1153 * ->io_ops is NULL for a thread that has closed its
1154 * io engine
1155 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001156 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001157 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001158 else
1159 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001160
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001161 if (!td->pid) {
1162 pending++;
1163 continue;
1164 }
1165 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001166 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001167 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001168 if (td->runstate == TD_EXITED) {
1169 td_set_runstate(td, TD_REAPED);
1170 goto reaped;
1171 }
1172 continue;
1173 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001174
1175 flags = WNOHANG;
1176 if (td->runstate == TD_EXITED)
1177 flags = 0;
1178
1179 /*
1180 * check if someone quit or got killed in an unusual way
1181 */
1182 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001183 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001184 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001185 log_err("fio: pid=%d disappeared %d\n",
1186 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001187 td_set_runstate(td, TD_REAPED);
1188 goto reaped;
1189 }
1190 perror("waitpid");
1191 } else if (ret == td->pid) {
1192 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001193 int sig = WTERMSIG(status);
1194
Jens Axboed9244942007-03-05 12:32:32 +01001195 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001196 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001197 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001198 td_set_runstate(td, TD_REAPED);
1199 goto reaped;
1200 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001201 if (WIFEXITED(status)) {
1202 if (WEXITSTATUS(status) && !td->error)
1203 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001204
Jens Axboea2f77c92007-02-22 11:53:00 +01001205 td_set_runstate(td, TD_REAPED);
1206 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001207 }
1208 }
Jens Axboeebac4652005-12-08 15:25:21 +01001209
Jens Axboea2f77c92007-02-22 11:53:00 +01001210 /*
1211 * thread is not dead, continue
1212 */
gurudas paie48676b2007-04-11 12:44:27 +02001213 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001214 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001215reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001216 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001217 (*m_rate) -= td->o.ratemin;
1218 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001219 if (!td->pid)
1220 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001221
1222 if (td->error)
1223 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001224
1225 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001226 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001227
Jens Axboe1f809d12007-10-25 18:34:02 +02001228 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001229 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001230}
1231
Jens Axboe906c8d72006-06-13 09:37:56 +02001232/*
1233 * Main function for kicking off and reaping jobs, as needed.
1234 */
Jens Axboeebac4652005-12-08 15:25:21 +01001235static void run_threads(void)
1236{
Jens Axboeebac4652005-12-08 15:25:21 +01001237 struct thread_data *td;
1238 unsigned long spent;
1239 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001240
Jens Axboe2f9ade32006-10-20 11:25:52 +02001241 if (fio_pin_memory())
1242 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001243
Jens Axboec6ae0a52006-06-12 11:04:44 +02001244 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001245 printf("Starting ");
1246 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001247 printf("%d thread%s", nr_thread,
1248 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001249 if (nr_process) {
1250 if (nr_thread)
1251 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001252 printf("%d process%s", nr_process,
1253 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001254 }
1255 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001256 fflush(stdout);
1257 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001258
Jens Axboe697a6062008-05-31 00:04:45 +02001259 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001260
Jens Axboeebac4652005-12-08 15:25:21 +01001261 todo = thread_number;
1262 nr_running = 0;
1263 nr_started = 0;
1264 m_rate = t_rate = 0;
1265
Jens Axboe34572e22006-10-20 09:33:18 +02001266 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001267 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001268
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001269 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001270 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001271 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001272 }
Jens Axboeebac4652005-12-08 15:25:21 +01001273
1274 /*
1275 * do file setup here so it happens sequentially,
1276 * we don't want X number of threads getting their
1277 * client data interspersed on disk
1278 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001279 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001280 exit_value++;
1281 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001282 log_err("fio: pid=%d, err=%d/%s\n",
1283 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001284 td_set_runstate(td, TD_REAPED);
1285 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001286 } else {
1287 struct fio_file *f;
1288 unsigned int i;
1289
1290 /*
1291 * for sharing to work, each job must always open
1292 * its own files. so close them, if we opened them
1293 * for creation
1294 */
1295 for_each_file(td, f, i)
1296 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001297 }
Jens Axboe380cf262007-01-11 14:01:27 +01001298
1299 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001300 }
1301
Jens Axboea2f77c92007-02-22 11:53:00 +01001302 set_genesis_time();
1303
Jens Axboeebac4652005-12-08 15:25:21 +01001304 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001305 struct thread_data *map[MAX_JOBS];
1306 struct timeval this_start;
1307 int this_jobs = 0, left;
1308
Jens Axboeebac4652005-12-08 15:25:21 +01001309 /*
1310 * create threads (TD_NOT_CREATED -> TD_CREATED)
1311 */
Jens Axboe34572e22006-10-20 09:33:18 +02001312 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001313 if (td->runstate != TD_NOT_CREATED)
1314 continue;
1315
1316 /*
1317 * never got a chance to start, killed by other
1318 * thread for some reason
1319 */
1320 if (td->terminate) {
1321 todo--;
1322 continue;
1323 }
1324
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001325 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001326 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001327
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001328 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001329 continue;
1330 }
1331
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001332 if (td->o.stonewall && (nr_started || nr_running)) {
1333 dprint(FD_PROCESS, "%s: stonewall wait\n",
1334 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001335 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001336 }
Jens Axboeebac4652005-12-08 15:25:21 +01001337
Jens Axboe75154842006-06-01 13:56:09 +02001338 /*
1339 * Set state to created. Thread will transition
1340 * to TD_INITIALIZED when it's done setting up.
1341 */
Jens Axboeebac4652005-12-08 15:25:21 +01001342 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001343 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001344 nr_started++;
1345
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001346 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001347 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001348 if (pthread_create(&td->thread, NULL,
1349 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001350 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001351 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001352 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001353 }
Jens Axboef4705a72008-03-10 10:52:22 +01001354 if (pthread_detach(td->thread) < 0)
1355 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001356 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001357 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001358 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001359 pid = fork();
1360 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001361 int ret = fork_main(shm_id, i);
1362
Jens Axboe5e1d3062008-05-23 11:55:53 +02001363 _exit(ret);
1364 } else if (i == fio_debug_jobno)
1365 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001366 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001367 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001368 }
1369
1370 /*
Jens Axboe75154842006-06-01 13:56:09 +02001371 * Wait for the started threads to transition to
1372 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001373 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001374 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001375 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001376 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001377 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1378 break;
1379
1380 usleep(100000);
1381
1382 for (i = 0; i < this_jobs; i++) {
1383 td = map[i];
1384 if (!td)
1385 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001386 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001387 map[i] = NULL;
1388 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001389 } else if (td->runstate >= TD_EXITED) {
1390 map[i] = NULL;
1391 left--;
1392 todo--;
1393 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001394 }
1395 }
1396 }
1397
1398 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001399 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001400 for (i = 0; i < this_jobs; i++) {
1401 td = map[i];
1402 if (!td)
1403 continue;
1404 kill(td->pid, SIGTERM);
1405 }
1406 break;
1407 }
1408
1409 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001410 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001411 */
Jens Axboe34572e22006-10-20 09:33:18 +02001412 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001413 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001414 continue;
1415
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001416 if (in_ramp_time(td))
1417 td_set_runstate(td, TD_RAMP);
1418 else
1419 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001420 nr_running++;
1421 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001422 m_rate += td->o.ratemin;
1423 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001424 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001425 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001426 }
1427
1428 reap_threads(&nr_running, &t_rate, &m_rate);
1429
1430 if (todo)
1431 usleep(100000);
1432 }
1433
1434 while (nr_running) {
1435 reap_threads(&nr_running, &t_rate, &m_rate);
1436 usleep(10000);
1437 }
1438
1439 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001440 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001441}
1442
Jens Axboeebac4652005-12-08 15:25:21 +01001443int main(int argc, char *argv[])
1444{
Jens Axboe29d610e2007-02-06 13:25:33 +01001445 long ps;
1446
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001447 sinit();
1448
Jens Axboedbe11252007-02-11 00:19:51 +01001449 /*
1450 * We need locale for number printing, if it isn't set then just
1451 * go with the US format.
1452 */
1453 if (!getenv("LC_NUMERIC"))
1454 setlocale(LC_NUMERIC, "en_US");
1455
Jens Axboeebac4652005-12-08 15:25:21 +01001456 if (parse_options(argc, argv))
1457 return 1;
1458
Jens Axboe4b472fa2007-04-04 11:04:34 +02001459 if (!thread_number)
1460 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001461
Jens Axboe29d610e2007-02-06 13:25:33 +01001462 ps = sysconf(_SC_PAGESIZE);
1463 if (ps < 0) {
1464 log_err("Failed to get page size\n");
1465 return 1;
1466 }
1467
Jens Axboecfc99db2007-03-14 10:34:47 +01001468 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001469 page_mask = ps - 1;
1470
Jens Axboebb3884d2007-01-17 17:23:11 +11001471 if (write_bw_log) {
1472 setup_log(&agg_io_log[DDIR_READ]);
1473 setup_log(&agg_io_log[DDIR_WRITE]);
1474 }
1475
Jens Axboecdd18ad2008-02-27 18:58:00 +01001476 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001477
Jens Axboea2f77c92007-02-22 11:53:00 +01001478 set_genesis_time();
1479
Jens Axboeccb0fa22008-05-30 23:18:00 +02001480 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001481
1482 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001483
Jens Axboebb3884d2007-01-17 17:23:11 +11001484 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001485 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001486 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001487 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1488 __finish_log(agg_io_log[DDIR_WRITE],
1489 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001490 }
1491 }
Jens Axboeebac4652005-12-08 15:25:21 +01001492
Jens Axboecdd18ad2008-02-27 18:58:00 +01001493 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001494 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001495}