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)
130 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
132 static int repeatable = DEF_RAND_REPEAT;
133 static int rate_quit = 1;
134 static int write_lat_log;
135 static int write_bw_log;
137 static int thread_number;
138 static char *ini_file;
140 static int max_jobs = MAX_JOBS;
142 static char run_str[MAX_JOBS + 1];
172 struct timeval start_time;
173 struct timeval issue_time;
177 unsigned long long offset;
179 struct list_head list;
184 unsigned long val_sq;
185 unsigned long max_val;
186 unsigned long min_val;
187 unsigned long samples;
196 unsigned long nr_samples;
197 unsigned long max_samples;
198 struct io_sample *log;
201 #define td_read(td) ((td)->ddir == DDIR_READ)
202 #define should_fsync(td) (!td_read(td) && !(td)->odirect)
211 volatile int terminate;
212 volatile int runstate;
215 unsigned int sequential;
219 unsigned int odirect;
220 unsigned int delay_sleep;
221 unsigned int fsync_blocks;
222 unsigned int start_delay;
223 unsigned int timeout;
224 unsigned int use_aio;
225 unsigned int create_file;
226 unsigned int overwrite;
227 unsigned int invalidate_cache;
228 unsigned long long file_size;
229 unsigned long long file_offset;
230 unsigned int sync_io;
231 unsigned int mem_type;
238 io_context_t aio_ctx;
239 unsigned int aio_depth;
240 struct io_event *aio_events;
242 unsigned int cur_depth;
243 struct list_head io_u_freelist;
244 struct list_head io_u_busylist;
247 unsigned int ratemin;
248 unsigned int ratecycle;
249 unsigned long rate_usec_cycle;
250 long rate_pending_usleep;
251 unsigned long rate_kb;
252 struct timeval lastrate;
254 unsigned long runtime; /* sec */
255 unsigned long blocks;
256 unsigned long io_blocks;
258 unsigned long last_block;
260 struct drand48_data random_state;
263 * bandwidth and latency stats
265 struct io_stat clat_stat; /* completion latency */
266 struct io_stat slat_stat; /* submission latency */
268 struct io_stat bw_stat; /* bandwidth stats */
269 unsigned long stat_io_kb;
270 struct timeval stat_sample_time;
272 struct io_log *lat_log;
273 struct io_log *bw_log;
275 struct timeval start;
278 static struct thread_data *threads;
279 static struct thread_data def_thread;
281 static sem_t startup_sem;
283 static void sig_handler(int sig)
287 for (i = 0; i < thread_number; i++) {
288 struct thread_data *td = &threads[i];
295 static int init_random_state(struct thread_data *td)
297 unsigned long seed = 123;
303 int fd = open("/dev/random", O_RDONLY);
310 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
319 srand48_r(seed, &td->random_state);
323 static unsigned long utime_since(struct timeval *s, struct timeval *e)
327 sec = e->tv_sec - s->tv_sec;
328 usec = e->tv_usec - s->tv_usec;
329 if (sec > 0 && usec < 0) {
334 sec *= (double) 1000000;
339 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
343 sec = e->tv_sec - s->tv_sec;
344 usec = e->tv_usec - s->tv_usec;
345 if (sec > 0 && usec < 0) {
350 sec *= (double) 1000;
351 usec /= (double) 1000;
356 static unsigned long mtime_since_now(struct timeval *s)
360 gettimeofday(&t, NULL);
361 return mtime_since(s, &t);
364 static inline unsigned long msec_now(struct timeval *s)
366 return s->tv_sec * 1000 + s->tv_usec / 1000;
369 static unsigned long long get_next_offset(struct thread_data *td)
371 unsigned long long b;
374 if (!td->sequential) {
375 lrand48_r(&td->random_state, &r);
376 b = (1+(double) (td->blocks-1) * r / (RAND_MAX+1.0));
382 return b * td->bs + td->file_offset;
385 static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
388 if (val > is->max_val)
390 if (val < is->min_val)
394 is->val_sq += val * val;
398 static void add_log_sample(struct thread_data *td, struct io_log *log,
401 if (log->nr_samples == log->max_samples) {
402 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
404 log->log = realloc(log->log, new_size);
405 log->max_samples <<= 1;
408 log->log[log->nr_samples].val = val;
409 log->log[log->nr_samples].time = mtime_since_now(&td->start);
413 static void add_clat_sample(struct thread_data *td, unsigned long msec)
415 add_stat_sample(td, &td->clat_stat, msec);
418 add_log_sample(td, td->lat_log, msec);
421 static void add_slat_sample(struct thread_data *td, unsigned long msec)
423 add_stat_sample(td, &td->slat_stat, msec);
426 static void add_bw_sample(struct thread_data *td, unsigned long msec)
428 unsigned long spent = mtime_since_now(&td->stat_sample_time);
434 rate = (td->io_kb - td->stat_io_kb) / spent;
435 add_stat_sample(td, &td->bw_stat, rate);
438 add_log_sample(td, td->bw_log, rate);
440 gettimeofday(&td->stat_sample_time, NULL);
441 td->stat_io_kb = td->io_kb;
444 static void usec_sleep(int usec)
446 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
450 rem.tv_sec = rem.tv_nsec = 0;
451 nanosleep(&req, &rem);
455 req.tv_nsec = rem.tv_nsec;
459 static void rate_throttle(struct thread_data *td, unsigned long time_spent)
464 if (time_spent < td->rate_usec_cycle) {
465 unsigned long s = td->rate_usec_cycle - time_spent;
467 td->rate_pending_usleep += s;
468 if (td->rate_pending_usleep >= 100000) {
469 usec_sleep(td->rate_pending_usleep);
470 td->rate_pending_usleep = 0;
473 long overtime = time_spent - td->rate_usec_cycle;
475 td->rate_pending_usleep -= overtime;
479 static int check_min_rate(struct thread_data *td, struct timeval *now)
485 * allow a 2 second settle period in the beginning
487 if (mtime_since(&td->start, now) < 2000)
491 * if rate blocks is set, sample is running
494 spent = mtime_since(&td->lastrate, now);
495 if (spent < td->ratecycle)
498 rate = (td->io_kb - td->rate_kb) / spent;
499 if (rate < td->ratemin) {
500 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
507 td->rate_kb = td->io_kb;
508 memcpy(&td->lastrate, now, sizeof(*now));
512 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
514 if (mtime_since(&td->start, t) >= td->timeout * 1000)
520 static void put_io_u(struct thread_data *td, struct io_u *io_u)
522 list_del(&io_u->list);
523 list_add(&io_u->list, &td->io_u_freelist);
527 static struct io_u *get_io_u(struct thread_data *td)
531 if (list_empty(&td->io_u_freelist))
534 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
535 list_del(&io_u->list);
536 list_add(&io_u->list, &td->io_u_busylist);
538 io_u->offset = get_next_offset(td);
542 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
544 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
547 gettimeofday(&io_u->start_time, NULL);
552 static void do_sync_io(struct thread_data *td)
554 unsigned long blocks, msec, usec;
559 for (blocks = 0; blocks < td->blocks; blocks++) {
568 if (td->cur_off != io_u->offset) {
569 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
576 usec_sleep(td->delay_sleep);
579 ret = read(td->fd, io_u->buf, io_u->buflen);
581 ret = write(td->fd, io_u->buf, io_u->buflen);
583 if (ret < (int) io_u->buflen) {
590 td->io_kb += io_u->buflen >> 10;
591 td->cur_off = io_u->offset + io_u->buflen;
593 if (should_fsync(td) && td->fsync_blocks &&
594 (td->io_blocks % td->fsync_blocks) == 0)
597 gettimeofday(&e, NULL);
599 usec = utime_since(&io_u->start_time, &e);
601 rate_throttle(td, usec);
603 if (check_min_rate(td, &e)) {
609 add_clat_sample(td, msec);
610 add_bw_sample(td, msec);
612 if (runtime_exceeded(td, &e))
618 if (should_fsync(td))
622 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
624 struct iocb *iocb = &io_u->iocb;
628 ret = io_submit(td->aio_ctx, 1, &iocb);
631 else if (ret == EAGAIN)
633 else if (ret == EINTR)
642 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
643 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
645 static void ios_completed(struct thread_data *td, int nr)
652 gettimeofday(&e, NULL);
654 for (i = 0; i < nr; i++) {
655 io_u = ev_to_iou(td->aio_events + i);
658 td->io_kb += io_u->buflen >> 10;
660 msec = mtime_since(&io_u->issue_time, &e);
662 add_clat_sample(td, msec);
663 add_bw_sample(td, msec);
669 static void cleanup_pending_aio(struct thread_data *td)
671 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
672 struct list_head *entry, *n;
677 * get immediately available events, if any
679 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
681 ios_completed(td, r);
684 * now cancel remaining active events
686 list_for_each_safe(entry, n, &td->io_u_busylist) {
687 io_u = list_entry(entry, struct io_u, list);
689 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
695 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
697 ios_completed(td, r);
701 static void do_async_io(struct thread_data *td)
704 unsigned long blocks, usec;
706 for (blocks = 0; blocks < td->blocks; blocks++) {
707 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
708 struct timespec *timeout;
709 int ret, min_evts = 0;
716 usec_sleep(td->delay_sleep);
720 memcpy(&s, &io_u->start_time, sizeof(s));
722 ret = io_u_queue(td, io_u);
729 gettimeofday(&io_u->issue_time, NULL);
730 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
732 if (td->cur_depth < td->aio_depth) {
740 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
747 ios_completed(td, ret);
749 if (should_fsync(td) && td->fsync_blocks &&
750 (td->io_blocks % td->fsync_blocks) == 0)
754 * the rate is batched for now, it should work for batches
755 * of completions except the very first one which may look
758 gettimeofday(&e, NULL);
759 usec = utime_since(&s, &e);
761 rate_throttle(td, usec);
763 if (check_min_rate(td, &e)) {
768 if (runtime_exceeded(td, &e))
773 cleanup_pending_aio(td);
775 if (should_fsync(td))
779 static void cleanup_aio(struct thread_data *td)
781 io_destroy(td->aio_ctx);
784 free(td->aio_events);
787 static int init_aio(struct thread_data *td)
789 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
794 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
798 static void cleanup_io_u(struct thread_data *td)
800 struct list_head *entry, *n;
803 list_for_each_safe(entry, n, &td->io_u_freelist) {
804 io_u = list_entry(entry, struct io_u, list);
806 list_del(&io_u->list);
810 if (td->mem_type == MEM_MALLOC)
811 free(td->orig_buffer);
812 else if (td->mem_type == MEM_SHM) {
813 struct shmid_ds sbuf;
815 shmdt(td->orig_buffer);
816 shmctl(td->shm_id, IPC_RMID, &sbuf);
820 static int init_io_u(struct thread_data *td)
823 int i, max_units, mem_size;
829 max_units = td->aio_depth;
831 mem_size = td->bs * max_units + MASK;
833 if (td->mem_type == MEM_MALLOC)
834 td->orig_buffer = malloc(mem_size);
835 else if (td->mem_type == MEM_SHM) {
836 td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
837 if (td->shm_id < 0) {
843 td->orig_buffer = shmat(td->shm_id, NULL, 0);
844 if (td->orig_buffer == (void *) -1) {
851 INIT_LIST_HEAD(&td->io_u_freelist);
852 INIT_LIST_HEAD(&td->io_u_busylist);
854 p = ALIGN(td->orig_buffer);
855 for (i = 0; i < max_units; i++) {
856 io_u = malloc(sizeof(*io_u));
857 memset(io_u, 0, sizeof(*io_u));
858 INIT_LIST_HEAD(&io_u->list);
860 io_u->buf = p + td->bs * i;
861 io_u->buflen = td->bs;
863 list_add(&io_u->list, &td->io_u_freelist);
869 static void setup_log(struct io_log **log)
871 struct io_log *l = malloc(sizeof(*l));
874 l->max_samples = 1024;
875 l->log = malloc(l->max_samples * sizeof(struct io_sample));
879 static void finish_log(struct thread_data *td, struct io_log *log, char *name)
885 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
886 f = fopen(file_name, "w");
892 for (i = 0; i < log->nr_samples; i++)
893 fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val);
900 static int create_file(struct thread_data *td)
906 * unless specifically asked for overwrite, let normal io extend it
908 if (!td_read(td) && !td->overwrite)
911 if (!td->file_size) {
912 fprintf(stderr, "Need size for create\n");
917 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
923 td->blocks = td->file_size / td->bs;
925 memset(b, 0, td->bs);
927 for (i = 0; i < td->blocks; i++) {
928 int r = write(td->fd, b, td->bs);
949 static int file_exists(struct thread_data *td)
953 if (stat(td->file_name, &st) != -1)
956 return errno != ENOENT;
959 static int setup_file(struct thread_data *td)
964 if (!file_exists(td)) {
965 if (!td->create_file) {
977 td->fd = open(td->file_name, flags | O_RDONLY);
984 td->fd = open(td->file_name, flags | O_WRONLY | O_CREAT, 0600);
993 if (fstat(td->fd, &st) == -1) {
998 if (td->file_size > st.st_size)
999 st.st_size = td->file_size;
1002 td->file_size = 1024 * 1024 * 1024;
1004 st.st_size = td->file_size;
1007 td->blocks = (st.st_size - td->file_offset) / td->bs;
1009 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1014 if (td->invalidate_cache) {
1015 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
1024 static void *thread_main(int shm_id, int offset, char *argv[])
1026 struct thread_data *td;
1032 data = shmat(shm_id, NULL, 0);
1033 if (data == (void *) -1) {
1038 td = data + offset * sizeof(struct thread_data);
1044 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1049 sprintf(argv[0], "fio%d", offset);
1051 if (td->use_aio && init_aio(td))
1054 if (init_random_state(td))
1058 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1067 sem_post(&startup_sem);
1068 sem_wait(&td->mutex);
1070 gettimeofday(&td->start, NULL);
1073 memcpy(&td->lastrate, &td->start, sizeof(td->start));
1075 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1082 td->runtime = mtime_since_now(&td->start);
1086 finish_log(td, td->bw_log, "bw");
1088 finish_log(td, td->lat_log, "lat");
1099 sem_post(&startup_sem);
1100 sem_wait(&td->mutex);
1102 td->runstate = TD_EXITED;
1107 static void free_shm(void)
1109 struct shmid_ds sbuf;
1114 shmctl(shm_id, IPC_RMID, &sbuf);
1118 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1119 double *mean, double *dev)
1123 if (is->samples == 0)
1129 n = (double) is->samples;
1130 *mean = (double) is->val / n;
1131 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1135 static void show_thread_status(struct thread_data *td)
1137 int prio, prio_class;
1138 unsigned long min, max, bw = 0;
1141 if (!td->io_kb && !td->error)
1145 bw = td->io_kb / td->runtime;
1147 prio = td->ioprio & 0xff;
1148 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1150 printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/s\n", td->thread_number, td->error, td->io_kb >> 10, bw);
1152 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1153 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1154 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1155 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1156 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1157 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1160 static int setup_rate(struct thread_data *td)
1162 int nr_reads_per_sec;
1167 if (td->rate < td->ratemin) {
1168 fprintf(stderr, "min rate larger than nominal rate\n");
1172 nr_reads_per_sec = td->rate * 1024 / td->bs;
1173 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1174 td->rate_pending_usleep = 0;
1178 static struct thread_data *get_new_job(int global)
1180 struct thread_data *td;
1184 if (thread_number >= max_jobs)
1187 td = &threads[thread_number++];
1188 memset(td, 0, sizeof(*td));
1191 td->thread_number = thread_number;
1193 td->ddir = def_thread.ddir;
1194 td->ioprio = def_thread.ioprio;
1195 td->sequential = def_thread.sequential;
1196 td->bs = def_thread.bs;
1197 td->min_bs = def_thread.min_bs;
1198 td->max_bs = def_thread.max_bs;
1199 td->odirect = def_thread.odirect;
1200 td->delay_sleep = def_thread.delay_sleep;
1201 td->fsync_blocks = def_thread.fsync_blocks;
1202 td->start_delay = def_thread.start_delay;
1203 td->timeout = def_thread.timeout;
1204 td->use_aio = def_thread.use_aio;
1205 td->create_file = def_thread.create_file;
1206 td->overwrite = def_thread.overwrite;
1207 td->invalidate_cache = def_thread.invalidate_cache;
1208 td->file_size = def_thread.file_size;
1209 td->file_offset = def_thread.file_offset;
1210 td->rate = def_thread.rate;
1211 td->ratemin = def_thread.ratemin;
1212 td->ratecycle = def_thread.ratecycle;
1213 td->aio_depth = def_thread.aio_depth;
1214 td->sync_io = def_thread.sync_io;
1215 td->mem_type = def_thread.mem_type;
1216 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
1221 static void put_job(struct thread_data *td)
1223 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1227 static int add_job(struct thread_data *td, const char *filename, int prioclass,
1230 if (td == &def_thread)
1233 strcpy(td->file_name, filename);
1234 sem_init(&td->mutex, 1, 0);
1235 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1237 td->clat_stat.min_val = ULONG_MAX;
1238 td->slat_stat.min_val = ULONG_MAX;
1239 td->bw_stat.min_val = ULONG_MAX;
1241 run_str[td->thread_number - 1] = 'P';
1243 if (td->use_aio && !td->aio_depth)
1250 setup_log(&td->lat_log);
1252 setup_log(&td->bw_log);
1254 printf("Client%d: file=%s, rw=%d, prio=%d/%d, seq=%d, odir=%d, bs=%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, prioclass, prio, td->sequential, td->odirect, td->bs, td->rate, td->use_aio, td->aio_depth);
1258 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1264 for (i = 0; i < sizeof(int) * 8; i++) {
1266 CPU_SET(i, &cpumask);
1270 static void fill_option(const char *input, char *output)
1275 while (input[i] != ',' && input[i] != '}' && input[i] != '\0') {
1276 output[i] = input[i];
1284 * convert string after '=' into decimal value, noting any size suffix
1286 static int str_cnv(char *p, unsigned long long *val)
1292 str = strstr(p, "=");
1300 switch (str[len - 2]) {
1311 mult = 1024 * 1024 * 1024;
1315 *val = strtoul(str, NULL, 10);
1316 if (*val == ULONG_MAX && errno == ERANGE)
1332 static void parse_jobs_cmd(int argc, char *argv[], int index)
1334 struct thread_data *td;
1335 unsigned int prio, prioclass, cpu;
1336 char *string, *filename, *p, *c;
1339 string = malloc(256);
1340 filename = malloc(256);
1342 for (i = index; i < argc; i++) {
1345 c = strpbrk(p, "{");
1351 td = get_new_job(0);
1358 c = strstr(p, "rw=");
1362 td->ddir = DDIR_READ;
1364 td->ddir = DDIR_WRITE;
1367 c = strstr(p, "prio=");
1373 c = strstr(p, "prioclass=");
1376 prioclass = *c - '0';
1379 c = strstr(p, "file=");
1382 fill_option(c, filename);
1385 c = strstr(p, "direct=");
1394 c = strstr(p, "sync=");
1403 c = strstr(p, "delay=");
1406 fill_option(c, string);
1407 td->delay_sleep = strtoul(string, NULL, 10);
1410 c = strstr(p, "rate=");
1413 fill_option(c, string);
1414 td->rate = strtoul(string, NULL, 10);
1417 c = strstr(p, "ratemin=");
1420 fill_option(c, string);
1421 td->ratemin = strtoul(string, NULL, 10);
1424 c = strstr(p, "ratecycle=");
1427 fill_option(c, string);
1428 td->ratecycle = strtoul(string, NULL, 10);
1431 c = strstr(p, "cpumask=");
1434 fill_option(c, string);
1435 cpu = strtoul(string, NULL, 10);
1436 fill_cpu_mask(td->cpumask, cpu);
1439 c = strstr(p, "fsync=");
1442 fill_option(c, string);
1443 td->fsync_blocks = strtoul(string, NULL, 10);
1446 c = strstr(p, "startdelay=");
1449 fill_option(c, string);
1450 td->start_delay = strtoul(string, NULL, 10);
1453 c = strstr(p, "timeout=");
1456 fill_option(c, string);
1457 td->timeout = strtoul(string, NULL, 10);
1460 c = strstr(p, "invalidate=");
1464 td->invalidate_cache = 1;
1466 td->invalidate_cache = 0;
1469 c = strstr(p, "bs=");
1471 unsigned long long bs;
1478 c = strstr(p, "size=");
1481 str_cnv(c, &td->file_size);
1484 c = strstr(p, "offset=");
1487 str_cnv(c, &td->file_offset);
1490 c = strstr(p, "aio_depth=");
1493 fill_option(c, string);
1494 td->aio_depth = strtoul(string, NULL, 10);
1497 c = strstr(p, "mem=");
1500 if (!strncmp(c, "malloc", 6))
1501 td->mem_type = MEM_MALLOC;
1502 else if (!strncmp(c, "shm", 3))
1503 td->mem_type = MEM_SHM;
1505 printf("bad mem type %s\n", c);
1508 c = strstr(p, "aio");
1512 c = strstr(p, "create");
1514 td->create_file = 1;
1516 c = strstr(p, "overwrite");
1520 c = strstr(p, "random");
1523 c = strstr(p, "sequential");
1527 if (add_job(td, filename, prioclass, prio))
1535 static int check_strcnv(char *p, char *name, unsigned long long *val)
1537 if (!strstr(p, name))
1540 return str_cnv(p, val);
1543 static int check_str(char *p, char *name, char *option)
1545 char *s = strstr(p, name);
1551 if (strstr(s, option))
1557 static int check_int(char *p, char *name, unsigned int *val)
1561 sprintf(str, "%s=%%d", name);
1562 if (sscanf(p, str, val) == 1)
1565 sprintf(str, "%s = %%d", name);
1566 if (sscanf(p, str, val) == 1)
1572 static int is_empty_or_comment(char *line)
1576 for (i = 0; i < strlen(line); i++) {
1579 if (!isspace(line[i]) && !iscntrl(line[i]))
1586 static int parse_jobs_ini(char *file)
1588 unsigned int prioclass, prio, cpu, global;
1589 unsigned long long ull;
1590 struct thread_data *td;
1591 char *string, *name;
1596 f = fopen(file, "r");
1602 string = malloc(4096);
1605 while ((p = fgets(string, 4096, f)) != NULL) {
1606 if (is_empty_or_comment(p))
1608 if (sscanf(p, "[%s]", name) != 1)
1611 global = !strncmp(name, "global", 6);
1613 name[strlen(name) - 1] = '\0';
1615 td = get_new_job(global);
1623 while ((p = fgets(string, 4096, f)) != NULL) {
1624 if (is_empty_or_comment(p))
1628 if (!check_int(p, "rw", &td->ddir)) {
1632 if (!check_int(p, "prio", &prio)) {
1636 if (!check_int(p, "prioclass", &prioclass)) {
1640 if (!check_int(p, "direct", &td->odirect)) {
1644 if (!check_int(p, "rate", &td->rate)) {
1648 if (!check_int(p, "ratemin", &td->ratemin)) {
1652 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1656 if (!check_int(p, "delay", &td->delay_sleep)) {
1660 if (!check_int(p, "cpumask", &cpu)) {
1661 fill_cpu_mask(td->cpumask, cpu);
1665 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1669 if (!check_int(p, "startdelay", &td->start_delay)) {
1673 if (!check_int(p, "timeout", &td->timeout)) {
1677 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1681 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1685 if (!check_int(p, "sync", &td->sync_io)) {
1689 if (!check_strcnv(p, "bs", &ull)) {
1694 if (!check_strcnv(p, "size", &td->file_size)) {
1698 if (!check_strcnv(p, "offset", &td->file_offset)) {
1702 if (!check_str(p, "mem", "malloc")) {
1703 td->mem_type = MEM_MALLOC;
1707 if (!check_str(p, "mem", "shm")) {
1708 td->mem_type = MEM_SHM;
1712 if (!strncmp(p, "sequential", 10)) {
1717 if (!strncmp(p, "random", 6)) {
1722 if (!strncmp(p, "aio", 3)) {
1727 if (!strncmp(p, "create", 6)) {
1728 td->create_file = 1;
1732 if (!strncmp(p, "overwrite", 9)) {
1737 printf("Client%d: bad option %s\n",td->thread_number,p);
1741 if (add_job(td, name, prioclass, prio))
1751 static int parse_options(int argc, char *argv[])
1755 for (i = 1; i < argc; i++) {
1756 char *parm = argv[i];
1765 def_thread.sequential = !!atoi(parm);
1769 def_thread.bs = atoi(parm);
1770 def_thread.bs <<= 10;
1771 if (!def_thread.bs) {
1772 printf("bad block size\n");
1773 def_thread.bs = DEF_BS;
1778 def_thread.timeout = atoi(parm);
1782 repeatable = !!atoi(parm);
1786 rate_quit = !!atoi(parm);
1790 def_thread.odirect = !!atoi(parm);
1793 if (i + 1 >= argc) {
1794 printf("-f needs file as arg\n");
1797 ini_file = strdup(argv[i+1]);
1807 printf("bad option %s\n", argv[i]);
1815 static void print_thread_status(struct thread_data *td, int nr_running,
1816 int t_rate, int m_rate)
1818 printf("Threads now running: %d", nr_running);
1819 if (m_rate || t_rate)
1820 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1821 printf(" : [%s]\r", run_str);
1825 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1830 * reap exited threads (TD_EXITED -> TD_REAPED)
1832 for (i = 0; i < thread_number; i++) {
1833 struct thread_data *td = &threads[i];
1835 if (td->runstate != TD_EXITED)
1838 td->runstate = TD_REAPED;
1839 run_str[td->thread_number - 1] = '_';
1840 waitpid(td->pid, NULL, 0);
1842 (*m_rate) -= td->ratemin;
1843 (*t_rate) -= td->rate;
1848 print_thread_status(td, *nr_running, *t_rate, *m_rate);
1852 static void run_threads(char *argv[])
1854 struct timeval genesis;
1855 struct thread_data *td;
1856 unsigned long spent;
1857 int i, todo, nr_running, m_rate, t_rate;
1859 gettimeofday(&genesis, NULL);
1861 printf("Starting %d threads\n", thread_number);
1864 signal(SIGINT, sig_handler);
1866 todo = thread_number;
1868 m_rate = t_rate = 0;
1872 * create threads (TD_NOT_CREATED -> TD_CREATED)
1874 for (i = 0; i < thread_number; i++) {
1877 if (td->runstate != TD_NOT_CREATED)
1881 * never got a chance to start, killed by other
1882 * thread for some reason
1884 if (td->terminate) {
1889 if (td->start_delay) {
1890 spent = mtime_since_now(&genesis);
1892 if (td->start_delay * 1000 > spent)
1896 td->runstate = TD_CREATED;
1897 run_str[td->thread_number - 1] = 'C';
1898 sem_init(&startup_sem, 1, 1);
1902 sem_wait(&startup_sem);
1904 thread_main(shm_id, i, argv);
1910 * start created threads (TD_CREATED -> TD_STARTED)
1912 for (i = 0; i < thread_number; i++) {
1913 struct thread_data *td = &threads[i];
1915 if (td->runstate != TD_CREATED)
1918 td->runstate = TD_STARTED;
1919 run_str[td->thread_number - 1] = '+';
1921 m_rate += td->ratemin;
1923 sem_post(&td->mutex);
1925 print_thread_status(td, nr_running, t_rate, m_rate);
1928 reap_threads(&nr_running, &t_rate, &m_rate);
1934 while (nr_running) {
1935 reap_threads(&nr_running, &t_rate, &m_rate);
1940 int setup_thread_area(void)
1943 * 1024 is too much on some machines, scale max_jobs if
1944 * we get a failure that looks like too large a shm segment
1947 int s = max_jobs * sizeof(struct thread_data);
1949 shm_id = shmget(0, s, IPC_CREAT | 0600);
1952 if (errno != EINVAL) {
1963 threads = shmat(shm_id, NULL, 0);
1964 if (threads == (void *) -1) {
1973 int main(int argc, char *argv[])
1975 static unsigned long max_run[2], min_run[2];
1976 static unsigned long max_bw[2], min_bw[2];
1977 static unsigned long read_mb, write_mb, read_agg, write_agg;
1980 if (setup_thread_area())
1983 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
1984 perror("sched_getaffinity");
1991 def_thread.ddir = DDIR_READ;
1992 def_thread.bs = DEF_BS;
1993 def_thread.odirect = DEF_ODIRECT;
1994 def_thread.ratecycle = DEF_RATE_CYCLE;
1995 def_thread.sequential = DEF_SEQUENTIAL;
1996 def_thread.timeout = DEF_TIMEOUT;
1997 def_thread.create_file = DEF_CREATE;
1998 def_thread.overwrite = DEF_OVERWRITE;
1999 def_thread.invalidate_cache = DEF_INVALIDATE;
2000 def_thread.sync_io = DEF_SYNCIO;
2001 def_thread.mem_type = MEM_MALLOC;
2003 i = parse_options(argc, argv);
2006 if (parse_jobs_ini(ini_file))
2009 parse_jobs_cmd(argc, argv, i);
2011 if (!thread_number) {
2012 printf("Nothing to do\n");
2018 min_bw[0] = min_run[0] = ~0UL;
2019 min_bw[1] = min_run[1] = ~0UL;
2020 for (i = 0; i < thread_number; i++) {
2021 struct thread_data *td = &threads[i];
2022 unsigned long bw = 0;
2027 if (td->runtime < min_run[td->ddir])
2028 min_run[td->ddir] = td->runtime;
2029 if (td->runtime > max_run[td->ddir])
2030 max_run[td->ddir] = td->runtime;
2033 bw = td->io_kb / td->runtime;
2034 if (bw < min_bw[td->ddir])
2035 min_bw[td->ddir] = bw;
2036 if (bw > max_bw[td->ddir])
2037 max_bw[td->ddir] = bw;
2040 read_mb += td->io_kb >> 10;
2042 read_agg += td->io_kb / td->runtime;
2044 write_mb += td->io_kb >> 10;
2046 write_agg += td->io_kb / td->runtime;
2050 show_thread_status(td);
2053 printf("\nRun status:\n");
2054 if (max_run[DDIR_READ])
2055 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]);
2056 if (max_run[DDIR_WRITE])
2057 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]);