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