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 >(); }
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_arrData;
132 typedef CppUnitMini::TestCase Base;
134 typedef key_thread key_type;
135 typedef size_t value_type;
136 typedef std::pair<key_type const, value_type> pair_type;
138 atomics::atomic<size_t> m_nInsThreadCount;
140 // Inserts keys from [0..N)
142 class InsertThread: public CppUnitMini::TestThread
146 virtual InsertThread * clone()
148 return new InsertThread( *this );
153 template <typename Q>
154 void operator()( bool /*bNew*/, Q const& )
156 template <typename Q, typename V>
157 void operator()( bool /*bNew*/, Q const&, V& )
161 size_t m_nInsertSuccess;
162 size_t m_nInsertFailed;
165 InsertThread( CppUnitMini::ThreadPool& pool, Map& rMap )
166 : CppUnitMini::TestThread( pool )
169 InsertThread( InsertThread& src )
170 : CppUnitMini::TestThread( src )
174 Map_DelOdd& getTest()
176 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
179 virtual void init() { cds::threading::Manager::attachThread() ; }
180 virtual void fini() { cds::threading::Manager::detachThread() ; }
189 std::vector<size_t>& arrData = getTest().m_arrData;
190 for ( size_t i = 0; i < arrData.size(); ++i ) {
191 if ( rMap.insert( key_type( arrData[i], m_nThreadNo )))
198 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
199 if ( arrData[i] & 1 ) {
200 rMap.ensure( key_type( arrData[i], m_nThreadNo ), f );
204 getTest().m_nInsThreadCount.fetch_sub( 1, atomics::memory_order_acquire );
209 bool operator()( key_type const& k1, key_type const& k2 ) const
211 return k1.nKey == k2.nKey;
213 bool operator()( size_t k1, key_type const& k2 ) const
215 return k1 == k2.nKey;
217 bool operator()( key_type const& k1, size_t k2 ) const
219 return k1.nKey == k2;
224 bool operator()( key_type const& k1, key_type const& k2 ) const
226 return k1.nKey < k2.nKey;
228 bool operator()( size_t k1, key_type const& k2 ) const
232 bool operator()( key_type const& k1, size_t k2 ) const
237 typedef key_equal equal_to;
240 // Deletes odd keys from [0..N)
242 class DeleteThread: public CppUnitMini::TestThread
246 virtual DeleteThread * clone()
248 return new DeleteThread( *this );
251 size_t m_nDeleteSuccess;
252 size_t m_nDeleteFailed;
255 DeleteThread( CppUnitMini::ThreadPool& pool, Map& rMap )
256 : CppUnitMini::TestThread( pool )
259 DeleteThread( DeleteThread& src )
260 : CppUnitMini::TestThread( src )
264 Map_DelOdd& getTest()
266 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
269 virtual void init() { cds::threading::Manager::attachThread() ; }
270 virtual void fini() { cds::threading::Manager::detachThread() ; }
279 std::vector<size_t>& arrData = getTest().m_arrData;
280 if ( m_nThreadNo & 1 ) {
281 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
282 for ( size_t i = 0; i < arrData.size(); ++i ) {
283 if ( arrData[i] & 1 ) {
284 if ( rMap.erase_with( arrData[i], key_less() ))
290 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
295 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
296 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
297 if ( arrData[i] & 1 ) {
298 if ( rMap.erase_with( arrData[i], key_less() ))
304 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
311 // Deletes odd keys from [0..N)
312 template <class GC, class Map >
313 class ExtractThread: public CppUnitMini::TestThread
317 virtual ExtractThread * clone()
319 return new ExtractThread( *this );
322 size_t m_nDeleteSuccess;
323 size_t m_nDeleteFailed;
326 ExtractThread( CppUnitMini::ThreadPool& pool, Map& rMap )
327 : CppUnitMini::TestThread( pool )
330 ExtractThread( ExtractThread& src )
331 : CppUnitMini::TestThread( src )
335 Map_DelOdd& getTest()
337 return reinterpret_cast<Map_DelOdd&>( m_Pool.m_Test );
340 virtual void init() { cds::threading::Manager::attachThread() ; }
341 virtual void fini() { cds::threading::Manager::detachThread() ; }
350 typename Map::guarded_ptr gp;
352 std::vector<size_t>& arrData = getTest().m_arrData;
353 if ( m_nThreadNo & 1 ) {
354 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
355 for ( size_t i = 0; i < arrData.size(); ++i ) {
356 if ( arrData[i] & 1 ) {
357 gp = rMap.extract_with( arrData[i], key_less());
365 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
370 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
371 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
372 if ( arrData[i] & 1 ) {
373 gp = rMap.extract_with( 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 xp = rMap.extract_with( arrData[i], key_less() );
444 xp = rMap.extract_with( arrData[i], key_less() );
453 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
458 for ( size_t k = 0; k < c_nInsThreadCount; ++k ) {
459 for ( size_t i = arrData.size() - 1; i > 0; --i ) {
460 if ( arrData[i] & 1 ) {
461 if ( Map::c_bExtractLockExternal ) {
463 typename Map::rcu_lock l;
464 xp = rMap.extract_with( arrData[i], key_less() );
472 xp = rMap.extract_with( arrData[i], key_less() );
481 if ( getTest().m_nInsThreadCount.load( atomics::memory_order_acquire ) == 0 )
490 void do_test( size_t nLoadFactor )
492 Map testMap( c_nMapSize, nLoadFactor );
493 do_test_with( testMap );
497 void do_test_extract( size_t nLoadFactor )
499 Map testMap( c_nMapSize, nLoadFactor );
500 do_test_extract_with( testMap );
504 void do_test_with( Map& testMap )
506 typedef InsertThread<Map> insert_thread;
507 typedef DeleteThread<Map> delete_thread;
509 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
511 CppUnitMini::ThreadPool pool( *this );
512 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
513 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount ? c_nDelThreadCount : cds::OS::topology::processor_count());
515 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
517 size_t nInsertSuccess = 0;
518 size_t nInsertFailed = 0;
519 size_t nDeleteSuccess = 0;
520 size_t nDeleteFailed = 0;
521 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
522 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
524 nInsertSuccess += pThread->m_nInsertSuccess;
525 nInsertFailed += pThread->m_nInsertFailed;
528 delete_thread * p = static_cast<delete_thread *>( *it );
529 nDeleteSuccess += p->m_nDeleteSuccess;
530 nDeleteFailed += p->m_nDeleteFailed;
534 CPPUNIT_MSG( " Totals (success/failed): \n\t"
535 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
536 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
538 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
539 CPPUNIT_CHECK( nInsertFailed == 0 );
545 void do_test_extract_with( Map& testMap )
547 typedef InsertThread<Map> insert_thread;
548 typedef DeleteThread<Map> delete_thread;
549 typedef ExtractThread< typename Map::gc, Map > extract_thread;
551 m_nInsThreadCount.store( c_nInsThreadCount, atomics::memory_order_release );
553 CppUnitMini::ThreadPool pool( *this );
554 pool.add( new insert_thread( pool, testMap ), c_nInsThreadCount );
555 if ( c_nDelThreadCount )
556 pool.add( new delete_thread( pool, testMap ), c_nDelThreadCount );
557 if ( c_nExtractThreadCount )
558 pool.add( new extract_thread( pool, testMap ), c_nExtractThreadCount );
560 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
562 size_t nInsertSuccess = 0;
563 size_t nInsertFailed = 0;
564 size_t nDeleteSuccess = 0;
565 size_t nDeleteFailed = 0;
566 size_t nExtractSuccess = 0;
567 size_t nExtractFailed = 0;
568 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
569 insert_thread * pThread = dynamic_cast<insert_thread *>( *it );
571 nInsertSuccess += pThread->m_nInsertSuccess;
572 nInsertFailed += pThread->m_nInsertFailed;
575 delete_thread * p = dynamic_cast<delete_thread *>( *it );
577 nDeleteSuccess += p->m_nDeleteSuccess;
578 nDeleteFailed += p->m_nDeleteFailed;
581 extract_thread * pExtract = dynamic_cast<extract_thread *>( *it );
583 nExtractSuccess += pExtract->m_nDeleteSuccess;
584 nExtractFailed += pExtract->m_nDeleteFailed;
589 CPPUNIT_MSG( " Totals (success/failed): \n\t"
590 << " Insert=" << nInsertSuccess << '/' << nInsertFailed << "\n\t"
591 << " Delete=" << nDeleteSuccess << '/' << nDeleteFailed << "\n\t"
592 << " Extract=" << nExtractSuccess << '/' << nExtractFailed << "\n\t"
594 CPPUNIT_CHECK( nInsertSuccess == c_nMapSize * c_nInsThreadCount );
595 CPPUNIT_CHECK( nInsertFailed == 0 );
601 void analyze( Map& testMap )
603 cds::OS::Timer timer;
605 // All even keys must be in the map
607 size_t nErrorCount = 0;
608 CPPUNIT_MSG( " Check even keys..." );
609 for ( size_t n = 0; n < c_nMapSize; n +=2 ) {
610 for ( size_t i = 0; i < c_nInsThreadCount; ++i ) {
611 if ( !testMap.find( key_type(n, i) ) ) {
612 if ( ++nErrorCount < 10 ) {
613 CPPUNIT_MSG( "key " << n << "-" << i << " is not found!");
618 CPPUNIT_CHECK_EX( nErrorCount == 0, "Totals: " << nErrorCount << " keys is not found");
621 check_before_cleanup( testMap );
623 CPPUNIT_MSG( " Clear map (single-threaded)..." );
626 CPPUNIT_MSG( " Duration=" << timer.duration() );
627 CPPUNIT_CHECK_EX( testMap.empty(), ((long long) testMap.size()) );
629 additional_check( testMap );
630 print_stat( testMap );
632 additional_cleanup( testMap );
639 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
640 << " delete thread count=" << c_nDelThreadCount
641 << " set size=" << c_nMapSize
644 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
645 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
646 do_test<Map>( nLoadFactor );
647 if ( c_bPrintGCState )
655 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
656 << ", delete=" << c_nDelThreadCount
657 << ", extract=" << c_nExtractThreadCount
658 << "; set size=" << c_nMapSize
661 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
662 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
663 do_test_extract<Map>( nLoadFactor );
664 if ( c_bPrintGCState )
672 CPPUNIT_MSG( "Insert thread count=" << c_nInsThreadCount
673 << " delete thread count=" << c_nDelThreadCount
674 << " set size=" << c_nMapSize
679 if ( c_bPrintGCState )
684 void test_nolf_extract()
686 CPPUNIT_MSG( "Thread count: insert=" << c_nInsThreadCount
687 << ", delete=" << c_nDelThreadCount
688 << ", extract=" << c_nExtractThreadCount
689 << "; set size=" << c_nMapSize
693 do_test_extract_with( s );
694 if ( c_bPrintGCState )
698 void setUpParams( const CppUnitMini::TestCfg& cfg );
700 void run_MichaelMap(const char *in_name, bool invert = false);
701 void run_SplitList(const char *in_name, bool invert = false);
702 //void run_StripedMap(const char *in_name, bool invert = false);
703 //void run_RefinableMap(const char *in_name, bool invert = false);
704 void run_CuckooMap(const char *in_name, bool invert = false);
705 void run_SkipListMap(const char *in_name, bool invert = false);
706 void run_EllenBinTreeMap(const char *in_name, bool invert = false);
707 void run_BronsonAVLTreeMap(const char *in_name, bool invert = false);
708 //void run_StdMap(const char *in_name, bool invert = false);
710 virtual void myRun(const char *in_name, bool invert = false);
712 # include "map2/map_defs.h"
713 CDSUNIT_DECLARE_MichaelMap
714 CDSUNIT_DECLARE_SplitList
715 //CDSUNIT_DECLARE_StripedMap
716 //CDSUNIT_DECLARE_RefinableMap
717 CDSUNIT_DECLARE_CuckooMap
718 CDSUNIT_DECLARE_SkipListMap
719 CDSUNIT_DECLARE_EllenBinTreeMap
720 CDSUNIT_DECLARE_BronsonAVLTreeMap
721 //CDSUNIT_DECLARE_StdMap