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);
105 inline size_t hash_value( map2::key_thread const& k )
\r
107 return std::hash<size_t>()( k.nKey );
\r
110 struct hash<map2::key_thread>
112 typedef size_t result_type;
113 typedef map2::key_thread argument_type;
115 size_t operator()(map2::key_thread const& k) const
117 return boost::hash<size_t>()( k.nKey );
119 size_t operator()(size_t k) const
121 return boost::hash<size_t>()( k );
128 template <typename Map>
129 static inline void check_before_clear( Map& s )
132 template <typename GC, typename Key, typename T, typename Traits>
133 static inline void check_before_clear( cds::container::EllenBinTreeMap<GC, Key, T, Traits>& s )
135 CPPUNIT_CHECK_CURRENT( s.check_consistency() );
138 class Map_DelOdd: public CppUnitMini::TestCase
140 std::vector<size_t> m_arrData;
143 typedef key_thread key_type;
144 typedef size_t value_type;
145 typedef std::pair<key_type const, value_type> pair_type;
147 atomics::atomic<size_t> m_nInsThreadCount;
149 // Inserts keys from [0..N)
151 class InsertThread: public CppUnitMini::TestThread
155 virtual InsertThread * clone()
157 return new InsertThread( *this );
162 template <typename Q>
163 void operator()( bool bNew, Q const& )
167 size_t m_nInsertSuccess;
168 size_t m_nInsertFailed;
171 InsertThread( CppUnitMini::ThreadPool& pool, Map& rMap )
172 : CppUnitMini::TestThread( pool )
175 InsertThread( InsertThread& src )
176 : CppUnitMini::TestThread( src )
180 Map_DelOdd& getTest()
182 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
185 virtual void init() { cds::threading::Manager::attachThread() ; }
186 virtual void fini() { cds::threading::Manager::detachThread() ; }
195 std::vector<size_t>& arrData = getTest().m_arrData;
196 for ( size_t i = 0; i < arrData.size(); ++i ) {
197 if ( rMap.insert( key_type( arrData[i], m_nThreadNo )))
204 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
205 if ( arrData[i] & 1 ) {
206 rMap.ensure( key_type( arrData[i], m_nThreadNo ), f );
210 getTest().m_nInsThreadCount.fetch_sub( 1, atomics::memory_order_acquire );
215 bool operator()( key_type const& k1, key_type const& k2 ) const
217 return k1.nKey == k2.nKey;
219 bool operator()( size_t k1, key_type const& k2 ) const
221 return k1 == k2.nKey;
223 bool operator()( key_type const& k1, size_t k2 ) const
225 return k1.nKey == k2;
230 bool operator()( key_type const& k1, key_type const& k2 ) const
232 return k1.nKey < k2.nKey;
234 bool operator()( size_t k1, key_type const& k2 ) const
238 bool operator()( key_type const& k1, size_t k2 ) const
243 typedef key_equal equal_to;
246 // Deletes odd keys from [0..N)
248 class DeleteThread: public CppUnitMini::TestThread
252 virtual DeleteThread * clone()
254 return new DeleteThread( *this );
257 size_t m_nDeleteSuccess;
258 size_t m_nDeleteFailed;
261 DeleteThread( CppUnitMini::ThreadPool& pool, Map& rMap )
262 : CppUnitMini::TestThread( pool )
265 DeleteThread( DeleteThread& src )
266 : CppUnitMini::TestThread( src )
270 Map_DelOdd& getTest()
272 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
275 virtual void init() { cds::threading::Manager::attachThread() ; }
276 virtual void fini() { cds::threading::Manager::detachThread() ; }
285 std::vector<size_t>& arrData = getTest().m_arrData;
286 if ( m_nThreadNo & 1 ) {
287 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
288 for ( size_t i = 0; i < arrData.size(); ++i ) {
289 if ( arrData[i] & 1 ) {
290 if ( rMap.erase_with( arrData[i], key_less() ))
296 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
301 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
302 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
303 if ( arrData[i] & 1 ) {
304 if ( rMap.erase_with( arrData[i], key_less() ))
310 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
317 // Deletes odd keys from [0..N)
318 template <class GC, class Map >
319 class ExtractThread: public CppUnitMini::TestThread
323 virtual ExtractThread * clone()
325 return new ExtractThread( *this );
328 size_t m_nDeleteSuccess;
329 size_t m_nDeleteFailed;
332 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
333 : CppUnitMini::TestThread( pool )
336 ExtractThread( ExtractThread& src )
337 : CppUnitMini::TestThread( src )
341 Map_DelOdd& getTest()
343 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
346 virtual void init() { cds::threading::Manager::attachThread() ; }
347 virtual void fini() { cds::threading::Manager::detachThread() ; }
356 typename Map::guarded_ptr gp;
358 std::vector<size_t>& arrData = getTest().m_arrData;
359 if ( m_nThreadNo & 1 ) {
360 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
361 for ( size_t i = 0; i < arrData.size(); ++i ) {
362 if ( arrData[i] & 1 ) {
363 if ( rMap.extract_with( gp, arrData[i], key_less() ))
369 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
374 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
375 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
376 if ( arrData[i] & 1 ) {
377 if ( rMap.extract_with( gp, arrData[i], key_less() ))
383 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
390 template <class RCU, class Map >
391 class ExtractThread< cds::urcu::gc<RCU>, Map > : public CppUnitMini::TestThread
395 virtual ExtractThread * clone()
397 return new ExtractThread( *this );
400 size_t m_nDeleteSuccess;
401 size_t m_nDeleteFailed;
404 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
405 : CppUnitMini::TestThread( pool )
408 ExtractThread( ExtractThread& src )
409 : CppUnitMini::TestThread( src )
413 Map_DelOdd& getTest()
415 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
418 virtual void init() { cds::threading::Manager::attachThread() ; }
419 virtual void fini() { cds::threading::Manager::detachThread() ; }
428 typename Map::exempt_ptr xp;
430 std::vector<size_t>& arrData = getTest().m_arrData;
431 if ( m_nThreadNo & 1 ) {
432 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
433 for ( size_t i = 0; i < arrData.size(); ++i ) {
434 if ( arrData[i] & 1 ) {
435 if ( Map::c_bExtractLockExternal ) {
437 typename Map::rcu_lock l;
438 if ( rMap.extract_with( xp, arrData[i], key_less() )) {
447 if ( rMap.extract_with( xp, arrData[i], key_less() )) {
456 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
461 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
462 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
463 if ( arrData[i] & 1 ) {
464 if ( Map::c_bExtractLockExternal ) {
466 typename Map::rcu_lock l;
467 if ( rMap.extract_with( xp, arrData[i], key_less() ))
475 if ( rMap.extract_with( xp, arrData[i], key_less() )) {
484 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
493 void do_test( size_t nLoadFactor )
495 Map testMap( c_nMapSize, nLoadFactor );
496 do_test_with( testMap );
500 void do_test_extract( size_t nLoadFactor )
502 Map testMap( c_nMapSize, nLoadFactor );
503 do_test_extract_with( testMap );
507 void do_test_with( Map& testMap )
509 typedef InsertThread<Map> insert_thread;
510 typedef DeleteThread<Map> delete_thread;
512 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
514 CppUnitMini::ThreadPool pool( *this );
515 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
516 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount ? c_nDelThreadCount : cds::OS::topology::processor_count());
518 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
520 size_t nInsertSuccess = 0;
521 size_t nInsertFailed = 0;
522 size_t nDeleteSuccess = 0;
523 size_t nDeleteFailed = 0;
524 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
525 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
527 nInsertSuccess += pThread->m_nInsertSuccess;
528 nInsertFailed += pThread->m_nInsertFailed;
531 delete_thread * p = static_cast<delete_thread *>( *it );
532 nDeleteSuccess += p->m_nDeleteSuccess;
533 nDeleteFailed += p->m_nDeleteFailed;
537 CPPUNIT_MSG( " Totals (success/failed): \n\t"
538 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
539 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
541 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
542 CPPUNIT_CHECK( nInsertFailed == 0 );
548 void do_test_extract_with( Map& testMap )
550 typedef InsertThread<Map> insert_thread;
551 typedef DeleteThread<Map> delete_thread;
552 typedef ExtractThread< typename Map::gc, Map > extract_thread;
554 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
556 CppUnitMini::ThreadPool pool( *this );
557 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
558 if ( c_nDelThreadCount )
559 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount );
560 if ( c_nExtractThreadCount )
561 pool.add( new extract_thread( pool, testMap ), c_nExtractThreadCount );
563 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
565 size_t nInsertSuccess = 0;
566 size_t nInsertFailed = 0;
567 size_t nDeleteSuccess = 0;
568 size_t nDeleteFailed = 0;
569 size_t nExtractSuccess = 0;
570 size_t nExtractFailed = 0;
571 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
572 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
574 nInsertSuccess += pThread->m_nInsertSuccess;
575 nInsertFailed += pThread->m_nInsertFailed;
578 delete_thread * p = dynamic_cast<delete_thread *>( *it );
580 nDeleteSuccess += p->m_nDeleteSuccess;
581 nDeleteFailed += p->m_nDeleteFailed;
584 extract_thread * pExtract = dynamic_cast<extract_thread *>( *it );
586 nExtractSuccess += pExtract->m_nDeleteSuccess;
587 nExtractFailed += pExtract->m_nDeleteFailed;
592 CPPUNIT_MSG( " Totals (success/failed): \n\t"
593 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
594 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
595 << " Extract=" << nExtractSuccess << '/' << nExtractFailed << "\n\t"
597 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
598 CPPUNIT_CHECK( nInsertFailed == 0 );
604 void analyze( Map& testMap )
606 cds::OS::Timer timer;
608 // All even keys must be in the map
610 size_t nErrorCount = 0;
611 CPPUNIT_MSG( " Check even keys..." );
612 for ( size_t n = 0; n < c_nMapSize; n +=2 ) {
613 for ( size_t i = 0; i < c_nInsThreadCount; ++i ) {
614 if ( !testMap.find( key_type(n, i) ) ) {
615 if ( ++nErrorCount < 10 ) {
616 CPPUNIT_MSG( "key " << n << "-" << i << " is not found!");
621 CPPUNIT_CHECK_EX( nErrorCount == 0, "Totals: " << nErrorCount << " keys is not found");
624 check_before_clear( testMap );
626 CPPUNIT_MSG( " Clear map (single-threaded)..." );
629 CPPUNIT_MSG( " Duration=" << timer.duration() );
630 CPPUNIT_CHECK_EX( testMap.empty(), ((long long) testMap.size()) );
632 additional_check( testMap );
633 print_stat( testMap );
635 additional_cleanup( testMap );
642 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
643 << " delete thread count=" << c_nDelThreadCount
644 << " set size=" << c_nMapSize
647 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
648 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
649 do_test<Map>( nLoadFactor );
650 if ( c_bPrintGCState )
658 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
659 << ", delete=" << c_nDelThreadCount
660 << ", extract=" << c_nExtractThreadCount
661 << "; set size=" << c_nMapSize
664 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
665 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
666 do_test_extract<Map>( nLoadFactor );
667 if ( c_bPrintGCState )
675 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
676 << " delete thread count=" << c_nDelThreadCount
677 << " set size=" << c_nMapSize
682 if ( c_bPrintGCState )
687 void test_nolf_extract()
689 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
690 << ", delete=" << c_nDelThreadCount
691 << ", extract=" << c_nExtractThreadCount
692 << "; set size=" << c_nMapSize
696 do_test_extract_with( s );
697 if ( c_bPrintGCState )
701 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
702 c_nMapSize = cfg.getULong("MapSize", static_cast<unsigned long>(c_nMapSize) );
703 c_nInsThreadCount = cfg.getULong("InsThreadCount", static_cast<unsigned long>(c_nInsThreadCount) );
704 c_nDelThreadCount = cfg.getULong("DelThreadCount", static_cast<unsigned long>(c_nDelThreadCount) );
705 c_nExtractThreadCount = cfg.getULong("ExtractThreadCount", static_cast<unsigned long>(c_nExtractThreadCount) );
706 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", static_cast<unsigned long>(c_nMaxLoadFactor) );
707 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
709 if ( c_nInsThreadCount == 0 )
710 c_nInsThreadCount = cds::OS::topology::processor_count();
711 if ( c_nDelThreadCount == 0 && c_nExtractThreadCount == 0 ) {
712 c_nExtractThreadCount = cds::OS::topology::processor_count() / 2;
713 c_nDelThreadCount = cds::OS::topology::processor_count() - c_nExtractThreadCount;
716 m_arrData.resize( c_nMapSize );
717 for ( size_t i = 0; i < c_nMapSize; ++i )
719 std::random_shuffle( m_arrData.begin(), m_arrData.end() );
722 # include "map2/map_defs.h"
723 CDSUNIT_DECLARE_MichaelMap
724 CDSUNIT_DECLARE_SplitList
725 //CDSUNIT_DECLARE_StripedMap
726 //CDSUNIT_DECLARE_RefinableMap
727 CDSUNIT_DECLARE_CuckooMap
728 CDSUNIT_DECLARE_SkipListMap
729 CDSUNIT_DECLARE_EllenBinTreeMap
730 //CDSUNIT_DECLARE_StdMap
732 CPPUNIT_TEST_SUITE( Map_DelOdd )
733 CDSUNIT_TEST_MichaelMap
734 CDSUNIT_TEST_SplitList
735 CDSUNIT_TEST_SkipListMap
736 CDSUNIT_TEST_EllenBinTreeMap
737 //CDSUNIT_TEST_StripedMap
738 //CDSUNIT_TEST_RefinableMap
739 CDSUNIT_TEST_CuckooMap
740 //CDSUNIT_TEST_StdMap
741 CPPUNIT_TEST_SUITE_END()
744 CPPUNIT_TEST_SUITE_REGISTRATION( Map_DelOdd );