Align io units to processor cache line size
authorJens Axboe <jens.axboe@oracle.com>
Wed, 29 Apr 2009 07:48:04 +0000 (09:48 +0200)
committerJens Axboe <jens.axboe@oracle.com>
Wed, 29 Apr 2009 07:48:04 +0000 (09:48 +0200)
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
fio.c
os/os-linux.h
os/os.h

diff --git a/fio.c b/fio.c
index ad2282d7516fe7fc0033b3bc721a12b16d370fe1..1aba82ded2aed00396f9b76e9926000de8347f54 100644 (file)
--- 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);
 
index 4460653fae44d0e8182239ac69105dbda2dd49c2..1cd8272d5241750c67107536b2a0a1268236e7c0 100644 (file)
@@ -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 5a3bc559186a2c076635a91c6cf85c72a9b80211..dbf095711c08344e054966512dae3f41d8c75280 100644 (file)
--- 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