From 63191f9a10de2b9075bc80a892c764117fbf63de Mon Sep 17 00:00:00 2001 From: Patrick McFarland Date: Thu, 14 Feb 2013 16:08:07 -0500 Subject: [PATCH] Add cacheline routines --- configure | 2 +- configure.ac | 2 +- src/libmowgli/Makefile | 2 +- src/libmowgli/platform/Makefile | 12 +++++- src/libmowgli/platform/cacheline.c | 61 ++++++++++++++++++++++++++++++ src/libmowgli/platform/cacheline.h | 28 ++++++++++++++ 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 src/libmowgli/platform/cacheline.c create mode 100644 src/libmowgli/platform/cacheline.h diff --git a/configure b/configure index ba7c0b8..3d5c63a 100755 --- a/configure +++ b/configure @@ -3928,7 +3928,7 @@ fi -LIBMOWGLI_MODULES="core base container dns eventloop ext linebuf module object thread vio" +LIBMOWGLI_MODULES="core base container dns eventloop ext linebuf module object platform thread vio" LIBMOWGLI_MODULE_BUILD="$(echo && echo x)" diff --git a/configure.ac b/configure.ac index 5a293d1..9b3bc63 100644 --- a/configure.ac +++ b/configure.ac @@ -43,7 +43,7 @@ fi AC_PATH_PROG(AR, ar) AC_PATH_PROG(RANLIB, ranlib) -LIBMOWGLI_MODULES="core base container dns eventloop ext linebuf module object thread vio" +LIBMOWGLI_MODULES="core base container dns eventloop ext linebuf module object platform thread vio" AC_SUBST(LIBMOWGLI_MODULES) LIBMOWGLI_MODULE_BUILD="$(echo && echo x)" diff --git a/src/libmowgli/Makefile b/src/libmowgli/Makefile index d404a35..b81552a 100644 --- a/src/libmowgli/Makefile +++ b/src/libmowgli/Makefile @@ -6,7 +6,7 @@ LIB_MINOR = 0 SHARED_LIB = ${LIBMOWGLI_SHARED_LIB} STATIC_LIB = ${LIBMOWGLI_STATIC_LIB} -SUBDIRS = ${LIBMOWGLI_MODULES} platform +SUBDIRS = ${LIBMOWGLI_MODULES} INCLUDES = mowgli.h diff --git a/src/libmowgli/platform/Makefile b/src/libmowgli/platform/Makefile index 6b6d7b9..b7851c9 100644 --- a/src/libmowgli/platform/Makefile +++ b/src/libmowgli/platform/Makefile @@ -1,9 +1,17 @@ +include ../../../extra.mk + +STATIC_PIC_LIB_NOINST = ${LIBMOWGLI_SHARED_PLATFORM} +STATIC_LIB_NOINST = ${LIBMOWGLI_STATIC_PLATFORM} + SUBDIRS = win32 -INCLUDES = constructor.h machine.h +SRCS = cacheline.c + +INCLUDES = cacheline.h constructor.h machine.h include ../../../buildsys.mk includesubdir = $(PACKAGE_NAME)/platform -include ../../../extra.mk +CPPFLAGS += -I. -I.. -I../../.. -DMOWGLI_CORE + diff --git a/src/libmowgli/platform/cacheline.c b/src/libmowgli/platform/cacheline.c new file mode 100644 index 0000000..b641fe5 --- /dev/null +++ b/src/libmowgli/platform/cacheline.c @@ -0,0 +1,61 @@ +/* + * libmowgli: A collection of useful routines for programming. + * cacheline.c: Platform specific routines to get L1 cache line size + * + * Copyright (c) 2013 Patrick McFarland + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mowgli.h" + +size_t _mowgli_cacheline_size; + +void mowgli_cacheline_init(void) { +#ifdef MOWGLI_OS_LINUX + _mowgli_cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); +#elif MOWGLI_OS_OSX + sysctlbyname("hw.cachelinesize", &_mowgli_cacheline_size, + sizeof(size_t), 0, 0); +#elif MOWGLI_OS_WIN +#define SLPI SYSTEM_LOGICAL_PROCESSOR_INFORMATION + DWORD buf_size = 0; + DWORD i = 0; + SLPI *buf = 0; + + GetLogicalProcessorInformation(0, &buf_size); + buf = (SLPI *)atheme_alloc(buf_size); + GetLogicalProcessorInformation(&buf[0], &buf_size); + + for (i = 0; i != buf_size / sizeof(SLPI); ++i) { + if (buf[i].Relationship == RelationCache && buf[i].Cache.Level == 1) { + _mowgli_cacheline_size = buf[i].Cache.LineSize; + break; + } + } + + atheme_free(buffer); +#undef SLPI +#else + // This is often true +#ifdef MOWGLI_CPU_BITS_32 + _mowgli_cacheline_size = 32; +#else + _mowgli_cacheline_size = 64; +#endif +#endif +} diff --git a/src/libmowgli/platform/cacheline.h b/src/libmowgli/platform/cacheline.h new file mode 100644 index 0000000..565999d --- /dev/null +++ b/src/libmowgli/platform/cacheline.h @@ -0,0 +1,28 @@ +/* + * libmowgli: A collection of useful routines for programming. + * cacheline.h: Platform specific routines to get L1 cache line size + * + * Copyright (c) 2013 Patrick McFarland + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +extern size_t _mowgli_cacheline_size; + +static inline size_t mowgli_cacheline_size(void) { + return _mowgli_cacheline_size; +}