blob: ece897e5fa3bdab4cec8f411f429ded502418312 [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>
Bruce Cran9b836562011-01-08 19:49:54 +010027#include <limits.h>
Jens Axboeebac4652005-12-08 15:25:21 +010028#include <signal.h>
29#include <time.h>
Jens Axboedbe11252007-02-11 00:19:51 +010030#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010031#include <assert.h>
Jens Axboea5b01f12010-11-11 09:19:49 +010032#include <time.h>
Jens Axboeebac4652005-12-08 15:25:21 +010033#include <sys/stat.h>
34#include <sys/wait.h>
35#include <sys/ipc.h>
36#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010037#include <sys/mman.h>
38
39#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020040#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010041#include "smalloc.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020042#include "verify.h"
Jens Axboea917a8b2010-09-02 13:23:20 +020043#include "trim.h"
Jens Axboe7c9b1bc2009-06-03 09:25:57 +020044#include "diskutil.h"
Jens Axboea696fa22009-12-04 10:05:02 +010045#include "cgroup.h"
Jens Axboe07b32322010-03-05 09:48:44 +010046#include "profile.h"
Jens Axboed54a1442010-03-25 23:07:35 +010047#include "lib/rand.h"
Jens Axboe91aea6e2011-04-25 20:46:58 +020048#include "memalign.h"
Jens Axboe009b1be2011-09-29 18:27:02 -060049#include "server.h"
Jens Axboeebac4652005-12-08 15:25:21 +010050
Jens Axboecfc99db2007-03-14 10:34:47 +010051unsigned long page_mask;
52unsigned long page_size;
Jens Axboed529ee12009-07-01 10:33:03 +020053
54#define PAGE_ALIGN(buf) \
Jens Axboe29d610e2007-02-06 13:25:33 +010055 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010056
57int groupid = 0;
58int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010059int nr_process = 0;
60int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010061int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020062int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020063unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010064
Jens Axboecdd18ad2008-02-27 18:58:00 +010065static struct fio_mutex *startup_mutex;
Fabio Checconid2bbb8d2009-05-18 12:54:50 +020066static struct fio_mutex *writeout_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010067static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010068static int exit_value;
Jens Axboebe4ecfd2008-12-08 14:10:52 +010069static pthread_t gtod_thread;
Jens Axboe79857012011-01-14 14:14:58 +010070static pthread_t disk_util_thread;
Jens Axboedae53412009-12-04 19:54:18 +010071static struct flist_head *cgroup_list;
Jens Axboe6adb38a2009-12-07 08:01:26 +010072static char *cgroup_mnt;
Jens Axboeebac4652005-12-08 15:25:21 +010073
Jens Axboed3cc4eb2011-08-25 14:24:03 +020074unsigned long arch_flags = 0;
75
Jens Axboebb3884d2007-01-17 17:23:11 +110076struct io_log *agg_io_log[2];
77
Jens Axboe75154842006-06-01 13:56:09 +020078#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010079
Jens Axboecca84642011-10-07 12:47:57 +020080static const char *fio_os_strings[os_nr] = {
81 "Invalid",
82 "Linux",
83 "AIX",
84 "FreeBSD",
85 "HP-UX",
86 "OSX",
87 "NetBSD",
88 "Solaris",
89 "Windows"
90};
91
92static const char *fio_arch_strings[arch_nr] = {
93 "Invalid",
94 "x86-64",
95 "x86",
96 "ppc",
97 "ia64",
98 "s390",
99 "alpha",
100 "sparc",
101 "sparc64",
102 "arm",
103 "sh",
104 "hppa",
105 "generic"
106};
107
108const char *fio_get_os_string(int nr)
109{
110 if (nr < os_nr)
111 return fio_os_strings[nr];
112
113 return NULL;
114}
115
116const char *fio_get_arch_string(int nr)
117{
118 if (nr < arch_nr)
119 return fio_arch_strings[nr];
120
121 return NULL;
122}
123
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200124void td_set_runstate(struct thread_data *td, int runstate)
Jens Axboe6ce15a32007-01-12 09:04:41 +0100125{
Jens Axboed8fab1b2008-03-06 10:25:17 +0100126 if (td->runstate == runstate)
127 return;
128
Jens Axboe5921e802008-05-30 15:02:38 +0200129 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
130 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +0100131 td->runstate = runstate;
132}
133
Jens Axboecc0df002011-10-03 20:53:32 +0200134void fio_terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +0100135{
Jens Axboe34572e22006-10-20 09:33:18 +0200136 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +0100137 int i;
138
Jens Axboe6f88bcb2008-03-19 10:29:07 +0100139 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
140
Jens Axboe34572e22006-10-20 09:33:18 +0200141 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +0100142 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +0100143 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +0200144 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +0100145 td->terminate = 1;
146 td->o.start_delay = 0;
147
Jens Axboe7a93ab12007-04-18 12:25:41 +0200148 /*
149 * if the thread is running, just let it exit
150 */
Michael Callahan6aa27812011-07-14 07:56:37 +0200151 if (!td->pid)
152 continue;
153 else if (td->runstate < TD_RAMP)
Bruce Cran03e20d62011-01-02 20:14:54 +0100154 kill(td->pid, SIGTERM);
Jens Axboef63f7f32008-03-01 15:25:24 +0100155 else {
156 struct ioengine_ops *ops = td->io_ops;
157
Bruce Cran03e20d62011-01-02 20:14:54 +0100158 if (ops && (ops->flags & FIO_SIGTERM))
159 kill(td->pid, SIGTERM);
Jens Axboef63f7f32008-03-01 15:25:24 +0100160 }
Jens Axboeebac4652005-12-08 15:25:21 +0100161 }
162 }
163}
164
Jens Axboe697a6062008-05-31 00:04:45 +0200165static void sig_int(int sig)
166{
167 if (threads) {
Jens Axboe4ceb30d2010-02-24 09:19:14 +0100168 log_info("\nfio: terminating on signal %d\n", sig);
Jens Axboe009b1be2011-09-29 18:27:02 -0600169 exit_backend = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100170 fflush(stdout);
Jens Axboedeaa1af2011-05-05 08:17:03 -0600171 exit_value = 128;
Jens Axboecc0df002011-10-03 20:53:32 +0200172 fio_terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100173 }
174}
175
Jens Axboe79857012011-01-14 14:14:58 +0100176static void *disk_thread_main(void *data)
Jens Axboea5b01f12010-11-11 09:19:49 +0100177{
Jens Axboe79857012011-01-14 14:14:58 +0100178 fio_mutex_up(startup_mutex);
179
180 while (threads) {
181 usleep(DISK_UTIL_MSEC * 1000);
Jens Axboe9a793c22011-01-14 15:22:14 +0100182 if (!threads)
183 break;
Jens Axboe79857012011-01-14 14:14:58 +0100184 update_io_ticks();
Jens Axboecf451d12011-10-03 16:48:30 +0200185
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200186 if (!is_backend)
Jens Axboecf451d12011-10-03 16:48:30 +0200187 print_thread_status();
Jens Axboe79857012011-01-14 14:14:58 +0100188 }
189
190 return NULL;
Jens Axboea5b01f12010-11-11 09:19:49 +0100191}
192
Jens Axboe79857012011-01-14 14:14:58 +0100193static int create_disk_util_thread(void)
Jens Axboea5b01f12010-11-11 09:19:49 +0100194{
Jens Axboe79857012011-01-14 14:14:58 +0100195 int ret;
Jens Axboea5b01f12010-11-11 09:19:49 +0100196
Jens Axboe69204d62011-01-14 18:29:19 +0100197 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
Jens Axboe79857012011-01-14 14:14:58 +0100198 if (ret) {
199 log_err("Can't create disk util thread: %s\n", strerror(ret));
200 return 1;
201 }
Jens Axboea5b01f12010-11-11 09:19:49 +0100202
Jens Axboe79857012011-01-14 14:14:58 +0100203 ret = pthread_detach(disk_util_thread);
204 if (ret) {
205 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
206 return 1;
207 }
Bruce Cran03e20d62011-01-02 20:14:54 +0100208
Jens Axboe79857012011-01-14 14:14:58 +0100209 dprint(FD_MUTEX, "wait on startup_mutex\n");
210 fio_mutex_down(startup_mutex);
211 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
212 return 0;
Jens Axboea5b01f12010-11-11 09:19:49 +0100213}
214
Jens Axboe697a6062008-05-31 00:04:45 +0200215static void set_sig_handlers(void)
216{
217 struct sigaction act;
218
219 memset(&act, 0, sizeof(act));
Jens Axboe697a6062008-05-31 00:04:45 +0200220 act.sa_handler = sig_int;
221 act.sa_flags = SA_RESTART;
222 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200223
224 memset(&act, 0, sizeof(act));
Jens Axboeb20b6be2011-05-11 13:23:01 +0200225 act.sa_handler = sig_int;
Jens Axboe5f8f9722008-11-18 19:10:21 +0100226 act.sa_flags = SA_RESTART;
Bruce Cran03e20d62011-01-02 20:14:54 +0100227 sigaction(SIGTERM, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200228
229 if (is_backend) {
230 memset(&act, 0, sizeof(act));
231 act.sa_handler = sig_int;
232 act.sa_flags = SA_RESTART;
233 sigaction(SIGPIPE, &act, NULL);
234 }
Jens Axboe697a6062008-05-31 00:04:45 +0200235}
236
237/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200238 * Check if we are above the minimum rate given.
239 */
Jens Axboe581e7142009-06-09 12:47:16 +0200240static int __check_min_rate(struct thread_data *td, struct timeval *now,
Steven Langbd93e342011-06-03 21:15:50 +0200241 enum fio_ddir ddir)
Jens Axboeebac4652005-12-08 15:25:21 +0100242{
Jens Axboe09042002007-02-23 10:56:22 +0100243 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100244 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100245 unsigned long spent;
246 unsigned long rate;
Jens Axboe581e7142009-06-09 12:47:16 +0200247 unsigned int ratemin = 0;
248 unsigned int rate_iops = 0;
249 unsigned int rate_iops_min = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100250
Jens Axboeff58fce2010-08-25 12:02:08 +0200251 assert(ddir_rw(ddir));
252
Jens Axboed982d872009-06-10 06:35:48 +0200253 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
254 return 0;
255
Jens Axboeebac4652005-12-08 15:25:21 +0100256 /*
257 * allow a 2 second settle period in the beginning
258 */
259 if (mtime_since(&td->start, now) < 2000)
260 return 0;
261
Jens Axboec8eeb9d2011-10-05 14:02:22 +0200262 iops += td->this_io_blocks[ddir];
Jens Axboe581e7142009-06-09 12:47:16 +0200263 bytes += td->this_io_bytes[ddir];
264 ratemin += td->o.ratemin[ddir];
265 rate_iops += td->o.rate_iops[ddir];
266 rate_iops_min += td->o.rate_iops_min[ddir];
Jens Axboe09042002007-02-23 10:56:22 +0100267
Jens Axboeebac4652005-12-08 15:25:21 +0100268 /*
269 * if rate blocks is set, sample is running
270 */
Jens Axboe581e7142009-06-09 12:47:16 +0200271 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
272 spent = mtime_since(&td->lastrate[ddir], now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100273 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100274 return 0;
275
Jens Axboe581e7142009-06-09 12:47:16 +0200276 if (td->o.rate[ddir]) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100277 /*
278 * check bandwidth specified rate
279 */
Jens Axboe581e7142009-06-09 12:47:16 +0200280 if (bytes < td->rate_bytes[ddir]) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100281 log_err("%s: min rate %u not met\n", td->o.name,
Jens Axboe581e7142009-06-09 12:47:16 +0200282 ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100283 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100284 } else {
Jens Axboe581e7142009-06-09 12:47:16 +0200285 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
286 if (rate < ratemin ||
287 bytes < td->rate_bytes[ddir]) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100288 log_err("%s: min rate %u not met, got"
Jens Axboeb22989b2009-07-17 22:29:23 +0200289 " %luKB/sec\n", td->o.name,
Jens Axboe581e7142009-06-09 12:47:16 +0200290 ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100291 return 1;
292 }
293 }
294 } else {
295 /*
296 * checks iops specified rate
297 */
Jens Axboe581e7142009-06-09 12:47:16 +0200298 if (iops < rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100299 log_err("%s: min iops rate %u not met\n",
Jens Axboe581e7142009-06-09 12:47:16 +0200300 td->o.name, rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100301 return 1;
302 } else {
Jens Axboe581e7142009-06-09 12:47:16 +0200303 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
304 if (rate < rate_iops_min ||
305 iops < td->rate_blocks[ddir]) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100306 log_err("%s: min iops rate %u not met,"
307 " got %lu\n", td->o.name,
Jens Axboe581e7142009-06-09 12:47:16 +0200308 rate_iops_min, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100309 }
Jens Axboe413dd452007-02-23 09:26:09 +0100310 }
Jens Axboeebac4652005-12-08 15:25:21 +0100311 }
312 }
313
Jens Axboe581e7142009-06-09 12:47:16 +0200314 td->rate_bytes[ddir] = bytes;
315 td->rate_blocks[ddir] = iops;
316 memcpy(&td->lastrate[ddir], now, sizeof(*now));
Jens Axboeebac4652005-12-08 15:25:21 +0100317 return 0;
318}
319
Jens Axboe581e7142009-06-09 12:47:16 +0200320static int check_min_rate(struct thread_data *td, struct timeval *now,
321 unsigned long *bytes_done)
322{
323 int ret = 0;
324
325 if (bytes_done[0])
326 ret |= __check_min_rate(td, now, 0);
327 if (bytes_done[1])
328 ret |= __check_min_rate(td, now, 1);
329
330 return ret;
331}
332
Jens Axboeebac4652005-12-08 15:25:21 +0100333static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
334{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100335 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100336 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100337 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100338 return 1;
339
340 return 0;
341}
342
Jens Axboe906c8d72006-06-13 09:37:56 +0200343/*
344 * When job exits, we can cancel the in-flight IO if we are using async
345 * io. Attempt to do so.
346 */
Jens Axboeebac4652005-12-08 15:25:21 +0100347static void cleanup_pending_aio(struct thread_data *td)
348{
Jens Axboe01743ee2008-06-02 12:19:19 +0200349 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100350 struct io_u *io_u;
351 int r;
352
353 /*
354 * get immediately available events, if any
355 */
Jens Axboe581e7142009-06-09 12:47:16 +0200356 r = io_u_queued_complete(td, 0, NULL);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100357 if (r < 0)
358 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100359
360 /*
361 * now cancel remaining active events
362 */
Jens Axboe2866c822006-10-09 15:57:48 +0200363 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200364 flist_for_each_safe(entry, n, &td->io_u_busylist) {
365 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100366
Jens Axboe0c6e7512007-02-22 11:19:39 +0100367 /*
368 * if the io_u isn't in flight, then that generally
369 * means someone leaked an io_u. complain but fix
370 * it up, so we don't stall here.
371 */
372 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
373 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100374 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100375 } else {
376 r = td->io_ops->cancel(td, io_u);
377 if (!r)
378 put_io_u(td, io_u);
379 }
Jens Axboeebac4652005-12-08 15:25:21 +0100380 }
381 }
382
Jens Axboe97601022007-02-18 12:47:29 +0100383 if (td->cur_depth)
Jens Axboe581e7142009-06-09 12:47:16 +0200384 r = io_u_queued_complete(td, td->cur_depth, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100385}
386
Jens Axboe906c8d72006-06-13 09:37:56 +0200387/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200388 * Helper to handle the final sync of a file. Works just like the normal
389 * io path, just does everything sync.
390 */
391static int fio_io_sync(struct thread_data *td, struct fio_file *f)
392{
393 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200394 int ret;
395
396 if (!io_u)
397 return 1;
398
399 io_u->ddir = DDIR_SYNC;
400 io_u->file = f;
401
402 if (td_io_prep(td, io_u)) {
403 put_io_u(td, io_u);
404 return 1;
405 }
406
Jens Axboe755200a2007-02-19 13:08:12 +0100407requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200408 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100409 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100410 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200411 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200412 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100413 } else if (ret == FIO_Q_QUEUED) {
Jens Axboe581e7142009-06-09 12:47:16 +0200414 if (io_u_queued_complete(td, 1, NULL) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100415 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100416 } else if (ret == FIO_Q_COMPLETED) {
417 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100418 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100419 return 1;
420 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200421
Jens Axboe581e7142009-06-09 12:47:16 +0200422 if (io_u_sync_complete(td, io_u, NULL) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100423 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100424 } else if (ret == FIO_Q_BUSY) {
425 if (td_io_commit(td))
426 return 1;
427 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200428 }
429
430 return 0;
431}
432
Jens Axboe687c6a22011-07-07 12:36:59 +0200433static inline void __update_tv_cache(struct thread_data *td)
434{
435 fio_gettime(&td->tv_cache, NULL);
436}
437
Jens Axboe993bf482008-11-14 13:04:53 +0100438static inline void update_tv_cache(struct thread_data *td)
439{
440 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
Jens Axboe687c6a22011-07-07 12:36:59 +0200441 __update_tv_cache(td);
Jens Axboe993bf482008-11-14 13:04:53 +0100442}
443
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200444static int break_on_this_error(struct thread_data *td, int *retptr)
445{
446 int ret = *retptr;
447
448 if (ret < 0 || td->error) {
449 int err;
450
Jens Axboe1ec99ee2009-06-15 12:37:30 +0200451 if (!td->o.continue_on_error)
452 return 1;
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200453
454 if (ret < 0)
455 err = -ret;
456 else
457 err = td->error;
458
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200459 if (td_non_fatal_error(err)) {
460 /*
461 * Continue with the I/Os in case of
462 * a non fatal error.
463 */
Radha Ramachandran8a4f8a22009-07-02 08:30:49 +0200464 update_error_count(td, err);
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200465 td_clear_error(td);
466 *retptr = 0;
467 return 0;
Radha Ramachandran8a4f8a22009-07-02 08:30:49 +0200468 } else if (td->o.fill_device && err == ENOSPC) {
469 /*
470 * We expect to hit this error if
471 * fill_device option is set.
472 */
473 td_clear_error(td);
474 td->terminate = 1;
475 return 1;
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200476 } else {
477 /*
478 * Stop the I/O in case of a fatal
479 * error.
480 */
Radha Ramachandran8a4f8a22009-07-02 08:30:49 +0200481 update_error_count(td, err);
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200482 return 1;
483 }
484 }
485
486 return 0;
487}
488
Jens Axboe858a3d42006-10-24 14:54:44 +0200489/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100490 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200491 * reads the blocks back in, and checks the crc/md5 of the data.
492 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100493static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100494{
Jens Axboe53cdc682006-10-18 11:50:58 +0200495 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100496 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100497 int ret, min_events;
498 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200499
Jens Axboe008618a2010-03-18 19:29:45 +0100500 dprint(FD_VERIFY, "starting loop\n");
501
Jens Axboee5b401d2006-10-18 16:03:40 +0200502 /*
503 * sync io first and invalidate cache, to make sure we really
504 * read from disk.
505 */
506 for_each_file(td, f, i) {
Jens Axboed6aed792009-06-03 08:41:15 +0200507 if (!fio_file_open(f))
Jens Axboe3d7b4852007-03-13 14:50:28 +0100508 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100509 if (fio_io_sync(td, f))
510 break;
511 if (file_invalidate_cache(td, f))
512 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200513 }
Jens Axboeebac4652005-12-08 15:25:21 +0100514
Jens Axboeb2fdda42007-02-20 20:52:51 +0100515 if (td->error)
516 return;
517
Jens Axboeebac4652005-12-08 15:25:21 +0100518 td_set_runstate(td, TD_VERIFYING);
519
Jens Axboe36167d82007-02-18 05:41:31 +0100520 io_u = NULL;
521 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200522 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100523
Jens Axboe993bf482008-11-14 13:04:53 +0100524 update_tv_cache(td);
525
526 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe687c6a22011-07-07 12:36:59 +0200527 __update_tv_cache(td);
528 if (runtime_exceeded(td, &td->tv_cache)) {
529 td->terminate = 1;
530 break;
531 }
Jens Axboe069c2912007-02-21 23:02:39 +0100532 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200533
Radha Ramachandrandcb48302009-06-16 08:31:29 +0200534 io_u = __get_io_u(td);
535 if (!io_u)
536 break;
537
Jens Axboe069c2912007-02-21 23:02:39 +0100538 if (get_next_verify(td, io_u)) {
539 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100540 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100541 }
Jens Axboeebac4652005-12-08 15:25:21 +0100542
Jens Axboe069c2912007-02-21 23:02:39 +0100543 if (td_io_prep(td, io_u)) {
544 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100545 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100546 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100547
Jens Axboee8462bd2009-07-06 12:59:04 +0200548 if (td->o.verify_async)
549 io_u->end_io = verify_io_u_async;
550 else
551 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100552
Jens Axboe11786802007-03-05 18:33:22 +0100553 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100554 switch (ret) {
555 case FIO_Q_COMPLETED:
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200556 if (io_u->error) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100557 ret = -io_u->error;
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200558 clear_io_u(td, io_u);
559 } else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100560 int bytes = io_u->xfer_buflen - io_u->resid;
561
Jens Axboe9e9d1642007-03-12 09:37:46 +0100562 /*
563 * zero read, fail
564 */
565 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200566 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100567 put_io_u(td, io_u);
568 break;
569 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200570
Jens Axboe36167d82007-02-18 05:41:31 +0100571 io_u->xfer_buflen = io_u->resid;
572 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200573 io_u->offset += bytes;
574
Jens Axboeff58fce2010-08-25 12:02:08 +0200575 if (ddir_rw(io_u->ddir))
576 td->ts.short_io_u[io_u->ddir]++;
Jens Axboe30061b92007-04-17 13:31:34 +0200577
Jens Axboe2b13e712011-01-19 14:04:16 -0700578 f = io_u->file;
Jens Axboed460eb32007-04-16 21:39:33 +0200579 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200580 goto sync_done;
581
Jens Axboe11786802007-03-05 18:33:22 +0100582 requeue_io_u(td, &io_u);
583 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200584sync_done:
Jens Axboe581e7142009-06-09 12:47:16 +0200585 ret = io_u_sync_complete(td, io_u, NULL);
Jens Axboe11786802007-03-05 18:33:22 +0100586 if (ret < 0)
587 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100588 }
Jens Axboe36167d82007-02-18 05:41:31 +0100589 continue;
590 case FIO_Q_QUEUED:
591 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100592 case FIO_Q_BUSY:
593 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100594 ret2 = td_io_commit(td);
595 if (ret2 < 0)
596 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100597 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100598 default:
599 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100600 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100601 break;
602 }
603
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200604 if (break_on_this_error(td, &ret))
Jens Axboeebac4652005-12-08 15:25:21 +0100605 break;
606
Jens Axboe3af6ef32007-02-18 06:57:43 +0100607 /*
608 * if we can queue more, do so. but check if there are
Jens Axboeccc02052011-07-14 12:45:13 +0200609 * completed io_u's first. Note that we can get BUSY even
610 * without IO queued, if the system is resource starved.
Jens Axboe3af6ef32007-02-18 06:57:43 +0100611 */
Jens Axboeccc02052011-07-14 12:45:13 +0200612 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
Jens Axboe49504212008-06-05 09:03:30 +0200613 if (full || !td->o.iodepth_batch_complete) {
Radha Ramachandran6a962762009-11-05 08:33:49 +0100614 min_events = min(td->o.iodepth_batch_complete,
615 td->cur_depth);
Dan Ehrenberg1f84f1d2011-07-23 13:36:44 +0200616 if (full && !min_events && td->o.iodepth_batch_complete != 0)
Jens Axboe838bc702008-05-22 13:08:23 +0200617 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100618
Jens Axboe49504212008-06-05 09:03:30 +0200619 do {
620 /*
621 * Reap required number of io units, if any,
622 * and do the verification on them through
623 * the callback handler
624 */
Jens Axboe581e7142009-06-09 12:47:16 +0200625 if (io_u_queued_complete(td, min_events, NULL) < 0) {
Jens Axboe49504212008-06-05 09:03:30 +0200626 ret = -1;
627 break;
628 }
629 } while (full && (td->cur_depth > td->o.iodepth_low));
630 }
631 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100632 break;
633 }
634
Jens Axboec01c0392007-02-26 12:43:42 +0100635 if (!td->error) {
636 min_events = td->cur_depth;
637
638 if (min_events)
Jens Axboe581e7142009-06-09 12:47:16 +0200639 ret = io_u_queued_complete(td, min_events, NULL);
Jens Axboec01c0392007-02-26 12:43:42 +0100640 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100641 cleanup_pending_aio(td);
642
643 td_set_runstate(td, TD_RUNNING);
Jens Axboe008618a2010-03-18 19:29:45 +0100644
645 dprint(FD_VERIFY, "exiting loop\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100646}
647
Jens Axboe32cd46a2006-06-07 13:40:40 +0200648/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200649 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200650 * and reaps them, checking for rate and errors along the way.
651 */
Jens Axboeebac4652005-12-08 15:25:21 +0100652static void do_io(struct thread_data *td)
653{
Jens Axboeaf52b342007-03-13 10:07:47 +0100654 unsigned int i;
655 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100656
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200657 if (in_ramp_time(td))
658 td_set_runstate(td, TD_RAMP);
659 else
660 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200661
Glen Ogilvie457bf392009-11-03 21:52:36 +0100662 while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
Jens Axboea917a8b2010-09-02 13:23:20 +0200663 (!flist_empty(&td->trim_list)) ||
Glen Ogilvie457bf392009-11-03 21:52:36 +0100664 ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) ) {
Jens Axboe97601022007-02-18 12:47:29 +0100665 struct timeval comp_time;
Jens Axboe581e7142009-06-09 12:47:16 +0200666 unsigned long bytes_done[2] = { 0, 0 };
Jens Axboe84585002006-10-19 20:26:22 +0200667 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100668 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200669 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100670
671 if (td->terminate)
672 break;
673
Jens Axboe993bf482008-11-14 13:04:53 +0100674 update_tv_cache(td);
Jens Axboe97601022007-02-18 12:47:29 +0100675
Jens Axboe993bf482008-11-14 13:04:53 +0100676 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe687c6a22011-07-07 12:36:59 +0200677 __update_tv_cache(td);
678 if (runtime_exceeded(td, &td->tv_cache)) {
679 td->terminate = 1;
680 break;
681 }
Jens Axboe97601022007-02-18 12:47:29 +0100682 }
Jens Axboe36167d82007-02-18 05:41:31 +0100683
Radha Ramachandrandcb48302009-06-16 08:31:29 +0200684 io_u = get_io_u(td);
685 if (!io_u)
686 break;
687
Jens Axboeb67740d2007-07-26 12:22:30 +0200688 /*
689 * Add verification end_io handler, if asked to verify
690 * a previously written file.
691 */
Jens Axboe715d2b32009-09-04 20:53:53 +0200692 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
693 !td_rw(td)) {
Jens Axboee8462bd2009-07-06 12:59:04 +0200694 if (td->o.verify_async)
695 io_u->end_io = verify_io_u_async;
696 else
697 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100698 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200699 } else if (in_ramp_time(td))
700 td_set_runstate(td, TD_RAMP);
701 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100702 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200703
Jens Axboe11786802007-03-05 18:33:22 +0100704 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100705 switch (ret) {
706 case FIO_Q_COMPLETED:
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200707 if (io_u->error) {
Jens Axboe54517922007-03-05 10:06:06 +0100708 ret = -io_u->error;
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200709 clear_io_u(td, io_u);
710 } else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100711 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200712 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100713
Jens Axboe9e9d1642007-03-12 09:37:46 +0100714 /*
715 * zero read, fail
716 */
717 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200718 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100719 put_io_u(td, io_u);
720 break;
721 }
722
Jens Axboe36167d82007-02-18 05:41:31 +0100723 io_u->xfer_buflen = io_u->resid;
724 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200725 io_u->offset += bytes;
726
Jens Axboeff58fce2010-08-25 12:02:08 +0200727 if (ddir_rw(io_u->ddir))
728 td->ts.short_io_u[io_u->ddir]++;
Jens Axboe30061b92007-04-17 13:31:34 +0200729
Jens Axboed460eb32007-04-16 21:39:33 +0200730 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200731 goto sync_done;
732
Jens Axboe11786802007-03-05 18:33:22 +0100733 requeue_io_u(td, &io_u);
734 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200735sync_done:
Jens Axboe581e7142009-06-09 12:47:16 +0200736 if (__should_check_rate(td, 0) ||
737 __should_check_rate(td, 1))
Jens Axboe993bf482008-11-14 13:04:53 +0100738 fio_gettime(&comp_time, NULL);
739
Jens Axboe581e7142009-06-09 12:47:16 +0200740 ret = io_u_sync_complete(td, io_u, bytes_done);
741 if (ret < 0)
742 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100743 }
Jens Axboe36167d82007-02-18 05:41:31 +0100744 break;
745 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100746 /*
747 * if the engine doesn't have a commit hook,
748 * the io_u is really queued. if it does have such
749 * a hook, it has to call io_u_queued() itself.
750 */
751 if (td->io_ops->commit == NULL)
752 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100753 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100754 case FIO_Q_BUSY:
755 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100756 ret2 = td_io_commit(td);
757 if (ret2 < 0)
758 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100759 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100760 default:
761 assert(ret < 0);
762 put_io_u(td, io_u);
763 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100764 }
765
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200766 if (break_on_this_error(td, &ret))
Jens Axboe36167d82007-02-18 05:41:31 +0100767 break;
768
Jens Axboe97601022007-02-18 12:47:29 +0100769 /*
Jens Axboeccc02052011-07-14 12:45:13 +0200770 * See if we need to complete some commands. Note that we
771 * can get BUSY even without IO queued, if the system is
772 * resource starved.
Jens Axboe97601022007-02-18 12:47:29 +0100773 */
Jens Axboeccc02052011-07-14 12:45:13 +0200774 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
Jens Axboe49504212008-06-05 09:03:30 +0200775 if (full || !td->o.iodepth_batch_complete) {
Radha Ramachandran6a962762009-11-05 08:33:49 +0100776 min_evts = min(td->o.iodepth_batch_complete,
777 td->cur_depth);
Dan Ehrenberg1f84f1d2011-07-23 13:36:44 +0200778 if (full && !min_evts && td->o.iodepth_batch_complete != 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100779 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200780
Jens Axboe581e7142009-06-09 12:47:16 +0200781 if (__should_check_rate(td, 0) ||
782 __should_check_rate(td, 1))
Jens Axboe993bf482008-11-14 13:04:53 +0100783 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200784
785 do {
Jens Axboe581e7142009-06-09 12:47:16 +0200786 ret = io_u_queued_complete(td, min_evts, bytes_done);
787 if (ret < 0)
Jens Axboe49504212008-06-05 09:03:30 +0200788 break;
789
Jens Axboe49504212008-06-05 09:03:30 +0200790 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100791 }
792
Jens Axboe49504212008-06-05 09:03:30 +0200793 if (ret < 0)
794 break;
Jens Axboe581e7142009-06-09 12:47:16 +0200795 if (!(bytes_done[0] + bytes_done[1]))
Jens Axboe97601022007-02-18 12:47:29 +0100796 continue;
797
Jens Axboe581e7142009-06-09 12:47:16 +0200798 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
799 if (check_min_rate(td, &comp_time, bytes_done)) {
Jens Axboe721938a2008-09-10 09:46:16 +0200800 if (exitall_on_terminate)
Jens Axboecc0df002011-10-03 20:53:32 +0200801 fio_terminate_threads(td->groupid);
Jens Axboe721938a2008-09-10 09:46:16 +0200802 td_verror(td, EIO, "check_min_rate");
803 break;
804 }
Jens Axboeebac4652005-12-08 15:25:21 +0100805 }
806
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100807 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100808 unsigned long long b;
809
810 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100811 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100812 int left;
813
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100814 if (td->o.thinktime_spin)
Jens Axboe6dd6f2c2008-12-10 13:24:12 +0100815 usec_spin(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100816
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100817 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100818 if (left)
819 usec_sleep(td, left);
820 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100821 }
Jens Axboeebac4652005-12-08 15:25:21 +0100822 }
823
Jens Axboed708ac52010-09-02 13:29:55 +0200824 if (td->trim_entries)
825 printf("trim entries %ld\n", td->trim_entries);
Jens Axboea917a8b2010-09-02 13:23:20 +0200826
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100827 if (td->o.fill_device && td->error == ENOSPC) {
828 td->error = 0;
829 td->terminate = 1;
830 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100831 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100832 struct fio_file *f;
833
Jens Axboec01c0392007-02-26 12:43:42 +0100834 i = td->cur_depth;
Steven Langd06093e2011-06-13 09:47:36 +0200835 if (i) {
Jens Axboe581e7142009-06-09 12:47:16 +0200836 ret = io_u_queued_complete(td, i, NULL);
Steven Langd06093e2011-06-13 09:47:36 +0200837 if (td->o.fill_device && td->error == ENOSPC)
838 td->error = 0;
839 }
Jens Axboeebac4652005-12-08 15:25:21 +0100840
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100841 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200842 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100843
844 for_each_file(td, f, i) {
Jens Axboed6aed792009-06-03 08:41:15 +0200845 if (!fio_file_open(f))
Jens Axboe3d7b4852007-03-13 14:50:28 +0100846 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200847 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100848 }
Jens Axboe84585002006-10-19 20:26:22 +0200849 }
Jens Axboec01c0392007-02-26 12:43:42 +0100850 } else
851 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100852
853 /*
854 * stop job if we failed doing any IO
855 */
856 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
857 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100858}
859
Jens Axboeebac4652005-12-08 15:25:21 +0100860static void cleanup_io_u(struct thread_data *td)
861{
Jens Axboe01743ee2008-06-02 12:19:19 +0200862 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100863 struct io_u *io_u;
864
Jens Axboe01743ee2008-06-02 12:19:19 +0200865 flist_for_each_safe(entry, n, &td->io_u_freelist) {
866 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100867
Jens Axboe01743ee2008-06-02 12:19:19 +0200868 flist_del(&io_u->list);
Jens Axboe91aea6e2011-04-25 20:46:58 +0200869 fio_memfree(io_u, sizeof(*io_u));
Jens Axboeebac4652005-12-08 15:25:21 +0100870 }
871
Jens Axboe2f9ade32006-10-20 11:25:52 +0200872 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100873}
874
875static int init_io_u(struct thread_data *td)
876{
877 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100878 unsigned int max_bs;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200879 int cl_align, i, max_units;
Jens Axboeebac4652005-12-08 15:25:21 +0100880 char *p;
881
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100882 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100883 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100884 td->orig_buffer_size = (unsigned long long) max_bs
885 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100886
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100887 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
888 unsigned long bs;
889
890 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
891 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
892 }
Jens Axboeebac4652005-12-08 15:25:21 +0100893
Jens Axboec3990642007-07-19 10:17:09 +0200894 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
895 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
896 return 1;
897 }
898
Jens Axboe2f9ade32006-10-20 11:25:52 +0200899 if (allocate_io_mem(td))
900 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100901
Jens Axboe1c04bf72010-09-07 13:28:17 +0200902 if (td->o.odirect || td->o.mem_align ||
903 (td->io_ops->flags & FIO_RAWIO))
Jens Axboed529ee12009-07-01 10:33:03 +0200904 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
Jens Axboeb3f378e2007-07-20 12:39:22 +0200905 else
906 p = td->orig_buffer;
907
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200908 cl_align = os_cache_line_size();
909
Jens Axboeebac4652005-12-08 15:25:21 +0100910 for (i = 0; i < max_units; i++) {
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200911 void *ptr;
912
Jens Axboedb1cd902007-04-26 13:47:07 +0200913 if (td->terminate)
914 return 1;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200915
Jens Axboe91aea6e2011-04-25 20:46:58 +0200916 ptr = fio_memalign(cl_align, sizeof(*io_u));
917 if (!ptr) {
918 log_err("fio: unable to allocate aligned memory\n");
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200919 break;
920 }
921
922 io_u = ptr;
Jens Axboeebac4652005-12-08 15:25:21 +0100923 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200924 INIT_FLIST_HEAD(&io_u->list);
Jens Axboed529ee12009-07-01 10:33:03 +0200925 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100926
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200927 if (!(td->io_ops->flags & FIO_NOIO)) {
928 io_u->buf = p + max_bs * i;
Jens Axboed529ee12009-07-01 10:33:03 +0200929 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
Jens Axboee9459e52007-04-17 15:46:32 +0200930
Jens Axboe3545a102011-08-31 15:20:15 -0600931 if (td_write(td))
Jens Axboe5973caf2008-05-21 19:52:35 +0200932 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboe3545a102011-08-31 15:20:15 -0600933 if (td_write(td) && td->o.verify_pattern_bytes) {
Radha Ramachandrancbe8d752010-07-14 08:36:07 +0200934 /*
935 * Fill the buffer with the pattern if we are
936 * going to be doing writes.
937 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100938 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
Radha Ramachandrancbe8d752010-07-14 08:36:07 +0200939 }
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200940 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100941
Jens Axboeb1ff3402006-02-17 11:35:58 +0100942 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100943 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200944 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100945 }
946
947 return 0;
948}
949
Jens Axboeda867742006-06-06 10:39:10 -0700950static int switch_ioscheduler(struct thread_data *td)
951{
952 char tmp[256], tmp2[128];
953 FILE *f;
954 int ret;
955
Jens Axboeba0fbe12007-03-09 14:34:23 +0100956 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200957 return 0;
958
Jens Axboeda867742006-06-06 10:39:10 -0700959 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
960
961 f = fopen(tmp, "r+");
962 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200963 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100964 log_err("fio: os or kernel doesn't support IO scheduler"
965 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200966 return 0;
967 }
968 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700969 return 1;
970 }
971
972 /*
973 * Set io scheduler.
974 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100975 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700976 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100977 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700978 fclose(f);
979 return 1;
980 }
981
982 rewind(f);
983
984 /*
985 * Read back and check that the selected scheduler is now the default.
986 */
987 ret = fread(tmp, 1, sizeof(tmp), f);
988 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100989 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700990 fclose(f);
991 return 1;
992 }
993
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100994 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700995 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100996 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100997 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700998 fclose(f);
999 return 1;
1000 }
1001
1002 fclose(f);
1003 return 0;
1004}
1005
Jens Axboe492158c2007-05-24 10:32:47 +02001006static int keep_running(struct thread_data *td)
1007{
1008 unsigned long long io_done;
1009
Jens Axboe20e354e2007-07-23 14:36:16 +02001010 if (td->done)
1011 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001012 if (td->o.time_based)
1013 return 1;
1014 if (td->o.loops) {
1015 td->o.loops--;
1016 return 1;
1017 }
1018
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001019 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
1020 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +02001021 if (io_done < td->o.size)
1022 return 1;
1023
1024 return 0;
1025}
1026
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001027static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +01001028{
Jens Axboef0505a12011-10-03 12:12:03 +02001029 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001030 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboec8eeb9d2011-10-05 14:02:22 +02001031 td->stat_io_blocks[0] = td->stat_io_blocks[1] = 0;
1032 td->this_io_blocks[0] = td->this_io_blocks[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +01001033 td->zone_bytes = 0;
Jens Axboe581e7142009-06-09 12:47:16 +02001034 td->rate_bytes[0] = td->rate_bytes[1] = 0;
1035 td->rate_blocks[0] = td->rate_blocks[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001036
Jens Axboec1324df2007-02-19 19:06:41 +01001037 td->last_was_sync = 0;
1038
Jens Axboe2ba1c292008-02-01 13:16:38 +01001039 /*
1040 * reset file done count if we are to start over
1041 */
1042 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +02001043 td->nr_done_files = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001044}
1045
1046void reset_all_stats(struct thread_data *td)
1047{
1048 struct timeval tv;
1049 int i;
1050
1051 reset_io_counters(td);
1052
1053 for (i = 0; i < 2; i++) {
1054 td->io_bytes[i] = 0;
1055 td->io_blocks[i] = 0;
1056 td->io_issues[i] = 0;
1057 td->ts.total_io_u[i] = 0;
1058 }
Bruce Cran9b836562011-01-08 19:49:54 +01001059
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001060 fio_gettime(&tv, NULL);
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001061 td->ts.runtime[0] = 0;
1062 td->ts.runtime[1] = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001063 memcpy(&td->epoch, &tv, sizeof(tv));
1064 memcpy(&td->start, &tv, sizeof(tv));
1065}
1066
Jens Axboedf9c26b2009-03-05 10:13:58 +01001067static void clear_io_state(struct thread_data *td)
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001068{
1069 struct fio_file *f;
1070 unsigned int i;
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001071
1072 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +02001073
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001074 close_files(td);
Jens Axboedf9c26b2009-03-05 10:13:58 +01001075 for_each_file(td, f, i)
Jens Axboed6aed792009-06-03 08:41:15 +02001076 fio_file_clear_done(f);
Jens Axboef0153942011-07-07 15:35:27 +02001077
1078 /*
1079 * Set the same seed to get repeatable runs
1080 */
1081 td_fill_rand_seeds(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001082}
1083
Jens Axboe398e8c42009-05-20 12:10:02 +02001084static int exec_string(const char *string)
1085{
1086 int ret, newlen = strlen(string) + 1 + 8;
1087 char *str;
1088
1089 str = malloc(newlen);
1090 sprintf(str, "sh -c %s", string);
1091
1092 ret = system(str);
1093 if (ret == -1)
1094 log_err("fio: exec of cmd <%s> failed\n", str);
1095
1096 free(str);
1097 return ret;
1098}
1099
Jens Axboe906c8d72006-06-13 09:37:56 +02001100/*
1101 * Entry point for the thread based jobs. The process based jobs end up
1102 * here as well, after a little setup.
1103 */
Jens Axboeebac4652005-12-08 15:25:21 +01001104static void *thread_main(void *data)
1105{
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001106 unsigned long long elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +01001107 struct thread_data *td = data;
Jens Axboee8462bd2009-07-06 12:59:04 +02001108 pthread_condattr_t attr;
Jens Axboea978ba62007-03-08 14:29:03 +01001109 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +01001110
Jens Axboe47f767c2011-07-12 21:17:49 +02001111 if (!td->o.use_thread) {
Jens Axboeebac4652005-12-08 15:25:21 +01001112 setsid();
Jens Axboe47f767c2011-07-12 21:17:49 +02001113 td->pid = getpid();
1114 } else
1115 td->pid = gettid();
Jens Axboeebac4652005-12-08 15:25:21 +01001116
Jens Axboe5921e802008-05-30 15:02:38 +02001117 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +01001118
Jens Axboe01743ee2008-06-02 12:19:19 +02001119 INIT_FLIST_HEAD(&td->io_u_freelist);
1120 INIT_FLIST_HEAD(&td->io_u_busylist);
1121 INIT_FLIST_HEAD(&td->io_u_requeues);
1122 INIT_FLIST_HEAD(&td->io_log_list);
1123 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001124 INIT_FLIST_HEAD(&td->verify_list);
Jens Axboe0d29de82010-09-01 13:54:15 +02001125 INIT_FLIST_HEAD(&td->trim_list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001126 pthread_mutex_init(&td->io_u_lock, NULL);
Jens Axboebb5d7d02007-03-27 10:21:25 +02001127 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +02001128
Jens Axboee8462bd2009-07-06 12:59:04 +02001129 pthread_condattr_init(&attr);
1130 pthread_cond_init(&td->verify_cond, &attr);
1131 pthread_cond_init(&td->free_cond, &attr);
1132
Jens Axboe75154842006-06-01 13:56:09 +02001133 td_set_runstate(td, TD_INITIALIZED);
Jens Axboe29adda32009-01-05 19:06:39 +01001134 dprint(FD_MUTEX, "up startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +01001135 fio_mutex_up(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001136 dprint(FD_MUTEX, "wait on td->mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +01001137 fio_mutex_down(td->mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001138 dprint(FD_MUTEX, "done waiting on td->mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001139
Jens Axboe37f56872007-03-09 12:42:35 +01001140 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +01001141 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +01001142 * eating a file descriptor
1143 */
Jens Axboecdd18ad2008-02-27 18:58:00 +01001144 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +01001145
Stefan Hajnoczidfc6e752011-01-07 20:58:05 +00001146 /*
1147 * A new gid requires privilege, so we need to do this before setting
1148 * the uid.
1149 */
Jens Axboee0b0d892009-12-08 10:10:14 +01001150 if (td->o.gid != -1U && setgid(td->o.gid)) {
1151 td_verror(td, errno, "setgid");
1152 goto err;
1153 }
Stefan Hajnoczidfc6e752011-01-07 20:58:05 +00001154 if (td->o.uid != -1U && setuid(td->o.uid)) {
1155 td_verror(td, errno, "setuid");
1156 goto err;
1157 }
Jens Axboee0b0d892009-12-08 10:10:14 +01001158
Jens Axboed84f8d42007-05-16 08:47:46 +02001159 /*
Jens Axboebe18a942011-07-08 19:25:04 +02001160 * If we have a gettimeofday() thread, make sure we exclude that
1161 * thread from this job
1162 */
1163 if (td->o.gtod_cpu)
1164 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1165
1166 /*
1167 * Set affinity first, in case it has an impact on the memory
1168 * allocations.
1169 */
1170 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1171 td_verror(td, errno, "cpu_set_affinity");
1172 goto err;
1173 }
1174
1175 /*
Jens Axboed84f8d42007-05-16 08:47:46 +02001176 * May alter parameters that init_io_u() will use, so we need to
1177 * do this first.
1178 */
1179 if (init_iolog(td))
1180 goto err;
1181
Jens Axboedb1cd902007-04-26 13:47:07 +02001182 if (init_io_u(td))
1183 goto err;
1184
Jens Axboee8462bd2009-07-06 12:59:04 +02001185 if (td->o.verify_async && verify_async_init(td))
1186 goto err;
1187
Jens Axboeac684782007-11-08 08:29:07 +01001188 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +02001189 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1190 td_verror(td, errno, "ioprio_set");
1191 goto err;
1192 }
1193 }
1194
Jens Axboe6adb38a2009-12-07 08:01:26 +01001195 if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
Jens Axboea696fa22009-12-04 10:05:02 +01001196 goto err;
1197
Jens Axboedb1cd902007-04-26 13:47:07 +02001198 if (nice(td->o.nice) == -1) {
1199 td_verror(td, errno, "nice");
1200 goto err;
1201 }
1202
1203 if (td->o.ioscheduler && switch_ioscheduler(td))
1204 goto err;
1205
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001206 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +01001207 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +01001208
1209 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +01001210 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +01001211
Jens Axboe68727072007-03-15 20:49:25 +01001212 if (init_random_map(td))
1213 goto err;
1214
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001215 if (td->o.exec_prerun) {
Jens Axboe398e8c42009-05-20 12:10:02 +02001216 if (exec_string(td->o.exec_prerun))
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001217 goto err;
1218 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001219
Zhang, Yanminafad68f2009-05-20 11:30:55 +02001220 if (td->o.pre_read) {
1221 if (pre_read_files(td) < 0)
1222 goto err;
1223 }
1224
Jens Axboe69008992006-11-24 13:12:56 +01001225 fio_gettime(&td->epoch, NULL);
Jens Axboec8aaba12011-10-03 12:14:19 +02001226 getrusage(RUSAGE_SELF, &td->ru_start);
Jens Axboe69008992006-11-24 13:12:56 +01001227
Jens Axboea978ba62007-03-08 14:29:03 +01001228 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001229 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +01001230 fio_gettime(&td->start, NULL);
Jens Axboec8eeb9d2011-10-05 14:02:22 +02001231 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1232 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
Jens Axboe993bf482008-11-14 13:04:53 +01001233 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +01001234
Jens Axboecf2dd8d2009-06-09 12:58:37 +02001235 if (td->o.ratemin[0] || td->o.ratemin[1])
Jens Axboec8eeb9d2011-10-05 14:02:22 +02001236 memcpy(&td->lastrate, &td->bw_sample_time,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001237 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +01001238
Jens Axboedf9c26b2009-03-05 10:13:58 +01001239 if (clear_state)
1240 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001241
Jens Axboeebac4652005-12-08 15:25:21 +01001242 prune_io_piece_log(td);
1243
Jens Axboeba0fbe12007-03-09 14:34:23 +01001244 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001245
Jens Axboea978ba62007-03-08 14:29:03 +01001246 clear_state = 1;
1247
Jens Axboe38d77ca2007-03-21 14:20:20 +01001248 if (td_read(td) && td->io_bytes[DDIR_READ]) {
Jens Axboe0a4957c2009-06-03 10:30:33 +02001249 elapsed = utime_since_now(&td->start);
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001250 td->ts.runtime[DDIR_READ] += elapsed;
Jens Axboe38d77ca2007-03-21 14:20:20 +01001251 }
1252 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
Jens Axboe0a4957c2009-06-03 10:30:33 +02001253 elapsed = utime_since_now(&td->start);
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001254 td->ts.runtime[DDIR_WRITE] += elapsed;
Jens Axboe38d77ca2007-03-21 14:20:20 +01001255 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001256
Jens Axboeebac4652005-12-08 15:25:21 +01001257 if (td->error || td->terminate)
1258 break;
1259
Shawn Lewise84c73a2007-08-02 22:19:32 +02001260 if (!td->o.do_verify ||
1261 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001262 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001263 continue;
1264
Jens Axboedf9c26b2009-03-05 10:13:58 +01001265 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001266
Jens Axboe02bcaa82006-11-24 10:42:00 +01001267 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001268
1269 do_verify(td);
1270
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001271 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001272
1273 if (td->error || td->terminate)
1274 break;
1275 }
1276
Jens Axboe36dff962006-11-24 13:29:20 +01001277 update_rusage_stat(td);
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001278 td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000;
1279 td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001280 td->ts.total_run_time = mtime_since_now(&td->epoch);
1281 td->ts.io_bytes[0] = td->io_bytes[0];
1282 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001283
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001284 fio_mutex_down(writeout_mutex);
Jens Axboe7b9f7332011-10-03 10:06:44 +02001285 if (td->bw_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001286 if (td->o.bw_log_file) {
Jens Axboe7b9f7332011-10-03 10:06:44 +02001287 finish_log_named(td, td->bw_log,
Jens Axboef4844702008-12-09 08:04:29 +01001288 td->o.bw_log_file, "bw");
1289 } else
Jens Axboe7b9f7332011-10-03 10:06:44 +02001290 finish_log(td, td->bw_log, "bw");
Jens Axboee3cedca2008-11-19 19:57:52 +01001291 }
Jens Axboe7b9f7332011-10-03 10:06:44 +02001292 if (td->lat_log) {
Jens Axboe02af0982010-06-24 09:59:34 +02001293 if (td->o.lat_log_file) {
Jens Axboe7b9f7332011-10-03 10:06:44 +02001294 finish_log_named(td, td->lat_log,
Jens Axboe02af0982010-06-24 09:59:34 +02001295 td->o.lat_log_file, "lat");
1296 } else
Jens Axboe7b9f7332011-10-03 10:06:44 +02001297 finish_log(td, td->lat_log, "lat");
Jens Axboe02af0982010-06-24 09:59:34 +02001298 }
Jens Axboe7b9f7332011-10-03 10:06:44 +02001299 if (td->slat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001300 if (td->o.lat_log_file) {
Jens Axboe7b9f7332011-10-03 10:06:44 +02001301 finish_log_named(td, td->slat_log,
Radha Ramachandran1ca95732009-04-29 07:23:26 +02001302 td->o.lat_log_file, "slat");
Jens Axboef4844702008-12-09 08:04:29 +01001303 } else
Jens Axboe7b9f7332011-10-03 10:06:44 +02001304 finish_log(td, td->slat_log, "slat");
Jens Axboee3cedca2008-11-19 19:57:52 +01001305 }
Jens Axboe7b9f7332011-10-03 10:06:44 +02001306 if (td->clat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001307 if (td->o.lat_log_file) {
Jens Axboe7b9f7332011-10-03 10:06:44 +02001308 finish_log_named(td, td->clat_log,
Jens Axboef4844702008-12-09 08:04:29 +01001309 td->o.lat_log_file, "clat");
1310 } else
Jens Axboe7b9f7332011-10-03 10:06:44 +02001311 finish_log(td, td->clat_log, "clat");
Jens Axboee3cedca2008-11-19 19:57:52 +01001312 }
Jens Axboec8eeb9d2011-10-05 14:02:22 +02001313 if (td->iops_log) {
1314 if (td->o.iops_log_file) {
1315 finish_log_named(td, td->iops_log,
1316 td->o.iops_log_file, "iops");
1317 } else
1318 finish_log(td, td->iops_log, "iops");
1319 }
1320
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001321 fio_mutex_up(writeout_mutex);
Jens Axboe398e8c42009-05-20 12:10:02 +02001322 if (td->o.exec_postrun)
1323 exec_string(td->o.exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +01001324
1325 if (exitall_on_terminate)
Jens Axboecc0df002011-10-03 20:53:32 +02001326 fio_terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001327
1328err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001329 if (td->error)
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001330 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001331 td->verror);
Jens Axboeb4707ed2009-11-04 12:51:17 +01001332
1333 if (td->o.verify_async)
1334 verify_async_exit(td);
1335
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001336 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001337 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001338 cleanup_io_u(td);
Jens Axboe6adb38a2009-12-07 08:01:26 +01001339 cgroup_shutdown(td, &cgroup_mnt);
Jens Axboef29b25a2007-07-23 08:56:43 +02001340
Jens Axboed2ce18b2008-12-12 20:51:40 +01001341 if (td->o.cpumask_set) {
1342 int ret = fio_cpuset_exit(&td->o.cpumask);
1343
1344 td_verror(td, ret, "fio_cpuset_exit");
1345 }
Jens Axboe4e78e402008-12-12 20:40:27 +01001346
Jens Axboef29b25a2007-07-23 08:56:43 +02001347 /*
1348 * do this very late, it will log file closing as well
1349 */
1350 if (td->o.write_iolog_file)
1351 write_iolog_close(td);
1352
Jens Axboed23bb322007-03-23 15:57:56 +01001353 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001354 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001355 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001356}
1357
Jens Axboe906c8d72006-06-13 09:37:56 +02001358/*
1359 * We cannot pass the td data into a forked process, so attach the td and
1360 * pass it to the thread worker.
1361 */
Jens Axboea6418142007-02-17 04:47:18 +01001362static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001363{
1364 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001365 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001366
Jens Axboe65258962011-07-09 08:31:53 +02001367#ifndef __hpux
Jens Axboeebac4652005-12-08 15:25:21 +01001368 data = shmat(shmid, NULL, 0);
1369 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001370 int __err = errno;
1371
Jens Axboeebac4652005-12-08 15:25:21 +01001372 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001373 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001374 }
Jens Axboe65258962011-07-09 08:31:53 +02001375#else
1376 /*
1377 * HP-UX inherits shm mappings?
1378 */
1379 data = threads;
1380#endif
Jens Axboeebac4652005-12-08 15:25:21 +01001381
1382 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001383 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001384 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001385 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001386}
1387
Jens Axboe906c8d72006-06-13 09:37:56 +02001388/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001389 * Run over the job map and reap the threads that have exited, if any.
1390 */
Jens Axboeebac4652005-12-08 15:25:21 +01001391static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1392{
Jens Axboe34572e22006-10-20 09:33:18 +02001393 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001394 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001395
1396 /*
1397 * reap exited threads (TD_EXITED -> TD_REAPED)
1398 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001399 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001400 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001401 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001402
Jens Axboe84585002006-10-19 20:26:22 +02001403 /*
1404 * ->io_ops is NULL for a thread that has closed its
1405 * io engine
1406 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001407 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001408 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001409 else
1410 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001411
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001412 if (!td->pid) {
1413 pending++;
1414 continue;
1415 }
1416 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001417 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001418 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001419 if (td->runstate == TD_EXITED) {
1420 td_set_runstate(td, TD_REAPED);
1421 goto reaped;
1422 }
1423 continue;
1424 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001425
1426 flags = WNOHANG;
1427 if (td->runstate == TD_EXITED)
1428 flags = 0;
1429
1430 /*
1431 * check if someone quit or got killed in an unusual way
1432 */
1433 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001434 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001435 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001436 log_err("fio: pid=%d disappeared %d\n",
1437 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001438 td_set_runstate(td, TD_REAPED);
1439 goto reaped;
1440 }
1441 perror("waitpid");
1442 } else if (ret == td->pid) {
1443 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001444 int sig = WTERMSIG(status);
1445
Bruce Cran03e20d62011-01-02 20:14:54 +01001446 if (sig != SIGTERM)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001447 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001448 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001449 td_set_runstate(td, TD_REAPED);
1450 goto reaped;
1451 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001452 if (WIFEXITED(status)) {
1453 if (WEXITSTATUS(status) && !td->error)
1454 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001455
Jens Axboea2f77c92007-02-22 11:53:00 +01001456 td_set_runstate(td, TD_REAPED);
1457 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001458 }
1459 }
Jens Axboeebac4652005-12-08 15:25:21 +01001460
Jens Axboea2f77c92007-02-22 11:53:00 +01001461 /*
1462 * thread is not dead, continue
1463 */
gurudas paie48676b2007-04-11 12:44:27 +02001464 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001465 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001466reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001467 (*nr_running)--;
Jens Axboe581e7142009-06-09 12:47:16 +02001468 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1469 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001470 if (!td->pid)
1471 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001472
1473 if (td->error)
1474 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001475
1476 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001477 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001478
Jens Axboe1f809d12007-10-25 18:34:02 +02001479 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboecc0df002011-10-03 20:53:32 +02001480 fio_terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001481}
1482
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001483static void *gtod_thread_main(void *data)
1484{
1485 fio_mutex_up(startup_mutex);
1486
1487 /*
1488 * As long as we have jobs around, update the clock. It would be nice
1489 * to have some way of NOT hammering that CPU with gettimeofday(),
1490 * but I'm not sure what to use outside of a simple CPU nop to relax
1491 * it - we don't want to lose precision.
1492 */
1493 while (threads) {
1494 fio_gtod_update();
1495 nop;
1496 }
1497
1498 return NULL;
1499}
1500
1501static int fio_start_gtod_thread(void)
1502{
bart Van Assche304a47c2010-08-01 21:31:26 +02001503 pthread_attr_t attr;
Jens Axboef42c1532009-01-05 19:07:13 +01001504 int ret;
1505
bart Van Assche304a47c2010-08-01 21:31:26 +02001506 pthread_attr_init(&attr);
1507 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1508 ret = pthread_create(&gtod_thread, &attr, gtod_thread_main, NULL);
1509 pthread_attr_destroy(&attr);
Jens Axboef42c1532009-01-05 19:07:13 +01001510 if (ret) {
1511 log_err("Can't create gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001512 return 1;
1513 }
Jens Axboef42c1532009-01-05 19:07:13 +01001514
1515 ret = pthread_detach(gtod_thread);
1516 if (ret) {
1517 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001518 return 1;
1519 }
1520
Jens Axboe29adda32009-01-05 19:06:39 +01001521 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001522 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001523 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001524 return 0;
1525}
1526
Jens Axboe906c8d72006-06-13 09:37:56 +02001527/*
1528 * Main function for kicking off and reaping jobs, as needed.
1529 */
Jens Axboeebac4652005-12-08 15:25:21 +01001530static void run_threads(void)
1531{
Jens Axboeebac4652005-12-08 15:25:21 +01001532 struct thread_data *td;
1533 unsigned long spent;
1534 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001535
Jens Axboe2f9ade32006-10-20 11:25:52 +02001536 if (fio_pin_memory())
1537 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001538
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001539 if (fio_gtod_offload && fio_start_gtod_thread())
1540 return;
1541
Jens Axboecc0df002011-10-03 20:53:32 +02001542 set_sig_handlers();
1543
Jens Axboec6ae0a52006-06-12 11:04:44 +02001544 if (!terse_output) {
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001545 log_info("Starting ");
Jens Axboe9cedf162007-03-12 11:29:30 +01001546 if (nr_thread)
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001547 log_info("%d thread%s", nr_thread,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001548 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001549 if (nr_process) {
1550 if (nr_thread)
1551 printf(" and ");
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001552 log_info("%d process%s", nr_process,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001553 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001554 }
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001555 log_info("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001556 fflush(stdout);
1557 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001558
Jens Axboeebac4652005-12-08 15:25:21 +01001559 todo = thread_number;
1560 nr_running = 0;
1561 nr_started = 0;
1562 m_rate = t_rate = 0;
1563
Jens Axboe34572e22006-10-20 09:33:18 +02001564 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001565 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001566
Jens Axboe067430b2011-09-08 14:30:24 +02001567 if (!td->o.create_serialize)
Jens Axboeebac4652005-12-08 15:25:21 +01001568 continue;
1569
1570 /*
1571 * do file setup here so it happens sequentially,
1572 * we don't want X number of threads getting their
1573 * client data interspersed on disk
1574 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001575 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001576 exit_value++;
1577 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001578 log_err("fio: pid=%d, err=%d/%s\n",
1579 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001580 td_set_runstate(td, TD_REAPED);
1581 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001582 } else {
1583 struct fio_file *f;
Jens Axboe2b13e712011-01-19 14:04:16 -07001584 unsigned int j;
Jens Axboec69e8872008-03-01 18:50:48 +01001585
1586 /*
1587 * for sharing to work, each job must always open
1588 * its own files. so close them, if we opened them
1589 * for creation
1590 */
Jens Axboe2b13e712011-01-19 14:04:16 -07001591 for_each_file(td, f, j) {
Jens Axboe22a57ba2009-06-09 14:34:35 +02001592 if (fio_file_open(f))
1593 td_io_close_file(td, f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001594 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001595 }
Jens Axboeebac4652005-12-08 15:25:21 +01001596 }
1597
Jens Axboea2f77c92007-02-22 11:53:00 +01001598 set_genesis_time();
1599
Jens Axboeebac4652005-12-08 15:25:21 +01001600 while (todo) {
Jens Axboefca70352011-07-06 20:12:54 +02001601 struct thread_data *map[REAL_MAX_JOBS];
Jens Axboe75154842006-06-01 13:56:09 +02001602 struct timeval this_start;
1603 int this_jobs = 0, left;
1604
Jens Axboeebac4652005-12-08 15:25:21 +01001605 /*
1606 * create threads (TD_NOT_CREATED -> TD_CREATED)
1607 */
Jens Axboe34572e22006-10-20 09:33:18 +02001608 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001609 if (td->runstate != TD_NOT_CREATED)
1610 continue;
1611
1612 /*
1613 * never got a chance to start, killed by other
1614 * thread for some reason
1615 */
1616 if (td->terminate) {
1617 todo--;
1618 continue;
1619 }
1620
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001621 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001622 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001623
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001624 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001625 continue;
1626 }
1627
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001628 if (td->o.stonewall && (nr_started || nr_running)) {
1629 dprint(FD_PROCESS, "%s: stonewall wait\n",
1630 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001631 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001632 }
Jens Axboeebac4652005-12-08 15:25:21 +01001633
Jens Axboe067430b2011-09-08 14:30:24 +02001634 init_disk_util(td);
1635
Jens Axboe75154842006-06-01 13:56:09 +02001636 /*
1637 * Set state to created. Thread will transition
1638 * to TD_INITIALIZED when it's done setting up.
1639 */
Jens Axboeebac4652005-12-08 15:25:21 +01001640 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001641 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001642 nr_started++;
1643
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001644 if (td->o.use_thread) {
Jens Axboef42c1532009-01-05 19:07:13 +01001645 int ret;
1646
Jens Axboeee56ad52008-02-01 10:30:20 +01001647 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboef42c1532009-01-05 19:07:13 +01001648 ret = pthread_create(&td->thread, NULL,
1649 thread_main, td);
1650 if (ret) {
1651 log_err("pthread_create: %s\n",
1652 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001653 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001654 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001655 }
Jens Axboef42c1532009-01-05 19:07:13 +01001656 ret = pthread_detach(td->thread);
1657 if (ret)
1658 log_err("pthread_detach: %s",
1659 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001660 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001661 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001662 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001663 pid = fork();
1664 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001665 int ret = fork_main(shm_id, i);
1666
Jens Axboe5e1d3062008-05-23 11:55:53 +02001667 _exit(ret);
1668 } else if (i == fio_debug_jobno)
1669 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001670 }
Jens Axboe29adda32009-01-05 19:06:39 +01001671 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboe656b1392009-07-01 23:02:10 +02001672 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1673 log_err("fio: job startup hung? exiting.\n");
Jens Axboecc0df002011-10-03 20:53:32 +02001674 fio_terminate_threads(TERMINATE_ALL);
Jens Axboe656b1392009-07-01 23:02:10 +02001675 fio_abort = 1;
1676 nr_started--;
1677 break;
1678 }
Jens Axboe29adda32009-01-05 19:06:39 +01001679 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001680 }
1681
1682 /*
Jens Axboe75154842006-06-01 13:56:09 +02001683 * Wait for the started threads to transition to
1684 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001685 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001686 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001687 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001688 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001689 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1690 break;
1691
1692 usleep(100000);
1693
1694 for (i = 0; i < this_jobs; i++) {
1695 td = map[i];
1696 if (!td)
1697 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001698 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001699 map[i] = NULL;
1700 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001701 } else if (td->runstate >= TD_EXITED) {
1702 map[i] = NULL;
1703 left--;
1704 todo--;
1705 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001706 }
1707 }
1708 }
1709
1710 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001711 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001712 for (i = 0; i < this_jobs; i++) {
1713 td = map[i];
1714 if (!td)
1715 continue;
1716 kill(td->pid, SIGTERM);
1717 }
1718 break;
1719 }
1720
1721 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001722 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001723 */
Jens Axboe34572e22006-10-20 09:33:18 +02001724 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001725 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001726 continue;
1727
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001728 if (in_ramp_time(td))
1729 td_set_runstate(td, TD_RAMP);
1730 else
1731 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001732 nr_running++;
1733 nr_started--;
Jens Axboe581e7142009-06-09 12:47:16 +02001734 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1735 t_rate += td->o.rate[0] + td->o.rate[1];
Jens Axboe75154842006-06-01 13:56:09 +02001736 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001737 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001738 }
1739
1740 reap_threads(&nr_running, &t_rate, &m_rate);
1741
Jens Axboe602d6ff2011-10-07 12:16:19 +02001742 if (todo) {
1743 if (is_backend)
1744 fio_server_idle_loop();
1745 else
1746 usleep(100000);
1747 }
Jens Axboeebac4652005-12-08 15:25:21 +01001748 }
1749
1750 while (nr_running) {
1751 reap_threads(&nr_running, &t_rate, &m_rate);
Jens Axboecc0df002011-10-03 20:53:32 +02001752
1753 if (is_backend)
1754 fio_server_idle_loop();
Jens Axboe70e0c312011-10-04 09:18:30 +02001755 else
1756 usleep(10000);
Jens Axboeebac4652005-12-08 15:25:21 +01001757 }
1758
1759 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001760 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001761}
1762
Jens Axboe50d16972011-09-29 17:45:28 -06001763int exec_run(void)
Jens Axboeebac4652005-12-08 15:25:21 +01001764{
Jens Axboea37f69b2011-10-01 12:24:21 -06001765 if (nr_clients)
Jens Axboe37db14f2011-09-30 17:00:42 -06001766 return fio_handle_clients();
Jens Axboe07b32322010-03-05 09:48:44 +01001767 if (exec_profile && load_profile(exec_profile))
1768 return 1;
1769
Jens Axboe74929ac2009-08-05 11:42:37 +02001770 if (!thread_number)
1771 return 0;
1772
Jens Axboebb3884d2007-01-17 17:23:11 +11001773 if (write_bw_log) {
1774 setup_log(&agg_io_log[DDIR_READ]);
1775 setup_log(&agg_io_log[DDIR_WRITE]);
1776 }
1777
Jens Axboecdd18ad2008-02-27 18:58:00 +01001778 startup_mutex = fio_mutex_init(0);
Bruce Cran03e20d62011-01-02 20:14:54 +01001779 if (startup_mutex == NULL)
1780 return 1;
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001781 writeout_mutex = fio_mutex_init(1);
Bruce Cran03e20d62011-01-02 20:14:54 +01001782 if (writeout_mutex == NULL)
1783 return 1;
Jens Axboe07739b52007-03-08 20:25:46 +01001784
Jens Axboea2f77c92007-02-22 11:53:00 +01001785 set_genesis_time();
Jens Axboe79857012011-01-14 14:14:58 +01001786 create_disk_util_thread();
Jens Axboeebac4652005-12-08 15:25:21 +01001787
Jens Axboedae53412009-12-04 19:54:18 +01001788 cgroup_list = smalloc(sizeof(*cgroup_list));
1789 INIT_FLIST_HEAD(cgroup_list);
1790
Jens Axboeebac4652005-12-08 15:25:21 +01001791 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001792
Jens Axboebb3884d2007-01-17 17:23:11 +11001793 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001794 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001795 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001796 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1797 __finish_log(agg_io_log[DDIR_WRITE],
1798 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001799 }
1800 }
Jens Axboeebac4652005-12-08 15:25:21 +01001801
Jens Axboedae53412009-12-04 19:54:18 +01001802 cgroup_kill(cgroup_list);
1803 sfree(cgroup_list);
Jens Axboe61eb3132010-03-31 20:05:59 +02001804 sfree(cgroup_mnt);
Jens Axboe39f22022009-12-04 11:29:12 +01001805
Jens Axboecdd18ad2008-02-27 18:58:00 +01001806 fio_mutex_remove(startup_mutex);
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001807 fio_mutex_remove(writeout_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001808 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001809}
Jens Axboe50d16972011-09-29 17:45:28 -06001810
1811void reset_fio_state(void)
1812{
1813 groupid = 0;
1814 thread_number = 0;
1815 nr_process = 0;
1816 nr_thread = 0;
1817 done_secs = 0;
1818}
1819
Jens Axboe0ad5edc2011-10-05 12:39:06 +02001820static int endian_check(void)
1821{
1822 union {
1823 uint8_t c[8];
1824 uint64_t v;
1825 } u;
1826 int le = 0, be = 0;
1827
1828 u.v = 0x12;
1829 if (u.c[7] == 0x12)
1830 be = 1;
1831 else if (u.c[0] == 0x12)
1832 le = 1;
1833
1834#if defined(FIO_LITTLE_ENDIAN)
1835 if (be)
1836 return 1;
1837#elif defined(FIO_BIG_ENDIAN)
1838 if (le)
1839 return 1;
1840#else
1841 return 1;
1842#endif
1843
1844 if (!le && !be)
1845 return 1;
1846
1847 return 0;
1848}
1849
Jens Axboe50d16972011-09-29 17:45:28 -06001850int main(int argc, char *argv[], char *envp[])
1851{
1852 long ps;
1853
Jens Axboe0ad5edc2011-10-05 12:39:06 +02001854 if (endian_check()) {
1855 log_err("fio: endianness settings appear wrong.\n");
1856 log_err("fio: please report this to fio@vger.kernel.org\n");
1857 return 1;
1858 }
1859
Jens Axboe50d16972011-09-29 17:45:28 -06001860 arch_init(envp);
1861
1862 sinit();
1863
1864 /*
1865 * We need locale for number printing, if it isn't set then just
1866 * go with the US format.
1867 */
1868 if (!getenv("LC_NUMERIC"))
1869 setlocale(LC_NUMERIC, "en_US");
1870
1871 ps = sysconf(_SC_PAGESIZE);
1872 if (ps < 0) {
1873 log_err("Failed to get page size\n");
1874 return 1;
1875 }
1876
1877 page_size = ps;
1878 page_mask = ps - 1;
1879
1880 fio_keywords_init();
1881
1882 if (parse_options(argc, argv))
1883 return 1;
1884
1885 return exec_run();
1886}