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