5 #include <mutex> //unique_lock
7 #include "set2/set_types.h"
8 #include "cppunit/thread.h"
10 #include <cds/sync/spinlock.h>
14 # define TEST_SET(X) void X() { test<SetTypes<key_type, value_type>::X >() ; }
15 # define TEST_SET_EXTRACT(X) TEST_SET(X)
16 # define TEST_SET_NOLF(X) void X() { test_nolf<SetTypes<key_type, value_type>::X >() ; }
17 # define TEST_SET_NOLF_EXTRACT(X) TEST_SET_NOLF(X)
19 class Set_InsDel_func: public CppUnitMini::TestCase
21 static size_t c_nMapSize ; // map size
22 static size_t c_nInsertThreadCount ; // count of insertion thread
23 static size_t c_nDeleteThreadCount ; // count of deletion thread
24 static size_t c_nEnsureThreadCount ; // count of ensure thread
25 static size_t c_nThreadPassCount ; // pass count for each thread
26 static size_t c_nMaxLoadFactor ; // maximum load factor
27 static bool c_bPrintGCState;
29 typedef size_t key_type;
33 atomics::atomic<size_t> nEnsureCall;
34 bool volatile bInitialized;
35 cds::OS::ThreadId threadId ; // insert thread id
37 typedef cds::sync::spin_lock< cds::backoff::pause > lock_type;
38 mutable lock_type m_access;
44 , bInitialized( false )
45 , threadId( cds::OS::get_current_thread_id() )
48 value_type( value_type const& s )
51 , nEnsureCall(s.nEnsureCall.load(atomics::memory_order_relaxed))
52 , bInitialized( s.bInitialized )
53 , threadId( cds::OS::get_current_thread_id() )
56 // boost::container::flat_map requires operator =
57 value_type& operator=( value_type const& v )
61 nEnsureCall.store( v.nEnsureCall.load(atomics::memory_order_relaxed), atomics::memory_order_relaxed );
62 bInitialized = v.bInitialized;
75 class Inserter: public CppUnitMini::TestThread
78 typedef typename Set::value_type keyval_type;
80 virtual Inserter * clone()
82 return new Inserter( *this );
85 struct insert_functor {
86 size_t nTestFunctorRef;
92 void operator()( keyval_type& val )
94 std::unique_lock< typename value_type::lock_type> ac( val.val.m_access );
96 val.val.nKey = val.key;
97 val.val.nData = val.key * 8;
100 val.val.bInitialized = true;
105 size_t m_nInsertSuccess;
106 size_t m_nInsertFailed;
108 size_t m_nTestFunctorRef;
111 Inserter( CppUnitMini::ThreadPool& pool, Set& rSet )
112 : CppUnitMini::TestThread( pool )
115 Inserter( Inserter& src )
116 : CppUnitMini::TestThread( src )
120 Set_InsDel_func& getTest()
122 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
125 virtual void init() { cds::threading::Manager::attachThread() ; }
126 virtual void fini() { cds::threading::Manager::detachThread() ; }
134 m_nTestFunctorRef = 0;
136 size_t * pKeyFirst = getTest().m_pKeyFirst;
137 size_t * pKeyLast = getTest().m_pKeyLast;
139 // func is passed by reference
142 if ( m_nThreadNo & 1 ) {
143 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
144 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
145 if ( rSet.insert( *p, std::ref(func) ) )
153 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
154 for ( size_t * p = pKeyLast - 1; p >= pKeyFirst; --p ) {
155 if ( rSet.insert( *p, std::ref(func) ) )
163 m_nTestFunctorRef = func.nTestFunctorRef;
168 class Ensurer: public CppUnitMini::TestThread
171 typedef typename Set::value_type keyval_type;
173 virtual Ensurer * clone()
175 return new Ensurer( *this );
178 struct ensure_functor {
187 void operator()( bool bNew, keyval_type& val, size_t /*nKey*/ )
189 std::unique_lock<typename value_type::lock_type> ac( val.val.m_access );
190 if ( !val.val.bInitialized )
192 val.val.nKey = val.key;
193 val.val.nData = val.key * 8;
194 val.val.bInitialized = true;
201 val.val.nEnsureCall.fetch_add( 1, atomics::memory_order_relaxed );
206 ensure_functor(const ensure_functor& );
210 size_t m_nEnsureFailed;
211 size_t m_nEnsureCreated;
212 size_t m_nEnsureExisted;
213 size_t m_nFunctorCreated;
214 size_t m_nFunctorModified;
217 Ensurer( CppUnitMini::ThreadPool& pool, Set& rSet )
218 : CppUnitMini::TestThread( pool )
221 Ensurer( Ensurer& src )
222 : CppUnitMini::TestThread( src )
226 Set_InsDel_func& getTest()
228 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
231 virtual void init() { cds::threading::Manager::attachThread() ; }
232 virtual void fini() { cds::threading::Manager::detachThread() ; }
242 size_t * pKeyFirst = getTest().m_pKeyFirst;
243 size_t * pKeyLast = getTest().m_pKeyLast;
247 if ( m_nThreadNo & 1 ) {
248 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
249 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
250 std::pair<bool, bool> ret = rSet.ensure( *p, std::ref( func ) );
263 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
264 for ( size_t * p = pKeyLast - 1 ; p >= pKeyFirst; --p ) {
265 std::pair<bool, bool> ret = rSet.ensure( *p, std::ref( func ) );
278 m_nFunctorCreated = func.nCreated;
279 m_nFunctorModified = func.nModified;
284 class Deleter: public CppUnitMini::TestThread
287 typedef typename Set::value_type keyval_type;
289 virtual Deleter * clone()
291 return new Deleter( *this );
294 struct value_container
307 struct erase_functor {
308 value_container m_cnt;
310 void operator ()( keyval_type const& itm )
312 keyval_type& item = const_cast<keyval_type&>(itm);
316 std::unique_lock< typename value_type::lock_type> ac( item.val.m_access );
317 if ( item.val.bInitialized ) {
318 if ( m_cnt.nKeyExpected == item.val.nKey && m_cnt.nKeyExpected * 8 == item.val.nData )
319 ++m_cnt.nSuccessItem;
330 cds::backoff::yield()();
336 size_t m_nDeleteSuccess;
337 size_t m_nDeleteFailed;
339 size_t m_nValueSuccess;
340 size_t m_nValueFailed;
343 Deleter( CppUnitMini::ThreadPool& pool, Set& rSet )
344 : CppUnitMini::TestThread( pool )
347 Deleter( Deleter& src )
348 : CppUnitMini::TestThread( src )
352 Set_InsDel_func& getTest()
354 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
357 virtual void init() { cds::threading::Manager::attachThread() ; }
358 virtual void fini() { cds::threading::Manager::detachThread() ; }
367 size_t * pKeyFirst = getTest().m_pKeyFirst;
368 size_t * pKeyLast = getTest().m_pKeyLast;
372 if ( m_nThreadNo & 1 ) {
373 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
374 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
375 func.m_cnt.nKeyExpected = *p;
376 if ( rSet.erase( *p, std::ref(func) ))
384 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
385 for ( size_t * p = pKeyLast - 1; p >= pKeyFirst; --p ) {
386 func.m_cnt.nKeyExpected = *p;
387 if ( rSet.erase( *p, std::ref(func) ))
395 m_nValueSuccess = func.m_cnt.nSuccessItem;
396 m_nValueFailed = func.m_cnt.nFailedItem;
402 void do_test( size_t nLoadFactor )
404 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
406 Set testSet( c_nMapSize, nLoadFactor );
407 do_test_with( testSet );
411 void do_test_with( Set& testSet )
413 typedef Inserter<Set> InserterThread;
414 typedef Deleter<Set> DeleterThread;
415 typedef Ensurer<Set> EnsurerThread;
417 m_pKeyArr = new size_t[ c_nMapSize ];
418 m_pKeyFirst = m_pKeyArr;
419 m_pKeyLast = m_pKeyFirst + c_nMapSize;
420 for ( size_t i = 0; i < c_nMapSize; ++i )
422 std::random_shuffle( m_pKeyFirst, m_pKeyLast );
424 cds::OS::Timer timer;
426 CppUnitMini::ThreadPool pool( *this );
427 pool.add( new InserterThread( pool, testSet ), c_nInsertThreadCount );
428 pool.add( new DeleterThread( pool, testSet ), c_nDeleteThreadCount );
429 pool.add( new EnsurerThread( pool, testSet ), c_nEnsureThreadCount );
431 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
435 size_t nInsertSuccess = 0;
436 size_t nInsertFailed = 0;
437 size_t nDeleteSuccess = 0;
438 size_t nDeleteFailed = 0;
439 size_t nDelValueSuccess = 0;
440 size_t nDelValueFailed = 0;
441 size_t nEnsureFailed = 0;
442 size_t nEnsureCreated = 0;
443 size_t nEnsureModified = 0;
444 size_t nEnsFuncCreated = 0;
445 size_t nEnsFuncModified = 0;
446 size_t nTestFunctorRef = 0;
448 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
449 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
451 nInsertSuccess += pThread->m_nInsertSuccess;
452 nInsertFailed += pThread->m_nInsertFailed;
453 nTestFunctorRef += pThread->m_nTestFunctorRef;
456 DeleterThread * p = dynamic_cast<DeleterThread *>( *it );
458 nDeleteSuccess += p->m_nDeleteSuccess;
459 nDeleteFailed += p->m_nDeleteFailed;
460 nDelValueSuccess += p->m_nValueSuccess;
461 nDelValueFailed += p->m_nValueFailed;
464 EnsurerThread * pEns = static_cast<EnsurerThread *>( *it );
465 nEnsureCreated += pEns->m_nEnsureCreated;
466 nEnsureModified += pEns->m_nEnsureExisted;
467 nEnsureFailed += pEns->m_nEnsureFailed;
468 nEnsFuncCreated += pEns->m_nFunctorCreated;
469 nEnsFuncModified += pEns->m_nFunctorModified;
475 " Totals: Ins succ=" << nInsertSuccess
476 << " Del succ=" << nDeleteSuccess << "\n"
477 << " : Ins fail=" << nInsertFailed
478 << " Del fail=" << nDeleteFailed << "\n"
479 << " : Ensure succ=" << (nEnsureCreated + nEnsureModified) << " fail=" << nEnsureFailed
480 << " create=" << nEnsureCreated << " modify=" << nEnsureModified << "\n"
481 << " Set size=" << testSet.size()
484 CPPUNIT_CHECK_EX( nDelValueFailed == 0, "Functor del failed=" << nDelValueFailed );
485 CPPUNIT_CHECK_EX( nDelValueSuccess == nDeleteSuccess, "Delete success=" << nDeleteSuccess << " functor=" << nDelValueSuccess );
487 CPPUNIT_CHECK( nEnsureFailed == 0 );
489 CPPUNIT_CHECK_EX( nEnsureCreated == nEnsFuncCreated, "Ensure created=" << nEnsureCreated << " functor=" << nEnsFuncCreated );
490 CPPUNIT_CHECK_EX( nEnsureModified == nEnsFuncModified, "Ensure modified=" << nEnsureModified << " functor=" << nEnsFuncModified );
492 // nTestFunctorRef is call count of insert functor
493 CPPUNIT_CHECK_EX( nTestFunctorRef == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nTestFunctorRef=" << nTestFunctorRef );
495 CPPUNIT_MSG( " Clear set (single-threaded)..." );
498 CPPUNIT_MSG( " Duration=" << timer.duration() );
499 CPPUNIT_CHECK( testSet.empty() );
501 additional_check( testSet );
502 print_stat( testSet );
504 additional_cleanup( testSet );
510 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
511 << " delete=" << c_nDeleteThreadCount
512 << " ensure=" << c_nEnsureThreadCount
513 << " pass count=" << c_nThreadPassCount
514 << " map size=" << c_nMapSize
517 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
518 do_test<Set>( nLoadFactor );
519 if ( c_bPrintGCState )
527 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
528 << " delete=" << c_nDeleteThreadCount
529 << " ensure=" << c_nEnsureThreadCount
530 << " pass count=" << c_nThreadPassCount
531 << " map size=" << c_nMapSize
536 if ( c_bPrintGCState )
540 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
541 c_nInsertThreadCount = cfg.getULong("InsertThreadCount", 4 );
542 c_nDeleteThreadCount = cfg.getULong("DeleteThreadCount", 4 );
543 c_nEnsureThreadCount = cfg.getULong("EnsureThreadCount", 4 );
544 c_nThreadPassCount = cfg.getULong("ThreadPassCount", 4 );
545 c_nMapSize = cfg.getULong("MapSize", 1000000 );
546 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
547 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
550 void run_MichaelSet(const char *in_name, bool invert = false);
551 void run_SplitList(const char *in_name, bool invert = false);
552 void run_StripedSet(const char *in_name, bool invert = false);
553 void run_RefinableSet(const char *in_name, bool invert = false);
554 void run_CuckooSet(const char *in_name, bool invert = false);
555 void run_SkipListSet(const char *in_name, bool invert = false);
556 void run_EllenBinTreeSet(const char *in_name, bool invert = false);
558 typedef CppUnitMini::TestCase Base;
559 virtual void myRun(const char *in_name, bool invert = false)
561 setUpParams( m_Cfg.get( "Map_InsDel_func" ));
563 run_MichaelSet(in_name, invert);
564 run_SplitList(in_name, invert);
565 run_SkipListSet(in_name, invert);
566 run_EllenBinTreeSet(in_name, invert);
567 run_StripedSet(in_name, invert);
568 run_RefinableSet(in_name, invert);
569 run_CuckooSet(in_name, invert);
575 # include "set2/set_defs.h"
576 CDSUNIT_DECLARE_MichaelSet
577 CDSUNIT_DECLARE_SplitList
578 CDSUNIT_DECLARE_StripedSet
579 CDSUNIT_DECLARE_RefinableSet
580 CDSUNIT_DECLARE_CuckooSet
581 CDSUNIT_DECLARE_SkipListSet
582 CDSUNIT_DECLARE_EllenBinTreeSet