4 #include <mutex> //unique_lock
5 #include "map2/map_types.h"
6 #include "cppunit/thread.h"
8 #include <cds/lock/spinlock.h>
10 #include <algorithm> // random_shuffle
14 # define TEST_MAP(X) void X() { test<MapTypes<key_type, value_type>::X >() ; }
15 # define TEST_MAP_EXTRACT(X) TEST_MAP(X)
16 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >() ; }
17 # define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
20 static size_t c_nMapSize = 1000000 ; // map size
21 static size_t c_nInsertThreadCount = 4; // count of insertion thread
22 static size_t c_nDeleteThreadCount = 4; // count of deletion thread
23 static size_t c_nEnsureThreadCount = 4; // count of ensure thread
24 static size_t c_nThreadPassCount = 4 ; // pass count for each thread
25 static size_t c_nMaxLoadFactor = 8 ; // maximum load factor
26 static bool c_bPrintGCState = true;
29 class Map_InsDel_func: public CppUnitMini::TestCase
31 typedef size_t key_type;
35 atomics::atomic<size_t> nEnsureCall;
36 atomics::atomic<bool> bInitialized;
37 cds::OS::ThreadId threadId ; // insert thread id
39 typedef cds::lock::Spinlock< cds::backoff::pause > lock_type;
40 mutable lock_type m_access;
46 , bInitialized( false )
47 , threadId( cds::OS::getCurrentThreadId() )
50 value_type( value_type const& s )
53 , nEnsureCall(s.nEnsureCall.load(atomics::memory_order_relaxed))
54 , bInitialized( s.bInitialized.load(atomics::memory_order_relaxed) )
55 , threadId( cds::OS::getCurrentThreadId() )
58 // boost::container::flat_map requires operator =
59 value_type& operator=( value_type const& v )
63 nEnsureCall.store( v.nEnsureCall.load(atomics::memory_order_relaxed), atomics::memory_order_relaxed );
64 bInitialized.store(v.bInitialized.load(atomics::memory_order_relaxed), atomics::memory_order_relaxed);
70 typedef std::vector<key_type> key_array;
71 key_array m_arrValues;
74 class Inserter: public CppUnitMini::TestThread
77 typedef typename Map::value_type pair_type;
79 virtual Inserter * clone()
81 return new Inserter( *this );
84 struct insert_functor {
85 size_t nTestFunctorRef;
91 void operator()( pair_type& val )
93 std::unique_lock< typename value_type::lock_type> ac( val.second.m_access );
95 val.second.nKey = val.first;
96 val.second.nData = val.first * 8;
99 val.second.bInitialized.store( true, atomics::memory_order_relaxed);
104 size_t m_nInsertSuccess;
105 size_t m_nInsertFailed;
107 size_t m_nTestFunctorRef;
110 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
111 : CppUnitMini::TestThread( pool )
114 Inserter( Inserter& src )
115 : CppUnitMini::TestThread( src )
119 Map_InsDel_func& getTest()
121 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
124 virtual void init() { cds::threading::Manager::attachThread() ; }
125 virtual void fini() { cds::threading::Manager::detachThread() ; }
133 m_nTestFunctorRef = 0;
135 // func is passed by reference
137 key_array const& arr = getTest().m_arrValues;
139 if ( m_nThreadNo & 1 ) {
140 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
141 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
142 if ( rMap.insert_key( *it, std::ref(func) ) )
150 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
151 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
152 if ( rMap.insert_key( *it, std::ref(func) ) )
160 m_nTestFunctorRef = func.nTestFunctorRef;
165 class Ensurer: public CppUnitMini::TestThread
168 typedef typename Map::value_type pair_type;
170 virtual Ensurer * clone()
172 return new Ensurer( *this );
175 struct ensure_functor {
184 void operator()( bool bNew, pair_type& val )
186 std::unique_lock<typename value_type::lock_type> ac( val.second.m_access );
189 val.second.nKey = val.first;
190 val.second.nData = val.first * 8;
191 val.second.bInitialized.store( true, atomics::memory_order_relaxed);
194 val.second.nEnsureCall.fetch_add( 1, atomics::memory_order_relaxed );
199 ensure_functor(const ensure_functor& );
203 size_t m_nEnsureFailed;
204 size_t m_nEnsureCreated;
205 size_t m_nEnsureExisted;
206 size_t m_nFunctorCreated;
207 size_t m_nFunctorModified;
210 Ensurer( CppUnitMini::ThreadPool& pool, Map& rMap )
211 : CppUnitMini::TestThread( pool )
214 Ensurer( Ensurer& src )
215 : CppUnitMini::TestThread( src )
219 Map_InsDel_func& getTest()
221 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
224 virtual void init() { cds::threading::Manager::attachThread() ; }
225 virtual void fini() { cds::threading::Manager::detachThread() ; }
237 key_array const& arr = getTest().m_arrValues;
239 if ( m_nThreadNo & 1 ) {
240 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
241 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
242 //for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
243 std::pair<bool, bool> ret = rMap.ensure( *it, std::ref( func ) );
256 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
257 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
258 //for ( size_t nItem = c_nMapSize; nItem > 0; --nItem ) {
259 std::pair<bool, bool> ret = rMap.ensure( *it, std::ref( func ) );
272 m_nFunctorCreated = func.nCreated;
273 m_nFunctorModified = func.nModified;
278 class Deleter: public CppUnitMini::TestThread
281 typedef typename Map::mapped_type value_type;
282 typedef typename Map::value_type pair_type;
284 virtual Deleter * clone()
286 return new Deleter( *this );
289 struct value_container
302 struct erase_functor {
303 value_container m_cnt;
305 void operator ()( pair_type& item )
308 if ( item.second.bInitialized.load( atomics::memory_order_relaxed )) {
309 std::unique_lock< typename value_type::lock_type> ac( item.second.m_access );
311 if ( m_cnt.nKeyExpected == item.second.nKey && m_cnt.nKeyExpected * 8 == item.second.nData )
312 ++m_cnt.nSuccessItem;
316 item.second.nKey = 0;
320 cds::backoff::yield()();
326 size_t m_nDeleteSuccess;
327 size_t m_nDeleteFailed;
329 size_t m_nValueSuccess;
330 size_t m_nValueFailed;
333 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
334 : CppUnitMini::TestThread( pool )
337 Deleter( Deleter& src )
338 : CppUnitMini::TestThread( src )
342 Map_InsDel_func& getTest()
344 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
347 virtual void init() { cds::threading::Manager::attachThread() ; }
348 virtual void fini() { cds::threading::Manager::detachThread() ; }
358 key_array const& arr = getTest().m_arrValues;
360 if ( m_nThreadNo & 1 ) {
361 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
362 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
363 func.m_cnt.nKeyExpected = *it;
364 if ( rMap.erase( *it, std::ref(func) ))
372 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
373 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
374 func.m_cnt.nKeyExpected = *it;
375 if ( rMap.erase( *it, std::ref(func) ))
383 m_nValueSuccess = func.m_cnt.nSuccessItem;
384 m_nValueFailed = func.m_cnt.nFailedItem;
391 void do_test( Map& testMap )
393 typedef Inserter<Map> InserterThread;
394 typedef Deleter<Map> DeleterThread;
395 typedef Ensurer<Map> EnsurerThread;
396 cds::OS::Timer timer;
399 m_arrValues.reserve( c_nMapSize );
400 for ( size_t i = 0; i < c_nMapSize; ++i )
401 m_arrValues.push_back( i );
402 std::random_shuffle( m_arrValues.begin(), m_arrValues.end() );
404 CppUnitMini::ThreadPool pool( *this );
405 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
406 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
407 pool.add( new EnsurerThread( pool, testMap ), c_nEnsureThreadCount );
409 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
411 size_t nInsertSuccess = 0;
412 size_t nInsertFailed = 0;
413 size_t nDeleteSuccess = 0;
414 size_t nDeleteFailed = 0;
415 size_t nDelValueSuccess = 0;
416 size_t nDelValueFailed = 0;
417 size_t nEnsureFailed = 0;
418 size_t nEnsureCreated = 0;
419 size_t nEnsureModified = 0;
420 size_t nEnsFuncCreated = 0;
421 size_t nEnsFuncModified = 0;
422 size_t nTestFunctorRef = 0;
424 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
425 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
427 nInsertSuccess += pThread->m_nInsertSuccess;
428 nInsertFailed += pThread->m_nInsertFailed;
429 nTestFunctorRef += pThread->m_nTestFunctorRef;
432 DeleterThread * p = dynamic_cast<DeleterThread *>( *it );
434 nDeleteSuccess += p->m_nDeleteSuccess;
435 nDeleteFailed += p->m_nDeleteFailed;
436 nDelValueSuccess += p->m_nValueSuccess;
437 nDelValueFailed += p->m_nValueFailed;
440 EnsurerThread * pEns = static_cast<EnsurerThread *>( *it );
441 nEnsureCreated += pEns->m_nEnsureCreated;
442 nEnsureModified += pEns->m_nEnsureExisted;
443 nEnsureFailed += pEns->m_nEnsureFailed;
444 nEnsFuncCreated += pEns->m_nFunctorCreated;
445 nEnsFuncModified += pEns->m_nFunctorModified;
450 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
451 << " Del succ=" << nDeleteSuccess << "\n"
452 << " : Ins fail=" << nInsertFailed
453 << " Del fail=" << nDeleteFailed << "\n"
454 << " : Ensure succ=" << (nEnsureCreated + nEnsureModified) << " fail=" << nEnsureFailed
455 << " create=" << nEnsureCreated << " modify=" << nEnsureModified << "\n"
456 << " Map size=" << testMap.size()
459 CPPUNIT_CHECK_EX( nDelValueFailed == 0, "Functor del failed=" << nDelValueFailed );
460 CPPUNIT_CHECK_EX( nDelValueSuccess == nDeleteSuccess, "Delete success=" << nDeleteSuccess << " functor=" << nDelValueSuccess );
462 CPPUNIT_CHECK( nEnsureFailed == 0 );
464 CPPUNIT_CHECK_EX( nEnsureCreated == nEnsFuncCreated, "Ensure created=" << nEnsureCreated << " functor=" << nEnsFuncCreated );
465 CPPUNIT_CHECK_EX( nEnsureModified == nEnsFuncModified, "Ensure modified=" << nEnsureModified << " functor=" << nEnsFuncModified );
467 // nTestFunctorRef is call count of insert functor
468 CPPUNIT_CHECK_EX( nTestFunctorRef == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nTestFunctorRef=" << nTestFunctorRef );
470 CPPUNIT_MSG( " Clear map (single-threaded)..." );
472 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
473 testMap.erase( nItem );
475 CPPUNIT_MSG( " Duration=" << timer.duration() );
476 CPPUNIT_CHECK( testMap.empty() );
478 additional_check( testMap );
479 print_stat( testMap );
480 additional_cleanup( testMap );
486 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
487 << " delete=" << c_nDeleteThreadCount
488 << " ensure=" << c_nEnsureThreadCount
489 << " pass count=" << c_nThreadPassCount
490 << " map size=" << c_nMapSize
493 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
494 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
495 Map testMap( c_nMapSize, nLoadFactor );
497 if ( c_bPrintGCState )
506 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
507 << " delete=" << c_nDeleteThreadCount
508 << " ensure=" << c_nEnsureThreadCount
509 << " pass count=" << c_nThreadPassCount
510 << " map size=" << c_nMapSize
515 if ( c_bPrintGCState )
519 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
520 c_nInsertThreadCount = cfg.getULong("InsertThreadCount", 4 );
521 c_nDeleteThreadCount = cfg.getULong("DeleteThreadCount", 4 );
522 c_nEnsureThreadCount = cfg.getULong("EnsureThreadCount", 4 );
523 c_nThreadPassCount = cfg.getULong("ThreadPassCount", 4 );
524 c_nMapSize = cfg.getULong("MapSize", 1000000 );
525 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
526 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
529 # include "map2/map_defs.h"
530 CDSUNIT_DECLARE_MichaelMap
531 CDSUNIT_DECLARE_SplitList
532 CDSUNIT_DECLARE_SkipListMap
533 CDSUNIT_DECLARE_EllenBinTreeMap
534 CDSUNIT_DECLARE_StripedMap
535 CDSUNIT_DECLARE_RefinableMap
536 CDSUNIT_DECLARE_CuckooMap
538 CPPUNIT_TEST_SUITE( Map_InsDel_func )
539 CDSUNIT_TEST_MichaelMap
540 CDSUNIT_TEST_SplitList
541 CDSUNIT_TEST_SkipListMap
542 CDSUNIT_TEST_EllenBinTreeMap
543 CDSUNIT_TEST_StripedMap
544 CDSUNIT_TEST_RefinableMap
545 CDSUNIT_TEST_CuckooMap
546 CPPUNIT_TEST_SUITE_END()
550 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_func );