From: Jens Axboe Date: Thu, 25 Mar 2010 22:03:18 +0000 (+0100) Subject: First step at speeding up io_u rand refill X-Git-Tag: fio-1.39-rc1~20 X-Git-Url: https://git.kernel.dk/?p=fio.git;a=commitdiff_plain;h=1fbbf72e16c27a6fda636db3891a41cd37dc6666;hp=fdba6ac393728b3f743013a6517a987fda79bd1a First step at speeding up io_u rand refill Makes it 3x faster here. Signed-off-by: Jens Axboe --- diff --git a/Makefile b/Makefile index 12042f4a..837f4211 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,8 @@ OBJS = gettime.o fio.o ioengines.o init.o stat.o log.o time.o filesetup.o \ rbtree.o diskutil.o fifo.o blktrace.o smalloc.o filehash.o helpers.o \ cgroup.o profile.o debug.o +OBJS += lib/rand.o + OBJS += crc/crc7.o OBJS += crc/crc16.o OBJS += crc/crc32.o diff --git a/Makefile.FreeBSD b/Makefile.FreeBSD index deae03d8..3c59a994 100644 --- a/Makefile.FreeBSD +++ b/Makefile.FreeBSD @@ -8,6 +8,8 @@ OBJS = gettime.o fio.o ioengines.o init.o stat.o log.o time.o filesetup.o \ eta.o verify.o memory.o io_u.o parse.o mutex.o options.o \ rbtree.o smalloc.o filehash.o helpers.o profile.o debug.o +OBJS += lib/rand.o + OBJS += crc/crc7.o OBJS += crc/crc16.o OBJS += crc/crc32.o diff --git a/Makefile.mac b/Makefile.mac index a726546b..fe84297f 100644 --- a/Makefile.mac +++ b/Makefile.mac @@ -8,6 +8,8 @@ OBJS = gettime.o fio.o ioengines.o init.o stat.o log.o time.o filesetup.o \ eta.o verify.o memory.o io_u.o parse.o mutex.o options.o \ rbtree.o smalloc.o filehash.o helpers.o profile.o debug.o +OBJS += lib/rand.o + OBJS += crc/crc7.o OBJS += crc/crc16.o OBJS += crc/crc32.o diff --git a/Makefile.solaris b/Makefile.solaris index 0ec6d730..49a4ef92 100644 --- a/Makefile.solaris +++ b/Makefile.solaris @@ -7,6 +7,8 @@ OBJS = gettime.o fio.o ioengines.o init.o stat.o log.o time.o filesetup.o \ rbtree.o fifo.o smalloc.o filehash.o lib/strsep.o helpers.o solaris.o \ profile.o debug.o +OBJS += lib/rand.o + OBJS += crc/crc7.o OBJS += crc/crc16.o OBJS += crc/crc32.o diff --git a/io_u.c b/io_u.c index afc90de0..4d3116b7 100644 --- a/io_u.c +++ b/io_u.c @@ -8,6 +8,7 @@ #include "fio.h" #include "hash.h" #include "verify.h" +#include "lib/rand.h" struct io_completion_data { int nr; /* input */ @@ -1217,7 +1218,7 @@ void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u, if (!td->o.zero_buffers) { while ((void *) ptr - io_u->buf < max_bs) { - *ptr = rand() * GOLDEN_RATIO_PRIME; + *ptr = __rand(&__fio_rand_state); ptr++; } } else diff --git a/lib/rand.c b/lib/rand.c new file mode 100644 index 00000000..b8d8f78d --- /dev/null +++ b/lib/rand.c @@ -0,0 +1,24 @@ +#include "rand.h" + +struct frand_state __fio_rand_state; + +static inline int __seed(unsigned int x, unsigned int m) +{ + return (x < m) ? x + m : x; +} + +void init_rand(struct frand_state *state) +{ +#define LCG(x) ((x) * 69069) /* super-duper LCG */ + + state->s1 = __seed(LCG((2^31) + (2^17) + (2^7)), 1); + state->s2 = __seed(LCG(state->s1), 7); + state->s3 = __seed(LCG(state->s2), 15); + + __rand(state); + __rand(state); + __rand(state); + __rand(state); + __rand(state); + __rand(state); +} diff --git a/lib/rand.h b/lib/rand.h new file mode 100644 index 00000000..363e7b6d --- /dev/null +++ b/lib/rand.h @@ -0,0 +1,23 @@ +#ifndef FIO_RAND_H +#define FIO_RAND_H + +struct frand_state { + unsigned int s1, s2, s3; +}; + +extern struct frand_state __fio_rand_state; + +static inline unsigned int __rand(struct frand_state *state) +{ +#define TAUSWORTHE(s,a,b,c,d) ((s&c)<>b) + + state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12); + state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4); + state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17); + + return (state->s1 ^ state->s2 ^ state->s3); +} + +extern void init_rand(struct frand_state *); + +#endif