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