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> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <string.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 24 | #include <signal.h> |
| 25 | #include <time.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 26 | #include <assert.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/wait.h> |
| 29 | #include <sys/ipc.h> |
| 30 | #include <sys/shm.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <sys/mman.h> |
| 33 | |
| 34 | #include "fio.h" |
| 35 | #include "os.h" |
| 36 | |
| 37 | #define MASK (4095) |
| 38 | |
| 39 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) |
| 40 | |
| 41 | int groupid = 0; |
| 42 | int thread_number = 0; |
| 43 | static char run_str[MAX_JOBS + 1]; |
| 44 | int shm_id = 0; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 45 | static struct timeval genesis; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 46 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 47 | static void print_thread_status(void); |
| 48 | |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 49 | extern unsigned long long mlock_size; |
| 50 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 51 | /* |
| 52 | * thread life cycle |
| 53 | */ |
| 54 | enum { |
| 55 | TD_NOT_CREATED = 0, |
| 56 | TD_CREATED, |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 57 | TD_INITIALIZED, |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 58 | TD_RUNNING, |
| 59 | TD_VERIFYING, |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 60 | TD_FSYNCING, |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 61 | TD_EXITED, |
| 62 | TD_REAPED, |
| 63 | }; |
| 64 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 65 | #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] | 66 | |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 67 | static volatile int startup_sem; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 68 | |
| 69 | #define TERMINATE_ALL (-1) |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 70 | #define JOB_START_TIMEOUT (5 * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 71 | |
| 72 | static void terminate_threads(int group_id) |
| 73 | { |
| 74 | int i; |
| 75 | |
| 76 | for (i = 0; i < thread_number; i++) { |
| 77 | struct thread_data *td = &threads[i]; |
| 78 | |
| 79 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
| 80 | td->terminate = 1; |
| 81 | td->start_delay = 0; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void sig_handler(int sig) |
| 87 | { |
| 88 | switch (sig) { |
| 89 | case SIGALRM: |
| 90 | update_io_ticks(); |
| 91 | disk_util_timer_arm(); |
| 92 | print_thread_status(); |
| 93 | break; |
| 94 | default: |
| 95 | printf("\nfio: terminating on signal\n"); |
| 96 | fflush(stdout); |
| 97 | terminate_threads(TERMINATE_ALL); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 102 | static int random_map_free(struct thread_data *td, unsigned long long block) |
| 103 | { |
| 104 | unsigned int idx = RAND_MAP_IDX(td, block); |
| 105 | unsigned int bit = RAND_MAP_BIT(td, block); |
| 106 | |
| 107 | return (td->file_map[idx] & (1UL << bit)) == 0; |
| 108 | } |
| 109 | |
| 110 | static int get_next_free_block(struct thread_data *td, unsigned long long *b) |
| 111 | { |
| 112 | int i; |
| 113 | |
| 114 | *b = 0; |
| 115 | i = 0; |
| 116 | while ((*b) * td->min_bs < td->io_size) { |
| 117 | if (td->file_map[i] != -1UL) { |
| 118 | *b += ffz(td->file_map[i]); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | *b += BLOCKS_PER_MAP; |
| 123 | i++; |
| 124 | } |
| 125 | |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | static void mark_random_map(struct thread_data *td, struct io_u *io_u) |
| 130 | { |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 131 | unsigned long long block = io_u->offset / (unsigned long long) td->min_bs; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 132 | unsigned int blocks = 0; |
| 133 | |
| 134 | while (blocks < (io_u->buflen / td->min_bs)) { |
| 135 | unsigned int idx, bit; |
| 136 | |
| 137 | if (!random_map_free(td, block)) |
| 138 | break; |
| 139 | |
| 140 | idx = RAND_MAP_IDX(td, block); |
| 141 | bit = RAND_MAP_BIT(td, block); |
| 142 | |
| 143 | assert(idx < td->num_maps); |
| 144 | |
| 145 | td->file_map[idx] |= (1UL << bit); |
| 146 | block++; |
| 147 | blocks++; |
| 148 | } |
| 149 | |
| 150 | if ((blocks * td->min_bs) < io_u->buflen) |
| 151 | io_u->buflen = blocks * td->min_bs; |
| 152 | } |
| 153 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 154 | static int get_next_offset(struct thread_data *td, unsigned long long *offset) |
| 155 | { |
| 156 | unsigned long long b, rb; |
| 157 | long r; |
| 158 | |
| 159 | if (!td->sequential) { |
Jens Axboe | 085227a | 2006-06-01 15:54:09 -0700 | [diff] [blame] | 160 | unsigned long long max_blocks = td->io_size / td->min_bs; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 161 | int loops = 50; |
| 162 | |
| 163 | do { |
Jens Axboe | 6dfd46b | 2006-06-07 13:57:06 +0200 | [diff] [blame] | 164 | r = os_random_long(&td->random_state); |
Jens Axboe | 085227a | 2006-06-01 15:54:09 -0700 | [diff] [blame] | 165 | b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0)); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 166 | rb = b + (td->file_offset / td->min_bs); |
| 167 | loops--; |
| 168 | } while (!random_map_free(td, rb) && loops); |
| 169 | |
| 170 | if (!loops) { |
| 171 | if (get_next_free_block(td, &b)) |
| 172 | return 1; |
| 173 | } |
| 174 | } else |
| 175 | b = td->last_pos / td->min_bs; |
| 176 | |
| 177 | *offset = (b * td->min_bs) + td->file_offset; |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 178 | if (*offset > td->real_file_size) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 179 | return 1; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static unsigned int get_next_buflen(struct thread_data *td) |
| 185 | { |
| 186 | unsigned int buflen; |
| 187 | long r; |
| 188 | |
| 189 | if (td->min_bs == td->max_bs) |
| 190 | buflen = td->min_bs; |
| 191 | else { |
Jens Axboe | 6dfd46b | 2006-06-07 13:57:06 +0200 | [diff] [blame] | 192 | r = os_random_long(&td->bsrange_state); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 193 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
| 194 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); |
| 195 | } |
| 196 | |
| 197 | if (buflen > td->io_size - td->this_io_bytes[td->ddir]) |
| 198 | buflen = td->io_size - td->this_io_bytes[td->ddir]; |
| 199 | |
| 200 | return buflen; |
| 201 | } |
| 202 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 203 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
| 204 | { |
| 205 | unsigned long spent; |
| 206 | unsigned long rate; |
| 207 | int ddir = td->ddir; |
| 208 | |
| 209 | /* |
| 210 | * allow a 2 second settle period in the beginning |
| 211 | */ |
| 212 | if (mtime_since(&td->start, now) < 2000) |
| 213 | return 0; |
| 214 | |
| 215 | /* |
| 216 | * if rate blocks is set, sample is running |
| 217 | */ |
| 218 | if (td->rate_bytes) { |
| 219 | spent = mtime_since(&td->lastrate, now); |
| 220 | if (spent < td->ratecycle) |
| 221 | return 0; |
| 222 | |
| 223 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; |
| 224 | if (rate < td->ratemin) { |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 225 | 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] | 226 | if (rate_quit) |
| 227 | terminate_threads(td->groupid); |
| 228 | return 1; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | td->rate_bytes = td->this_io_bytes[ddir]; |
| 233 | memcpy(&td->lastrate, now, sizeof(*now)); |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 238 | { |
| 239 | if (!td->timeout) |
| 240 | return 0; |
| 241 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) |
| 242 | return 1; |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static void fill_random_bytes(struct thread_data *td, |
| 248 | unsigned char *p, unsigned int len) |
| 249 | { |
| 250 | unsigned int todo; |
| 251 | double r; |
| 252 | |
| 253 | while (len) { |
Jens Axboe | 6dfd46b | 2006-06-07 13:57:06 +0200 | [diff] [blame] | 254 | r = os_random_double(&td->verify_state); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * lrand48_r seems to be broken and only fill the bottom |
| 258 | * 32-bits, even on 64-bit archs with 64-bit longs |
| 259 | */ |
| 260 | todo = sizeof(r); |
| 261 | if (todo > len) |
| 262 | todo = len; |
| 263 | |
| 264 | memcpy(p, &r, todo); |
| 265 | |
| 266 | len -= todo; |
| 267 | p += todo; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static void hexdump(void *buffer, int len) |
| 272 | { |
| 273 | unsigned char *p = buffer; |
| 274 | int i; |
| 275 | |
| 276 | for (i = 0; i < len; i++) |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 277 | fprintf(f_out, "%02x", p[i]); |
| 278 | fprintf(f_out, "\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) |
| 282 | { |
| 283 | unsigned char *p = (unsigned char *) io_u->buf; |
| 284 | unsigned long c; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 285 | |
| 286 | p += sizeof(*hdr); |
| 287 | c = crc32(p, hdr->len - sizeof(*hdr)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 288 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 289 | if (c != hdr->crc32) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 290 | log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
| 291 | log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c); |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 292 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 295 | return 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) |
| 299 | { |
| 300 | unsigned char *p = (unsigned char *) io_u->buf; |
| 301 | struct md5_ctx md5_ctx; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 302 | |
| 303 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
| 304 | p += sizeof(*hdr); |
| 305 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); |
| 306 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 307 | if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 308 | log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 309 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); |
| 310 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 311 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 314 | return 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static int verify_io_u(struct io_u *io_u) |
| 318 | { |
| 319 | struct verify_header *hdr = (struct verify_header *) io_u->buf; |
| 320 | int ret; |
| 321 | |
| 322 | if (hdr->fio_magic != FIO_HDR_MAGIC) |
| 323 | return 1; |
| 324 | |
| 325 | if (hdr->verify_type == VERIFY_MD5) |
| 326 | ret = verify_io_u_md5(hdr, io_u); |
| 327 | else if (hdr->verify_type == VERIFY_CRC32) |
| 328 | ret = verify_io_u_crc32(hdr, io_u); |
| 329 | else { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 330 | log_err("Bad verify type %d\n", hdr->verify_type); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 331 | ret = 1; |
| 332 | } |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
| 338 | { |
| 339 | hdr->crc32 = crc32(p, len); |
| 340 | } |
| 341 | |
| 342 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
| 343 | { |
| 344 | struct md5_ctx md5_ctx; |
| 345 | |
| 346 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
| 347 | md5_update(&md5_ctx, p, len); |
| 348 | memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); |
| 349 | } |
| 350 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 351 | static int get_rw_ddir(struct thread_data *td) |
| 352 | { |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 353 | if (td_rw(td)) { |
| 354 | struct timeval now; |
| 355 | unsigned long elapsed; |
| 356 | |
| 357 | gettimeofday(&now, NULL); |
| 358 | elapsed = mtime_since_now(&td->rwmix_switch); |
| 359 | |
| 360 | /* |
| 361 | * Check if it's time to seed a new data direction. |
| 362 | */ |
| 363 | if (elapsed >= td->rwmixcycle) { |
Jens Axboe | c1ee2ca | 2006-06-07 22:42:37 +0200 | [diff] [blame] | 364 | int v; |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 365 | long r; |
| 366 | |
Jens Axboe | c1ee2ca | 2006-06-07 22:42:37 +0200 | [diff] [blame] | 367 | r = os_random_long(&td->rwmix_state); |
| 368 | v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0))); |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 369 | if (v < td->rwmixread) |
| 370 | td->rwmix_ddir = DDIR_READ; |
| 371 | else |
| 372 | td->rwmix_ddir = DDIR_WRITE; |
| 373 | memcpy(&td->rwmix_switch, &now, sizeof(now)); |
| 374 | } |
| 375 | return td->rwmix_ddir; |
| 376 | } else if (td_read(td)) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 377 | return DDIR_READ; |
| 378 | else |
| 379 | return DDIR_WRITE; |
| 380 | } |
| 381 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 382 | /* |
| 383 | * fill body of io_u->buf with random data and add a header with the |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 384 | * crc32 or md5 sum of that data. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 385 | */ |
| 386 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) |
| 387 | { |
| 388 | unsigned char *p = (unsigned char *) io_u->buf; |
| 389 | struct verify_header hdr; |
| 390 | |
| 391 | hdr.fio_magic = FIO_HDR_MAGIC; |
| 392 | hdr.len = io_u->buflen; |
| 393 | p += sizeof(hdr); |
| 394 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); |
| 395 | |
| 396 | if (td->verify == VERIFY_MD5) { |
| 397 | fill_md5(&hdr, p, io_u->buflen - sizeof(hdr)); |
| 398 | hdr.verify_type = VERIFY_MD5; |
| 399 | } else { |
| 400 | fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr)); |
| 401 | hdr.verify_type = VERIFY_CRC32; |
| 402 | } |
| 403 | |
| 404 | memcpy(io_u->buf, &hdr, sizeof(hdr)); |
| 405 | } |
| 406 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 407 | static int td_io_prep(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 408 | { |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 409 | if (td->io_prep && td->io_prep(td, io_u)) |
| 410 | return 1; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 415 | void put_io_u(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 416 | { |
| 417 | list_del(&io_u->list); |
| 418 | list_add(&io_u->list, &td->io_u_freelist); |
| 419 | td->cur_depth--; |
| 420 | } |
| 421 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 422 | static int fill_io_u(struct thread_data *td, struct io_u *io_u) |
| 423 | { |
| 424 | /* |
| 425 | * If using an iolog, grab next piece if any available. |
| 426 | */ |
| 427 | if (td->read_iolog) |
| 428 | return read_iolog_get(td, io_u); |
| 429 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 430 | /* |
| 431 | * No log, let the seq/rand engine retrieve the next position. |
| 432 | */ |
| 433 | if (!get_next_offset(td, &io_u->offset)) { |
| 434 | io_u->buflen = get_next_buflen(td); |
| 435 | |
| 436 | if (io_u->buflen) { |
| 437 | io_u->ddir = get_rw_ddir(td); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 438 | |
| 439 | /* |
| 440 | * If using a write iolog, store this entry. |
| 441 | */ |
| 442 | if (td->write_iolog) |
| 443 | write_iolog_put(td, io_u); |
| 444 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return 1; |
| 450 | } |
| 451 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 452 | #define queue_full(td) list_empty(&(td)->io_u_freelist) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 453 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 454 | struct io_u *__get_io_u(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 455 | { |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 456 | struct io_u *io_u = NULL; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 457 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 458 | if (!queue_full(td)) { |
| 459 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 460 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 461 | io_u->error = 0; |
| 462 | io_u->resid = 0; |
| 463 | list_del(&io_u->list); |
| 464 | list_add(&io_u->list, &td->io_u_busylist); |
| 465 | td->cur_depth++; |
| 466 | } |
| 467 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 468 | return io_u; |
| 469 | } |
| 470 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 471 | static struct io_u *get_io_u(struct thread_data *td) |
| 472 | { |
| 473 | struct io_u *io_u; |
| 474 | |
| 475 | io_u = __get_io_u(td); |
| 476 | if (!io_u) |
| 477 | return NULL; |
| 478 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 479 | if (td->zone_bytes >= td->zone_size) { |
| 480 | td->zone_bytes = 0; |
| 481 | td->last_pos += td->zone_skip; |
| 482 | } |
| 483 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 484 | if (fill_io_u(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 485 | put_io_u(td, io_u); |
| 486 | return NULL; |
| 487 | } |
| 488 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 489 | if (io_u->buflen + io_u->offset > td->real_file_size) |
| 490 | io_u->buflen = td->real_file_size - io_u->offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 491 | |
| 492 | if (!io_u->buflen) { |
| 493 | put_io_u(td, io_u); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 497 | if (!td->read_iolog && !td->sequential) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 498 | mark_random_map(td, io_u); |
| 499 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 500 | td->last_pos += io_u->buflen; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 501 | |
| 502 | if (td->verify != VERIFY_NONE) |
| 503 | populate_io_u(td, io_u); |
| 504 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 505 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 506 | put_io_u(td, io_u); |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | gettimeofday(&io_u->start_time, NULL); |
| 511 | return io_u; |
| 512 | } |
| 513 | |
| 514 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
| 515 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 516 | td->runstate = runstate; |
| 517 | } |
| 518 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 519 | static int get_next_verify(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 520 | { |
| 521 | struct io_piece *ipo; |
| 522 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 523 | if (!list_empty(&td->io_hist_list)) { |
| 524 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 525 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 526 | list_del(&ipo->list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 527 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 528 | io_u->offset = ipo->offset; |
| 529 | io_u->buflen = ipo->len; |
| 530 | io_u->ddir = DDIR_READ; |
| 531 | free(ipo); |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 538 | static int sync_td(struct thread_data *td) |
| 539 | { |
| 540 | if (td->io_sync) |
| 541 | return td->io_sync(td); |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int io_u_getevents(struct thread_data *td, int min, int max, |
| 547 | struct timespec *t) |
| 548 | { |
| 549 | return td->io_getevents(td, min, max, t); |
| 550 | } |
| 551 | |
| 552 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) |
| 553 | { |
| 554 | gettimeofday(&io_u->issue_time, NULL); |
| 555 | |
| 556 | return td->io_queue(td, io_u); |
| 557 | } |
| 558 | |
| 559 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
| 560 | |
| 561 | static void io_completed(struct thread_data *td, struct io_u *io_u, |
| 562 | struct io_completion_data *icd) |
| 563 | { |
| 564 | struct timeval e; |
| 565 | unsigned long msec; |
| 566 | |
| 567 | gettimeofday(&e, NULL); |
| 568 | |
| 569 | if (!io_u->error) { |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 570 | unsigned int bytes = io_u->buflen - io_u->resid; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 571 | const int idx = io_u->ddir; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 572 | |
| 573 | td->io_blocks[idx]++; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 574 | td->io_bytes[idx] += bytes; |
| 575 | td->zone_bytes += bytes; |
| 576 | td->this_io_bytes[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 577 | |
| 578 | msec = mtime_since(&io_u->issue_time, &e); |
| 579 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 580 | add_clat_sample(td, idx, msec); |
| 581 | add_bw_sample(td, idx); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 582 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 583 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 584 | log_io_piece(td, io_u); |
| 585 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 586 | icd->bytes_done[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 587 | } else |
| 588 | icd->error = io_u->error; |
| 589 | } |
| 590 | |
| 591 | static void ios_completed(struct thread_data *td,struct io_completion_data *icd) |
| 592 | { |
| 593 | struct io_u *io_u; |
| 594 | int i; |
| 595 | |
| 596 | icd->error = 0; |
| 597 | icd->bytes_done[0] = icd->bytes_done[1] = 0; |
| 598 | |
| 599 | for (i = 0; i < icd->nr; i++) { |
| 600 | io_u = td->io_event(td, i); |
| 601 | |
| 602 | io_completed(td, io_u, icd); |
| 603 | put_io_u(td, io_u); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static void cleanup_pending_aio(struct thread_data *td) |
| 608 | { |
| 609 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 610 | struct list_head *entry, *n; |
| 611 | struct io_completion_data icd; |
| 612 | struct io_u *io_u; |
| 613 | int r; |
| 614 | |
| 615 | /* |
| 616 | * get immediately available events, if any |
| 617 | */ |
| 618 | r = io_u_getevents(td, 0, td->cur_depth, &ts); |
| 619 | if (r > 0) { |
| 620 | icd.nr = r; |
| 621 | ios_completed(td, &icd); |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * now cancel remaining active events |
| 626 | */ |
| 627 | if (td->io_cancel) { |
| 628 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
| 629 | io_u = list_entry(entry, struct io_u, list); |
| 630 | |
| 631 | r = td->io_cancel(td, io_u); |
| 632 | if (!r) |
| 633 | put_io_u(td, io_u); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (td->cur_depth) { |
| 638 | r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL); |
| 639 | if (r > 0) { |
| 640 | icd.nr = r; |
| 641 | ios_completed(td, &icd); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | static int do_io_u_verify(struct thread_data *td, struct io_u **io_u) |
| 647 | { |
| 648 | struct io_u *v_io_u = *io_u; |
| 649 | int ret = 0; |
| 650 | |
| 651 | if (v_io_u) { |
| 652 | ret = verify_io_u(v_io_u); |
| 653 | put_io_u(td, v_io_u); |
| 654 | *io_u = NULL; |
| 655 | } |
| 656 | |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static void do_verify(struct thread_data *td) |
| 661 | { |
| 662 | struct timeval t; |
| 663 | struct io_u *io_u, *v_io_u = NULL; |
| 664 | struct io_completion_data icd; |
| 665 | int ret; |
| 666 | |
| 667 | td_set_runstate(td, TD_VERIFYING); |
| 668 | |
| 669 | do { |
| 670 | if (td->terminate) |
| 671 | break; |
| 672 | |
| 673 | gettimeofday(&t, NULL); |
| 674 | if (runtime_exceeded(td, &t)) |
| 675 | break; |
| 676 | |
| 677 | io_u = __get_io_u(td); |
| 678 | if (!io_u) |
| 679 | break; |
| 680 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 681 | if (get_next_verify(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 682 | put_io_u(td, io_u); |
| 683 | break; |
| 684 | } |
| 685 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 686 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 687 | put_io_u(td, io_u); |
| 688 | break; |
| 689 | } |
| 690 | |
| 691 | ret = io_u_queue(td, io_u); |
| 692 | if (ret) { |
| 693 | put_io_u(td, io_u); |
| 694 | td_verror(td, ret); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * we have one pending to verify, do that while |
| 700 | * we are doing io on the next one |
| 701 | */ |
| 702 | if (do_io_u_verify(td, &v_io_u)) |
| 703 | break; |
| 704 | |
| 705 | ret = io_u_getevents(td, 1, 1, NULL); |
| 706 | if (ret != 1) { |
| 707 | if (ret < 0) |
| 708 | td_verror(td, ret); |
| 709 | break; |
| 710 | } |
| 711 | |
| 712 | v_io_u = td->io_event(td, 0); |
| 713 | icd.nr = 1; |
| 714 | icd.error = 0; |
| 715 | io_completed(td, v_io_u, &icd); |
| 716 | |
| 717 | if (icd.error) { |
| 718 | td_verror(td, icd.error); |
| 719 | put_io_u(td, v_io_u); |
| 720 | v_io_u = NULL; |
| 721 | break; |
| 722 | } |
| 723 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 724 | /* |
| 725 | * if we can't submit more io, we need to verify now |
| 726 | */ |
| 727 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) |
| 728 | break; |
| 729 | |
| 730 | } while (1); |
| 731 | |
| 732 | do_io_u_verify(td, &v_io_u); |
| 733 | |
| 734 | if (td->cur_depth) |
| 735 | cleanup_pending_aio(td); |
| 736 | |
| 737 | td_set_runstate(td, TD_RUNNING); |
| 738 | } |
| 739 | |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 740 | /* |
| 741 | * Main IO worker functions. It retrieves io_u's to process and queues |
| 742 | * and reaps them, checking for rate and errors along the way. |
| 743 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 744 | static void do_io(struct thread_data *td) |
| 745 | { |
| 746 | struct io_completion_data icd; |
| 747 | struct timeval s, e; |
| 748 | unsigned long usec; |
| 749 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 750 | td_set_runstate(td, TD_RUNNING); |
| 751 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 752 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
| 753 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 754 | struct timespec *timeout; |
| 755 | int ret, min_evts = 0; |
| 756 | struct io_u *io_u; |
| 757 | |
| 758 | if (td->terminate) |
| 759 | break; |
| 760 | |
| 761 | io_u = get_io_u(td); |
| 762 | if (!io_u) |
| 763 | break; |
| 764 | |
| 765 | memcpy(&s, &io_u->start_time, sizeof(s)); |
| 766 | |
| 767 | ret = io_u_queue(td, io_u); |
| 768 | if (ret) { |
| 769 | put_io_u(td, io_u); |
| 770 | td_verror(td, ret); |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); |
| 775 | |
| 776 | if (td->cur_depth < td->iodepth) { |
| 777 | timeout = &ts; |
| 778 | min_evts = 0; |
| 779 | } else { |
| 780 | timeout = NULL; |
| 781 | min_evts = 1; |
| 782 | } |
| 783 | |
| 784 | ret = io_u_getevents(td, min_evts, td->cur_depth, timeout); |
| 785 | if (ret < 0) { |
| 786 | td_verror(td, ret); |
| 787 | break; |
| 788 | } else if (!ret) |
| 789 | continue; |
| 790 | |
| 791 | icd.nr = ret; |
| 792 | ios_completed(td, &icd); |
| 793 | if (icd.error) { |
| 794 | td_verror(td, icd.error); |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * the rate is batched for now, it should work for batches |
| 800 | * of completions except the very first one which may look |
| 801 | * a little bursty |
| 802 | */ |
| 803 | gettimeofday(&e, NULL); |
| 804 | usec = utime_since(&s, &e); |
| 805 | |
| 806 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); |
| 807 | |
| 808 | if (check_min_rate(td, &e)) { |
| 809 | td_verror(td, ENOMEM); |
| 810 | break; |
| 811 | } |
| 812 | |
| 813 | if (runtime_exceeded(td, &e)) |
| 814 | break; |
| 815 | |
| 816 | if (td->thinktime) |
| 817 | usec_sleep(td, td->thinktime); |
| 818 | |
| 819 | if (should_fsync(td) && td->fsync_blocks && |
| 820 | (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0) |
| 821 | sync_td(td); |
| 822 | } |
| 823 | |
| 824 | if (td->cur_depth) |
| 825 | cleanup_pending_aio(td); |
| 826 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 827 | if (should_fsync(td) && td->end_fsync) { |
| 828 | td_set_runstate(td, TD_FSYNCING); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 829 | sync_td(td); |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 830 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static void cleanup_io(struct thread_data *td) |
| 834 | { |
| 835 | if (td->io_cleanup) |
| 836 | td->io_cleanup(td); |
| 837 | } |
| 838 | |
| 839 | static int init_io(struct thread_data *td) |
| 840 | { |
| 841 | if (td->io_engine == FIO_SYNCIO) |
| 842 | return fio_syncio_init(td); |
| 843 | else if (td->io_engine == FIO_MMAPIO) |
| 844 | return fio_mmapio_init(td); |
| 845 | else if (td->io_engine == FIO_LIBAIO) |
| 846 | return fio_libaio_init(td); |
| 847 | else if (td->io_engine == FIO_POSIXAIO) |
| 848 | return fio_posixaio_init(td); |
| 849 | else if (td->io_engine == FIO_SGIO) |
| 850 | return fio_sgio_init(td); |
Jens Axboe | 8756e4d | 2006-05-27 20:24:53 +0200 | [diff] [blame] | 851 | else if (td->io_engine == FIO_SPLICEIO) |
| 852 | return fio_spliceio_init(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 853 | else { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 854 | log_err("bad io_engine %d\n", td->io_engine); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 855 | return 1; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | static void cleanup_io_u(struct thread_data *td) |
| 860 | { |
| 861 | struct list_head *entry, *n; |
| 862 | struct io_u *io_u; |
| 863 | |
| 864 | list_for_each_safe(entry, n, &td->io_u_freelist) { |
| 865 | io_u = list_entry(entry, struct io_u, list); |
| 866 | |
| 867 | list_del(&io_u->list); |
| 868 | free(io_u); |
| 869 | } |
| 870 | |
| 871 | if (td->mem_type == MEM_MALLOC) |
| 872 | free(td->orig_buffer); |
| 873 | else if (td->mem_type == MEM_SHM) { |
| 874 | struct shmid_ds sbuf; |
| 875 | |
| 876 | shmdt(td->orig_buffer); |
| 877 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
| 878 | } else if (td->mem_type == MEM_MMAP) |
| 879 | munmap(td->orig_buffer, td->orig_buffer_size); |
| 880 | else |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 881 | log_err("Bad memory type %d\n", td->mem_type); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 882 | |
| 883 | td->orig_buffer = NULL; |
| 884 | } |
| 885 | |
| 886 | static int init_io_u(struct thread_data *td) |
| 887 | { |
| 888 | struct io_u *io_u; |
| 889 | int i, max_units; |
| 890 | char *p; |
| 891 | |
| 892 | if (td->io_engine & FIO_SYNCIO) |
| 893 | max_units = 1; |
| 894 | else |
| 895 | max_units = td->iodepth; |
| 896 | |
| 897 | td->orig_buffer_size = td->max_bs * max_units + MASK; |
| 898 | |
| 899 | if (td->mem_type == MEM_MALLOC) |
| 900 | td->orig_buffer = malloc(td->orig_buffer_size); |
| 901 | else if (td->mem_type == MEM_SHM) { |
| 902 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); |
| 903 | if (td->shm_id < 0) { |
| 904 | td_verror(td, errno); |
| 905 | perror("shmget"); |
| 906 | return 1; |
| 907 | } |
| 908 | |
| 909 | td->orig_buffer = shmat(td->shm_id, NULL, 0); |
| 910 | if (td->orig_buffer == (void *) -1) { |
| 911 | td_verror(td, errno); |
| 912 | perror("shmat"); |
| 913 | td->orig_buffer = NULL; |
| 914 | return 1; |
| 915 | } |
| 916 | } else if (td->mem_type == MEM_MMAP) { |
| 917 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 918 | if (td->orig_buffer == MAP_FAILED) { |
| 919 | td_verror(td, errno); |
| 920 | perror("mmap"); |
| 921 | td->orig_buffer = NULL; |
| 922 | return 1; |
| 923 | } |
| 924 | } |
| 925 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 926 | p = ALIGN(td->orig_buffer); |
| 927 | for (i = 0; i < max_units; i++) { |
| 928 | io_u = malloc(sizeof(*io_u)); |
| 929 | memset(io_u, 0, sizeof(*io_u)); |
| 930 | INIT_LIST_HEAD(&io_u->list); |
| 931 | |
| 932 | io_u->buf = p + td->max_bs * i; |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 933 | io_u->index = i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 934 | list_add(&io_u->list, &td->io_u_freelist); |
| 935 | } |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | static int create_file(struct thread_data *td, unsigned long long size, |
| 941 | int extend) |
| 942 | { |
| 943 | unsigned long long left; |
| 944 | unsigned int bs; |
| 945 | int r, oflags; |
| 946 | char *b; |
| 947 | |
| 948 | /* |
| 949 | * unless specifically asked for overwrite, let normal io extend it |
| 950 | */ |
| 951 | if (td_write(td) && !td->overwrite) |
| 952 | return 0; |
| 953 | |
| 954 | if (!size) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 955 | log_err("Need size for create\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 956 | td_verror(td, EINVAL); |
| 957 | return 1; |
| 958 | } |
| 959 | |
| 960 | if (!extend) { |
| 961 | oflags = O_CREAT | O_TRUNC; |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 962 | fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 963 | } else { |
| 964 | oflags = O_APPEND; |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 965 | fprintf(f_out, "%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | td->fd = open(td->file_name, O_WRONLY | oflags, 0644); |
| 969 | if (td->fd < 0) { |
| 970 | td_verror(td, errno); |
| 971 | return 1; |
| 972 | } |
| 973 | |
| 974 | if (!extend && ftruncate(td->fd, td->file_size) == -1) { |
| 975 | td_verror(td, errno); |
| 976 | return 1; |
| 977 | } |
| 978 | |
| 979 | td->io_size = td->file_size; |
| 980 | b = malloc(td->max_bs); |
| 981 | memset(b, 0, td->max_bs); |
| 982 | |
| 983 | left = size; |
| 984 | while (left && !td->terminate) { |
| 985 | bs = td->max_bs; |
| 986 | if (bs > left) |
| 987 | bs = left; |
| 988 | |
| 989 | r = write(td->fd, b, bs); |
| 990 | |
| 991 | if (r == (int) bs) { |
| 992 | left -= bs; |
| 993 | continue; |
| 994 | } else { |
| 995 | if (r < 0) |
| 996 | td_verror(td, errno); |
| 997 | else |
| 998 | td_verror(td, EIO); |
| 999 | |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | if (td->terminate) |
| 1005 | unlink(td->file_name); |
| 1006 | else if (td->create_fsync) |
| 1007 | fsync(td->fd); |
| 1008 | |
| 1009 | close(td->fd); |
| 1010 | td->fd = -1; |
| 1011 | free(b); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | static int file_size(struct thread_data *td) |
| 1016 | { |
| 1017 | struct stat st; |
| 1018 | |
| 1019 | if (fstat(td->fd, &st) == -1) { |
| 1020 | td_verror(td, errno); |
| 1021 | return 1; |
| 1022 | } |
| 1023 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1024 | td->real_file_size = st.st_size; |
| 1025 | |
| 1026 | if (!td->file_size || td->file_size > td->real_file_size) |
| 1027 | td->file_size = td->real_file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1028 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1029 | td->file_size -= td->file_offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | static int bdev_size(struct thread_data *td) |
| 1034 | { |
Jens Axboe | 9104f87 | 2005-12-28 23:50:45 +0100 | [diff] [blame] | 1035 | unsigned long long bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1036 | int r; |
| 1037 | |
| 1038 | r = blockdev_size(td->fd, &bytes); |
| 1039 | if (r) { |
| 1040 | td_verror(td, r); |
| 1041 | return 1; |
| 1042 | } |
| 1043 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1044 | td->real_file_size = bytes; |
| 1045 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1046 | /* |
| 1047 | * no extend possibilities, so limit size to device size if too large |
| 1048 | */ |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1049 | if (!td->file_size || td->file_size > td->real_file_size) |
| 1050 | td->file_size = td->real_file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1051 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1052 | td->file_size -= td->file_offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | static int get_file_size(struct thread_data *td) |
| 1057 | { |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1058 | int ret = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1059 | |
| 1060 | if (td->filetype == FIO_TYPE_FILE) |
| 1061 | ret = file_size(td); |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1062 | else if (td->filetype == FIO_TYPE_BD) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1063 | ret = bdev_size(td); |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1064 | else |
| 1065 | td->real_file_size = -1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1066 | |
| 1067 | if (ret) |
| 1068 | return ret; |
| 1069 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1070 | if (td->file_offset > td->real_file_size) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 1071 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1072 | return 1; |
| 1073 | } |
| 1074 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1075 | td->io_size = td->file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1076 | if (td->io_size == 0) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 1077 | log_err("%s: no io blocks\n", td->name); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1078 | td_verror(td, EINVAL); |
| 1079 | return 1; |
| 1080 | } |
| 1081 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1082 | if (!td->zone_size) |
| 1083 | td->zone_size = td->io_size; |
| 1084 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1085 | td->total_io_size = td->io_size * td->loops; |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static int setup_file_mmap(struct thread_data *td) |
| 1090 | { |
| 1091 | int flags; |
| 1092 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1093 | if (td_rw(td)) |
| 1094 | flags = PROT_READ | PROT_WRITE; |
| 1095 | else if (td_write(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1096 | flags = PROT_WRITE; |
| 1097 | |
| 1098 | if (td->verify != VERIFY_NONE) |
| 1099 | flags |= PROT_READ; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1100 | } else |
| 1101 | flags = PROT_READ; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1102 | |
| 1103 | td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset); |
| 1104 | if (td->mmap == MAP_FAILED) { |
| 1105 | td->mmap = NULL; |
| 1106 | td_verror(td, errno); |
| 1107 | return 1; |
| 1108 | } |
| 1109 | |
| 1110 | if (td->invalidate_cache) { |
| 1111 | if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) { |
| 1112 | td_verror(td, errno); |
| 1113 | return 1; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | if (td->sequential) { |
| 1118 | if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) { |
| 1119 | td_verror(td, errno); |
| 1120 | return 1; |
| 1121 | } |
| 1122 | } else { |
| 1123 | if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) { |
| 1124 | td_verror(td, errno); |
| 1125 | return 1; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | static int setup_file_plain(struct thread_data *td) |
| 1133 | { |
| 1134 | if (td->invalidate_cache) { |
| 1135 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) { |
| 1136 | td_verror(td, errno); |
| 1137 | return 1; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | if (td->sequential) { |
| 1142 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) { |
| 1143 | td_verror(td, errno); |
| 1144 | return 1; |
| 1145 | } |
| 1146 | } else { |
| 1147 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) { |
| 1148 | td_verror(td, errno); |
| 1149 | return 1; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | static int setup_file(struct thread_data *td) |
| 1157 | { |
| 1158 | struct stat st; |
| 1159 | int flags = 0; |
| 1160 | |
| 1161 | if (stat(td->file_name, &st) == -1) { |
| 1162 | if (errno != ENOENT) { |
| 1163 | td_verror(td, errno); |
| 1164 | return 1; |
| 1165 | } |
| 1166 | if (!td->create_file) { |
| 1167 | td_verror(td, ENOENT); |
| 1168 | return 1; |
| 1169 | } |
| 1170 | if (create_file(td, td->file_size, 0)) |
| 1171 | return 1; |
| 1172 | } else if (td->filetype == FIO_TYPE_FILE) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1173 | if (st.st_size < (off_t) td->file_size) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1174 | if (create_file(td, td->file_size - st.st_size, 1)) |
| 1175 | return 1; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | if (td->odirect) |
Jens Axboe | 2c0ecd2 | 2006-06-08 13:25:41 +0200 | [diff] [blame] | 1180 | flags |= OS_O_DIRECT; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1181 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1182 | if (td_write(td) || td_rw(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1183 | if (td->filetype == FIO_TYPE_FILE) { |
| 1184 | if (!td->overwrite) |
| 1185 | flags |= O_TRUNC; |
| 1186 | |
| 1187 | flags |= O_CREAT; |
| 1188 | } |
| 1189 | if (td->sync_io) |
| 1190 | flags |= O_SYNC; |
| 1191 | |
| 1192 | flags |= O_RDWR; |
| 1193 | |
| 1194 | td->fd = open(td->file_name, flags, 0600); |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1195 | } else { |
| 1196 | if (td->filetype == FIO_TYPE_CHAR) |
| 1197 | flags |= O_RDWR; |
| 1198 | else |
| 1199 | flags |= O_RDONLY; |
| 1200 | |
| 1201 | td->fd = open(td->file_name, flags); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | if (td->fd == -1) { |
| 1205 | td_verror(td, errno); |
| 1206 | return 1; |
| 1207 | } |
| 1208 | |
| 1209 | if (get_file_size(td)) |
| 1210 | return 1; |
| 1211 | |
| 1212 | if (td->io_engine != FIO_MMAPIO) |
| 1213 | return setup_file_plain(td); |
| 1214 | else |
| 1215 | return setup_file_mmap(td); |
| 1216 | } |
| 1217 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 1218 | static int switch_ioscheduler(struct thread_data *td) |
| 1219 | { |
| 1220 | char tmp[256], tmp2[128]; |
| 1221 | FILE *f; |
| 1222 | int ret; |
| 1223 | |
| 1224 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
| 1225 | |
| 1226 | f = fopen(tmp, "r+"); |
| 1227 | if (!f) { |
| 1228 | td_verror(td, errno); |
| 1229 | return 1; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Set io scheduler. |
| 1234 | */ |
| 1235 | ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f); |
| 1236 | if (ferror(f) || ret != 1) { |
| 1237 | td_verror(td, errno); |
| 1238 | fclose(f); |
| 1239 | return 1; |
| 1240 | } |
| 1241 | |
| 1242 | rewind(f); |
| 1243 | |
| 1244 | /* |
| 1245 | * Read back and check that the selected scheduler is now the default. |
| 1246 | */ |
| 1247 | ret = fread(tmp, 1, sizeof(tmp), f); |
| 1248 | if (ferror(f) || ret < 0) { |
| 1249 | td_verror(td, errno); |
| 1250 | fclose(f); |
| 1251 | return 1; |
| 1252 | } |
| 1253 | |
| 1254 | sprintf(tmp2, "[%s]", td->ioscheduler); |
| 1255 | if (!strstr(tmp, tmp2)) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 1256 | log_err("fio: io scheduler %s not found\n", td->ioscheduler); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 1257 | td_verror(td, EINVAL); |
| 1258 | fclose(f); |
| 1259 | return 1; |
| 1260 | } |
| 1261 | |
| 1262 | fclose(f); |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1266 | static void clear_io_state(struct thread_data *td) |
| 1267 | { |
| 1268 | if (td->io_engine == FIO_SYNCIO) |
| 1269 | lseek(td->fd, SEEK_SET, 0); |
| 1270 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1271 | td->last_pos = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1272 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
| 1273 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1274 | td->zone_bytes = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1275 | |
| 1276 | if (td->file_map) |
| 1277 | memset(td->file_map, 0, td->num_maps * sizeof(long)); |
| 1278 | } |
| 1279 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1280 | static void *thread_main(void *data) |
| 1281 | { |
| 1282 | struct thread_data *td = data; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1283 | |
| 1284 | if (!td->use_thread) |
| 1285 | setsid(); |
| 1286 | |
| 1287 | td->pid = getpid(); |
| 1288 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1289 | INIT_LIST_HEAD(&td->io_u_freelist); |
| 1290 | INIT_LIST_HEAD(&td->io_u_busylist); |
| 1291 | INIT_LIST_HEAD(&td->io_hist_list); |
| 1292 | INIT_LIST_HEAD(&td->io_log_list); |
| 1293 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1294 | if (init_io_u(td)) |
| 1295 | goto err; |
| 1296 | |
| 1297 | if (fio_setaffinity(td) == -1) { |
| 1298 | td_verror(td, errno); |
| 1299 | goto err; |
| 1300 | } |
| 1301 | |
| 1302 | if (init_io(td)) |
| 1303 | goto err; |
| 1304 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1305 | if (init_iolog(td)) |
| 1306 | goto err; |
| 1307 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1308 | if (td->ioprio) { |
| 1309 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 1310 | td_verror(td, errno); |
| 1311 | goto err; |
| 1312 | } |
| 1313 | } |
| 1314 | |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1315 | if (nice(td->nice) < 0) { |
| 1316 | td_verror(td, errno); |
| 1317 | goto err; |
| 1318 | } |
| 1319 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1320 | if (init_random_state(td)) |
| 1321 | goto err; |
| 1322 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 1323 | if (td->ioscheduler && switch_ioscheduler(td)) |
| 1324 | goto err; |
| 1325 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1326 | td_set_runstate(td, TD_INITIALIZED); |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1327 | fio_sem_up(&startup_sem); |
| 1328 | fio_sem_down(&td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1329 | |
| 1330 | if (!td->create_serialize && setup_file(td)) |
| 1331 | goto err; |
| 1332 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1333 | gettimeofday(&td->epoch, NULL); |
| 1334 | |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 1335 | if (td->exec_prerun) |
| 1336 | system(td->exec_prerun); |
| 1337 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1338 | while (td->loops--) { |
| 1339 | getrusage(RUSAGE_SELF, &td->ru_start); |
| 1340 | gettimeofday(&td->start, NULL); |
| 1341 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); |
| 1342 | |
| 1343 | if (td->ratemin) |
| 1344 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); |
| 1345 | |
| 1346 | clear_io_state(td); |
| 1347 | prune_io_piece_log(td); |
| 1348 | |
| 1349 | do_io(td); |
| 1350 | |
| 1351 | td->runtime[td->ddir] += mtime_since_now(&td->start); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1352 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1353 | td->runtime[td->ddir ^ 1] = td->runtime[td->ddir]; |
| 1354 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1355 | update_rusage_stat(td); |
| 1356 | |
| 1357 | if (td->error || td->terminate) |
| 1358 | break; |
| 1359 | |
| 1360 | if (td->verify == VERIFY_NONE) |
| 1361 | continue; |
| 1362 | |
| 1363 | clear_io_state(td); |
| 1364 | gettimeofday(&td->start, NULL); |
| 1365 | |
| 1366 | do_verify(td); |
| 1367 | |
| 1368 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); |
| 1369 | |
| 1370 | if (td->error || td->terminate) |
| 1371 | break; |
| 1372 | } |
| 1373 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1374 | if (td->bw_log) |
| 1375 | finish_log(td, td->bw_log, "bw"); |
| 1376 | if (td->slat_log) |
| 1377 | finish_log(td, td->slat_log, "slat"); |
| 1378 | if (td->clat_log) |
| 1379 | finish_log(td, td->clat_log, "clat"); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 1380 | if (td->write_iolog) |
| 1381 | write_iolog_close(td); |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 1382 | if (td->exec_postrun) |
| 1383 | system(td->exec_postrun); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1384 | |
| 1385 | if (exitall_on_terminate) |
| 1386 | terminate_threads(td->groupid); |
| 1387 | |
| 1388 | err: |
| 1389 | if (td->fd != -1) { |
| 1390 | close(td->fd); |
| 1391 | td->fd = -1; |
| 1392 | } |
| 1393 | if (td->mmap) |
| 1394 | munmap(td->mmap, td->file_size); |
| 1395 | cleanup_io(td); |
| 1396 | cleanup_io_u(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1397 | td_set_runstate(td, TD_EXITED); |
| 1398 | return NULL; |
| 1399 | |
| 1400 | } |
| 1401 | |
| 1402 | static void *fork_main(int shmid, int offset) |
| 1403 | { |
| 1404 | struct thread_data *td; |
| 1405 | void *data; |
| 1406 | |
| 1407 | data = shmat(shmid, NULL, 0); |
| 1408 | if (data == (void *) -1) { |
| 1409 | perror("shmat"); |
| 1410 | return NULL; |
| 1411 | } |
| 1412 | |
| 1413 | td = data + offset * sizeof(struct thread_data); |
| 1414 | thread_main(td); |
| 1415 | shmdt(data); |
| 1416 | return NULL; |
| 1417 | } |
| 1418 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1419 | static void check_str_update(struct thread_data *td) |
| 1420 | { |
| 1421 | char c = run_str[td->thread_number - 1]; |
| 1422 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1423 | switch (td->runstate) { |
| 1424 | case TD_REAPED: |
| 1425 | c = '_'; |
| 1426 | break; |
| 1427 | case TD_EXITED: |
| 1428 | c = 'E'; |
| 1429 | break; |
| 1430 | case TD_RUNNING: |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1431 | if (td_rw(td)) { |
| 1432 | if (td->sequential) |
| 1433 | c = 'M'; |
| 1434 | else |
| 1435 | c = 'm'; |
| 1436 | } else if (td_read(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1437 | if (td->sequential) |
| 1438 | c = 'R'; |
| 1439 | else |
| 1440 | c = 'r'; |
| 1441 | } else { |
| 1442 | if (td->sequential) |
| 1443 | c = 'W'; |
| 1444 | else |
| 1445 | c = 'w'; |
| 1446 | } |
| 1447 | break; |
| 1448 | case TD_VERIFYING: |
| 1449 | c = 'V'; |
| 1450 | break; |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 1451 | case TD_FSYNCING: |
| 1452 | c = 'F'; |
| 1453 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1454 | case TD_CREATED: |
| 1455 | c = 'C'; |
| 1456 | break; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1457 | case TD_INITIALIZED: |
| 1458 | c = 'I'; |
| 1459 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1460 | case TD_NOT_CREATED: |
| 1461 | c = 'P'; |
| 1462 | break; |
| 1463 | default: |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 1464 | log_err("state %d\n", td->runstate); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | run_str[td->thread_number - 1] = c; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1468 | } |
| 1469 | |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1470 | static void eta_to_str(char *str, int eta_sec) |
| 1471 | { |
| 1472 | unsigned int d, h, m, s; |
| 1473 | static int always_d, always_h; |
| 1474 | |
| 1475 | d = h = m = s = 0; |
| 1476 | |
| 1477 | s = eta_sec % 60; |
| 1478 | eta_sec /= 60; |
| 1479 | m = eta_sec % 60; |
| 1480 | eta_sec /= 60; |
| 1481 | h = eta_sec % 24; |
| 1482 | eta_sec /= 24; |
| 1483 | d = eta_sec; |
| 1484 | |
| 1485 | if (d || always_d) { |
| 1486 | always_d = 1; |
| 1487 | str += sprintf(str, "%02dd:", d); |
| 1488 | } |
| 1489 | if (h || always_h) { |
| 1490 | always_h = 1; |
| 1491 | str += sprintf(str, "%02dh:", h); |
| 1492 | } |
| 1493 | |
| 1494 | str += sprintf(str, "%02dm:", m); |
| 1495 | str += sprintf(str, "%02ds", s); |
| 1496 | } |
| 1497 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1498 | static int thread_eta(struct thread_data *td, unsigned long elapsed) |
| 1499 | { |
| 1500 | unsigned long long bytes_total, bytes_done; |
| 1501 | unsigned int eta_sec = 0; |
| 1502 | |
| 1503 | bytes_total = td->total_io_size; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1504 | |
| 1505 | /* |
| 1506 | * if writing, bytes_total will be twice the size. If mixing, |
| 1507 | * assume a 50/50 split and thus bytes_total will be 50% larger. |
| 1508 | */ |
| 1509 | if (td->verify) { |
| 1510 | if (td_rw(td)) |
| 1511 | bytes_total = bytes_total * 3 / 2; |
| 1512 | else |
| 1513 | bytes_total <<= 1; |
| 1514 | } |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1515 | if (td->zone_size && td->zone_skip) |
| 1516 | bytes_total /= (td->zone_skip / td->zone_size); |
| 1517 | |
| 1518 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) { |
| 1519 | double perc; |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1520 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1521 | bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]; |
| 1522 | perc = (double) bytes_done / (double) bytes_total; |
| 1523 | if (perc > 1.0) |
| 1524 | perc = 1.0; |
| 1525 | |
| 1526 | eta_sec = (elapsed * (1.0 / perc)) - elapsed; |
| 1527 | |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1528 | if (td->timeout && eta_sec > (td->timeout - elapsed)) |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1529 | eta_sec = td->timeout - elapsed; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1530 | } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED |
| 1531 | || td->runstate == TD_INITIALIZED) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1532 | int t_eta = 0, r_eta = 0; |
| 1533 | |
| 1534 | /* |
| 1535 | * We can only guess - assume it'll run the full timeout |
| 1536 | * if given, otherwise assume it'll run at the specified rate. |
| 1537 | */ |
| 1538 | if (td->timeout) |
| 1539 | t_eta = td->timeout + td->start_delay - elapsed; |
| 1540 | if (td->rate) { |
| 1541 | r_eta = (bytes_total / 1024) / td->rate; |
| 1542 | r_eta += td->start_delay - elapsed; |
| 1543 | } |
| 1544 | |
| 1545 | if (r_eta && t_eta) |
| 1546 | eta_sec = min(r_eta, t_eta); |
| 1547 | else if (r_eta) |
| 1548 | eta_sec = r_eta; |
| 1549 | else if (t_eta) |
| 1550 | eta_sec = t_eta; |
| 1551 | else |
| 1552 | eta_sec = INT_MAX; |
| 1553 | } else { |
| 1554 | /* |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 1555 | * thread is already done or waiting for fsync |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1556 | */ |
| 1557 | eta_sec = 0; |
| 1558 | } |
| 1559 | |
| 1560 | return eta_sec; |
| 1561 | } |
| 1562 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1563 | static void print_thread_status(void) |
| 1564 | { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1565 | unsigned long elapsed = time_since_now(&genesis); |
Jens Axboe | 71a751c | 2006-06-08 14:48:47 +0200 | [diff] [blame] | 1566 | int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1567 | char eta_str[32]; |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1568 | double perc = 0.0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1569 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1570 | eta_secs = malloc(thread_number * sizeof(int)); |
| 1571 | memset(eta_secs, 0, thread_number * sizeof(int)); |
| 1572 | |
Jens Axboe | 71a751c | 2006-06-08 14:48:47 +0200 | [diff] [blame] | 1573 | nr_pending = nr_running = t_rate = m_rate = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1574 | for (i = 0; i < thread_number; i++) { |
| 1575 | struct thread_data *td = &threads[i]; |
| 1576 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 1577 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING|| |
| 1578 | td->runstate == TD_FSYNCING) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1579 | nr_running++; |
| 1580 | t_rate += td->rate; |
| 1581 | m_rate += td->ratemin; |
Jens Axboe | 71a751c | 2006-06-08 14:48:47 +0200 | [diff] [blame] | 1582 | } else if (td->runstate < TD_RUNNING) |
| 1583 | nr_pending++; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1584 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1585 | if (elapsed >= 3) |
| 1586 | eta_secs[i] = thread_eta(td, elapsed); |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1587 | else |
| 1588 | eta_secs[i] = INT_MAX; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1589 | |
| 1590 | check_str_update(td); |
| 1591 | } |
| 1592 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1593 | if (exitall_on_terminate) |
| 1594 | eta_sec = INT_MAX; |
| 1595 | else |
| 1596 | eta_sec = 0; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1597 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1598 | for (i = 0; i < thread_number; i++) { |
| 1599 | if (exitall_on_terminate) { |
| 1600 | if (eta_secs[i] < eta_sec) |
| 1601 | eta_sec = eta_secs[i]; |
| 1602 | } else { |
| 1603 | if (eta_secs[i] > eta_sec) |
| 1604 | eta_sec = eta_secs[i]; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1605 | } |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1606 | } |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1607 | |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1608 | if (eta_sec != INT_MAX && elapsed) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1609 | perc = (double) elapsed / (double) (elapsed + eta_sec); |
| 1610 | eta_to_str(eta_str, eta_sec); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
Jens Axboe | 71a751c | 2006-06-08 14:48:47 +0200 | [diff] [blame] | 1613 | if (!nr_running && !nr_pending) |
Jens Axboe | f44c1d4 | 2006-06-08 14:46:24 +0200 | [diff] [blame] | 1614 | return; |
| 1615 | |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 1616 | printf("Threads running: %d", nr_running); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1617 | if (m_rate || t_rate) |
| 1618 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1619 | if (eta_sec != INT_MAX) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1620 | perc *= 100.0; |
| 1621 | printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str); |
| 1622 | } |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1623 | printf("\r"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1624 | fflush(stdout); |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1625 | free(eta_secs); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 1629 | { |
| 1630 | int i; |
| 1631 | |
| 1632 | /* |
| 1633 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 1634 | */ |
| 1635 | for (i = 0; i < thread_number; i++) { |
| 1636 | struct thread_data *td = &threads[i]; |
| 1637 | |
| 1638 | if (td->runstate != TD_EXITED) |
| 1639 | continue; |
| 1640 | |
| 1641 | td_set_runstate(td, TD_REAPED); |
| 1642 | |
| 1643 | if (td->use_thread) { |
| 1644 | long ret; |
| 1645 | |
| 1646 | if (pthread_join(td->thread, (void *) &ret)) |
| 1647 | perror("thread_join"); |
| 1648 | } else |
| 1649 | waitpid(td->pid, NULL, 0); |
| 1650 | |
| 1651 | (*nr_running)--; |
| 1652 | (*m_rate) -= td->ratemin; |
| 1653 | (*t_rate) -= td->rate; |
| 1654 | } |
| 1655 | } |
| 1656 | |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1657 | static void fio_unpin_memory(void *pinned) |
| 1658 | { |
| 1659 | if (pinned) { |
| 1660 | if (munlock(pinned, mlock_size) < 0) |
| 1661 | perror("munlock"); |
| 1662 | munmap(pinned, mlock_size); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | static void *fio_pin_memory(void) |
| 1667 | { |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 1668 | unsigned long long phys_mem; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1669 | void *ptr; |
| 1670 | |
| 1671 | if (!mlock_size) |
| 1672 | return NULL; |
| 1673 | |
| 1674 | /* |
| 1675 | * Don't allow mlock of more than real_mem-128MB |
| 1676 | */ |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 1677 | phys_mem = os_phys_mem(); |
| 1678 | if (phys_mem) { |
| 1679 | if ((mlock_size + 128 * 1024 * 1024) > phys_mem) { |
| 1680 | mlock_size = phys_mem - 128 * 1024 * 1024; |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 1681 | fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20); |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 1686 | if (!ptr) { |
| 1687 | perror("malloc locked mem"); |
| 1688 | return NULL; |
| 1689 | } |
| 1690 | if (mlock(ptr, mlock_size) < 0) { |
| 1691 | munmap(ptr, mlock_size); |
| 1692 | perror("mlock"); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | |
| 1696 | return ptr; |
| 1697 | } |
| 1698 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1699 | static void run_threads(void) |
| 1700 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1701 | struct thread_data *td; |
| 1702 | unsigned long spent; |
| 1703 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1704 | void *mlocked_mem; |
| 1705 | |
| 1706 | mlocked_mem = fio_pin_memory(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1707 | |
| 1708 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1709 | fflush(stdout); |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 1710 | |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 1711 | signal(SIGINT, sig_handler); |
| 1712 | signal(SIGALRM, sig_handler); |
| 1713 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1714 | todo = thread_number; |
| 1715 | nr_running = 0; |
| 1716 | nr_started = 0; |
| 1717 | m_rate = t_rate = 0; |
| 1718 | |
| 1719 | for (i = 0; i < thread_number; i++) { |
| 1720 | td = &threads[i]; |
| 1721 | |
| 1722 | run_str[td->thread_number - 1] = 'P'; |
| 1723 | |
| 1724 | init_disk_util(td); |
| 1725 | |
| 1726 | if (!td->create_serialize) |
| 1727 | continue; |
| 1728 | |
| 1729 | /* |
| 1730 | * do file setup here so it happens sequentially, |
| 1731 | * we don't want X number of threads getting their |
| 1732 | * client data interspersed on disk |
| 1733 | */ |
| 1734 | if (setup_file(td)) { |
| 1735 | td_set_runstate(td, TD_REAPED); |
| 1736 | todo--; |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | gettimeofday(&genesis, NULL); |
| 1741 | |
| 1742 | while (todo) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1743 | struct thread_data *map[MAX_JOBS]; |
| 1744 | struct timeval this_start; |
| 1745 | int this_jobs = 0, left; |
| 1746 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1747 | /* |
| 1748 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 1749 | */ |
| 1750 | for (i = 0; i < thread_number; i++) { |
| 1751 | td = &threads[i]; |
| 1752 | |
| 1753 | if (td->runstate != TD_NOT_CREATED) |
| 1754 | continue; |
| 1755 | |
| 1756 | /* |
| 1757 | * never got a chance to start, killed by other |
| 1758 | * thread for some reason |
| 1759 | */ |
| 1760 | if (td->terminate) { |
| 1761 | todo--; |
| 1762 | continue; |
| 1763 | } |
| 1764 | |
| 1765 | if (td->start_delay) { |
| 1766 | spent = mtime_since_now(&genesis); |
| 1767 | |
| 1768 | if (td->start_delay * 1000 > spent) |
| 1769 | continue; |
| 1770 | } |
| 1771 | |
| 1772 | if (td->stonewall && (nr_started || nr_running)) |
| 1773 | break; |
| 1774 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1775 | /* |
| 1776 | * Set state to created. Thread will transition |
| 1777 | * to TD_INITIALIZED when it's done setting up. |
| 1778 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1779 | td_set_runstate(td, TD_CREATED); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1780 | map[this_jobs++] = td; |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1781 | fio_sem_init(&startup_sem, 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1782 | nr_started++; |
| 1783 | |
| 1784 | if (td->use_thread) { |
| 1785 | if (pthread_create(&td->thread, NULL, thread_main, td)) { |
| 1786 | perror("thread_create"); |
| 1787 | nr_started--; |
| 1788 | } |
| 1789 | } else { |
| 1790 | if (fork()) |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1791 | fio_sem_down(&startup_sem); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1792 | else { |
| 1793 | fork_main(shm_id, i); |
| 1794 | exit(0); |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | /* |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1800 | * Wait for the started threads to transition to |
| 1801 | * TD_INITIALIZED. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1802 | */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1803 | printf("fio: Waiting for threads to initialize...\n"); |
| 1804 | gettimeofday(&this_start, NULL); |
| 1805 | left = this_jobs; |
| 1806 | while (left) { |
| 1807 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
| 1808 | break; |
| 1809 | |
| 1810 | usleep(100000); |
| 1811 | |
| 1812 | for (i = 0; i < this_jobs; i++) { |
| 1813 | td = map[i]; |
| 1814 | if (!td) |
| 1815 | continue; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1816 | if (td->runstate == TD_INITIALIZED) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1817 | map[i] = NULL; |
| 1818 | left--; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1819 | } else if (td->runstate >= TD_EXITED) { |
| 1820 | map[i] = NULL; |
| 1821 | left--; |
| 1822 | todo--; |
| 1823 | nr_running++; /* work-around... */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | if (left) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 1829 | log_err("fio: %d jobs failed to start\n", left); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1830 | for (i = 0; i < this_jobs; i++) { |
| 1831 | td = map[i]; |
| 1832 | if (!td) |
| 1833 | continue; |
| 1834 | kill(td->pid, SIGTERM); |
| 1835 | } |
| 1836 | break; |
| 1837 | } |
| 1838 | |
| 1839 | /* |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1840 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1841 | */ |
| 1842 | printf("fio: Go for launch\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1843 | for (i = 0; i < thread_number; i++) { |
| 1844 | td = &threads[i]; |
| 1845 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1846 | if (td->runstate != TD_INITIALIZED) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1847 | continue; |
| 1848 | |
| 1849 | td_set_runstate(td, TD_RUNNING); |
| 1850 | nr_running++; |
| 1851 | nr_started--; |
| 1852 | m_rate += td->ratemin; |
| 1853 | t_rate += td->rate; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1854 | todo--; |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1855 | fio_sem_up(&td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1859 | |
| 1860 | if (todo) |
| 1861 | usleep(100000); |
| 1862 | } |
| 1863 | |
| 1864 | while (nr_running) { |
| 1865 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1866 | usleep(10000); |
| 1867 | } |
| 1868 | |
| 1869 | update_io_ticks(); |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1870 | fio_unpin_memory(mlocked_mem); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1871 | } |
| 1872 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1873 | int main(int argc, char *argv[]) |
| 1874 | { |
| 1875 | if (parse_options(argc, argv)) |
| 1876 | return 1; |
| 1877 | |
| 1878 | if (!thread_number) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 1879 | log_err("Nothing to do\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1880 | return 1; |
| 1881 | } |
| 1882 | |
| 1883 | disk_util_timer_arm(); |
| 1884 | |
| 1885 | run_threads(); |
| 1886 | show_run_stats(); |
| 1887 | |
| 1888 | return 0; |
| 1889 | } |