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