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;
138 static int thread_number;
139 static char *ini_file;
141 static int max_jobs = MAX_JOBS;
143 static char run_str[MAX_JOBS + 1];
173 struct timeval start_time;
174 struct timeval issue_time;
178 unsigned long long offset;
180 struct list_head list;
185 unsigned long val_sq;
186 unsigned long max_val;
187 unsigned long min_val;
188 unsigned long samples;
197 unsigned long nr_samples;
198 unsigned long max_samples;
199 struct io_sample *log;
202 #define td_read(td) ((td)->ddir == DDIR_READ)
203 #define should_fsync(td) (!td_read(td) && !(td)->odirect)
212 volatile int terminate;
213 volatile int runstate;
216 unsigned int sequential;
220 unsigned int odirect;
221 unsigned int delay_sleep;
222 unsigned int fsync_blocks;
223 unsigned int start_delay;
224 unsigned int timeout;
225 unsigned int use_aio;
226 unsigned int create_file;
227 unsigned int overwrite;
228 unsigned int invalidate_cache;
229 unsigned long long file_size;
230 unsigned long long file_offset;
231 unsigned int sync_io;
232 unsigned int mem_type;
235 struct drand48_data bsrange_state;
241 io_context_t aio_ctx;
242 unsigned int aio_depth;
243 struct io_event *aio_events;
245 unsigned int cur_depth;
246 struct list_head io_u_freelist;
247 struct list_head io_u_busylist;
250 unsigned int ratemin;
251 unsigned int ratecycle;
252 unsigned long rate_usec_cycle;
253 long rate_pending_usleep;
254 unsigned long rate_kb;
255 struct timeval lastrate;
257 unsigned long runtime; /* sec */
259 unsigned long io_blocks;
261 unsigned long last_kb;
263 struct drand48_data random_state;
266 * bandwidth and latency stats
268 struct io_stat clat_stat; /* completion latency */
269 struct io_stat slat_stat; /* submission latency */
271 struct io_stat bw_stat; /* bandwidth stats */
272 unsigned long stat_io_kb;
273 struct timeval stat_sample_time;
275 struct io_log *lat_log;
276 struct io_log *bw_log;
278 struct timeval start;
281 static struct thread_data *threads;
282 static struct thread_data def_thread;
284 static sem_t startup_sem;
286 static void sig_handler(int sig)
290 for (i = 0; i < thread_number; i++) {
291 struct thread_data *td = &threads[i];
298 static int init_random_state(struct thread_data *td)
300 unsigned long seed = DEF_RANDSEED;
302 srand48_r(seed, &td->bsrange_state);
308 int fd = open("/dev/random", O_RDONLY);
315 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
324 srand48_r(seed, &td->random_state);
328 static unsigned long utime_since(struct timeval *s, struct timeval *e)
332 sec = e->tv_sec - s->tv_sec;
333 usec = e->tv_usec - s->tv_usec;
334 if (sec > 0 && usec < 0) {
339 sec *= (double) 1000000;
344 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
348 sec = e->tv_sec - s->tv_sec;
349 usec = e->tv_usec - s->tv_usec;
350 if (sec > 0 && usec < 0) {
355 sec *= (double) 1000;
356 usec /= (double) 1000;
361 static unsigned long mtime_since_now(struct timeval *s)
365 gettimeofday(&t, NULL);
366 return mtime_since(s, &t);
369 static inline unsigned long msec_now(struct timeval *s)
371 return s->tv_sec * 1000 + s->tv_usec / 1000;
374 static unsigned long long get_next_offset(struct thread_data *td)
376 unsigned long long b;
379 if (!td->sequential) {
380 lrand48_r(&td->random_state, &r);
381 b = (1+(double) (td->kb-1) * r / (RAND_MAX+1.0));
383 b = td->last_kb << 10;
385 return b + td->file_offset;
388 static unsigned int get_next_buflen(struct thread_data *td)
393 if (td->min_bs == td->max_bs)
396 lrand48_r(&td->bsrange_state, &r);
397 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
398 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
401 if (buflen > ((td->kb - td->io_kb) << 10))
402 buflen = (td->kb - td->io_kb) << 10;
404 td->last_kb += buflen >> 10;
408 static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
411 if (val > is->max_val)
413 if (val < is->min_val)
417 is->val_sq += val * val;
421 static void add_log_sample(struct thread_data *td, struct io_log *log,
424 if (log->nr_samples == log->max_samples) {
425 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
427 log->log = realloc(log->log, new_size);
428 log->max_samples <<= 1;
431 log->log[log->nr_samples].val = val;
432 log->log[log->nr_samples].time = mtime_since_now(&td->start);
436 static void add_clat_sample(struct thread_data *td, unsigned long msec)
438 add_stat_sample(td, &td->clat_stat, msec);
441 add_log_sample(td, td->lat_log, msec);
444 static void add_slat_sample(struct thread_data *td, unsigned long msec)
446 add_stat_sample(td, &td->slat_stat, msec);
449 static void add_bw_sample(struct thread_data *td, unsigned long msec)
451 unsigned long spent = mtime_since_now(&td->stat_sample_time);
457 rate = ((td->io_kb - td->stat_io_kb) * 1024) / spent;
458 add_stat_sample(td, &td->bw_stat, rate);
461 add_log_sample(td, td->bw_log, rate);
463 gettimeofday(&td->stat_sample_time, NULL);
464 td->stat_io_kb = td->io_kb;
467 static void usec_sleep(int usec)
469 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
473 rem.tv_sec = rem.tv_nsec = 0;
474 nanosleep(&req, &rem);
478 req.tv_nsec = rem.tv_nsec;
482 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
485 unsigned long usec_cycle;
490 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
492 if (time_spent < usec_cycle) {
493 unsigned long s = usec_cycle - time_spent;
495 td->rate_pending_usleep += s;
496 if (td->rate_pending_usleep >= 100000) {
497 usec_sleep(td->rate_pending_usleep);
498 td->rate_pending_usleep = 0;
501 long overtime = time_spent - usec_cycle;
503 td->rate_pending_usleep -= overtime;
507 static int check_min_rate(struct thread_data *td, struct timeval *now)
513 * allow a 2 second settle period in the beginning
515 if (mtime_since(&td->start, now) < 2000)
519 * if rate blocks is set, sample is running
522 spent = mtime_since(&td->lastrate, now);
523 if (spent < td->ratecycle)
526 rate = ((td->io_kb - td->rate_kb) * 1024) / spent;
527 if (rate < td->ratemin) {
528 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
535 td->rate_kb = td->io_kb;
536 memcpy(&td->lastrate, now, sizeof(*now));
540 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
542 if (mtime_since(&td->start, t) >= td->timeout * 1000)
548 static void put_io_u(struct thread_data *td, struct io_u *io_u)
550 list_del(&io_u->list);
551 list_add(&io_u->list, &td->io_u_freelist);
555 static struct io_u *get_io_u(struct thread_data *td)
559 unsigned long long off;
561 if (list_empty(&td->io_u_freelist))
564 off = get_next_offset(td);
565 len = get_next_buflen(td);
569 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
570 list_del(&io_u->list);
571 list_add(&io_u->list, &td->io_u_busylist);
578 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
580 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
583 gettimeofday(&io_u->start_time, NULL);
588 static void do_sync_io(struct thread_data *td)
590 unsigned long msec, usec;
595 for (td->io_kb = 0; td->io_kb < td->kb;) {
606 if (td->cur_off != io_u->offset) {
607 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
614 usec_sleep(td->delay_sleep);
617 ret = read(td->fd, io_u->buf, io_u->buflen);
619 ret = write(td->fd, io_u->buf, io_u->buflen);
621 if (ret < (int) io_u->buflen) {
628 td->io_kb += io_u->buflen >> 10;
629 td->cur_off = io_u->offset + io_u->buflen;
631 if (should_fsync(td) && td->fsync_blocks &&
632 (td->io_blocks % td->fsync_blocks) == 0)
635 gettimeofday(&e, NULL);
637 usec = utime_since(&io_u->start_time, &e);
639 rate_throttle(td, usec, io_u->buflen);
641 if (check_min_rate(td, &e)) {
647 add_clat_sample(td, msec);
648 add_bw_sample(td, msec);
650 if (runtime_exceeded(td, &e))
656 if (should_fsync(td))
660 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
662 struct iocb *iocb = &io_u->iocb;
666 ret = io_submit(td->aio_ctx, 1, &iocb);
669 else if (ret == EAGAIN)
671 else if (ret == EINTR)
680 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
681 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
683 static int ios_completed(struct thread_data *td, int nr)
690 gettimeofday(&e, NULL);
692 for (i = 0, bytes_done = 0; i < nr; i++) {
693 io_u = ev_to_iou(td->aio_events + i);
696 td->io_kb += io_u->buflen >> 10;
697 bytes_done += io_u->buflen;
699 msec = mtime_since(&io_u->issue_time, &e);
701 add_clat_sample(td, msec);
702 add_bw_sample(td, msec);
710 static void cleanup_pending_aio(struct thread_data *td)
712 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
713 struct list_head *entry, *n;
718 * get immediately available events, if any
720 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
722 ios_completed(td, r);
725 * now cancel remaining active events
727 list_for_each_safe(entry, n, &td->io_u_busylist) {
728 io_u = list_entry(entry, struct io_u, list);
730 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
736 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
738 ios_completed(td, r);
742 static void do_async_io(struct thread_data *td)
747 for (td->io_kb = 0; td->io_kb < td->kb;) {
748 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
749 struct timespec *timeout;
750 int ret, min_evts = 0;
752 unsigned int bytes_done;
758 usec_sleep(td->delay_sleep);
764 memcpy(&s, &io_u->start_time, sizeof(s));
766 ret = io_u_queue(td, io_u);
773 gettimeofday(&io_u->issue_time, NULL);
774 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
776 if (td->cur_depth < td->aio_depth) {
784 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
791 bytes_done = ios_completed(td, ret);
793 if (should_fsync(td) && td->fsync_blocks &&
794 (td->io_blocks % td->fsync_blocks) == 0)
798 * the rate is batched for now, it should work for batches
799 * of completions except the very first one which may look
802 gettimeofday(&e, NULL);
803 usec = utime_since(&s, &e);
805 rate_throttle(td, usec, bytes_done);
807 if (check_min_rate(td, &e)) {
812 if (runtime_exceeded(td, &e))
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");
1147 sem_post(&startup_sem);
1148 sem_wait(&td->mutex);
1150 td->runstate = TD_EXITED;
1155 static void free_shm(void)
1157 struct shmid_ds sbuf;
1162 shmctl(shm_id, IPC_RMID, &sbuf);
1166 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1167 double *mean, double *dev)
1171 if (is->samples == 0)
1177 n = (double) is->samples;
1178 *mean = (double) is->val / n;
1179 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1183 static void show_thread_status(struct thread_data *td)
1185 int prio, prio_class;
1186 unsigned long min, max, bw = 0;
1189 if (!td->io_kb && !td->error)
1193 bw = td->io_kb * 1024 / td->runtime;
1195 prio = td->ioprio & 0xff;
1196 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1198 printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/s\n", td->thread_number, td->error, td->io_kb >> 10, bw);
1200 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1201 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1202 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1203 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1204 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1205 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1208 static int setup_rate(struct thread_data *td)
1210 int nr_reads_per_sec;
1215 if (td->rate < td->ratemin) {
1216 fprintf(stderr, "min rate larger than nominal rate\n");
1220 nr_reads_per_sec = td->rate * 1024 / td->min_bs;
1221 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1222 td->rate_pending_usleep = 0;
1226 static struct thread_data *get_new_job(int global)
1228 struct thread_data *td;
1232 if (thread_number >= max_jobs)
1235 td = &threads[thread_number++];
1236 memset(td, 0, sizeof(*td));
1239 td->thread_number = thread_number;
1241 td->ddir = def_thread.ddir;
1242 td->ioprio = def_thread.ioprio;
1243 td->sequential = def_thread.sequential;
1244 td->bs = def_thread.bs;
1245 td->min_bs = def_thread.min_bs;
1246 td->max_bs = def_thread.max_bs;
1247 td->odirect = def_thread.odirect;
1248 td->delay_sleep = def_thread.delay_sleep;
1249 td->fsync_blocks = def_thread.fsync_blocks;
1250 td->start_delay = def_thread.start_delay;
1251 td->timeout = def_thread.timeout;
1252 td->use_aio = def_thread.use_aio;
1253 td->create_file = def_thread.create_file;
1254 td->overwrite = def_thread.overwrite;
1255 td->invalidate_cache = def_thread.invalidate_cache;
1256 td->file_size = def_thread.file_size;
1257 td->file_offset = def_thread.file_offset;
1258 td->rate = def_thread.rate;
1259 td->ratemin = def_thread.ratemin;
1260 td->ratecycle = def_thread.ratecycle;
1261 td->aio_depth = def_thread.aio_depth;
1262 td->sync_io = def_thread.sync_io;
1263 td->mem_type = def_thread.mem_type;
1264 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
1269 static void put_job(struct thread_data *td)
1271 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1275 static int add_job(struct thread_data *td, const char *filename, int prioclass,
1278 if (td == &def_thread)
1281 strcpy(td->file_name, filename);
1282 sem_init(&td->mutex, 1, 0);
1283 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1285 td->clat_stat.min_val = ULONG_MAX;
1286 td->slat_stat.min_val = ULONG_MAX;
1287 td->bw_stat.min_val = ULONG_MAX;
1289 run_str[td->thread_number - 1] = 'P';
1291 if (td->use_aio && !td->aio_depth)
1298 setup_log(&td->lat_log);
1300 setup_log(&td->bw_log);
1302 if (td->min_bs == -1)
1303 td->min_bs = td->bs;
1304 if (td->max_bs == -1)
1305 td->max_bs = td->bs;
1307 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);
1311 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1317 for (i = 0; i < sizeof(int) * 8; i++) {
1319 CPU_SET(i, &cpumask);
1323 static void fill_option(const char *input, char *output)
1328 while (input[i] != ',' && input[i] != '}' && input[i] != '\0') {
1329 output[i] = input[i];
1336 unsigned long get_mult(char c)
1347 return 1024 * 1024 * 1024;
1354 * convert string after '=' into decimal value, noting any size suffix
1356 static int str_cnv(char *p, unsigned long long *val)
1361 str = strstr(p, "=");
1368 *val = strtoul(str, NULL, 10);
1369 if (*val == ULONG_MAX && errno == ERANGE)
1372 *val *= get_mult(str[len - 2]);
1384 static void parse_jobs_cmd(int argc, char *argv[], int index)
1386 struct thread_data *td;
1387 unsigned int prio, prioclass, cpu;
1388 char *string, *filename, *p, *c;
1391 string = malloc(256);
1392 filename = malloc(256);
1394 for (i = index; i < argc; i++) {
1397 c = strpbrk(p, "{");
1403 td = get_new_job(0);
1410 c = strstr(p, "rw=");
1414 td->ddir = DDIR_READ;
1416 td->ddir = DDIR_WRITE;
1419 c = strstr(p, "prio=");
1425 c = strstr(p, "prioclass=");
1428 prioclass = *c - '0';
1431 c = strstr(p, "file=");
1434 fill_option(c, filename);
1437 c = strstr(p, "direct=");
1446 c = strstr(p, "sync=");
1455 c = strstr(p, "delay=");
1458 fill_option(c, string);
1459 td->delay_sleep = strtoul(string, NULL, 10);
1462 c = strstr(p, "rate=");
1465 fill_option(c, string);
1466 td->rate = strtoul(string, NULL, 10);
1469 c = strstr(p, "ratemin=");
1472 fill_option(c, string);
1473 td->ratemin = strtoul(string, NULL, 10);
1476 c = strstr(p, "ratecycle=");
1479 fill_option(c, string);
1480 td->ratecycle = strtoul(string, NULL, 10);
1483 c = strstr(p, "cpumask=");
1486 fill_option(c, string);
1487 cpu = strtoul(string, NULL, 10);
1488 fill_cpu_mask(td->cpumask, cpu);
1491 c = strstr(p, "fsync=");
1494 fill_option(c, string);
1495 td->fsync_blocks = strtoul(string, NULL, 10);
1498 c = strstr(p, "startdelay=");
1501 fill_option(c, string);
1502 td->start_delay = strtoul(string, NULL, 10);
1505 c = strstr(p, "timeout=");
1508 fill_option(c, string);
1509 td->timeout = strtoul(string, NULL, 10);
1512 c = strstr(p, "invalidate=");
1516 td->invalidate_cache = 1;
1518 td->invalidate_cache = 0;
1521 c = strstr(p, "bs=");
1523 unsigned long long bs;
1530 c = strstr(p, "size=");
1533 str_cnv(c, &td->file_size);
1536 c = strstr(p, "offset=");
1539 str_cnv(c, &td->file_offset);
1542 c = strstr(p, "aio_depth=");
1545 fill_option(c, string);
1546 td->aio_depth = strtoul(string, NULL, 10);
1549 c = strstr(p, "mem=");
1552 if (!strncmp(c, "malloc", 6))
1553 td->mem_type = MEM_MALLOC;
1554 else if (!strncmp(c, "shm", 3))
1555 td->mem_type = MEM_SHM;
1557 printf("bad mem type %s\n", c);
1560 c = strstr(p, "aio");
1564 c = strstr(p, "create");
1566 td->create_file = 1;
1568 c = strstr(p, "overwrite");
1572 c = strstr(p, "random");
1575 c = strstr(p, "sequential");
1579 if (add_job(td, filename, prioclass, prio))
1587 static int check_strcnv(char *p, char *name, unsigned long long *val)
1589 if (!strstr(p, name))
1592 return str_cnv(p, val);
1595 static int check_str(char *p, char *name, char *option)
1597 char *s = strstr(p, name);
1603 if (strstr(s, option))
1609 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
1614 sprintf(str, "%s=%%lu%%c-%%lu%%c", name);
1615 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1621 sprintf(str, "%s = %%lu%%c-%%lu%%c", name);
1622 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1628 sprintf(str, "%s=%%lu-%%lu", name);
1629 if (sscanf(p, str, s, e) == 2)
1632 sprintf(str, "%s = %%lu-%%lu", name);
1633 if (sscanf(p, str, s, e) == 2)
1640 static int check_int(char *p, char *name, unsigned int *val)
1644 sprintf(str, "%s=%%d", name);
1645 if (sscanf(p, str, val) == 1)
1648 sprintf(str, "%s = %%d", name);
1649 if (sscanf(p, str, val) == 1)
1655 static int is_empty_or_comment(char *line)
1659 for (i = 0; i < strlen(line); i++) {
1662 if (!isspace(line[i]) && !iscntrl(line[i]))
1669 static int parse_jobs_ini(char *file)
1671 unsigned int prioclass, prio, cpu, global;
1672 unsigned long long ull;
1673 unsigned long ul1, ul2;
1674 struct thread_data *td;
1675 char *string, *name;
1680 f = fopen(file, "r");
1686 string = malloc(4096);
1689 while ((p = fgets(string, 4096, f)) != NULL) {
1690 if (is_empty_or_comment(p))
1692 if (sscanf(p, "[%s]", name) != 1)
1695 global = !strncmp(name, "global", 6);
1697 name[strlen(name) - 1] = '\0';
1699 td = get_new_job(global);
1707 while ((p = fgets(string, 4096, f)) != NULL) {
1708 if (is_empty_or_comment(p))
1712 if (!check_int(p, "rw", &td->ddir)) {
1716 if (!check_int(p, "prio", &prio)) {
1720 if (!check_int(p, "prioclass", &prioclass)) {
1724 if (!check_int(p, "direct", &td->odirect)) {
1728 if (!check_int(p, "rate", &td->rate)) {
1732 if (!check_int(p, "ratemin", &td->ratemin)) {
1736 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1740 if (!check_int(p, "delay", &td->delay_sleep)) {
1744 if (!check_int(p, "cpumask", &cpu)) {
1745 fill_cpu_mask(td->cpumask, cpu);
1749 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1753 if (!check_int(p, "startdelay", &td->start_delay)) {
1757 if (!check_int(p, "timeout", &td->timeout)) {
1761 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1765 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1769 if (!check_int(p, "sync", &td->sync_io)) {
1773 if (!check_range(p, "bsrange", &ul1, &ul2)) {
1779 if (!check_strcnv(p, "bs", &ull)) {
1784 if (!check_strcnv(p, "size", &td->file_size)) {
1788 if (!check_strcnv(p, "offset", &td->file_offset)) {
1792 if (!check_str(p, "mem", "malloc")) {
1793 td->mem_type = MEM_MALLOC;
1797 if (!check_str(p, "mem", "shm")) {
1798 td->mem_type = MEM_SHM;
1802 if (!strncmp(p, "sequential", 10)) {
1807 if (!strncmp(p, "random", 6)) {
1812 if (!strncmp(p, "aio", 3)) {
1817 if (!strncmp(p, "create", 6)) {
1818 td->create_file = 1;
1822 if (!strncmp(p, "overwrite", 9)) {
1827 printf("Client%d: bad option %s\n",td->thread_number,p);
1831 if (add_job(td, name, prioclass, prio))
1841 static int parse_options(int argc, char *argv[])
1845 for (i = 1; i < argc; i++) {
1846 char *parm = argv[i];
1855 def_thread.sequential = !!atoi(parm);
1859 def_thread.bs = atoi(parm);
1860 def_thread.bs <<= 10;
1861 if (!def_thread.bs) {
1862 printf("bad block size\n");
1863 def_thread.bs = DEF_BS;
1868 def_thread.timeout = atoi(parm);
1872 repeatable = !!atoi(parm);
1876 rate_quit = !!atoi(parm);
1880 def_thread.odirect = !!atoi(parm);
1883 if (i + 1 >= argc) {
1884 printf("-f needs file as arg\n");
1887 ini_file = strdup(argv[i+1]);
1897 printf("bad option %s\n", argv[i]);
1905 static void print_thread_status(struct thread_data *td, int nr_running,
1906 int t_rate, int m_rate)
1908 printf("Threads now running: %d", nr_running);
1909 if (m_rate || t_rate)
1910 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1911 printf(" : [%s]\r", run_str);
1915 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1920 * reap exited threads (TD_EXITED -> TD_REAPED)
1922 for (i = 0; i < thread_number; i++) {
1923 struct thread_data *td = &threads[i];
1925 if (td->runstate != TD_EXITED)
1928 td->runstate = TD_REAPED;
1929 run_str[td->thread_number - 1] = '_';
1930 waitpid(td->pid, NULL, 0);
1932 (*m_rate) -= td->ratemin;
1933 (*t_rate) -= td->rate;
1938 print_thread_status(td, *nr_running, *t_rate, *m_rate);
1942 static void run_threads(char *argv[])
1944 struct timeval genesis;
1945 struct thread_data *td;
1946 unsigned long spent;
1947 int i, todo, nr_running, m_rate, t_rate;
1949 gettimeofday(&genesis, NULL);
1951 printf("Starting %d threads\n", thread_number);
1954 signal(SIGINT, sig_handler);
1956 todo = thread_number;
1958 m_rate = t_rate = 0;
1962 * create threads (TD_NOT_CREATED -> TD_CREATED)
1964 for (i = 0; i < thread_number; i++) {
1967 if (td->runstate != TD_NOT_CREATED)
1971 * never got a chance to start, killed by other
1972 * thread for some reason
1974 if (td->terminate) {
1979 if (td->start_delay) {
1980 spent = mtime_since_now(&genesis);
1982 if (td->start_delay * 1000 > spent)
1987 * do file setup here so it happens sequentially,
1988 * we don't want X number of threads getting their
1989 * client data interspersed on disk
1991 if (setup_file(td)) {
1996 td->runstate = TD_CREATED;
1997 run_str[td->thread_number - 1] = 'C';
1998 sem_init(&startup_sem, 1, 1);
2002 sem_wait(&startup_sem);
2004 thread_main(shm_id, i, argv);
2010 * start created threads (TD_CREATED -> TD_STARTED)
2012 for (i = 0; i < thread_number; i++) {
2013 struct thread_data *td = &threads[i];
2015 if (td->runstate != TD_CREATED)
2018 td->runstate = TD_STARTED;
2019 run_str[td->thread_number - 1] = '+';
2021 m_rate += td->ratemin;
2023 sem_post(&td->mutex);
2025 print_thread_status(td, nr_running, t_rate, m_rate);
2028 reap_threads(&nr_running, &t_rate, &m_rate);
2034 while (nr_running) {
2035 reap_threads(&nr_running, &t_rate, &m_rate);
2040 int setup_thread_area(void)
2043 * 1024 is too much on some machines, scale max_jobs if
2044 * we get a failure that looks like too large a shm segment
2047 int s = max_jobs * sizeof(struct thread_data);
2049 shm_id = shmget(0, s, IPC_CREAT | 0600);
2052 if (errno != EINVAL) {
2063 threads = shmat(shm_id, NULL, 0);
2064 if (threads == (void *) -1) {
2073 int main(int argc, char *argv[])
2075 static unsigned long max_run[2], min_run[2];
2076 static unsigned long max_bw[2], min_bw[2];
2077 static unsigned long read_mb, write_mb, read_agg, write_agg;
2080 if (setup_thread_area())
2083 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
2084 perror("sched_getaffinity");
2091 def_thread.ddir = DDIR_READ;
2092 def_thread.bs = DEF_BS;
2093 def_thread.min_bs = -1;
2094 def_thread.max_bs = -1;
2095 def_thread.odirect = DEF_ODIRECT;
2096 def_thread.ratecycle = DEF_RATE_CYCLE;
2097 def_thread.sequential = DEF_SEQUENTIAL;
2098 def_thread.timeout = DEF_TIMEOUT;
2099 def_thread.create_file = DEF_CREATE;
2100 def_thread.overwrite = DEF_OVERWRITE;
2101 def_thread.invalidate_cache = DEF_INVALIDATE;
2102 def_thread.sync_io = DEF_SYNCIO;
2103 def_thread.mem_type = MEM_MALLOC;
2105 i = parse_options(argc, argv);
2108 if (parse_jobs_ini(ini_file))
2111 parse_jobs_cmd(argc, argv, i);
2113 if (!thread_number) {
2114 printf("Nothing to do\n");
2120 min_bw[0] = min_run[0] = ~0UL;
2121 min_bw[1] = min_run[1] = ~0UL;
2122 for (i = 0; i < thread_number; i++) {
2123 struct thread_data *td = &threads[i];
2124 unsigned long bw = 0;
2129 if (td->runtime < min_run[td->ddir])
2130 min_run[td->ddir] = td->runtime;
2131 if (td->runtime > max_run[td->ddir])
2132 max_run[td->ddir] = td->runtime;
2135 bw = td->io_kb * 1024 / td->runtime;
2136 if (bw < min_bw[td->ddir])
2137 min_bw[td->ddir] = bw;
2138 if (bw > max_bw[td->ddir])
2139 max_bw[td->ddir] = bw;
2142 read_mb += td->io_kb >> 10;
2144 read_agg += td->io_kb * 1024 / td->runtime;
2146 write_mb += td->io_kb >> 10;
2148 write_agg += td->io_kb * 1024 / td->runtime;
2152 show_thread_status(td);
2155 printf("\nRun status:\n");
2156 if (max_run[DDIR_READ])
2157 printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", read_mb, read_agg, min_bw[0], max_bw[0], min_run[0], max_run[0]);
2158 if (max_run[DDIR_WRITE])
2159 printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", write_mb, write_agg, min_bw[1], max_bw[1], min_run[1], max_run[1]);