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