2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/ioctl.h>
40 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
43 int thread_number = 0;
46 char *fio_inst_prefix = _INST_PREFIX;
48 extern unsigned long long mlock_size;
50 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
52 static volatile int startup_sem;
54 #define TERMINATE_ALL (-1)
55 #define JOB_START_TIMEOUT (5 * 1000)
57 static void terminate_threads(int group_id)
61 for (i = 0; i < thread_number; i++) {
62 struct thread_data *td = &threads[i];
64 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
71 static void sig_handler(int sig)
76 disk_util_timer_arm();
77 print_thread_status();
80 printf("\nfio: terminating on signal\n");
82 terminate_threads(TERMINATE_ALL);
88 * The ->file_map[] contains a map of blocks we have or have not done io
89 * to yet. Used to make sure we cover the entire range in a fair fashion.
91 static int random_map_free(struct thread_data *td, struct fio_file *f,
92 unsigned long long block)
94 unsigned int idx = RAND_MAP_IDX(td, f, block);
95 unsigned int bit = RAND_MAP_BIT(td, f, block);
97 return (f->file_map[idx] & (1UL << bit)) == 0;
101 * Return the next free block in the map.
103 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
104 unsigned long long *b)
110 while ((*b) * td->min_bs < f->file_size) {
111 if (f->file_map[i] != -1UL) {
112 *b += ffz(f->file_map[i]);
116 *b += BLOCKS_PER_MAP;
124 * Mark a given offset as used in the map.
126 static void mark_random_map(struct thread_data *td, struct fio_file *f,
129 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
130 unsigned int blocks = 0;
132 while (blocks < (io_u->buflen / td->min_bs)) {
133 unsigned int idx, bit;
135 if (!random_map_free(td, f, block))
138 idx = RAND_MAP_IDX(td, f, block);
139 bit = RAND_MAP_BIT(td, f, block);
141 assert(idx < f->num_maps);
143 f->file_map[idx] |= (1UL << bit);
148 if ((blocks * td->min_bs) < io_u->buflen)
149 io_u->buflen = blocks * td->min_bs;
153 * For random io, generate a random new block and see if it's used. Repeat
154 * until we find a free one. For sequential io, just return the end of
155 * the last io issued.
157 static int get_next_offset(struct thread_data *td, struct fio_file *f,
158 unsigned long long *offset)
160 unsigned long long b, rb;
163 if (!td->sequential) {
164 unsigned long long max_blocks = td->io_size / td->min_bs;
168 r = os_random_long(&td->random_state);
169 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
170 rb = b + (f->file_offset / td->min_bs);
172 } while (!random_map_free(td, f, rb) && loops);
175 if (get_next_free_block(td, f, &b))
179 b = f->last_pos / td->min_bs;
181 *offset = (b * td->min_bs) + f->file_offset;
182 if (*offset > f->file_size)
188 static unsigned int get_next_buflen(struct thread_data *td)
193 if (td->min_bs == td->max_bs)
196 r = os_random_long(&td->bsrange_state);
197 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
198 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
201 if (buflen > td->io_size - td->this_io_bytes[td->ddir]) {
203 * if using direct/raw io, we may not be able to
204 * shrink the size. so just fail it.
206 if (td->io_ops->flags & FIO_RAWIO)
209 buflen = td->io_size - td->this_io_bytes[td->ddir];
216 * Check if we are above the minimum rate given.
218 static int check_min_rate(struct thread_data *td, struct timeval *now)
225 * allow a 2 second settle period in the beginning
227 if (mtime_since(&td->start, now) < 2000)
231 * if rate blocks is set, sample is running
233 if (td->rate_bytes) {
234 spent = mtime_since(&td->lastrate, now);
235 if (spent < td->ratecycle)
238 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
239 if (rate < td->ratemin) {
240 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
242 terminate_threads(td->groupid);
247 td->rate_bytes = td->this_io_bytes[ddir];
248 memcpy(&td->lastrate, now, sizeof(*now));
252 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
256 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
262 static void fill_random_bytes(struct thread_data *td,
263 unsigned char *p, unsigned int len)
269 r = os_random_double(&td->verify_state);
272 * lrand48_r seems to be broken and only fill the bottom
273 * 32-bits, even on 64-bit archs with 64-bit longs
286 static void hexdump(void *buffer, int len)
288 unsigned char *p = buffer;
291 for (i = 0; i < len; i++)
292 fprintf(f_out, "%02x", p[i]);
293 fprintf(f_out, "\n");
296 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
298 unsigned char *p = (unsigned char *) io_u->buf;
302 c = crc32(p, hdr->len - sizeof(*hdr));
304 if (c != hdr->crc32) {
305 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
306 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
313 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
315 unsigned char *p = (unsigned char *) io_u->buf;
316 struct md5_ctx md5_ctx;
318 memset(&md5_ctx, 0, sizeof(md5_ctx));
320 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
322 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
323 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
324 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
325 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
332 static int verify_io_u(struct io_u *io_u)
334 struct verify_header *hdr = (struct verify_header *) io_u->buf;
337 if (hdr->fio_magic != FIO_HDR_MAGIC)
340 if (hdr->verify_type == VERIFY_MD5)
341 ret = verify_io_u_md5(hdr, io_u);
342 else if (hdr->verify_type == VERIFY_CRC32)
343 ret = verify_io_u_crc32(hdr, io_u);
345 log_err("Bad verify type %d\n", hdr->verify_type);
352 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
354 hdr->crc32 = crc32(p, len);
357 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
359 struct md5_ctx md5_ctx;
361 memset(&md5_ctx, 0, sizeof(md5_ctx));
362 md5_update(&md5_ctx, p, len);
363 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
367 * Return the data direction for the next io_u. If the job is a
368 * mixed read/write workload, check the rwmix cycle and switch if
371 static int get_rw_ddir(struct thread_data *td)
375 unsigned long elapsed;
377 gettimeofday(&now, NULL);
378 elapsed = mtime_since_now(&td->rwmix_switch);
381 * Check if it's time to seed a new data direction.
383 if (elapsed >= td->rwmixcycle) {
387 r = os_random_long(&td->rwmix_state);
388 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
389 if (v < td->rwmixread)
390 td->rwmix_ddir = DDIR_READ;
392 td->rwmix_ddir = DDIR_WRITE;
393 memcpy(&td->rwmix_switch, &now, sizeof(now));
395 return td->rwmix_ddir;
396 } else if (td_read(td))
403 * fill body of io_u->buf with random data and add a header with the
404 * crc32 or md5 sum of that data.
406 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
408 unsigned char *p = (unsigned char *) io_u->buf;
409 struct verify_header hdr;
411 hdr.fio_magic = FIO_HDR_MAGIC;
412 hdr.len = io_u->buflen;
414 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
416 if (td->verify == VERIFY_MD5) {
417 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
418 hdr.verify_type = VERIFY_MD5;
420 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
421 hdr.verify_type = VERIFY_CRC32;
424 memcpy(io_u->buf, &hdr, sizeof(hdr));
427 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
429 if (td->io_ops->prep && td->io_ops->prep(td, io_u))
435 void put_io_u(struct thread_data *td, struct io_u *io_u)
438 list_del(&io_u->list);
439 list_add(&io_u->list, &td->io_u_freelist);
443 static int fill_io_u(struct thread_data *td, struct fio_file *f,
447 * If using an iolog, grab next piece if any available.
450 return read_iolog_get(td, io_u);
453 * No log, let the seq/rand engine retrieve the next position.
455 if (!get_next_offset(td, f, &io_u->offset)) {
456 io_u->buflen = get_next_buflen(td);
459 io_u->ddir = get_rw_ddir(td);
462 * If using a write iolog, store this entry.
465 write_iolog_put(td, io_u);
475 #define queue_full(td) list_empty(&(td)->io_u_freelist)
477 struct io_u *__get_io_u(struct thread_data *td)
479 struct io_u *io_u = NULL;
481 if (!queue_full(td)) {
482 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
486 list_del(&io_u->list);
487 list_add(&io_u->list, &td->io_u_busylist);
495 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
496 * etc. The returned io_u is fully ready to be prepped and submitted.
498 static struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
502 io_u = __get_io_u(td);
506 if (td->zone_bytes >= td->zone_size) {
508 f->last_pos += td->zone_skip;
511 if (fill_io_u(td, f, io_u)) {
516 if (io_u->buflen + io_u->offset > f->file_size) {
517 if (td->io_ops->flags & FIO_RAWIO) {
522 io_u->buflen = f->file_size - io_u->offset;
530 if (!td->read_iolog && !td->sequential)
531 mark_random_map(td, f, io_u);
533 f->last_pos += io_u->buflen;
535 if (td->verify != VERIFY_NONE)
536 populate_io_u(td, io_u);
538 if (td_io_prep(td, io_u)) {
543 gettimeofday(&io_u->start_time, NULL);
547 static inline void td_set_runstate(struct thread_data *td, int runstate)
549 td->runstate = runstate;
552 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
554 struct io_piece *ipo;
556 if (!list_empty(&td->io_hist_list)) {
557 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
559 list_del(&ipo->list);
561 io_u->offset = ipo->offset;
562 io_u->buflen = ipo->len;
563 io_u->ddir = DDIR_READ;
571 static struct fio_file *get_next_file(struct thread_data *td)
573 int old_next_file = td->next_file;
577 f = &td->files[td->next_file];
580 if (td->next_file >= td->nr_files)
587 } while (td->next_file != old_next_file);
592 static int td_io_sync(struct thread_data *td, struct fio_file *f)
594 if (td->io_ops->sync)
595 return td->io_ops->sync(td, f);
600 static int td_io_getevents(struct thread_data *td, int min, int max,
603 return td->io_ops->getevents(td, min, max, t);
606 static int td_io_queue(struct thread_data *td, struct io_u *io_u)
608 gettimeofday(&io_u->issue_time, NULL);
610 return td->io_ops->queue(td, io_u);
613 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
615 static void io_completed(struct thread_data *td, struct io_u *io_u,
616 struct io_completion_data *icd)
621 gettimeofday(&e, NULL);
624 unsigned int bytes = io_u->buflen - io_u->resid;
625 const int idx = io_u->ddir;
627 td->io_blocks[idx]++;
628 td->io_bytes[idx] += bytes;
629 td->zone_bytes += bytes;
630 td->this_io_bytes[idx] += bytes;
632 msec = mtime_since(&io_u->issue_time, &e);
634 add_clat_sample(td, idx, msec);
635 add_bw_sample(td, idx);
637 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
638 log_io_piece(td, io_u);
640 icd->bytes_done[idx] += bytes;
642 icd->error = io_u->error;
645 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
651 icd->bytes_done[0] = icd->bytes_done[1] = 0;
653 for (i = 0; i < icd->nr; i++) {
654 io_u = td->io_ops->event(td, i);
656 io_completed(td, io_u, icd);
662 * When job exits, we can cancel the in-flight IO if we are using async
663 * io. Attempt to do so.
665 static void cleanup_pending_aio(struct thread_data *td)
667 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
668 struct list_head *entry, *n;
669 struct io_completion_data icd;
674 * get immediately available events, if any
676 r = td_io_getevents(td, 0, td->cur_depth, &ts);
679 ios_completed(td, &icd);
683 * now cancel remaining active events
685 if (td->io_ops->cancel) {
686 list_for_each_safe(entry, n, &td->io_u_busylist) {
687 io_u = list_entry(entry, struct io_u, list);
689 r = td->io_ops->cancel(td, io_u);
696 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
699 ios_completed(td, &icd);
704 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
706 struct io_u *v_io_u = *io_u;
710 ret = verify_io_u(v_io_u);
711 put_io_u(td, v_io_u);
719 * The main verify engine. Runs over the writes we previusly submitted,
720 * reads the blocks back in, and checks the crc/md5 of the data.
722 static void do_verify(struct thread_data *td)
725 struct io_u *io_u, *v_io_u = NULL;
726 struct io_completion_data icd;
730 td_set_runstate(td, TD_VERIFYING);
736 gettimeofday(&t, NULL);
737 if (runtime_exceeded(td, &t))
740 io_u = __get_io_u(td);
744 if (get_next_verify(td, io_u)) {
749 f = get_next_file(td);
755 if (td_io_prep(td, io_u)) {
760 ret = td_io_queue(td, io_u);
768 * we have one pending to verify, do that while
769 * we are doing io on the next one
771 if (do_io_u_verify(td, &v_io_u))
774 ret = td_io_getevents(td, 1, 1, NULL);
781 v_io_u = td->io_ops->event(td, 0);
784 io_completed(td, v_io_u, &icd);
787 td_verror(td, icd.error);
788 put_io_u(td, v_io_u);
794 * if we can't submit more io, we need to verify now
796 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
801 do_io_u_verify(td, &v_io_u);
804 cleanup_pending_aio(td);
806 td_set_runstate(td, TD_RUNNING);
810 * Not really an io thread, all it does is burn CPU cycles in the specified
813 static void do_cpuio(struct thread_data *td)
816 int split = 100 / td->cpuload;
819 while (!td->terminate) {
820 gettimeofday(&e, NULL);
822 if (runtime_exceeded(td, &e))
828 usec_sleep(td, 10000);
835 * Main IO worker function. It retrieves io_u's to process and queues
836 * and reaps them, checking for rate and errors along the way.
838 static void do_io(struct thread_data *td)
840 struct io_completion_data icd;
846 td_set_runstate(td, TD_RUNNING);
848 while (td->this_io_bytes[td->ddir] < td->io_size) {
849 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
850 struct timespec *timeout;
851 int ret, min_evts = 0;
857 f = get_next_file(td);
861 io_u = get_io_u(td, f);
865 memcpy(&s, &io_u->start_time, sizeof(s));
867 ret = td_io_queue(td, io_u);
874 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
876 if (td->cur_depth < td->iodepth) {
884 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
892 ios_completed(td, &icd);
894 td_verror(td, icd.error);
899 * the rate is batched for now, it should work for batches
900 * of completions except the very first one which may look
903 gettimeofday(&e, NULL);
904 usec = utime_since(&s, &e);
906 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
908 if (check_min_rate(td, &e)) {
909 td_verror(td, ENOMEM);
913 if (runtime_exceeded(td, &e))
917 usec_sleep(td, td->thinktime);
919 if (should_fsync(td) && td->fsync_blocks &&
920 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
925 cleanup_pending_aio(td);
927 if (should_fsync(td) && td->end_fsync) {
928 td_set_runstate(td, TD_FSYNCING);
929 for_each_file(td, f, i)
934 static int td_io_init(struct thread_data *td)
936 if (td->io_ops->init)
937 return td->io_ops->init(td);
942 static void cleanup_io_u(struct thread_data *td)
944 struct list_head *entry, *n;
947 list_for_each_safe(entry, n, &td->io_u_freelist) {
948 io_u = list_entry(entry, struct io_u, list);
950 list_del(&io_u->list);
954 if (td->mem_type == MEM_MALLOC)
955 free(td->orig_buffer);
956 else if (td->mem_type == MEM_SHM) {
957 struct shmid_ds sbuf;
959 shmdt(td->orig_buffer);
960 shmctl(td->shm_id, IPC_RMID, &sbuf);
961 } else if (td->mem_type == MEM_MMAP)
962 munmap(td->orig_buffer, td->orig_buffer_size);
964 log_err("Bad memory type %d\n", td->mem_type);
966 td->orig_buffer = NULL;
969 static int init_io_u(struct thread_data *td)
975 if (td->io_ops->flags & FIO_CPUIO)
978 if (td->io_ops->flags & FIO_SYNCIO)
981 max_units = td->iodepth;
983 td->orig_buffer_size = td->max_bs * max_units + MASK;
985 if (td->mem_type == MEM_MALLOC)
986 td->orig_buffer = malloc(td->orig_buffer_size);
987 else if (td->mem_type == MEM_SHM) {
988 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
989 if (td->shm_id < 0) {
990 td_verror(td, errno);
995 td->orig_buffer = shmat(td->shm_id, NULL, 0);
996 if (td->orig_buffer == (void *) -1) {
997 td_verror(td, errno);
999 td->orig_buffer = NULL;
1002 } else if (td->mem_type == MEM_MMAP) {
1003 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1004 if (td->orig_buffer == MAP_FAILED) {
1005 td_verror(td, errno);
1007 td->orig_buffer = NULL;
1012 p = ALIGN(td->orig_buffer);
1013 for (i = 0; i < max_units; i++) {
1014 io_u = malloc(sizeof(*io_u));
1015 memset(io_u, 0, sizeof(*io_u));
1016 INIT_LIST_HEAD(&io_u->list);
1018 io_u->buf = p + td->max_bs * i;
1020 list_add(&io_u->list, &td->io_u_freelist);
1026 static int switch_ioscheduler(struct thread_data *td)
1028 char tmp[256], tmp2[128];
1032 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1034 f = fopen(tmp, "r+");
1036 td_verror(td, errno);
1043 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1044 if (ferror(f) || ret != 1) {
1045 td_verror(td, errno);
1053 * Read back and check that the selected scheduler is now the default.
1055 ret = fread(tmp, 1, sizeof(tmp), f);
1056 if (ferror(f) || ret < 0) {
1057 td_verror(td, errno);
1062 sprintf(tmp2, "[%s]", td->ioscheduler);
1063 if (!strstr(tmp, tmp2)) {
1064 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
1065 td_verror(td, EINVAL);
1074 static void clear_io_state(struct thread_data *td)
1079 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1080 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1083 for_each_file(td, f, i) {
1085 if (td->io_ops->flags & FIO_SYNCIO)
1086 lseek(f->fd, SEEK_SET, 0);
1089 memset(f->file_map, 0, f->num_maps * sizeof(long));
1094 * Entry point for the thread based jobs. The process based jobs end up
1095 * here as well, after a little setup.
1097 static void *thread_main(void *data)
1099 struct thread_data *td = data;
1101 if (!td->use_thread)
1106 INIT_LIST_HEAD(&td->io_u_freelist);
1107 INIT_LIST_HEAD(&td->io_u_busylist);
1108 INIT_LIST_HEAD(&td->io_hist_list);
1109 INIT_LIST_HEAD(&td->io_log_list);
1114 if (fio_setaffinity(td) == -1) {
1115 td_verror(td, errno);
1126 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1127 td_verror(td, errno);
1132 if (nice(td->nice) == -1) {
1133 td_verror(td, errno);
1137 if (init_random_state(td))
1140 if (td->ioscheduler && switch_ioscheduler(td))
1143 td_set_runstate(td, TD_INITIALIZED);
1144 fio_sem_up(&startup_sem);
1145 fio_sem_down(&td->mutex);
1147 if (!td->create_serialize && setup_files(td))
1150 gettimeofday(&td->epoch, NULL);
1152 if (td->exec_prerun)
1153 system(td->exec_prerun);
1155 while (td->loops--) {
1156 getrusage(RUSAGE_SELF, &td->ru_start);
1157 gettimeofday(&td->start, NULL);
1158 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1161 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1164 prune_io_piece_log(td);
1166 if (td->io_ops->flags & FIO_CPUIO)
1171 td->runtime[td->ddir] += mtime_since_now(&td->start);
1172 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1173 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1175 update_rusage_stat(td);
1177 if (td->error || td->terminate)
1180 if (td->verify == VERIFY_NONE)
1184 gettimeofday(&td->start, NULL);
1188 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1190 if (td->error || td->terminate)
1195 finish_log(td, td->bw_log, "bw");
1197 finish_log(td, td->slat_log, "slat");
1199 finish_log(td, td->clat_log, "clat");
1200 if (td->write_iolog)
1201 write_iolog_close(td);
1202 if (td->exec_postrun)
1203 system(td->exec_postrun);
1205 if (exitall_on_terminate)
1206 terminate_threads(td->groupid);
1212 td_set_runstate(td, TD_EXITED);
1218 * We cannot pass the td data into a forked process, so attach the td and
1219 * pass it to the thread worker.
1221 static void *fork_main(int shmid, int offset)
1223 struct thread_data *td;
1226 data = shmat(shmid, NULL, 0);
1227 if (data == (void *) -1) {
1232 td = data + offset * sizeof(struct thread_data);
1239 * Run over the job map and reap the threads that have exited, if any.
1241 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1246 * reap exited threads (TD_EXITED -> TD_REAPED)
1248 for (i = 0, cputhreads = 0; i < thread_number; i++) {
1249 struct thread_data *td = &threads[i];
1251 if (td->io_ops->flags & FIO_CPUIO)
1254 if (td->runstate != TD_EXITED)
1257 td_set_runstate(td, TD_REAPED);
1259 if (td->use_thread) {
1262 if (pthread_join(td->thread, (void *) &ret))
1263 perror("thread_join");
1265 waitpid(td->pid, NULL, 0);
1268 (*m_rate) -= td->ratemin;
1269 (*t_rate) -= td->rate;
1272 if (*nr_running == cputhreads)
1273 terminate_threads(TERMINATE_ALL);
1276 static void fio_unpin_memory(void *pinned)
1279 if (munlock(pinned, mlock_size) < 0)
1281 munmap(pinned, mlock_size);
1285 static void *fio_pin_memory(void)
1287 unsigned long long phys_mem;
1294 * Don't allow mlock of more than real_mem-128MB
1296 phys_mem = os_phys_mem();
1298 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1299 mlock_size = phys_mem - 128 * 1024 * 1024;
1300 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
1304 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1306 perror("malloc locked mem");
1309 if (mlock(ptr, mlock_size) < 0) {
1310 munmap(ptr, mlock_size);
1319 * Main function for kicking off and reaping jobs, as needed.
1321 static void run_threads(void)
1323 struct thread_data *td;
1324 unsigned long spent;
1325 int i, todo, nr_running, m_rate, t_rate, nr_started;
1328 mlocked_mem = fio_pin_memory();
1330 if (!terse_output) {
1331 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1335 signal(SIGINT, sig_handler);
1336 signal(SIGALRM, sig_handler);
1338 todo = thread_number;
1341 m_rate = t_rate = 0;
1343 for (i = 0; i < thread_number; i++) {
1346 print_status_init(td->thread_number - 1);
1350 if (!td->create_serialize)
1354 * do file setup here so it happens sequentially,
1355 * we don't want X number of threads getting their
1356 * client data interspersed on disk
1358 if (setup_files(td)) {
1359 td_set_runstate(td, TD_REAPED);
1367 struct thread_data *map[MAX_JOBS];
1368 struct timeval this_start;
1369 int this_jobs = 0, left;
1372 * create threads (TD_NOT_CREATED -> TD_CREATED)
1374 for (i = 0; i < thread_number; i++) {
1377 if (td->runstate != TD_NOT_CREATED)
1381 * never got a chance to start, killed by other
1382 * thread for some reason
1384 if (td->terminate) {
1389 if (td->start_delay) {
1390 spent = mtime_since_genesis();
1392 if (td->start_delay * 1000 > spent)
1396 if (td->stonewall && (nr_started || nr_running))
1400 * Set state to created. Thread will transition
1401 * to TD_INITIALIZED when it's done setting up.
1403 td_set_runstate(td, TD_CREATED);
1404 map[this_jobs++] = td;
1405 fio_sem_init(&startup_sem, 1);
1408 if (td->use_thread) {
1409 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1410 perror("thread_create");
1415 fio_sem_down(&startup_sem);
1417 fork_main(shm_id, i);
1424 * Wait for the started threads to transition to
1427 gettimeofday(&this_start, NULL);
1430 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1435 for (i = 0; i < this_jobs; i++) {
1439 if (td->runstate == TD_INITIALIZED) {
1442 } else if (td->runstate >= TD_EXITED) {
1446 nr_running++; /* work-around... */
1452 log_err("fio: %d jobs failed to start\n", left);
1453 for (i = 0; i < this_jobs; i++) {
1457 kill(td->pid, SIGTERM);
1463 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1465 for (i = 0; i < thread_number; i++) {
1468 if (td->runstate != TD_INITIALIZED)
1471 td_set_runstate(td, TD_RUNNING);
1474 m_rate += td->ratemin;
1477 fio_sem_up(&td->mutex);
1480 reap_threads(&nr_running, &t_rate, &m_rate);
1486 while (nr_running) {
1487 reap_threads(&nr_running, &t_rate, &m_rate);
1492 fio_unpin_memory(mlocked_mem);
1495 int main(int argc, char *argv[])
1497 if (parse_options(argc, argv))
1500 if (!thread_number) {
1501 log_err("Nothing to do\n");
1505 disk_util_timer_arm();