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 | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 29 | #include <assert.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/wait.h> |
| 32 | #include <sys/ipc.h> |
| 33 | #include <sys/shm.h> |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <sys/mman.h> |
| 36 | |
| 37 | #include "fio.h" |
| 38 | #include "os.h" |
| 39 | |
| 40 | #define MASK (4095) |
| 41 | |
| 42 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) |
| 43 | |
| 44 | int groupid = 0; |
| 45 | int thread_number = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 46 | int shm_id = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 47 | int temp_stall_ts; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 48 | |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 49 | static volatile int startup_sem; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 50 | static volatile int fio_abort; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 51 | |
| 52 | #define TERMINATE_ALL (-1) |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 53 | #define JOB_START_TIMEOUT (5 * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 54 | |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 55 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
| 56 | { |
| 57 | td->runstate = runstate; |
| 58 | } |
| 59 | |
| 60 | static void terminate_threads(int group_id, int forced_kill) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 61 | { |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 62 | struct thread_data *td; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 63 | int i; |
| 64 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 65 | for_each_td(td, i) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 66 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
| 67 | td->terminate = 1; |
| 68 | td->start_delay = 0; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 69 | if (forced_kill) |
| 70 | td_set_runstate(td, TD_EXITED); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void sig_handler(int sig) |
| 76 | { |
| 77 | switch (sig) { |
| 78 | case SIGALRM: |
| 79 | update_io_ticks(); |
| 80 | disk_util_timer_arm(); |
| 81 | print_thread_status(); |
| 82 | break; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 83 | case SIGSEGV: |
| 84 | fprintf(stderr, "fio: got segfault, aborting\n"); |
| 85 | terminate_threads(TERMINATE_ALL, 1); |
| 86 | fio_abort = 1; |
| 87 | exit(0); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 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 | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 91 | terminate_threads(TERMINATE_ALL, 0); |
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 | { |
| 101 | unsigned long spent; |
| 102 | unsigned long rate; |
| 103 | int ddir = td->ddir; |
| 104 | |
| 105 | /* |
| 106 | * allow a 2 second settle period in the beginning |
| 107 | */ |
| 108 | if (mtime_since(&td->start, now) < 2000) |
| 109 | return 0; |
| 110 | |
| 111 | /* |
| 112 | * if rate blocks is set, sample is running |
| 113 | */ |
| 114 | if (td->rate_bytes) { |
| 115 | spent = mtime_since(&td->lastrate, now); |
| 116 | if (spent < td->ratecycle) |
| 117 | return 0; |
| 118 | |
| 119 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; |
| 120 | if (rate < td->ratemin) { |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 121 | fprintf(f_out, "%s: min rate %u not met, got %luKiB/sec\n", td->name, td->ratemin, rate); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 122 | return 1; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | td->rate_bytes = td->this_io_bytes[ddir]; |
| 127 | memcpy(&td->lastrate, now, sizeof(*now)); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 132 | { |
| 133 | if (!td->timeout) |
| 134 | return 0; |
| 135 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) |
| 136 | return 1; |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 141 | static struct fio_file *get_next_file(struct thread_data *td) |
| 142 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 143 | unsigned int old_next_file = td->next_file; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 144 | struct fio_file *f; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 145 | |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 146 | do { |
| 147 | f = &td->files[td->next_file]; |
| 148 | |
| 149 | td->next_file++; |
| 150 | if (td->next_file >= td->nr_files) |
| 151 | td->next_file = 0; |
| 152 | |
| 153 | if (f->fd != -1) |
| 154 | break; |
| 155 | |
| 156 | f = NULL; |
| 157 | } while (td->next_file != old_next_file); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 158 | |
| 159 | return f; |
| 160 | } |
| 161 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 162 | /* |
| 163 | * When job exits, we can cancel the in-flight IO if we are using async |
| 164 | * io. Attempt to do so. |
| 165 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 166 | static void cleanup_pending_aio(struct thread_data *td) |
| 167 | { |
| 168 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 169 | struct list_head *entry, *n; |
| 170 | struct io_completion_data icd; |
| 171 | struct io_u *io_u; |
| 172 | int r; |
| 173 | |
| 174 | /* |
| 175 | * get immediately available events, if any |
| 176 | */ |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 177 | r = td_io_getevents(td, 0, td->cur_depth, &ts); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 178 | if (r > 0) { |
| 179 | icd.nr = r; |
| 180 | ios_completed(td, &icd); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * now cancel remaining active events |
| 185 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 186 | if (td->io_ops->cancel) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 187 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
| 188 | io_u = list_entry(entry, struct io_u, list); |
| 189 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 190 | r = td->io_ops->cancel(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 191 | if (!r) |
| 192 | put_io_u(td, io_u); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (td->cur_depth) { |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 197 | r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 198 | if (r > 0) { |
| 199 | icd.nr = r; |
| 200 | ios_completed(td, &icd); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 205 | /* |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 206 | * Helper to handle the final sync of a file. Works just like the normal |
| 207 | * io path, just does everything sync. |
| 208 | */ |
| 209 | static int fio_io_sync(struct thread_data *td, struct fio_file *f) |
| 210 | { |
| 211 | struct io_u *io_u = __get_io_u(td); |
| 212 | struct io_completion_data icd; |
| 213 | int ret; |
| 214 | |
| 215 | if (!io_u) |
| 216 | return 1; |
| 217 | |
| 218 | io_u->ddir = DDIR_SYNC; |
| 219 | io_u->file = f; |
| 220 | |
| 221 | if (td_io_prep(td, io_u)) { |
| 222 | put_io_u(td, io_u); |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | ret = td_io_queue(td, io_u); |
| 227 | if (ret) { |
Jens Axboe | 353a7e0 | 2006-10-25 09:16:07 +0200 | [diff] [blame] | 228 | td_verror(td, io_u->error); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 229 | put_io_u(td, io_u); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | ret = td_io_getevents(td, 1, td->cur_depth, NULL); |
| 234 | if (ret < 0) { |
Jens Axboe | 353a7e0 | 2006-10-25 09:16:07 +0200 | [diff] [blame] | 235 | td_verror(td, ret); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | icd.nr = ret; |
| 240 | ios_completed(td, &icd); |
| 241 | if (icd.error) { |
| 242 | td_verror(td, icd.error); |
| 243 | return 1; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 250 | * The main verify engine. Runs over the writes we previusly submitted, |
| 251 | * reads the blocks back in, and checks the crc/md5 of the data. |
| 252 | */ |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 253 | static void do_verify(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 254 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 255 | struct io_u *io_u, *v_io_u = NULL; |
| 256 | struct io_completion_data icd; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 257 | struct fio_file *f; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 258 | int ret, i; |
| 259 | |
| 260 | /* |
| 261 | * sync io first and invalidate cache, to make sure we really |
| 262 | * read from disk. |
| 263 | */ |
| 264 | for_each_file(td, f, i) { |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 265 | fio_io_sync(td, f); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 266 | file_invalidate_cache(td, f); |
| 267 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 268 | |
| 269 | td_set_runstate(td, TD_VERIFYING); |
| 270 | |
| 271 | do { |
| 272 | if (td->terminate) |
| 273 | break; |
| 274 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 275 | io_u = __get_io_u(td); |
| 276 | if (!io_u) |
| 277 | break; |
| 278 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 279 | if (runtime_exceeded(td, &io_u->start_time)) { |
| 280 | put_io_u(td, io_u); |
| 281 | break; |
| 282 | } |
| 283 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 284 | if (get_next_verify(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 285 | put_io_u(td, io_u); |
| 286 | break; |
| 287 | } |
| 288 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 289 | f = get_next_file(td); |
| 290 | if (!f) |
| 291 | break; |
| 292 | |
| 293 | io_u->file = f; |
| 294 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 295 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 296 | put_io_u(td, io_u); |
| 297 | break; |
| 298 | } |
| 299 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 300 | ret = td_io_queue(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 301 | if (ret) { |
Jens Axboe | 353a7e0 | 2006-10-25 09:16:07 +0200 | [diff] [blame] | 302 | td_verror(td, io_u->error); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 303 | put_io_u(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * we have one pending to verify, do that while |
| 309 | * we are doing io on the next one |
| 310 | */ |
| 311 | if (do_io_u_verify(td, &v_io_u)) |
| 312 | break; |
| 313 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 314 | ret = td_io_getevents(td, 1, 1, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 315 | if (ret != 1) { |
| 316 | if (ret < 0) |
| 317 | td_verror(td, ret); |
| 318 | break; |
| 319 | } |
| 320 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 321 | v_io_u = td->io_ops->event(td, 0); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 322 | icd.nr = 1; |
| 323 | icd.error = 0; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 324 | fio_gettime(&icd.time, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 325 | io_completed(td, v_io_u, &icd); |
| 326 | |
| 327 | if (icd.error) { |
| 328 | td_verror(td, icd.error); |
| 329 | put_io_u(td, v_io_u); |
| 330 | v_io_u = NULL; |
| 331 | break; |
| 332 | } |
| 333 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 334 | /* |
| 335 | * if we can't submit more io, we need to verify now |
| 336 | */ |
| 337 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) |
| 338 | break; |
| 339 | |
| 340 | } while (1); |
| 341 | |
| 342 | do_io_u_verify(td, &v_io_u); |
| 343 | |
| 344 | if (td->cur_depth) |
| 345 | cleanup_pending_aio(td); |
| 346 | |
| 347 | td_set_runstate(td, TD_RUNNING); |
| 348 | } |
| 349 | |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 350 | /* |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 351 | * Not really an io thread, all it does is burn CPU cycles in the specified |
| 352 | * manner. |
| 353 | */ |
| 354 | static void do_cpuio(struct thread_data *td) |
| 355 | { |
| 356 | struct timeval e; |
| 357 | int split = 100 / td->cpuload; |
| 358 | int i = 0; |
| 359 | |
| 360 | while (!td->terminate) { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 361 | fio_gettime(&e, NULL); |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 362 | |
| 363 | if (runtime_exceeded(td, &e)) |
| 364 | break; |
| 365 | |
| 366 | if (!(i % split)) |
| 367 | __usec_sleep(10000); |
| 368 | else |
| 369 | usec_sleep(td, 10000); |
| 370 | |
| 371 | i++; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 376 | * 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] | 377 | * and reaps them, checking for rate and errors along the way. |
| 378 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 379 | static void do_io(struct thread_data *td) |
| 380 | { |
| 381 | struct io_completion_data icd; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 382 | struct timeval s; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 383 | unsigned long usec; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 384 | struct fio_file *f; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 385 | int i, ret = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 386 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 387 | td_set_runstate(td, TD_RUNNING); |
| 388 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 389 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 390 | struct timespec *timeout; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 391 | int min_evts = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 392 | struct io_u *io_u; |
| 393 | |
| 394 | if (td->terminate) |
| 395 | break; |
| 396 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 397 | f = get_next_file(td); |
| 398 | if (!f) |
| 399 | break; |
| 400 | |
| 401 | io_u = get_io_u(td, f); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 402 | if (!io_u) |
| 403 | break; |
| 404 | |
| 405 | memcpy(&s, &io_u->start_time, sizeof(s)); |
| 406 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 407 | ret = td_io_queue(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 408 | if (ret) { |
Jens Axboe | 353a7e0 | 2006-10-25 09:16:07 +0200 | [diff] [blame] | 409 | td_verror(td, io_u->error); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 410 | put_io_u(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | |
| 414 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); |
| 415 | |
| 416 | if (td->cur_depth < td->iodepth) { |
Jens Axboe | 8d7ad8d | 2007-01-09 21:22:18 +0100 | [diff] [blame] | 417 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 418 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 419 | timeout = &ts; |
| 420 | min_evts = 0; |
| 421 | } else { |
| 422 | timeout = NULL; |
| 423 | min_evts = 1; |
| 424 | } |
| 425 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 426 | ret = td_io_getevents(td, min_evts, td->cur_depth, timeout); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 427 | if (ret < 0) { |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 428 | td_verror(td, ret); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 429 | break; |
| 430 | } else if (!ret) |
| 431 | continue; |
| 432 | |
| 433 | icd.nr = ret; |
| 434 | ios_completed(td, &icd); |
| 435 | if (icd.error) { |
| 436 | td_verror(td, icd.error); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * the rate is batched for now, it should work for batches |
| 442 | * of completions except the very first one which may look |
| 443 | * a little bursty |
| 444 | */ |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 445 | usec = utime_since(&s, &icd.time); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 446 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 447 | rate_throttle(td, usec, icd.bytes_done[td->ddir], td->ddir); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 448 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 449 | if (check_min_rate(td, &icd.time)) { |
Jens Axboe | 98aa62d | 2006-11-07 14:16:19 +0100 | [diff] [blame] | 450 | if (exitall_on_terminate) |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 451 | terminate_threads(td->groupid, 0); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 452 | td_verror(td, ENOMEM); |
| 453 | break; |
| 454 | } |
| 455 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 456 | if (runtime_exceeded(td, &icd.time)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 457 | break; |
| 458 | |
Jens Axboe | 9c1f743 | 2007-01-03 20:43:19 +0100 | [diff] [blame] | 459 | if (td->thinktime) { |
| 460 | unsigned long long b; |
| 461 | |
| 462 | b = td->io_blocks[0] + td->io_blocks[1]; |
Jens Axboe | 25309a3 | 2007-01-10 11:33:14 +0100 | [diff] [blame] | 463 | if (!(b % td->thinktime_blocks)) |
Jens Axboe | 9c1f743 | 2007-01-03 20:43:19 +0100 | [diff] [blame] | 464 | usec_sleep(td, td->thinktime); |
| 465 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 468 | if (!td->error) { |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 469 | if (td->cur_depth) |
| 470 | cleanup_pending_aio(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 471 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 472 | if (should_fsync(td) && td->end_fsync) { |
| 473 | td_set_runstate(td, TD_FSYNCING); |
| 474 | for_each_file(td, f, i) |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 475 | fio_io_sync(td, f); |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 476 | } |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 477 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 478 | } |
| 479 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 480 | static void cleanup_io_u(struct thread_data *td) |
| 481 | { |
| 482 | struct list_head *entry, *n; |
| 483 | struct io_u *io_u; |
| 484 | |
| 485 | list_for_each_safe(entry, n, &td->io_u_freelist) { |
| 486 | io_u = list_entry(entry, struct io_u, list); |
| 487 | |
| 488 | list_del(&io_u->list); |
| 489 | free(io_u); |
| 490 | } |
| 491 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 492 | free_io_mem(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Jens Axboe | 6b9cea2 | 2006-10-30 17:00:24 +0100 | [diff] [blame] | 495 | /* |
| 496 | * "randomly" fill the buffer contents |
| 497 | */ |
Jens Axboe | 66eeb29 | 2006-11-01 08:35:44 +0100 | [diff] [blame] | 498 | static void fill_rand_buf(struct io_u *io_u, int max_bs) |
Jens Axboe | 6b9cea2 | 2006-10-30 17:00:24 +0100 | [diff] [blame] | 499 | { |
Jens Axboe | 66eeb29 | 2006-11-01 08:35:44 +0100 | [diff] [blame] | 500 | int *ptr = io_u->buf; |
Jens Axboe | 6b9cea2 | 2006-10-30 17:00:24 +0100 | [diff] [blame] | 501 | |
| 502 | while ((void *) ptr - io_u->buf < max_bs) { |
| 503 | *ptr = rand() * 0x9e370001; |
| 504 | ptr++; |
| 505 | } |
| 506 | } |
| 507 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 508 | static int init_io_u(struct thread_data *td) |
| 509 | { |
| 510 | struct io_u *io_u; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 511 | unsigned int max_bs; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 512 | int i, max_units; |
| 513 | char *p; |
| 514 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 515 | if (td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 516 | return 0; |
| 517 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 518 | if (td->io_ops->flags & FIO_SYNCIO) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 519 | max_units = 1; |
| 520 | else |
| 521 | max_units = td->iodepth; |
| 522 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 523 | max_bs = max(td->max_bs[DDIR_READ], td->max_bs[DDIR_WRITE]); |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 524 | td->orig_buffer_size = max_bs * max_units; |
| 525 | |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 526 | if (td->mem_type == MEM_SHMHUGE || td->mem_type == MEM_MMAPHUGE) |
Jens Axboe | 56bb17f | 2006-12-20 20:27:36 +0100 | [diff] [blame] | 527 | td->orig_buffer_size = (td->orig_buffer_size + td->hugepage_size - 1) & ~(td->hugepage_size - 1); |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 528 | else |
| 529 | td->orig_buffer_size += MASK; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 530 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 531 | if (allocate_io_mem(td)) |
| 532 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 533 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 534 | p = ALIGN(td->orig_buffer); |
| 535 | for (i = 0; i < max_units; i++) { |
| 536 | io_u = malloc(sizeof(*io_u)); |
| 537 | memset(io_u, 0, sizeof(*io_u)); |
| 538 | INIT_LIST_HEAD(&io_u->list); |
| 539 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 540 | io_u->buf = p + max_bs * i; |
Jens Axboe | 6b9cea2 | 2006-10-30 17:00:24 +0100 | [diff] [blame] | 541 | if (td_write(td) || td_rw(td)) |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 542 | fill_rand_buf(io_u, max_bs); |
Jens Axboe | 6b9cea2 | 2006-10-30 17:00:24 +0100 | [diff] [blame] | 543 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 544 | io_u->index = i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 545 | list_add(&io_u->list, &td->io_u_freelist); |
| 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 551 | static int switch_ioscheduler(struct thread_data *td) |
| 552 | { |
| 553 | char tmp[256], tmp2[128]; |
| 554 | FILE *f; |
| 555 | int ret; |
| 556 | |
Jens Axboe | f48b467 | 2006-10-27 11:30:07 +0200 | [diff] [blame] | 557 | if (td->io_ops->flags & FIO_CPUIO) |
| 558 | return 0; |
| 559 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 560 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
| 561 | |
| 562 | f = fopen(tmp, "r+"); |
| 563 | if (!f) { |
| 564 | td_verror(td, errno); |
| 565 | return 1; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Set io scheduler. |
| 570 | */ |
| 571 | ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f); |
| 572 | if (ferror(f) || ret != 1) { |
| 573 | td_verror(td, errno); |
| 574 | fclose(f); |
| 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | rewind(f); |
| 579 | |
| 580 | /* |
| 581 | * Read back and check that the selected scheduler is now the default. |
| 582 | */ |
| 583 | ret = fread(tmp, 1, sizeof(tmp), f); |
| 584 | if (ferror(f) || ret < 0) { |
| 585 | td_verror(td, errno); |
| 586 | fclose(f); |
| 587 | return 1; |
| 588 | } |
| 589 | |
| 590 | sprintf(tmp2, "[%s]", td->ioscheduler); |
| 591 | if (!strstr(tmp, tmp2)) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 592 | log_err("fio: io scheduler %s not found\n", td->ioscheduler); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 593 | td_verror(td, EINVAL); |
| 594 | fclose(f); |
| 595 | return 1; |
| 596 | } |
| 597 | |
| 598 | fclose(f); |
| 599 | return 0; |
| 600 | } |
| 601 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 602 | static void clear_io_state(struct thread_data *td) |
| 603 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 604 | struct fio_file *f; |
| 605 | int i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 606 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 607 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
| 608 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 609 | td->zone_bytes = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 610 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 611 | for_each_file(td, f, i) { |
| 612 | f->last_pos = 0; |
| 613 | if (td->io_ops->flags & FIO_SYNCIO) |
| 614 | lseek(f->fd, SEEK_SET, 0); |
| 615 | |
| 616 | if (f->file_map) |
| 617 | memset(f->file_map, 0, f->num_maps * sizeof(long)); |
| 618 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 619 | } |
| 620 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 621 | /* |
| 622 | * Entry point for the thread based jobs. The process based jobs end up |
| 623 | * here as well, after a little setup. |
| 624 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 625 | static void *thread_main(void *data) |
| 626 | { |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 627 | unsigned long long runtime[2]; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 628 | struct thread_data *td = data; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 629 | |
| 630 | if (!td->use_thread) |
| 631 | setsid(); |
| 632 | |
| 633 | td->pid = getpid(); |
| 634 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 635 | INIT_LIST_HEAD(&td->io_u_freelist); |
| 636 | INIT_LIST_HEAD(&td->io_u_busylist); |
| 637 | INIT_LIST_HEAD(&td->io_hist_list); |
| 638 | INIT_LIST_HEAD(&td->io_log_list); |
| 639 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 640 | if (init_io_u(td)) |
| 641 | goto err; |
| 642 | |
| 643 | if (fio_setaffinity(td) == -1) { |
| 644 | td_verror(td, errno); |
| 645 | goto err; |
| 646 | } |
| 647 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 648 | if (td_io_init(td)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 649 | goto err; |
| 650 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 651 | if (init_iolog(td)) |
| 652 | goto err; |
| 653 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 654 | if (td->ioprio) { |
| 655 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 656 | td_verror(td, errno); |
| 657 | goto err; |
| 658 | } |
| 659 | } |
| 660 | |
Jens Axboe | 1056eaa | 2006-07-13 10:33:45 -0700 | [diff] [blame] | 661 | if (nice(td->nice) == -1) { |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 662 | td_verror(td, errno); |
| 663 | goto err; |
| 664 | } |
| 665 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 666 | if (init_random_state(td)) |
| 667 | goto err; |
| 668 | |
Jens Axboe | 56f9498 | 2006-10-24 09:33:42 +0200 | [diff] [blame] | 669 | if (td->ioscheduler && switch_ioscheduler(td)) |
| 670 | goto err; |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 671 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 672 | td_set_runstate(td, TD_INITIALIZED); |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 673 | fio_sem_up(&startup_sem); |
| 674 | fio_sem_down(&td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 675 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 676 | if (!td->create_serialize && setup_files(td)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 677 | goto err; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 678 | if (open_files(td)) |
| 679 | goto err; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 680 | |
Jens Axboe | 56f9498 | 2006-10-24 09:33:42 +0200 | [diff] [blame] | 681 | if (td->exec_prerun) |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 682 | system(td->exec_prerun); |
| 683 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 684 | fio_gettime(&td->epoch, NULL); |
Jens Axboe | 36dff96 | 2006-11-24 13:29:20 +0100 | [diff] [blame] | 685 | getrusage(RUSAGE_SELF, &td->ru_start); |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 686 | |
| 687 | runtime[0] = runtime[1] = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 688 | while (td->loops--) { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 689 | fio_gettime(&td->start, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 690 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); |
| 691 | |
| 692 | if (td->ratemin) |
| 693 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); |
| 694 | |
| 695 | clear_io_state(td); |
| 696 | prune_io_piece_log(td); |
| 697 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 698 | if (td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 699 | do_cpuio(td); |
| 700 | else |
| 701 | do_io(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 702 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 703 | runtime[td->ddir] += utime_since_now(&td->start); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 704 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 705 | runtime[td->ddir ^ 1] = runtime[td->ddir]; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 706 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 707 | if (td->error || td->terminate) |
| 708 | break; |
| 709 | |
| 710 | if (td->verify == VERIFY_NONE) |
| 711 | continue; |
| 712 | |
| 713 | clear_io_state(td); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 714 | fio_gettime(&td->start, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 715 | |
| 716 | do_verify(td); |
| 717 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 718 | runtime[DDIR_READ] += utime_since_now(&td->start); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 719 | |
| 720 | if (td->error || td->terminate) |
| 721 | break; |
| 722 | } |
| 723 | |
Jens Axboe | 36dff96 | 2006-11-24 13:29:20 +0100 | [diff] [blame] | 724 | update_rusage_stat(td); |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 725 | fio_gettime(&td->end_time, NULL); |
| 726 | td->runtime[0] = runtime[0] / 1000; |
| 727 | td->runtime[1] = runtime[1] / 1000; |
| 728 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 729 | if (td->bw_log) |
| 730 | finish_log(td, td->bw_log, "bw"); |
| 731 | if (td->slat_log) |
| 732 | finish_log(td, td->slat_log, "slat"); |
| 733 | if (td->clat_log) |
| 734 | finish_log(td, td->clat_log, "clat"); |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 735 | if (td->write_iolog_file) |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 736 | write_iolog_close(td); |
Jens Axboe | 56f9498 | 2006-10-24 09:33:42 +0200 | [diff] [blame] | 737 | if (td->exec_postrun) |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 738 | system(td->exec_postrun); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 739 | |
| 740 | if (exitall_on_terminate) |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 741 | terminate_threads(td->groupid, 0); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 742 | |
| 743 | err: |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 744 | close_files(td); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 745 | close_ioengine(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 746 | cleanup_io_u(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 747 | td_set_runstate(td, TD_EXITED); |
| 748 | return NULL; |
| 749 | |
| 750 | } |
| 751 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 752 | /* |
| 753 | * We cannot pass the td data into a forked process, so attach the td and |
| 754 | * pass it to the thread worker. |
| 755 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 756 | static void *fork_main(int shmid, int offset) |
| 757 | { |
| 758 | struct thread_data *td; |
| 759 | void *data; |
| 760 | |
| 761 | data = shmat(shmid, NULL, 0); |
| 762 | if (data == (void *) -1) { |
| 763 | perror("shmat"); |
| 764 | return NULL; |
| 765 | } |
| 766 | |
| 767 | td = data + offset * sizeof(struct thread_data); |
| 768 | thread_main(td); |
| 769 | shmdt(data); |
| 770 | return NULL; |
| 771 | } |
| 772 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 773 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 774 | * Run over the job map and reap the threads that have exited, if any. |
| 775 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 776 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 777 | { |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 778 | struct thread_data *td; |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 779 | int i, cputhreads, pending; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 780 | |
| 781 | /* |
| 782 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 783 | */ |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 784 | pending = cputhreads = 0; |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 785 | for_each_td(td, i) { |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 786 | /* |
| 787 | * ->io_ops is NULL for a thread that has closed its |
| 788 | * io engine |
| 789 | */ |
| 790 | if (td->io_ops && td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 791 | cputhreads++; |
| 792 | |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 793 | if (td->runstate != TD_EXITED) { |
| 794 | if (td->runstate < TD_RUNNING) |
| 795 | pending++; |
| 796 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 797 | continue; |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 798 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 799 | |
| 800 | td_set_runstate(td, TD_REAPED); |
| 801 | |
| 802 | if (td->use_thread) { |
| 803 | long ret; |
| 804 | |
| 805 | if (pthread_join(td->thread, (void *) &ret)) |
| 806 | perror("thread_join"); |
| 807 | } else |
| 808 | waitpid(td->pid, NULL, 0); |
| 809 | |
| 810 | (*nr_running)--; |
| 811 | (*m_rate) -= td->ratemin; |
| 812 | (*t_rate) -= td->rate; |
| 813 | } |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 814 | |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 815 | if (*nr_running == cputhreads && !pending) |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 816 | terminate_threads(TERMINATE_ALL, 0); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 819 | /* |
| 820 | * Main function for kicking off and reaping jobs, as needed. |
| 821 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 822 | static void run_threads(void) |
| 823 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 824 | struct thread_data *td; |
| 825 | unsigned long spent; |
| 826 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 827 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 828 | if (fio_pin_memory()) |
| 829 | return; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 830 | |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 831 | if (!terse_output) { |
| 832 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); |
| 833 | fflush(stdout); |
| 834 | } |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 835 | |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 836 | signal(SIGINT, sig_handler); |
| 837 | signal(SIGALRM, sig_handler); |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 838 | signal(SIGSEGV, sig_handler); |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 839 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 840 | todo = thread_number; |
| 841 | nr_running = 0; |
| 842 | nr_started = 0; |
| 843 | m_rate = t_rate = 0; |
| 844 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 845 | for_each_td(td, i) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 846 | print_status_init(td->thread_number - 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 847 | |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 848 | if (!td->create_serialize) { |
| 849 | init_disk_util(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 850 | continue; |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 851 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 852 | |
| 853 | /* |
| 854 | * do file setup here so it happens sequentially, |
| 855 | * we don't want X number of threads getting their |
| 856 | * client data interspersed on disk |
| 857 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 858 | if (setup_files(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 859 | td_set_runstate(td, TD_REAPED); |
| 860 | todo--; |
| 861 | } |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 862 | |
| 863 | init_disk_util(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 866 | while (todo) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 867 | struct thread_data *map[MAX_JOBS]; |
| 868 | struct timeval this_start; |
| 869 | int this_jobs = 0, left; |
| 870 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 871 | /* |
| 872 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 873 | */ |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 874 | for_each_td(td, i) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 875 | if (td->runstate != TD_NOT_CREATED) |
| 876 | continue; |
| 877 | |
| 878 | /* |
| 879 | * never got a chance to start, killed by other |
| 880 | * thread for some reason |
| 881 | */ |
| 882 | if (td->terminate) { |
| 883 | todo--; |
| 884 | continue; |
| 885 | } |
| 886 | |
| 887 | if (td->start_delay) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 888 | spent = mtime_since_genesis(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 889 | |
| 890 | if (td->start_delay * 1000 > spent) |
| 891 | continue; |
| 892 | } |
| 893 | |
| 894 | if (td->stonewall && (nr_started || nr_running)) |
| 895 | break; |
| 896 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 897 | /* |
| 898 | * Set state to created. Thread will transition |
| 899 | * to TD_INITIALIZED when it's done setting up. |
| 900 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 901 | td_set_runstate(td, TD_CREATED); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 902 | map[this_jobs++] = td; |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 903 | fio_sem_init(&startup_sem, 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 904 | nr_started++; |
| 905 | |
| 906 | if (td->use_thread) { |
| 907 | if (pthread_create(&td->thread, NULL, thread_main, td)) { |
| 908 | perror("thread_create"); |
| 909 | nr_started--; |
| 910 | } |
| 911 | } else { |
| 912 | if (fork()) |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 913 | fio_sem_down(&startup_sem); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 914 | else { |
| 915 | fork_main(shm_id, i); |
| 916 | exit(0); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /* |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 922 | * Wait for the started threads to transition to |
| 923 | * TD_INITIALIZED. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 924 | */ |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 925 | fio_gettime(&this_start, NULL); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 926 | left = this_jobs; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 927 | while (left && !fio_abort) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 928 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
| 929 | break; |
| 930 | |
| 931 | usleep(100000); |
| 932 | |
| 933 | for (i = 0; i < this_jobs; i++) { |
| 934 | td = map[i]; |
| 935 | if (!td) |
| 936 | continue; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 937 | if (td->runstate == TD_INITIALIZED) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 938 | map[i] = NULL; |
| 939 | left--; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 940 | } else if (td->runstate >= TD_EXITED) { |
| 941 | map[i] = NULL; |
| 942 | left--; |
| 943 | todo--; |
| 944 | nr_running++; /* work-around... */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | if (left) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 950 | log_err("fio: %d jobs failed to start\n", left); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 951 | for (i = 0; i < this_jobs; i++) { |
| 952 | td = map[i]; |
| 953 | if (!td) |
| 954 | continue; |
| 955 | kill(td->pid, SIGTERM); |
| 956 | } |
| 957 | break; |
| 958 | } |
| 959 | |
| 960 | /* |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 961 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 962 | */ |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 963 | for_each_td(td, i) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 964 | if (td->runstate != TD_INITIALIZED) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 965 | continue; |
| 966 | |
| 967 | td_set_runstate(td, TD_RUNNING); |
| 968 | nr_running++; |
| 969 | nr_started--; |
| 970 | m_rate += td->ratemin; |
| 971 | t_rate += td->rate; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 972 | todo--; |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 973 | fio_sem_up(&td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 977 | |
| 978 | if (todo) |
| 979 | usleep(100000); |
| 980 | } |
| 981 | |
| 982 | while (nr_running) { |
| 983 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 984 | usleep(10000); |
| 985 | } |
| 986 | |
| 987 | update_io_ticks(); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 988 | fio_unpin_memory(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 991 | int main(int argc, char *argv[]) |
| 992 | { |
| 993 | if (parse_options(argc, argv)) |
| 994 | return 1; |
| 995 | |
| 996 | if (!thread_number) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 997 | log_err("Nothing to do\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 998 | return 1; |
| 999 | } |
| 1000 | |
| 1001 | disk_util_timer_arm(); |
| 1002 | |
| 1003 | run_threads(); |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame^] | 1004 | |
| 1005 | if (!fio_abort) |
| 1006 | show_run_stats(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1007 | |
| 1008 | return 0; |
| 1009 | } |