From 5470e50ffb422d72e2fe18cbcc4aa5014c4c05fe Mon Sep 17 00:00:00 2001 From: rakshasa Date: Fri, 18 Dec 2009 21:56:14 +0000 Subject: [PATCH] * Added autoconf support for cacheline_aligned and SMP_CACHE_BYTES. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1115 e378c898-3ddf-0310-93e7-cc216c733640 --- configure.ac | 3 +++ rak/allocators.h | 19 ++++++++----------- scripts/common.m4 | 20 ++++++++++++++++++++ src/core/poll_manager_select.cc | 5 ++--- src/core/poll_manager_select.h | 2 +- src/thread_base.cc | 4 ++-- src/thread_main.h | 2 +- src/thread_worker.h | 2 +- 8 files changed, 38 insertions(+), 19 deletions(-) diff --git a/configure.ac b/configure.ac index 3e51887a..b8f95fef 100644 --- a/configure.ac +++ b/configure.ac @@ -45,6 +45,9 @@ AC_LANG_POP(C++) AC_DEFINE(HAVE_CONFIG_H, 1, true if config.h was included) AC_DEFINE(USER_AGENT, [std::string(PACKAGE "/" VERSION "/") + torrent::version()], Http user agent) +AC_CHECK_FUNC(posix_memalign) +TORRENT_CHECK_CACHELINE() + CC_ATTRIBUTE_UNUSED( AC_DEFINE([__UNUSED], [__attribute__((unused))], [Wrapper around unused attribute]), AC_DEFINE([__UNUSED], [], [Null-wrapper if unused attribute is unsupported]) diff --git a/rak/allocators.h b/rak/allocators.h index bc1c48c7..7d943615 100644 --- a/rak/allocators.h +++ b/rak/allocators.h @@ -39,20 +39,21 @@ #ifndef RAK_ALLOCATORS_H #define RAK_ALLOCATORS_H +#include #include #include +#include namespace rak { template class cacheline_allocator { public: - typedef std::allocator base_type; - typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; + typedef const void* const_void_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; @@ -72,10 +73,11 @@ public: size_type max_size () const throw() { return std::numeric_limits::max() / sizeof(T); } - pointer allocate(size_type num, std::allocator::const_pointer hint = 0) { + pointer allocate(size_type num, const_void_pointer hint = 0) { return alloc_size(num*sizeof(T)); } + + static pointer alloc_size(size_type size) { pointer ptr = NULL; - posix_memalign((void**)&ptr, L1_CACHE_BYTES, num*sizeof(T)); - + posix_memalign((void**)&ptr, LT_SMP_CACHE_BYTES, size); return ptr; } @@ -102,11 +104,6 @@ bool operator!= (const cacheline_allocator&, const cacheline_allocator&) // template -void* operator new(size_t s, rak::cacheline_allocator a) { - typename rak::cacheline_allocator::pointer ptr = NULL; - posix_memalign((void**)&ptr, L1_CACHE_BYTES, s); - - return ptr; -} +void* operator new(size_t s, rak::cacheline_allocator a) { return a.alloc_size(s); } #endif // namespace rak diff --git a/scripts/common.m4 b/scripts/common.m4 index 81cfe920..5dc40194 100644 --- a/scripts/common.m4 +++ b/scripts/common.m4 @@ -181,6 +181,26 @@ AC_DEFUN([TORRENT_CHECK_MADVISE], [ ]) ]) +AC_DEFUN([TORRENT_CHECK_CACHELINE], [ + AC_MSG_CHECKING(for cacheline) + + AC_COMPILE_IFELSE( + [[#include + #include + void* vptr __cacheline_aligned; + void f() { posix_memalign(&vptr, SMP_CACHE_BYTES, 42); } + ]], + [ + AC_MSG_RESULT(found builtin) + AC_DEFINE(LT_SMP_CACHE_BYTES, SMP_CACHE_BYTES, Largest L1 cache size we know of, should work on all archs.) + AC_DEFINE(lt_cacheline_aligned, __cacheline_aligned, LibTorrent defined cacheline aligned.) + ], [ + AC_MSG_RESULT(using default 128 bytes) + AC_DEFINE(LT_SMP_CACHE_BYTES, 128, Largest L1 cache size we know of, should work on all archs.) + AC_DEFINE(lt_cacheline_aligned, __attribute__((__aligned__(LT_SMP_CACHE_BYTES))), LibTorrent defined cacheline aligned.) + ]) +]) + AC_DEFUN([TORRENT_CHECK_EXECINFO], [ AC_MSG_CHECKING(for execinfo.h) diff --git a/src/core/poll_manager_select.cc b/src/core/poll_manager_select.cc index 47a148b2..cc658e64 100644 --- a/src/core/poll_manager_select.cc +++ b/src/core/poll_manager_select.cc @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -53,9 +54,7 @@ PollManagerSelect::PollManagerSelect(torrent::Poll* p) : PollManager(p) { #if defined USE_VARIABLE_FDSET m_setSize = (m_poll->open_max() + 7) / 8; - char* buffer; - posix_memalign((void**)&buffer, L1_CACHE_BYTES, 3 * m_setSize); - + char* buffer = rak::cacheline_allocator::alloc_size(3 * m_setSize); std::memset(buffer, 0, 3 * m_setSize); m_readSet = (fd_set*)buffer; diff --git a/src/core/poll_manager_select.h b/src/core/poll_manager_select.h index be75c51e..e0d0e4fc 100644 --- a/src/core/poll_manager_select.h +++ b/src/core/poll_manager_select.h @@ -45,7 +45,7 @@ namespace torrent { namespace core { -class __cacheline_aligned PollManagerSelect : public PollManager { +class lt_cacheline_aligned PollManagerSelect : public PollManager { public: static PollManagerSelect* create(int maxOpenSockets); ~PollManagerSelect(); diff --git a/src/thread_base.cc b/src/thread_base.cc index ebdce394..520dedb2 100644 --- a/src/thread_base.cc +++ b/src/thread_base.cc @@ -47,9 +47,9 @@ // Temporarly injected into config.h. /* temp hack */ -//#define __cacheline_aligned __attribute__((__aligned__(128))) +//#define lt_cacheline_aligned __attribute__((__aligned__(128))) -class __cacheline_aligned thread_queue_hack { +class lt_cacheline_aligned thread_queue_hack { public: typedef ThreadBase::thread_base_func value_type; typedef ThreadBase::thread_base_func* iterator; diff --git a/src/thread_main.h b/src/thread_main.h index 00312a29..cfd488f3 100644 --- a/src/thread_main.h +++ b/src/thread_main.h @@ -42,7 +42,7 @@ // Check if cacheline aligned with inheritance ends up taking two // cachelines. -class __cacheline_aligned ThreadMain : public ThreadBase { +class lt_cacheline_aligned ThreadMain : public ThreadBase { public: ThreadMain() {} ~ThreadMain(); diff --git a/src/thread_worker.h b/src/thread_worker.h index 33d4de07..c04d5157 100644 --- a/src/thread_worker.h +++ b/src/thread_worker.h @@ -44,7 +44,7 @@ // Check if cacheline aligned with inheritance ends up taking two // cachelines. -class __cacheline_aligned ThreadWorker : public ThreadBase { +class lt_cacheline_aligned ThreadWorker : public ThreadBase { public: ThreadWorker(); ~ThreadWorker();