5 #include <mutex> //unique_lock
7 #include "set2/set_type.h"
8 #include "cppunit/thread.h"
9 #include <cds/sync/spinlock.h>
13 #define TEST_CASE(TAG, X) void X();
15 class Set_InsDel_func: public CppUnitMini::TestCase
18 size_t c_nSetSize = 1000000; // set 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 ensure thread
22 size_t c_nThreadPassCount = 4; // pass count for each thread
23 size_t c_nMaxLoadFactor = 8; // maximum load factor
26 size_t c_nCuckooInitialSize = 1024;// initial size for CuckooSet
27 size_t c_nCuckooProbesetSize = 16; // CuckooSet probeset size (only for list-based probeset)
28 size_t c_nCuckooProbesetThreshold = 0; // CUckooSet probeset threshold (0 - use default)
30 size_t c_nFeldmanSet_HeadBits = 10;
31 size_t c_nFeldmanSet_ArrayBits = 4;
33 size_t c_nLoadFactor = 2;
36 typedef size_t key_type;
40 atomics::atomic<size_t> nUpdateCall;
41 bool volatile bInitialized;
42 cds::OS::ThreadId threadId ; // insert 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 )
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 = v.bInitialized;
82 class Inserter: public CppUnitMini::TestThread
85 typedef typename Set::value_type keyval_type;
87 virtual Inserter * clone()
89 return new Inserter( *this );
92 struct insert_functor {
93 size_t nTestFunctorRef;
99 void operator()( keyval_type& val )
101 std::unique_lock< typename value_type::lock_type> ac( val.val.m_access );
103 val.val.nKey = val.key;
104 val.val.nData = val.key * 8;
107 val.val.bInitialized = true;
112 size_t m_nInsertSuccess;
113 size_t m_nInsertFailed;
115 size_t m_nTestFunctorRef;
118 Inserter( CppUnitMini::ThreadPool& pool, Set& rSet )
119 : CppUnitMini::TestThread( pool )
122 Inserter( Inserter& src )
123 : CppUnitMini::TestThread( src )
127 Set_InsDel_func& getTest()
129 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
132 virtual void init() { cds::threading::Manager::attachThread() ; }
133 virtual void fini() { cds::threading::Manager::detachThread() ; }
141 m_nTestFunctorRef = 0;
143 size_t * pKeyFirst = getTest().m_pKeyFirst;
144 size_t * pKeyLast = getTest().m_pKeyLast;
145 size_t const nPassCount = getTest().c_nThreadPassCount;
147 // func is passed by reference
150 if ( m_nThreadNo & 1 ) {
151 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
152 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
153 if ( rSet.insert( *p, std::ref(func) ) )
161 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
162 for ( size_t * p = pKeyLast - 1; p >= pKeyFirst; --p ) {
163 if ( rSet.insert( *p, std::ref(func) ) )
171 m_nTestFunctorRef = func.nTestFunctorRef;
176 class Updater: public CppUnitMini::TestThread
179 typedef typename Set::value_type keyval_type;
181 virtual Updater * clone()
183 return new Updater( *this );
186 struct update_functor {
195 void operator()( bool bNew, keyval_type& val, size_t /*nKey*/ )
197 std::unique_lock<typename value_type::lock_type> ac( val.val.m_access );
198 if ( !val.val.bInitialized )
200 val.val.nKey = val.key;
201 val.val.nData = val.key * 8;
202 val.val.bInitialized = true;
209 val.val.nUpdateCall.fetch_add( 1, atomics::memory_order_relaxed );
214 void operator()( keyval_type& cur, keyval_type * old )
216 operator()( old == nullptr, cur, 0 );
220 update_functor(const update_functor& );
224 size_t m_nUpdateFailed;
225 size_t m_nUpdateCreated;
226 size_t m_nUpdateExisted;
227 size_t m_nFunctorCreated;
228 size_t m_nFunctorModified;
231 Updater( CppUnitMini::ThreadPool& pool, Set& rSet )
232 : CppUnitMini::TestThread( pool )
235 Updater( Updater& src )
236 : CppUnitMini::TestThread( src )
240 Set_InsDel_func& getTest()
242 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
245 virtual void init() { cds::threading::Manager::attachThread() ; }
246 virtual void fini() { cds::threading::Manager::detachThread() ; }
256 size_t * pKeyFirst = getTest().m_pKeyFirst;
257 size_t * pKeyLast = getTest().m_pKeyLast;
258 size_t const nPassCount = getTest().c_nThreadPassCount;
262 if ( m_nThreadNo & 1 ) {
263 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
264 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
265 std::pair<bool, bool> ret = rSet.update( *p, std::ref( func ), true );
278 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
279 for ( size_t * p = pKeyLast - 1 ; p >= pKeyFirst; --p ) {
280 std::pair<bool, bool> ret = rSet.update( *p, std::ref( func ), true );
293 m_nFunctorCreated = func.nCreated;
294 m_nFunctorModified = func.nModified;
299 class Deleter: public CppUnitMini::TestThread
302 typedef typename Set::value_type keyval_type;
304 virtual Deleter * clone()
306 return new Deleter( *this );
309 struct value_container
322 struct erase_functor {
323 value_container m_cnt;
325 void operator ()( keyval_type const& itm )
327 keyval_type& item = const_cast<keyval_type&>(itm);
331 std::unique_lock< typename value_type::lock_type> ac( item.val.m_access );
332 if ( item.val.bInitialized ) {
333 if ( m_cnt.nKeyExpected == item.val.nKey && m_cnt.nKeyExpected * 8 == item.val.nData )
334 ++m_cnt.nSuccessItem;
345 cds::backoff::yield()();
351 size_t m_nDeleteSuccess;
352 size_t m_nDeleteFailed;
354 size_t m_nValueSuccess;
355 size_t m_nValueFailed;
358 Deleter( CppUnitMini::ThreadPool& pool, Set& rSet )
359 : CppUnitMini::TestThread( pool )
362 Deleter( Deleter& src )
363 : CppUnitMini::TestThread( src )
367 Set_InsDel_func& getTest()
369 return reinterpret_cast<Set_InsDel_func&>( m_Pool.m_Test );
372 virtual void init() { cds::threading::Manager::attachThread() ; }
373 virtual void fini() { cds::threading::Manager::detachThread() ; }
382 size_t * pKeyFirst = getTest().m_pKeyFirst;
383 size_t * pKeyLast = getTest().m_pKeyLast;
384 size_t const nPassCount = getTest().c_nThreadPassCount;
388 if ( m_nThreadNo & 1 ) {
389 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
390 for ( size_t * p = pKeyFirst; p < pKeyLast; ++p ) {
391 func.m_cnt.nKeyExpected = *p;
392 if ( rSet.erase( *p, std::ref(func) ))
400 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
401 for ( size_t * p = pKeyLast - 1; p >= pKeyFirst; --p ) {
402 func.m_cnt.nKeyExpected = *p;
403 if ( rSet.erase( *p, std::ref(func) ))
411 m_nValueSuccess = func.m_cnt.nSuccessItem;
412 m_nValueFailed = func.m_cnt.nFailedItem;
419 void do_test( Set& testSet )
421 typedef Inserter<Set> InserterThread;
422 typedef Deleter<Set> DeleterThread;
423 typedef Updater<Set> UpdaterThread;
425 m_pKeyArr = new size_t[ c_nSetSize ];
426 m_pKeyFirst = m_pKeyArr;
427 m_pKeyLast = m_pKeyFirst + c_nSetSize;
428 for ( size_t i = 0; i < c_nSetSize; ++i )
430 shuffle( m_pKeyFirst, m_pKeyLast );
432 cds::OS::Timer timer;
434 CppUnitMini::ThreadPool pool( *this );
435 pool.add( new InserterThread( pool, testSet ), c_nInsertThreadCount );
436 pool.add( new DeleterThread( pool, testSet ), c_nDeleteThreadCount );
437 pool.add( new UpdaterThread( pool, testSet ), c_nUpdateThreadCount );
439 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;
483 " Totals: Ins succ=" << nInsertSuccess
484 << " Del succ=" << nDeleteSuccess << "\n"
485 << " : Ins fail=" << nInsertFailed
486 << " Del fail=" << nDeleteFailed << "\n"
487 << " : Update succ=" << (nUpdateCreated + nUpdateModified) << " fail=" << nUpdateFailed
488 << " create=" << nUpdateCreated << " modify=" << nUpdateModified << "\n"
489 << " Set size=" << testSet.size()
492 CPPUNIT_CHECK_EX( nDelValueFailed == 0, "Functor del failed=" << nDelValueFailed );
493 CPPUNIT_CHECK_EX( nDelValueSuccess == nDeleteSuccess, "Delete success=" << nDeleteSuccess << " functor=" << nDelValueSuccess );
495 CPPUNIT_CHECK( nUpdateFailed == 0 );
497 CPPUNIT_CHECK_EX( nUpdateCreated == nEnsFuncCreated, "Update created=" << nUpdateCreated << " functor=" << nEnsFuncCreated );
498 CPPUNIT_CHECK_EX( nUpdateModified == nEnsFuncModified, "Update modified=" << nUpdateModified << " functor=" << nEnsFuncModified );
500 // nTestFunctorRef is call count of insert functor
501 CPPUNIT_CHECK_EX( nTestFunctorRef == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nTestFunctorRef=" << nTestFunctorRef );
503 CPPUNIT_MSG( " Clear set (single-threaded)..." );
506 CPPUNIT_MSG( " Duration=" << timer.duration() );
507 CPPUNIT_CHECK( testSet.empty() );
509 additional_check( testSet );
510 print_stat( testSet );
512 additional_cleanup( testSet );
518 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
519 << " delete=" << c_nDeleteThreadCount
520 << " ensure=" << c_nUpdateThreadCount
521 << " pass count=" << c_nThreadPassCount
522 << " set size=" << c_nSetSize
525 if ( Set::c_bLoadFactorDepended ) {
526 for ( c_nLoadFactor = 1; c_nLoadFactor <= c_nMaxLoadFactor; c_nLoadFactor *= 2 ) {
527 CPPUNIT_MSG(" LoadFactor = " << c_nLoadFactor );
530 if ( c_bPrintGCState )
537 if ( c_bPrintGCState )
542 void setUpParams( const CppUnitMini::TestCfg& cfg );
544 # include "set2/set_defs.h"
545 CDSUNIT_DECLARE_MichaelSet
546 CDSUNIT_DECLARE_SkipListSet
547 CDSUNIT_DECLARE_SplitList
548 CDSUNIT_DECLARE_StripedSet
549 CDSUNIT_DECLARE_RefinableSet
550 CDSUNIT_DECLARE_CuckooSet
551 CDSUNIT_DECLARE_EllenBinTreeSet
552 CDSUNIT_DECLARE_FeldmanHashSet_fixed
553 CDSUNIT_DECLARE_FeldmanHashSet_city
555 CPPUNIT_TEST_SUITE_(Set_InsDel_func, "Map_InsDel_func")
556 CDSUNIT_TEST_MichaelSet
557 CDSUNIT_TEST_SplitList
558 CDSUNIT_TEST_SkipListSet
559 CDSUNIT_TEST_FeldmanHashSet_fixed
560 CDSUNIT_TEST_FeldmanHashSet_city
561 CDSUNIT_TEST_EllenBinTreeSet
562 CDSUNIT_TEST_StripedSet
563 CDSUNIT_TEST_RefinableSet
564 CDSUNIT_TEST_CuckooSet
565 CPPUNIT_TEST_SUITE_END();