Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * fio - the flexible io tester |
| 3 | * |
| 4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> |
Jens Axboe | aae22ca | 2006-09-05 10:46:22 +0200 | [diff] [blame] | 5 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 6 | * |
Jens Axboe | 8e9fe63 | 2006-10-24 15:11:09 +0200 | [diff] [blame] | 7 | * The license below covers all files distributed with fio unless otherwise |
| 8 | * noted in the file itself. |
| 9 | * |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
Jens Axboe | 8e9fe63 | 2006-10-24 15:11:09 +0200 | [diff] [blame] | 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 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 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <string.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 27 | #include <signal.h> |
| 28 | #include <time.h> |
Jens Axboe | dbe1125 | 2007-02-11 00:19:51 +0100 | [diff] [blame] | 29 | #include <locale.h> |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 30 | #include <assert.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <sys/ipc.h> |
| 34 | #include <sys/shm.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 35 | #include <sys/mman.h> |
| 36 | |
| 37 | #include "fio.h" |
Jens Axboe | 210f43b | 2007-04-17 19:52:18 +0200 | [diff] [blame] | 38 | #include "hash.h" |
Jens Axboe | 2e5cdb1 | 2008-03-01 18:52:49 +0100 | [diff] [blame] | 39 | #include "smalloc.h" |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 40 | |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 41 | unsigned long page_mask; |
| 42 | unsigned long page_size; |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 43 | #define ALIGN(buf) \ |
| 44 | (char *) (((unsigned long) (buf) + page_mask) & ~page_mask) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 45 | |
| 46 | int groupid = 0; |
| 47 | int thread_number = 0; |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 48 | int nr_process = 0; |
| 49 | int nr_thread = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 50 | int shm_id = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 51 | int temp_stall_ts; |
Jens Axboe | d3eeeab | 2008-04-06 12:19:05 +0200 | [diff] [blame] | 52 | unsigned long done_secs = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 53 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 54 | static struct fio_mutex *startup_mutex; |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 55 | static struct fio_mutex *writeout_mutex; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 56 | static volatile int fio_abort; |
Jens Axboe | 437c9b7 | 2007-02-13 18:20:37 +0100 | [diff] [blame] | 57 | static int exit_value; |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 58 | static struct itimerval itimer; |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 59 | static pthread_t gtod_thread; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 60 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 61 | struct io_log *agg_io_log[2]; |
| 62 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 63 | #define TERMINATE_ALL (-1) |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 64 | #define JOB_START_TIMEOUT (5 * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 65 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 66 | void td_set_runstate(struct thread_data *td, int runstate) |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 67 | { |
Jens Axboe | d8fab1b | 2008-03-06 10:25:17 +0100 | [diff] [blame] | 68 | if (td->runstate == runstate) |
| 69 | return; |
| 70 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 71 | dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid, |
| 72 | td->runstate, runstate); |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 73 | td->runstate = runstate; |
| 74 | } |
| 75 | |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 76 | static void terminate_threads(int group_id) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 77 | { |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 78 | struct thread_data *td; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 79 | int i; |
| 80 | |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 81 | dprint(FD_PROCESS, "terminate group_id=%d\n", group_id); |
| 82 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 83 | for_each_td(td, i) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 84 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 85 | dprint(FD_PROCESS, "setting terminate on %s/%d\n", |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 86 | td->o.name, (int) td->pid); |
Jens Axboe | ad830ed | 2008-02-18 21:11:24 +0100 | [diff] [blame] | 87 | td->terminate = 1; |
| 88 | td->o.start_delay = 0; |
| 89 | |
Jens Axboe | 7a93ab1 | 2007-04-18 12:25:41 +0200 | [diff] [blame] | 90 | /* |
| 91 | * if the thread is running, just let it exit |
| 92 | */ |
| 93 | if (td->runstate < TD_RUNNING) |
Jens Axboe | c1302d4 | 2007-03-05 20:18:31 +0100 | [diff] [blame] | 94 | kill(td->pid, SIGQUIT); |
Jens Axboe | f63f7f3 | 2008-03-01 15:25:24 +0100 | [diff] [blame] | 95 | else { |
| 96 | struct ioengine_ops *ops = td->io_ops; |
| 97 | |
| 98 | if (ops && (ops->flags & FIO_SIGQUIT)) |
| 99 | kill(td->pid, SIGQUIT); |
| 100 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 105 | static void status_timer_arm(void) |
| 106 | { |
| 107 | itimer.it_value.tv_sec = 0; |
| 108 | itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000; |
| 109 | setitimer(ITIMER_REAL, &itimer, NULL); |
| 110 | } |
| 111 | |
Jens Axboe | 02444ad | 2008-10-07 11:33:00 +0200 | [diff] [blame] | 112 | static void sig_alrm(int fio_unused sig) |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 113 | { |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 114 | if (threads) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 115 | update_io_ticks(); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 116 | print_thread_status(); |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 117 | status_timer_arm(); |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Jens Axboe | 5f8f972 | 2008-11-18 19:10:21 +0100 | [diff] [blame] | 121 | /* |
| 122 | * Happens on thread runs with ctrl-c, ignore our own SIGQUIT |
| 123 | */ |
| 124 | static void sig_quit(int sig) |
| 125 | { |
| 126 | } |
| 127 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 128 | static void sig_int(int sig) |
| 129 | { |
| 130 | if (threads) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 131 | printf("\nfio: terminating on signal %d\n", sig); |
| 132 | fflush(stdout); |
| 133 | terminate_threads(TERMINATE_ALL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Jens Axboe | 02444ad | 2008-10-07 11:33:00 +0200 | [diff] [blame] | 137 | static void sig_ill(int fio_unused sig) |
Jens Axboe | de79c91 | 2008-08-04 21:43:55 +0200 | [diff] [blame] | 138 | { |
| 139 | if (!threads) |
| 140 | return; |
| 141 | |
| 142 | log_err("fio: illegal instruction. your cpu does not support " |
| 143 | "the sse4.2 instruction for crc32c\n"); |
| 144 | terminate_threads(TERMINATE_ALL); |
| 145 | exit(4); |
| 146 | } |
| 147 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 148 | static void set_sig_handlers(void) |
| 149 | { |
| 150 | struct sigaction act; |
| 151 | |
| 152 | memset(&act, 0, sizeof(act)); |
| 153 | act.sa_handler = sig_alrm; |
| 154 | act.sa_flags = SA_RESTART; |
| 155 | sigaction(SIGALRM, &act, NULL); |
| 156 | |
| 157 | memset(&act, 0, sizeof(act)); |
| 158 | act.sa_handler = sig_int; |
| 159 | act.sa_flags = SA_RESTART; |
| 160 | sigaction(SIGINT, &act, NULL); |
Jens Axboe | de79c91 | 2008-08-04 21:43:55 +0200 | [diff] [blame] | 161 | |
| 162 | memset(&act, 0, sizeof(act)); |
| 163 | act.sa_handler = sig_ill; |
| 164 | act.sa_flags = SA_RESTART; |
| 165 | sigaction(SIGILL, &act, NULL); |
Jens Axboe | 5f8f972 | 2008-11-18 19:10:21 +0100 | [diff] [blame] | 166 | |
| 167 | memset(&act, 0, sizeof(act)); |
| 168 | act.sa_handler = sig_quit; |
| 169 | act.sa_flags = SA_RESTART; |
| 170 | sigaction(SIGQUIT, &act, NULL); |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 173 | static inline int should_check_rate(struct thread_data *td) |
| 174 | { |
Jens Axboe | e235d5f | 2009-01-19 12:49:48 +0100 | [diff] [blame] | 175 | struct thread_options *o = &td->o; |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 176 | |
Jens Axboe | e235d5f | 2009-01-19 12:49:48 +0100 | [diff] [blame] | 177 | /* |
| 178 | * If some rate setting was given, we need to check it |
| 179 | */ |
| 180 | if (o->rate || o->ratemin || o->rate_iops || o->rate_iops_min) |
| 181 | return 1; |
| 182 | |
| 183 | return 0; |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 186 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 187 | * Check if we are above the minimum rate given. |
| 188 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 189 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
| 190 | { |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 191 | unsigned long long bytes = 0; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 192 | unsigned long iops = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 193 | unsigned long spent; |
| 194 | unsigned long rate; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * allow a 2 second settle period in the beginning |
| 198 | */ |
| 199 | if (mtime_since(&td->start, now) < 2000) |
| 200 | return 0; |
| 201 | |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 202 | if (td_read(td)) { |
| 203 | iops += td->io_blocks[DDIR_READ]; |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 204 | bytes += td->this_io_bytes[DDIR_READ]; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 205 | } |
| 206 | if (td_write(td)) { |
| 207 | iops += td->io_blocks[DDIR_WRITE]; |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 208 | bytes += td->this_io_bytes[DDIR_WRITE]; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 209 | } |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 210 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 211 | /* |
| 212 | * if rate blocks is set, sample is running |
| 213 | */ |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 214 | if (td->rate_bytes || td->rate_blocks) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 215 | spent = mtime_since(&td->lastrate, now); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 216 | if (spent < td->o.ratecycle) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 217 | return 0; |
| 218 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 219 | if (td->o.rate) { |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 220 | /* |
| 221 | * check bandwidth specified rate |
| 222 | */ |
| 223 | if (bytes < td->rate_bytes) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 224 | log_err("%s: min rate %u not met\n", td->o.name, |
| 225 | td->o.ratemin); |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 226 | return 1; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 227 | } else { |
| 228 | rate = (bytes - td->rate_bytes) / spent; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 229 | if (rate < td->o.ratemin || |
| 230 | bytes < td->rate_bytes) { |
| 231 | log_err("%s: min rate %u not met, got" |
| 232 | " %luKiB/sec\n", td->o.name, |
| 233 | td->o.ratemin, rate); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 234 | return 1; |
| 235 | } |
| 236 | } |
| 237 | } else { |
| 238 | /* |
| 239 | * checks iops specified rate |
| 240 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 241 | if (iops < td->o.rate_iops) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 242 | log_err("%s: min iops rate %u not met\n", |
| 243 | td->o.name, td->o.rate_iops); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 244 | return 1; |
| 245 | } else { |
| 246 | rate = (iops - td->rate_blocks) / spent; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 247 | if (rate < td->o.rate_iops_min || |
| 248 | iops < td->rate_blocks) { |
| 249 | log_err("%s: min iops rate %u not met," |
| 250 | " got %lu\n", td->o.name, |
| 251 | td->o.rate_iops_min, |
| 252 | rate); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 253 | } |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 254 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 258 | td->rate_bytes = bytes; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 259 | td->rate_blocks = iops; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 260 | memcpy(&td->lastrate, now, sizeof(*now)); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 265 | { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 266 | if (!td->o.timeout) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 267 | return 0; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 268 | if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 269 | return 1; |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 274 | /* |
| 275 | * When job exits, we can cancel the in-flight IO if we are using async |
| 276 | * io. Attempt to do so. |
| 277 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 278 | static void cleanup_pending_aio(struct thread_data *td) |
| 279 | { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 280 | struct flist_head *entry, *n; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 281 | struct io_u *io_u; |
| 282 | int r; |
| 283 | |
| 284 | /* |
| 285 | * get immediately available events, if any |
| 286 | */ |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 287 | r = io_u_queued_complete(td, 0); |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 288 | if (r < 0) |
| 289 | return; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * now cancel remaining active events |
| 293 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 294 | if (td->io_ops->cancel) { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 295 | flist_for_each_safe(entry, n, &td->io_u_busylist) { |
| 296 | io_u = flist_entry(entry, struct io_u, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 297 | |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 298 | /* |
| 299 | * if the io_u isn't in flight, then that generally |
| 300 | * means someone leaked an io_u. complain but fix |
| 301 | * it up, so we don't stall here. |
| 302 | */ |
| 303 | if ((io_u->flags & IO_U_F_FLIGHT) == 0) { |
| 304 | log_err("fio: non-busy IO on busy list\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 305 | put_io_u(td, io_u); |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 306 | } else { |
| 307 | r = td->io_ops->cancel(td, io_u); |
| 308 | if (!r) |
| 309 | put_io_u(td, io_u); |
| 310 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 314 | if (td->cur_depth) |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 315 | r = io_u_queued_complete(td, td->cur_depth); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 318 | /* |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 319 | * Helper to handle the final sync of a file. Works just like the normal |
| 320 | * io path, just does everything sync. |
| 321 | */ |
| 322 | static int fio_io_sync(struct thread_data *td, struct fio_file *f) |
| 323 | { |
| 324 | struct io_u *io_u = __get_io_u(td); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 325 | int ret; |
| 326 | |
| 327 | if (!io_u) |
| 328 | return 1; |
| 329 | |
| 330 | io_u->ddir = DDIR_SYNC; |
| 331 | io_u->file = f; |
| 332 | |
| 333 | if (td_io_prep(td, io_u)) { |
| 334 | put_io_u(td, io_u); |
| 335 | return 1; |
| 336 | } |
| 337 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 338 | requeue: |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 339 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 340 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 341 | td_verror(td, io_u->error, "td_io_queue"); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 342 | put_io_u(td, io_u); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 343 | return 1; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 344 | } else if (ret == FIO_Q_QUEUED) { |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 345 | if (io_u_queued_complete(td, 1) < 0) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 346 | return 1; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 347 | } else if (ret == FIO_Q_COMPLETED) { |
| 348 | if (io_u->error) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 349 | td_verror(td, io_u->error, "td_io_queue"); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 350 | return 1; |
| 351 | } |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 352 | |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 353 | if (io_u_sync_complete(td, io_u) < 0) |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 354 | return 1; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 355 | } else if (ret == FIO_Q_BUSY) { |
| 356 | if (td_io_commit(td)) |
| 357 | return 1; |
| 358 | goto requeue; |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 364 | static inline void update_tv_cache(struct thread_data *td) |
| 365 | { |
| 366 | if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask) |
| 367 | fio_gettime(&td->tv_cache, NULL); |
| 368 | } |
| 369 | |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 370 | /* |
Jens Axboe | 34403fb | 2007-03-02 21:43:25 +0100 | [diff] [blame] | 371 | * The main verify engine. Runs over the writes we previously submitted, |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 372 | * reads the blocks back in, and checks the crc/md5 of the data. |
| 373 | */ |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 374 | static void do_verify(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 375 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 376 | struct fio_file *f; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 377 | struct io_u *io_u; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 378 | int ret, min_events; |
| 379 | unsigned int i; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * sync io first and invalidate cache, to make sure we really |
| 383 | * read from disk. |
| 384 | */ |
| 385 | for_each_file(td, f, i) { |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 386 | if (!(f->flags & FIO_FILE_OPEN)) |
| 387 | continue; |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 388 | if (fio_io_sync(td, f)) |
| 389 | break; |
| 390 | if (file_invalidate_cache(td, f)) |
| 391 | break; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 392 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 393 | |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 394 | if (td->error) |
| 395 | return; |
| 396 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 397 | td_set_runstate(td, TD_VERIFYING); |
| 398 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 399 | io_u = NULL; |
| 400 | while (!td->terminate) { |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 401 | int ret2, full; |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 402 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 403 | io_u = __get_io_u(td); |
| 404 | if (!io_u) |
| 405 | break; |
| 406 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 407 | update_tv_cache(td); |
| 408 | |
| 409 | if (runtime_exceeded(td, &td->tv_cache)) { |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 410 | put_io_u(td, io_u); |
Jens Axboe | 90a87d4 | 2007-04-18 09:02:32 +0200 | [diff] [blame] | 411 | td->terminate = 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 412 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 413 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 414 | |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 415 | if (get_next_verify(td, io_u)) { |
| 416 | put_io_u(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 417 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 418 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 419 | |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 420 | if (td_io_prep(td, io_u)) { |
| 421 | put_io_u(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 422 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 423 | } |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 424 | |
| 425 | io_u->end_io = verify_io_u; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 426 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 427 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 428 | switch (ret) { |
| 429 | case FIO_Q_COMPLETED: |
| 430 | if (io_u->error) |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 431 | ret = -io_u->error; |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 432 | else if (io_u->resid) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 433 | int bytes = io_u->xfer_buflen - io_u->resid; |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 434 | struct fio_file *f = io_u->file; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 435 | |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 436 | /* |
| 437 | * zero read, fail |
| 438 | */ |
| 439 | if (!bytes) { |
Jens Axboe | 41d9f0f | 2008-06-02 12:19:50 +0200 | [diff] [blame] | 440 | td_verror(td, EIO, "full resid"); |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 441 | put_io_u(td, io_u); |
| 442 | break; |
| 443 | } |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 444 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 445 | io_u->xfer_buflen = io_u->resid; |
| 446 | io_u->xfer_buf += bytes; |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 447 | io_u->offset += bytes; |
| 448 | |
Jens Axboe | 30061b9 | 2007-04-17 13:31:34 +0200 | [diff] [blame] | 449 | td->ts.short_io_u[io_u->ddir]++; |
| 450 | |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 451 | if (io_u->offset == f->real_file_size) |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 452 | goto sync_done; |
| 453 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 454 | requeue_io_u(td, &io_u); |
| 455 | } else { |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 456 | sync_done: |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 457 | ret = io_u_sync_complete(td, io_u); |
| 458 | if (ret < 0) |
| 459 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 460 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 461 | continue; |
| 462 | case FIO_Q_QUEUED: |
| 463 | break; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 464 | case FIO_Q_BUSY: |
| 465 | requeue_io_u(td, &io_u); |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 466 | ret2 = td_io_commit(td); |
| 467 | if (ret2 < 0) |
| 468 | ret = ret2; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 469 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 470 | default: |
| 471 | assert(ret < 0); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 472 | td_verror(td, -ret, "td_io_queue"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 473 | break; |
| 474 | } |
| 475 | |
Jens Axboe | 9978463 | 2007-02-19 10:06:17 +0100 | [diff] [blame] | 476 | if (ret < 0 || td->error) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 477 | break; |
| 478 | |
Jens Axboe | 3af6ef3 | 2007-02-18 06:57:43 +0100 | [diff] [blame] | 479 | /* |
| 480 | * if we can queue more, do so. but check if there are |
| 481 | * completed io_u's first. |
| 482 | */ |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 483 | full = queue_full(td) || ret == FIO_Q_BUSY; |
| 484 | if (full || !td->o.iodepth_batch_complete) { |
| 485 | min_events = td->o.iodepth_batch_complete; |
| 486 | if (full && !min_events) |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 487 | min_events = 1; |
Jens Axboe | e916b39 | 2007-02-20 14:37:26 +0100 | [diff] [blame] | 488 | |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 489 | do { |
| 490 | /* |
| 491 | * Reap required number of io units, if any, |
| 492 | * and do the verification on them through |
| 493 | * the callback handler |
| 494 | */ |
| 495 | if (io_u_queued_complete(td, min_events) < 0) { |
| 496 | ret = -1; |
| 497 | break; |
| 498 | } |
| 499 | } while (full && (td->cur_depth > td->o.iodepth_low)); |
| 500 | } |
| 501 | if (ret < 0) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 502 | break; |
| 503 | } |
| 504 | |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 505 | if (!td->error) { |
| 506 | min_events = td->cur_depth; |
| 507 | |
| 508 | if (min_events) |
| 509 | ret = io_u_queued_complete(td, min_events); |
| 510 | } else |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 511 | cleanup_pending_aio(td); |
| 512 | |
| 513 | td_set_runstate(td, TD_RUNNING); |
| 514 | } |
| 515 | |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 516 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 517 | * Main IO worker function. It retrieves io_u's to process and queues |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 518 | * and reaps them, checking for rate and errors along the way. |
| 519 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 520 | static void do_io(struct thread_data *td) |
| 521 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 522 | unsigned long usec; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 523 | unsigned int i; |
| 524 | int ret = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 525 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 526 | if (in_ramp_time(td)) |
| 527 | td_set_runstate(td, TD_RAMP); |
| 528 | else |
| 529 | td_set_runstate(td, TD_RUNNING); |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 530 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 531 | while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) { |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 532 | struct timeval comp_time; |
| 533 | long bytes_done = 0; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 534 | int min_evts = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 535 | struct io_u *io_u; |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 536 | int ret2, full; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 537 | |
| 538 | if (td->terminate) |
| 539 | break; |
| 540 | |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 541 | io_u = get_io_u(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 542 | if (!io_u) |
| 543 | break; |
| 544 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 545 | update_tv_cache(td); |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 546 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 547 | if (runtime_exceeded(td, &td->tv_cache)) { |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 548 | put_io_u(td, io_u); |
Jens Axboe | 90a87d4 | 2007-04-18 09:02:32 +0200 | [diff] [blame] | 549 | td->terminate = 1; |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 550 | break; |
| 551 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 552 | |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 553 | /* |
| 554 | * Add verification end_io handler, if asked to verify |
| 555 | * a previously written file. |
| 556 | */ |
Jens Axboe | cc3fc85 | 2008-03-06 11:47:53 +0100 | [diff] [blame] | 557 | if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) { |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 558 | io_u->end_io = verify_io_u; |
Jens Axboe | d8fab1b | 2008-03-06 10:25:17 +0100 | [diff] [blame] | 559 | td_set_runstate(td, TD_VERIFYING); |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 560 | } else if (in_ramp_time(td)) |
| 561 | td_set_runstate(td, TD_RAMP); |
| 562 | else |
Jens Axboe | d8fab1b | 2008-03-06 10:25:17 +0100 | [diff] [blame] | 563 | td_set_runstate(td, TD_RUNNING); |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 564 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 565 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 566 | switch (ret) { |
| 567 | case FIO_Q_COMPLETED: |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 568 | if (io_u->error) |
| 569 | ret = -io_u->error; |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 570 | else if (io_u->resid) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 571 | int bytes = io_u->xfer_buflen - io_u->resid; |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 572 | struct fio_file *f = io_u->file; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 573 | |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 574 | /* |
| 575 | * zero read, fail |
| 576 | */ |
| 577 | if (!bytes) { |
Jens Axboe | 41d9f0f | 2008-06-02 12:19:50 +0200 | [diff] [blame] | 578 | td_verror(td, EIO, "full resid"); |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 579 | put_io_u(td, io_u); |
| 580 | break; |
| 581 | } |
| 582 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 583 | io_u->xfer_buflen = io_u->resid; |
| 584 | io_u->xfer_buf += bytes; |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 585 | io_u->offset += bytes; |
| 586 | |
Jens Axboe | 30061b9 | 2007-04-17 13:31:34 +0200 | [diff] [blame] | 587 | td->ts.short_io_u[io_u->ddir]++; |
| 588 | |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 589 | if (io_u->offset == f->real_file_size) |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 590 | goto sync_done; |
| 591 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 592 | requeue_io_u(td, &io_u); |
| 593 | } else { |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 594 | sync_done: |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 595 | if (should_check_rate(td)) |
| 596 | fio_gettime(&comp_time, NULL); |
| 597 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 598 | bytes_done = io_u_sync_complete(td, io_u); |
| 599 | if (bytes_done < 0) |
| 600 | ret = bytes_done; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 601 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 602 | break; |
| 603 | case FIO_Q_QUEUED: |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 604 | /* |
| 605 | * if the engine doesn't have a commit hook, |
| 606 | * the io_u is really queued. if it does have such |
| 607 | * a hook, it has to call io_u_queued() itself. |
| 608 | */ |
| 609 | if (td->io_ops->commit == NULL) |
| 610 | io_u_queued(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 611 | break; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 612 | case FIO_Q_BUSY: |
| 613 | requeue_io_u(td, &io_u); |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 614 | ret2 = td_io_commit(td); |
| 615 | if (ret2 < 0) |
| 616 | ret = ret2; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 617 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 618 | default: |
| 619 | assert(ret < 0); |
| 620 | put_io_u(td, io_u); |
| 621 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Jens Axboe | 9978463 | 2007-02-19 10:06:17 +0100 | [diff] [blame] | 624 | if (ret < 0 || td->error) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 625 | break; |
| 626 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 627 | /* |
| 628 | * See if we need to complete some commands |
| 629 | */ |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 630 | full = queue_full(td) || ret == FIO_Q_BUSY; |
| 631 | if (full || !td->o.iodepth_batch_complete) { |
| 632 | min_evts = td->o.iodepth_batch_complete; |
| 633 | if (full && !min_evts) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 634 | min_evts = 1; |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 635 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 636 | if (should_check_rate(td)) |
| 637 | fio_gettime(&comp_time, NULL); |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 638 | |
| 639 | do { |
| 640 | ret = io_u_queued_complete(td, min_evts); |
| 641 | if (ret <= 0) |
| 642 | break; |
| 643 | |
| 644 | bytes_done += ret; |
| 645 | } while (full && (td->cur_depth > td->o.iodepth_low)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 648 | if (ret < 0) |
| 649 | break; |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 650 | if (!bytes_done) |
| 651 | continue; |
| 652 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 653 | /* |
| 654 | * the rate is batched for now, it should work for batches |
| 655 | * of completions except the very first one which may look |
| 656 | * a little bursty |
| 657 | */ |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 658 | if (!in_ramp_time(td) && should_check_rate(td)) { |
| 659 | usec = utime_since(&td->tv_cache, &comp_time); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 660 | |
Jens Axboe | 721938a | 2008-09-10 09:46:16 +0200 | [diff] [blame] | 661 | rate_throttle(td, usec, bytes_done); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 662 | |
Jens Axboe | 721938a | 2008-09-10 09:46:16 +0200 | [diff] [blame] | 663 | if (check_min_rate(td, &comp_time)) { |
| 664 | if (exitall_on_terminate) |
| 665 | terminate_threads(td->groupid); |
| 666 | td_verror(td, EIO, "check_min_rate"); |
| 667 | break; |
| 668 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 671 | if (td->o.thinktime) { |
Jens Axboe | 9c1f743 | 2007-01-03 20:43:19 +0100 | [diff] [blame] | 672 | unsigned long long b; |
| 673 | |
| 674 | b = td->io_blocks[0] + td->io_blocks[1]; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 675 | if (!(b % td->o.thinktime_blocks)) { |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 676 | int left; |
| 677 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 678 | if (td->o.thinktime_spin) |
Jens Axboe | 6dd6f2c | 2008-12-10 13:24:12 +0100 | [diff] [blame] | 679 | usec_spin(td->o.thinktime_spin); |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 680 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 681 | left = td->o.thinktime - td->o.thinktime_spin; |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 682 | if (left) |
| 683 | usec_sleep(td, left); |
| 684 | } |
Jens Axboe | 9c1f743 | 2007-01-03 20:43:19 +0100 | [diff] [blame] | 685 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Shawn Lewis | aa31f1f | 2008-01-11 09:45:11 +0100 | [diff] [blame] | 688 | if (td->o.fill_device && td->error == ENOSPC) { |
| 689 | td->error = 0; |
| 690 | td->terminate = 1; |
| 691 | } |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 692 | if (!td->error) { |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 693 | struct fio_file *f; |
| 694 | |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 695 | i = td->cur_depth; |
| 696 | if (i) |
| 697 | ret = io_u_queued_complete(td, i); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 698 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 699 | if (should_fsync(td) && td->o.end_fsync) { |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 700 | td_set_runstate(td, TD_FSYNCING); |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 701 | |
| 702 | for_each_file(td, f, i) { |
| 703 | if (!(f->flags & FIO_FILE_OPEN)) |
| 704 | continue; |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 705 | fio_io_sync(td, f); |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 706 | } |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 707 | } |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 708 | } else |
| 709 | cleanup_pending_aio(td); |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 710 | |
| 711 | /* |
| 712 | * stop job if we failed doing any IO |
| 713 | */ |
| 714 | if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0) |
| 715 | td->done = 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 716 | } |
| 717 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 718 | static void cleanup_io_u(struct thread_data *td) |
| 719 | { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 720 | struct flist_head *entry, *n; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 721 | struct io_u *io_u; |
| 722 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 723 | flist_for_each_safe(entry, n, &td->io_u_freelist) { |
| 724 | io_u = flist_entry(entry, struct io_u, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 725 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 726 | flist_del(&io_u->list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 727 | free(io_u); |
| 728 | } |
| 729 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 730 | free_io_mem(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static int init_io_u(struct thread_data *td) |
| 734 | { |
| 735 | struct io_u *io_u; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 736 | unsigned int max_bs; |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 737 | int cl_align, i, max_units; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 738 | char *p; |
| 739 | |
Jens Axboe | 1e3c09a | 2008-02-04 10:48:13 +0100 | [diff] [blame] | 740 | max_units = td->o.iodepth; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 741 | max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 742 | td->orig_buffer_size = (unsigned long long) max_bs |
| 743 | * (unsigned long long) max_units; |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 744 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 745 | if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) { |
| 746 | unsigned long bs; |
| 747 | |
| 748 | bs = td->orig_buffer_size + td->o.hugepage_size - 1; |
| 749 | td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1); |
| 750 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 751 | |
Jens Axboe | c399064 | 2007-07-19 10:17:09 +0200 | [diff] [blame] | 752 | if (td->orig_buffer_size != (size_t) td->orig_buffer_size) { |
| 753 | log_err("fio: IO memory too large. Reduce max_bs or iodepth\n"); |
| 754 | return 1; |
| 755 | } |
| 756 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 757 | if (allocate_io_mem(td)) |
| 758 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 759 | |
Jens Axboe | b3f378e | 2007-07-20 12:39:22 +0200 | [diff] [blame] | 760 | if (td->o.odirect) |
| 761 | p = ALIGN(td->orig_buffer); |
| 762 | else |
| 763 | p = td->orig_buffer; |
| 764 | |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 765 | cl_align = os_cache_line_size(); |
| 766 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 767 | for (i = 0; i < max_units; i++) { |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 768 | void *ptr; |
| 769 | |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 770 | if (td->terminate) |
| 771 | return 1; |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 772 | |
| 773 | if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) { |
| 774 | log_err("fio: posix_memalign=%s\n", strerror(errno)); |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | io_u = ptr; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 779 | memset(io_u, 0, sizeof(*io_u)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 780 | INIT_FLIST_HEAD(&io_u->list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 781 | |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame] | 782 | if (!(td->io_ops->flags & FIO_NOIO)) { |
| 783 | io_u->buf = p + max_bs * i; |
Jens Axboe | e9459e5 | 2007-04-17 15:46:32 +0200 | [diff] [blame] | 784 | |
Jens Axboe | 5973caf | 2008-05-21 19:52:35 +0200 | [diff] [blame] | 785 | if (td_write(td) && !td->o.refill_buffers) |
| 786 | io_u_fill_buffer(td, io_u, max_bs); |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame] | 787 | } |
Jens Axboe | 6b9cea2 | 2006-10-30 17:00:24 +0100 | [diff] [blame] | 788 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 789 | io_u->index = i; |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 790 | io_u->flags = IO_U_F_FREE; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 791 | flist_add(&io_u->list, &td->io_u_freelist); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 797 | static int switch_ioscheduler(struct thread_data *td) |
| 798 | { |
| 799 | char tmp[256], tmp2[128]; |
| 800 | FILE *f; |
| 801 | int ret; |
| 802 | |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 803 | if (td->io_ops->flags & FIO_DISKLESSIO) |
Jens Axboe | f48b467 | 2006-10-27 11:30:07 +0200 | [diff] [blame] | 804 | return 0; |
| 805 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 806 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
| 807 | |
| 808 | f = fopen(tmp, "r+"); |
| 809 | if (!f) { |
Jens Axboe | cbf5c1d | 2007-04-16 12:56:00 +0200 | [diff] [blame] | 810 | if (errno == ENOENT) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 811 | log_err("fio: os or kernel doesn't support IO scheduler" |
| 812 | " switching\n"); |
Jens Axboe | cbf5c1d | 2007-04-16 12:56:00 +0200 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | td_verror(td, errno, "fopen iosched"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 816 | return 1; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * Set io scheduler. |
| 821 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 822 | ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 823 | if (ferror(f) || ret != 1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 824 | td_verror(td, errno, "fwrite"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 825 | fclose(f); |
| 826 | return 1; |
| 827 | } |
| 828 | |
| 829 | rewind(f); |
| 830 | |
| 831 | /* |
| 832 | * Read back and check that the selected scheduler is now the default. |
| 833 | */ |
| 834 | ret = fread(tmp, 1, sizeof(tmp), f); |
| 835 | if (ferror(f) || ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 836 | td_verror(td, errno, "fread"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 837 | fclose(f); |
| 838 | return 1; |
| 839 | } |
| 840 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 841 | sprintf(tmp2, "[%s]", td->o.ioscheduler); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 842 | if (!strstr(tmp, tmp2)) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 843 | log_err("fio: io scheduler %s not found\n", td->o.ioscheduler); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 844 | td_verror(td, EINVAL, "iosched_switch"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 845 | fclose(f); |
| 846 | return 1; |
| 847 | } |
| 848 | |
| 849 | fclose(f); |
| 850 | return 0; |
| 851 | } |
| 852 | |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 853 | static int keep_running(struct thread_data *td) |
| 854 | { |
| 855 | unsigned long long io_done; |
| 856 | |
Jens Axboe | 20e354e | 2007-07-23 14:36:16 +0200 | [diff] [blame] | 857 | if (td->done) |
| 858 | return 0; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 859 | if (td->o.time_based) |
| 860 | return 1; |
| 861 | if (td->o.loops) { |
| 862 | td->o.loops--; |
| 863 | return 1; |
| 864 | } |
| 865 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 866 | io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE] |
| 867 | + td->io_skip_bytes; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 868 | if (io_done < td->o.size) |
| 869 | return 1; |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 874 | static void reset_io_counters(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 875 | { |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 876 | td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 877 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 878 | td->zone_bytes = 0; |
Jens Axboe | d7d3b49 | 2007-03-12 08:02:10 +0100 | [diff] [blame] | 879 | td->rate_bytes = 0; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 880 | td->rate_blocks = 0; |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 881 | td->rw_end_set[0] = td->rw_end_set[1] = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 882 | |
Jens Axboe | c1324df | 2007-02-19 19:06:41 +0100 | [diff] [blame] | 883 | td->last_was_sync = 0; |
| 884 | |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 885 | /* |
| 886 | * reset file done count if we are to start over |
| 887 | */ |
| 888 | if (td->o.time_based || td->o.loops) |
Jens Axboe | 48f5abd | 2007-07-20 13:25:04 +0200 | [diff] [blame] | 889 | td->nr_done_files = 0; |
Jens Axboe | 5bfc35d | 2009-04-07 13:28:12 +0200 | [diff] [blame] | 890 | |
| 891 | /* |
| 892 | * Set the same seed to get repeatable runs |
| 893 | */ |
| 894 | td_fill_rand_seeds(td); |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | void reset_all_stats(struct thread_data *td) |
| 898 | { |
| 899 | struct timeval tv; |
| 900 | int i; |
| 901 | |
| 902 | reset_io_counters(td); |
| 903 | |
| 904 | for (i = 0; i < 2; i++) { |
| 905 | td->io_bytes[i] = 0; |
| 906 | td->io_blocks[i] = 0; |
| 907 | td->io_issues[i] = 0; |
| 908 | td->ts.total_io_u[i] = 0; |
| 909 | } |
| 910 | |
| 911 | fio_gettime(&tv, NULL); |
| 912 | memcpy(&td->epoch, &tv, sizeof(tv)); |
| 913 | memcpy(&td->start, &tv, sizeof(tv)); |
| 914 | } |
| 915 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 916 | static void clear_io_state(struct thread_data *td) |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 917 | { |
| 918 | struct fio_file *f; |
| 919 | unsigned int i; |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 920 | |
| 921 | reset_io_counters(td); |
Jens Axboe | 281f9b6 | 2007-05-23 09:34:42 +0200 | [diff] [blame] | 922 | |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 923 | close_files(td); |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 924 | for_each_file(td, f, i) |
Jens Axboe | 281f9b6 | 2007-05-23 09:34:42 +0200 | [diff] [blame] | 925 | f->flags &= ~FIO_FILE_DONE; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 926 | } |
| 927 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 928 | /* |
| 929 | * Entry point for the thread based jobs. The process based jobs end up |
| 930 | * here as well, after a little setup. |
| 931 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 932 | static void *thread_main(void *data) |
| 933 | { |
Shawn Lewis | 1092731 | 2007-11-21 09:38:13 +0100 | [diff] [blame] | 934 | unsigned long long runtime[2], elapsed; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 935 | struct thread_data *td = data; |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 936 | int clear_state; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 937 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 938 | if (!td->o.use_thread) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 939 | setsid(); |
| 940 | |
| 941 | td->pid = getpid(); |
| 942 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 943 | dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 944 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 945 | INIT_FLIST_HEAD(&td->io_u_freelist); |
| 946 | INIT_FLIST_HEAD(&td->io_u_busylist); |
| 947 | INIT_FLIST_HEAD(&td->io_u_requeues); |
| 948 | INIT_FLIST_HEAD(&td->io_log_list); |
| 949 | INIT_FLIST_HEAD(&td->io_hist_list); |
Jens Axboe | bb5d7d0 | 2007-03-27 10:21:25 +0200 | [diff] [blame] | 950 | td->io_hist_tree = RB_ROOT; |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 951 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 952 | td_set_runstate(td, TD_INITIALIZED); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 953 | dprint(FD_MUTEX, "up startup_mutex\n"); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 954 | fio_mutex_up(startup_mutex); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 955 | dprint(FD_MUTEX, "wait on td->mutex\n"); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 956 | fio_mutex_down(td->mutex); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 957 | dprint(FD_MUTEX, "done waiting on td->mutex\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 958 | |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 959 | /* |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 960 | * the ->mutex mutex is now no longer used, close it to avoid |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 961 | * eating a file descriptor |
| 962 | */ |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 963 | fio_mutex_remove(td->mutex); |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 964 | |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 965 | /* |
| 966 | * May alter parameters that init_io_u() will use, so we need to |
| 967 | * do this first. |
| 968 | */ |
| 969 | if (init_iolog(td)) |
| 970 | goto err; |
| 971 | |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 972 | if (init_io_u(td)) |
| 973 | goto err; |
| 974 | |
Jens Axboe | 375b269 | 2007-05-22 09:13:02 +0200 | [diff] [blame] | 975 | if (td->o.cpumask_set && fio_setaffinity(td) == -1) { |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 976 | td_verror(td, errno, "cpu_set_affinity"); |
| 977 | goto err; |
| 978 | } |
| 979 | |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 980 | /* |
| 981 | * If we have a gettimeofday() thread, make sure we exclude that |
| 982 | * thread from this job |
| 983 | */ |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 984 | if (td->o.gtod_cpu) { |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 985 | fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu); |
| 986 | if (fio_setaffinity(td) == -1) { |
| 987 | td_verror(td, errno, "cpu_set_affinity"); |
| 988 | goto err; |
| 989 | } |
| 990 | } |
| 991 | |
Jens Axboe | ac68478 | 2007-11-08 08:29:07 +0100 | [diff] [blame] | 992 | if (td->ioprio_set) { |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 993 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 994 | td_verror(td, errno, "ioprio_set"); |
| 995 | goto err; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | if (nice(td->o.nice) == -1) { |
| 1000 | td_verror(td, errno, "nice"); |
| 1001 | goto err; |
| 1002 | } |
| 1003 | |
| 1004 | if (td->o.ioscheduler && switch_ioscheduler(td)) |
| 1005 | goto err; |
| 1006 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1007 | if (!td->o.create_serialize && setup_files(td)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1008 | goto err; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1009 | |
| 1010 | if (td_io_init(td)) |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 1011 | goto err; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1012 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 1013 | if (init_random_map(td)) |
| 1014 | goto err; |
| 1015 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1016 | if (td->o.exec_prerun) { |
| 1017 | if (system(td->o.exec_prerun) < 0) |
Jens Axboe | 69cfd7e | 2007-02-07 13:58:53 +0100 | [diff] [blame] | 1018 | goto err; |
| 1019 | } |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 1020 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 1021 | fio_gettime(&td->epoch, NULL); |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 1022 | getrusage(RUSAGE_SELF, &td->ts.ru_start); |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 1023 | |
| 1024 | runtime[0] = runtime[1] = 0; |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1025 | clear_state = 0; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 1026 | while (keep_running(td)) { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 1027 | fio_gettime(&td->start, NULL); |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 1028 | memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start)); |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 1029 | memcpy(&td->tv_cache, &td->start, sizeof(td->start)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1030 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1031 | if (td->o.ratemin) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1032 | memcpy(&td->lastrate, &td->ts.stat_sample_time, |
| 1033 | sizeof(td->lastrate)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1034 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 1035 | if (clear_state) |
| 1036 | clear_io_state(td); |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1037 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1038 | prune_io_piece_log(td); |
| 1039 | |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 1040 | do_io(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1041 | |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1042 | clear_state = 1; |
| 1043 | |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 1044 | if (td_read(td) && td->io_bytes[DDIR_READ]) { |
| 1045 | if (td->rw_end_set[DDIR_READ]) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1046 | elapsed = utime_since(&td->start, |
| 1047 | &td->rw_end[DDIR_READ]); |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 1048 | else |
| 1049 | elapsed = utime_since_now(&td->start); |
| 1050 | |
| 1051 | runtime[DDIR_READ] += elapsed; |
| 1052 | } |
| 1053 | if (td_write(td) && td->io_bytes[DDIR_WRITE]) { |
| 1054 | if (td->rw_end_set[DDIR_WRITE]) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1055 | elapsed = utime_since(&td->start, |
| 1056 | &td->rw_end[DDIR_WRITE]); |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 1057 | else |
| 1058 | elapsed = utime_since_now(&td->start); |
| 1059 | |
| 1060 | runtime[DDIR_WRITE] += elapsed; |
| 1061 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1062 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1063 | if (td->error || td->terminate) |
| 1064 | break; |
| 1065 | |
Shawn Lewis | e84c73a | 2007-08-02 22:19:32 +0200 | [diff] [blame] | 1066 | if (!td->o.do_verify || |
| 1067 | td->o.verify == VERIFY_NONE || |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 1068 | (td->io_ops->flags & FIO_UNIDIR)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1069 | continue; |
| 1070 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 1071 | clear_io_state(td); |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1072 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 1073 | fio_gettime(&td->start, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1074 | |
| 1075 | do_verify(td); |
| 1076 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 1077 | runtime[DDIR_READ] += utime_since_now(&td->start); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1078 | |
| 1079 | if (td->error || td->terminate) |
| 1080 | break; |
| 1081 | } |
| 1082 | |
Jens Axboe | 36dff96 | 2006-11-24 13:29:20 +0100 | [diff] [blame] | 1083 | update_rusage_stat(td); |
ljzhang,Yaxin Hu,Jianchao Tang | 47f0cc4 | 2007-07-26 11:00:29 +0200 | [diff] [blame] | 1084 | td->ts.runtime[0] = (runtime[0] + 999) / 1000; |
| 1085 | td->ts.runtime[1] = (runtime[1] + 999) / 1000; |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 1086 | td->ts.total_run_time = mtime_since_now(&td->epoch); |
| 1087 | td->ts.io_bytes[0] = td->io_bytes[0]; |
| 1088 | td->ts.io_bytes[1] = td->io_bytes[1]; |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 1089 | |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 1090 | fio_mutex_down(writeout_mutex); |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 1091 | if (td->ts.bw_log) { |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1092 | if (td->o.bw_log_file) { |
| 1093 | finish_log_named(td, td->ts.bw_log, |
| 1094 | td->o.bw_log_file, "bw"); |
| 1095 | } else |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 1096 | finish_log(td, td->ts.bw_log, "bw"); |
| 1097 | } |
| 1098 | if (td->ts.slat_log) { |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1099 | if (td->o.lat_log_file) { |
| 1100 | finish_log_named(td, td->ts.slat_log, |
Radha Ramachandran | 1ca9573 | 2009-04-29 07:23:26 +0200 | [diff] [blame] | 1101 | td->o.lat_log_file, "slat"); |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1102 | } else |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 1103 | finish_log(td, td->ts.slat_log, "slat"); |
| 1104 | } |
| 1105 | if (td->ts.clat_log) { |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1106 | if (td->o.lat_log_file) { |
| 1107 | finish_log_named(td, td->ts.clat_log, |
| 1108 | td->o.lat_log_file, "clat"); |
| 1109 | } else |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 1110 | finish_log(td, td->ts.clat_log, "clat"); |
| 1111 | } |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 1112 | fio_mutex_up(writeout_mutex); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1113 | if (td->o.exec_postrun) { |
| 1114 | if (system(td->o.exec_postrun) < 0) |
| 1115 | log_err("fio: postrun %s failed\n", td->o.exec_postrun); |
Jens Axboe | 69cfd7e | 2007-02-07 13:58:53 +0100 | [diff] [blame] | 1116 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1117 | |
| 1118 | if (exitall_on_terminate) |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 1119 | terminate_threads(td->groupid); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1120 | |
| 1121 | err: |
Jens Axboe | 5bf13a5 | 2007-02-17 01:51:47 +0100 | [diff] [blame] | 1122 | if (td->error) |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1123 | printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error, |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1124 | td->verror); |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 1125 | close_and_free_files(td); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1126 | close_ioengine(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1127 | cleanup_io_u(td); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1128 | |
Jens Axboe | d2ce18b | 2008-12-12 20:51:40 +0100 | [diff] [blame] | 1129 | if (td->o.cpumask_set) { |
| 1130 | int ret = fio_cpuset_exit(&td->o.cpumask); |
| 1131 | |
| 1132 | td_verror(td, ret, "fio_cpuset_exit"); |
| 1133 | } |
Jens Axboe | 4e78e40 | 2008-12-12 20:40:27 +0100 | [diff] [blame] | 1134 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1135 | /* |
| 1136 | * do this very late, it will log file closing as well |
| 1137 | */ |
| 1138 | if (td->o.write_iolog_file) |
| 1139 | write_iolog_close(td); |
| 1140 | |
Jens Axboe | d23bb32 | 2007-03-23 15:57:56 +0100 | [diff] [blame] | 1141 | options_mem_free(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1142 | td_set_runstate(td, TD_EXITED); |
Jens Axboe | 43d7680 | 2007-02-21 16:35:29 +0100 | [diff] [blame] | 1143 | return (void *) (unsigned long) td->error; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1144 | } |
| 1145 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1146 | /* |
| 1147 | * We cannot pass the td data into a forked process, so attach the td and |
| 1148 | * pass it to the thread worker. |
| 1149 | */ |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1150 | static int fork_main(int shmid, int offset) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1151 | { |
| 1152 | struct thread_data *td; |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1153 | void *data, *ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1154 | |
| 1155 | data = shmat(shmid, NULL, 0); |
| 1156 | if (data == (void *) -1) { |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1157 | int __err = errno; |
| 1158 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1159 | perror("shmat"); |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1160 | return __err; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | td = data + offset * sizeof(struct thread_data); |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1164 | ret = thread_main(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1165 | shmdt(data); |
Jens Axboe | 43d7680 | 2007-02-21 16:35:29 +0100 | [diff] [blame] | 1166 | return (int) (unsigned long) ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1167 | } |
| 1168 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1169 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1170 | * Run over the job map and reap the threads that have exited, if any. |
| 1171 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1172 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 1173 | { |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1174 | struct thread_data *td; |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1175 | int i, cputhreads, realthreads, pending, status, ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1176 | |
| 1177 | /* |
| 1178 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 1179 | */ |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1180 | realthreads = pending = cputhreads = 0; |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1181 | for_each_td(td, i) { |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1182 | int flags = 0; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1183 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 1184 | /* |
| 1185 | * ->io_ops is NULL for a thread that has closed its |
| 1186 | * io engine |
| 1187 | */ |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 1188 | if (td->io_ops && !strcmp(td->io_ops->name, "cpuio")) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1189 | cputhreads++; |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1190 | else |
| 1191 | realthreads++; |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1192 | |
Aaron Carroll | a1dedc1 | 2008-02-20 09:14:12 +0100 | [diff] [blame] | 1193 | if (!td->pid) { |
| 1194 | pending++; |
| 1195 | continue; |
| 1196 | } |
| 1197 | if (td->runstate == TD_REAPED) |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1198 | continue; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1199 | if (td->o.use_thread) { |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1200 | if (td->runstate == TD_EXITED) { |
| 1201 | td_set_runstate(td, TD_REAPED); |
| 1202 | goto reaped; |
| 1203 | } |
| 1204 | continue; |
| 1205 | } |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1206 | |
| 1207 | flags = WNOHANG; |
| 1208 | if (td->runstate == TD_EXITED) |
| 1209 | flags = 0; |
| 1210 | |
| 1211 | /* |
| 1212 | * check if someone quit or got killed in an unusual way |
| 1213 | */ |
| 1214 | ret = waitpid(td->pid, &status, flags); |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1215 | if (ret < 0) { |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1216 | if (errno == ECHILD) { |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1217 | log_err("fio: pid=%d disappeared %d\n", |
| 1218 | (int) td->pid, td->runstate); |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1219 | td_set_runstate(td, TD_REAPED); |
| 1220 | goto reaped; |
| 1221 | } |
| 1222 | perror("waitpid"); |
| 1223 | } else if (ret == td->pid) { |
| 1224 | if (WIFSIGNALED(status)) { |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1225 | int sig = WTERMSIG(status); |
| 1226 | |
Jens Axboe | d924494 | 2007-03-05 12:32:32 +0100 | [diff] [blame] | 1227 | if (sig != SIGQUIT) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1228 | log_err("fio: pid=%d, got signal=%d\n", |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1229 | (int) td->pid, sig); |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1230 | td_set_runstate(td, TD_REAPED); |
| 1231 | goto reaped; |
| 1232 | } |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1233 | if (WIFEXITED(status)) { |
| 1234 | if (WEXITSTATUS(status) && !td->error) |
| 1235 | td->error = WEXITSTATUS(status); |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1236 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1237 | td_set_runstate(td, TD_REAPED); |
| 1238 | goto reaped; |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1239 | } |
| 1240 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1241 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1242 | /* |
| 1243 | * thread is not dead, continue |
| 1244 | */ |
gurudas pai | e48676b | 2007-04-11 12:44:27 +0200 | [diff] [blame] | 1245 | pending++; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1246 | continue; |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1247 | reaped: |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1248 | (*nr_running)--; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1249 | (*m_rate) -= td->o.ratemin; |
| 1250 | (*t_rate) -= td->o.rate; |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1251 | if (!td->pid) |
| 1252 | pending--; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1253 | |
| 1254 | if (td->error) |
| 1255 | exit_value++; |
Jens Axboe | d3eeeab | 2008-04-06 12:19:05 +0200 | [diff] [blame] | 1256 | |
| 1257 | done_secs += mtime_since_now(&td->epoch) / 1000; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1258 | } |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1259 | |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1260 | if (*nr_running == cputhreads && !pending && realthreads) |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 1261 | terminate_threads(TERMINATE_ALL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1262 | } |
| 1263 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1264 | static void *gtod_thread_main(void *data) |
| 1265 | { |
| 1266 | fio_mutex_up(startup_mutex); |
| 1267 | |
| 1268 | /* |
| 1269 | * As long as we have jobs around, update the clock. It would be nice |
| 1270 | * to have some way of NOT hammering that CPU with gettimeofday(), |
| 1271 | * but I'm not sure what to use outside of a simple CPU nop to relax |
| 1272 | * it - we don't want to lose precision. |
| 1273 | */ |
| 1274 | while (threads) { |
| 1275 | fio_gtod_update(); |
| 1276 | nop; |
| 1277 | } |
| 1278 | |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | |
| 1282 | static int fio_start_gtod_thread(void) |
| 1283 | { |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1284 | int ret; |
| 1285 | |
| 1286 | ret = pthread_create(>od_thread, NULL, gtod_thread_main, NULL); |
| 1287 | if (ret) { |
| 1288 | log_err("Can't create gtod thread: %s\n", strerror(ret)); |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1289 | return 1; |
| 1290 | } |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1291 | |
| 1292 | ret = pthread_detach(gtod_thread); |
| 1293 | if (ret) { |
| 1294 | log_err("Can't detatch gtod thread: %s\n", strerror(ret)); |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1295 | return 1; |
| 1296 | } |
| 1297 | |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1298 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1299 | fio_mutex_down(startup_mutex); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1300 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1301 | return 0; |
| 1302 | } |
| 1303 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1304 | /* |
| 1305 | * Main function for kicking off and reaping jobs, as needed. |
| 1306 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1307 | static void run_threads(void) |
| 1308 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1309 | struct thread_data *td; |
| 1310 | unsigned long spent; |
| 1311 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1312 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 1313 | if (fio_pin_memory()) |
| 1314 | return; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1315 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1316 | if (fio_gtod_offload && fio_start_gtod_thread()) |
| 1317 | return; |
| 1318 | |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1319 | if (!terse_output) { |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1320 | printf("Starting "); |
| 1321 | if (nr_thread) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1322 | printf("%d thread%s", nr_thread, |
| 1323 | nr_thread > 1 ? "s" : ""); |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1324 | if (nr_process) { |
| 1325 | if (nr_thread) |
| 1326 | printf(" and "); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1327 | printf("%d process%s", nr_process, |
| 1328 | nr_process > 1 ? "es" : ""); |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1329 | } |
| 1330 | printf("\n"); |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1331 | fflush(stdout); |
| 1332 | } |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 1333 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 1334 | set_sig_handlers(); |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 1335 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1336 | todo = thread_number; |
| 1337 | nr_running = 0; |
| 1338 | nr_started = 0; |
| 1339 | m_rate = t_rate = 0; |
| 1340 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1341 | for_each_td(td, i) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1342 | print_status_init(td->thread_number - 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1343 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1344 | if (!td->o.create_serialize) { |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1345 | init_disk_util(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1346 | continue; |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1347 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1348 | |
| 1349 | /* |
| 1350 | * do file setup here so it happens sequentially, |
| 1351 | * we don't want X number of threads getting their |
| 1352 | * client data interspersed on disk |
| 1353 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1354 | if (setup_files(td)) { |
Jens Axboe | 5bf13a5 | 2007-02-17 01:51:47 +0100 | [diff] [blame] | 1355 | exit_value++; |
| 1356 | if (td->error) |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1357 | log_err("fio: pid=%d, err=%d/%s\n", |
| 1358 | (int) td->pid, td->error, td->verror); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1359 | td_set_runstate(td, TD_REAPED); |
| 1360 | todo--; |
Jens Axboe | c69e887 | 2008-03-01 18:50:48 +0100 | [diff] [blame] | 1361 | } else { |
| 1362 | struct fio_file *f; |
| 1363 | unsigned int i; |
| 1364 | |
| 1365 | /* |
| 1366 | * for sharing to work, each job must always open |
| 1367 | * its own files. so close them, if we opened them |
| 1368 | * for creation |
| 1369 | */ |
| 1370 | for_each_file(td, f, i) |
| 1371 | td_io_close_file(td, f); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1372 | } |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1373 | |
| 1374 | init_disk_util(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1375 | } |
| 1376 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1377 | set_genesis_time(); |
| 1378 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1379 | while (todo) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1380 | struct thread_data *map[MAX_JOBS]; |
| 1381 | struct timeval this_start; |
| 1382 | int this_jobs = 0, left; |
| 1383 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1384 | /* |
| 1385 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 1386 | */ |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1387 | for_each_td(td, i) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1388 | if (td->runstate != TD_NOT_CREATED) |
| 1389 | continue; |
| 1390 | |
| 1391 | /* |
| 1392 | * never got a chance to start, killed by other |
| 1393 | * thread for some reason |
| 1394 | */ |
| 1395 | if (td->terminate) { |
| 1396 | todo--; |
| 1397 | continue; |
| 1398 | } |
| 1399 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1400 | if (td->o.start_delay) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1401 | spent = mtime_since_genesis(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1402 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1403 | if (td->o.start_delay * 1000 > spent) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1404 | continue; |
| 1405 | } |
| 1406 | |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1407 | if (td->o.stonewall && (nr_started || nr_running)) { |
| 1408 | dprint(FD_PROCESS, "%s: stonewall wait\n", |
| 1409 | td->o.name); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1410 | break; |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1411 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1412 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1413 | /* |
| 1414 | * Set state to created. Thread will transition |
| 1415 | * to TD_INITIALIZED when it's done setting up. |
| 1416 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1417 | td_set_runstate(td, TD_CREATED); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1418 | map[this_jobs++] = td; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1419 | nr_started++; |
| 1420 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1421 | if (td->o.use_thread) { |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1422 | int ret; |
| 1423 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1424 | dprint(FD_PROCESS, "will pthread_create\n"); |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1425 | ret = pthread_create(&td->thread, NULL, |
| 1426 | thread_main, td); |
| 1427 | if (ret) { |
| 1428 | log_err("pthread_create: %s\n", |
| 1429 | strerror(ret)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1430 | nr_started--; |
Jens Axboe | c8bb6fa | 2007-03-12 11:03:04 +0100 | [diff] [blame] | 1431 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1432 | } |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1433 | ret = pthread_detach(td->thread); |
| 1434 | if (ret) |
| 1435 | log_err("pthread_detach: %s", |
| 1436 | strerror(ret)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1437 | } else { |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1438 | pid_t pid; |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1439 | dprint(FD_PROCESS, "will fork\n"); |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1440 | pid = fork(); |
| 1441 | if (!pid) { |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1442 | int ret = fork_main(shm_id, i); |
| 1443 | |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1444 | _exit(ret); |
| 1445 | } else if (i == fio_debug_jobno) |
| 1446 | *fio_debug_jobp = pid; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1447 | } |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1448 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1449 | fio_mutex_down(startup_mutex); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1450 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /* |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1454 | * Wait for the started threads to transition to |
| 1455 | * TD_INITIALIZED. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1456 | */ |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 1457 | fio_gettime(&this_start, NULL); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1458 | left = this_jobs; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1459 | while (left && !fio_abort) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1460 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
| 1461 | break; |
| 1462 | |
| 1463 | usleep(100000); |
| 1464 | |
| 1465 | for (i = 0; i < this_jobs; i++) { |
| 1466 | td = map[i]; |
| 1467 | if (!td) |
| 1468 | continue; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1469 | if (td->runstate == TD_INITIALIZED) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1470 | map[i] = NULL; |
| 1471 | left--; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1472 | } else if (td->runstate >= TD_EXITED) { |
| 1473 | map[i] = NULL; |
| 1474 | left--; |
| 1475 | todo--; |
| 1476 | nr_running++; /* work-around... */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | if (left) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1482 | log_err("fio: %d jobs failed to start\n", left); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1483 | for (i = 0; i < this_jobs; i++) { |
| 1484 | td = map[i]; |
| 1485 | if (!td) |
| 1486 | continue; |
| 1487 | kill(td->pid, SIGTERM); |
| 1488 | } |
| 1489 | break; |
| 1490 | } |
| 1491 | |
| 1492 | /* |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1493 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1494 | */ |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1495 | for_each_td(td, i) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1496 | if (td->runstate != TD_INITIALIZED) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1497 | continue; |
| 1498 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 1499 | if (in_ramp_time(td)) |
| 1500 | td_set_runstate(td, TD_RAMP); |
| 1501 | else |
| 1502 | td_set_runstate(td, TD_RUNNING); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1503 | nr_running++; |
| 1504 | nr_started--; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1505 | m_rate += td->o.ratemin; |
| 1506 | t_rate += td->o.rate; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1507 | todo--; |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1508 | fio_mutex_up(td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1512 | |
| 1513 | if (todo) |
| 1514 | usleep(100000); |
| 1515 | } |
| 1516 | |
| 1517 | while (nr_running) { |
| 1518 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1519 | usleep(10000); |
| 1520 | } |
| 1521 | |
| 1522 | update_io_ticks(); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 1523 | fio_unpin_memory(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1524 | } |
| 1525 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1526 | int main(int argc, char *argv[]) |
| 1527 | { |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1528 | long ps; |
| 1529 | |
Jens Axboe | 2e5cdb1 | 2008-03-01 18:52:49 +0100 | [diff] [blame] | 1530 | sinit(); |
| 1531 | |
Jens Axboe | dbe1125 | 2007-02-11 00:19:51 +0100 | [diff] [blame] | 1532 | /* |
| 1533 | * We need locale for number printing, if it isn't set then just |
| 1534 | * go with the US format. |
| 1535 | */ |
| 1536 | if (!getenv("LC_NUMERIC")) |
| 1537 | setlocale(LC_NUMERIC, "en_US"); |
| 1538 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1539 | if (parse_options(argc, argv)) |
| 1540 | return 1; |
| 1541 | |
Jens Axboe | 4b472fa | 2007-04-04 11:04:34 +0200 | [diff] [blame] | 1542 | if (!thread_number) |
| 1543 | return 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1544 | |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1545 | ps = sysconf(_SC_PAGESIZE); |
| 1546 | if (ps < 0) { |
| 1547 | log_err("Failed to get page size\n"); |
| 1548 | return 1; |
| 1549 | } |
| 1550 | |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 1551 | page_size = ps; |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1552 | page_mask = ps - 1; |
| 1553 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1554 | if (write_bw_log) { |
| 1555 | setup_log(&agg_io_log[DDIR_READ]); |
| 1556 | setup_log(&agg_io_log[DDIR_WRITE]); |
| 1557 | } |
| 1558 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1559 | startup_mutex = fio_mutex_init(0); |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 1560 | writeout_mutex = fio_mutex_init(1); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 1561 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1562 | set_genesis_time(); |
| 1563 | |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 1564 | status_timer_arm(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1565 | |
| 1566 | run_threads(); |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1567 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1568 | if (!fio_abort) { |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1569 | show_run_stats(); |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1570 | if (write_bw_log) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1571 | __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log"); |
| 1572 | __finish_log(agg_io_log[DDIR_WRITE], |
| 1573 | "agg-write_bw.log"); |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1574 | } |
| 1575 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1576 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1577 | fio_mutex_remove(startup_mutex); |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 1578 | fio_mutex_remove(writeout_mutex); |
Jens Axboe | 437c9b7 | 2007-02-13 18:20:37 +0100 | [diff] [blame] | 1579 | return exit_value; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1580 | } |