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;
40 atomics::atomic<size_t> nUpdateCall;
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.load(atomics::memory_order_relaxed))
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.store( v.nUpdateCall.load(atomics::memory_order_relaxed), atomics::memory_order_relaxed );
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 );
203 v.bInitialized.store( true, atomics::memory_order_relaxed);
206 v.nUpdateCall.fetch_add( 1, 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 )
221 operator()( old == nullptr, cur.first, cur.second );
225 update_functor(const update_functor& ) = delete;
229 size_t m_nUpdateFailed;
230 size_t m_nUpdateCreated;
231 size_t m_nUpdateExisted;
232 size_t m_nFunctorCreated;
233 size_t m_nFunctorModified;
236 Updater( CppUnitMini::ThreadPool& pool, Map& rMap )
237 : CppUnitMini::TestThread( pool )
240 Updater( Updater& src )
241 : CppUnitMini::TestThread( src )
245 Map_InsDel_func& getTest()
247 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
250 virtual void init() { cds::threading::Manager::attachThread() ; }
251 virtual void fini() { cds::threading::Manager::detachThread() ; }
263 key_array const& arr = getTest().m_arrValues;
264 size_t const nPassCount = getTest().c_nThreadPassCount;
266 if ( m_nThreadNo & 1 ) {
267 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
268 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
269 std::pair<bool, bool> ret = rMap.update( *it, std::ref( func ));
282 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
283 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
284 std::pair<bool, bool> ret = rMap.update( *it, std::ref( func ));
297 m_nFunctorCreated = func.nCreated;
298 m_nFunctorModified = func.nModified;
303 class Deleter: public CppUnitMini::TestThread
306 typedef typename Map::mapped_type value_type;
308 virtual Deleter * clone()
310 return new Deleter( *this );
313 struct value_container
326 struct erase_functor {
327 value_container m_cnt;
329 template <typename Key, typename Val>
330 void operator()( Key const& /*key*/, Val& v )
333 if ( v.bInitialized.load( atomics::memory_order_relaxed )) {
334 std::unique_lock< typename value_type::lock_type> ac( v.m_access );
336 if ( m_cnt.nKeyExpected == v.nKey && m_cnt.nKeyExpected * 8 == v.nData )
337 ++m_cnt.nSuccessItem;
345 cds::backoff::yield()();
349 template <typename Pair>
350 void operator ()( Pair& item )
352 operator()( item.first, item.second );
357 size_t m_nDeleteSuccess;
358 size_t m_nDeleteFailed;
360 size_t m_nValueSuccess;
361 size_t m_nValueFailed;
364 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
365 : CppUnitMini::TestThread( pool )
368 Deleter( Deleter& src )
369 : CppUnitMini::TestThread( src )
373 Map_InsDel_func& getTest()
375 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
378 virtual void init() { cds::threading::Manager::attachThread() ; }
379 virtual void fini() { cds::threading::Manager::detachThread() ; }
389 key_array const& arr = getTest().m_arrValues;
390 size_t const nPassCount = getTest().c_nThreadPassCount;
392 if ( m_nThreadNo & 1 ) {
393 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
394 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
395 func.m_cnt.nKeyExpected = *it;
396 if ( rMap.erase( *it, std::ref(func)))
404 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
405 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
406 func.m_cnt.nKeyExpected = *it;
407 if ( rMap.erase( *it, std::ref(func)))
415 m_nValueSuccess = func.m_cnt.nSuccessItem;
416 m_nValueFailed = func.m_cnt.nFailedItem;
423 void do_test( Map& testMap )
425 typedef Inserter<Map> InserterThread;
426 typedef Deleter<Map> DeleterThread;
427 typedef Updater<Map> UpdaterThread;
428 cds::OS::Timer timer;
431 m_arrValues.reserve( c_nMapSize );
432 for ( size_t i = 0; i < c_nMapSize; ++i )
433 m_arrValues.push_back( i );
434 shuffle( m_arrValues.begin(), m_arrValues.end());
436 CppUnitMini::ThreadPool pool( *this );
437 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
438 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
439 pool.add( new UpdaterThread( pool, testMap ), c_nUpdateThreadCount );
441 CPPUNIT_MSG( " Duration=" << pool.avgDuration());
443 size_t nInsertSuccess = 0;
444 size_t nInsertFailed = 0;
445 size_t nDeleteSuccess = 0;
446 size_t nDeleteFailed = 0;
447 size_t nDelValueSuccess = 0;
448 size_t nDelValueFailed = 0;
449 size_t nUpdateFailed = 0;
450 size_t nUpdateCreated = 0;
451 size_t nUpdateModified = 0;
452 size_t nEnsFuncCreated = 0;
453 size_t nEnsFuncModified = 0;
454 size_t nTestFunctorRef = 0;
456 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
457 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
459 nInsertSuccess += pThread->m_nInsertSuccess;
460 nInsertFailed += pThread->m_nInsertFailed;
461 nTestFunctorRef += pThread->m_nTestFunctorRef;
464 DeleterThread * p = dynamic_cast<DeleterThread *>( *it );
466 nDeleteSuccess += p->m_nDeleteSuccess;
467 nDeleteFailed += p->m_nDeleteFailed;
468 nDelValueSuccess += p->m_nValueSuccess;
469 nDelValueFailed += p->m_nValueFailed;
472 UpdaterThread * pEns = static_cast<UpdaterThread *>( *it );
473 nUpdateCreated += pEns->m_nUpdateCreated;
474 nUpdateModified += pEns->m_nUpdateExisted;
475 nUpdateFailed += pEns->m_nUpdateFailed;
476 nEnsFuncCreated += pEns->m_nFunctorCreated;
477 nEnsFuncModified += pEns->m_nFunctorModified;
482 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
483 << " Del succ=" << nDeleteSuccess << "\n"
484 << " : Ins fail=" << nInsertFailed
485 << " Del fail=" << nDeleteFailed << "\n"
486 << " : Update succ=" << (nUpdateCreated + nUpdateModified) << " fail=" << nUpdateFailed
487 << " create=" << nUpdateCreated << " modify=" << nUpdateModified << "\n"
488 << " Map size=" << testMap.size()
491 CPPUNIT_CHECK_EX( nDelValueFailed == 0, "Functor del failed=" << nDelValueFailed );
492 CPPUNIT_CHECK_EX( nDelValueSuccess == nDeleteSuccess, "Delete success=" << nDeleteSuccess << " functor=" << nDelValueSuccess );
494 CPPUNIT_CHECK( nUpdateFailed == 0 );
496 CPPUNIT_CHECK_EX( nUpdateCreated == nEnsFuncCreated, "Update created=" << nUpdateCreated << " functor=" << nEnsFuncCreated );
497 CPPUNIT_CHECK_EX( nUpdateModified == nEnsFuncModified, "Update modified=" << nUpdateModified << " functor=" << nEnsFuncModified );
499 // nTestFunctorRef is call count of insert functor
500 CPPUNIT_CHECK_EX( nTestFunctorRef == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nTestFunctorRef=" << nTestFunctorRef );
502 check_before_cleanup( testMap );
504 CPPUNIT_MSG( " Clear map (single-threaded)..." );
506 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
507 testMap.erase( nItem );
509 CPPUNIT_MSG( " Duration=" << timer.duration());
510 CPPUNIT_CHECK( testMap.empty());
512 additional_check( testMap );
513 print_stat( testMap );
514 additional_cleanup( testMap );
520 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
521 << " delete=" << c_nDeleteThreadCount
522 << " update=" << c_nUpdateThreadCount
523 << " pass count=" << c_nThreadPassCount
524 << " map size=" << c_nMapSize
527 if ( Map::c_bLoadFactorDepended ) {
528 for ( c_nLoadFactor = 1; c_nLoadFactor <= c_nMaxLoadFactor; c_nLoadFactor *= 2 ) {
529 CPPUNIT_MSG( "Load factor=" << c_nLoadFactor );
530 Map testMap( *this );
532 if ( c_bPrintGCState )
537 Map testMap( *this );
539 if ( c_bPrintGCState )
544 void setUpParams( const CppUnitMini::TestCfg& cfg );
546 # include "map2/map_defs.h"
547 CDSUNIT_DECLARE_MichaelMap
548 CDSUNIT_DECLARE_SplitList
549 CDSUNIT_DECLARE_SkipListMap
550 CDSUNIT_DECLARE_EllenBinTreeMap
551 CDSUNIT_DECLARE_BronsonAVLTreeMap
552 CDSUNIT_DECLARE_FeldmanHashMap_fixed
553 CDSUNIT_DECLARE_FeldmanHashMap_city
554 CDSUNIT_DECLARE_StripedMap
555 CDSUNIT_DECLARE_RefinableMap
556 CDSUNIT_DECLARE_CuckooMap
558 CPPUNIT_TEST_SUITE(Map_InsDel_func)
559 CDSUNIT_TEST_MichaelMap
560 CDSUNIT_TEST_SplitList
561 CDSUNIT_TEST_SkipListMap
562 CDSUNIT_TEST_EllenBinTreeMap
563 CDSUNIT_TEST_BronsonAVLTreeMap
564 CDSUNIT_TEST_FeldmanHashMap_fixed
565 CDSUNIT_TEST_FeldmanHashMap_city
566 CDSUNIT_TEST_CuckooMap
567 CDSUNIT_TEST_StripedMap
568 CDSUNIT_TEST_RefinableMap
569 CPPUNIT_TEST_SUITE_END();