From: khizmax Date: Fri, 16 Sep 2016 21:32:04 +0000 (+0300) Subject: FIxed aux node operations in SplitListSet X-Git-Tag: v2.2.0~129 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=libcds.git;a=commitdiff_plain;h=3c50e4dac322a846e3623788d442006859cf3287 FIxed aux node operations in SplitListSet Added stress tests for FreeList --- diff --git a/cds/intrusive/details/base.h b/cds/intrusive/details/base.h index 03673dc1..4e2b156a 100644 --- a/cds/intrusive/details/base.h +++ b/cds/intrusive/details/base.h @@ -320,6 +320,9 @@ namespace intrusive { /** @defgroup cds_intrusive_list List @ingroup cds_intrusive_containers */ + /** @defgroup cds_intrusive_freelist Free-list + @ingroup cds_intrusive_containers + */ //@cond template diff --git a/cds/intrusive/details/split_list_base.h b/cds/intrusive/details/split_list_base.h index fa36827e..a10a50f1 100644 --- a/cds/intrusive/details/split_list_base.h +++ b/cds/intrusive/details/split_list_base.h @@ -306,25 +306,21 @@ namespace cds { namespace intrusive { //@endcond public: - typedef GC gc; ///< Garbage collector - typedef Node node_type; ///< Bucket node type - typedef atomics::atomic table_entry; ///< Table entry type + typedef GC gc; ///< Garbage collector + typedef Node node_type; ///< Bucket node type typedef typename options::allocator allocator; ///< allocator + typedef typename options::memory_model memory_model; ///< Memory model for atomic operations + typedef typename options::free_list free_list; ///< Free-list - /// Bucket table allocator - typedef cds::details::Allocator< table_entry, allocator > bucket_table_allocator; - - /// Memory model for atomic operations - typedef typename options::memory_model memory_model; + /// Auxiliary node type + struct aux_node_type: public node_type, public free_list::node + {}; - /// Free-list - typedef typename options::free_list free_list; + typedef atomics::atomic table_entry; ///< Table entry type + typedef cds::details::Allocator< table_entry, allocator > bucket_table_allocator; ///< Bucket table allocator protected: //@cond - struct aux_node_type: public node_type, public free_list::node - {}; - const size_t m_nLoadFactor; ///< load factor (average count of items per bucket) const size_t m_nCapacity; ///< Bucket table capacity table_entry * m_Table; ///< Bucket table @@ -383,14 +379,14 @@ namespace cds { namespace intrusive { } /// Returns head node of bucket \p nBucket - node_type * bucket( size_t nBucket ) const + aux_node_type * bucket( size_t nBucket ) const { assert( nBucket < capacity() ); return m_Table[ nBucket ].load(memory_model::memory_order_acquire); } /// Set \p pNode as a head of bucket \p nBucket - void bucket( size_t nBucket, node_type * pNode ) + void bucket( size_t nBucket, aux_node_type * pNode ) { assert( nBucket < capacity() ); assert( bucket( nBucket ) == nullptr ); @@ -399,7 +395,7 @@ namespace cds { namespace intrusive { } /// Allocates auxiliary node; can return \p nullptr if the table exhausted - node_type* alloc_aux_node() + aux_node_type* alloc_aux_node() { if ( m_nAuxNodeAllocated.load( memory_model::memory_order_relaxed ) < capacity() ) { // alloc next free node from m_auxNode @@ -418,7 +414,7 @@ namespace cds { namespace intrusive { } /// Places node type to free-list - void free_aux_node( node_type* p ) + void free_aux_node( aux_node_type* p ) { m_freeList.put( static_cast( p )); } @@ -476,14 +472,15 @@ namespace cds { namespace intrusive { /// Free-list typedef typename options::free_list free_list; - protected: - //@cond - typedef atomics::atomic table_entry; ///< Table entry type - typedef atomics::atomic segment_type; ///< Bucket table segment type - + /// Auxiliary node type class aux_node_type: public node_type, public free_list::node {}; + protected: + //@cond + typedef atomics::atomic table_entry; ///< Table entry type + typedef atomics::atomic segment_type; ///< Bucket table segment type + struct aux_node_segment { atomics::atomic< size_t > aux_node_count; // how many aux nodes allocated from the segment aux_node_segment* next_segment; @@ -568,7 +565,7 @@ namespace cds { namespace intrusive { } /// Returns head node of the bucket \p nBucket - node_type * bucket( size_t nBucket ) const + aux_node_type * bucket( size_t nBucket ) const { size_t nSegment = nBucket >> m_metrics.nSegmentSizeLog2; assert( nSegment < m_metrics.nSegmentCount ); @@ -580,7 +577,7 @@ namespace cds { namespace intrusive { } /// Set \p pNode as a head of bucket \p nBucket - void bucket( size_t nBucket, node_type * pNode ) + void bucket( size_t nBucket, aux_node_type * pNode ) { size_t nSegment = nBucket >> m_metrics.nSegmentSizeLog2; assert( nSegment < m_metrics.nSegmentCount ); @@ -597,7 +594,7 @@ namespace cds { namespace intrusive { } /// Allocates auxiliary node; can return \p nullptr if the table exhausted - node_type* alloc_aux_node() + aux_node_type* alloc_aux_node() { for ( ;; ) { aux_node_segment* aux_segment = m_auxNodeList.load( memory_model::memory_order_relaxed ); @@ -628,7 +625,7 @@ namespace cds { namespace intrusive { } /// Places auxiliary node type to free-list - void free_aux_node( node_type* p ) + void free_aux_node( aux_node_type* p ) { m_freeList.put( static_cast( p )); } diff --git a/cds/intrusive/free_list.h b/cds/intrusive/free_list.h index 113610a9..0ac727b3 100644 --- a/cds/intrusive/free_list.h +++ b/cds/intrusive/free_list.h @@ -36,7 +36,7 @@ namespace cds { namespace intrusive { /// Lock-free free list - /** @ingroup cds_intrusive_helper + /** @ingroup cds_intrusive_freelist Free list is a helper class intended for reusing objects instead of freeing them completely; this avoids the overhead of \p malloc(), and also avoids its worst-case behavior of taking an operating system lock. diff --git a/cds/intrusive/free_list_cached.h b/cds/intrusive/free_list_cached.h new file mode 100644 index 00000000..ef80887b --- /dev/null +++ b/cds/intrusive/free_list_cached.h @@ -0,0 +1,192 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + Source code repo: http://github.com/khizmax/libcds/ + Download: http://sourceforge.net/projects/libcds/files/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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. +*/ + +#ifndef CDSLIB_INTRUSIVE_FREE_LIST_CACHED_H +#define CDSLIB_INTRUSIVE_FREE_LIST_CACHED_H + +#include +#include +#include +#include + +#include +#include + +namespace cds { namespace intrusive { + + /// Cached free list + /** @ingroup cds_intrusive_freelist + + The class that is a wrapper over other \p FreeList contains a small cache of free elements. + Before placing a new item into underlying \p FreeList the cached free-list tryes + to put that item into the cache if its corresponding slot is empty. The slot is calculated by + current thread id: + \code + int slot = std::hash()( std::this_thread::get_id() ) & (CacheSize - 1); + \endcode + + When getting the free-list checks the corresponding cache slot. If it is not empty, its + contents is returned. + + In some cases such simple algorithm significantly reduces \p FreeList contention. + + Template parameters: + - \p FreeList - a free-list implementation: \p FreeList, \p TaggedFreeList + - \p CacheSize - size of cache, a small power-of-two number, default is 16 + - \p Padding - padding of cache elements for solving false sharing, default is \p cds::c_nCacheLineSize + */ + template + class CachedFreeList + { + public: + typedef FreeList free_list_type; ///< Undelying free-list type + typedef typename free_list_type::node node; ///< Free-list node + + static size_t const c_cache_size = CacheSize; ///< Cache size + static unsigned const c_padding = Padding; ///< Cache element padding + + static_assert( c_cache_size >= 4, "Cache size is too small" ); + static_assert( (c_cache_size & (c_cache_size - 1)) == 0, "CacheSize must be power of two" ); + static_assert( (c_padding & (c_padding - 1)) == 0, "Padding must be power-of-two"); + + public: + /// Creates empty free list + CachedFreeList() + { + for ( auto& i: m_cache ) + i.store( nullptr, atomics::memory_order_relaxed ); + } + + /// Destroys the free list. Free-list must be empty. + /** + @warning dtor does not free elements of the list. + To free elements you should manually call \p clear() with an appropriate disposer. + */ + ~CachedFreeList() + { + assert( empty()); + } + + /// Puts \p pNode to the free list + void put( node* pNode ) + { + // try to put into free cell of cache + node* expect = nullptr; + if ( m_cache[ get_hash() ].compare_exchange_weak( expect, pNode, atomics::memory_order_release, atomics::memory_order_relaxed )) + return; + + // cache cell is not empty - use free-list + m_freeList.put( pNode ); + } + + /// Gets a node from the free list. If the list is empty, returns \p nullptr + node * get() + { + // try get from cache + atomics::atomic& cell = m_cache[ get_hash() ]; + node* p = cell.load( atomics::memory_order_relaxed ); + if ( p && cell.compare_exchange_weak( p, nullptr, atomics::memory_order_acquire, atomics::memory_order_relaxed )) + return p; + + // try read from free-list + p = m_freeList.get(); + if ( p ) + return p; + + // iterate the cache + for ( auto& cell : m_cache ) { + p = cell.load( atomics::memory_order_relaxed ); + if ( p && cell.compare_exchange_weak( p, nullptr, atomics::memory_order_acquire, atomics::memory_order_relaxed )) + return p; + } + + return m_freeList.get(); + } + + /// Checks whether the free list is empty + bool empty() const + { + if ( !m_freeList.empty() ) + return false; + + for ( auto& cell : m_cache ) { + node* p = cell.load( atomics::memory_order_relaxed ); + if ( p ) + return false; + } + + return true; + } + + /// Clears the free list (not atomic) + /** + For each element \p disp disposer is called to free memory. + The \p Disposer interface: + \code + struct disposer + { + void operator()( FreeList::node * node ); + }; + \endcode + + This method must be explicitly called before the free list destructor. + */ + template + void clear( Disposer disp ) + { + m_freeList.clear( disp ); + for ( auto& cell : m_cache ) { + node* p = cell.load( atomics::memory_order_relaxed ); + if ( p ) { + disp( p ); + cell.store( nullptr, atomics::memory_order_relaxed ); + } + } + } + + private: + //@cond + size_t get_hash() + { + return std::hash()( std::this_thread::get_id() ) & (c_cache_size - 1); + } + //@endcond + private: + //@cond + typedef typename cds::details::type_padding< atomics::atomic, c_padding >::type array_item; + array_item m_cache[ c_cache_size ]; + free_list_type m_freeList; + //@endcond + }; + +}} // namespace cds::intrusive +//@endcond + +#endif // CDSLIB_INTRUSIVE_FREE_LIST_CACHED_H diff --git a/cds/intrusive/free_list_selector.h b/cds/intrusive/free_list_selector.h index d407b265..25297baf 100644 --- a/cds/intrusive/free_list_selector.h +++ b/cds/intrusive/free_list_selector.h @@ -5,7 +5,7 @@ Source code repo: http://github.com/khizmax/libcds/ Download: http://sourceforge.net/projects/libcds/files/ - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -25,7 +25,7 @@ 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. + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef CDSLIB_INTRUSIVE_FREE_LIST_SELECTOR_H diff --git a/cds/intrusive/free_list_tagged.h b/cds/intrusive/free_list_tagged.h index ab71e8f9..d8b93767 100644 --- a/cds/intrusive/free_list_tagged.h +++ b/cds/intrusive/free_list_tagged.h @@ -36,7 +36,8 @@ namespace cds { namespace intrusive { /// Lock-free free list based on tagged pointers (required double-width CAS) - /** @ingroup cds_intrusive_helper + /** @ingroup cds_intrusive_freelist + This variant of \p FreeList is intended for processor architectures that support double-width CAS. It uses tagged pointer technique to solve ABA problem. diff --git a/cds/intrusive/split_list.h b/cds/intrusive/split_list.h index 00258117..7f3892bb 100644 --- a/cds/intrusive/split_list.h +++ b/cds/intrusive/split_list.h @@ -258,7 +258,6 @@ namespace cds { namespace intrusive { //@cond typedef typename ordered_list::node_type list_node_type; ///< Node type as declared in ordered list typedef split_list::node node_type; ///< split-list node type - typedef node_type aux_node_type; ///< dummy node type /// Split-list node traits /** @@ -271,11 +270,13 @@ namespace cds { namespace intrusive { typedef typename split_list::details::bucket_table_selector< traits::dynamic_bucket_table , gc - , aux_node_type + , node_type , opt::allocator< typename traits::allocator > , opt::memory_model< memory_model > , opt::free_list< typename traits::free_list > >::type bucket_table; + + typedef typename bucket_table::aux_node_type aux_node_type; ///< auxiliary node type //@endcond protected: @@ -287,7 +288,7 @@ namespace cds { namespace intrusive { typedef typename base_class::auxiliary_head bucket_head_type; public: - bool insert_at( aux_node_type * pHead, value_type& val ) + bool insert_at( aux_node_type* pHead, value_type& val ) { assert( pHead != nullptr ); bucket_head_type h(pHead); diff --git a/cds/intrusive/split_list_nogc.h b/cds/intrusive/split_list_nogc.h index f4f146f9..ef8acbe0 100644 --- a/cds/intrusive/split_list_nogc.h +++ b/cds/intrusive/split_list_nogc.h @@ -96,9 +96,9 @@ namespace cds { namespace intrusive { "cds::atomicity::empty_item_counter is not allowed as a item counter"); protected: + //@cond typedef typename ordered_list::node_type list_node_type; ///< Node type as declared in ordered list typedef split_list::node node_type; ///< split-list node type - typedef node_type aux_node_type; ///< dummy node type /// Split-list node traits /** @@ -107,16 +107,17 @@ namespace cds { namespace intrusive { */ typedef split_list::node_traits node_traits; - //@cond /// Bucket table implementation typedef typename split_list::details::bucket_table_selector< traits::dynamic_bucket_table , gc - , aux_node_type + , node_type , opt::allocator< typename traits::allocator > , opt::memory_model< memory_model > >::type bucket_table; + typedef typename bucket_table::aux_node_type aux_node_type; ///< dummy node type + typedef typename ordered_list::iterator list_iterator; typedef typename ordered_list::const_iterator list_const_iterator; //@endcond @@ -704,10 +705,12 @@ namespace cds { namespace intrusive { protected: //@cond - typedef typename cds::details::type_padding< bucket_table, traits::padding >::type padded_bucket_table; + static unsigned const c_padding = cds::opt::actual_padding< traits::padding >::value; + + typedef typename cds::details::type_padding< bucket_table, c_padding >::type padded_bucket_table; padded_bucket_table m_Buckets; ///< bucket table - typedef typename cds::details::type_padding< ordered_list_wrapper, traits::padding>::type padded_ordered_list; + typedef typename cds::details::type_padding< ordered_list_wrapper, c_padding >::type padded_ordered_list; padded_ordered_list m_List; ///< Ordered list containing split-list items atomics::atomic m_nBucketCountLog2; ///< log2( current bucket count ) diff --git a/cds/intrusive/split_list_rcu.h b/cds/intrusive/split_list_rcu.h index 9791aa21..17df965c 100644 --- a/cds/intrusive/split_list_rcu.h +++ b/cds/intrusive/split_list_rcu.h @@ -132,9 +132,9 @@ namespace cds { namespace intrusive { "cds::atomicity::empty_item_counter is not allowed as a item counter"); protected: + //@cond typedef typename ordered_list::node_type list_node_type; ///< Node type as declared in ordered list typedef split_list::node node_type; ///< split-list node type - typedef node_type aux_node_type; ///< dummy node type /// Split-list node traits /** @@ -143,16 +143,17 @@ namespace cds { namespace intrusive { */ typedef split_list::node_traits node_traits; - //@cond /// Bucket table implementation typedef typename split_list::details::bucket_table_selector< traits::dynamic_bucket_table , gc - , aux_node_type + , node_type , opt::allocator< typename traits::allocator > , opt::memory_model< memory_model > >::type bucket_table; + typedef typename bucket_table::aux_node_type aux_node_type; ///< auxiliary node type + //@endcond protected: @@ -1090,10 +1091,12 @@ namespace cds { namespace intrusive { protected: //@cond - typedef typename cds::details::type_padding< bucket_table, traits::padding >::type padded_bucket_table; + static unsigned const c_padding = cds::opt::actual_padding< traits::padding >::value; + + typedef typename cds::details::type_padding< bucket_table, c_padding >::type padded_bucket_table; padded_bucket_table m_Buckets; ///< bucket table - typedef typename cds::details::type_padding< ordered_list_wrapper, traits::padding>::type padded_ordered_list; + typedef typename cds::details::type_padding< ordered_list_wrapper, c_padding >::type padded_ordered_list; padded_ordered_list m_List; ///< Ordered list containing split-list items atomics::atomic m_nBucketCountLog2; ///< log2( current bucket count ) diff --git a/projects/Win/vc14/cds.sln b/projects/Win/vc14/cds.sln index ace405a4..decb0e12 100644 --- a/projects/Win/vc14/cds.sln +++ b/projects/Win/vc14/cds.sln @@ -233,6 +233,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stress-set-iteration", "str {408FE9BC-44F0-4E6A-89FA-D6F952584239} = {408FE9BC-44F0-4E6A-89FA-D6F952584239} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stress-freelist", "stress-freelist.vcxproj", "{79A6845E-85BF-4000-94FF-9DF2473460D4}" + ProjectSection(ProjectDependencies) = postProject + {A34CED07-A442-4FA1-81C4-F8B9CD3C832B} = {A34CED07-A442-4FA1-81C4-F8B9CD3C832B} + {408FE9BC-44F0-4E6A-89FA-D6F952584239} = {408FE9BC-44F0-4E6A-89FA-D6F952584239} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -615,6 +621,18 @@ Global {31952FA8-A303-4A0B-94C4-ABA5A8A6DBCE}.Release|Win32.Build.0 = Release|Win32 {31952FA8-A303-4A0B-94C4-ABA5A8A6DBCE}.Release|x64.ActiveCfg = Release|x64 {31952FA8-A303-4A0B-94C4-ABA5A8A6DBCE}.Release|x64.Build.0 = Release|x64 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.Debug|Win32.ActiveCfg = Debug|Win32 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.Debug|Win32.Build.0 = Debug|Win32 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.Debug|x64.ActiveCfg = Debug|x64 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.Debug|x64.Build.0 = Debug|x64 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.DebugVLD|Win32.ActiveCfg = DebugVLD|Win32 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.DebugVLD|Win32.Build.0 = DebugVLD|Win32 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.DebugVLD|x64.ActiveCfg = DebugVLD|x64 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.DebugVLD|x64.Build.0 = DebugVLD|x64 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.Release|Win32.ActiveCfg = Release|Win32 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.Release|Win32.Build.0 = Release|Win32 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.Release|x64.ActiveCfg = Release|x64 + {79A6845E-85BF-4000-94FF-9DF2473460D4}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -654,6 +672,7 @@ Global {1BB746AC-7856-4E59-9430-51177621DC35} = {7D3EE35B-185D-40B5-88C2-7F9933426978} {24DF3B87-387E-4EFC-BDE0-8DAD279FE19A} = {7D3EE35B-185D-40B5-88C2-7F9933426978} {31952FA8-A303-4A0B-94C4-ABA5A8A6DBCE} = {0D83E8C7-97D1-4BA1-928A-6846E7089652} + {79A6845E-85BF-4000-94FF-9DF2473460D4} = {10E1FAF2-904D-405E-8AB5-6878A1B03346} EndGlobalSection GlobalSection(DPCodeReviewSolutionGUID) = preSolution DPCodeReviewSolutionGUID = {00000000-0000-0000-0000-000000000000} diff --git a/projects/Win/vc14/cds.vcxproj b/projects/Win/vc14/cds.vcxproj index acd0a6c0..5b7e85c6 100644 --- a/projects/Win/vc14/cds.vcxproj +++ b/projects/Win/vc14/cds.vcxproj @@ -532,6 +532,7 @@ + diff --git a/projects/Win/vc14/cds.vcxproj.filters b/projects/Win/vc14/cds.vcxproj.filters index e59bf011..14f3e2ee 100644 --- a/projects/Win/vc14/cds.vcxproj.filters +++ b/projects/Win/vc14/cds.vcxproj.filters @@ -102,10 +102,6 @@ {7226715d-6777-4c01-8e66-83b3885c00c1} - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx - {ae97048d-bd62-4ff2-be28-3c84338e7186} @@ -1286,5 +1282,8 @@ Header Files\cds\intrusive + + Header Files\cds\intrusive + \ No newline at end of file diff --git a/projects/Win/vc14/stress-freelist.vcxproj b/projects/Win/vc14/stress-freelist.vcxproj new file mode 100644 index 00000000..221700de --- /dev/null +++ b/projects/Win/vc14/stress-freelist.vcxproj @@ -0,0 +1,239 @@ + + + + + DebugVLD + Win32 + + + DebugVLD + x64 + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + {79A6845E-85BF-4000-94FF-9DF2473460D4} + Win32Proj + stress_freelist + 8.1 + stress-freelist + + + + Application + true + v140 + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectName)_d + + + true + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectName)_d + + + true + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectName)_d + + + true + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectName)_d + + + false + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)-release\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + + + false + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)-release\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + NotUsing + Level3 + Disabled + _ENABLE_ATOMIC_ALIGNMENT_FIX;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + $(GTEST_LIB32);$(GTEST_ROOT)/lib/x86;$(BOOST_PATH)/stage32/lib;$(BOOST_PATH)/stage/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtestd.lib;stress-framework_d.lib;%(AdditionalDependencies) + + + + + NotUsing + Level3 + Disabled + _ENABLE_ATOMIC_ALIGNMENT_FIX;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + $(GTEST_LIB32);$(GTEST_ROOT)/lib/x86;$(BOOST_PATH)/stage32/lib;$(BOOST_PATH)/stage/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtestd.lib;stress-framework_d.lib;%(AdditionalDependencies) + + + + + NotUsing + Level3 + Disabled + _ENABLE_ATOMIC_ALIGNMENT_FIX;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + $(GTEST_LIB64);$(GTEST_ROOT)/lib/x64;$(BOOST_PATH)/stage64/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtestd.lib;stress-framework_d.lib;%(AdditionalDependencies) + + + + + NotUsing + Level3 + Disabled + _ENABLE_ATOMIC_ALIGNMENT_FIX;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + $(GTEST_LIB64);$(GTEST_ROOT)/lib/x64;$(BOOST_PATH)/stage64/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtestd.lib;stress-framework_d.lib;%(AdditionalDependencies) + + + + + Level3 + NotUsing + MaxSpeed + true + true + _ENABLE_ATOMIC_ALIGNMENT_FIX;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + true + true + $(GTEST_LIB32);$(GTEST_ROOT)/lib/x86;$(BOOST_PATH)/stage32/lib;$(BOOST_PATH)/stage/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtest.lib;stress-framework.lib;%(AdditionalDependencies) + + + + + Level3 + NotUsing + MaxSpeed + true + true + _ENABLE_ATOMIC_ALIGNMENT_FIX;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + true + true + $(GTEST_LIB64);$(GTEST_ROOT)/lib/x64;$(BOOST_PATH)/stage64/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtest.lib;stress-framework.lib;%(AdditionalDependencies) + + + + + + \ No newline at end of file diff --git a/projects/Win/vc14/stress-freelist.vcxproj.filters b/projects/Win/vc14/stress-freelist.vcxproj.filters new file mode 100644 index 00000000..d7e0247e --- /dev/null +++ b/projects/Win/vc14/stress-freelist.vcxproj.filters @@ -0,0 +1,24 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/projects/Win/vc14/stress-stack.vcxproj.filters b/projects/Win/vc14/stress-stack.vcxproj.filters index 9cae1e96..501850f4 100644 --- a/projects/Win/vc14/stress-stack.vcxproj.filters +++ b/projects/Win/vc14/stress-stack.vcxproj.filters @@ -9,10 +9,6 @@ {93995380-89BD-4b04-88EB-625FBE52EBFB} h;hh;hpp;hxx;hm;inl;inc;xsd - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - diff --git a/test/stress/CMakeLists.txt b/test/stress/CMakeLists.txt index 8801787c..7aa703dc 100644 --- a/test/stress/CMakeLists.txt +++ b/test/stress/CMakeLists.txt @@ -21,6 +21,7 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/freelist) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/map) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/pqueue) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/queue) @@ -29,6 +30,7 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/stack) add_custom_target( stress-all DEPENDS + stress-freelist stress-map stress-pqueue stress-queue diff --git a/test/stress/data/test-debug.conf b/test/stress/data/test-debug.conf index c72fb215..961a6504 100644 --- a/test/stress/data/test-debug.conf +++ b/test/stress/data/test-debug.conf @@ -272,3 +272,7 @@ CuckooProbesetThreshold=0 # *** FeldmanHashMap properties FeldmanMapHeadBits=8 FeldmanMapArrayBits=4 + +[free_list] +ThreadCount=4 +PassCount=100000 \ No newline at end of file diff --git a/test/stress/data/test-express.conf b/test/stress/data/test-express.conf index 0590d3f3..1ec6aa14 100644 --- a/test/stress/data/test-express.conf +++ b/test/stress/data/test-express.conf @@ -266,3 +266,7 @@ CuckooProbesetThreshold=0 # *** FeldmanHashMap properties FeldmanMapHeadBits=8 FeldmanMapArrayBits=4 + +[free_list] +ThreadCount=4 +PassCount=1000000 \ No newline at end of file diff --git a/test/stress/data/test.conf b/test/stress/data/test.conf index e6ab618d..565e448b 100644 --- a/test/stress/data/test.conf +++ b/test/stress/data/test.conf @@ -264,3 +264,7 @@ CuckooProbesetThreshold=0 # *** FeldmanHashMap properties FeldmanMapHeadBits=10 FeldmanMapArrayBits=4 + +[free_list] +ThreadCount=4 +PassCount=1000000 \ No newline at end of file diff --git a/test/stress/freelist/CMakeLists.txt b/test/stress/freelist/CMakeLists.txt new file mode 100644 index 00000000..32d549a2 --- /dev/null +++ b/test/stress/freelist/CMakeLists.txt @@ -0,0 +1,22 @@ +set(PACKAGE_NAME stress-freelist) + +set(CDSSTRESS_FREELIST_SOURCES + ../main.cpp + put_get.cpp + put_get_single.cpp +) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_executable(${PACKAGE_NAME} ${CDSSTRESS_FREELIST_SOURCES} $) +target_link_libraries(${PACKAGE_NAME} + ${CDS_SHARED_LIBRARY} + ${GTEST_LIBRARY} + ${Boost_THREAD_LIBRARY} + ${Boost_SYSTEM_LIBRARY} + ${CMAKE_THREAD_LIBS_INIT} +) + +add_test(NAME ${PACKAGE_NAME} COMMAND ${PACKAGE_NAME} WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}) \ No newline at end of file diff --git a/test/stress/freelist/put_get.cpp b/test/stress/freelist/put_get.cpp new file mode 100644 index 00000000..08593617 --- /dev/null +++ b/test/stress/freelist/put_get.cpp @@ -0,0 +1,183 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + Source code repo: http://github.com/khizmax/libcds/ + Download: http://sourceforge.net/projects/libcds/files/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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 + +#include +#include +#include + +namespace { + class put_get: public cds_test::stress_fixture + { + protected: + static size_t s_nThreadCount; + static size_t s_nPassCount; + static size_t const c_nArraySize = 100; + + template + struct value_type: public FreeList::node + { + size_t counter; + + value_type() + : counter(0) + {} + }; + + template + class Worker: public cds_test::thread + { + typedef cds_test::thread base_class; + public: + FreeList& m_FreeList; + size_t m_nSuccess = 0; + + public: + Worker( cds_test::thread_pool& pool, FreeList& s ) + : base_class( pool ) + , m_FreeList( s ) + {} + + Worker( Worker& src ) + : base_class( src ) + , m_FreeList( src.m_FreeList ) + {} + + virtual thread * clone() + { + return new Worker( *this ); + } + + virtual void test() + { + typedef value_type item_type; + item_type* arr[ c_nArraySize ]; + + for ( size_t pass = 0; pass < s_nPassCount; ++pass ) { + size_t n = 0; + item_type* p; + + while ( (p = static_cast( m_FreeList.get())) != nullptr ) { + p->counter++; + arr[n] = p; + ++m_nSuccess; + ++n; + } + + for ( size_t i = 0; i < n; ++i ) + m_FreeList.put( arr[i] ); + } + } + }; + + public: + static void SetUpTestCase() + { + cds_test::config const& cfg = get_config( "free_list" ); + + s_nThreadCount = cfg.get_size_t( "ThreadCount", s_nThreadCount ); + s_nPassCount = cfg.get_size_t( "PassCount", s_nPassCount ); + + if ( s_nThreadCount == 0 ) + s_nThreadCount = 1; + if ( s_nPassCount == 0 ) + s_nPassCount = 1000; + } + //static void TearDownTestCase(); + + protected: + + template + void test( FreeList& list ) + { + cds_test::thread_pool& pool = get_pool(); + + value_type arr[c_nArraySize]; + + for ( auto& i : arr ) + list.put( &i ); + + pool.add( new Worker( pool, list ), s_nThreadCount ); + + propout() << std::make_pair( "work_thread", s_nThreadCount ) + << std::make_pair( "pass_count", s_nPassCount ); + + std::chrono::milliseconds duration = pool.run(); + + propout() << std::make_pair( "duration", duration ); + + // analyze result + size_t nTotal = 0; + for ( auto const& i : arr ) + nTotal += i.counter; + + size_t nSuccess = 0; + for ( size_t threadNo = 0; threadNo < pool.size(); ++threadNo ) + nSuccess += static_cast&>( pool.get( threadNo )).m_nSuccess; + + EXPECT_EQ( nSuccess, nTotal ); + + list.clear( []( typename FreeList::node* ) {} ); + } + }; + + size_t put_get::s_nThreadCount = 4; + size_t put_get::s_nPassCount = 100000; + +#define CDSSTRESS_FREELIST_F( name, freelist_type ) \ + TEST_F( put_get, name ) \ + { \ + freelist_type fl; \ + test( fl ); \ + } + + CDSSTRESS_FREELIST_F( FreeList, cds::intrusive::FreeList ) + + typedef cds::intrusive::CachedFreeList cached_free_list; + CDSSTRESS_FREELIST_F( CachedFreeList, cached_free_list ) + + TEST_F( put_get, TaggetFreeList ) + { + struct tagged_ptr { + void* p; + uintptr_t tag; + }; + + atomics::atomic tp; + if ( tp.is_lock_free() ) { + cds::intrusive::TaggedFreeList fl; + test( fl ); + } + else + std::cout << "Double-width CAS is not supported\n"; + } + +} // namespace diff --git a/test/stress/freelist/put_get_single.cpp b/test/stress/freelist/put_get_single.cpp new file mode 100644 index 00000000..c7d33358 --- /dev/null +++ b/test/stress/freelist/put_get_single.cpp @@ -0,0 +1,163 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + Source code repo: http://github.com/khizmax/libcds/ + Download: http://sourceforge.net/projects/libcds/files/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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 + +#include +#include +#include + +namespace { + class put_get_single: public cds_test::stress_fixture + { + protected: + static size_t s_nThreadCount; + static size_t s_nPassCount; + + template + struct value_type: public FreeList::node + { + size_t counter; + + value_type() + : counter(0) + {} + }; + + template + class Worker: public cds_test::thread + { + typedef cds_test::thread base_class; + public: + FreeList& m_FreeList; + size_t m_nSuccess = 0; + + public: + Worker( cds_test::thread_pool& pool, FreeList& s ) + : base_class( pool ) + , m_FreeList( s ) + {} + + Worker( Worker& src ) + : base_class( src ) + , m_FreeList( src.m_FreeList ) + {} + + virtual thread * clone() + { + return new Worker( *this ); + } + + virtual void test() + { + typedef value_type item_type; + + for ( size_t pass = 0; pass < s_nPassCount; ++pass ) { + item_type* p; + while ( (p = static_cast( m_FreeList.get())) == nullptr ); + p->counter++; + m_FreeList.put( p ); + } + } + }; + + public: + static void SetUpTestCase() + { + cds_test::config const& cfg = get_config( "free_list" ); + + s_nThreadCount = cfg.get_size_t( "ThreadCount", s_nThreadCount ); + s_nPassCount = cfg.get_size_t( "PassCount", s_nPassCount ); + + if ( s_nThreadCount == 0 ) + s_nThreadCount = 1; + if ( s_nPassCount == 0 ) + s_nPassCount = 1000; + } + //static void TearDownTestCase(); + + protected: + + template + void test( FreeList& list ) + { + cds_test::thread_pool& pool = get_pool(); + + value_type item;; + list.put( &item ); + + pool.add( new Worker( pool, list ), s_nThreadCount ); + + propout() << std::make_pair( "work_thread", s_nThreadCount ) + << std::make_pair( "pass_count", s_nPassCount ); + + std::chrono::milliseconds duration = pool.run(); + + propout() << std::make_pair( "duration", duration ); + + // analyze result + EXPECT_EQ( item.counter, s_nPassCount * s_nThreadCount ); + + list.clear( []( typename FreeList::node* ) {} ); + } + }; + + size_t put_get_single::s_nThreadCount = 4; + size_t put_get_single::s_nPassCount = 100000; + +#define CDSSTRESS_FREELIST_F( name, freelist_type ) \ + TEST_F( put_get_single, name ) \ + { \ + freelist_type fl; \ + test( fl ); \ + } + + CDSSTRESS_FREELIST_F( FreeList, cds::intrusive::FreeList ) + + typedef cds::intrusive::CachedFreeList cached_free_list; + CDSSTRESS_FREELIST_F( CachedFreeList, cached_free_list ) + + TEST_F( put_get_single, TaggetFreeList ) + { + struct tagged_ptr { + void* p; + uintptr_t tag; + }; + + atomics::atomic tp; + if ( tp.is_lock_free() ) { + cds::intrusive::TaggedFreeList fl; + test( fl ); + } + else + std::cout << "Double-width CAS is not supported\n"; + } + +} // namespace