From eb7ccf38bf2e9208b593d021c50e9ad2ec0781ea Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Wed, 29 Apr 2009 09:48:04 +0200 Subject: [PATCH] Align io units to processor cache line size Signed-off-by: Jens Axboe --- fio.c | 14 ++++++++++++-- os/os-linux.h | 20 ++++++++++++++++++++ os/os.h | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/fio.c b/fio.c index ad2282d7..1aba82de 100644 --- a/fio.c +++ b/fio.c @@ -733,7 +733,7 @@ static int init_io_u(struct thread_data *td) { struct io_u *io_u; unsigned int max_bs; - int i, max_units; + int cl_align, i, max_units; char *p; max_units = td->o.iodepth; @@ -761,10 +761,20 @@ static int init_io_u(struct thread_data *td) else p = td->orig_buffer; + cl_align = os_cache_line_size(); + for (i = 0; i < max_units; i++) { + void *ptr; + if (td->terminate) return 1; - io_u = malloc(sizeof(*io_u)); + + if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) { + log_err("fio: posix_memalign=%s\n", strerror(errno)); + break; + } + + io_u = ptr; memset(io_u, 0, sizeof(*io_u)); INIT_FLIST_HEAD(&io_u->list); diff --git a/os/os-linux.h b/os/os-linux.h index 4460653f..1cd8272d 100644 --- a/os/os-linux.h +++ b/os/os-linux.h @@ -30,6 +30,7 @@ #define FIO_HAVE_FALLOCATE #define FIO_HAVE_POSIXAIO_FSYNC #define FIO_HAVE_PSHARED_MUTEX +#define FIO_HAVE_CL_SIZE #define OS_MAP_ANON MAP_ANONYMOUS @@ -249,4 +250,23 @@ static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev) #define FIO_O_NOATIME 0 #endif +#define CACHE_LINE_FILE \ + "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size" + +static inline int arch_cache_line_size(void) +{ + char size[32]; + int fd, ret; + + fd = open(CACHE_LINE_FILE, O_RDONLY); + if (fd < 0) + return -1; + + ret = read(fd, size, sizeof(size)); + if (ret <= 0) + return -1; + + return atoi(size); +} + #endif diff --git a/os/os.h b/os/os.h index 5a3bc559..dbf09571 100644 --- a/os/os.h +++ b/os/os.h @@ -90,4 +90,20 @@ static inline int load_blktrace(struct thread_data *td, const char *fname) } #endif +#define FIO_DEF_CL_SIZE 128 + +static inline int os_cache_line_size(void) +{ +#ifdef FIO_HAVE_CL_SIZE + int ret = arch_cache_line_size(); + + if (ret <= 0) + return FIO_DEF_CL_SIZE; + + return ret; +#else + return FIO_DEF_CL_SIZE; +#endif +} + #endif -- 2.25.1