4 #include <mutex> //unique_lock
5 #include "map2/map_type.h"
6 #include "cppunit/thread.h"
8 #include <cds/sync/spinlock.h>
13 #define TEST_CASE(TAG, X) void X();
15 class Map_InsDel_func: public CppUnitMini::TestCase
18 size_t c_nMapSize = 1000000; // map size
19 size_t c_nInsertThreadCount = 4; // count of insertion thread
20 size_t c_nDeleteThreadCount = 4; // count of deletion thread
21 size_t c_nUpdateThreadCount = 4; // count of updating thread
22 size_t c_nThreadPassCount = 4; // pass count for each thread
23 size_t c_nMaxLoadFactor = 8; // maximum load factor
24 bool c_bPrintGCState = true;
26 size_t c_nCuckooInitialSize = 1024;// initial size for CuckooMap
27 size_t c_nCuckooProbesetSize = 16; // CuckooMap probeset size (only for list-based probeset)
28 size_t c_nCuckooProbesetThreshold = 0; // CUckooMap probeset threshold (o - use default)
30 size_t c_nFeldmanMap_HeadBits = 10;
31 size_t c_nFeldmanMap_ArrayBits = 4;
33 size_t c_nLoadFactor; // current load factor
36 typedef size_t key_type;
41 atomics::atomic<bool> bInitialized;
42 cds::OS::ThreadId threadId; // inserter thread id
44 typedef cds::sync::spin_lock< cds::backoff::pause > lock_type;
45 mutable lock_type m_access;
51 , bInitialized( false )
52 , threadId( cds::OS::get_current_thread_id())
55 value_type( value_type const& s )
58 , nUpdateCall( s.nUpdateCall )
59 , bInitialized( s.bInitialized.load(atomics::memory_order_relaxed))
60 , threadId( cds::OS::get_current_thread_id())
63 // boost::container::flat_map requires operator =
64 value_type& operator=( value_type const& v )
68 nUpdateCall = v.nUpdateCall;
69 bInitialized.store(v.bInitialized.load(atomics::memory_order_relaxed), atomics::memory_order_relaxed);
75 typedef std::vector<key_type> key_array;
76 key_array m_arrValues;
79 class Inserter: public CppUnitMini::TestThread
83 virtual Inserter * clone()
85 return new Inserter( *this );
88 struct insert_functor {
89 size_t nTestFunctorRef;
95 template <typename Pair>
96 void operator()( Pair& val )
98 operator()( val.first, val.second );
101 template <typename Key, typename Val >
102 void operator()( Key const& key, Val& v )
104 std::unique_lock< typename value_type::lock_type> ac( v.m_access );
110 v.bInitialized.store( true, atomics::memory_order_relaxed);
115 size_t m_nInsertSuccess;
116 size_t m_nInsertFailed;
118 size_t m_nTestFunctorRef;
121 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
122 : CppUnitMini::TestThread( pool )
125 Inserter( Inserter& src )
126 : CppUnitMini::TestThread( src )
130 Map_InsDel_func& getTest()
132 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
135 virtual void init() { cds::threading::Manager::attachThread() ; }
136 virtual void fini() { cds::threading::Manager::detachThread() ; }
144 m_nTestFunctorRef = 0;
146 // func is passed by reference
148 key_array const& arr = getTest().m_arrValues;
149 size_t const nPassCount = getTest().c_nThreadPassCount;
151 if ( m_nThreadNo & 1 ) {
152 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
153 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
154 if ( rMap.insert_with( *it, std::ref(func)))
162 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
163 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
164 if ( rMap.insert_with( *it, std::ref(func)))
172 m_nTestFunctorRef = func.nTestFunctorRef;
177 class Updater: public CppUnitMini::TestThread
181 virtual Updater * clone()
183 return new Updater( *this );
186 struct update_functor {
195 template <typename Key, typename Val>
196 void operator()( bool /*bNew*/, Key const& key, Val& v )
198 std::unique_lock<typename value_type::lock_type> ac( v.m_access );
199 if ( !v.bInitialized.load( atomics::memory_order_acquire )) {
203 v.bInitialized.store( true, atomics::memory_order_relaxed);
211 template <typename Pair>
212 void operator()( bool bNew, Pair& val )
214 operator()( bNew, val.first, val.second );
217 // For FeldmanHashMap
218 template <typename Val>
219 void operator()( Val& cur, Val * old )
222 cur.second.nKey = cur.first;
223 cur.second.nData = cur.first * 8;
224 cur.second.bInitialized.store( true, atomics::memory_order_release );
226 operator()( old == nullptr, cur.first, cur.second );
230 update_functor(const update_functor& ) = delete;
234 size_t m_nUpdateFailed;
235 size_t m_nUpdateCreated;
236 size_t m_nUpdateExisted;
237 size_t m_nFunctorCreated;
238 size_t m_nFunctorModified;
241 Updater( CppUnitMini::ThreadPool& pool, Map& rMap )
242 : CppUnitMini::TestThread( pool )
245 Updater( Updater& src )
246 : CppUnitMini::TestThread( src )
250 Map_InsDel_func& getTest()
252 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
255 virtual void init() { cds::threading::Manager::attachThread() ; }
256 virtual void fini() { cds::threading::Manager::detachThread() ; }
268 key_array const& arr = getTest().m_arrValues;
269 size_t const nPassCount = getTest().c_nThreadPassCount;
271 if ( m_nThreadNo & 1 ) {
272 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
273 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
274 std::pair<bool, bool> ret = rMap.update( *it, std::ref( func ));
287 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
288 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
289 std::pair<bool, bool> ret = rMap.update( *it, std::ref( func ));
302 m_nFunctorCreated = func.nCreated;
303 m_nFunctorModified = func.nModified;
308 class Deleter: public CppUnitMini::TestThread
311 typedef typename Map::mapped_type value_type;
313 virtual Deleter * clone()
315 return new Deleter( *this );
318 struct value_container
331 struct erase_functor {
332 value_container m_cnt;
334 template <typename Key, typename Val>
335 void operator()( Key const& /*key*/, Val& v )
338 if ( v.bInitialized.load( atomics::memory_order_relaxed )) {
339 std::unique_lock< typename value_type::lock_type> ac( v.m_access );
341 if ( m_cnt.nKeyExpected == v.nKey && m_cnt.nKeyExpected * 8 == v.nData )
342 ++m_cnt.nSuccessItem;
350 cds::backoff::yield()();
354 template <typename Pair>
355 void operator ()( Pair& item )
357 operator()( item.first, item.second );
362 size_t m_nDeleteSuccess;
363 size_t m_nDeleteFailed;
365 size_t m_nValueSuccess;
366 size_t m_nValueFailed;
369 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
370 : CppUnitMini::TestThread( pool )
373 Deleter( Deleter& src )
374 : CppUnitMini::TestThread( src )
378 Map_InsDel_func& getTest()
380 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
383 virtual void init() { cds::threading::Manager::attachThread() ; }
384 virtual void fini() { cds::threading::Manager::detachThread() ; }
394 key_array const& arr = getTest().m_arrValues;
395 size_t const nPassCount = getTest().c_nThreadPassCount;
397 if ( m_nThreadNo & 1 ) {
398 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
399 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
400 func.m_cnt.nKeyExpected = *it;
401 if ( rMap.erase( *it, std::ref(func)))
409 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
410 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
411 func.m_cnt.nKeyExpected = *it;
412 if ( rMap.erase( *it, std::ref(func)))
420 m_nValueSuccess = func.m_cnt.nSuccessItem;
421 m_nValueFailed = func.m_cnt.nFailedItem;
428 void do_test( Map& testMap )
430 typedef Inserter<Map> InserterThread;
431 typedef Deleter<Map> DeleterThread;
432 typedef Updater<Map> UpdaterThread;
433 cds::OS::Timer timer;
436 m_arrValues.reserve( c_nMapSize );
437 for ( size_t i = 0; i < c_nMapSize; ++i )
438 m_arrValues.push_back( i );
439 shuffle( m_arrValues.begin(), m_arrValues.end());
441 CppUnitMini::ThreadPool pool( *this );
442 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
443 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
444 pool.add( new UpdaterThread( pool, testMap ), c_nUpdateThreadCount );
446 CPPUNIT_MSG( " Duration=" << pool.avgDuration());
448 size_t nInsertSuccess = 0;
449 size_t nInsertFailed = 0;
450 size_t nDeleteSuccess = 0;
451 size_t nDeleteFailed = 0;
452 size_t nDelValueSuccess = 0;
453 size_t nDelValueFailed = 0;
454 size_t nUpdateFailed = 0;
455 size_t nUpdateCreated = 0;
456 size_t nUpdateModified = 0;
457 size_t nEnsFuncCreated = 0;
458 size_t nEnsFuncModified = 0;
459 size_t nInsFuncCalled = 0;
461 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
462 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
464 nInsertSuccess += pThread->m_nInsertSuccess;
465 nInsertFailed += pThread->m_nInsertFailed;
466 nInsFuncCalled += pThread->m_nTestFunctorRef;
469 DeleterThread * p = dynamic_cast<DeleterThread *>( *it );
471 nDeleteSuccess += p->m_nDeleteSuccess;
472 nDeleteFailed += p->m_nDeleteFailed;
473 nDelValueSuccess += p->m_nValueSuccess;
474 nDelValueFailed += p->m_nValueFailed;
477 UpdaterThread * pEns = static_cast<UpdaterThread *>( *it );
478 nUpdateCreated += pEns->m_nUpdateCreated;
479 nUpdateModified += pEns->m_nUpdateExisted;
480 nUpdateFailed += pEns->m_nUpdateFailed;
481 nEnsFuncCreated += pEns->m_nFunctorCreated;
482 nEnsFuncModified += pEns->m_nFunctorModified;
487 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
488 << " Del succ=" << nDeleteSuccess << "\n"
489 << " : Ins fail=" << nInsertFailed
490 << " Del fail=" << nDeleteFailed << "\n"
491 << " : Update succ=" << (nUpdateCreated + nUpdateModified) << " fail=" << nUpdateFailed
492 << " create=" << nUpdateCreated << " modify=" << nUpdateModified << "\n"
493 << " Map size=" << testMap.size()
496 CPPUNIT_CHECK_EX( nDelValueFailed == 0, "Functor del failed=" << nDelValueFailed );
497 CPPUNIT_CHECK_EX( nDelValueSuccess == nDeleteSuccess, "Delete success=" << nDeleteSuccess << " functor=" << nDelValueSuccess );
499 CPPUNIT_CHECK( nUpdateFailed == 0 );
501 CPPUNIT_CHECK_EX( nUpdateCreated == nEnsFuncCreated, "Update created=" << nUpdateCreated << " functor=" << nEnsFuncCreated );
502 CPPUNIT_CHECK_EX( nUpdateModified == nEnsFuncModified, "Update modified=" << nUpdateModified << " functor=" << nEnsFuncModified );
504 // nInsFuncCalled is call count of insert functor
505 CPPUNIT_CHECK_EX( nInsFuncCalled == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nInsFuncCalled=" << nInsFuncCalled );
507 check_before_cleanup( testMap );
509 CPPUNIT_MSG( " Clear map (single-threaded)..." );
511 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
512 testMap.erase( nItem );
514 CPPUNIT_MSG( " Duration=" << timer.duration());
515 CPPUNIT_CHECK( testMap.empty());
517 additional_check( testMap );
518 print_stat( testMap );
519 additional_cleanup( testMap );
525 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
526 << " delete=" << c_nDeleteThreadCount
527 << " update=" << c_nUpdateThreadCount
528 << " pass count=" << c_nThreadPassCount
529 << " map size=" << c_nMapSize
532 if ( Map::c_bLoadFactorDepended ) {
533 for ( c_nLoadFactor = 1; c_nLoadFactor <= c_nMaxLoadFactor; c_nLoadFactor *= 2 ) {
534 CPPUNIT_MSG( "Load factor=" << c_nLoadFactor );
535 Map testMap( *this );
537 if ( c_bPrintGCState )
542 Map testMap( *this );
544 if ( c_bPrintGCState )
549 void setUpParams( const CppUnitMini::TestCfg& cfg );
551 # include "map2/map_defs.h"
552 CDSUNIT_DECLARE_MichaelMap
553 CDSUNIT_DECLARE_SplitList
554 CDSUNIT_DECLARE_SkipListMap
555 CDSUNIT_DECLARE_EllenBinTreeMap
556 CDSUNIT_DECLARE_BronsonAVLTreeMap
557 CDSUNIT_DECLARE_FeldmanHashMap_fixed
558 CDSUNIT_DECLARE_FeldmanHashMap_city
559 CDSUNIT_DECLARE_StripedMap
560 CDSUNIT_DECLARE_RefinableMap
561 CDSUNIT_DECLARE_CuckooMap
563 CPPUNIT_TEST_SUITE(Map_InsDel_func)
564 CDSUNIT_TEST_MichaelMap
565 CDSUNIT_TEST_SplitList
566 CDSUNIT_TEST_SkipListMap
567 CDSUNIT_TEST_EllenBinTreeMap
568 CDSUNIT_TEST_BronsonAVLTreeMap
569 CDSUNIT_TEST_FeldmanHashMap_fixed
570 CDSUNIT_TEST_FeldmanHashMap_city
571 CDSUNIT_TEST_CuckooMap
572 CDSUNIT_TEST_StripedMap
573 CDSUNIT_TEST_RefinableMap
574 CPPUNIT_TEST_SUITE_END();