3 #include "cppunit/thread.h"
4 #include "map2/map_types.h"
5 #include <algorithm> // random_shuffle
9 # define TEST_MAP(X) void X() { test<MapTypes<key_type, value_type>::X >(); }
10 # define TEST_MAP_EXTRACT(X) void X() { test_extract<MapTypes<key_type, value_type>::X >(); }
11 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >(); }
12 # define TEST_MAP_NOLF_EXTRACT(X) void X() { test_nolf_extract<MapTypes<key_type, value_type>::X >(); }
15 static size_t c_nMapSize = 1000000 ; // max map 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 MapTypes<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<map2::key_thread>
79 bool operator()(map2::key_thread const& k1, map2::key_thread const& k2) const
81 if ( k1.nKey <= k2.nKey )
82 return k1.nKey < k2.nKey || k1.nThread < k2.nThread;
88 struct hash<map2::key_thread>
90 typedef size_t result_type;
91 typedef map2::key_thread argument_type;
93 size_t operator()( map2::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);
107 struct hash<map2::key_thread>
109 typedef size_t result_type;
110 typedef map2::key_thread argument_type;
112 size_t operator()(map2::key_thread const& k) const
114 return boost::hash<size_t>()( k.nKey );
116 size_t operator()(size_t k) const
118 return boost::hash<size_t>()( k );
126 template <typename Map>
127 static inline void check_before_clear( Map& s )
130 template <typename GC, typename Key, typename T, typename Traits>
131 static inline void check_before_clear( cds::container::EllenBinTreeMap<GC, Key, T, Traits>& s )
133 CPPUNIT_CHECK_CURRENT( s.check_consistency() );
136 class Map_DelOdd: public CppUnitMini::TestCase
138 std::vector<size_t> m_arrData;
141 typedef key_thread key_type;
142 typedef size_t value_type;
143 typedef std::pair<key_type const, value_type> pair_type;
145 atomics::atomic<size_t> m_nInsThreadCount;
147 // Inserts keys from [0..N)
149 class InsertThread: public CppUnitMini::TestThread
153 virtual InsertThread * clone()
155 return new InsertThread( *this );
160 template <typename Q>
161 void operator()( bool bNew, Q const& )
165 size_t m_nInsertSuccess;
166 size_t m_nInsertFailed;
169 InsertThread( CppUnitMini::ThreadPool& pool, Map& rMap )
170 : CppUnitMini::TestThread( pool )
173 InsertThread( InsertThread& src )
174 : CppUnitMini::TestThread( src )
178 Map_DelOdd& getTest()
180 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
183 virtual void init() { cds::threading::Manager::attachThread() ; }
184 virtual void fini() { cds::threading::Manager::detachThread() ; }
193 std::vector<size_t>& arrData = getTest().m_arrData;
194 for ( size_t i = 0; i < arrData.size(); ++i ) {
195 if ( rMap.insert( key_type( arrData[i], m_nThreadNo )))
202 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
203 if ( arrData[i] & 1 ) {
204 rMap.ensure( key_type( arrData[i], m_nThreadNo ), f );
208 getTest().m_nInsThreadCount.fetch_sub( 1, atomics::memory_order_acquire );
213 bool operator()( key_type const& k1, key_type const& k2 ) const
215 return k1.nKey == k2.nKey;
217 bool operator()( size_t k1, key_type const& k2 ) const
219 return k1 == k2.nKey;
221 bool operator()( key_type const& k1, size_t k2 ) const
223 return k1.nKey == k2;
228 bool operator()( key_type const& k1, key_type const& k2 ) const
230 return k1.nKey < k2.nKey;
232 bool operator()( size_t k1, key_type const& k2 ) const
236 bool operator()( key_type const& k1, size_t k2 ) const
241 typedef key_equal equal_to;
244 // Deletes odd keys from [0..N)
246 class DeleteThread: public CppUnitMini::TestThread
250 virtual DeleteThread * clone()
252 return new DeleteThread( *this );
255 size_t m_nDeleteSuccess;
256 size_t m_nDeleteFailed;
259 DeleteThread( CppUnitMini::ThreadPool& pool, Map& rMap )
260 : CppUnitMini::TestThread( pool )
263 DeleteThread( DeleteThread& src )
264 : CppUnitMini::TestThread( src )
268 Map_DelOdd& getTest()
270 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
273 virtual void init() { cds::threading::Manager::attachThread() ; }
274 virtual void fini() { cds::threading::Manager::detachThread() ; }
283 std::vector<size_t>& arrData = getTest().m_arrData;
284 if ( m_nThreadNo & 1 ) {
285 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
286 for ( size_t i = 0; i < arrData.size(); ++i ) {
287 if ( arrData[i] & 1 ) {
288 if ( rMap.erase_with( arrData[i], key_less() ))
294 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
299 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
300 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
301 if ( arrData[i] & 1 ) {
302 if ( rMap.erase_with( arrData[i], key_less() ))
308 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
315 // Deletes odd keys from [0..N)
316 template <class GC, class Map >
317 class ExtractThread: public CppUnitMini::TestThread
321 virtual ExtractThread * clone()
323 return new ExtractThread( *this );
326 size_t m_nDeleteSuccess;
327 size_t m_nDeleteFailed;
330 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
331 : CppUnitMini::TestThread( pool )
334 ExtractThread( ExtractThread& src )
335 : CppUnitMini::TestThread( src )
339 Map_DelOdd& getTest()
341 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
344 virtual void init() { cds::threading::Manager::attachThread() ; }
345 virtual void fini() { cds::threading::Manager::detachThread() ; }
354 typename Map::guarded_ptr gp;
356 std::vector<size_t>& arrData = getTest().m_arrData;
357 if ( m_nThreadNo & 1 ) {
358 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
359 for ( size_t i = 0; i < arrData.size(); ++i ) {
360 if ( arrData[i] & 1 ) {
361 if ( rMap.extract_with( gp, arrData[i], key_less() ))
367 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
372 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
373 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
374 if ( arrData[i] & 1 ) {
375 if ( rMap.extract_with( gp, arrData[i], key_less() ))
381 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
388 template <class RCU, class Map >
389 class ExtractThread< cds::urcu::gc<RCU>, Map > : public CppUnitMini::TestThread
393 virtual ExtractThread * clone()
395 return new ExtractThread( *this );
398 size_t m_nDeleteSuccess;
399 size_t m_nDeleteFailed;
402 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
403 : CppUnitMini::TestThread( pool )
406 ExtractThread( ExtractThread& src )
407 : CppUnitMini::TestThread( src )
411 Map_DelOdd& getTest()
413 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
416 virtual void init() { cds::threading::Manager::attachThread() ; }
417 virtual void fini() { cds::threading::Manager::detachThread() ; }
426 typename Map::exempt_ptr xp;
428 std::vector<size_t>& arrData = getTest().m_arrData;
429 if ( m_nThreadNo & 1 ) {
430 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
431 for ( size_t i = 0; i < arrData.size(); ++i ) {
432 if ( arrData[i] & 1 ) {
433 if ( Map::c_bExtractLockExternal ) {
435 typename Map::rcu_lock l;
436 if ( rMap.extract_with( xp, arrData[i], key_less() )) {
445 if ( rMap.extract_with( xp, arrData[i], key_less() )) {
454 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
459 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
460 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
461 if ( arrData[i] & 1 ) {
462 if ( Map::c_bExtractLockExternal ) {
464 typename Map::rcu_lock l;
465 if ( rMap.extract_with( xp, arrData[i], key_less() ))
473 if ( rMap.extract_with( xp, arrData[i], key_less() )) {
482 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
491 void do_test( size_t nLoadFactor )
493 Map testMap( c_nMapSize, nLoadFactor );
494 do_test_with( testMap );
498 void do_test_extract( size_t nLoadFactor )
500 Map testMap( c_nMapSize, nLoadFactor );
501 do_test_extract_with( testMap );
505 void do_test_with( Map& testMap )
507 typedef InsertThread<Map> insert_thread;
508 typedef DeleteThread<Map> delete_thread;
510 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
512 CppUnitMini::ThreadPool pool( *this );
513 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
514 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount ? c_nDelThreadCount : cds::OS::topology::processor_count());
516 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
518 size_t nInsertSuccess = 0;
519 size_t nInsertFailed = 0;
520 size_t nDeleteSuccess = 0;
521 size_t nDeleteFailed = 0;
522 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
523 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
525 nInsertSuccess += pThread->m_nInsertSuccess;
526 nInsertFailed += pThread->m_nInsertFailed;
529 delete_thread * p = static_cast<delete_thread *>( *it );
530 nDeleteSuccess += p->m_nDeleteSuccess;
531 nDeleteFailed += p->m_nDeleteFailed;
535 CPPUNIT_MSG( " Totals (success/failed): \n\t"
536 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
537 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
539 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
540 CPPUNIT_CHECK( nInsertFailed == 0 );
546 void do_test_extract_with( Map& testMap )
548 typedef InsertThread<Map> insert_thread;
549 typedef DeleteThread<Map> delete_thread;
550 typedef ExtractThread< typename Map::gc, Map > extract_thread;
552 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
554 CppUnitMini::ThreadPool pool( *this );
555 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
556 if ( c_nDelThreadCount )
557 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount );
558 if ( c_nExtractThreadCount )
559 pool.add( new extract_thread( pool, testMap ), c_nExtractThreadCount );
561 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
563 size_t nInsertSuccess = 0;
564 size_t nInsertFailed = 0;
565 size_t nDeleteSuccess = 0;
566 size_t nDeleteFailed = 0;
567 size_t nExtractSuccess = 0;
568 size_t nExtractFailed = 0;
569 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
570 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
572 nInsertSuccess += pThread->m_nInsertSuccess;
573 nInsertFailed += pThread->m_nInsertFailed;
576 delete_thread * p = dynamic_cast<delete_thread *>( *it );
578 nDeleteSuccess += p->m_nDeleteSuccess;
579 nDeleteFailed += p->m_nDeleteFailed;
582 extract_thread * pExtract = dynamic_cast<extract_thread *>( *it );
584 nExtractSuccess += pExtract->m_nDeleteSuccess;
585 nExtractFailed += pExtract->m_nDeleteFailed;
590 CPPUNIT_MSG( " Totals (success/failed): \n\t"
591 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
592 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
593 << " Extract=" << nExtractSuccess << '/' << nExtractFailed << "\n\t"
595 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
596 CPPUNIT_CHECK( nInsertFailed == 0 );
602 void analyze( Map& testMap )
604 cds::OS::Timer timer;
606 // All even keys must be in the map
608 size_t nErrorCount = 0;
609 CPPUNIT_MSG( " Check even keys..." );
610 for ( size_t n = 0; n < c_nMapSize; n +=2 ) {
611 for ( size_t i = 0; i < c_nInsThreadCount; ++i ) {
612 if ( !testMap.find( key_type(n, i) ) ) {
613 if ( ++nErrorCount < 10 ) {
614 CPPUNIT_MSG( "key " << n << "-" << i << " is not found!");
619 CPPUNIT_CHECK_EX( nErrorCount == 0, "Totals: " << nErrorCount << " keys is not found");
622 check_before_clear( testMap );
624 CPPUNIT_MSG( " Clear map (single-threaded)..." );
627 CPPUNIT_MSG( " Duration=" << timer.duration() );
628 CPPUNIT_CHECK_EX( testMap.empty(), ((long long) testMap.size()) );
630 additional_check( testMap );
631 print_stat( testMap );
633 additional_cleanup( testMap );
640 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
641 << " delete thread count=" << c_nDelThreadCount
642 << " set size=" << c_nMapSize
645 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
646 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
647 do_test<Map>( nLoadFactor );
648 if ( c_bPrintGCState )
656 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
657 << ", delete=" << c_nDelThreadCount
658 << ", extract=" << c_nExtractThreadCount
659 << "; set size=" << c_nMapSize
662 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
663 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
664 do_test_extract<Map>( nLoadFactor );
665 if ( c_bPrintGCState )
673 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
674 << " delete thread count=" << c_nDelThreadCount
675 << " set size=" << c_nMapSize
680 if ( c_bPrintGCState )
685 void test_nolf_extract()
687 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
688 << ", delete=" << c_nDelThreadCount
689 << ", extract=" << c_nExtractThreadCount
690 << "; set size=" << c_nMapSize
694 do_test_extract_with( s );
695 if ( c_bPrintGCState )
699 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
700 c_nMapSize = cfg.getULong("MapSize", static_cast<unsigned long>(c_nMapSize) );
701 c_nInsThreadCount = cfg.getULong("InsThreadCount", static_cast<unsigned long>(c_nInsThreadCount) );
702 c_nDelThreadCount = cfg.getULong("DelThreadCount", static_cast<unsigned long>(c_nDelThreadCount) );
703 c_nExtractThreadCount = cfg.getULong("ExtractThreadCount", static_cast<unsigned long>(c_nExtractThreadCount) );
704 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", static_cast<unsigned long>(c_nMaxLoadFactor) );
705 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
707 if ( c_nInsThreadCount == 0 )
708 c_nInsThreadCount = cds::OS::topology::processor_count();
709 if ( c_nDelThreadCount == 0 && c_nExtractThreadCount == 0 ) {
710 c_nExtractThreadCount = cds::OS::topology::processor_count() / 2;
711 c_nDelThreadCount = cds::OS::topology::processor_count() - c_nExtractThreadCount;
714 m_arrData.resize( c_nMapSize );
715 for ( size_t i = 0; i < c_nMapSize; ++i )
717 std::random_shuffle( m_arrData.begin(), m_arrData.end() );
720 # include "map2/map_defs.h"
721 CDSUNIT_DECLARE_MichaelMap
722 CDSUNIT_DECLARE_SplitList
723 //CDSUNIT_DECLARE_StripedMap
724 //CDSUNIT_DECLARE_RefinableMap
725 CDSUNIT_DECLARE_CuckooMap
726 CDSUNIT_DECLARE_SkipListMap
727 CDSUNIT_DECLARE_EllenBinTreeMap
728 //CDSUNIT_DECLARE_StdMap
730 CPPUNIT_TEST_SUITE( Map_DelOdd )
731 CDSUNIT_TEST_MichaelMap
732 CDSUNIT_TEST_SplitList
733 CDSUNIT_TEST_SkipListMap
734 CDSUNIT_TEST_EllenBinTreeMap
735 //CDSUNIT_TEST_StripedMap
736 //CDSUNIT_TEST_RefinableMap
737 CDSUNIT_TEST_CuckooMap
738 //CDSUNIT_TEST_StdMap
739 CPPUNIT_TEST_SUITE_END()
742 CPPUNIT_TEST_SUITE_REGISTRATION( Map_DelOdd );