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