3 #include "cppunit/thread.h"
4 #include "map2/map_type.h"
5 #include <cds/os/topology.h>
9 # define TEST_MAP(IMPL, C, X) void C::X() { test<map_type<IMPL, key_type, value_type>::X >(); }
10 # define TEST_MAP_EXTRACT(IMPL, C, X) void C::X() { test_extract<map_type<IMPL, key_type, value_type>::X >(); }
11 # define TEST_MAP_NOLF(IMPL, C, X) void C::X() { test_nolf<map_type<IMPL, key_type, value_type>::X >(); }
12 # define TEST_MAP_NOLF_EXTRACT(IMPL, C, X) void C::X() { test_nolf_extract<map_type<IMPL, key_type, value_type>::X >(); }
20 key_thread( size_t key, size_t threadNo )
29 //typedef MapTypes<key_thread, size_t>::key_val key_value_pair;
33 struct cmp<key_thread> {
34 int operator ()(key_thread const& k1, key_thread const& k2) const
36 if ( k1.nKey < k2.nKey )
38 if ( k1.nKey > k2.nKey )
40 if ( k1.nThread < k2.nThread )
42 if ( k1.nThread > k2.nThread )
46 int operator ()(key_thread const& k1, size_t k2) const
54 int operator ()(size_t k1, key_thread const& k2) const
68 struct less<map2::key_thread>
70 bool operator()(map2::key_thread const& k1, map2::key_thread const& k2) const
72 if ( k1.nKey <= k2.nKey )
73 return k1.nKey < k2.nKey || k1.nThread < k2.nThread;
79 struct hash<map2::key_thread>
81 typedef size_t result_type;
82 typedef map2::key_thread argument_type;
84 size_t operator()( map2::key_thread const& k ) const
86 return std::hash<size_t>()(k.nKey);
88 size_t operator()( size_t k ) const
90 return std::hash<size_t>()(k);
96 inline size_t hash_value( map2::key_thread const& k )
98 return std::hash<size_t>()( k.nKey );
102 struct hash<map2::key_thread>
104 typedef size_t result_type;
105 typedef map2::key_thread argument_type;
107 size_t operator()(map2::key_thread const& k) const
109 return boost::hash<size_t>()( k.nKey );
111 size_t operator()(size_t k) const
113 return boost::hash<size_t>()( k );
120 class Map_DelOdd: public CppUnitMini::TestCase
122 static size_t c_nMapSize; // max map size
123 static size_t c_nInsThreadCount; // insert thread count
124 static size_t c_nDelThreadCount; // delete thread count
125 static size_t c_nExtractThreadCount; // extract thread count
126 static size_t c_nMaxLoadFactor; // maximum load factor
127 static bool c_bPrintGCState;
129 std::vector<size_t> m_arrInsert;
130 std::vector<size_t> m_arrRemove;
133 typedef CppUnitMini::TestCase Base;
135 typedef key_thread key_type;
136 typedef size_t value_type;
137 typedef std::pair<key_type const, value_type> pair_type;
139 atomics::atomic<size_t> m_nInsThreadCount;
141 // Inserts keys from [0..N)
143 class InsertThread: public CppUnitMini::TestThread
147 virtual InsertThread * clone()
149 return new InsertThread( *this );
154 template <typename Q>
155 void operator()( bool /*bNew*/, Q const& )
157 template <typename Q, typename V>
158 void operator()( bool /*bNew*/, Q const&, V& )
162 size_t m_nInsertSuccess;
163 size_t m_nInsertFailed;
166 InsertThread( CppUnitMini::ThreadPool& pool, Map& rMap )
167 : CppUnitMini::TestThread( pool )
170 InsertThread( InsertThread& src )
171 : CppUnitMini::TestThread( src )
175 Map_DelOdd& getTest()
177 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
180 virtual void init() { cds::threading::Manager::attachThread() ; }
181 virtual void fini() { cds::threading::Manager::detachThread() ; }
190 std::vector<size_t>& arrData = getTest().m_arrInsert;
191 for ( size_t i = 0; i < arrData.size(); ++i ) {
192 if ( rMap.insert( key_type( arrData[i], m_nThreadNo )))
199 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
200 if ( arrData[i] & 1 ) {
201 rMap.ensure( key_type( arrData[i], m_nThreadNo ), f );
205 getTest().m_nInsThreadCount.fetch_sub( 1, atomics::memory_order_acquire );
210 bool operator()( key_type const& k1, key_type const& k2 ) const
212 return k1.nKey == k2.nKey;
214 bool operator()( size_t k1, key_type const& k2 ) const
216 return k1 == k2.nKey;
218 bool operator()( key_type const& k1, size_t k2 ) const
220 return k1.nKey == k2;
225 bool operator()( key_type const& k1, key_type const& k2 ) const
227 return k1.nKey < k2.nKey;
229 bool operator()( size_t k1, key_type const& k2 ) const
233 bool operator()( key_type const& k1, size_t k2 ) const
238 typedef key_equal equal_to;
241 // Deletes odd keys from [0..N)
243 class DeleteThread: public CppUnitMini::TestThread
247 virtual DeleteThread * clone()
249 return new DeleteThread( *this );
252 size_t m_nDeleteSuccess;
253 size_t m_nDeleteFailed;
256 DeleteThread( CppUnitMini::ThreadPool& pool, Map& rMap )
257 : CppUnitMini::TestThread( pool )
260 DeleteThread( DeleteThread& src )
261 : CppUnitMini::TestThread( src )
265 Map_DelOdd& getTest()
267 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
270 virtual void init() { cds::threading::Manager::attachThread() ; }
271 virtual void fini() { cds::threading::Manager::detachThread() ; }
280 for ( size_t pass = 0; pass < 2; pass++ ) {
281 std::vector<size_t>& arrData = getTest().m_arrRemove;
282 if ( m_nThreadNo & 1 ) {
283 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
284 for ( size_t i = 0; i < arrData.size(); ++i ) {
285 if ( arrData[i] & 1 ) {
286 if ( rMap.erase_with( arrData[i], key_less() ))
292 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
297 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
298 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
299 if ( arrData[i] & 1 ) {
300 if ( rMap.erase_with( arrData[i], key_less() ))
306 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
314 // Deletes odd keys from [0..N)
315 template <class GC, class Map >
316 class ExtractThread: public CppUnitMini::TestThread
320 virtual ExtractThread * clone()
322 return new ExtractThread( *this );
325 size_t m_nDeleteSuccess;
326 size_t m_nDeleteFailed;
329 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
330 : CppUnitMini::TestThread( pool )
333 ExtractThread( ExtractThread& src )
334 : CppUnitMini::TestThread( src )
338 Map_DelOdd& getTest()
340 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
343 virtual void init() { cds::threading::Manager::attachThread() ; }
344 virtual void fini() { cds::threading::Manager::detachThread() ; }
353 typename Map::guarded_ptr gp;
355 for ( size_t pass = 0; pass < 2; ++pass ) {
356 std::vector<size_t>& arrData = getTest().m_arrRemove;
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 gp = rMap.extract_with( 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 gp = rMap.extract_with( arrData[i], key_less());
385 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
393 template <class RCU, class Map >
394 class ExtractThread< cds::urcu::gc<RCU>, Map > : public CppUnitMini::TestThread
398 virtual ExtractThread * clone()
400 return new ExtractThread( *this );
403 size_t m_nDeleteSuccess;
404 size_t m_nDeleteFailed;
407 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
408 : CppUnitMini::TestThread( pool )
411 ExtractThread( ExtractThread& src )
412 : CppUnitMini::TestThread( src )
416 Map_DelOdd& getTest()
418 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
421 virtual void init() { cds::threading::Manager::attachThread() ; }
422 virtual void fini() { cds::threading::Manager::detachThread() ; }
431 typename Map::exempt_ptr xp;
433 std::vector<size_t>& arrData = getTest().m_arrRemove;
434 if ( m_nThreadNo & 1 ) {
435 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
436 for ( size_t i = 0; i < arrData.size(); ++i ) {
437 if ( arrData[i] & 1 ) {
438 if ( Map::c_bExtractLockExternal ) {
440 typename Map::rcu_lock l;
441 xp = rMap.extract_with( arrData[i], key_less() );
449 xp = rMap.extract_with( arrData[i], key_less() );
458 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
463 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
464 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
465 if ( arrData[i] & 1 ) {
466 if ( Map::c_bExtractLockExternal ) {
468 typename Map::rcu_lock l;
469 xp = rMap.extract_with( arrData[i], key_less() );
477 xp = rMap.extract_with( arrData[i], key_less() );
486 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
495 void do_test( size_t nLoadFactor )
497 Map testMap( c_nMapSize, nLoadFactor );
498 do_test_with( testMap );
502 void do_test_extract( size_t nLoadFactor )
504 Map testMap( c_nMapSize, nLoadFactor );
505 do_test_extract_with( testMap );
509 void do_test_with( Map& testMap )
511 typedef InsertThread<Map> insert_thread;
512 typedef DeleteThread<Map> delete_thread;
514 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
516 CppUnitMini::ThreadPool pool( *this );
517 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
518 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount ? c_nDelThreadCount : cds::OS::topology::processor_count());
520 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
522 size_t nInsertSuccess = 0;
523 size_t nInsertFailed = 0;
524 size_t nDeleteSuccess = 0;
525 size_t nDeleteFailed = 0;
526 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
527 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
529 nInsertSuccess += pThread->m_nInsertSuccess;
530 nInsertFailed += pThread->m_nInsertFailed;
533 delete_thread * p = static_cast<delete_thread *>( *it );
534 nDeleteSuccess += p->m_nDeleteSuccess;
535 nDeleteFailed += p->m_nDeleteFailed;
539 CPPUNIT_MSG( " Totals (success/failed): \n\t"
540 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
541 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
543 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
544 CPPUNIT_CHECK( nInsertFailed == 0 );
550 void do_test_extract_with( Map& testMap )
552 typedef InsertThread<Map> insert_thread;
553 typedef DeleteThread<Map> delete_thread;
554 typedef ExtractThread< typename Map::gc, Map > extract_thread;
556 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
558 CppUnitMini::ThreadPool pool( *this );
559 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
560 if ( c_nDelThreadCount )
561 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount );
562 if ( c_nExtractThreadCount )
563 pool.add( new extract_thread( pool, testMap ), c_nExtractThreadCount );
565 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
567 size_t nInsertSuccess = 0;
568 size_t nInsertFailed = 0;
569 size_t nDeleteSuccess = 0;
570 size_t nDeleteFailed = 0;
571 size_t nExtractSuccess = 0;
572 size_t nExtractFailed = 0;
573 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
574 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
576 nInsertSuccess += pThread->m_nInsertSuccess;
577 nInsertFailed += pThread->m_nInsertFailed;
580 delete_thread * p = dynamic_cast<delete_thread *>( *it );
582 nDeleteSuccess += p->m_nDeleteSuccess;
583 nDeleteFailed += p->m_nDeleteFailed;
586 extract_thread * pExtract = dynamic_cast<extract_thread *>( *it );
588 nExtractSuccess += pExtract->m_nDeleteSuccess;
589 nExtractFailed += pExtract->m_nDeleteFailed;
594 CPPUNIT_MSG( " Totals (success/failed): \n\t"
595 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
596 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
597 << " Extract=" << nExtractSuccess << '/' << nExtractFailed << "\n\t"
599 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
600 CPPUNIT_CHECK( nInsertFailed == 0 );
606 void analyze( Map& testMap )
608 cds::OS::Timer timer;
610 // All even keys must be in the map
612 size_t nErrorCount = 0;
613 CPPUNIT_MSG( " Check even keys..." );
614 for ( size_t n = 0; n < c_nMapSize; n +=2 ) {
615 for ( size_t i = 0; i < c_nInsThreadCount; ++i ) {
616 if ( !testMap.find( key_type(n, i) ) ) {
617 if ( ++nErrorCount < 10 ) {
618 CPPUNIT_MSG( "key " << n << "-" << i << " is not found!");
623 CPPUNIT_CHECK_EX( nErrorCount == 0, "Totals: " << nErrorCount << " keys is not found");
626 check_before_cleanup( testMap );
628 CPPUNIT_MSG( " Clear map (single-threaded)..." );
631 CPPUNIT_MSG( " Duration=" << timer.duration() );
632 CPPUNIT_CHECK_EX( testMap.empty(), ((long long) testMap.size()) );
634 additional_check( testMap );
635 print_stat( testMap );
637 additional_cleanup( testMap );
643 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
644 << " delete thread count=" << c_nDelThreadCount
645 << " set size=" << c_nMapSize
648 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
649 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
650 do_test<Map>( nLoadFactor );
651 if ( c_bPrintGCState )
659 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
660 << ", delete=" << c_nDelThreadCount
661 << ", extract=" << c_nExtractThreadCount
662 << "; set size=" << c_nMapSize
665 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
666 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
667 do_test_extract<Map>( nLoadFactor );
668 if ( c_bPrintGCState )
676 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
677 << " delete thread count=" << c_nDelThreadCount
678 << " set size=" << c_nMapSize
683 if ( c_bPrintGCState )
688 void test_nolf_extract()
690 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
691 << ", delete=" << c_nDelThreadCount
692 << ", extract=" << c_nExtractThreadCount
693 << "; set size=" << c_nMapSize
697 do_test_extract_with( s );
698 if ( c_bPrintGCState )
702 void setUpParams( const CppUnitMini::TestCfg& cfg );
704 void run_MichaelMap(const char *in_name, bool invert = false);
705 void run_SplitList(const char *in_name, bool invert = false);
706 //void run_StripedMap(const char *in_name, bool invert = false);
707 //void run_RefinableMap(const char *in_name, bool invert = false);
708 void run_CuckooMap(const char *in_name, bool invert = false);
709 void run_SkipListMap(const char *in_name, bool invert = false);
710 void run_EllenBinTreeMap(const char *in_name, bool invert = false);
711 void run_BronsonAVLTreeMap(const char *in_name, bool invert = false);
712 //void run_StdMap(const char *in_name, bool invert = false);
714 virtual void myRun(const char *in_name, bool invert = false);
716 # include "map2/map_defs.h"
717 CDSUNIT_DECLARE_MichaelMap
718 CDSUNIT_DECLARE_SplitList
719 //CDSUNIT_DECLARE_StripedMap
720 //CDSUNIT_DECLARE_RefinableMap
721 CDSUNIT_DECLARE_CuckooMap
722 CDSUNIT_DECLARE_SkipListMap
723 CDSUNIT_DECLARE_EllenBinTreeMap
724 CDSUNIT_DECLARE_BronsonAVLTreeMap
725 //CDSUNIT_DECLARE_StdMap