3 #include "cppunit/thread.h"
4 #include "set2/set_types.h"
5 #include <algorithm> // random_shuffle
9 # define TEST_SET(X) void X() { test<SetTypes<key_type, value_type>::X >() ; }
10 # define TEST_SET_EXTRACT(X) void X() { test_extract<SetTypes<key_type, value_type>::X >() ; }
11 # define TEST_SET_NOLF(X) void X() { test_nolf<SetTypes<key_type, value_type>::X >() ; }
12 # define TEST_SET_NOLF_EXTRACT(X) void X() { test_nolf_extract<SetTypes<key_type, value_type>::X >() ; }
15 static size_t c_nSetSize = 1000000 ; // max set size
16 static size_t c_nInsThreadCount = 4 ; // insert thread count
17 static size_t c_nDelThreadCount = 4 ; // delete thread count
18 static size_t c_nExtractThreadCount = 4 ; // extract thread count
19 static size_t c_nMaxLoadFactor = 8 ; // maximum load factor
20 static bool c_bPrintGCState = true;
29 key_thread( size_t key, size_t threadNo )
38 typedef SetTypes<key_thread, size_t>::key_val key_value_pair;
42 struct cmp<key_thread> {
43 int operator ()(key_thread const& k1, key_thread const& k2) const
45 if ( k1.nKey < k2.nKey )
47 if ( k1.nKey > k2.nKey )
49 if ( k1.nThread < k2.nThread )
51 if ( k1.nThread > k2.nThread )
55 int operator ()(key_thread const& k1, size_t k2) const
63 int operator ()(size_t k1, key_thread const& k2) const
77 struct less<set2::key_thread>
79 bool operator()(set2::key_thread const& k1, set2::key_thread const& k2) const
81 if ( k1.nKey <= k2.nKey )
82 return k1.nKey < k2.nKey || k1.nThread < k2.nThread;
88 struct hash<set2::key_thread>
90 typedef size_t result_type;
91 typedef set2::key_thread argument_type;
93 size_t operator()( set2::key_thread const& k ) const
95 return std::hash<size_t>()(k.nKey);
97 size_t operator()( size_t k ) const
99 return std::hash<size_t>()(k);
106 inline size_t hash_value( set2::key_thread const& k )
108 return std::hash<size_t>()( k.nKey );
112 struct hash<set2::key_thread>
114 typedef size_t result_type;
115 typedef set2::key_thread argument_type;
117 size_t operator()(set2::key_thread const& k) const
119 return boost::hash<size_t>()( k.nKey );
121 size_t operator()(size_t k) const
123 return boost::hash<size_t>()( k );
130 template <typename Set>
131 static inline void check_before_clear( Set& s )
134 template <typename GC, typename Key, typename T, typename Traits>
135 static inline void check_before_clear( cds::container::EllenBinTreeSet<GC, Key, T, Traits>& s )
137 CPPUNIT_CHECK_CURRENT( s.check_consistency() );
140 class Set_DelOdd: public CppUnitMini::TestCase
142 std::vector<size_t> m_arrData;
145 typedef key_thread key_type;
146 typedef size_t value_type;
148 atomics::atomic<size_t> m_nInsThreadCount;
150 // Inserts keys from [0..N)
152 class InsertThread: public CppUnitMini::TestThread
156 virtual InsertThread * clone()
158 return new InsertThread( *this );
163 template <typename Q>
164 void operator()( bool bNew, key_value_pair const&, Q const& )
168 size_t m_nInsertSuccess;
169 size_t m_nInsertFailed;
172 InsertThread( CppUnitMini::ThreadPool& pool, Set& rMap )
173 : CppUnitMini::TestThread( pool )
176 InsertThread( InsertThread& src )
177 : CppUnitMini::TestThread( src )
181 Set_DelOdd& getTest()
183 return reinterpret_cast<Set_DelOdd&>( m_Pool.m_Test );
186 virtual void init() { cds::threading::Manager::attachThread() ; }
187 virtual void fini() { cds::threading::Manager::detachThread() ; }
196 std::vector<size_t>& arrData = getTest().m_arrData;
197 for ( size_t i = 0; i < arrData.size(); ++i ) {
198 if ( rSet.insert( key_type( arrData[i], m_nThreadNo )))
205 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
206 if ( arrData[i] & 1 ) {
207 rSet.ensure( key_type( arrData[i], m_nThreadNo ), f );
211 getTest().m_nInsThreadCount.fetch_sub( 1, atomics::memory_order_release );
216 bool operator()( key_type const& k1, key_type const& k2 ) const
218 return k1.nKey == k2.nKey;
220 bool operator()( size_t k1, key_type const& k2 ) const
222 return k1 == k2.nKey;
224 bool operator()( key_type const& k1, size_t k2 ) const
226 return k1.nKey == k2;
228 bool operator ()( key_value_pair const& k1, key_value_pair const& k2 ) const
230 return operator()( k1.key, k2.key );
232 bool operator ()( key_value_pair const& k1, key_type const& k2 ) const
234 return operator()( k1.key, k2 );
236 bool operator ()( key_type const& k1, key_value_pair const& k2 ) const
238 return operator()( k1, k2.key );
240 bool operator ()( key_value_pair const& k1, size_t k2 ) const
242 return operator()( k1.key, k2 );
244 bool operator ()( size_t k1, key_value_pair const& k2 ) const
246 return operator()( k1, k2.key );
251 bool operator()( key_type const& k1, key_type const& k2 ) const
253 return k1.nKey < k2.nKey;
255 bool operator()( size_t k1, key_type const& k2 ) const
259 bool operator()( key_type const& k1, size_t k2 ) const
263 bool operator ()( key_value_pair const& k1, key_value_pair const& k2 ) const
265 return operator()( k1.key, k2.key );
267 bool operator ()( key_value_pair const& k1, key_type const& k2 ) const
269 return operator()( k1.key, k2 );
271 bool operator ()( key_type const& k1, key_value_pair const& k2 ) const
273 return operator()( k1, k2.key );
275 bool operator ()( key_value_pair const& k1, size_t k2 ) const
277 return operator()( k1.key, k2 );
279 bool operator ()( size_t k1, key_value_pair const& k2 ) const
281 return operator()( k1, k2.key );
284 typedef key_equal equal_to;
287 // Deletes odd keys from [0..N)
289 class DeleteThread: public CppUnitMini::TestThread
293 virtual DeleteThread * clone()
295 return new DeleteThread( *this );
298 size_t m_nDeleteSuccess;
299 size_t m_nDeleteFailed;
302 DeleteThread( CppUnitMini::ThreadPool& pool, Set& rMap )
303 : CppUnitMini::TestThread( pool )
306 DeleteThread( DeleteThread& src )
307 : CppUnitMini::TestThread( src )
311 Set_DelOdd& getTest()
313 return reinterpret_cast<Set_DelOdd&>( m_Pool.m_Test );
316 virtual void init() { cds::threading::Manager::attachThread() ; }
317 virtual void fini() { cds::threading::Manager::detachThread() ; }
326 std::vector<size_t>& arrData = getTest().m_arrData;
327 if ( m_nThreadNo & 1 ) {
328 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
329 for ( size_t i = 0; i < arrData.size(); ++i ) {
330 if ( arrData[i] & 1 ) {
331 if ( rSet.erase_with( arrData[i], key_less() ))
337 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
342 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
343 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
344 if ( arrData[i] & 1 ) {
345 if ( rSet.erase_with( arrData[i], key_less() ))
351 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
358 // Extracts odd keys from [0..N)
359 template <typename GC, class Set>
360 class ExtractThread: public CppUnitMini::TestThread
364 virtual ExtractThread * clone()
366 return new ExtractThread( *this );
369 size_t m_nExtractSuccess;
370 size_t m_nExtractFailed;
373 ExtractThread( CppUnitMini::ThreadPool& pool, Set& rMap )
374 : CppUnitMini::TestThread( pool )
377 ExtractThread( ExtractThread& src )
378 : CppUnitMini::TestThread( src )
382 Set_DelOdd& getTest()
384 return reinterpret_cast<Set_DelOdd&>( m_Pool.m_Test );
387 virtual void init() { cds::threading::Manager::attachThread() ; }
388 virtual void fini() { cds::threading::Manager::detachThread() ; }
395 m_nExtractFailed = 0;
397 typename Set::guarded_ptr gp;
399 std::vector<size_t>& arrData = getTest().m_arrData;
400 if ( m_nThreadNo & 1 ) {
401 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
402 for ( size_t i = 0; i < arrData.size(); ++i ) {
403 if ( arrData[i] & 1 ) {
404 gp = rSet.extract_with( arrData[i], key_less());
411 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
416 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
417 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
418 if ( arrData[i] & 1 ) {
419 gp = rSet.extract_with( arrData[i], key_less());
426 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
433 template <typename RCU, class Set>
434 class ExtractThread< cds::urcu::gc<RCU>, Set >: public CppUnitMini::TestThread
438 virtual ExtractThread * clone()
440 return new ExtractThread( *this );
443 size_t m_nExtractSuccess;
444 size_t m_nExtractFailed;
447 ExtractThread( CppUnitMini::ThreadPool& pool, Set& rMap )
448 : CppUnitMini::TestThread( pool )
451 ExtractThread( ExtractThread& src )
452 : CppUnitMini::TestThread( src )
456 Set_DelOdd& getTest()
458 return reinterpret_cast<Set_DelOdd&>( m_Pool.m_Test );
461 virtual void init() { cds::threading::Manager::attachThread() ; }
462 virtual void fini() { cds::threading::Manager::detachThread() ; }
469 m_nExtractFailed = 0;
471 typename Set::exempt_ptr xp;
473 std::vector<size_t>& arrData = getTest().m_arrData;
474 if ( m_nThreadNo & 1 ) {
475 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
476 for ( size_t i = 0; i < arrData.size(); ++i ) {
477 if ( arrData[i] & 1 ) {
478 if ( Set::c_bExtractLockExternal ) {
479 typename Set::rcu_lock l;
480 xp = rSet.extract_with( arrData[i], key_less() );
487 xp = rSet.extract_with( arrData[i], key_less() );
496 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
501 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
502 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
503 if ( arrData[i] & 1 ) {
504 if ( Set::c_bExtractLockExternal ) {
505 typename Set::rcu_lock l;
506 xp = rSet.extract_with( arrData[i], key_less() );
513 xp = rSet.extract_with( arrData[i], key_less() );
522 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
531 void do_test( size_t nLoadFactor )
533 Set testSet( c_nSetSize, nLoadFactor );
534 do_test_with( testSet );
539 void do_test_extract( size_t nLoadFactor )
541 Set testSet( c_nSetSize, nLoadFactor );
542 do_test_extract_with( testSet );
547 void do_test_with( Set& testSet )
549 typedef InsertThread<Set> insert_thread;
550 typedef DeleteThread<Set> delete_thread;
552 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
554 CppUnitMini::ThreadPool pool( *this );
555 pool.add( new insert_thread( pool, testSet ), c_nInsThreadCount );
556 pool.add( new delete_thread( pool, testSet ), c_nDelThreadCount ? c_nDelThreadCount : cds::OS::topology::processor_count());
558 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
560 size_t nInsertSuccess = 0;
561 size_t nInsertFailed = 0;
562 size_t nDeleteSuccess = 0;
563 size_t nDeleteFailed = 0;
564 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
565 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
567 nInsertSuccess += pThread->m_nInsertSuccess;
568 nInsertFailed += pThread->m_nInsertFailed;
571 delete_thread * p = static_cast<delete_thread *>( *it );
572 nDeleteSuccess += p->m_nDeleteSuccess;
573 nDeleteFailed += p->m_nDeleteFailed;
577 CPPUNIT_CHECK( nInsertSuccess == c_nSetSize * c_nInsThreadCount );
578 CPPUNIT_CHECK( nInsertFailed == 0 );
580 CPPUNIT_MSG( " Totals (success/failed): \n\t"
581 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
582 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
587 void do_test_extract_with( Set& testSet )
589 typedef InsertThread<Set> insert_thread;
590 typedef DeleteThread<Set> delete_thread;
591 typedef ExtractThread< typename Set::gc, Set > extract_thread;
593 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
595 CppUnitMini::ThreadPool pool( *this );
596 pool.add( new insert_thread( pool, testSet ), c_nInsThreadCount );
597 if ( c_nDelThreadCount )
598 pool.add( new delete_thread( pool, testSet ), c_nDelThreadCount );
599 if ( c_nExtractThreadCount )
600 pool.add( new extract_thread( pool, testSet ), c_nExtractThreadCount );
602 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
604 size_t nInsertSuccess = 0;
605 size_t nInsertFailed = 0;
606 size_t nDeleteSuccess = 0;
607 size_t nDeleteFailed = 0;
608 size_t nExtractSuccess = 0;
609 size_t nExtractFailed = 0;
610 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
611 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
613 nInsertSuccess += pThread->m_nInsertSuccess;
614 nInsertFailed += pThread->m_nInsertFailed;
617 delete_thread * p = dynamic_cast<delete_thread *>( *it );
619 nDeleteSuccess += p->m_nDeleteSuccess;
620 nDeleteFailed += p->m_nDeleteFailed;
623 extract_thread * pExt = dynamic_cast<extract_thread *>( *it );
625 nExtractSuccess += pExt->m_nExtractSuccess;
626 nExtractFailed += pExt->m_nExtractFailed;
631 CPPUNIT_CHECK( nInsertSuccess == c_nSetSize * c_nInsThreadCount );
632 CPPUNIT_CHECK( nInsertFailed == 0 );
634 CPPUNIT_MSG( " Totals (success/failed): \n\t"
635 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
636 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
637 << " Extract=" << nExtractSuccess << '/' << nExtractFailed << "\n\t"
641 template <typename Set>
642 void analyze( Set& testSet )
644 // All even keys must be in the set
646 CPPUNIT_MSG( " Check even keys..." );
647 size_t nErrorCount = 0;
648 for ( size_t n = 0; n < c_nSetSize; n +=2 ) {
649 for ( size_t i = 0; i < c_nInsThreadCount; ++i ) {
650 if ( !testSet.find( key_type(n, i) ) ) {
651 if ( ++nErrorCount < 10 ) {
652 CPPUNIT_MSG( "key " << n << "-" << i << " is not found!");
657 CPPUNIT_CHECK_EX( nErrorCount == 0, "Totals: " << nErrorCount << " keys is not found");
660 check_before_clear( testSet );
662 CPPUNIT_MSG( " Clear map (single-threaded)..." );
663 cds::OS::Timer timer;
665 CPPUNIT_MSG( " Duration=" << timer.duration() );
666 CPPUNIT_CHECK_EX( testSet.empty(), ((long long) testSet.size()) );
668 additional_check( testSet );
669 print_stat( testSet );
670 additional_cleanup( testSet );
676 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
677 << " delete thread count=" << c_nDelThreadCount
678 << " set size=" << c_nSetSize
681 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
682 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
683 do_test<Set>( nLoadFactor );
684 if ( c_bPrintGCState )
692 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
693 << " delete thread count=" << c_nDelThreadCount
694 << " extract thread count=" << c_nExtractThreadCount
695 << " set size=" << c_nSetSize
698 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
699 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
700 do_test_extract<Set>( nLoadFactor );
701 if ( c_bPrintGCState )
709 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
710 << " delete thread count=" << c_nDelThreadCount
711 << " set size=" << c_nSetSize
720 if ( c_bPrintGCState )
725 void test_nolf_extract()
727 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
728 << " delete thread count=" << c_nDelThreadCount
729 << " extract thread count=" << c_nExtractThreadCount
730 << " set size=" << c_nSetSize
735 do_test_extract_with( s );
739 if ( c_bPrintGCState )
743 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
744 c_nSetSize = cfg.getULong("MapSize", static_cast<unsigned long>(c_nSetSize) );
745 c_nInsThreadCount = cfg.getULong("InsThreadCount", static_cast<unsigned long>(c_nInsThreadCount) );
746 c_nDelThreadCount = cfg.getULong("DelThreadCount", static_cast<unsigned long>(c_nDelThreadCount) );
747 c_nExtractThreadCount = cfg.getULong("ExtractThreadCount", static_cast<unsigned long>(c_nExtractThreadCount) );
748 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", static_cast<unsigned long>(c_nMaxLoadFactor) );
749 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
751 if ( c_nInsThreadCount == 0 )
752 c_nInsThreadCount = cds::OS::topology::processor_count();
753 if ( c_nDelThreadCount == 0 && c_nExtractThreadCount == 0 ) {
754 c_nExtractThreadCount = cds::OS::topology::processor_count() / 2;
755 c_nDelThreadCount = cds::OS::topology::processor_count() - c_nExtractThreadCount;
758 m_arrData.resize( c_nSetSize );
759 for ( size_t i = 0; i < c_nSetSize; ++i )
761 std::random_shuffle( m_arrData.begin(), m_arrData.end() );
764 # include "set2/set_defs.h"
765 CDSUNIT_DECLARE_MichaelSet
766 CDSUNIT_DECLARE_SplitList
767 //CDSUNIT_DECLARE_StripedSet
768 //CDSUNIT_DECLARE_RefinableSet
769 CDSUNIT_DECLARE_CuckooSet
770 CDSUNIT_DECLARE_SkipListSet
771 CDSUNIT_DECLARE_EllenBinTreeSet
772 //CDSUNIT_DECLARE_StdSet
774 CPPUNIT_TEST_SUITE_( Set_DelOdd, "Map_DelOdd" )
775 CDSUNIT_TEST_MichaelSet
776 CDSUNIT_TEST_SplitList
777 CDSUNIT_TEST_SkipListSet
778 CDSUNIT_TEST_EllenBinTreeSet
779 //CDSUNIT_TEST_StripedSet
780 //CDSUNIT_TEST_RefinableSet
781 CDSUNIT_TEST_CuckooSet
782 //CDSUNIT_TEST_StdSet
783 CPPUNIT_TEST_SUITE_END()
786 CPPUNIT_TEST_SUITE_REGISTRATION( Set_DelOdd );