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