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 | */ |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | #include <signal.h> |
| 28 | #include <time.h> |
| 29 | #include <math.h> |
| 30 | #include <assert.h> |
| 31 | #include <dirent.h> |
| 32 | #include <libgen.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/wait.h> |
| 36 | #include <sys/ipc.h> |
| 37 | #include <sys/shm.h> |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/mman.h> |
| 40 | |
| 41 | #include "fio.h" |
| 42 | #include "os.h" |
| 43 | |
| 44 | #define MASK (4095) |
| 45 | |
| 46 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) |
| 47 | |
| 48 | int groupid = 0; |
| 49 | int thread_number = 0; |
| 50 | static char run_str[MAX_JOBS + 1]; |
| 51 | int shm_id = 0; |
| 52 | static LIST_HEAD(disk_list); |
| 53 | static struct itimerval itimer; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 54 | static struct timeval genesis; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 55 | |
| 56 | static void update_io_ticks(void); |
| 57 | static void disk_util_timer_arm(void); |
| 58 | static void print_thread_status(void); |
| 59 | |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 60 | extern unsigned long long mlock_size; |
| 61 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 62 | /* |
| 63 | * thread life cycle |
| 64 | */ |
| 65 | enum { |
| 66 | TD_NOT_CREATED = 0, |
| 67 | TD_CREATED, |
| 68 | TD_RUNNING, |
| 69 | TD_VERIFYING, |
| 70 | TD_EXITED, |
| 71 | TD_REAPED, |
| 72 | }; |
| 73 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 74 | #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] | 75 | |
| 76 | static sem_t startup_sem; |
| 77 | |
| 78 | #define TERMINATE_ALL (-1) |
| 79 | |
| 80 | static void terminate_threads(int group_id) |
| 81 | { |
| 82 | int i; |
| 83 | |
| 84 | for (i = 0; i < thread_number; i++) { |
| 85 | struct thread_data *td = &threads[i]; |
| 86 | |
| 87 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
| 88 | td->terminate = 1; |
| 89 | td->start_delay = 0; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void sig_handler(int sig) |
| 95 | { |
| 96 | switch (sig) { |
| 97 | case SIGALRM: |
| 98 | update_io_ticks(); |
| 99 | disk_util_timer_arm(); |
| 100 | print_thread_status(); |
| 101 | break; |
| 102 | default: |
| 103 | printf("\nfio: terminating on signal\n"); |
| 104 | fflush(stdout); |
| 105 | terminate_threads(TERMINATE_ALL); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static unsigned long utime_since(struct timeval *s, struct timeval *e) |
| 111 | { |
| 112 | double sec, usec; |
| 113 | |
| 114 | sec = e->tv_sec - s->tv_sec; |
| 115 | usec = e->tv_usec - s->tv_usec; |
| 116 | if (sec > 0 && usec < 0) { |
| 117 | sec--; |
| 118 | usec += 1000000; |
| 119 | } |
| 120 | |
| 121 | sec *= (double) 1000000; |
| 122 | |
| 123 | return sec + usec; |
| 124 | } |
| 125 | |
| 126 | static unsigned long utime_since_now(struct timeval *s) |
| 127 | { |
| 128 | struct timeval t; |
| 129 | |
| 130 | gettimeofday(&t, NULL); |
| 131 | return utime_since(s, &t); |
| 132 | } |
| 133 | |
| 134 | static unsigned long mtime_since(struct timeval *s, struct timeval *e) |
| 135 | { |
| 136 | double sec, usec; |
| 137 | |
| 138 | sec = e->tv_sec - s->tv_sec; |
| 139 | usec = e->tv_usec - s->tv_usec; |
| 140 | if (sec > 0 && usec < 0) { |
| 141 | sec--; |
| 142 | usec += 1000000; |
| 143 | } |
| 144 | |
| 145 | sec *= (double) 1000; |
| 146 | usec /= (double) 1000; |
| 147 | |
| 148 | return sec + usec; |
| 149 | } |
| 150 | |
| 151 | static unsigned long mtime_since_now(struct timeval *s) |
| 152 | { |
| 153 | struct timeval t; |
| 154 | |
| 155 | gettimeofday(&t, NULL); |
| 156 | return mtime_since(s, &t); |
| 157 | } |
| 158 | |
| 159 | static inline unsigned long msec_now(struct timeval *s) |
| 160 | { |
| 161 | return s->tv_sec * 1000 + s->tv_usec / 1000; |
| 162 | } |
| 163 | |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 164 | static unsigned long time_since_now(struct timeval *s) |
| 165 | { |
| 166 | return mtime_since_now(s) / 1000; |
| 167 | } |
| 168 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 169 | static int random_map_free(struct thread_data *td, unsigned long long block) |
| 170 | { |
| 171 | unsigned int idx = RAND_MAP_IDX(td, block); |
| 172 | unsigned int bit = RAND_MAP_BIT(td, block); |
| 173 | |
| 174 | return (td->file_map[idx] & (1UL << bit)) == 0; |
| 175 | } |
| 176 | |
| 177 | static int get_next_free_block(struct thread_data *td, unsigned long long *b) |
| 178 | { |
| 179 | int i; |
| 180 | |
| 181 | *b = 0; |
| 182 | i = 0; |
| 183 | while ((*b) * td->min_bs < td->io_size) { |
| 184 | if (td->file_map[i] != -1UL) { |
| 185 | *b += ffz(td->file_map[i]); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | *b += BLOCKS_PER_MAP; |
| 190 | i++; |
| 191 | } |
| 192 | |
| 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | static void mark_random_map(struct thread_data *td, struct io_u *io_u) |
| 197 | { |
| 198 | unsigned long block = io_u->offset / td->min_bs; |
| 199 | unsigned int blocks = 0; |
| 200 | |
| 201 | while (blocks < (io_u->buflen / td->min_bs)) { |
| 202 | unsigned int idx, bit; |
| 203 | |
| 204 | if (!random_map_free(td, block)) |
| 205 | break; |
| 206 | |
| 207 | idx = RAND_MAP_IDX(td, block); |
| 208 | bit = RAND_MAP_BIT(td, block); |
| 209 | |
| 210 | assert(idx < td->num_maps); |
| 211 | |
| 212 | td->file_map[idx] |= (1UL << bit); |
| 213 | block++; |
| 214 | blocks++; |
| 215 | } |
| 216 | |
| 217 | if ((blocks * td->min_bs) < io_u->buflen) |
| 218 | io_u->buflen = blocks * td->min_bs; |
| 219 | } |
| 220 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 221 | static inline void add_stat_sample(struct io_stat *is, unsigned long val) |
| 222 | { |
| 223 | if (val > is->max_val) |
| 224 | is->max_val = val; |
| 225 | if (val < is->min_val) |
| 226 | is->min_val = val; |
| 227 | |
| 228 | is->val += val; |
| 229 | is->val_sq += val * val; |
| 230 | is->samples++; |
| 231 | } |
| 232 | |
| 233 | static void add_log_sample(struct thread_data *td, struct io_log *iolog, |
| 234 | unsigned long val, int ddir) |
| 235 | { |
| 236 | if (iolog->nr_samples == iolog->max_samples) { |
| 237 | int new_size = sizeof(struct io_sample) * iolog->max_samples*2; |
| 238 | |
| 239 | iolog->log = realloc(iolog->log, new_size); |
| 240 | iolog->max_samples <<= 1; |
| 241 | } |
| 242 | |
| 243 | iolog->log[iolog->nr_samples].val = val; |
| 244 | iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch); |
| 245 | iolog->log[iolog->nr_samples].ddir = ddir; |
| 246 | iolog->nr_samples++; |
| 247 | } |
| 248 | |
| 249 | static void add_clat_sample(struct thread_data *td, int ddir,unsigned long msec) |
| 250 | { |
| 251 | add_stat_sample(&td->clat_stat[ddir], msec); |
| 252 | |
| 253 | if (td->clat_log) |
| 254 | add_log_sample(td, td->clat_log, msec, ddir); |
| 255 | } |
| 256 | |
| 257 | static void add_slat_sample(struct thread_data *td, int ddir,unsigned long msec) |
| 258 | { |
| 259 | add_stat_sample(&td->slat_stat[ddir], msec); |
| 260 | |
| 261 | if (td->slat_log) |
| 262 | add_log_sample(td, td->slat_log, msec, ddir); |
| 263 | } |
| 264 | |
| 265 | static void add_bw_sample(struct thread_data *td, int ddir) |
| 266 | { |
| 267 | unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]); |
| 268 | unsigned long rate; |
| 269 | |
| 270 | if (spent < td->bw_avg_time) |
| 271 | return; |
| 272 | |
| 273 | rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent; |
| 274 | add_stat_sample(&td->bw_stat[ddir], rate); |
| 275 | |
| 276 | if (td->bw_log) |
| 277 | add_log_sample(td, td->bw_log, rate, ddir); |
| 278 | |
| 279 | gettimeofday(&td->stat_sample_time[ddir], NULL); |
| 280 | td->stat_io_bytes[ddir] = td->this_io_bytes[ddir]; |
| 281 | } |
| 282 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 283 | static int get_next_offset(struct thread_data *td, unsigned long long *offset) |
| 284 | { |
| 285 | unsigned long long b, rb; |
| 286 | long r; |
| 287 | |
| 288 | if (!td->sequential) { |
| 289 | unsigned long max_blocks = td->io_size / td->min_bs; |
| 290 | int loops = 50; |
| 291 | |
| 292 | do { |
| 293 | lrand48_r(&td->random_state, &r); |
| 294 | b = ((max_blocks - 1) * r / (RAND_MAX+1.0)); |
| 295 | rb = b + (td->file_offset / td->min_bs); |
| 296 | loops--; |
| 297 | } while (!random_map_free(td, rb) && loops); |
| 298 | |
| 299 | if (!loops) { |
| 300 | if (get_next_free_block(td, &b)) |
| 301 | return 1; |
| 302 | } |
| 303 | } else |
| 304 | b = td->last_pos / td->min_bs; |
| 305 | |
| 306 | *offset = (b * td->min_bs) + td->file_offset; |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 307 | if (*offset > td->real_file_size) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 308 | return 1; |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static unsigned int get_next_buflen(struct thread_data *td) |
| 314 | { |
| 315 | unsigned int buflen; |
| 316 | long r; |
| 317 | |
| 318 | if (td->min_bs == td->max_bs) |
| 319 | buflen = td->min_bs; |
| 320 | else { |
| 321 | lrand48_r(&td->bsrange_state, &r); |
| 322 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
| 323 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); |
| 324 | } |
| 325 | |
| 326 | if (buflen > td->io_size - td->this_io_bytes[td->ddir]) |
| 327 | buflen = td->io_size - td->this_io_bytes[td->ddir]; |
| 328 | |
| 329 | return buflen; |
| 330 | } |
| 331 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 332 | /* |
| 333 | * busy looping version for the last few usec |
| 334 | */ |
| 335 | static void __usec_sleep(unsigned int usec) |
| 336 | { |
| 337 | struct timeval start; |
| 338 | |
| 339 | gettimeofday(&start, NULL); |
| 340 | while (utime_since_now(&start) < usec) |
| 341 | nop; |
| 342 | } |
| 343 | |
| 344 | static void usec_sleep(struct thread_data *td, unsigned long usec) |
| 345 | { |
| 346 | struct timespec req, rem; |
| 347 | |
| 348 | req.tv_sec = usec / 1000000; |
| 349 | req.tv_nsec = usec * 1000 - req.tv_sec * 1000000; |
| 350 | |
| 351 | do { |
| 352 | if (usec < 5000) { |
| 353 | __usec_sleep(usec); |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | rem.tv_sec = rem.tv_nsec = 0; |
| 358 | if (nanosleep(&req, &rem) < 0) |
| 359 | break; |
| 360 | |
| 361 | if ((rem.tv_sec + rem.tv_nsec) == 0) |
| 362 | break; |
| 363 | |
| 364 | req.tv_nsec = rem.tv_nsec; |
| 365 | req.tv_sec = rem.tv_sec; |
| 366 | |
| 367 | usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000; |
| 368 | } while (!td->terminate); |
| 369 | } |
| 370 | |
| 371 | static void rate_throttle(struct thread_data *td, unsigned long time_spent, |
| 372 | unsigned int bytes) |
| 373 | { |
| 374 | unsigned long usec_cycle; |
| 375 | |
| 376 | if (!td->rate) |
| 377 | return; |
| 378 | |
| 379 | usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs); |
| 380 | |
| 381 | if (time_spent < usec_cycle) { |
| 382 | unsigned long s = usec_cycle - time_spent; |
| 383 | |
| 384 | td->rate_pending_usleep += s; |
| 385 | if (td->rate_pending_usleep >= 100000) { |
| 386 | usec_sleep(td, td->rate_pending_usleep); |
| 387 | td->rate_pending_usleep = 0; |
| 388 | } |
| 389 | } else { |
| 390 | long overtime = time_spent - usec_cycle; |
| 391 | |
| 392 | td->rate_pending_usleep -= overtime; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
| 397 | { |
| 398 | unsigned long spent; |
| 399 | unsigned long rate; |
| 400 | int ddir = td->ddir; |
| 401 | |
| 402 | /* |
| 403 | * allow a 2 second settle period in the beginning |
| 404 | */ |
| 405 | if (mtime_since(&td->start, now) < 2000) |
| 406 | return 0; |
| 407 | |
| 408 | /* |
| 409 | * if rate blocks is set, sample is running |
| 410 | */ |
| 411 | if (td->rate_bytes) { |
| 412 | spent = mtime_since(&td->lastrate, now); |
| 413 | if (spent < td->ratecycle) |
| 414 | return 0; |
| 415 | |
| 416 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; |
| 417 | if (rate < td->ratemin) { |
| 418 | printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate); |
| 419 | if (rate_quit) |
| 420 | terminate_threads(td->groupid); |
| 421 | return 1; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | td->rate_bytes = td->this_io_bytes[ddir]; |
| 426 | memcpy(&td->lastrate, now, sizeof(*now)); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 431 | { |
| 432 | if (!td->timeout) |
| 433 | return 0; |
| 434 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) |
| 435 | return 1; |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static void fill_random_bytes(struct thread_data *td, |
| 441 | unsigned char *p, unsigned int len) |
| 442 | { |
| 443 | unsigned int todo; |
| 444 | double r; |
| 445 | |
| 446 | while (len) { |
| 447 | drand48_r(&td->verify_state, &r); |
| 448 | |
| 449 | /* |
| 450 | * lrand48_r seems to be broken and only fill the bottom |
| 451 | * 32-bits, even on 64-bit archs with 64-bit longs |
| 452 | */ |
| 453 | todo = sizeof(r); |
| 454 | if (todo > len) |
| 455 | todo = len; |
| 456 | |
| 457 | memcpy(p, &r, todo); |
| 458 | |
| 459 | len -= todo; |
| 460 | p += todo; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | static void hexdump(void *buffer, int len) |
| 465 | { |
| 466 | unsigned char *p = buffer; |
| 467 | int i; |
| 468 | |
| 469 | for (i = 0; i < len; i++) |
| 470 | printf("%02x", p[i]); |
| 471 | printf("\n"); |
| 472 | } |
| 473 | |
| 474 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) |
| 475 | { |
| 476 | unsigned char *p = (unsigned char *) io_u->buf; |
| 477 | unsigned long c; |
| 478 | int ret; |
| 479 | |
| 480 | p += sizeof(*hdr); |
| 481 | c = crc32(p, hdr->len - sizeof(*hdr)); |
| 482 | ret = c != hdr->crc32; |
| 483 | |
| 484 | if (ret) { |
| 485 | fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
| 486 | fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c); |
| 487 | } |
| 488 | |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) |
| 493 | { |
| 494 | unsigned char *p = (unsigned char *) io_u->buf; |
| 495 | struct md5_ctx md5_ctx; |
| 496 | int ret; |
| 497 | |
| 498 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
| 499 | p += sizeof(*hdr); |
| 500 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); |
| 501 | |
| 502 | ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); |
| 503 | if (ret) { |
| 504 | fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
| 505 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); |
| 506 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); |
| 507 | } |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static int verify_io_u(struct io_u *io_u) |
| 513 | { |
| 514 | struct verify_header *hdr = (struct verify_header *) io_u->buf; |
| 515 | int ret; |
| 516 | |
| 517 | if (hdr->fio_magic != FIO_HDR_MAGIC) |
| 518 | return 1; |
| 519 | |
| 520 | if (hdr->verify_type == VERIFY_MD5) |
| 521 | ret = verify_io_u_md5(hdr, io_u); |
| 522 | else if (hdr->verify_type == VERIFY_CRC32) |
| 523 | ret = verify_io_u_crc32(hdr, io_u); |
| 524 | else { |
| 525 | fprintf(stderr, "Bad verify type %d\n", hdr->verify_type); |
| 526 | ret = 1; |
| 527 | } |
| 528 | |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
| 533 | { |
| 534 | hdr->crc32 = crc32(p, len); |
| 535 | } |
| 536 | |
| 537 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
| 538 | { |
| 539 | struct md5_ctx md5_ctx; |
| 540 | |
| 541 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
| 542 | md5_update(&md5_ctx, p, len); |
| 543 | memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); |
| 544 | } |
| 545 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 546 | unsigned int hweight32(unsigned int w) |
| 547 | { |
| 548 | unsigned int res = w - ((w >> 1) & 0x55555555); |
| 549 | |
| 550 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); |
| 551 | res = (res + (res >> 4)) & 0x0F0F0F0F; |
| 552 | res = res + (res >> 8); |
| 553 | |
| 554 | return (res + (res >> 16)) & 0x000000FF; |
| 555 | } |
| 556 | |
| 557 | unsigned long hweight64(unsigned long long w) |
| 558 | { |
| 559 | #if __WORDSIZE == 32 |
| 560 | return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w); |
| 561 | #elif __WORDSIZE == 64 |
| 562 | unsigned long long v = w - ((w >> 1) & 0x5555555555555555ul); |
| 563 | |
| 564 | v = (v & 0x3333333333333333ul) + ((v >> 2) & 0x3333333333333333ul); |
| 565 | v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0Ful; |
| 566 | v = v + (v >> 8); |
| 567 | v = v + (v >> 16); |
| 568 | |
| 569 | return (v + (v >> 32)) & 0x00000000000000FFul; |
| 570 | #else |
| 571 | #error __WORDSIZE not defined |
| 572 | #endif |
| 573 | } |
| 574 | |
| 575 | static int get_rw_ddir(struct thread_data *td) |
| 576 | { |
| 577 | /* |
| 578 | * perhaps cheasy, but use the hamming weight of the position |
| 579 | * as a randomizer for data direction. |
| 580 | */ |
| 581 | if (td_rw(td)) |
| 582 | return hweight64(td->last_pos) & 1; |
| 583 | else if (td_read(td)) |
| 584 | return DDIR_READ; |
| 585 | else |
| 586 | return DDIR_WRITE; |
| 587 | } |
| 588 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 589 | /* |
| 590 | * fill body of io_u->buf with random data and add a header with the |
| 591 | * (eg) sha1sum of that data. |
| 592 | */ |
| 593 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) |
| 594 | { |
| 595 | unsigned char *p = (unsigned char *) io_u->buf; |
| 596 | struct verify_header hdr; |
| 597 | |
| 598 | hdr.fio_magic = FIO_HDR_MAGIC; |
| 599 | hdr.len = io_u->buflen; |
| 600 | p += sizeof(hdr); |
| 601 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); |
| 602 | |
| 603 | if (td->verify == VERIFY_MD5) { |
| 604 | fill_md5(&hdr, p, io_u->buflen - sizeof(hdr)); |
| 605 | hdr.verify_type = VERIFY_MD5; |
| 606 | } else { |
| 607 | fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr)); |
| 608 | hdr.verify_type = VERIFY_CRC32; |
| 609 | } |
| 610 | |
| 611 | memcpy(io_u->buf, &hdr, sizeof(hdr)); |
| 612 | } |
| 613 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 614 | 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] | 615 | { |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 616 | if (td->io_prep && td->io_prep(td, io_u)) |
| 617 | return 1; |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 622 | 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] | 623 | { |
| 624 | list_del(&io_u->list); |
| 625 | list_add(&io_u->list, &td->io_u_freelist); |
| 626 | td->cur_depth--; |
| 627 | } |
| 628 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 629 | static int fill_io_u(struct thread_data *td, struct io_u *io_u) |
| 630 | { |
| 631 | /* |
| 632 | * If using an iolog, grab next piece if any available. |
| 633 | */ |
| 634 | if (td->iolog) { |
| 635 | struct io_piece *ipo; |
| 636 | |
| 637 | if (list_empty(&td->io_log_list)) |
| 638 | return 1; |
| 639 | |
| 640 | ipo = list_entry(td->io_log_list.next, struct io_piece, list); |
| 641 | list_del(&ipo->list); |
| 642 | io_u->offset = ipo->offset; |
| 643 | io_u->buflen = ipo->len; |
| 644 | io_u->ddir = ipo->ddir; |
| 645 | free(ipo); |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * No log, let the seq/rand engine retrieve the next position. |
| 651 | */ |
| 652 | if (!get_next_offset(td, &io_u->offset)) { |
| 653 | io_u->buflen = get_next_buflen(td); |
| 654 | |
| 655 | if (io_u->buflen) { |
| 656 | io_u->ddir = get_rw_ddir(td); |
| 657 | return 0; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return 1; |
| 662 | } |
| 663 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 664 | #define queue_full(td) (list_empty(&(td)->io_u_freelist)) |
| 665 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 666 | struct io_u *__get_io_u(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 667 | { |
| 668 | struct io_u *io_u; |
| 669 | |
| 670 | if (queue_full(td)) |
| 671 | return NULL; |
| 672 | |
| 673 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); |
| 674 | io_u->error = 0; |
| 675 | io_u->resid = 0; |
| 676 | list_del(&io_u->list); |
| 677 | list_add(&io_u->list, &td->io_u_busylist); |
| 678 | td->cur_depth++; |
| 679 | return io_u; |
| 680 | } |
| 681 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 682 | static struct io_u *get_io_u(struct thread_data *td) |
| 683 | { |
| 684 | struct io_u *io_u; |
| 685 | |
| 686 | io_u = __get_io_u(td); |
| 687 | if (!io_u) |
| 688 | return NULL; |
| 689 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 690 | if (td->zone_bytes >= td->zone_size) { |
| 691 | td->zone_bytes = 0; |
| 692 | td->last_pos += td->zone_skip; |
| 693 | } |
| 694 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 695 | if (fill_io_u(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 696 | put_io_u(td, io_u); |
| 697 | return NULL; |
| 698 | } |
| 699 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 700 | if (io_u->buflen + io_u->offset > td->real_file_size) |
| 701 | io_u->buflen = td->real_file_size - io_u->offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 702 | |
| 703 | if (!io_u->buflen) { |
| 704 | put_io_u(td, io_u); |
| 705 | return NULL; |
| 706 | } |
| 707 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 708 | if (!td->iolog && !td->sequential) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 709 | mark_random_map(td, io_u); |
| 710 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 711 | td->last_pos += io_u->buflen; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 712 | |
| 713 | if (td->verify != VERIFY_NONE) |
| 714 | populate_io_u(td, io_u); |
| 715 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 716 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 717 | put_io_u(td, io_u); |
| 718 | return NULL; |
| 719 | } |
| 720 | |
| 721 | gettimeofday(&io_u->start_time, NULL); |
| 722 | return io_u; |
| 723 | } |
| 724 | |
| 725 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
| 726 | { |
| 727 | td->old_runstate = td->runstate; |
| 728 | td->runstate = runstate; |
| 729 | } |
| 730 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 731 | 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] | 732 | { |
| 733 | struct io_piece *ipo; |
| 734 | |
| 735 | if (list_empty(&td->io_hist_list)) |
| 736 | return 1; |
| 737 | |
| 738 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
| 739 | list_del(&ipo->list); |
| 740 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 741 | io_u->offset = ipo->offset; |
| 742 | io_u->buflen = ipo->len; |
| 743 | io_u->ddir = DDIR_READ; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 744 | free(ipo); |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static void prune_io_piece_log(struct thread_data *td) |
| 749 | { |
| 750 | struct io_piece *ipo; |
| 751 | |
| 752 | while (!list_empty(&td->io_hist_list)) { |
| 753 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
| 754 | |
| 755 | list_del(&ipo->list); |
| 756 | free(ipo); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * log a succesful write, so we can unwind the log for verify |
| 762 | */ |
| 763 | static void log_io_piece(struct thread_data *td, struct io_u *io_u) |
| 764 | { |
| 765 | struct io_piece *ipo = malloc(sizeof(struct io_piece)); |
| 766 | struct list_head *entry; |
| 767 | |
| 768 | INIT_LIST_HEAD(&ipo->list); |
| 769 | ipo->offset = io_u->offset; |
| 770 | ipo->len = io_u->buflen; |
| 771 | |
| 772 | /* |
| 773 | * for random io where the writes extend the file, it will typically |
| 774 | * be laid out with the block scattered as written. it's faster to |
| 775 | * read them in in that order again, so don't sort |
| 776 | */ |
| 777 | if (td->sequential || !td->overwrite) { |
| 778 | list_add_tail(&ipo->list, &td->io_hist_list); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * for random io, sort the list so verify will run faster |
| 784 | */ |
| 785 | entry = &td->io_hist_list; |
| 786 | while ((entry = entry->prev) != &td->io_hist_list) { |
| 787 | struct io_piece *__ipo = list_entry(entry, struct io_piece, list); |
| 788 | |
| 789 | if (__ipo->offset < ipo->offset) |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | list_add(&ipo->list, entry); |
| 794 | } |
| 795 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 796 | static int init_iolog(struct thread_data *td) |
| 797 | { |
| 798 | unsigned long long offset; |
| 799 | unsigned int bytes; |
| 800 | char *str, *p; |
| 801 | FILE *f; |
| 802 | int rw, i, reads, writes; |
| 803 | |
| 804 | if (!td->iolog) |
| 805 | return 0; |
| 806 | |
| 807 | f = fopen(td->iolog_file, "r"); |
| 808 | if (!f) { |
| 809 | perror("fopen iolog"); |
| 810 | return 1; |
| 811 | } |
| 812 | |
| 813 | str = malloc(4096); |
| 814 | reads = writes = i = 0; |
| 815 | while ((p = fgets(str, 4096, f)) != NULL) { |
| 816 | struct io_piece *ipo; |
| 817 | |
| 818 | if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) { |
| 819 | fprintf(stderr, "bad iolog: %s\n", p); |
| 820 | continue; |
| 821 | } |
| 822 | if (rw == DDIR_READ) |
| 823 | reads++; |
| 824 | else if (rw == DDIR_WRITE) |
| 825 | writes++; |
| 826 | else { |
| 827 | fprintf(stderr, "bad ddir: %d\n", rw); |
| 828 | continue; |
| 829 | } |
| 830 | |
| 831 | ipo = malloc(sizeof(*ipo)); |
| 832 | INIT_LIST_HEAD(&ipo->list); |
| 833 | ipo->offset = offset; |
| 834 | ipo->len = bytes; |
| 835 | if (bytes > td->max_bs) |
| 836 | td->max_bs = bytes; |
| 837 | ipo->ddir = rw; |
| 838 | list_add_tail(&ipo->list, &td->io_log_list); |
| 839 | i++; |
| 840 | } |
| 841 | |
| 842 | free(str); |
| 843 | fclose(f); |
| 844 | |
| 845 | if (!i) |
| 846 | return 1; |
| 847 | |
| 848 | if (reads && !writes) |
| 849 | td->ddir = DDIR_READ; |
| 850 | else if (!reads && writes) |
| 851 | td->ddir = DDIR_READ; |
| 852 | else |
| 853 | td->iomix = 1; |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 858 | static int sync_td(struct thread_data *td) |
| 859 | { |
| 860 | if (td->io_sync) |
| 861 | return td->io_sync(td); |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | static int io_u_getevents(struct thread_data *td, int min, int max, |
| 867 | struct timespec *t) |
| 868 | { |
| 869 | return td->io_getevents(td, min, max, t); |
| 870 | } |
| 871 | |
| 872 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) |
| 873 | { |
| 874 | gettimeofday(&io_u->issue_time, NULL); |
| 875 | |
| 876 | return td->io_queue(td, io_u); |
| 877 | } |
| 878 | |
| 879 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
| 880 | |
| 881 | static void io_completed(struct thread_data *td, struct io_u *io_u, |
| 882 | struct io_completion_data *icd) |
| 883 | { |
| 884 | struct timeval e; |
| 885 | unsigned long msec; |
| 886 | |
| 887 | gettimeofday(&e, NULL); |
| 888 | |
| 889 | if (!io_u->error) { |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 890 | unsigned int bytes = io_u->buflen - io_u->resid; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 891 | const int idx = io_u->ddir; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 892 | |
| 893 | td->io_blocks[idx]++; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 894 | td->io_bytes[idx] += bytes; |
| 895 | td->zone_bytes += bytes; |
| 896 | td->this_io_bytes[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 897 | |
| 898 | msec = mtime_since(&io_u->issue_time, &e); |
| 899 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 900 | add_clat_sample(td, idx, msec); |
| 901 | add_bw_sample(td, idx); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 902 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 903 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 904 | log_io_piece(td, io_u); |
| 905 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 906 | icd->bytes_done[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 907 | } else |
| 908 | icd->error = io_u->error; |
| 909 | } |
| 910 | |
| 911 | static void ios_completed(struct thread_data *td,struct io_completion_data *icd) |
| 912 | { |
| 913 | struct io_u *io_u; |
| 914 | int i; |
| 915 | |
| 916 | icd->error = 0; |
| 917 | icd->bytes_done[0] = icd->bytes_done[1] = 0; |
| 918 | |
| 919 | for (i = 0; i < icd->nr; i++) { |
| 920 | io_u = td->io_event(td, i); |
| 921 | |
| 922 | io_completed(td, io_u, icd); |
| 923 | put_io_u(td, io_u); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | static void cleanup_pending_aio(struct thread_data *td) |
| 928 | { |
| 929 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 930 | struct list_head *entry, *n; |
| 931 | struct io_completion_data icd; |
| 932 | struct io_u *io_u; |
| 933 | int r; |
| 934 | |
| 935 | /* |
| 936 | * get immediately available events, if any |
| 937 | */ |
| 938 | r = io_u_getevents(td, 0, td->cur_depth, &ts); |
| 939 | if (r > 0) { |
| 940 | icd.nr = r; |
| 941 | ios_completed(td, &icd); |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * now cancel remaining active events |
| 946 | */ |
| 947 | if (td->io_cancel) { |
| 948 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
| 949 | io_u = list_entry(entry, struct io_u, list); |
| 950 | |
| 951 | r = td->io_cancel(td, io_u); |
| 952 | if (!r) |
| 953 | put_io_u(td, io_u); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | if (td->cur_depth) { |
| 958 | r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL); |
| 959 | if (r > 0) { |
| 960 | icd.nr = r; |
| 961 | ios_completed(td, &icd); |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | static int do_io_u_verify(struct thread_data *td, struct io_u **io_u) |
| 967 | { |
| 968 | struct io_u *v_io_u = *io_u; |
| 969 | int ret = 0; |
| 970 | |
| 971 | if (v_io_u) { |
| 972 | ret = verify_io_u(v_io_u); |
| 973 | put_io_u(td, v_io_u); |
| 974 | *io_u = NULL; |
| 975 | } |
| 976 | |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | static void do_verify(struct thread_data *td) |
| 981 | { |
| 982 | struct timeval t; |
| 983 | struct io_u *io_u, *v_io_u = NULL; |
| 984 | struct io_completion_data icd; |
| 985 | int ret; |
| 986 | |
| 987 | td_set_runstate(td, TD_VERIFYING); |
| 988 | |
| 989 | do { |
| 990 | if (td->terminate) |
| 991 | break; |
| 992 | |
| 993 | gettimeofday(&t, NULL); |
| 994 | if (runtime_exceeded(td, &t)) |
| 995 | break; |
| 996 | |
| 997 | io_u = __get_io_u(td); |
| 998 | if (!io_u) |
| 999 | break; |
| 1000 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1001 | if (get_next_verify(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1002 | put_io_u(td, io_u); |
| 1003 | break; |
| 1004 | } |
| 1005 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1006 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1007 | put_io_u(td, io_u); |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | ret = io_u_queue(td, io_u); |
| 1012 | if (ret) { |
| 1013 | put_io_u(td, io_u); |
| 1014 | td_verror(td, ret); |
| 1015 | break; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * we have one pending to verify, do that while |
| 1020 | * we are doing io on the next one |
| 1021 | */ |
| 1022 | if (do_io_u_verify(td, &v_io_u)) |
| 1023 | break; |
| 1024 | |
| 1025 | ret = io_u_getevents(td, 1, 1, NULL); |
| 1026 | if (ret != 1) { |
| 1027 | if (ret < 0) |
| 1028 | td_verror(td, ret); |
| 1029 | break; |
| 1030 | } |
| 1031 | |
| 1032 | v_io_u = td->io_event(td, 0); |
| 1033 | icd.nr = 1; |
| 1034 | icd.error = 0; |
| 1035 | io_completed(td, v_io_u, &icd); |
| 1036 | |
| 1037 | if (icd.error) { |
| 1038 | td_verror(td, icd.error); |
| 1039 | put_io_u(td, v_io_u); |
| 1040 | v_io_u = NULL; |
| 1041 | break; |
| 1042 | } |
| 1043 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1044 | /* |
| 1045 | * if we can't submit more io, we need to verify now |
| 1046 | */ |
| 1047 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) |
| 1048 | break; |
| 1049 | |
| 1050 | } while (1); |
| 1051 | |
| 1052 | do_io_u_verify(td, &v_io_u); |
| 1053 | |
| 1054 | if (td->cur_depth) |
| 1055 | cleanup_pending_aio(td); |
| 1056 | |
| 1057 | td_set_runstate(td, TD_RUNNING); |
| 1058 | } |
| 1059 | |
| 1060 | static void do_io(struct thread_data *td) |
| 1061 | { |
| 1062 | struct io_completion_data icd; |
| 1063 | struct timeval s, e; |
| 1064 | unsigned long usec; |
| 1065 | |
| 1066 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
| 1067 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 1068 | struct timespec *timeout; |
| 1069 | int ret, min_evts = 0; |
| 1070 | struct io_u *io_u; |
| 1071 | |
| 1072 | if (td->terminate) |
| 1073 | break; |
| 1074 | |
| 1075 | io_u = get_io_u(td); |
| 1076 | if (!io_u) |
| 1077 | break; |
| 1078 | |
| 1079 | memcpy(&s, &io_u->start_time, sizeof(s)); |
| 1080 | |
| 1081 | ret = io_u_queue(td, io_u); |
| 1082 | if (ret) { |
| 1083 | put_io_u(td, io_u); |
| 1084 | td_verror(td, ret); |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); |
| 1089 | |
| 1090 | if (td->cur_depth < td->iodepth) { |
| 1091 | timeout = &ts; |
| 1092 | min_evts = 0; |
| 1093 | } else { |
| 1094 | timeout = NULL; |
| 1095 | min_evts = 1; |
| 1096 | } |
| 1097 | |
| 1098 | ret = io_u_getevents(td, min_evts, td->cur_depth, timeout); |
| 1099 | if (ret < 0) { |
| 1100 | td_verror(td, ret); |
| 1101 | break; |
| 1102 | } else if (!ret) |
| 1103 | continue; |
| 1104 | |
| 1105 | icd.nr = ret; |
| 1106 | ios_completed(td, &icd); |
| 1107 | if (icd.error) { |
| 1108 | td_verror(td, icd.error); |
| 1109 | break; |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * the rate is batched for now, it should work for batches |
| 1114 | * of completions except the very first one which may look |
| 1115 | * a little bursty |
| 1116 | */ |
| 1117 | gettimeofday(&e, NULL); |
| 1118 | usec = utime_since(&s, &e); |
| 1119 | |
| 1120 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); |
| 1121 | |
| 1122 | if (check_min_rate(td, &e)) { |
| 1123 | td_verror(td, ENOMEM); |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | if (runtime_exceeded(td, &e)) |
| 1128 | break; |
| 1129 | |
| 1130 | if (td->thinktime) |
| 1131 | usec_sleep(td, td->thinktime); |
| 1132 | |
| 1133 | if (should_fsync(td) && td->fsync_blocks && |
| 1134 | (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0) |
| 1135 | sync_td(td); |
| 1136 | } |
| 1137 | |
| 1138 | if (td->cur_depth) |
| 1139 | cleanup_pending_aio(td); |
| 1140 | |
Jens Axboe | fc1a471 | 2006-05-30 13:04:05 +0200 | [diff] [blame] | 1141 | if (should_fsync(td) && td->end_fsync) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1142 | sync_td(td); |
| 1143 | } |
| 1144 | |
| 1145 | static void cleanup_io(struct thread_data *td) |
| 1146 | { |
| 1147 | if (td->io_cleanup) |
| 1148 | td->io_cleanup(td); |
| 1149 | } |
| 1150 | |
| 1151 | static int init_io(struct thread_data *td) |
| 1152 | { |
| 1153 | if (td->io_engine == FIO_SYNCIO) |
| 1154 | return fio_syncio_init(td); |
| 1155 | else if (td->io_engine == FIO_MMAPIO) |
| 1156 | return fio_mmapio_init(td); |
| 1157 | else if (td->io_engine == FIO_LIBAIO) |
| 1158 | return fio_libaio_init(td); |
| 1159 | else if (td->io_engine == FIO_POSIXAIO) |
| 1160 | return fio_posixaio_init(td); |
| 1161 | else if (td->io_engine == FIO_SGIO) |
| 1162 | return fio_sgio_init(td); |
Jens Axboe | 8756e4d | 2006-05-27 20:24:53 +0200 | [diff] [blame] | 1163 | else if (td->io_engine == FIO_SPLICEIO) |
| 1164 | return fio_spliceio_init(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1165 | else { |
| 1166 | fprintf(stderr, "bad io_engine %d\n", td->io_engine); |
| 1167 | return 1; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | static void cleanup_io_u(struct thread_data *td) |
| 1172 | { |
| 1173 | struct list_head *entry, *n; |
| 1174 | struct io_u *io_u; |
| 1175 | |
| 1176 | list_for_each_safe(entry, n, &td->io_u_freelist) { |
| 1177 | io_u = list_entry(entry, struct io_u, list); |
| 1178 | |
| 1179 | list_del(&io_u->list); |
| 1180 | free(io_u); |
| 1181 | } |
| 1182 | |
| 1183 | if (td->mem_type == MEM_MALLOC) |
| 1184 | free(td->orig_buffer); |
| 1185 | else if (td->mem_type == MEM_SHM) { |
| 1186 | struct shmid_ds sbuf; |
| 1187 | |
| 1188 | shmdt(td->orig_buffer); |
| 1189 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
| 1190 | } else if (td->mem_type == MEM_MMAP) |
| 1191 | munmap(td->orig_buffer, td->orig_buffer_size); |
| 1192 | else |
| 1193 | fprintf(stderr, "Bad memory type %d\n", td->mem_type); |
| 1194 | |
| 1195 | td->orig_buffer = NULL; |
| 1196 | } |
| 1197 | |
| 1198 | static int init_io_u(struct thread_data *td) |
| 1199 | { |
| 1200 | struct io_u *io_u; |
| 1201 | int i, max_units; |
| 1202 | char *p; |
| 1203 | |
| 1204 | if (td->io_engine & FIO_SYNCIO) |
| 1205 | max_units = 1; |
| 1206 | else |
| 1207 | max_units = td->iodepth; |
| 1208 | |
| 1209 | td->orig_buffer_size = td->max_bs * max_units + MASK; |
| 1210 | |
| 1211 | if (td->mem_type == MEM_MALLOC) |
| 1212 | td->orig_buffer = malloc(td->orig_buffer_size); |
| 1213 | else if (td->mem_type == MEM_SHM) { |
| 1214 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); |
| 1215 | if (td->shm_id < 0) { |
| 1216 | td_verror(td, errno); |
| 1217 | perror("shmget"); |
| 1218 | return 1; |
| 1219 | } |
| 1220 | |
| 1221 | td->orig_buffer = shmat(td->shm_id, NULL, 0); |
| 1222 | if (td->orig_buffer == (void *) -1) { |
| 1223 | td_verror(td, errno); |
| 1224 | perror("shmat"); |
| 1225 | td->orig_buffer = NULL; |
| 1226 | return 1; |
| 1227 | } |
| 1228 | } else if (td->mem_type == MEM_MMAP) { |
| 1229 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 1230 | if (td->orig_buffer == MAP_FAILED) { |
| 1231 | td_verror(td, errno); |
| 1232 | perror("mmap"); |
| 1233 | td->orig_buffer = NULL; |
| 1234 | return 1; |
| 1235 | } |
| 1236 | } |
| 1237 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1238 | p = ALIGN(td->orig_buffer); |
| 1239 | for (i = 0; i < max_units; i++) { |
| 1240 | io_u = malloc(sizeof(*io_u)); |
| 1241 | memset(io_u, 0, sizeof(*io_u)); |
| 1242 | INIT_LIST_HEAD(&io_u->list); |
| 1243 | |
| 1244 | io_u->buf = p + td->max_bs * i; |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 1245 | io_u->index = i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1246 | list_add(&io_u->list, &td->io_u_freelist); |
| 1247 | } |
| 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | static int create_file(struct thread_data *td, unsigned long long size, |
| 1253 | int extend) |
| 1254 | { |
| 1255 | unsigned long long left; |
| 1256 | unsigned int bs; |
| 1257 | int r, oflags; |
| 1258 | char *b; |
| 1259 | |
| 1260 | /* |
| 1261 | * unless specifically asked for overwrite, let normal io extend it |
| 1262 | */ |
| 1263 | if (td_write(td) && !td->overwrite) |
| 1264 | return 0; |
| 1265 | |
| 1266 | if (!size) { |
| 1267 | fprintf(stderr, "Need size for create\n"); |
| 1268 | td_verror(td, EINVAL); |
| 1269 | return 1; |
| 1270 | } |
| 1271 | |
| 1272 | if (!extend) { |
| 1273 | oflags = O_CREAT | O_TRUNC; |
| 1274 | printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20); |
| 1275 | } else { |
| 1276 | oflags = O_APPEND; |
| 1277 | printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20); |
| 1278 | } |
| 1279 | |
| 1280 | td->fd = open(td->file_name, O_WRONLY | oflags, 0644); |
| 1281 | if (td->fd < 0) { |
| 1282 | td_verror(td, errno); |
| 1283 | return 1; |
| 1284 | } |
| 1285 | |
| 1286 | if (!extend && ftruncate(td->fd, td->file_size) == -1) { |
| 1287 | td_verror(td, errno); |
| 1288 | return 1; |
| 1289 | } |
| 1290 | |
| 1291 | td->io_size = td->file_size; |
| 1292 | b = malloc(td->max_bs); |
| 1293 | memset(b, 0, td->max_bs); |
| 1294 | |
| 1295 | left = size; |
| 1296 | while (left && !td->terminate) { |
| 1297 | bs = td->max_bs; |
| 1298 | if (bs > left) |
| 1299 | bs = left; |
| 1300 | |
| 1301 | r = write(td->fd, b, bs); |
| 1302 | |
| 1303 | if (r == (int) bs) { |
| 1304 | left -= bs; |
| 1305 | continue; |
| 1306 | } else { |
| 1307 | if (r < 0) |
| 1308 | td_verror(td, errno); |
| 1309 | else |
| 1310 | td_verror(td, EIO); |
| 1311 | |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | if (td->terminate) |
| 1317 | unlink(td->file_name); |
| 1318 | else if (td->create_fsync) |
| 1319 | fsync(td->fd); |
| 1320 | |
| 1321 | close(td->fd); |
| 1322 | td->fd = -1; |
| 1323 | free(b); |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | static int file_size(struct thread_data *td) |
| 1328 | { |
| 1329 | struct stat st; |
| 1330 | |
| 1331 | if (fstat(td->fd, &st) == -1) { |
| 1332 | td_verror(td, errno); |
| 1333 | return 1; |
| 1334 | } |
| 1335 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1336 | td->real_file_size = st.st_size; |
| 1337 | |
| 1338 | if (!td->file_size || td->file_size > td->real_file_size) |
| 1339 | td->file_size = td->real_file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1340 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1341 | td->file_size -= td->file_offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | static int bdev_size(struct thread_data *td) |
| 1346 | { |
Jens Axboe | 9104f87 | 2005-12-28 23:50:45 +0100 | [diff] [blame] | 1347 | unsigned long long bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1348 | int r; |
| 1349 | |
| 1350 | r = blockdev_size(td->fd, &bytes); |
| 1351 | if (r) { |
| 1352 | td_verror(td, r); |
| 1353 | return 1; |
| 1354 | } |
| 1355 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1356 | td->real_file_size = bytes; |
| 1357 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1358 | /* |
| 1359 | * no extend possibilities, so limit size to device size if too large |
| 1360 | */ |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1361 | if (!td->file_size || td->file_size > td->real_file_size) |
| 1362 | td->file_size = td->real_file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1363 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1364 | td->file_size -= td->file_offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | static int get_file_size(struct thread_data *td) |
| 1369 | { |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1370 | int ret = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1371 | |
| 1372 | if (td->filetype == FIO_TYPE_FILE) |
| 1373 | ret = file_size(td); |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1374 | else if (td->filetype == FIO_TYPE_BD) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1375 | ret = bdev_size(td); |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1376 | else |
| 1377 | td->real_file_size = -1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1378 | |
| 1379 | if (ret) |
| 1380 | return ret; |
| 1381 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1382 | if (td->file_offset > td->real_file_size) { |
| 1383 | fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1384 | return 1; |
| 1385 | } |
| 1386 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1387 | td->io_size = td->file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1388 | if (td->io_size == 0) { |
| 1389 | fprintf(stderr, "Client%d: no io blocks\n", td->thread_number); |
| 1390 | td_verror(td, EINVAL); |
| 1391 | return 1; |
| 1392 | } |
| 1393 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1394 | if (!td->zone_size) |
| 1395 | td->zone_size = td->io_size; |
| 1396 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1397 | td->total_io_size = td->io_size * td->loops; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | static int setup_file_mmap(struct thread_data *td) |
| 1402 | { |
| 1403 | int flags; |
| 1404 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1405 | if (td_rw(td)) |
| 1406 | flags = PROT_READ | PROT_WRITE; |
| 1407 | else if (td_write(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1408 | flags = PROT_WRITE; |
| 1409 | |
| 1410 | if (td->verify != VERIFY_NONE) |
| 1411 | flags |= PROT_READ; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1412 | } else |
| 1413 | flags = PROT_READ; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1414 | |
| 1415 | td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset); |
| 1416 | if (td->mmap == MAP_FAILED) { |
| 1417 | td->mmap = NULL; |
| 1418 | td_verror(td, errno); |
| 1419 | return 1; |
| 1420 | } |
| 1421 | |
| 1422 | if (td->invalidate_cache) { |
| 1423 | if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) { |
| 1424 | td_verror(td, errno); |
| 1425 | return 1; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | if (td->sequential) { |
| 1430 | if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) { |
| 1431 | td_verror(td, errno); |
| 1432 | return 1; |
| 1433 | } |
| 1434 | } else { |
| 1435 | if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) { |
| 1436 | td_verror(td, errno); |
| 1437 | return 1; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | static int setup_file_plain(struct thread_data *td) |
| 1445 | { |
| 1446 | if (td->invalidate_cache) { |
| 1447 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) { |
| 1448 | td_verror(td, errno); |
| 1449 | return 1; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | if (td->sequential) { |
| 1454 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) { |
| 1455 | td_verror(td, errno); |
| 1456 | return 1; |
| 1457 | } |
| 1458 | } else { |
| 1459 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) { |
| 1460 | td_verror(td, errno); |
| 1461 | return 1; |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | static int setup_file(struct thread_data *td) |
| 1469 | { |
| 1470 | struct stat st; |
| 1471 | int flags = 0; |
| 1472 | |
| 1473 | if (stat(td->file_name, &st) == -1) { |
| 1474 | if (errno != ENOENT) { |
| 1475 | td_verror(td, errno); |
| 1476 | return 1; |
| 1477 | } |
| 1478 | if (!td->create_file) { |
| 1479 | td_verror(td, ENOENT); |
| 1480 | return 1; |
| 1481 | } |
| 1482 | if (create_file(td, td->file_size, 0)) |
| 1483 | return 1; |
| 1484 | } else if (td->filetype == FIO_TYPE_FILE) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1485 | if (st.st_size < (off_t) td->file_size) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1486 | if (create_file(td, td->file_size - st.st_size, 1)) |
| 1487 | return 1; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | if (td->odirect) |
| 1492 | flags |= O_DIRECT; |
| 1493 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1494 | if (td_write(td) || td_rw(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1495 | if (td->filetype == FIO_TYPE_FILE) { |
| 1496 | if (!td->overwrite) |
| 1497 | flags |= O_TRUNC; |
| 1498 | |
| 1499 | flags |= O_CREAT; |
| 1500 | } |
| 1501 | if (td->sync_io) |
| 1502 | flags |= O_SYNC; |
| 1503 | |
| 1504 | flags |= O_RDWR; |
| 1505 | |
| 1506 | td->fd = open(td->file_name, flags, 0600); |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1507 | } else { |
| 1508 | if (td->filetype == FIO_TYPE_CHAR) |
| 1509 | flags |= O_RDWR; |
| 1510 | else |
| 1511 | flags |= O_RDONLY; |
| 1512 | |
| 1513 | td->fd = open(td->file_name, flags); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | if (td->fd == -1) { |
| 1517 | td_verror(td, errno); |
| 1518 | return 1; |
| 1519 | } |
| 1520 | |
| 1521 | if (get_file_size(td)) |
| 1522 | return 1; |
| 1523 | |
| 1524 | if (td->io_engine != FIO_MMAPIO) |
| 1525 | return setup_file_plain(td); |
| 1526 | else |
| 1527 | return setup_file_mmap(td); |
| 1528 | } |
| 1529 | |
| 1530 | static int check_dev_match(dev_t dev, char *path) |
| 1531 | { |
| 1532 | unsigned int major, minor; |
| 1533 | char line[256], *p; |
| 1534 | FILE *f; |
| 1535 | |
| 1536 | f = fopen(path, "r"); |
| 1537 | if (!f) { |
| 1538 | perror("open path"); |
| 1539 | return 1; |
| 1540 | } |
| 1541 | |
| 1542 | p = fgets(line, sizeof(line), f); |
| 1543 | if (!p) { |
| 1544 | fclose(f); |
| 1545 | return 1; |
| 1546 | } |
| 1547 | |
| 1548 | if (sscanf(p, "%u:%u", &major, &minor) != 2) { |
| 1549 | fclose(f); |
| 1550 | return 1; |
| 1551 | } |
| 1552 | |
| 1553 | if (((major << 8) | minor) == dev) { |
| 1554 | fclose(f); |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | fclose(f); |
| 1559 | return 1; |
| 1560 | } |
| 1561 | |
| 1562 | static int find_block_dir(dev_t dev, char *path) |
| 1563 | { |
| 1564 | struct dirent *dir; |
| 1565 | struct stat st; |
| 1566 | int found = 0; |
| 1567 | DIR *D; |
| 1568 | |
| 1569 | D = opendir(path); |
| 1570 | if (!D) |
| 1571 | return 0; |
| 1572 | |
| 1573 | while ((dir = readdir(D)) != NULL) { |
| 1574 | char full_path[256]; |
| 1575 | |
| 1576 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 1577 | continue; |
| 1578 | if (!strcmp(dir->d_name, "device")) |
| 1579 | continue; |
| 1580 | |
| 1581 | sprintf(full_path, "%s/%s", path, dir->d_name); |
| 1582 | |
| 1583 | if (!strcmp(dir->d_name, "dev")) { |
| 1584 | if (!check_dev_match(dev, full_path)) { |
| 1585 | found = 1; |
| 1586 | break; |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | if (stat(full_path, &st) == -1) { |
| 1591 | perror("stat"); |
| 1592 | break; |
| 1593 | } |
| 1594 | |
| 1595 | if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) |
| 1596 | continue; |
| 1597 | |
| 1598 | found = find_block_dir(dev, full_path); |
| 1599 | if (found) { |
| 1600 | strcpy(path, full_path); |
| 1601 | break; |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | closedir(D); |
| 1606 | return found; |
| 1607 | } |
| 1608 | |
| 1609 | static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus) |
| 1610 | { |
| 1611 | unsigned in_flight; |
| 1612 | char line[256]; |
| 1613 | FILE *f; |
| 1614 | char *p; |
| 1615 | |
| 1616 | f = fopen(du->path, "r"); |
| 1617 | if (!f) |
| 1618 | return 1; |
| 1619 | |
| 1620 | p = fgets(line, sizeof(line), f); |
| 1621 | if (!p) { |
| 1622 | fclose(f); |
| 1623 | return 1; |
| 1624 | } |
| 1625 | |
| 1626 | if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) { |
| 1627 | fclose(f); |
| 1628 | return 1; |
| 1629 | } |
| 1630 | |
| 1631 | fclose(f); |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | static void update_io_tick_disk(struct disk_util *du) |
| 1636 | { |
| 1637 | struct disk_util_stat __dus, *dus, *ldus; |
| 1638 | struct timeval t; |
| 1639 | |
| 1640 | if (get_io_ticks(du, &__dus)) |
| 1641 | return; |
| 1642 | |
| 1643 | dus = &du->dus; |
| 1644 | ldus = &du->last_dus; |
| 1645 | |
| 1646 | dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]); |
| 1647 | dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]); |
| 1648 | dus->ios[0] += (__dus.ios[0] - ldus->ios[0]); |
| 1649 | dus->ios[1] += (__dus.ios[1] - ldus->ios[1]); |
| 1650 | dus->merges[0] += (__dus.merges[0] - ldus->merges[0]); |
| 1651 | dus->merges[1] += (__dus.merges[1] - ldus->merges[1]); |
| 1652 | dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]); |
| 1653 | dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]); |
| 1654 | dus->io_ticks += (__dus.io_ticks - ldus->io_ticks); |
| 1655 | dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue); |
| 1656 | |
| 1657 | gettimeofday(&t, NULL); |
| 1658 | du->msec += mtime_since(&du->time, &t); |
| 1659 | memcpy(&du->time, &t, sizeof(t)); |
| 1660 | memcpy(ldus, &__dus, sizeof(__dus)); |
| 1661 | } |
| 1662 | |
| 1663 | static void update_io_ticks(void) |
| 1664 | { |
| 1665 | struct list_head *entry; |
| 1666 | struct disk_util *du; |
| 1667 | |
| 1668 | list_for_each(entry, &disk_list) { |
| 1669 | du = list_entry(entry, struct disk_util, list); |
| 1670 | update_io_tick_disk(du); |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | static int disk_util_exists(dev_t dev) |
| 1675 | { |
| 1676 | struct list_head *entry; |
| 1677 | struct disk_util *du; |
| 1678 | |
| 1679 | list_for_each(entry, &disk_list) { |
| 1680 | du = list_entry(entry, struct disk_util, list); |
| 1681 | |
| 1682 | if (du->dev == dev) |
| 1683 | return 1; |
| 1684 | } |
| 1685 | |
| 1686 | return 0; |
| 1687 | } |
| 1688 | |
| 1689 | static void disk_util_add(dev_t dev, char *path) |
| 1690 | { |
| 1691 | struct disk_util *du = malloc(sizeof(*du)); |
| 1692 | |
| 1693 | memset(du, 0, sizeof(*du)); |
| 1694 | INIT_LIST_HEAD(&du->list); |
| 1695 | sprintf(du->path, "%s/stat", path); |
| 1696 | du->name = strdup(basename(path)); |
| 1697 | du->dev = dev; |
| 1698 | |
| 1699 | gettimeofday(&du->time, NULL); |
| 1700 | get_io_ticks(du, &du->last_dus); |
| 1701 | |
| 1702 | list_add_tail(&du->list, &disk_list); |
| 1703 | } |
| 1704 | |
| 1705 | static void init_disk_util(struct thread_data *td) |
| 1706 | { |
| 1707 | struct stat st; |
| 1708 | char foo[256], tmp[256]; |
| 1709 | dev_t dev; |
| 1710 | char *p; |
| 1711 | |
| 1712 | if (!td->do_disk_util) |
| 1713 | return; |
| 1714 | |
| 1715 | if (!stat(td->file_name, &st)) { |
| 1716 | if (S_ISBLK(st.st_mode)) |
| 1717 | dev = st.st_rdev; |
| 1718 | else |
| 1719 | dev = st.st_dev; |
| 1720 | } else { |
| 1721 | /* |
| 1722 | * must be a file, open "." in that path |
| 1723 | */ |
| 1724 | strcpy(foo, td->file_name); |
| 1725 | p = dirname(foo); |
| 1726 | if (stat(p, &st)) { |
| 1727 | perror("disk util stat"); |
| 1728 | return; |
| 1729 | } |
| 1730 | |
| 1731 | dev = st.st_dev; |
| 1732 | } |
| 1733 | |
| 1734 | if (disk_util_exists(dev)) |
| 1735 | return; |
| 1736 | |
| 1737 | sprintf(foo, "/sys/block"); |
| 1738 | if (!find_block_dir(dev, foo)) |
| 1739 | return; |
| 1740 | |
| 1741 | /* |
Jens Axboe | 64cb3e1 | 2006-01-26 11:04:48 -0800 | [diff] [blame] | 1742 | * for md/dm, there's no queue dir. we already have the right place |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1743 | */ |
Jens Axboe | 64cb3e1 | 2006-01-26 11:04:48 -0800 | [diff] [blame] | 1744 | sprintf(tmp, "%s/stat", foo); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1745 | if (stat(tmp, &st)) { |
Jens Axboe | 64cb3e1 | 2006-01-26 11:04:48 -0800 | [diff] [blame] | 1746 | /* |
| 1747 | * if this is inside a partition dir, jump back to parent |
| 1748 | */ |
| 1749 | sprintf(tmp, "%s/queue", foo); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1750 | if (stat(tmp, &st)) { |
Jens Axboe | 64cb3e1 | 2006-01-26 11:04:48 -0800 | [diff] [blame] | 1751 | p = dirname(foo); |
| 1752 | sprintf(tmp, "%s/queue", p); |
| 1753 | if (stat(tmp, &st)) { |
| 1754 | fprintf(stderr, "unknown sysfs layout\n"); |
| 1755 | return; |
| 1756 | } |
| 1757 | sprintf(foo, "%s", p); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1758 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | disk_util_add(dev, foo); |
| 1762 | } |
| 1763 | |
| 1764 | static void disk_util_timer_arm(void) |
| 1765 | { |
| 1766 | itimer.it_value.tv_sec = 0; |
| 1767 | itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000; |
| 1768 | setitimer(ITIMER_REAL, &itimer, NULL); |
| 1769 | } |
| 1770 | |
| 1771 | static void clear_io_state(struct thread_data *td) |
| 1772 | { |
| 1773 | if (td->io_engine == FIO_SYNCIO) |
| 1774 | lseek(td->fd, SEEK_SET, 0); |
| 1775 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1776 | td->last_pos = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1777 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
| 1778 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1779 | td->zone_bytes = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1780 | |
| 1781 | if (td->file_map) |
| 1782 | memset(td->file_map, 0, td->num_maps * sizeof(long)); |
| 1783 | } |
| 1784 | |
| 1785 | static void update_rusage_stat(struct thread_data *td) |
| 1786 | { |
| 1787 | if (!(td->runtime[0] + td->runtime[1])) |
| 1788 | return; |
| 1789 | |
| 1790 | getrusage(RUSAGE_SELF, &td->ru_end); |
| 1791 | |
| 1792 | td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime); |
| 1793 | td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime); |
| 1794 | td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw); |
| 1795 | |
| 1796 | |
| 1797 | memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end)); |
| 1798 | } |
| 1799 | |
| 1800 | static void *thread_main(void *data) |
| 1801 | { |
| 1802 | struct thread_data *td = data; |
| 1803 | int ret = 1; |
| 1804 | |
| 1805 | if (!td->use_thread) |
| 1806 | setsid(); |
| 1807 | |
| 1808 | td->pid = getpid(); |
| 1809 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1810 | INIT_LIST_HEAD(&td->io_u_freelist); |
| 1811 | INIT_LIST_HEAD(&td->io_u_busylist); |
| 1812 | INIT_LIST_HEAD(&td->io_hist_list); |
| 1813 | INIT_LIST_HEAD(&td->io_log_list); |
| 1814 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1815 | if (init_io_u(td)) |
| 1816 | goto err; |
| 1817 | |
| 1818 | if (fio_setaffinity(td) == -1) { |
| 1819 | td_verror(td, errno); |
| 1820 | goto err; |
| 1821 | } |
| 1822 | |
| 1823 | if (init_io(td)) |
| 1824 | goto err; |
| 1825 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1826 | if (init_iolog(td)) |
| 1827 | goto err; |
| 1828 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1829 | if (td->ioprio) { |
| 1830 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 1831 | td_verror(td, errno); |
| 1832 | goto err; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | sem_post(&startup_sem); |
| 1837 | sem_wait(&td->mutex); |
| 1838 | |
| 1839 | if (!td->create_serialize && setup_file(td)) |
| 1840 | goto err; |
| 1841 | |
| 1842 | if (init_random_state(td)) |
| 1843 | goto err; |
| 1844 | |
| 1845 | gettimeofday(&td->epoch, NULL); |
| 1846 | |
| 1847 | while (td->loops--) { |
| 1848 | getrusage(RUSAGE_SELF, &td->ru_start); |
| 1849 | gettimeofday(&td->start, NULL); |
| 1850 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); |
| 1851 | |
| 1852 | if (td->ratemin) |
| 1853 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); |
| 1854 | |
| 1855 | clear_io_state(td); |
| 1856 | prune_io_piece_log(td); |
| 1857 | |
| 1858 | do_io(td); |
| 1859 | |
| 1860 | td->runtime[td->ddir] += mtime_since_now(&td->start); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1861 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1862 | td->runtime[td->ddir ^ 1] = td->runtime[td->ddir]; |
| 1863 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1864 | update_rusage_stat(td); |
| 1865 | |
| 1866 | if (td->error || td->terminate) |
| 1867 | break; |
| 1868 | |
| 1869 | if (td->verify == VERIFY_NONE) |
| 1870 | continue; |
| 1871 | |
| 1872 | clear_io_state(td); |
| 1873 | gettimeofday(&td->start, NULL); |
| 1874 | |
| 1875 | do_verify(td); |
| 1876 | |
| 1877 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); |
| 1878 | |
| 1879 | if (td->error || td->terminate) |
| 1880 | break; |
| 1881 | } |
| 1882 | |
| 1883 | ret = 0; |
| 1884 | |
| 1885 | if (td->bw_log) |
| 1886 | finish_log(td, td->bw_log, "bw"); |
| 1887 | if (td->slat_log) |
| 1888 | finish_log(td, td->slat_log, "slat"); |
| 1889 | if (td->clat_log) |
| 1890 | finish_log(td, td->clat_log, "clat"); |
| 1891 | |
| 1892 | if (exitall_on_terminate) |
| 1893 | terminate_threads(td->groupid); |
| 1894 | |
| 1895 | err: |
| 1896 | if (td->fd != -1) { |
| 1897 | close(td->fd); |
| 1898 | td->fd = -1; |
| 1899 | } |
| 1900 | if (td->mmap) |
| 1901 | munmap(td->mmap, td->file_size); |
| 1902 | cleanup_io(td); |
| 1903 | cleanup_io_u(td); |
| 1904 | if (ret) { |
| 1905 | sem_post(&startup_sem); |
| 1906 | sem_wait(&td->mutex); |
| 1907 | } |
| 1908 | td_set_runstate(td, TD_EXITED); |
| 1909 | return NULL; |
| 1910 | |
| 1911 | } |
| 1912 | |
| 1913 | static void *fork_main(int shmid, int offset) |
| 1914 | { |
| 1915 | struct thread_data *td; |
| 1916 | void *data; |
| 1917 | |
| 1918 | data = shmat(shmid, NULL, 0); |
| 1919 | if (data == (void *) -1) { |
| 1920 | perror("shmat"); |
| 1921 | return NULL; |
| 1922 | } |
| 1923 | |
| 1924 | td = data + offset * sizeof(struct thread_data); |
| 1925 | thread_main(td); |
| 1926 | shmdt(data); |
| 1927 | return NULL; |
| 1928 | } |
| 1929 | |
| 1930 | static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max, |
| 1931 | double *mean, double *dev) |
| 1932 | { |
| 1933 | double n; |
| 1934 | |
| 1935 | if (is->samples == 0) |
| 1936 | return 0; |
| 1937 | |
| 1938 | *min = is->min_val; |
| 1939 | *max = is->max_val; |
| 1940 | |
| 1941 | n = (double) is->samples; |
| 1942 | *mean = (double) is->val / n; |
| 1943 | *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1)); |
| 1944 | if (!(*min + *max) && !(*mean + *dev)) |
| 1945 | return 0; |
| 1946 | |
| 1947 | return 1; |
| 1948 | } |
| 1949 | |
| 1950 | static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs, |
| 1951 | int ddir) |
| 1952 | { |
| 1953 | char *ddir_str[] = { "read ", "write" }; |
| 1954 | unsigned long min, max, bw; |
| 1955 | double mean, dev; |
| 1956 | |
| 1957 | if (!td->runtime[ddir]) |
| 1958 | return; |
| 1959 | |
| 1960 | bw = td->io_bytes[ddir] / td->runtime[ddir]; |
Jens Axboe | 9104f87 | 2005-12-28 23:50:45 +0100 | [diff] [blame] | 1961 | printf(" %s: io=%6lluMiB, bw=%6luKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1962 | |
| 1963 | if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev)) |
| 1964 | printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); |
| 1965 | |
| 1966 | if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev)) |
| 1967 | printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); |
| 1968 | |
| 1969 | if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) { |
| 1970 | double p_of_agg; |
| 1971 | |
| 1972 | p_of_agg = mean * 100 / (double) rs->agg[ddir]; |
| 1973 | printf(" bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, dev=%5.02f\n", min, max, p_of_agg, mean, dev); |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | static void show_thread_status(struct thread_data *td, |
| 1978 | struct group_run_stats *rs) |
| 1979 | { |
| 1980 | double usr_cpu, sys_cpu; |
| 1981 | |
| 1982 | if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error) |
| 1983 | return; |
| 1984 | |
| 1985 | printf("Client%d (groupid=%d): err=%2d:\n", td->thread_number, td->groupid, td->error); |
| 1986 | |
| 1987 | show_ddir_status(td, rs, td->ddir); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1988 | if (td->io_bytes[td->ddir ^ 1]) |
| 1989 | show_ddir_status(td, rs, td->ddir ^ 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1990 | |
| 1991 | if (td->runtime[0] + td->runtime[1]) { |
| 1992 | double runt = td->runtime[0] + td->runtime[1]; |
| 1993 | |
| 1994 | usr_cpu = (double) td->usr_time * 100 / runt; |
| 1995 | sys_cpu = (double) td->sys_time * 100 / runt; |
| 1996 | } else { |
| 1997 | usr_cpu = 0; |
| 1998 | sys_cpu = 0; |
| 1999 | } |
| 2000 | |
| 2001 | printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx); |
| 2002 | } |
| 2003 | |
| 2004 | static void check_str_update(struct thread_data *td) |
| 2005 | { |
| 2006 | char c = run_str[td->thread_number - 1]; |
| 2007 | |
| 2008 | if (td->runstate == td->old_runstate) |
| 2009 | return; |
| 2010 | |
| 2011 | switch (td->runstate) { |
| 2012 | case TD_REAPED: |
| 2013 | c = '_'; |
| 2014 | break; |
| 2015 | case TD_EXITED: |
| 2016 | c = 'E'; |
| 2017 | break; |
| 2018 | case TD_RUNNING: |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 2019 | if (td_rw(td)) { |
| 2020 | if (td->sequential) |
| 2021 | c = 'M'; |
| 2022 | else |
| 2023 | c = 'm'; |
| 2024 | } else if (td_read(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2025 | if (td->sequential) |
| 2026 | c = 'R'; |
| 2027 | else |
| 2028 | c = 'r'; |
| 2029 | } else { |
| 2030 | if (td->sequential) |
| 2031 | c = 'W'; |
| 2032 | else |
| 2033 | c = 'w'; |
| 2034 | } |
| 2035 | break; |
| 2036 | case TD_VERIFYING: |
| 2037 | c = 'V'; |
| 2038 | break; |
| 2039 | case TD_CREATED: |
| 2040 | c = 'C'; |
| 2041 | break; |
| 2042 | case TD_NOT_CREATED: |
| 2043 | c = 'P'; |
| 2044 | break; |
| 2045 | default: |
| 2046 | printf("state %d\n", td->runstate); |
| 2047 | } |
| 2048 | |
| 2049 | run_str[td->thread_number - 1] = c; |
| 2050 | td->old_runstate = td->runstate; |
| 2051 | } |
| 2052 | |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2053 | static void eta_to_str(char *str, int eta_sec) |
| 2054 | { |
| 2055 | unsigned int d, h, m, s; |
| 2056 | static int always_d, always_h; |
| 2057 | |
| 2058 | d = h = m = s = 0; |
| 2059 | |
| 2060 | s = eta_sec % 60; |
| 2061 | eta_sec /= 60; |
| 2062 | m = eta_sec % 60; |
| 2063 | eta_sec /= 60; |
| 2064 | h = eta_sec % 24; |
| 2065 | eta_sec /= 24; |
| 2066 | d = eta_sec; |
| 2067 | |
| 2068 | if (d || always_d) { |
| 2069 | always_d = 1; |
| 2070 | str += sprintf(str, "%02dd:", d); |
| 2071 | } |
| 2072 | if (h || always_h) { |
| 2073 | always_h = 1; |
| 2074 | str += sprintf(str, "%02dh:", h); |
| 2075 | } |
| 2076 | |
| 2077 | str += sprintf(str, "%02dm:", m); |
| 2078 | str += sprintf(str, "%02ds", s); |
| 2079 | } |
| 2080 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2081 | static int thread_eta(struct thread_data *td, unsigned long elapsed) |
| 2082 | { |
| 2083 | unsigned long long bytes_total, bytes_done; |
| 2084 | unsigned int eta_sec = 0; |
| 2085 | |
| 2086 | bytes_total = td->total_io_size; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 2087 | |
| 2088 | /* |
| 2089 | * if writing, bytes_total will be twice the size. If mixing, |
| 2090 | * assume a 50/50 split and thus bytes_total will be 50% larger. |
| 2091 | */ |
| 2092 | if (td->verify) { |
| 2093 | if (td_rw(td)) |
| 2094 | bytes_total = bytes_total * 3 / 2; |
| 2095 | else |
| 2096 | bytes_total <<= 1; |
| 2097 | } |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2098 | if (td->zone_size && td->zone_skip) |
| 2099 | bytes_total /= (td->zone_skip / td->zone_size); |
| 2100 | |
| 2101 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) { |
| 2102 | double perc; |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2103 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2104 | bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]; |
| 2105 | perc = (double) bytes_done / (double) bytes_total; |
| 2106 | if (perc > 1.0) |
| 2107 | perc = 1.0; |
| 2108 | |
| 2109 | eta_sec = (elapsed * (1.0 / perc)) - elapsed; |
| 2110 | |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2111 | if (td->timeout && eta_sec > (td->timeout - elapsed)) |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2112 | eta_sec = td->timeout - elapsed; |
| 2113 | } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED) { |
| 2114 | int t_eta = 0, r_eta = 0; |
| 2115 | |
| 2116 | /* |
| 2117 | * We can only guess - assume it'll run the full timeout |
| 2118 | * if given, otherwise assume it'll run at the specified rate. |
| 2119 | */ |
| 2120 | if (td->timeout) |
| 2121 | t_eta = td->timeout + td->start_delay - elapsed; |
| 2122 | if (td->rate) { |
| 2123 | r_eta = (bytes_total / 1024) / td->rate; |
| 2124 | r_eta += td->start_delay - elapsed; |
| 2125 | } |
| 2126 | |
| 2127 | if (r_eta && t_eta) |
| 2128 | eta_sec = min(r_eta, t_eta); |
| 2129 | else if (r_eta) |
| 2130 | eta_sec = r_eta; |
| 2131 | else if (t_eta) |
| 2132 | eta_sec = t_eta; |
| 2133 | else |
| 2134 | eta_sec = INT_MAX; |
| 2135 | } else { |
| 2136 | /* |
| 2137 | * thread is already done |
| 2138 | */ |
| 2139 | eta_sec = 0; |
| 2140 | } |
| 2141 | |
| 2142 | return eta_sec; |
| 2143 | } |
| 2144 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2145 | static void print_thread_status(void) |
| 2146 | { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2147 | unsigned long elapsed = time_since_now(&genesis); |
| 2148 | int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2149 | char eta_str[32]; |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2150 | double perc = 0.0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2151 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2152 | eta_secs = malloc(thread_number * sizeof(int)); |
| 2153 | memset(eta_secs, 0, thread_number * sizeof(int)); |
| 2154 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2155 | nr_running = t_rate = m_rate = 0; |
| 2156 | for (i = 0; i < thread_number; i++) { |
| 2157 | struct thread_data *td = &threads[i]; |
| 2158 | |
| 2159 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){ |
| 2160 | nr_running++; |
| 2161 | t_rate += td->rate; |
| 2162 | m_rate += td->ratemin; |
| 2163 | } |
| 2164 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2165 | if (elapsed >= 3) |
| 2166 | eta_secs[i] = thread_eta(td, elapsed); |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2167 | else |
| 2168 | eta_secs[i] = INT_MAX; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2169 | |
| 2170 | check_str_update(td); |
| 2171 | } |
| 2172 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2173 | if (exitall_on_terminate) |
| 2174 | eta_sec = INT_MAX; |
| 2175 | else |
| 2176 | eta_sec = 0; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2177 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2178 | for (i = 0; i < thread_number; i++) { |
| 2179 | if (exitall_on_terminate) { |
| 2180 | if (eta_secs[i] < eta_sec) |
| 2181 | eta_sec = eta_secs[i]; |
| 2182 | } else { |
| 2183 | if (eta_secs[i] > eta_sec) |
| 2184 | eta_sec = eta_secs[i]; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2185 | } |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2186 | } |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2187 | |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2188 | if (eta_sec != INT_MAX && elapsed) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2189 | perc = (double) elapsed / (double) (elapsed + eta_sec); |
| 2190 | eta_to_str(eta_str, eta_sec); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2191 | } |
| 2192 | |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2193 | printf("Threads now running (%d)", nr_running); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2194 | if (m_rate || t_rate) |
| 2195 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2196 | if (eta_sec != INT_MAX) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2197 | perc *= 100.0; |
| 2198 | printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str); |
| 2199 | } |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2200 | printf("\r"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2201 | fflush(stdout); |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2202 | free(eta_secs); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 2206 | { |
| 2207 | int i; |
| 2208 | |
| 2209 | /* |
| 2210 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 2211 | */ |
| 2212 | for (i = 0; i < thread_number; i++) { |
| 2213 | struct thread_data *td = &threads[i]; |
| 2214 | |
| 2215 | if (td->runstate != TD_EXITED) |
| 2216 | continue; |
| 2217 | |
| 2218 | td_set_runstate(td, TD_REAPED); |
| 2219 | |
| 2220 | if (td->use_thread) { |
| 2221 | long ret; |
| 2222 | |
| 2223 | if (pthread_join(td->thread, (void *) &ret)) |
| 2224 | perror("thread_join"); |
| 2225 | } else |
| 2226 | waitpid(td->pid, NULL, 0); |
| 2227 | |
| 2228 | (*nr_running)--; |
| 2229 | (*m_rate) -= td->ratemin; |
| 2230 | (*t_rate) -= td->rate; |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | static void run_threads(void) |
| 2235 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2236 | struct thread_data *td; |
| 2237 | unsigned long spent; |
| 2238 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2239 | void *mlocked_mem = NULL; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2240 | |
| 2241 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2242 | |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame^] | 2243 | signal(SIGINT, sig_handler); |
| 2244 | signal(SIGALRM, sig_handler); |
| 2245 | |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2246 | if (mlock_size) { |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame^] | 2247 | mlocked_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2248 | if (!mlocked_mem) { |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame^] | 2249 | perror("mmap locked mem"); |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2250 | return; |
| 2251 | } |
| 2252 | if (mlock(mlocked_mem, mlock_size) < 0) { |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame^] | 2253 | munmap(mlocked_mem, mlock_size); |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2254 | return; |
| 2255 | } |
| 2256 | } |
| 2257 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2258 | fflush(stdout); |
| 2259 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2260 | todo = thread_number; |
| 2261 | nr_running = 0; |
| 2262 | nr_started = 0; |
| 2263 | m_rate = t_rate = 0; |
| 2264 | |
| 2265 | for (i = 0; i < thread_number; i++) { |
| 2266 | td = &threads[i]; |
| 2267 | |
| 2268 | run_str[td->thread_number - 1] = 'P'; |
| 2269 | |
| 2270 | init_disk_util(td); |
| 2271 | |
| 2272 | if (!td->create_serialize) |
| 2273 | continue; |
| 2274 | |
| 2275 | /* |
| 2276 | * do file setup here so it happens sequentially, |
| 2277 | * we don't want X number of threads getting their |
| 2278 | * client data interspersed on disk |
| 2279 | */ |
| 2280 | if (setup_file(td)) { |
| 2281 | td_set_runstate(td, TD_REAPED); |
| 2282 | todo--; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | gettimeofday(&genesis, NULL); |
| 2287 | |
| 2288 | while (todo) { |
| 2289 | /* |
| 2290 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 2291 | */ |
| 2292 | for (i = 0; i < thread_number; i++) { |
| 2293 | td = &threads[i]; |
| 2294 | |
| 2295 | if (td->runstate != TD_NOT_CREATED) |
| 2296 | continue; |
| 2297 | |
| 2298 | /* |
| 2299 | * never got a chance to start, killed by other |
| 2300 | * thread for some reason |
| 2301 | */ |
| 2302 | if (td->terminate) { |
| 2303 | todo--; |
| 2304 | continue; |
| 2305 | } |
| 2306 | |
| 2307 | if (td->start_delay) { |
| 2308 | spent = mtime_since_now(&genesis); |
| 2309 | |
| 2310 | if (td->start_delay * 1000 > spent) |
| 2311 | continue; |
| 2312 | } |
| 2313 | |
| 2314 | if (td->stonewall && (nr_started || nr_running)) |
| 2315 | break; |
| 2316 | |
| 2317 | td_set_runstate(td, TD_CREATED); |
| 2318 | sem_init(&startup_sem, 0, 1); |
| 2319 | todo--; |
| 2320 | nr_started++; |
| 2321 | |
| 2322 | if (td->use_thread) { |
| 2323 | if (pthread_create(&td->thread, NULL, thread_main, td)) { |
| 2324 | perror("thread_create"); |
| 2325 | nr_started--; |
| 2326 | } |
| 2327 | } else { |
| 2328 | if (fork()) |
| 2329 | sem_wait(&startup_sem); |
| 2330 | else { |
| 2331 | fork_main(shm_id, i); |
| 2332 | exit(0); |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | /* |
| 2338 | * start created threads (TD_CREATED -> TD_RUNNING) |
| 2339 | */ |
| 2340 | for (i = 0; i < thread_number; i++) { |
| 2341 | td = &threads[i]; |
| 2342 | |
| 2343 | if (td->runstate != TD_CREATED) |
| 2344 | continue; |
| 2345 | |
| 2346 | td_set_runstate(td, TD_RUNNING); |
| 2347 | nr_running++; |
| 2348 | nr_started--; |
| 2349 | m_rate += td->ratemin; |
| 2350 | t_rate += td->rate; |
| 2351 | sem_post(&td->mutex); |
| 2352 | } |
| 2353 | |
| 2354 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 2355 | |
| 2356 | if (todo) |
| 2357 | usleep(100000); |
| 2358 | } |
| 2359 | |
| 2360 | while (nr_running) { |
| 2361 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 2362 | usleep(10000); |
| 2363 | } |
| 2364 | |
| 2365 | update_io_ticks(); |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2366 | |
| 2367 | if (mlocked_mem) { |
| 2368 | if (munlock(mlocked_mem, mlock_size) < 0) |
| 2369 | perror("munlock"); |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame^] | 2370 | munmap(mlocked_mem, mlock_size); |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2371 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | static void show_group_stats(struct group_run_stats *rs, int id) |
| 2375 | { |
| 2376 | printf("\nRun status group %d (all jobs):\n", id); |
| 2377 | |
| 2378 | if (rs->max_run[DDIR_READ]) |
Jens Axboe | 9104f87 | 2005-12-28 23:50:45 +0100 | [diff] [blame] | 2379 | printf(" READ: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[0], rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2380 | if (rs->max_run[DDIR_WRITE]) |
Jens Axboe | 9104f87 | 2005-12-28 23:50:45 +0100 | [diff] [blame] | 2381 | printf(" WRITE: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | static void show_disk_util(void) |
| 2385 | { |
| 2386 | struct disk_util_stat *dus; |
| 2387 | struct list_head *entry; |
| 2388 | struct disk_util *du; |
| 2389 | double util; |
| 2390 | |
| 2391 | printf("\nDisk stats (read/write):\n"); |
| 2392 | |
| 2393 | list_for_each(entry, &disk_list) { |
| 2394 | du = list_entry(entry, struct disk_util, list); |
| 2395 | dus = &du->dus; |
| 2396 | |
| 2397 | util = (double) 100 * du->dus.io_ticks / (double) du->msec; |
| 2398 | if (util > 100.0) |
| 2399 | util = 100.0; |
| 2400 | |
| 2401 | printf(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util); |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | static void show_run_stats(void) |
| 2406 | { |
| 2407 | struct group_run_stats *runstats, *rs; |
| 2408 | struct thread_data *td; |
| 2409 | int i; |
| 2410 | |
| 2411 | runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1)); |
| 2412 | |
| 2413 | for (i = 0; i < groupid + 1; i++) { |
| 2414 | rs = &runstats[i]; |
| 2415 | |
| 2416 | memset(rs, 0, sizeof(*rs)); |
| 2417 | rs->min_bw[0] = rs->min_run[0] = ~0UL; |
| 2418 | rs->min_bw[1] = rs->min_run[1] = ~0UL; |
| 2419 | } |
| 2420 | |
| 2421 | for (i = 0; i < thread_number; i++) { |
| 2422 | unsigned long rbw, wbw; |
| 2423 | |
| 2424 | td = &threads[i]; |
| 2425 | |
| 2426 | if (td->error) { |
| 2427 | printf("Client%d: %s\n", td->thread_number, td->verror); |
| 2428 | continue; |
| 2429 | } |
| 2430 | |
| 2431 | rs = &runstats[td->groupid]; |
| 2432 | |
| 2433 | if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0]) |
| 2434 | rs->min_run[0] = td->runtime[0]; |
| 2435 | if (td->runtime[0] > rs->max_run[0]) |
| 2436 | rs->max_run[0] = td->runtime[0]; |
| 2437 | if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1]) |
| 2438 | rs->min_run[1] = td->runtime[1]; |
| 2439 | if (td->runtime[1] > rs->max_run[1]) |
| 2440 | rs->max_run[1] = td->runtime[1]; |
| 2441 | |
| 2442 | rbw = wbw = 0; |
| 2443 | if (td->runtime[0]) |
| 2444 | rbw = td->io_bytes[0] / td->runtime[0]; |
| 2445 | if (td->runtime[1]) |
| 2446 | wbw = td->io_bytes[1] / td->runtime[1]; |
| 2447 | |
| 2448 | if (rbw < rs->min_bw[0]) |
| 2449 | rs->min_bw[0] = rbw; |
| 2450 | if (wbw < rs->min_bw[1]) |
| 2451 | rs->min_bw[1] = wbw; |
| 2452 | if (rbw > rs->max_bw[0]) |
| 2453 | rs->max_bw[0] = rbw; |
| 2454 | if (wbw > rs->max_bw[1]) |
| 2455 | rs->max_bw[1] = wbw; |
| 2456 | |
| 2457 | rs->io_mb[0] += td->io_bytes[0] >> 20; |
| 2458 | rs->io_mb[1] += td->io_bytes[1] >> 20; |
| 2459 | } |
| 2460 | |
| 2461 | for (i = 0; i < groupid + 1; i++) { |
| 2462 | rs = &runstats[i]; |
| 2463 | |
| 2464 | if (rs->max_run[0]) |
| 2465 | rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0]; |
| 2466 | if (rs->max_run[1]) |
| 2467 | rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1]; |
| 2468 | } |
| 2469 | |
| 2470 | /* |
| 2471 | * don't overwrite last signal output |
| 2472 | */ |
| 2473 | printf("\n"); |
| 2474 | |
| 2475 | for (i = 0; i < thread_number; i++) { |
| 2476 | td = &threads[i]; |
| 2477 | rs = &runstats[td->groupid]; |
| 2478 | |
| 2479 | show_thread_status(td, rs); |
| 2480 | } |
| 2481 | |
| 2482 | for (i = 0; i < groupid + 1; i++) |
| 2483 | show_group_stats(&runstats[i], i); |
| 2484 | |
| 2485 | show_disk_util(); |
| 2486 | } |
| 2487 | |
| 2488 | int main(int argc, char *argv[]) |
| 2489 | { |
| 2490 | if (parse_options(argc, argv)) |
| 2491 | return 1; |
| 2492 | |
| 2493 | if (!thread_number) { |
| 2494 | printf("Nothing to do\n"); |
| 2495 | return 1; |
| 2496 | } |
| 2497 | |
| 2498 | disk_util_timer_arm(); |
| 2499 | |
| 2500 | run_threads(); |
| 2501 | show_run_stats(); |
| 2502 | |
| 2503 | return 0; |
| 2504 | } |