Pass arch/os in probe
[fio.git] / fio.c
CommitLineData
ebac4655
JA
1/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
aae22ca7 5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
ebac4655 6 *
8e9fe637
JA
7 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
ebac4655 10 * This program is free software; you can redistribute it and/or modify
8e9fe637
JA
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
ebac4655
JA
13 *
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 */
ebac4655
JA
24#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
9b836561 27#include <limits.h>
ebac4655
JA
28#include <signal.h>
29#include <time.h>
dbe1125e 30#include <locale.h>
36167d82 31#include <assert.h>
a5b01f1b 32#include <time.h>
ebac4655
JA
33#include <sys/stat.h>
34#include <sys/wait.h>
35#include <sys/ipc.h>
36#include <sys/shm.h>
ebac4655
JA
37#include <sys/mman.h>
38
39#include "fio.h"
210f43b3 40#include "hash.h"
2e5cdb11 41#include "smalloc.h"
4f5af7b2 42#include "verify.h"
a917a8b3 43#include "trim.h"
7c9b1bce 44#include "diskutil.h"
a696fa2a 45#include "cgroup.h"
07b3232d 46#include "profile.h"
d54a144d 47#include "lib/rand.h"
91aea6ec 48#include "memalign.h"
009b1be4 49#include "server.h"
ebac4655 50
cfc99db7
JA
51unsigned long page_mask;
52unsigned long page_size;
d529ee19
JA
53
54#define PAGE_ALIGN(buf) \
29d610e1 55 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
ebac4655
JA
56
57int groupid = 0;
58int thread_number = 0;
9cedf167
JA
59int nr_process = 0;
60int nr_thread = 0;
ebac4655 61int shm_id = 0;
53cdc686 62int temp_stall_ts;
d3eeeabc 63unsigned long done_secs = 0;
ebac4655 64
cdd18ad8 65static struct fio_mutex *startup_mutex;
d2bbb8d9 66static struct fio_mutex *writeout_mutex;
6ce15a32 67static volatile int fio_abort;
437c9b71 68static int exit_value;
be4ecfdf 69static pthread_t gtod_thread;
7985701b 70static pthread_t disk_util_thread;
dae5341c 71static struct flist_head *cgroup_list;
6adb38a1 72static char *cgroup_mnt;
ebac4655 73
d3cc4ebf
JA
74unsigned long arch_flags = 0;
75
bb3884d8
JA
76struct io_log *agg_io_log[2];
77
75154845 78#define JOB_START_TIMEOUT (5 * 1000)
ebac4655 79
cca84643
JA
80static 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
b29ee5b3 124void td_set_runstate(struct thread_data *td, int runstate)
6ce15a32 125{
d8fab1b6
JA
126 if (td->runstate == runstate)
127 return;
128
5921e80c
JA
129 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
130 td->runstate, runstate);
6ce15a32
JA
131 td->runstate = runstate;
132}
133
cc0df00a 134void fio_terminate_threads(int group_id)
ebac4655 135{
34572e28 136 struct thread_data *td;
ebac4655
JA
137 int i;
138
6f88bcb0
JA
139 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
140
34572e28 141 for_each_td(td, i) {
ebac4655 142 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
6f88bcb0 143 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
5921e80c 144 td->o.name, (int) td->pid);
ad830ed7
JA
145 td->terminate = 1;
146 td->o.start_delay = 0;
147
7a93ab19
JA
148 /*
149 * if the thread is running, just let it exit
150 */
6aa27816
MC
151 if (!td->pid)
152 continue;
153 else if (td->runstate < TD_RAMP)
03e20d68 154 kill(td->pid, SIGTERM);
f63f7f3b
JA
155 else {
156 struct ioengine_ops *ops = td->io_ops;
157
03e20d68
BC
158 if (ops && (ops->flags & FIO_SIGTERM))
159 kill(td->pid, SIGTERM);
f63f7f3b 160 }
ebac4655
JA
161 }
162 }
163}
164
697a606c
JA
165static void sig_int(int sig)
166{
167 if (threads) {
4ceb30d4 168 log_info("\nfio: terminating on signal %d\n", sig);
009b1be4 169 exit_backend = 1;
5ec10eaa 170 fflush(stdout);
deaa1af2 171 exit_value = 128;
cc0df00a 172 fio_terminate_threads(TERMINATE_ALL);
ebac4655
JA
173 }
174}
175
7985701b 176static void *disk_thread_main(void *data)
a5b01f1b 177{
7985701b
JA
178 fio_mutex_up(startup_mutex);
179
180 while (threads) {
181 usleep(DISK_UTIL_MSEC * 1000);
9a793c2f
JA
182 if (!threads)
183 break;
7985701b 184 update_io_ticks();
cf451d1e
JA
185
186 if (is_backend)
187 fio_server_send_status();
188 else
189 print_thread_status();
7985701b
JA
190 }
191
192 return NULL;
a5b01f1b
JA
193}
194
7985701b 195static int create_disk_util_thread(void)
a5b01f1b 196{
7985701b 197 int ret;
a5b01f1b 198
69204d6e 199 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
7985701b
JA
200 if (ret) {
201 log_err("Can't create disk util thread: %s\n", strerror(ret));
202 return 1;
203 }
a5b01f1b 204
7985701b
JA
205 ret = pthread_detach(disk_util_thread);
206 if (ret) {
207 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
208 return 1;
209 }
03e20d68 210
7985701b
JA
211 dprint(FD_MUTEX, "wait on startup_mutex\n");
212 fio_mutex_down(startup_mutex);
213 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
214 return 0;
a5b01f1b
JA
215}
216
697a606c
JA
217static void set_sig_handlers(void)
218{
219 struct sigaction act;
220
697a606c
JA
221 memset(&act, 0, sizeof(act));
222 act.sa_handler = sig_int;
223 act.sa_flags = SA_RESTART;
224 sigaction(SIGINT, &act, NULL);
de79c915 225
5f8f9726 226 memset(&act, 0, sizeof(act));
b20b6be4 227 act.sa_handler = sig_int;
5f8f9726 228 act.sa_flags = SA_RESTART;
03e20d68 229 sigaction(SIGTERM, &act, NULL);
cc0df00a
JA
230
231 if (is_backend) {
232 memset(&act, 0, sizeof(act));
233 act.sa_handler = sig_int;
234 act.sa_flags = SA_RESTART;
235 sigaction(SIGPIPE, &act, NULL);
236 }
697a606c
JA
237}
238
906c8d75
JA
239/*
240 * Check if we are above the minimum rate given.
241 */
581e7141 242static int __check_min_rate(struct thread_data *td, struct timeval *now,
bd93e34b 243 enum fio_ddir ddir)
ebac4655 244{
0904200b 245 unsigned long long bytes = 0;
4e991c23 246 unsigned long iops = 0;
ebac4655
JA
247 unsigned long spent;
248 unsigned long rate;
581e7141
JA
249 unsigned int ratemin = 0;
250 unsigned int rate_iops = 0;
251 unsigned int rate_iops_min = 0;
ebac4655 252
ff58fced
JA
253 assert(ddir_rw(ddir));
254
d982d873
JA
255 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
256 return 0;
257
ebac4655
JA
258 /*
259 * allow a 2 second settle period in the beginning
260 */
261 if (mtime_since(&td->start, now) < 2000)
262 return 0;
263
c8eeb9df 264 iops += td->this_io_blocks[ddir];
581e7141
JA
265 bytes += td->this_io_bytes[ddir];
266 ratemin += td->o.ratemin[ddir];
267 rate_iops += td->o.rate_iops[ddir];
268 rate_iops_min += td->o.rate_iops_min[ddir];
0904200b 269
ebac4655
JA
270 /*
271 * if rate blocks is set, sample is running
272 */
581e7141
JA
273 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
274 spent = mtime_since(&td->lastrate[ddir], now);
2dc1bbeb 275 if (spent < td->o.ratecycle)
ebac4655
JA
276 return 0;
277
581e7141 278 if (td->o.rate[ddir]) {
4e991c23
JA
279 /*
280 * check bandwidth specified rate
281 */
581e7141 282 if (bytes < td->rate_bytes[ddir]) {
5ec10eaa 283 log_err("%s: min rate %u not met\n", td->o.name,
581e7141 284 ratemin);
4e991c23
JA
285 return 1;
286 } else {
581e7141
JA
287 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
288 if (rate < ratemin ||
289 bytes < td->rate_bytes[ddir]) {
5ec10eaa 290 log_err("%s: min rate %u not met, got"
b22989b9 291 " %luKB/sec\n", td->o.name,
581e7141 292 ratemin, rate);
4e991c23
JA
293 return 1;
294 }
295 }
413dd459 296 } else {
4e991c23
JA
297 /*
298 * checks iops specified rate
299 */
581e7141 300 if (iops < rate_iops) {
5ec10eaa 301 log_err("%s: min iops rate %u not met\n",
581e7141 302 td->o.name, rate_iops);
413dd459 303 return 1;
4e991c23 304 } else {
581e7141
JA
305 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
306 if (rate < rate_iops_min ||
307 iops < td->rate_blocks[ddir]) {
5ec10eaa
JA
308 log_err("%s: min iops rate %u not met,"
309 " got %lu\n", td->o.name,
581e7141 310 rate_iops_min, rate);
4e991c23 311 }
413dd459 312 }
ebac4655
JA
313 }
314 }
315
581e7141
JA
316 td->rate_bytes[ddir] = bytes;
317 td->rate_blocks[ddir] = iops;
318 memcpy(&td->lastrate[ddir], now, sizeof(*now));
ebac4655
JA
319 return 0;
320}
321
581e7141
JA
322static int check_min_rate(struct thread_data *td, struct timeval *now,
323 unsigned long *bytes_done)
324{
325 int ret = 0;
326
327 if (bytes_done[0])
328 ret |= __check_min_rate(td, now, 0);
329 if (bytes_done[1])
330 ret |= __check_min_rate(td, now, 1);
331
332 return ret;
333}
334
ebac4655
JA
335static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
336{
2dc1bbeb 337 if (!td->o.timeout)
ebac4655 338 return 0;
2dc1bbeb 339 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
ebac4655
JA
340 return 1;
341
342 return 0;
343}
344
906c8d75
JA
345/*
346 * When job exits, we can cancel the in-flight IO if we are using async
347 * io. Attempt to do so.
348 */
ebac4655
JA
349static void cleanup_pending_aio(struct thread_data *td)
350{
01743ee1 351 struct flist_head *entry, *n;
ebac4655
JA
352 struct io_u *io_u;
353 int r;
354
355 /*
356 * get immediately available events, if any
357 */
581e7141 358 r = io_u_queued_complete(td, 0, NULL);
b2fdda43
JA
359 if (r < 0)
360 return;
ebac4655
JA
361
362 /*
363 * now cancel remaining active events
364 */
2866c82d 365 if (td->io_ops->cancel) {
01743ee1
JA
366 flist_for_each_safe(entry, n, &td->io_u_busylist) {
367 io_u = flist_entry(entry, struct io_u, list);
ebac4655 368
0c6e7517
JA
369 /*
370 * if the io_u isn't in flight, then that generally
371 * means someone leaked an io_u. complain but fix
372 * it up, so we don't stall here.
373 */
374 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
375 log_err("fio: non-busy IO on busy list\n");
ebac4655 376 put_io_u(td, io_u);
0c6e7517
JA
377 } else {
378 r = td->io_ops->cancel(td, io_u);
379 if (!r)
380 put_io_u(td, io_u);
381 }
ebac4655
JA
382 }
383 }
384
97601024 385 if (td->cur_depth)
581e7141 386 r = io_u_queued_complete(td, td->cur_depth, NULL);
ebac4655
JA
387}
388
858a3d47
JA
389/*
390 * Helper to handle the final sync of a file. Works just like the normal
391 * io path, just does everything sync.
392 */
393static int fio_io_sync(struct thread_data *td, struct fio_file *f)
394{
395 struct io_u *io_u = __get_io_u(td);
858a3d47
JA
396 int ret;
397
398 if (!io_u)
399 return 1;
400
401 io_u->ddir = DDIR_SYNC;
402 io_u->file = f;
403
404 if (td_io_prep(td, io_u)) {
405 put_io_u(td, io_u);
406 return 1;
407 }
408
755200a3 409requeue:
858a3d47 410 ret = td_io_queue(td, io_u);
36167d82 411 if (ret < 0) {
e1161c32 412 td_verror(td, io_u->error, "td_io_queue");
858a3d47 413 put_io_u(td, io_u);
858a3d47 414 return 1;
36167d82 415 } else if (ret == FIO_Q_QUEUED) {
581e7141 416 if (io_u_queued_complete(td, 1, NULL) < 0)
36167d82 417 return 1;
36167d82
JA
418 } else if (ret == FIO_Q_COMPLETED) {
419 if (io_u->error) {
e1161c32 420 td_verror(td, io_u->error, "td_io_queue");
36167d82
JA
421 return 1;
422 }
858a3d47 423
581e7141 424 if (io_u_sync_complete(td, io_u, NULL) < 0)
b2fdda43 425 return 1;
755200a3
JA
426 } else if (ret == FIO_Q_BUSY) {
427 if (td_io_commit(td))
428 return 1;
429 goto requeue;
858a3d47
JA
430 }
431
432 return 0;
433}
434
687c6a23
JA
435static inline void __update_tv_cache(struct thread_data *td)
436{
437 fio_gettime(&td->tv_cache, NULL);
438}
439
993bf48b
JA
440static inline void update_tv_cache(struct thread_data *td)
441{
442 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
687c6a23 443 __update_tv_cache(td);
993bf48b
JA
444}
445
f2bba182
RR
446static int break_on_this_error(struct thread_data *td, int *retptr)
447{
448 int ret = *retptr;
449
450 if (ret < 0 || td->error) {
451 int err;
452
1ec99eea
JA
453 if (!td->o.continue_on_error)
454 return 1;
f2bba182
RR
455
456 if (ret < 0)
457 err = -ret;
458 else
459 err = td->error;
460
f2bba182
RR
461 if (td_non_fatal_error(err)) {
462 /*
463 * Continue with the I/Os in case of
464 * a non fatal error.
465 */
8a4f8a24 466 update_error_count(td, err);
f2bba182
RR
467 td_clear_error(td);
468 *retptr = 0;
469 return 0;
8a4f8a24
RR
470 } else if (td->o.fill_device && err == ENOSPC) {
471 /*
472 * We expect to hit this error if
473 * fill_device option is set.
474 */
475 td_clear_error(td);
476 td->terminate = 1;
477 return 1;
f2bba182
RR
478 } else {
479 /*
480 * Stop the I/O in case of a fatal
481 * error.
482 */
8a4f8a24 483 update_error_count(td, err);
f2bba182
RR
484 return 1;
485 }
486 }
487
488 return 0;
489}
490
906c8d75 491/*
34403fb1 492 * The main verify engine. Runs over the writes we previously submitted,
906c8d75
JA
493 * reads the blocks back in, and checks the crc/md5 of the data.
494 */
1e97cce9 495static void do_verify(struct thread_data *td)
ebac4655 496{
53cdc686 497 struct fio_file *f;
36167d82 498 struct io_u *io_u;
af52b345
JA
499 int ret, min_events;
500 unsigned int i;
e5b401d4 501
008618a8
JA
502 dprint(FD_VERIFY, "starting loop\n");
503
e5b401d4
JA
504 /*
505 * sync io first and invalidate cache, to make sure we really
506 * read from disk.
507 */
508 for_each_file(td, f, i) {
d6aed795 509 if (!fio_file_open(f))
3d7b485f 510 continue;
b2fdda43
JA
511 if (fio_io_sync(td, f))
512 break;
513 if (file_invalidate_cache(td, f))
514 break;
e5b401d4 515 }
ebac4655 516
b2fdda43
JA
517 if (td->error)
518 return;
519
ebac4655
JA
520 td_set_runstate(td, TD_VERIFYING);
521
36167d82
JA
522 io_u = NULL;
523 while (!td->terminate) {
4950421a 524 int ret2, full;
5451792e 525
993bf48b
JA
526 update_tv_cache(td);
527
528 if (runtime_exceeded(td, &td->tv_cache)) {
687c6a23
JA
529 __update_tv_cache(td);
530 if (runtime_exceeded(td, &td->tv_cache)) {
531 td->terminate = 1;
532 break;
533 }
069c2918 534 }
02bcaa8c 535
dcb4830d
RR
536 io_u = __get_io_u(td);
537 if (!io_u)
538 break;
539
069c2918
JA
540 if (get_next_verify(td, io_u)) {
541 put_io_u(td, io_u);
ebac4655 542 break;
069c2918 543 }
ebac4655 544
069c2918
JA
545 if (td_io_prep(td, io_u)) {
546 put_io_u(td, io_u);
53cdc686 547 break;
069c2918 548 }
d7762cf8 549
e8462bd8
JA
550 if (td->o.verify_async)
551 io_u->end_io = verify_io_u_async;
552 else
553 io_u->end_io = verify_io_u;
53cdc686 554
11786802 555 ret = td_io_queue(td, io_u);
36167d82
JA
556 switch (ret) {
557 case FIO_Q_COMPLETED:
f2bba182 558 if (io_u->error) {
22819ec2 559 ret = -io_u->error;
f2bba182
RR
560 clear_io_u(td, io_u);
561 } else if (io_u->resid) {
36167d82 562 int bytes = io_u->xfer_buflen - io_u->resid;
ebac4655 563
9e9d164e
JA
564 /*
565 * zero read, fail
566 */
567 if (!bytes) {
41d9f0fd 568 td_verror(td, EIO, "full resid");
9e9d164e
JA
569 put_io_u(td, io_u);
570 break;
571 }
8400d9b2 572
36167d82
JA
573 io_u->xfer_buflen = io_u->resid;
574 io_u->xfer_buf += bytes;
8400d9b2
JA
575 io_u->offset += bytes;
576
ff58fced
JA
577 if (ddir_rw(io_u->ddir))
578 td->ts.short_io_u[io_u->ddir]++;
30061b97 579
2b13e716 580 f = io_u->file;
d460eb31 581 if (io_u->offset == f->real_file_size)
8400d9b2
JA
582 goto sync_done;
583
11786802
JA
584 requeue_io_u(td, &io_u);
585 } else {
8400d9b2 586sync_done:
581e7141 587 ret = io_u_sync_complete(td, io_u, NULL);
11786802
JA
588 if (ret < 0)
589 break;
36167d82 590 }
36167d82
JA
591 continue;
592 case FIO_Q_QUEUED:
593 break;
755200a3
JA
594 case FIO_Q_BUSY:
595 requeue_io_u(td, &io_u);
5451792e
JA
596 ret2 = td_io_commit(td);
597 if (ret2 < 0)
598 ret = ret2;
755200a3 599 break;
36167d82
JA
600 default:
601 assert(ret < 0);
e1161c32 602 td_verror(td, -ret, "td_io_queue");
ebac4655
JA
603 break;
604 }
605
f2bba182 606 if (break_on_this_error(td, &ret))
3af6ef39
JA
607 break;
608
ebac4655 609 /*
3af6ef39 610 * if we can queue more, do so. but check if there are
ccc02050
JA
611 * completed io_u's first. Note that we can get BUSY even
612 * without IO queued, if the system is resource starved.
ebac4655 613 */
ccc02050 614 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
4950421a 615 if (full || !td->o.iodepth_batch_complete) {
6a96276c
RR
616 min_events = min(td->o.iodepth_batch_complete,
617 td->cur_depth);
1f84f1d9 618 if (full && !min_events && td->o.iodepth_batch_complete != 0)
838bc709 619 min_events = 1;
e916b390 620
4950421a
JA
621 do {
622 /*
623 * Reap required number of io units, if any,
624 * and do the verification on them through
625 * the callback handler
626 */
581e7141 627 if (io_u_queued_complete(td, min_events, NULL) < 0) {
4950421a
JA
628 ret = -1;
629 break;
630 }
631 } while (full && (td->cur_depth > td->o.iodepth_low));
632 }
633 if (ret < 0)
ebac4655 634 break;
36167d82 635 }
ebac4655 636
c01c0395
JA
637 if (!td->error) {
638 min_events = td->cur_depth;
639
640 if (min_events)
581e7141 641 ret = io_u_queued_complete(td, min_events, NULL);
c01c0395 642 } else
ebac4655
JA
643 cleanup_pending_aio(td);
644
645 td_set_runstate(td, TD_RUNNING);
008618a8
JA
646
647 dprint(FD_VERIFY, "exiting loop\n");
ebac4655
JA
648}
649
32cd46a0 650/*
906c8d75 651 * Main IO worker function. It retrieves io_u's to process and queues
32cd46a0
JA
652 * and reaps them, checking for rate and errors along the way.
653 */
ebac4655
JA
654static void do_io(struct thread_data *td)
655{
af52b345
JA
656 unsigned int i;
657 int ret = 0;
ebac4655 658
b29ee5b3
JA
659 if (in_ramp_time(td))
660 td_set_runstate(td, TD_RAMP);
661 else
662 td_set_runstate(td, TD_RUNNING);
5853e5a8 663
457bf399 664 while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
a917a8b3 665 (!flist_empty(&td->trim_list)) ||
457bf399 666 ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) ) {
97601024 667 struct timeval comp_time;
581e7141 668 unsigned long bytes_done[2] = { 0, 0 };
84585003 669 int min_evts = 0;
ebac4655 670 struct io_u *io_u;
4950421a 671 int ret2, full;
ebac4655
JA
672
673 if (td->terminate)
674 break;
675
993bf48b 676 update_tv_cache(td);
97601024 677
993bf48b 678 if (runtime_exceeded(td, &td->tv_cache)) {
687c6a23
JA
679 __update_tv_cache(td);
680 if (runtime_exceeded(td, &td->tv_cache)) {
681 td->terminate = 1;
682 break;
683 }
97601024 684 }
36167d82 685
dcb4830d
RR
686 io_u = get_io_u(td);
687 if (!io_u)
688 break;
689
b67740d3
JA
690 /*
691 * Add verification end_io handler, if asked to verify
692 * a previously written file.
693 */
715d2b3e
JA
694 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
695 !td_rw(td)) {
e8462bd8
JA
696 if (td->o.verify_async)
697 io_u->end_io = verify_io_u_async;
698 else
699 io_u->end_io = verify_io_u;
d8fab1b6 700 td_set_runstate(td, TD_VERIFYING);
b29ee5b3
JA
701 } else if (in_ramp_time(td))
702 td_set_runstate(td, TD_RAMP);
703 else
d8fab1b6 704 td_set_runstate(td, TD_RUNNING);
b67740d3 705
11786802 706 ret = td_io_queue(td, io_u);
36167d82
JA
707 switch (ret) {
708 case FIO_Q_COMPLETED:
f2bba182 709 if (io_u->error) {
5451792e 710 ret = -io_u->error;
f2bba182
RR
711 clear_io_u(td, io_u);
712 } else if (io_u->resid) {
36167d82 713 int bytes = io_u->xfer_buflen - io_u->resid;
d460eb31 714 struct fio_file *f = io_u->file;
36167d82 715
9e9d164e
JA
716 /*
717 * zero read, fail
718 */
719 if (!bytes) {
41d9f0fd 720 td_verror(td, EIO, "full resid");
9e9d164e
JA
721 put_io_u(td, io_u);
722 break;
723 }
724
cec6b55d 725 io_u->xfer_buflen = io_u->resid;
36167d82 726 io_u->xfer_buf += bytes;
5a7c5680
JA
727 io_u->offset += bytes;
728
ff58fced
JA
729 if (ddir_rw(io_u->ddir))
730 td->ts.short_io_u[io_u->ddir]++;
30061b97 731
d460eb31 732 if (io_u->offset == f->real_file_size)
5a7c5680
JA
733 goto sync_done;
734
11786802
JA
735 requeue_io_u(td, &io_u);
736 } else {
5a7c5680 737sync_done:
581e7141
JA
738 if (__should_check_rate(td, 0) ||
739 __should_check_rate(td, 1))
993bf48b
JA
740 fio_gettime(&comp_time, NULL);
741
581e7141
JA
742 ret = io_u_sync_complete(td, io_u, bytes_done);
743 if (ret < 0)
744 break;
cec6b55d 745 }
36167d82
JA
746 break;
747 case FIO_Q_QUEUED:
7e77dd02
JA
748 /*
749 * if the engine doesn't have a commit hook,
750 * the io_u is really queued. if it does have such
751 * a hook, it has to call io_u_queued() itself.
752 */
753 if (td->io_ops->commit == NULL)
754 io_u_queued(td, io_u);
36167d82 755 break;
755200a3
JA
756 case FIO_Q_BUSY:
757 requeue_io_u(td, &io_u);
5451792e
JA
758 ret2 = td_io_commit(td);
759 if (ret2 < 0)
760 ret = ret2;
755200a3 761 break;
36167d82
JA
762 default:
763 assert(ret < 0);
764 put_io_u(td, io_u);
765 break;
ebac4655
JA
766 }
767
f2bba182 768 if (break_on_this_error(td, &ret))
36167d82
JA
769 break;
770
97601024 771 /*
ccc02050
JA
772 * See if we need to complete some commands. Note that we
773 * can get BUSY even without IO queued, if the system is
774 * resource starved.
97601024 775 */
ccc02050 776 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
4950421a 777 if (full || !td->o.iodepth_batch_complete) {
6a96276c
RR
778 min_evts = min(td->o.iodepth_batch_complete,
779 td->cur_depth);
1f84f1d9 780 if (full && !min_evts && td->o.iodepth_batch_complete != 0)
36167d82 781 min_evts = 1;
4950421a 782
581e7141
JA
783 if (__should_check_rate(td, 0) ||
784 __should_check_rate(td, 1))
993bf48b 785 fio_gettime(&comp_time, NULL);
4950421a
JA
786
787 do {
581e7141
JA
788 ret = io_u_queued_complete(td, min_evts, bytes_done);
789 if (ret < 0)
4950421a
JA
790 break;
791
4950421a 792 } while (full && (td->cur_depth > td->o.iodepth_low));
ebac4655
JA
793 }
794
4950421a
JA
795 if (ret < 0)
796 break;
581e7141 797 if (!(bytes_done[0] + bytes_done[1]))
97601024
JA
798 continue;
799
581e7141
JA
800 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
801 if (check_min_rate(td, &comp_time, bytes_done)) {
721938ae 802 if (exitall_on_terminate)
cc0df00a 803 fio_terminate_threads(td->groupid);
721938ae
JA
804 td_verror(td, EIO, "check_min_rate");
805 break;
806 }
ebac4655
JA
807 }
808
2dc1bbeb 809 if (td->o.thinktime) {
9c1f7434
JA
810 unsigned long long b;
811
812 b = td->io_blocks[0] + td->io_blocks[1];
2dc1bbeb 813 if (!(b % td->o.thinktime_blocks)) {
48097d5c
JA
814 int left;
815
2dc1bbeb 816 if (td->o.thinktime_spin)
6dd6f2cd 817 usec_spin(td->o.thinktime_spin);
48097d5c 818
2dc1bbeb 819 left = td->o.thinktime - td->o.thinktime_spin;
48097d5c
JA
820 if (left)
821 usec_sleep(td, left);
822 }
9c1f7434 823 }
ebac4655
JA
824 }
825
d708ac5e
JA
826 if (td->trim_entries)
827 printf("trim entries %ld\n", td->trim_entries);
a917a8b3 828
aa31f1f1
SL
829 if (td->o.fill_device && td->error == ENOSPC) {
830 td->error = 0;
831 td->terminate = 1;
832 }
4d2413c6 833 if (!td->error) {
3d7c391d
JA
834 struct fio_file *f;
835
c01c0395 836 i = td->cur_depth;
d06093e7 837 if (i) {
581e7141 838 ret = io_u_queued_complete(td, i, NULL);
d06093e7
SL
839 if (td->o.fill_device && td->error == ENOSPC)
840 td->error = 0;
841 }
ebac4655 842
2dc1bbeb 843 if (should_fsync(td) && td->o.end_fsync) {
84585003 844 td_set_runstate(td, TD_FSYNCING);
3d7b485f
JA
845
846 for_each_file(td, f, i) {
d6aed795 847 if (!fio_file_open(f))
3d7b485f 848 continue;
858a3d47 849 fio_io_sync(td, f);
3d7b485f 850 }
84585003 851 }
c01c0395
JA
852 } else
853 cleanup_pending_aio(td);
2ba1c290
JA
854
855 /*
856 * stop job if we failed doing any IO
857 */
858 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
859 td->done = 1;
ebac4655
JA
860}
861
ebac4655
JA
862static void cleanup_io_u(struct thread_data *td)
863{
01743ee1 864 struct flist_head *entry, *n;
ebac4655
JA
865 struct io_u *io_u;
866
01743ee1
JA
867 flist_for_each_safe(entry, n, &td->io_u_freelist) {
868 io_u = flist_entry(entry, struct io_u, list);
ebac4655 869
01743ee1 870 flist_del(&io_u->list);
91aea6ec 871 fio_memfree(io_u, sizeof(*io_u));
ebac4655
JA
872 }
873
2f9ade3c 874 free_io_mem(td);
ebac4655
JA
875}
876
877static int init_io_u(struct thread_data *td)
878{
879 struct io_u *io_u;
a00735e6 880 unsigned int max_bs;
eb7ccf38 881 int cl_align, i, max_units;
ebac4655
JA
882 char *p;
883
1e3c09af 884 max_units = td->o.iodepth;
2dc1bbeb 885 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
5ec10eaa
JA
886 td->orig_buffer_size = (unsigned long long) max_bs
887 * (unsigned long long) max_units;
888
889 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
890 unsigned long bs;
74b025b0 891
5ec10eaa
JA
892 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
893 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
894 }
ebac4655 895
c3990645
JA
896 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
897 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
898 return 1;
899 }
900
2f9ade3c
JA
901 if (allocate_io_mem(td))
902 return 1;
ebac4655 903
1c04bf78
JA
904 if (td->o.odirect || td->o.mem_align ||
905 (td->io_ops->flags & FIO_RAWIO))
d529ee19 906 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
b3f378e9
JA
907 else
908 p = td->orig_buffer;
909
eb7ccf38
JA
910 cl_align = os_cache_line_size();
911
ebac4655 912 for (i = 0; i < max_units; i++) {
eb7ccf38
JA
913 void *ptr;
914
db1cd90b
JA
915 if (td->terminate)
916 return 1;
eb7ccf38 917
91aea6ec
JA
918 ptr = fio_memalign(cl_align, sizeof(*io_u));
919 if (!ptr) {
920 log_err("fio: unable to allocate aligned memory\n");
eb7ccf38
JA
921 break;
922 }
923
924 io_u = ptr;
ebac4655 925 memset(io_u, 0, sizeof(*io_u));
01743ee1 926 INIT_FLIST_HEAD(&io_u->list);
d529ee19 927 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
ebac4655 928
b4c5e1ac
JA
929 if (!(td->io_ops->flags & FIO_NOIO)) {
930 io_u->buf = p + max_bs * i;
d529ee19 931 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
e9459e5a 932
3545a109 933 if (td_write(td))
5973cafb 934 io_u_fill_buffer(td, io_u, max_bs);
3545a109 935 if (td_write(td) && td->o.verify_pattern_bytes) {
cbe8d756
RR
936 /*
937 * Fill the buffer with the pattern if we are
938 * going to be doing writes.
939 */
7d9fb455 940 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
cbe8d756 941 }
b4c5e1ac 942 }
6b9cea23 943
b1ff3403 944 io_u->index = i;
0c6e7517 945 io_u->flags = IO_U_F_FREE;
01743ee1 946 flist_add(&io_u->list, &td->io_u_freelist);
ebac4655
JA
947 }
948
949 return 0;
950}
951
da86774e
JA
952static int switch_ioscheduler(struct thread_data *td)
953{
954 char tmp[256], tmp2[128];
955 FILE *f;
956 int ret;
957
ba0fbe10 958 if (td->io_ops->flags & FIO_DISKLESSIO)
f48b467c
JA
959 return 0;
960
da86774e
JA
961 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
962
963 f = fopen(tmp, "r+");
964 if (!f) {
cbf5c1d7 965 if (errno == ENOENT) {
5ec10eaa
JA
966 log_err("fio: os or kernel doesn't support IO scheduler"
967 " switching\n");
cbf5c1d7
JA
968 return 0;
969 }
970 td_verror(td, errno, "fopen iosched");
da86774e
JA
971 return 1;
972 }
973
974 /*
975 * Set io scheduler.
976 */
2dc1bbeb 977 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
da86774e 978 if (ferror(f) || ret != 1) {
e1161c32 979 td_verror(td, errno, "fwrite");
da86774e
JA
980 fclose(f);
981 return 1;
982 }
983
984 rewind(f);
985
986 /*
987 * Read back and check that the selected scheduler is now the default.
988 */
989 ret = fread(tmp, 1, sizeof(tmp), f);
990 if (ferror(f) || ret < 0) {
e1161c32 991 td_verror(td, errno, "fread");
da86774e
JA
992 fclose(f);
993 return 1;
994 }
995
2dc1bbeb 996 sprintf(tmp2, "[%s]", td->o.ioscheduler);
da86774e 997 if (!strstr(tmp, tmp2)) {
2dc1bbeb 998 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
e1161c32 999 td_verror(td, EINVAL, "iosched_switch");
da86774e
JA
1000 fclose(f);
1001 return 1;
1002 }
1003
1004 fclose(f);
1005 return 0;
1006}
1007
492158cf
JA
1008static int keep_running(struct thread_data *td)
1009{
1010 unsigned long long io_done;
1011
20e354ef
JA
1012 if (td->done)
1013 return 0;
492158cf
JA
1014 if (td->o.time_based)
1015 return 1;
1016 if (td->o.loops) {
1017 td->o.loops--;
1018 return 1;
1019 }
1020
5ec10eaa
JA
1021 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
1022 + td->io_skip_bytes;
492158cf
JA
1023 if (io_done < td->o.size)
1024 return 1;
1025
1026 return 0;
1027}
1028
b29ee5b3 1029static void reset_io_counters(struct thread_data *td)
ebac4655 1030{
f0505a14 1031 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
ebac4655 1032 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
c8eeb9df
JA
1033 td->stat_io_blocks[0] = td->stat_io_blocks[1] = 0;
1034 td->this_io_blocks[0] = td->this_io_blocks[1] = 0;
20dc95c4 1035 td->zone_bytes = 0;
581e7141
JA
1036 td->rate_bytes[0] = td->rate_bytes[1] = 0;
1037 td->rate_blocks[0] = td->rate_blocks[1] = 0;
ebac4655 1038
c1324df1
JA
1039 td->last_was_sync = 0;
1040
2ba1c290
JA
1041 /*
1042 * reset file done count if we are to start over
1043 */
1044 if (td->o.time_based || td->o.loops)
48f5abd3 1045 td->nr_done_files = 0;
b29ee5b3
JA
1046}
1047
1048void reset_all_stats(struct thread_data *td)
1049{
1050 struct timeval tv;
1051 int i;
1052
1053 reset_io_counters(td);
1054
1055 for (i = 0; i < 2; i++) {
1056 td->io_bytes[i] = 0;
1057 td->io_blocks[i] = 0;
1058 td->io_issues[i] = 0;
1059 td->ts.total_io_u[i] = 0;
1060 }
9b836561 1061
b29ee5b3 1062 fio_gettime(&tv, NULL);
cda99fa0
SSP
1063 td->ts.runtime[0] = 0;
1064 td->ts.runtime[1] = 0;
b29ee5b3
JA
1065 memcpy(&td->epoch, &tv, sizeof(tv));
1066 memcpy(&td->start, &tv, sizeof(tv));
1067}
1068
df9c26b1 1069static void clear_io_state(struct thread_data *td)
b29ee5b3
JA
1070{
1071 struct fio_file *f;
1072 unsigned int i;
b29ee5b3
JA
1073
1074 reset_io_counters(td);
281f9b63 1075
24ffd2c2 1076 close_files(td);
df9c26b1 1077 for_each_file(td, f, i)
d6aed795 1078 fio_file_clear_done(f);
f0153948
JA
1079
1080 /*
1081 * Set the same seed to get repeatable runs
1082 */
1083 td_fill_rand_seeds(td);
ebac4655
JA
1084}
1085
398e8c4d
JA
1086static int exec_string(const char *string)
1087{
1088 int ret, newlen = strlen(string) + 1 + 8;
1089 char *str;
1090
1091 str = malloc(newlen);
1092 sprintf(str, "sh -c %s", string);
1093
1094 ret = system(str);
1095 if (ret == -1)
1096 log_err("fio: exec of cmd <%s> failed\n", str);
1097
1098 free(str);
1099 return ret;
1100}
1101
906c8d75
JA
1102/*
1103 * Entry point for the thread based jobs. The process based jobs end up
1104 * here as well, after a little setup.
1105 */
ebac4655
JA
1106static void *thread_main(void *data)
1107{
cda99fa0 1108 unsigned long long elapsed;
ebac4655 1109 struct thread_data *td = data;
e8462bd8 1110 pthread_condattr_t attr;
a978ba68 1111 int clear_state;
ebac4655 1112
47f767c1 1113 if (!td->o.use_thread) {
ebac4655 1114 setsid();
47f767c1
JA
1115 td->pid = getpid();
1116 } else
1117 td->pid = gettid();
ebac4655 1118
5921e80c 1119 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
ee56ad50 1120
01743ee1
JA
1121 INIT_FLIST_HEAD(&td->io_u_freelist);
1122 INIT_FLIST_HEAD(&td->io_u_busylist);
1123 INIT_FLIST_HEAD(&td->io_u_requeues);
1124 INIT_FLIST_HEAD(&td->io_log_list);
1125 INIT_FLIST_HEAD(&td->io_hist_list);
e8462bd8 1126 INIT_FLIST_HEAD(&td->verify_list);
0d29de83 1127 INIT_FLIST_HEAD(&td->trim_list);
e8462bd8 1128 pthread_mutex_init(&td->io_u_lock, NULL);
bb5d7d0b 1129 td->io_hist_tree = RB_ROOT;
aea47d44 1130
e8462bd8
JA
1131 pthread_condattr_init(&attr);
1132 pthread_cond_init(&td->verify_cond, &attr);
1133 pthread_cond_init(&td->free_cond, &attr);
1134
db1cd90b 1135 td_set_runstate(td, TD_INITIALIZED);
29adda3c 1136 dprint(FD_MUTEX, "up startup_mutex\n");
cdd18ad8 1137 fio_mutex_up(startup_mutex);
29adda3c 1138 dprint(FD_MUTEX, "wait on td->mutex\n");
cdd18ad8 1139 fio_mutex_down(td->mutex);
29adda3c 1140 dprint(FD_MUTEX, "done waiting on td->mutex\n");
db1cd90b
JA
1141
1142 /*
cdd18ad8 1143 * the ->mutex mutex is now no longer used, close it to avoid
db1cd90b
JA
1144 * eating a file descriptor
1145 */
cdd18ad8 1146 fio_mutex_remove(td->mutex);
db1cd90b 1147
dfc6e751
SH
1148 /*
1149 * A new gid requires privilege, so we need to do this before setting
1150 * the uid.
1151 */
e0b0d892
JA
1152 if (td->o.gid != -1U && setgid(td->o.gid)) {
1153 td_verror(td, errno, "setgid");
1154 goto err;
1155 }
dfc6e751
SH
1156 if (td->o.uid != -1U && setuid(td->o.uid)) {
1157 td_verror(td, errno, "setuid");
1158 goto err;
1159 }
e0b0d892 1160
be18a94f
JA
1161 /*
1162 * If we have a gettimeofday() thread, make sure we exclude that
1163 * thread from this job
1164 */
1165 if (td->o.gtod_cpu)
1166 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1167
1168 /*
1169 * Set affinity first, in case it has an impact on the memory
1170 * allocations.
1171 */
1172 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1173 td_verror(td, errno, "cpu_set_affinity");
1174 goto err;
1175 }
1176
d84f8d49
JA
1177 /*
1178 * May alter parameters that init_io_u() will use, so we need to
1179 * do this first.
1180 */
1181 if (init_iolog(td))
1182 goto err;
1183
ebac4655 1184 if (init_io_u(td))
db1cd90b 1185 goto err;
ebac4655 1186
e8462bd8
JA
1187 if (td->o.verify_async && verify_async_init(td))
1188 goto err;
1189
ac684785 1190 if (td->ioprio_set) {
ebac4655 1191 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
e1161c32 1192 td_verror(td, errno, "ioprio_set");
db1cd90b 1193 goto err;
ebac4655
JA
1194 }
1195 }
1196
6adb38a1 1197 if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
a696fa2a
JA
1198 goto err;
1199
2dc1bbeb 1200 if (nice(td->o.nice) == -1) {
e1161c32 1201 td_verror(td, errno, "nice");
db1cd90b 1202 goto err;
b6f4d880
JA
1203 }
1204
2dc1bbeb 1205 if (td->o.ioscheduler && switch_ioscheduler(td))
db1cd90b 1206 goto err;
37f56873 1207
2dc1bbeb 1208 if (!td->o.create_serialize && setup_files(td))
ebac4655
JA
1209 goto err;
1210
7d6c5283
JA
1211 if (td_io_init(td))
1212 goto err;
1213
68727076
JA
1214 if (init_random_map(td))
1215 goto err;
1216
2dc1bbeb 1217 if (td->o.exec_prerun) {
398e8c4d 1218 if (exec_string(td->o.exec_prerun))
69cfd7e0
JA
1219 goto err;
1220 }
4e0ba8af 1221
afad68f7
ZY
1222 if (td->o.pre_read) {
1223 if (pre_read_files(td) < 0)
1224 goto err;
1225 }
1226
69008999 1227 fio_gettime(&td->epoch, NULL);
c8aaba19 1228 getrusage(RUSAGE_SELF, &td->ru_start);
69008999 1229
a978ba68 1230 clear_state = 0;
492158cf 1231 while (keep_running(td)) {
02bcaa8c 1232 fio_gettime(&td->start, NULL);
c8eeb9df
JA
1233 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1234 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
993bf48b 1235 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
ebac4655 1236
cf2dd8d6 1237 if (td->o.ratemin[0] || td->o.ratemin[1])
c8eeb9df 1238 memcpy(&td->lastrate, &td->bw_sample_time,
5ec10eaa 1239 sizeof(td->lastrate));
ebac4655 1240
df9c26b1
JA
1241 if (clear_state)
1242 clear_io_state(td);
a978ba68 1243
ebac4655
JA
1244 prune_io_piece_log(td);
1245
ba0fbe10 1246 do_io(td);
ebac4655 1247
a978ba68
JA
1248 clear_state = 1;
1249
38d77cae 1250 if (td_read(td) && td->io_bytes[DDIR_READ]) {
0a4957c6 1251 elapsed = utime_since_now(&td->start);
cda99fa0 1252 td->ts.runtime[DDIR_READ] += elapsed;
38d77cae
JA
1253 }
1254 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
0a4957c6 1255 elapsed = utime_since_now(&td->start);
cda99fa0 1256 td->ts.runtime[DDIR_WRITE] += elapsed;
38d77cae 1257 }
5ec10eaa 1258
ebac4655
JA
1259 if (td->error || td->terminate)
1260 break;
1261
e84c73a8
SL
1262 if (!td->o.do_verify ||
1263 td->o.verify == VERIFY_NONE ||
b67740d3 1264 (td->io_ops->flags & FIO_UNIDIR))
ebac4655
JA
1265 continue;
1266
df9c26b1 1267 clear_io_state(td);
a978ba68 1268
02bcaa8c 1269 fio_gettime(&td->start, NULL);
ebac4655
JA
1270
1271 do_verify(td);
1272
cda99fa0 1273 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
ebac4655
JA
1274
1275 if (td->error || td->terminate)
1276 break;
1277 }
1278
36dff966 1279 update_rusage_stat(td);
cda99fa0
SSP
1280 td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000;
1281 td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000;
756867bd
JA
1282 td->ts.total_run_time = mtime_since_now(&td->epoch);
1283 td->ts.io_bytes[0] = td->io_bytes[0];
1284 td->ts.io_bytes[1] = td->io_bytes[1];
1285
d2bbb8d9 1286 fio_mutex_down(writeout_mutex);
7b9f733a 1287 if (td->bw_log) {
f484470b 1288 if (td->o.bw_log_file) {
7b9f733a 1289 finish_log_named(td, td->bw_log,
f484470b
JA
1290 td->o.bw_log_file, "bw");
1291 } else
7b9f733a 1292 finish_log(td, td->bw_log, "bw");
e3cedca7 1293 }
7b9f733a 1294 if (td->lat_log) {
02af0988 1295 if (td->o.lat_log_file) {
7b9f733a 1296 finish_log_named(td, td->lat_log,
02af0988
JA
1297 td->o.lat_log_file, "lat");
1298 } else
7b9f733a 1299 finish_log(td, td->lat_log, "lat");
02af0988 1300 }
7b9f733a 1301 if (td->slat_log) {
f484470b 1302 if (td->o.lat_log_file) {
7b9f733a 1303 finish_log_named(td, td->slat_log,
1ca95738 1304 td->o.lat_log_file, "slat");
f484470b 1305 } else
7b9f733a 1306 finish_log(td, td->slat_log, "slat");
e3cedca7 1307 }
7b9f733a 1308 if (td->clat_log) {
f484470b 1309 if (td->o.lat_log_file) {
7b9f733a 1310 finish_log_named(td, td->clat_log,
f484470b
JA
1311 td->o.lat_log_file, "clat");
1312 } else
7b9f733a 1313 finish_log(td, td->clat_log, "clat");
e3cedca7 1314 }
c8eeb9df
JA
1315 if (td->iops_log) {
1316 if (td->o.iops_log_file) {
1317 finish_log_named(td, td->iops_log,
1318 td->o.iops_log_file, "iops");
1319 } else
1320 finish_log(td, td->iops_log, "iops");
1321 }
1322
d2bbb8d9 1323 fio_mutex_up(writeout_mutex);
398e8c4d
JA
1324 if (td->o.exec_postrun)
1325 exec_string(td->o.exec_postrun);
ebac4655
JA
1326
1327 if (exitall_on_terminate)
cc0df00a 1328 fio_terminate_threads(td->groupid);
ebac4655
JA
1329
1330err:
5bf13a5a 1331 if (td->error)
4ceb30d4 1332 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
5ec10eaa 1333 td->verror);
b4707ed7
JA
1334
1335 if (td->o.verify_async)
1336 verify_async_exit(td);
1337
24ffd2c2 1338 close_and_free_files(td);
2866c82d 1339 close_ioengine(td);
ebac4655 1340 cleanup_io_u(td);
6adb38a1 1341 cgroup_shutdown(td, &cgroup_mnt);
f29b25a3 1342
d2ce18b5
JA
1343 if (td->o.cpumask_set) {
1344 int ret = fio_cpuset_exit(&td->o.cpumask);
1345
1346 td_verror(td, ret, "fio_cpuset_exit");
1347 }
4e78e405 1348
f29b25a3
JA
1349 /*
1350 * do this very late, it will log file closing as well
1351 */
1352 if (td->o.write_iolog_file)
1353 write_iolog_close(td);
1354
d23bb327 1355 options_mem_free(td);
ebac4655 1356 td_set_runstate(td, TD_EXITED);
43d76807 1357 return (void *) (unsigned long) td->error;
ebac4655
JA
1358}
1359
906c8d75
JA
1360/*
1361 * We cannot pass the td data into a forked process, so attach the td and
1362 * pass it to the thread worker.
1363 */
a6418147 1364static int fork_main(int shmid, int offset)
ebac4655
JA
1365{
1366 struct thread_data *td;
a6418147 1367 void *data, *ret;
ebac4655 1368
65258961 1369#ifndef __hpux
ebac4655
JA
1370 data = shmat(shmid, NULL, 0);
1371 if (data == (void *) -1) {
a6418147
JA
1372 int __err = errno;
1373
ebac4655 1374 perror("shmat");
a6418147 1375 return __err;
ebac4655 1376 }
65258961
JA
1377#else
1378 /*
1379 * HP-UX inherits shm mappings?
1380 */
1381 data = threads;
1382#endif
ebac4655
JA
1383
1384 td = data + offset * sizeof(struct thread_data);
a6418147 1385 ret = thread_main(td);
ebac4655 1386 shmdt(data);
43d76807 1387 return (int) (unsigned long) ret;
ebac4655
JA
1388}
1389
906c8d75
JA
1390/*
1391 * Run over the job map and reap the threads that have exited, if any.
1392 */
ebac4655
JA
1393static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1394{
34572e28 1395 struct thread_data *td;
1f809d15 1396 int i, cputhreads, realthreads, pending, status, ret;
ebac4655
JA
1397
1398 /*
1399 * reap exited threads (TD_EXITED -> TD_REAPED)
1400 */
1f809d15 1401 realthreads = pending = cputhreads = 0;
34572e28 1402 for_each_td(td, i) {
3707f45b 1403 int flags = 0;
a2f77c9f 1404
84585003
JA
1405 /*
1406 * ->io_ops is NULL for a thread that has closed its
1407 * io engine
1408 */
ba0fbe10 1409 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
b990b5c0 1410 cputhreads++;
1f809d15
JA
1411 else
1412 realthreads++;
b990b5c0 1413
a1dedc1f
AC
1414 if (!td->pid) {
1415 pending++;
1416 continue;
1417 }
1418 if (td->runstate == TD_REAPED)
a2f77c9f 1419 continue;
2dc1bbeb 1420 if (td->o.use_thread) {
3707f45b
JA
1421 if (td->runstate == TD_EXITED) {
1422 td_set_runstate(td, TD_REAPED);
1423 goto reaped;
1424 }
1425 continue;
1426 }
a2f77c9f
JA
1427
1428 flags = WNOHANG;
1429 if (td->runstate == TD_EXITED)
1430 flags = 0;
1431
1432 /*
1433 * check if someone quit or got killed in an unusual way
1434 */
1435 ret = waitpid(td->pid, &status, flags);
3707f45b 1436 if (ret < 0) {
a2f77c9f 1437 if (errno == ECHILD) {
5921e80c
JA
1438 log_err("fio: pid=%d disappeared %d\n",
1439 (int) td->pid, td->runstate);
a2f77c9f
JA
1440 td_set_runstate(td, TD_REAPED);
1441 goto reaped;
1442 }
1443 perror("waitpid");
1444 } else if (ret == td->pid) {
1445 if (WIFSIGNALED(status)) {
fab6aa71
JA
1446 int sig = WTERMSIG(status);
1447
03e20d68 1448 if (sig != SIGTERM)
5ec10eaa 1449 log_err("fio: pid=%d, got signal=%d\n",
5921e80c 1450 (int) td->pid, sig);
fab6aa71
JA
1451 td_set_runstate(td, TD_REAPED);
1452 goto reaped;
1453 }
a2f77c9f
JA
1454 if (WIFEXITED(status)) {
1455 if (WEXITSTATUS(status) && !td->error)
1456 td->error = WEXITSTATUS(status);
a2f77c9f 1457
a2f77c9f
JA
1458 td_set_runstate(td, TD_REAPED);
1459 goto reaped;
a6418147
JA
1460 }
1461 }
ebac4655 1462
a2f77c9f
JA
1463 /*
1464 * thread is not dead, continue
1465 */
e48676ba 1466 pending++;
a2f77c9f 1467 continue;
fab6aa71 1468reaped:
ebac4655 1469 (*nr_running)--;
581e7141
JA
1470 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1471 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
6f88bcb0
JA
1472 if (!td->pid)
1473 pending--;
a2f77c9f
JA
1474
1475 if (td->error)
1476 exit_value++;
d3eeeabc
JA
1477
1478 done_secs += mtime_since_now(&td->epoch) / 1000;
ebac4655 1479 }
b990b5c0 1480
1f809d15 1481 if (*nr_running == cputhreads && !pending && realthreads)
cc0df00a 1482 fio_terminate_threads(TERMINATE_ALL);
ebac4655
JA
1483}
1484
be4ecfdf
JA
1485static void *gtod_thread_main(void *data)
1486{
1487 fio_mutex_up(startup_mutex);
1488
1489 /*
1490 * As long as we have jobs around, update the clock. It would be nice
1491 * to have some way of NOT hammering that CPU with gettimeofday(),
1492 * but I'm not sure what to use outside of a simple CPU nop to relax
1493 * it - we don't want to lose precision.
1494 */
1495 while (threads) {
1496 fio_gtod_update();
1497 nop;
1498 }
1499
1500 return NULL;
1501}
1502
1503static int fio_start_gtod_thread(void)
1504{
304a47c7 1505 pthread_attr_t attr;
f42c153d
JA
1506 int ret;
1507
304a47c7
VA
1508 pthread_attr_init(&attr);
1509 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1510 ret = pthread_create(&gtod_thread, &attr, gtod_thread_main, NULL);
1511 pthread_attr_destroy(&attr);
f42c153d
JA
1512 if (ret) {
1513 log_err("Can't create gtod thread: %s\n", strerror(ret));
be4ecfdf
JA
1514 return 1;
1515 }
f42c153d
JA
1516
1517 ret = pthread_detach(gtod_thread);
1518 if (ret) {
1519 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
be4ecfdf
JA
1520 return 1;
1521 }
1522
29adda3c 1523 dprint(FD_MUTEX, "wait on startup_mutex\n");
be4ecfdf 1524 fio_mutex_down(startup_mutex);
29adda3c 1525 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
be4ecfdf
JA
1526 return 0;
1527}
1528
906c8d75
JA
1529/*
1530 * Main function for kicking off and reaping jobs, as needed.
1531 */
ebac4655
JA
1532static void run_threads(void)
1533{
ebac4655
JA
1534 struct thread_data *td;
1535 unsigned long spent;
1536 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2 1537
2f9ade3c
JA
1538 if (fio_pin_memory())
1539 return;
ebac4655 1540
be4ecfdf
JA
1541 if (fio_gtod_offload && fio_start_gtod_thread())
1542 return;
1543
cc0df00a
JA
1544 set_sig_handlers();
1545
c6ae0a5b 1546 if (!terse_output) {
4ceb30d4 1547 log_info("Starting ");
9cedf167 1548 if (nr_thread)
4ceb30d4 1549 log_info("%d thread%s", nr_thread,
5ec10eaa 1550 nr_thread > 1 ? "s" : "");
9cedf167
JA
1551 if (nr_process) {
1552 if (nr_thread)
1553 printf(" and ");
4ceb30d4 1554 log_info("%d process%s", nr_process,
5ec10eaa 1555 nr_process > 1 ? "es" : "");
9cedf167 1556 }
4ceb30d4 1557 log_info("\n");
c6ae0a5b
JA
1558 fflush(stdout);
1559 }
c04f7ec3 1560
ebac4655
JA
1561 todo = thread_number;
1562 nr_running = 0;
1563 nr_started = 0;
1564 m_rate = t_rate = 0;
1565
34572e28 1566 for_each_td(td, i) {
263e529f 1567 print_status_init(td->thread_number - 1);
ebac4655 1568
067430bb 1569 if (!td->o.create_serialize)
ebac4655
JA
1570 continue;
1571
1572 /*
1573 * do file setup here so it happens sequentially,
1574 * we don't want X number of threads getting their
1575 * client data interspersed on disk
1576 */
53cdc686 1577 if (setup_files(td)) {
5bf13a5a
JA
1578 exit_value++;
1579 if (td->error)
5921e80c
JA
1580 log_err("fio: pid=%d, err=%d/%s\n",
1581 (int) td->pid, td->error, td->verror);
ebac4655
JA
1582 td_set_runstate(td, TD_REAPED);
1583 todo--;
c69e8875
JA
1584 } else {
1585 struct fio_file *f;
2b13e716 1586 unsigned int j;
c69e8875
JA
1587
1588 /*
1589 * for sharing to work, each job must always open
1590 * its own files. so close them, if we opened them
1591 * for creation
1592 */
2b13e716 1593 for_each_file(td, f, j) {
22a57ba8
JA
1594 if (fio_file_open(f))
1595 td_io_close_file(td, f);
22a57ba8 1596 }
5ec10eaa 1597 }
ebac4655
JA
1598 }
1599
a2f77c9f
JA
1600 set_genesis_time();
1601
ebac4655 1602 while (todo) {
fca70358 1603 struct thread_data *map[REAL_MAX_JOBS];
75154845
JA
1604 struct timeval this_start;
1605 int this_jobs = 0, left;
1606
ebac4655
JA
1607 /*
1608 * create threads (TD_NOT_CREATED -> TD_CREATED)
1609 */
34572e28 1610 for_each_td(td, i) {
ebac4655
JA
1611 if (td->runstate != TD_NOT_CREATED)
1612 continue;
1613
1614 /*
1615 * never got a chance to start, killed by other
1616 * thread for some reason
1617 */
1618 if (td->terminate) {
1619 todo--;
1620 continue;
1621 }
1622
2dc1bbeb 1623 if (td->o.start_delay) {
263e529f 1624 spent = mtime_since_genesis();
ebac4655 1625
2dc1bbeb 1626 if (td->o.start_delay * 1000 > spent)
ebac4655
JA
1627 continue;
1628 }
1629
6f88bcb0
JA
1630 if (td->o.stonewall && (nr_started || nr_running)) {
1631 dprint(FD_PROCESS, "%s: stonewall wait\n",
1632 td->o.name);
ebac4655 1633 break;
6f88bcb0 1634 }
ebac4655 1635
067430bb
JA
1636 init_disk_util(td);
1637
75154845
JA
1638 /*
1639 * Set state to created. Thread will transition
1640 * to TD_INITIALIZED when it's done setting up.
1641 */
ebac4655 1642 td_set_runstate(td, TD_CREATED);
75154845 1643 map[this_jobs++] = td;
ebac4655
JA
1644 nr_started++;
1645
2dc1bbeb 1646 if (td->o.use_thread) {
f42c153d
JA
1647 int ret;
1648
ee56ad50 1649 dprint(FD_PROCESS, "will pthread_create\n");
f42c153d
JA
1650 ret = pthread_create(&td->thread, NULL,
1651 thread_main, td);
1652 if (ret) {
1653 log_err("pthread_create: %s\n",
1654 strerror(ret));
ebac4655 1655 nr_started--;
c8bb6faf 1656 break;
ebac4655 1657 }
f42c153d
JA
1658 ret = pthread_detach(td->thread);
1659 if (ret)
1660 log_err("pthread_detach: %s",
1661 strerror(ret));
ebac4655 1662 } else {
5e1d306e 1663 pid_t pid;
ee56ad50 1664 dprint(FD_PROCESS, "will fork\n");
5e1d306e
JA
1665 pid = fork();
1666 if (!pid) {
a6418147
JA
1667 int ret = fork_main(shm_id, i);
1668
5e1d306e
JA
1669 _exit(ret);
1670 } else if (i == fio_debug_jobno)
1671 *fio_debug_jobp = pid;
ebac4655 1672 }
29adda3c 1673 dprint(FD_MUTEX, "wait on startup_mutex\n");
656b1393
JA
1674 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1675 log_err("fio: job startup hung? exiting.\n");
cc0df00a 1676 fio_terminate_threads(TERMINATE_ALL);
656b1393
JA
1677 fio_abort = 1;
1678 nr_started--;
1679 break;
1680 }
29adda3c 1681 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
ebac4655
JA
1682 }
1683
1684 /*
75154845
JA
1685 * Wait for the started threads to transition to
1686 * TD_INITIALIZED.
ebac4655 1687 */
02bcaa8c 1688 fio_gettime(&this_start, NULL);
75154845 1689 left = this_jobs;
6ce15a32 1690 while (left && !fio_abort) {
75154845
JA
1691 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1692 break;
1693
1694 usleep(100000);
1695
1696 for (i = 0; i < this_jobs; i++) {
1697 td = map[i];
1698 if (!td)
1699 continue;
b6f4d880 1700 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1701 map[i] = NULL;
1702 left--;
b6f4d880
JA
1703 } else if (td->runstate >= TD_EXITED) {
1704 map[i] = NULL;
1705 left--;
1706 todo--;
1707 nr_running++; /* work-around... */
75154845
JA
1708 }
1709 }
1710 }
1711
1712 if (left) {
3b70d7e5 1713 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
1714 for (i = 0; i < this_jobs; i++) {
1715 td = map[i];
1716 if (!td)
1717 continue;
1718 kill(td->pid, SIGTERM);
1719 }
1720 break;
1721 }
1722
1723 /*
b6f4d880 1724 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 1725 */
34572e28 1726 for_each_td(td, i) {
75154845 1727 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1728 continue;
1729
b29ee5b3
JA
1730 if (in_ramp_time(td))
1731 td_set_runstate(td, TD_RAMP);
1732 else
1733 td_set_runstate(td, TD_RUNNING);
ebac4655
JA
1734 nr_running++;
1735 nr_started--;
581e7141
JA
1736 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1737 t_rate += td->o.rate[0] + td->o.rate[1];
75154845 1738 todo--;
cdd18ad8 1739 fio_mutex_up(td->mutex);
ebac4655
JA
1740 }
1741
1742 reap_threads(&nr_running, &t_rate, &m_rate);
1743
602d6ff9
JA
1744 if (todo) {
1745 if (is_backend)
1746 fio_server_idle_loop();
1747 else
1748 usleep(100000);
1749 }
ebac4655
JA
1750 }
1751
1752 while (nr_running) {
1753 reap_threads(&nr_running, &t_rate, &m_rate);
cc0df00a
JA
1754
1755 if (is_backend)
1756 fio_server_idle_loop();
70e0c316
JA
1757 else
1758 usleep(10000);
ebac4655
JA
1759 }
1760
1761 update_io_ticks();
2f9ade3c 1762 fio_unpin_memory();
ebac4655
JA
1763}
1764
50d16976 1765int exec_run(void)
ebac4655 1766{
a37f69b7 1767 if (nr_clients)
37db14fe 1768 return fio_handle_clients();
07b3232d
JA
1769 if (exec_profile && load_profile(exec_profile))
1770 return 1;
1771
74929ac2
JA
1772 if (!thread_number)
1773 return 0;
1774
bb3884d8
JA
1775 if (write_bw_log) {
1776 setup_log(&agg_io_log[DDIR_READ]);
1777 setup_log(&agg_io_log[DDIR_WRITE]);
1778 }
1779
cdd18ad8 1780 startup_mutex = fio_mutex_init(0);
03e20d68
BC
1781 if (startup_mutex == NULL)
1782 return 1;
d2bbb8d9 1783 writeout_mutex = fio_mutex_init(1);
03e20d68
BC
1784 if (writeout_mutex == NULL)
1785 return 1;
07739b57 1786
a2f77c9f 1787 set_genesis_time();
7985701b 1788 create_disk_util_thread();
ebac4655 1789
dae5341c
JA
1790 cgroup_list = smalloc(sizeof(*cgroup_list));
1791 INIT_FLIST_HEAD(cgroup_list);
1792
ebac4655 1793 run_threads();
6ce15a32 1794
bb3884d8 1795 if (!fio_abort) {
6ce15a32 1796 show_run_stats();
bb3884d8 1797 if (write_bw_log) {
5ec10eaa
JA
1798 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1799 __finish_log(agg_io_log[DDIR_WRITE],
1800 "agg-write_bw.log");
bb3884d8
JA
1801 }
1802 }
ebac4655 1803
dae5341c
JA
1804 cgroup_kill(cgroup_list);
1805 sfree(cgroup_list);
61eb313e 1806 sfree(cgroup_mnt);
39f22027 1807
cdd18ad8 1808 fio_mutex_remove(startup_mutex);
d2bbb8d9 1809 fio_mutex_remove(writeout_mutex);
437c9b71 1810 return exit_value;
ebac4655 1811}
50d16976
JA
1812
1813void reset_fio_state(void)
1814{
1815 groupid = 0;
1816 thread_number = 0;
1817 nr_process = 0;
1818 nr_thread = 0;
1819 done_secs = 0;
1820}
1821
0ad5edcc
JA
1822static int endian_check(void)
1823{
1824 union {
1825 uint8_t c[8];
1826 uint64_t v;
1827 } u;
1828 int le = 0, be = 0;
1829
1830 u.v = 0x12;
1831 if (u.c[7] == 0x12)
1832 be = 1;
1833 else if (u.c[0] == 0x12)
1834 le = 1;
1835
1836#if defined(FIO_LITTLE_ENDIAN)
1837 if (be)
1838 return 1;
1839#elif defined(FIO_BIG_ENDIAN)
1840 if (le)
1841 return 1;
1842#else
1843 return 1;
1844#endif
1845
1846 if (!le && !be)
1847 return 1;
1848
1849 return 0;
1850}
1851
50d16976
JA
1852int main(int argc, char *argv[], char *envp[])
1853{
1854 long ps;
1855
0ad5edcc
JA
1856 if (endian_check()) {
1857 log_err("fio: endianness settings appear wrong.\n");
1858 log_err("fio: please report this to fio@vger.kernel.org\n");
1859 return 1;
1860 }
1861
50d16976
JA
1862 arch_init(envp);
1863
1864 sinit();
1865
1866 /*
1867 * We need locale for number printing, if it isn't set then just
1868 * go with the US format.
1869 */
1870 if (!getenv("LC_NUMERIC"))
1871 setlocale(LC_NUMERIC, "en_US");
1872
1873 ps = sysconf(_SC_PAGESIZE);
1874 if (ps < 0) {
1875 log_err("Failed to get page size\n");
1876 return 1;
1877 }
1878
1879 page_size = ps;
1880 page_mask = ps - 1;
1881
1882 fio_keywords_init();
1883
1884 if (parse_options(argc, argv))
1885 return 1;
1886
1887 return exec_run();
1888}