3 #include "set2/set_types.h"
4 #include "cppunit/thread.h"
6 #include <cds/lock/spinlock.h>
8 #include <boost/ref.hpp>
12 # define TEST_SET(X) void X() { test<SetTypes<key_type, value_type>::X >() ; }
13 # define TEST_SET_EXTRACT(X) TEST_SET(X)
14 # define TEST_SET_NOLF(X) void X() { test_nolf<SetTypes<key_type, value_type>::X >() ; }
15 # define TEST_SET_NOLF_EXTRACT(X) TEST_SET_NOLF(X)
17 class Set_InsDel_func: public CppUnitMini::TestCase
19 static size_t c_nMapSize ; // map size
20 static size_t c_nInsertThreadCount ; // count of insertion thread
21 static size_t c_nDeleteThreadCount ; // count of deletion thread
22 static size_t c_nEnsureThreadCount ; // count of ensure thread
23 static size_t c_nThreadPassCount ; // pass count for each thread
24 static size_t c_nMaxLoadFactor ; // maximum load factor
25 static bool c_bPrintGCState;
27 typedef size_t key_type;
31 CDS_ATOMIC::atomic<size_t> nEnsureCall;
32 bool volatile bInitialized;
33 cds::OS::ThreadId threadId ; // insert thread id
35 typedef cds::lock::Spinlock< cds::backoff::pause > lock_type;
36 mutable lock_type m_access;
42 , bInitialized( false )
43 , threadId( cds::OS::getCurrentThreadId() )
46 value_type( value_type const& s )
49 , nEnsureCall(s.nEnsureCall.load(CDS_ATOMIC::memory_order_relaxed))
50 , bInitialized( s.bInitialized )
51 , threadId( cds::OS::getCurrentThreadId() )
54 // boost::container::flat_map requires operator =
55 value_type& operator=( value_type const& v )
59 nEnsureCall.store( v.nEnsureCall.load(CDS_ATOMIC::memory_order_relaxed), CDS_ATOMIC::memory_order_relaxed );
60 bInitialized = v.bInitialized;
73 class Inserter: public CppUnitMini::TestThread
76 typedef typename Set::value_type keyval_type;
78 virtual Inserter * clone()
80 return new Inserter( *this );
83 struct insert_functor {
84 size_t nTestFunctorRef;
90 void operator()( keyval_type& val )
92 cds::lock::scoped_lock< typename value_type::lock_type> ac( val.val.m_access );
94 val.val.nKey = val.key;
95 val.val.nData = val.key * 8;
98 val.val.bInitialized = true;
103 size_t m_nInsertSuccess;
104 size_t m_nInsertFailed;
106 size_t m_nTestFunctorRef;
109 Inserter( CppUnitMini::ThreadPool& pool, Set& rSet )
110 : CppUnitMini::TestThread( pool )
113 Inserter( Inserter& src )
114 : CppUnitMini::TestThread( src )
118 Set_InsDel_func& getTest()
120 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
123 virtual void init() { cds::threading::Manager::attachThread() ; }
124 virtual void fini() { cds::threading::Manager::detachThread() ; }
132 m_nTestFunctorRef = 0;
134 size_t * pKeyFirst = getTest().m_pKeyFirst;
135 size_t * pKeyLast = getTest().m_pKeyLast;
137 // func is passed by reference
140 if ( m_nThreadNo & 1 ) {
141 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
142 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
143 if ( rSet.insert( *p, cds::ref(func) ) )
151 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
152 for ( size_t * p = pKeyLast - 1; p >= pKeyFirst; --p ) {
153 if ( rSet.insert( *p, cds::ref(func) ) )
161 m_nTestFunctorRef = func.nTestFunctorRef;
166 class Ensurer: public CppUnitMini::TestThread
169 typedef typename Set::value_type keyval_type;
171 virtual Ensurer * clone()
173 return new Ensurer( *this );
176 struct ensure_functor {
185 void operator()( bool bNew, keyval_type& val, size_t nKey )
187 cds::lock::scoped_lock<typename value_type::lock_type> ac( val.val.m_access );
188 if ( !val.val.bInitialized )
190 val.val.nKey = val.key;
191 val.val.nData = val.key * 8;
192 val.val.bInitialized = true;
199 val.val.nEnsureCall.fetch_add( 1, CDS_ATOMIC::memory_order_relaxed );
204 ensure_functor(const ensure_functor& );
208 size_t m_nEnsureFailed;
209 size_t m_nEnsureCreated;
210 size_t m_nEnsureExisted;
211 size_t m_nFunctorCreated;
212 size_t m_nFunctorModified;
215 Ensurer( CppUnitMini::ThreadPool& pool, Set& rSet )
216 : CppUnitMini::TestThread( pool )
219 Ensurer( Ensurer& src )
220 : CppUnitMini::TestThread( src )
224 Set_InsDel_func& getTest()
226 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
229 virtual void init() { cds::threading::Manager::attachThread() ; }
230 virtual void fini() { cds::threading::Manager::detachThread() ; }
240 size_t * pKeyFirst = getTest().m_pKeyFirst;
241 size_t * pKeyLast = getTest().m_pKeyLast;
245 if ( m_nThreadNo & 1 ) {
246 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
247 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
248 std::pair<bool, bool> ret = rSet.ensure( *p, cds::ref( func ) );
261 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
262 for ( size_t * p = pKeyLast - 1 ; p >= pKeyFirst; --p ) {
263 std::pair<bool, bool> ret = rSet.ensure( *p, cds::ref( func ) );
276 m_nFunctorCreated = func.nCreated;
277 m_nFunctorModified = func.nModified;
282 class Deleter: public CppUnitMini::TestThread
285 typedef typename Set::value_type keyval_type;
287 virtual Deleter * clone()
289 return new Deleter( *this );
292 struct value_container
305 struct erase_functor {
306 value_container m_cnt;
308 void operator ()( keyval_type const& itm )
310 keyval_type& item = const_cast<keyval_type&>(itm);
314 cds::lock::scoped_lock< typename value_type::lock_type> ac( item.val.m_access );
315 if ( item.val.bInitialized ) {
316 if ( m_cnt.nKeyExpected == item.val.nKey && m_cnt.nKeyExpected * 8 == item.val.nData )
317 ++m_cnt.nSuccessItem;
328 cds::backoff::yield()();
334 size_t m_nDeleteSuccess;
335 size_t m_nDeleteFailed;
337 size_t m_nValueSuccess;
338 size_t m_nValueFailed;
341 Deleter( CppUnitMini::ThreadPool& pool, Set& rSet )
342 : CppUnitMini::TestThread( pool )
345 Deleter( Deleter& src )
346 : CppUnitMini::TestThread( src )
350 Set_InsDel_func& getTest()
352 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
355 virtual void init() { cds::threading::Manager::attachThread() ; }
356 virtual void fini() { cds::threading::Manager::detachThread() ; }
365 size_t * pKeyFirst = getTest().m_pKeyFirst;
366 size_t * pKeyLast = getTest().m_pKeyLast;
370 if ( m_nThreadNo & 1 ) {
371 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
372 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
373 func.m_cnt.nKeyExpected = *p;
374 if ( rSet.erase( *p, cds::ref(func) ))
382 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
383 for ( size_t * p = pKeyLast - 1; p >= pKeyFirst; --p ) {
384 func.m_cnt.nKeyExpected = *p;
385 if ( rSet.erase( *p, cds::ref(func) ))
393 m_nValueSuccess = func.m_cnt.nSuccessItem;
394 m_nValueFailed = func.m_cnt.nFailedItem;
400 void do_test( size_t nLoadFactor )
402 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
404 Set testSet( c_nMapSize, nLoadFactor );
405 do_test_with( testSet );
409 void do_test_with( Set& testSet )
411 typedef Inserter<Set> InserterThread;
412 typedef Deleter<Set> DeleterThread;
413 typedef Ensurer<Set> EnsurerThread;
415 m_pKeyArr = new size_t[ c_nMapSize ];
416 m_pKeyFirst = m_pKeyArr;
417 m_pKeyLast = m_pKeyFirst + c_nMapSize;
418 for ( size_t i = 0; i < c_nMapSize; ++i )
420 std::random_shuffle( m_pKeyFirst, m_pKeyLast );
422 cds::OS::Timer timer;
424 CppUnitMini::ThreadPool pool( *this );
425 pool.add( new InserterThread( pool, testSet ), c_nInsertThreadCount );
426 pool.add( new DeleterThread( pool, testSet ), c_nDeleteThreadCount );
427 pool.add( new EnsurerThread( pool, testSet ), c_nEnsureThreadCount );
429 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
433 size_t nInsertSuccess = 0;
434 size_t nInsertFailed = 0;
435 size_t nDeleteSuccess = 0;
436 size_t nDeleteFailed = 0;
437 size_t nDelValueSuccess = 0;
438 size_t nDelValueFailed = 0;
439 size_t nEnsureFailed = 0;
440 size_t nEnsureCreated = 0;
441 size_t nEnsureModified = 0;
442 size_t nEnsFuncCreated = 0;
443 size_t nEnsFuncModified = 0;
444 size_t nTestFunctorRef = 0;
446 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
447 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
449 nInsertSuccess += pThread->m_nInsertSuccess;
450 nInsertFailed += pThread->m_nInsertFailed;
451 nTestFunctorRef += pThread->m_nTestFunctorRef;
454 DeleterThread * p = dynamic_cast<DeleterThread *>( *it );
456 nDeleteSuccess += p->m_nDeleteSuccess;
457 nDeleteFailed += p->m_nDeleteFailed;
458 nDelValueSuccess += p->m_nValueSuccess;
459 nDelValueFailed += p->m_nValueFailed;
462 EnsurerThread * pEns = static_cast<EnsurerThread *>( *it );
463 nEnsureCreated += pEns->m_nEnsureCreated;
464 nEnsureModified += pEns->m_nEnsureExisted;
465 nEnsureFailed += pEns->m_nEnsureFailed;
466 nEnsFuncCreated += pEns->m_nFunctorCreated;
467 nEnsFuncModified += pEns->m_nFunctorModified;
473 " Totals: Ins succ=" << nInsertSuccess
474 << " Del succ=" << nDeleteSuccess << "\n"
475 << " : Ins fail=" << nInsertFailed
476 << " Del fail=" << nDeleteFailed << "\n"
477 << " : Ensure succ=" << (nEnsureCreated + nEnsureModified) << " fail=" << nEnsureFailed
478 << " create=" << nEnsureCreated << " modify=" << nEnsureModified << "\n"
479 << " Set size=" << testSet.size()
482 CPPUNIT_CHECK_EX( nDelValueFailed == 0, "Functor del failed=" << nDelValueFailed );
483 CPPUNIT_CHECK_EX( nDelValueSuccess == nDeleteSuccess, "Delete success=" << nDeleteSuccess << " functor=" << nDelValueSuccess );
485 CPPUNIT_CHECK( nEnsureFailed == 0 );
487 CPPUNIT_CHECK_EX( nEnsureCreated == nEnsFuncCreated, "Ensure created=" << nEnsureCreated << " functor=" << nEnsFuncCreated );
488 CPPUNIT_CHECK_EX( nEnsureModified == nEnsFuncModified, "Ensure modified=" << nEnsureModified << " functor=" << nEnsFuncModified );
490 // nTestFunctorRef is call count of insert functor
491 CPPUNIT_CHECK_EX( nTestFunctorRef == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nTestFunctorRef=" << nTestFunctorRef );
493 CPPUNIT_MSG( " Clear set (single-threaded)..." );
496 CPPUNIT_MSG( " Duration=" << timer.duration() );
497 CPPUNIT_CHECK( testSet.empty() );
499 additional_check( testSet );
500 print_stat( testSet );
502 additional_cleanup( testSet );
508 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
509 << " delete=" << c_nDeleteThreadCount
510 << " ensure=" << c_nEnsureThreadCount
511 << " pass count=" << c_nThreadPassCount
512 << " map size=" << c_nMapSize
515 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
516 do_test<Set>( nLoadFactor );
517 if ( c_bPrintGCState )
525 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
526 << " delete=" << c_nDeleteThreadCount
527 << " ensure=" << c_nEnsureThreadCount
528 << " pass count=" << c_nThreadPassCount
529 << " map size=" << c_nMapSize
534 if ( c_bPrintGCState )
538 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
539 c_nInsertThreadCount = cfg.getULong("InsertThreadCount", 4 );
540 c_nDeleteThreadCount = cfg.getULong("DeleteThreadCount", 4 );
541 c_nEnsureThreadCount = cfg.getULong("EnsureThreadCount", 4 );
542 c_nThreadPassCount = cfg.getULong("ThreadPassCount", 4 );
543 c_nMapSize = cfg.getULong("MapSize", 1000000 );
544 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
545 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
548 void run_MichaelSet(const char *in_name, bool invert = false);
549 void run_SplitList(const char *in_name, bool invert = false);
550 void run_StripedSet(const char *in_name, bool invert = false);
551 void run_RefinableSet(const char *in_name, bool invert = false);
552 void run_CuckooSet(const char *in_name, bool invert = false);
553 void run_SkipListSet(const char *in_name, bool invert = false);
554 void run_EllenBinTreeSet(const char *in_name, bool invert = false);
556 typedef CppUnitMini::TestCase Base;
557 virtual void myRun(const char *in_name, bool invert = false)
559 setUpParams( m_Cfg.get( "Map_InsDel_func" ));
561 run_MichaelSet(in_name, invert);
562 run_SplitList(in_name, invert);
563 run_SkipListSet(in_name, invert);
564 run_EllenBinTreeSet(in_name, invert);
565 run_StripedSet(in_name, invert);
566 run_RefinableSet(in_name, invert);
567 run_CuckooSet(in_name, invert);
573 # include "set2/set_defs.h"
574 CDSUNIT_DECLARE_MichaelSet
575 CDSUNIT_DECLARE_SplitList
576 CDSUNIT_DECLARE_StripedSet
577 CDSUNIT_DECLARE_RefinableSet
578 CDSUNIT_DECLARE_CuckooSet
579 CDSUNIT_DECLARE_SkipListSet
580 CDSUNIT_DECLARE_EllenBinTreeSet