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 )
107 return std::hash<size_t>()( k.nKey );
111 struct hash<map2::key_thread>
113 typedef size_t result_type;
114 typedef map2::key_thread argument_type;
116 size_t operator()(map2::key_thread const& k) const
118 return boost::hash<size_t>()( k.nKey );
120 size_t operator()(size_t k) const
122 return boost::hash<size_t>()( k );
129 template <typename Map>
130 static inline void check_before_clear( Map& /*s*/ )
133 template <typename GC, typename Key, typename T, typename Traits>
134 static inline void check_before_clear( cds::container::EllenBinTreeMap<GC, Key, T, Traits>& s )
136 CPPUNIT_CHECK_CURRENT( s.check_consistency() );
139 class Map_DelOdd: public CppUnitMini::TestCase
141 std::vector<size_t> m_arrData;
144 typedef key_thread key_type;
145 typedef size_t value_type;
146 typedef std::pair<key_type const, value_type> pair_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*/, Q const& )
168 size_t m_nInsertSuccess;
169 size_t m_nInsertFailed;
172 InsertThread( CppUnitMini::ThreadPool& pool, Map& rMap )
173 : CppUnitMini::TestThread( pool )
176 InsertThread( InsertThread& src )
177 : CppUnitMini::TestThread( src )
181 Map_DelOdd& getTest()
183 return reinterpret_cast<Map_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 ( rMap.insert( key_type( arrData[i], m_nThreadNo )))
205 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
206 if ( arrData[i] & 1 ) {
207 rMap.ensure( key_type( arrData[i], m_nThreadNo ), f );
211 getTest().m_nInsThreadCount.fetch_sub( 1, atomics::memory_order_acquire );
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;
231 bool operator()( key_type const& k1, key_type const& k2 ) const
233 return k1.nKey < k2.nKey;
235 bool operator()( size_t k1, key_type const& k2 ) const
239 bool operator()( key_type const& k1, size_t k2 ) const
244 typedef key_equal equal_to;
247 // Deletes odd keys from [0..N)
249 class DeleteThread: public CppUnitMini::TestThread
253 virtual DeleteThread * clone()
255 return new DeleteThread( *this );
258 size_t m_nDeleteSuccess;
259 size_t m_nDeleteFailed;
262 DeleteThread( CppUnitMini::ThreadPool& pool, Map& rMap )
263 : CppUnitMini::TestThread( pool )
266 DeleteThread( DeleteThread& src )
267 : CppUnitMini::TestThread( src )
271 Map_DelOdd& getTest()
273 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
276 virtual void init() { cds::threading::Manager::attachThread() ; }
277 virtual void fini() { cds::threading::Manager::detachThread() ; }
286 std::vector<size_t>& arrData = getTest().m_arrData;
287 if ( m_nThreadNo & 1 ) {
288 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
289 for ( size_t i = 0; i < arrData.size(); ++i ) {
290 if ( arrData[i] & 1 ) {
291 if ( rMap.erase_with( arrData[i], key_less() ))
297 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
302 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
303 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
304 if ( arrData[i] & 1 ) {
305 if ( rMap.erase_with( arrData[i], key_less() ))
311 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
318 // Deletes odd keys from [0..N)
319 template <class GC, class Map >
320 class ExtractThread: public CppUnitMini::TestThread
324 virtual ExtractThread * clone()
326 return new ExtractThread( *this );
329 size_t m_nDeleteSuccess;
330 size_t m_nDeleteFailed;
333 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
334 : CppUnitMini::TestThread( pool )
337 ExtractThread( ExtractThread& src )
338 : CppUnitMini::TestThread( src )
342 Map_DelOdd& getTest()
344 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
347 virtual void init() { cds::threading::Manager::attachThread() ; }
348 virtual void fini() { cds::threading::Manager::detachThread() ; }
357 typename Map::guarded_ptr gp;
359 std::vector<size_t>& arrData = getTest().m_arrData;
360 if ( m_nThreadNo & 1 ) {
361 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
362 for ( size_t i = 0; i < arrData.size(); ++i ) {
363 if ( arrData[i] & 1 ) {
364 gp = rMap.extract_with( arrData[i], key_less());
372 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
377 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
378 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
379 if ( arrData[i] & 1 ) {
380 gp = rMap.extract_with( arrData[i], key_less());
388 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
395 template <class RCU, class Map >
396 class ExtractThread< cds::urcu::gc<RCU>, Map > : public CppUnitMini::TestThread
400 virtual ExtractThread * clone()
402 return new ExtractThread( *this );
405 size_t m_nDeleteSuccess;
406 size_t m_nDeleteFailed;
409 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
410 : CppUnitMini::TestThread( pool )
413 ExtractThread( ExtractThread& src )
414 : CppUnitMini::TestThread( src )
418 Map_DelOdd& getTest()
420 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
423 virtual void init() { cds::threading::Manager::attachThread() ; }
424 virtual void fini() { cds::threading::Manager::detachThread() ; }
433 typename Map::exempt_ptr xp;
435 std::vector<size_t>& arrData = getTest().m_arrData;
436 if ( m_nThreadNo & 1 ) {
437 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
438 for ( size_t i = 0; i < arrData.size(); ++i ) {
439 if ( arrData[i] & 1 ) {
440 if ( Map::c_bExtractLockExternal ) {
442 typename Map::rcu_lock l;
443 xp = rMap.extract_with( arrData[i], key_less() );
451 xp = rMap.extract_with( arrData[i], key_less() );
460 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
465 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
466 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
467 if ( arrData[i] & 1 ) {
468 if ( Map::c_bExtractLockExternal ) {
470 typename Map::rcu_lock l;
471 xp = rMap.extract_with( arrData[i], key_less() );
479 xp = rMap.extract_with( arrData[i], key_less() );
488 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
497 void do_test( size_t nLoadFactor )
499 Map testMap( c_nMapSize, nLoadFactor );
500 do_test_with( testMap );
504 void do_test_extract( size_t nLoadFactor )
506 Map testMap( c_nMapSize, nLoadFactor );
507 do_test_extract_with( testMap );
511 void do_test_with( Map& testMap )
513 typedef InsertThread<Map> insert_thread;
514 typedef DeleteThread<Map> delete_thread;
516 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
518 CppUnitMini::ThreadPool pool( *this );
519 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
520 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount ? c_nDelThreadCount : cds::OS::topology::processor_count());
522 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
524 size_t nInsertSuccess = 0;
525 size_t nInsertFailed = 0;
526 size_t nDeleteSuccess = 0;
527 size_t nDeleteFailed = 0;
528 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
529 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
531 nInsertSuccess += pThread->m_nInsertSuccess;
532 nInsertFailed += pThread->m_nInsertFailed;
535 delete_thread * p = static_cast<delete_thread *>( *it );
536 nDeleteSuccess += p->m_nDeleteSuccess;
537 nDeleteFailed += p->m_nDeleteFailed;
541 CPPUNIT_MSG( " Totals (success/failed): \n\t"
542 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
543 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
545 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
546 CPPUNIT_CHECK( nInsertFailed == 0 );
552 void do_test_extract_with( Map& testMap )
554 typedef InsertThread<Map> insert_thread;
555 typedef DeleteThread<Map> delete_thread;
556 typedef ExtractThread< typename Map::gc, Map > extract_thread;
558 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
560 CppUnitMini::ThreadPool pool( *this );
561 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
562 if ( c_nDelThreadCount )
563 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount );
564 if ( c_nExtractThreadCount )
565 pool.add( new extract_thread( pool, testMap ), c_nExtractThreadCount );
567 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
569 size_t nInsertSuccess = 0;
570 size_t nInsertFailed = 0;
571 size_t nDeleteSuccess = 0;
572 size_t nDeleteFailed = 0;
573 size_t nExtractSuccess = 0;
574 size_t nExtractFailed = 0;
575 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
576 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
578 nInsertSuccess += pThread->m_nInsertSuccess;
579 nInsertFailed += pThread->m_nInsertFailed;
582 delete_thread * p = dynamic_cast<delete_thread *>( *it );
584 nDeleteSuccess += p->m_nDeleteSuccess;
585 nDeleteFailed += p->m_nDeleteFailed;
588 extract_thread * pExtract = dynamic_cast<extract_thread *>( *it );
590 nExtractSuccess += pExtract->m_nDeleteSuccess;
591 nExtractFailed += pExtract->m_nDeleteFailed;
596 CPPUNIT_MSG( " Totals (success/failed): \n\t"
597 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
598 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
599 << " Extract=" << nExtractSuccess << '/' << nExtractFailed << "\n\t"
601 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
602 CPPUNIT_CHECK( nInsertFailed == 0 );
608 void analyze( Map& testMap )
610 cds::OS::Timer timer;
612 // All even keys must be in the map
614 size_t nErrorCount = 0;
615 CPPUNIT_MSG( " Check even keys..." );
616 for ( size_t n = 0; n < c_nMapSize; n +=2 ) {
617 for ( size_t i = 0; i < c_nInsThreadCount; ++i ) {
618 if ( !testMap.find( key_type(n, i) ) ) {
619 if ( ++nErrorCount < 10 ) {
620 CPPUNIT_MSG( "key " << n << "-" << i << " is not found!");
625 CPPUNIT_CHECK_EX( nErrorCount == 0, "Totals: " << nErrorCount << " keys is not found");
628 check_before_clear( testMap );
630 CPPUNIT_MSG( " Clear map (single-threaded)..." );
633 CPPUNIT_MSG( " Duration=" << timer.duration() );
634 CPPUNIT_CHECK_EX( testMap.empty(), ((long long) testMap.size()) );
636 additional_check( testMap );
637 print_stat( testMap );
639 additional_cleanup( testMap );
646 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
647 << " delete thread count=" << c_nDelThreadCount
648 << " set size=" << c_nMapSize
651 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
652 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
653 do_test<Map>( nLoadFactor );
654 if ( c_bPrintGCState )
662 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
663 << ", delete=" << c_nDelThreadCount
664 << ", extract=" << c_nExtractThreadCount
665 << "; set size=" << c_nMapSize
668 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
669 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
670 do_test_extract<Map>( nLoadFactor );
671 if ( c_bPrintGCState )
679 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
680 << " delete thread count=" << c_nDelThreadCount
681 << " set size=" << c_nMapSize
686 if ( c_bPrintGCState )
691 void test_nolf_extract()
693 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
694 << ", delete=" << c_nDelThreadCount
695 << ", extract=" << c_nExtractThreadCount
696 << "; set size=" << c_nMapSize
700 do_test_extract_with( s );
701 if ( c_bPrintGCState )
705 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
706 c_nMapSize = cfg.getULong("MapSize", static_cast<unsigned long>(c_nMapSize) );
707 c_nInsThreadCount = cfg.getULong("InsThreadCount", static_cast<unsigned long>(c_nInsThreadCount) );
708 c_nDelThreadCount = cfg.getULong("DelThreadCount", static_cast<unsigned long>(c_nDelThreadCount) );
709 c_nExtractThreadCount = cfg.getULong("ExtractThreadCount", static_cast<unsigned long>(c_nExtractThreadCount) );
710 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", static_cast<unsigned long>(c_nMaxLoadFactor) );
711 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
713 if ( c_nInsThreadCount == 0 )
714 c_nInsThreadCount = cds::OS::topology::processor_count();
715 if ( c_nDelThreadCount == 0 && c_nExtractThreadCount == 0 ) {
716 c_nExtractThreadCount = cds::OS::topology::processor_count() / 2;
717 c_nDelThreadCount = cds::OS::topology::processor_count() - c_nExtractThreadCount;
720 m_arrData.resize( c_nMapSize );
721 for ( size_t i = 0; i < c_nMapSize; ++i )
723 std::random_shuffle( m_arrData.begin(), m_arrData.end() );
726 # include "map2/map_defs.h"
727 CDSUNIT_DECLARE_MichaelMap
728 CDSUNIT_DECLARE_SplitList
729 //CDSUNIT_DECLARE_StripedMap
730 //CDSUNIT_DECLARE_RefinableMap
731 CDSUNIT_DECLARE_CuckooMap
732 CDSUNIT_DECLARE_SkipListMap
733 CDSUNIT_DECLARE_EllenBinTreeMap
734 //CDSUNIT_DECLARE_StdMap
736 CPPUNIT_TEST_SUITE( Map_DelOdd )
737 CDSUNIT_TEST_MichaelMap
738 CDSUNIT_TEST_SplitList
739 CDSUNIT_TEST_SkipListMap
740 CDSUNIT_TEST_EllenBinTreeMap
741 //CDSUNIT_TEST_StripedMap
742 //CDSUNIT_TEST_RefinableMap
743 CDSUNIT_TEST_CuckooMap
744 //CDSUNIT_TEST_StdMap
745 CPPUNIT_TEST_SUITE_END()
748 CPPUNIT_TEST_SUITE_REGISTRATION( Map_DelOdd );