2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
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.
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.
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
35 #include <sys/types.h>
38 #include <semaphore.h>
41 #include <asm/unistd.h>
45 #define MAX_JOBS (1024)
48 * assume we don't have _get either, if _set isn't defined
50 #ifndef __NR_ioprio_set
52 #define __NR_ioprio_set 289
53 #define __NR_ioprio_get 290
54 #elif defined(__powerpc__) || defined(__powerpc64__)
55 #define __NR_ioprio_set 273
56 #define __NR_ioprio_get 274
57 #elif defined(__x86_64__)
58 #define __NR_ioprio_set 251
59 #define __NR_ioprio_get 252
60 #elif defined(__ia64__)
61 #define __NR_ioprio_set 1274
62 #define __NR_ioprio_get 1275
63 #elif defined(__alpha__)
64 #define __NR_ioprio_set 442
65 #define __NR_ioprio_get 443
66 #elif defined(__s390x__) || defined(__s390__)
67 #define __NR_ioprio_set 282
68 #define __NR_ioprio_get 283
70 #error "Unsupported arch"
74 #ifndef __NR_fadvise64
76 #define __NR_fadvise64 250
77 #elif defined(__powerpc__) || defined(__powerpc64__)
78 #define __NR_fadvise64 233
79 #elif defined(__x86_64__)
80 #define __NR_fadvise64 221
81 #elif defined(__ia64__)
82 #define __NR_fadvise64 1234
83 #elif defined(__alpha__)
84 #define __NR_fadvise64 413
85 #elif defined(__s390x__) || defined(__s390__)
86 #define __NR_fadvise64 253
88 #error "Unsupported arch"
92 static int ioprio_set(int which, int who, int ioprio)
94 return syscall(__NR_ioprio_set, which, who, ioprio);
98 * we want fadvise64 really, but it's so tangled... later
100 static int fadvise(int fd, loff_t offset, size_t len, int advice)
103 return syscall(__NR_fadvise64, fd, offset, offset >> 32, len, advice);
105 return posix_fadvise(fd, (off_t) offset, len, advice);
110 IOPRIO_WHO_PROCESS = 1,
115 #define IOPRIO_CLASS_SHIFT 13
119 #define DEF_BS (4096)
120 #define DEF_TIMEOUT (30)
121 #define DEF_RATE_CYCLE (1000)
122 #define DEF_ODIRECT (1)
123 #define DEF_SEQUENTIAL (1)
124 #define DEF_RAND_REPEAT (1)
125 #define DEF_OVERWRITE (0)
126 #define DEF_CREATE (1)
127 #define DEF_INVALIDATE (1)
128 #define DEF_SYNCIO (0)
129 #define DEF_RANDSEED (0xb1899bedUL)
131 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
133 static int repeatable = DEF_RAND_REPEAT;
134 static int rate_quit = 1;
135 static int write_lat_log;
136 static int write_bw_log;
137 static int exitall_on_terminate;
139 static int thread_number;
140 static char *ini_file;
142 static int max_jobs = MAX_JOBS;
144 static char run_str[MAX_JOBS + 1];
174 struct timeval start_time;
175 struct timeval issue_time;
179 unsigned long long offset;
181 struct list_head list;
186 unsigned long val_sq;
187 unsigned long max_val;
188 unsigned long min_val;
189 unsigned long samples;
198 unsigned long nr_samples;
199 unsigned long max_samples;
200 struct io_sample *log;
203 #define td_read(td) ((td)->ddir == DDIR_READ)
204 #define should_fsync(td) (!td_read(td) && !(td)->odirect)
213 volatile int terminate;
214 volatile int runstate;
217 unsigned int sequential;
221 unsigned int odirect;
222 unsigned int thinktime;
223 unsigned int fsync_blocks;
224 unsigned int start_delay;
225 unsigned int timeout;
226 unsigned int use_aio;
227 unsigned int create_file;
228 unsigned int overwrite;
229 unsigned int invalidate_cache;
230 unsigned long long file_size;
231 unsigned long long file_offset;
232 unsigned int sync_io;
233 unsigned int mem_type;
236 struct drand48_data bsrange_state;
242 io_context_t aio_ctx;
243 unsigned int aio_depth;
244 struct io_event *aio_events;
246 unsigned int cur_depth;
247 struct list_head io_u_freelist;
248 struct list_head io_u_busylist;
251 unsigned int ratemin;
252 unsigned int ratecycle;
253 unsigned long rate_usec_cycle;
254 long rate_pending_usleep;
255 unsigned long rate_kb;
256 struct timeval lastrate;
258 unsigned long runtime; /* sec */
260 unsigned long io_blocks;
262 unsigned long last_kb;
264 struct drand48_data random_state;
267 * bandwidth and latency stats
269 struct io_stat clat_stat; /* completion latency */
270 struct io_stat slat_stat; /* submission latency */
272 struct io_stat bw_stat; /* bandwidth stats */
273 unsigned long stat_io_kb;
274 struct timeval stat_sample_time;
276 struct io_log *lat_log;
277 struct io_log *bw_log;
279 struct timeval start;
282 static struct thread_data *threads;
283 static struct thread_data def_thread;
285 static sem_t startup_sem;
287 static void sig_handler(int sig)
291 for (i = 0; i < thread_number; i++) {
292 struct thread_data *td = &threads[i];
299 static int init_random_state(struct thread_data *td)
301 unsigned long seed = DEF_RANDSEED;
303 srand48_r(seed, &td->bsrange_state);
309 int fd = open("/dev/random", O_RDONLY);
316 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
325 srand48_r(seed, &td->random_state);
329 static unsigned long utime_since(struct timeval *s, struct timeval *e)
333 sec = e->tv_sec - s->tv_sec;
334 usec = e->tv_usec - s->tv_usec;
335 if (sec > 0 && usec < 0) {
340 sec *= (double) 1000000;
345 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
349 sec = e->tv_sec - s->tv_sec;
350 usec = e->tv_usec - s->tv_usec;
351 if (sec > 0 && usec < 0) {
356 sec *= (double) 1000;
357 usec /= (double) 1000;
362 static unsigned long mtime_since_now(struct timeval *s)
366 gettimeofday(&t, NULL);
367 return mtime_since(s, &t);
370 static inline unsigned long msec_now(struct timeval *s)
372 return s->tv_sec * 1000 + s->tv_usec / 1000;
375 static unsigned long long get_next_offset(struct thread_data *td)
377 unsigned long long kb;
380 if (!td->sequential) {
381 lrand48_r(&td->random_state, &r);
382 kb = (1+(double) (td->kb-1) * r / (RAND_MAX+1.0));
386 return (kb << 10) + td->file_offset;
389 static unsigned int get_next_buflen(struct thread_data *td)
394 if (td->min_bs == td->max_bs)
397 lrand48_r(&td->bsrange_state, &r);
398 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
399 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
402 if (buflen > ((td->kb - td->io_kb) << 10))
403 buflen = (td->kb - td->io_kb) << 10;
405 td->last_kb += buflen >> 10;
409 static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
412 if (val > is->max_val)
414 if (val < is->min_val)
418 is->val_sq += val * val;
422 static void add_log_sample(struct thread_data *td, struct io_log *log,
425 if (log->nr_samples == log->max_samples) {
426 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
428 log->log = realloc(log->log, new_size);
429 log->max_samples <<= 1;
432 log->log[log->nr_samples].val = val;
433 log->log[log->nr_samples].time = mtime_since_now(&td->start);
437 static void add_clat_sample(struct thread_data *td, unsigned long msec)
439 add_stat_sample(td, &td->clat_stat, msec);
442 add_log_sample(td, td->lat_log, msec);
445 static void add_slat_sample(struct thread_data *td, unsigned long msec)
447 add_stat_sample(td, &td->slat_stat, msec);
450 static void add_bw_sample(struct thread_data *td, unsigned long msec)
452 unsigned long spent = mtime_since_now(&td->stat_sample_time);
458 rate = ((td->io_kb - td->stat_io_kb) * 1024) / spent;
459 add_stat_sample(td, &td->bw_stat, rate);
462 add_log_sample(td, td->bw_log, rate);
464 gettimeofday(&td->stat_sample_time, NULL);
465 td->stat_io_kb = td->io_kb;
468 static void usec_sleep(int usec)
470 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
474 rem.tv_sec = rem.tv_nsec = 0;
475 nanosleep(&req, &rem);
479 req.tv_nsec = rem.tv_nsec;
483 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
486 unsigned long usec_cycle;
491 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
493 if (time_spent < usec_cycle) {
494 unsigned long s = usec_cycle - time_spent;
496 td->rate_pending_usleep += s;
497 if (td->rate_pending_usleep >= 100000) {
498 usec_sleep(td->rate_pending_usleep);
499 td->rate_pending_usleep = 0;
502 long overtime = time_spent - usec_cycle;
504 td->rate_pending_usleep -= overtime;
508 static int check_min_rate(struct thread_data *td, struct timeval *now)
514 * allow a 2 second settle period in the beginning
516 if (mtime_since(&td->start, now) < 2000)
520 * if rate blocks is set, sample is running
523 spent = mtime_since(&td->lastrate, now);
524 if (spent < td->ratecycle)
527 rate = ((td->io_kb - td->rate_kb) * 1024) / spent;
528 if (rate < td->ratemin) {
529 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
536 td->rate_kb = td->io_kb;
537 memcpy(&td->lastrate, now, sizeof(*now));
541 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
543 if (mtime_since(&td->start, t) >= td->timeout * 1000)
549 static void put_io_u(struct thread_data *td, struct io_u *io_u)
551 list_del(&io_u->list);
552 list_add(&io_u->list, &td->io_u_freelist);
556 static struct io_u *get_io_u(struct thread_data *td)
560 unsigned long long off;
562 if (list_empty(&td->io_u_freelist))
565 off = get_next_offset(td);
566 len = get_next_buflen(td);
570 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
571 list_del(&io_u->list);
572 list_add(&io_u->list, &td->io_u_busylist);
579 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
581 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
584 gettimeofday(&io_u->start_time, NULL);
589 static void do_sync_io(struct thread_data *td)
591 unsigned long msec, usec;
596 for (td->io_kb = 0; td->io_kb < td->kb;) {
607 if (td->cur_off != io_u->offset) {
608 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
615 ret = read(td->fd, io_u->buf, io_u->buflen);
617 ret = write(td->fd, io_u->buf, io_u->buflen);
619 if (ret < (int) io_u->buflen) {
626 td->io_kb += io_u->buflen >> 10;
627 td->cur_off = io_u->offset + io_u->buflen;
629 gettimeofday(&e, NULL);
631 usec = utime_since(&io_u->start_time, &e);
633 rate_throttle(td, usec, io_u->buflen);
635 if (check_min_rate(td, &e)) {
641 add_clat_sample(td, msec);
642 add_bw_sample(td, msec);
644 if (runtime_exceeded(td, &e))
650 usec_sleep(td->thinktime);
652 if (should_fsync(td) && td->fsync_blocks &&
653 (td->io_blocks % td->fsync_blocks) == 0)
657 if (should_fsync(td))
661 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
663 struct iocb *iocb = &io_u->iocb;
667 ret = io_submit(td->aio_ctx, 1, &iocb);
670 else if (ret == EAGAIN)
672 else if (ret == EINTR)
681 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
682 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
684 static int ios_completed(struct thread_data *td, int nr)
691 gettimeofday(&e, NULL);
693 for (i = 0, bytes_done = 0; i < nr; i++) {
694 io_u = ev_to_iou(td->aio_events + i);
697 td->io_kb += io_u->buflen >> 10;
698 bytes_done += io_u->buflen;
700 msec = mtime_since(&io_u->issue_time, &e);
702 add_clat_sample(td, msec);
703 add_bw_sample(td, msec);
711 static void cleanup_pending_aio(struct thread_data *td)
713 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
714 struct list_head *entry, *n;
719 * get immediately available events, if any
721 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
723 ios_completed(td, r);
726 * now cancel remaining active events
728 list_for_each_safe(entry, n, &td->io_u_busylist) {
729 io_u = list_entry(entry, struct io_u, list);
731 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
737 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
739 ios_completed(td, r);
743 static void do_async_io(struct thread_data *td)
748 for (td->io_kb = 0; td->io_kb < td->kb;) {
749 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
750 struct timespec *timeout;
751 int ret, min_evts = 0;
753 unsigned int bytes_done;
762 memcpy(&s, &io_u->start_time, sizeof(s));
764 ret = io_u_queue(td, io_u);
771 gettimeofday(&io_u->issue_time, NULL);
772 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
773 if (td->cur_depth < td->aio_depth) {
781 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
788 bytes_done = ios_completed(td, ret);
791 * the rate is batched for now, it should work for batches
792 * of completions except the very first one which may look
795 gettimeofday(&e, NULL);
796 usec = utime_since(&s, &e);
798 rate_throttle(td, usec, bytes_done);
800 if (check_min_rate(td, &e)) {
805 if (runtime_exceeded(td, &e))
809 usec_sleep(td->thinktime);
811 if (should_fsync(td) && td->fsync_blocks &&
812 (td->io_blocks % td->fsync_blocks) == 0)
817 cleanup_pending_aio(td);
819 if (should_fsync(td))
823 static void cleanup_aio(struct thread_data *td)
825 io_destroy(td->aio_ctx);
828 free(td->aio_events);
831 static int init_aio(struct thread_data *td)
833 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
838 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
842 static void cleanup_io_u(struct thread_data *td)
844 struct list_head *entry, *n;
847 list_for_each_safe(entry, n, &td->io_u_freelist) {
848 io_u = list_entry(entry, struct io_u, list);
850 list_del(&io_u->list);
854 if (td->mem_type == MEM_MALLOC)
855 free(td->orig_buffer);
856 else if (td->mem_type == MEM_SHM) {
857 struct shmid_ds sbuf;
859 shmdt(td->orig_buffer);
860 shmctl(td->shm_id, IPC_RMID, &sbuf);
864 static int init_io_u(struct thread_data *td)
867 int i, max_units, mem_size;
873 max_units = td->aio_depth;
875 mem_size = td->max_bs * max_units + MASK;
877 if (td->mem_type == MEM_MALLOC)
878 td->orig_buffer = malloc(mem_size);
879 else if (td->mem_type == MEM_SHM) {
880 td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
881 if (td->shm_id < 0) {
887 td->orig_buffer = shmat(td->shm_id, NULL, 0);
888 if (td->orig_buffer == (void *) -1) {
895 INIT_LIST_HEAD(&td->io_u_freelist);
896 INIT_LIST_HEAD(&td->io_u_busylist);
898 p = ALIGN(td->orig_buffer);
899 for (i = 0; i < max_units; i++) {
900 io_u = malloc(sizeof(*io_u));
901 memset(io_u, 0, sizeof(*io_u));
902 INIT_LIST_HEAD(&io_u->list);
904 io_u->buf = p + td->max_bs * i;
905 list_add(&io_u->list, &td->io_u_freelist);
911 static void setup_log(struct io_log **log)
913 struct io_log *l = malloc(sizeof(*l));
916 l->max_samples = 1024;
917 l->log = malloc(l->max_samples * sizeof(struct io_sample));
921 static void finish_log(struct thread_data *td, struct io_log *log, char *name)
927 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
928 f = fopen(file_name, "w");
934 for (i = 0; i < log->nr_samples; i++)
935 fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val);
942 static int create_file(struct thread_data *td)
944 unsigned long long left;
949 * unless specifically asked for overwrite, let normal io extend it
951 if (!td_read(td) && !td->overwrite)
954 if (!td->file_size) {
955 fprintf(stderr, "Need size for create\n");
960 printf("Client%d: Laying out IO file\n", td->thread_number);
962 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
968 td->kb = td->file_size >> 10;
969 b = malloc(td->max_bs);
970 memset(b, 0, td->max_bs);
972 left = td->file_size;
978 r = write(td->fd, b, bs);
1000 static int file_exists(struct thread_data *td)
1004 if (stat(td->file_name, &st) != -1)
1007 return errno != ENOENT;
1010 static int setup_file(struct thread_data *td)
1015 if (!file_exists(td)) {
1016 if (!td->create_file) {
1020 if (create_file(td))
1028 td->fd = open(td->file_name, flags | O_RDONLY);
1035 td->fd = open(td->file_name, flags | O_WRONLY | O_CREAT, 0600);
1044 if (fstat(td->fd, &st) == -1) {
1049 if (td->file_size > st.st_size)
1050 st.st_size = td->file_size;
1053 td->file_size = 1024 * 1024 * 1024;
1055 st.st_size = td->file_size;
1058 td->kb = (st.st_size - td->file_offset) / 1024;
1060 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1065 if (td->invalidate_cache) {
1066 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
1075 static void *thread_main(int shm_id, int offset, char *argv[])
1077 struct thread_data *td;
1083 data = shmat(shm_id, NULL, 0);
1084 if (data == (void *) -1) {
1089 td = data + offset * sizeof(struct thread_data);
1095 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1100 sprintf(argv[0], "fio%d", offset);
1102 if (td->use_aio && init_aio(td))
1105 if (init_random_state(td))
1109 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1115 sem_post(&startup_sem);
1116 sem_wait(&td->mutex);
1118 gettimeofday(&td->start, NULL);
1121 memcpy(&td->lastrate, &td->start, sizeof(td->start));
1123 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1130 td->runtime = mtime_since_now(&td->start);
1134 finish_log(td, td->bw_log, "bw");
1136 finish_log(td, td->lat_log, "lat");
1138 if (exitall_on_terminate)
1150 sem_post(&startup_sem);
1151 sem_wait(&td->mutex);
1153 td->runstate = TD_EXITED;
1158 static void free_shm(void)
1160 struct shmid_ds sbuf;
1165 shmctl(shm_id, IPC_RMID, &sbuf);
1169 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1170 double *mean, double *dev)
1174 if (is->samples == 0)
1180 n = (double) is->samples;
1181 *mean = (double) is->val / n;
1182 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1186 static void show_thread_status(struct thread_data *td)
1188 int prio, prio_class;
1189 unsigned long min, max, bw = 0;
1192 if (!td->io_kb && !td->error)
1196 bw = td->io_kb * 1024 / td->runtime;
1198 prio = td->ioprio & 0xff;
1199 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1201 printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", td->thread_number, td->error, td->io_kb >> 10, bw, td->runtime);
1203 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1204 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1205 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1206 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1207 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1208 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1211 static int setup_rate(struct thread_data *td)
1213 int nr_reads_per_sec;
1218 if (td->rate < td->ratemin) {
1219 fprintf(stderr, "min rate larger than nominal rate\n");
1223 nr_reads_per_sec = td->rate * 1024 / td->min_bs;
1224 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1225 td->rate_pending_usleep = 0;
1229 static struct thread_data *get_new_job(int global)
1231 struct thread_data *td;
1235 if (thread_number >= max_jobs)
1238 td = &threads[thread_number++];
1239 memset(td, 0, sizeof(*td));
1242 td->thread_number = thread_number;
1244 td->ddir = def_thread.ddir;
1245 td->ioprio = def_thread.ioprio;
1246 td->sequential = def_thread.sequential;
1247 td->bs = def_thread.bs;
1248 td->min_bs = def_thread.min_bs;
1249 td->max_bs = def_thread.max_bs;
1250 td->odirect = def_thread.odirect;
1251 td->thinktime = def_thread.thinktime;
1252 td->fsync_blocks = def_thread.fsync_blocks;
1253 td->start_delay = def_thread.start_delay;
1254 td->timeout = def_thread.timeout;
1255 td->use_aio = def_thread.use_aio;
1256 td->create_file = def_thread.create_file;
1257 td->overwrite = def_thread.overwrite;
1258 td->invalidate_cache = def_thread.invalidate_cache;
1259 td->file_size = def_thread.file_size;
1260 td->file_offset = def_thread.file_offset;
1261 td->rate = def_thread.rate;
1262 td->ratemin = def_thread.ratemin;
1263 td->ratecycle = def_thread.ratecycle;
1264 td->aio_depth = def_thread.aio_depth;
1265 td->sync_io = def_thread.sync_io;
1266 td->mem_type = def_thread.mem_type;
1267 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
1272 static void put_job(struct thread_data *td)
1274 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1278 static int add_job(struct thread_data *td, const char *filename, int prioclass,
1281 if (td == &def_thread)
1284 strcpy(td->file_name, filename);
1285 sem_init(&td->mutex, 1, 0);
1286 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1288 td->clat_stat.min_val = ULONG_MAX;
1289 td->slat_stat.min_val = ULONG_MAX;
1290 td->bw_stat.min_val = ULONG_MAX;
1292 run_str[td->thread_number - 1] = 'P';
1294 if (td->use_aio && !td->aio_depth)
1297 if (td->min_bs == -1)
1298 td->min_bs = td->bs;
1299 if (td->max_bs == -1)
1300 td->max_bs = td->bs;
1306 setup_log(&td->lat_log);
1308 setup_log(&td->bw_log);
1310 printf("Client%d: file=%s, rw=%d, prio=%d/%d, seq=%d, odir=%d, bs=%d-%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, prioclass, prio, td->sequential, td->odirect, td->min_bs, td->max_bs, td->rate, td->use_aio, td->aio_depth);
1314 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1320 for (i = 0; i < sizeof(int) * 8; i++) {
1322 CPU_SET(i, &cpumask);
1326 static void fill_option(const char *input, char *output)
1331 while (input[i] != ',' && input[i] != '}' && input[i] != '\0') {
1332 output[i] = input[i];
1339 unsigned long get_mult(char c)
1350 return 1024 * 1024 * 1024;
1357 * convert string after '=' into decimal value, noting any size suffix
1359 static int str_cnv(char *p, unsigned long long *val)
1364 str = strstr(p, "=");
1371 *val = strtoul(str, NULL, 10);
1372 if (*val == ULONG_MAX && errno == ERANGE)
1375 *val *= get_mult(str[len - 2]);
1387 static void parse_jobs_cmd(int argc, char *argv[], int index)
1389 struct thread_data *td;
1390 unsigned int prio, prioclass, cpu;
1391 char *string, *filename, *p, *c;
1394 string = malloc(256);
1395 filename = malloc(256);
1397 for (i = index; i < argc; i++) {
1400 c = strpbrk(p, "{");
1406 td = get_new_job(0);
1413 c = strstr(p, "rw=");
1417 td->ddir = DDIR_READ;
1419 td->ddir = DDIR_WRITE;
1422 c = strstr(p, "prio=");
1428 c = strstr(p, "prioclass=");
1431 prioclass = *c - '0';
1434 c = strstr(p, "file=");
1437 fill_option(c, filename);
1440 c = strstr(p, "direct=");
1449 c = strstr(p, "sync=");
1458 c = strstr(p, "thinktime=");
1461 fill_option(c, string);
1462 td->thinktime = strtoul(string, NULL, 10);
1465 c = strstr(p, "rate=");
1468 fill_option(c, string);
1469 td->rate = strtoul(string, NULL, 10);
1472 c = strstr(p, "ratemin=");
1475 fill_option(c, string);
1476 td->ratemin = strtoul(string, NULL, 10);
1479 c = strstr(p, "ratecycle=");
1482 fill_option(c, string);
1483 td->ratecycle = strtoul(string, NULL, 10);
1486 c = strstr(p, "cpumask=");
1489 fill_option(c, string);
1490 cpu = strtoul(string, NULL, 10);
1491 fill_cpu_mask(td->cpumask, cpu);
1494 c = strstr(p, "fsync=");
1497 fill_option(c, string);
1498 td->fsync_blocks = strtoul(string, NULL, 10);
1501 c = strstr(p, "startdelay=");
1504 fill_option(c, string);
1505 td->start_delay = strtoul(string, NULL, 10);
1508 c = strstr(p, "timeout=");
1511 fill_option(c, string);
1512 td->timeout = strtoul(string, NULL, 10);
1515 c = strstr(p, "invalidate=");
1519 td->invalidate_cache = 1;
1521 td->invalidate_cache = 0;
1524 c = strstr(p, "bs=");
1526 unsigned long long bs;
1533 c = strstr(p, "size=");
1536 str_cnv(c, &td->file_size);
1539 c = strstr(p, "offset=");
1542 str_cnv(c, &td->file_offset);
1545 c = strstr(p, "aio_depth=");
1548 fill_option(c, string);
1549 td->aio_depth = strtoul(string, NULL, 10);
1552 c = strstr(p, "mem=");
1555 if (!strncmp(c, "malloc", 6))
1556 td->mem_type = MEM_MALLOC;
1557 else if (!strncmp(c, "shm", 3))
1558 td->mem_type = MEM_SHM;
1560 printf("bad mem type %s\n", c);
1563 c = strstr(p, "aio");
1567 c = strstr(p, "create");
1569 td->create_file = 1;
1571 c = strstr(p, "overwrite");
1575 c = strstr(p, "random");
1578 c = strstr(p, "sequential");
1582 if (add_job(td, filename, prioclass, prio))
1590 static int check_strcnv(char *p, char *name, unsigned long long *val)
1592 if (!strstr(p, name))
1595 return str_cnv(p, val);
1598 static int check_str(char *p, char *name, char *option)
1600 char *s = strstr(p, name);
1606 if (strstr(s, option))
1612 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
1617 sprintf(str, "%s=%%lu%%c-%%lu%%c", name);
1618 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1624 sprintf(str, "%s = %%lu%%c-%%lu%%c", name);
1625 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1631 sprintf(str, "%s=%%lu-%%lu", name);
1632 if (sscanf(p, str, s, e) == 2)
1635 sprintf(str, "%s = %%lu-%%lu", name);
1636 if (sscanf(p, str, s, e) == 2)
1643 static int check_int(char *p, char *name, unsigned int *val)
1647 sprintf(str, "%s=%%d", name);
1648 if (sscanf(p, str, val) == 1)
1651 sprintf(str, "%s = %%d", name);
1652 if (sscanf(p, str, val) == 1)
1658 static int is_empty_or_comment(char *line)
1662 for (i = 0; i < strlen(line); i++) {
1665 if (!isspace(line[i]) && !iscntrl(line[i]))
1672 static int parse_jobs_ini(char *file)
1674 unsigned int prioclass, prio, cpu, global;
1675 unsigned long long ull;
1676 unsigned long ul1, ul2;
1677 struct thread_data *td;
1678 char *string, *name;
1683 f = fopen(file, "r");
1689 string = malloc(4096);
1692 while ((p = fgets(string, 4096, f)) != NULL) {
1693 if (is_empty_or_comment(p))
1695 if (sscanf(p, "[%s]", name) != 1)
1698 global = !strncmp(name, "global", 6);
1700 name[strlen(name) - 1] = '\0';
1702 td = get_new_job(global);
1710 while ((p = fgets(string, 4096, f)) != NULL) {
1711 if (is_empty_or_comment(p))
1715 if (!check_int(p, "rw", &td->ddir)) {
1719 if (!check_int(p, "prio", &prio)) {
1723 if (!check_int(p, "prioclass", &prioclass)) {
1727 if (!check_int(p, "direct", &td->odirect)) {
1731 if (!check_int(p, "rate", &td->rate)) {
1735 if (!check_int(p, "ratemin", &td->ratemin)) {
1739 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1743 if (!check_int(p, "thinktime", &td->thinktime)) {
1747 if (!check_int(p, "cpumask", &cpu)) {
1748 fill_cpu_mask(td->cpumask, cpu);
1752 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1756 if (!check_int(p, "startdelay", &td->start_delay)) {
1760 if (!check_int(p, "timeout", &td->timeout)) {
1764 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1768 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1772 if (!check_int(p, "sync", &td->sync_io)) {
1776 if (!check_range(p, "bsrange", &ul1, &ul2)) {
1782 if (!check_strcnv(p, "bs", &ull)) {
1787 if (!check_strcnv(p, "size", &td->file_size)) {
1791 if (!check_strcnv(p, "offset", &td->file_offset)) {
1795 if (!check_str(p, "mem", "malloc")) {
1796 td->mem_type = MEM_MALLOC;
1800 if (!check_str(p, "mem", "shm")) {
1801 td->mem_type = MEM_SHM;
1805 if (!strncmp(p, "sequential", 10)) {
1810 if (!strncmp(p, "random", 6)) {
1815 if (!strncmp(p, "aio", 3)) {
1820 if (!strncmp(p, "create", 6)) {
1821 td->create_file = 1;
1825 if (!strncmp(p, "overwrite", 9)) {
1830 if (!strncmp(p, "exitall", 7)) {
1831 exitall_on_terminate = 1;
1835 printf("Client%d: bad option %s\n",td->thread_number,p);
1839 if (add_job(td, name, prioclass, prio))
1849 static int parse_options(int argc, char *argv[])
1853 for (i = 1; i < argc; i++) {
1854 char *parm = argv[i];
1863 def_thread.sequential = !!atoi(parm);
1867 def_thread.bs = atoi(parm);
1868 def_thread.bs <<= 10;
1869 if (!def_thread.bs) {
1870 printf("bad block size\n");
1871 def_thread.bs = DEF_BS;
1876 def_thread.timeout = atoi(parm);
1880 repeatable = !!atoi(parm);
1884 rate_quit = !!atoi(parm);
1888 def_thread.odirect = !!atoi(parm);
1891 if (i + 1 >= argc) {
1892 printf("-f needs file as arg\n");
1895 ini_file = strdup(argv[i+1]);
1905 printf("bad option %s\n", argv[i]);
1913 static void print_thread_status(struct thread_data *td, int nr_running,
1914 int t_rate, int m_rate)
1916 printf("Threads now running: %d", nr_running);
1917 if (m_rate || t_rate)
1918 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1919 printf(" : [%s]\r", run_str);
1923 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1928 * reap exited threads (TD_EXITED -> TD_REAPED)
1930 for (i = 0; i < thread_number; i++) {
1931 struct thread_data *td = &threads[i];
1933 if (td->runstate != TD_EXITED)
1936 td->runstate = TD_REAPED;
1937 run_str[td->thread_number - 1] = '_';
1938 waitpid(td->pid, NULL, 0);
1940 (*m_rate) -= td->ratemin;
1941 (*t_rate) -= td->rate;
1946 print_thread_status(td, *nr_running, *t_rate, *m_rate);
1950 static void run_threads(char *argv[])
1952 struct timeval genesis;
1953 struct thread_data *td;
1954 unsigned long spent;
1955 int i, todo, nr_running, m_rate, t_rate;
1957 printf("Starting %d threads\n", thread_number);
1960 signal(SIGINT, sig_handler);
1962 todo = thread_number;
1964 m_rate = t_rate = 0;
1966 for (i = 0; i < thread_number; i++) {
1970 * do file setup here so it happens sequentially,
1971 * we don't want X number of threads getting their
1972 * client data interspersed on disk
1974 if (setup_file(td)) {
1975 td->runstate = TD_REAPED;
1980 gettimeofday(&genesis, NULL);
1984 * create threads (TD_NOT_CREATED -> TD_CREATED)
1986 for (i = 0; i < thread_number; i++) {
1989 if (td->runstate != TD_NOT_CREATED)
1993 * never got a chance to start, killed by other
1994 * thread for some reason
1996 if (td->terminate) {
2001 if (td->start_delay) {
2002 spent = mtime_since_now(&genesis);
2004 if (td->start_delay * 1000 > spent)
2008 td->runstate = TD_CREATED;
2009 run_str[td->thread_number - 1] = 'C';
2010 sem_init(&startup_sem, 1, 1);
2014 sem_wait(&startup_sem);
2016 thread_main(shm_id, i, argv);
2022 * start created threads (TD_CREATED -> TD_STARTED)
2024 for (i = 0; i < thread_number; i++) {
2025 struct thread_data *td = &threads[i];
2027 if (td->runstate != TD_CREATED)
2030 td->runstate = TD_STARTED;
2031 run_str[td->thread_number - 1] = '+';
2033 m_rate += td->ratemin;
2035 sem_post(&td->mutex);
2037 print_thread_status(td, nr_running, t_rate, m_rate);
2040 reap_threads(&nr_running, &t_rate, &m_rate);
2046 while (nr_running) {
2047 reap_threads(&nr_running, &t_rate, &m_rate);
2052 int setup_thread_area(void)
2055 * 1024 is too much on some machines, scale max_jobs if
2056 * we get a failure that looks like too large a shm segment
2059 int s = max_jobs * sizeof(struct thread_data);
2061 shm_id = shmget(0, s, IPC_CREAT | 0600);
2064 if (errno != EINVAL) {
2075 threads = shmat(shm_id, NULL, 0);
2076 if (threads == (void *) -1) {
2085 int main(int argc, char *argv[])
2087 static unsigned long max_run[2], min_run[2];
2088 static unsigned long max_bw[2], min_bw[2];
2089 static unsigned long io_mb[2], agg[2];
2092 if (setup_thread_area())
2095 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
2096 perror("sched_getaffinity");
2103 def_thread.ddir = DDIR_READ;
2104 def_thread.bs = DEF_BS;
2105 def_thread.min_bs = -1;
2106 def_thread.max_bs = -1;
2107 def_thread.odirect = DEF_ODIRECT;
2108 def_thread.ratecycle = DEF_RATE_CYCLE;
2109 def_thread.sequential = DEF_SEQUENTIAL;
2110 def_thread.timeout = DEF_TIMEOUT;
2111 def_thread.create_file = DEF_CREATE;
2112 def_thread.overwrite = DEF_OVERWRITE;
2113 def_thread.invalidate_cache = DEF_INVALIDATE;
2114 def_thread.sync_io = DEF_SYNCIO;
2115 def_thread.mem_type = MEM_MALLOC;
2117 i = parse_options(argc, argv);
2120 if (parse_jobs_ini(ini_file))
2123 parse_jobs_cmd(argc, argv, i);
2125 if (!thread_number) {
2126 printf("Nothing to do\n");
2132 min_bw[0] = min_run[0] = ~0UL;
2133 min_bw[1] = min_run[1] = ~0UL;
2134 io_mb[0] = io_mb[1] = 0;
2135 agg[0] = agg[1] = 0;
2136 for (i = 0; i < thread_number; i++) {
2137 struct thread_data *td = &threads[i];
2138 unsigned long bw = 0;
2141 if (td->runtime < min_run[td->ddir])
2142 min_run[td->ddir] = td->runtime;
2143 if (td->runtime > max_run[td->ddir])
2144 max_run[td->ddir] = td->runtime;
2147 bw = td->io_kb * 1024 / td->runtime;
2148 if (bw < min_bw[td->ddir])
2149 min_bw[td->ddir] = bw;
2150 if (bw > max_bw[td->ddir])
2151 max_bw[td->ddir] = bw;
2153 io_mb[td->ddir] += td->io_kb >> 10;
2156 show_thread_status(td);
2160 agg[0] = io_mb[0] * 1024 * 1000 / max_run[0];
2162 agg[1] = io_mb[1] * 1024 * 1000 / max_run[1];
2164 printf("\nRun status:\n");
2165 if (max_run[DDIR_READ])
2166 printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", io_mb[0], agg[0], min_bw[0], max_bw[0], min_run[0], max_run[0]);
2167 if (max_run[DDIR_WRITE])
2168 printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", io_mb[1], agg[1], min_bw[1], max_bw[1], min_run[1], max_run[1]);