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