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