Skip to content

Commit

Permalink
Add cacheline routines
Browse files Browse the repository at this point in the history
  • Loading branch information
Diablo-D3 committed Feb 14, 2013
1 parent c1b38e0 commit 63191f9
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
2 changes: 1 addition & 1 deletion src/libmowgli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions src/libmowgli/platform/Makefile
Original file line number Diff line number Diff line change
@@ -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

61 changes: 61 additions & 0 deletions src/libmowgli/platform/cacheline.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* 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
}
28 changes: 28 additions & 0 deletions src/libmowgli/platform/cacheline.h
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* 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;
}

0 comments on commit 63191f9

Please sign in to comment.