Removing unused vars
[libcds.git] / tests / unit / map2 / map_insdel_func.cpp
1 //$$CDS-header$$
2
3 #include <functional>
4 #include <mutex>    //unique_lock
5 #include "map2/map_types.h"
6 #include "cppunit/thread.h"
7
8 #include <cds/lock/spinlock.h>
9 #include <vector>
10 #include <algorithm>    // random_shuffle
11
12 namespace map2 {
13
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)
18
19     namespace {
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;
27     }
28
29     class Map_InsDel_func: public CppUnitMini::TestCase
30     {
31         typedef size_t  key_type;
32         struct value_type {
33             size_t      nKey;
34             size_t      nData;
35             atomics::atomic<size_t> nEnsureCall;
36             atomics::atomic<bool>   bInitialized;
37             cds::OS::ThreadId          threadId     ;   // insert thread id
38
39             typedef cds::lock::Spinlock< cds::backoff::pause >   lock_type;
40             mutable lock_type   m_access;
41
42             value_type()
43                 : nKey(0)
44                 , nData(0)
45                 , nEnsureCall(0)
46                 , bInitialized( false )
47                 , threadId( cds::OS::getCurrentThreadId() )
48             {}
49
50             value_type( value_type const& s )
51                 : nKey(s.nKey)
52                 , nData(s.nData)
53                 , nEnsureCall(s.nEnsureCall.load(atomics::memory_order_relaxed))
54                 , bInitialized( s.bInitialized.load(atomics::memory_order_relaxed) )
55                 , threadId( cds::OS::getCurrentThreadId() )
56             {}
57
58             // boost::container::flat_map requires operator =
59             value_type& operator=( value_type const& v )
60             {
61                 nKey = v.nKey;
62                 nData = v.nData;
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);
65
66                 return *this;
67             }
68         };
69
70         typedef std::vector<key_type>   key_array;
71         key_array                       m_arrValues;
72
73         template <class Map>
74         class Inserter: public CppUnitMini::TestThread
75         {
76             Map&     m_Map;
77             typedef typename Map::value_type pair_type;
78
79             virtual Inserter *    clone()
80             {
81                 return new Inserter( *this );
82             }
83
84             struct insert_functor {
85                 size_t nTestFunctorRef;
86
87                 insert_functor()
88                     : nTestFunctorRef(0)
89                 {}
90
91                 void operator()( pair_type& val )
92                 {
93                     std::unique_lock< typename value_type::lock_type>    ac( val.second.m_access );
94
95                     val.second.nKey  = val.first;
96                     val.second.nData = val.first * 8;
97
98                     ++nTestFunctorRef;
99                     val.second.bInitialized.store( true, atomics::memory_order_relaxed);
100                 }
101             };
102
103         public:
104             size_t  m_nInsertSuccess;
105             size_t  m_nInsertFailed;
106
107             size_t  m_nTestFunctorRef;
108
109         public:
110             Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
111                 : CppUnitMini::TestThread( pool )
112                 , m_Map( rMap )
113             {}
114             Inserter( Inserter& src )
115                 : CppUnitMini::TestThread( src )
116                 , m_Map( src.m_Map )
117             {}
118
119             Map_InsDel_func&  getTest()
120             {
121                 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
122             }
123
124             virtual void init() { cds::threading::Manager::attachThread()   ; }
125             virtual void fini() { cds::threading::Manager::detachThread()   ; }
126
127             virtual void test()
128             {
129                 Map& rMap = m_Map;
130
131                 m_nInsertSuccess =
132                     m_nInsertFailed =
133                     m_nTestFunctorRef = 0;
134
135                 // func is passed by reference
136                 insert_functor  func;
137                 key_array const& arr = getTest().m_arrValues;
138
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) ) )
143                                 ++m_nInsertSuccess;
144                             else
145                                 ++m_nInsertFailed;
146                         }
147                     }
148                 }
149                 else {
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) ) )
153                                 ++m_nInsertSuccess;
154                             else
155                                 ++m_nInsertFailed;
156                         }
157                     }
158                 }
159
160                 m_nTestFunctorRef = func.nTestFunctorRef;
161             }
162         };
163
164         template <class Map>
165         class Ensurer: public CppUnitMini::TestThread
166         {
167             Map&     m_Map;
168             typedef typename Map::value_type pair_type;
169
170             virtual Ensurer *    clone()
171             {
172                 return new Ensurer( *this );
173             }
174
175             struct ensure_functor {
176                 size_t  nCreated;
177                 size_t  nModified;
178
179                 ensure_functor()
180                     : nCreated(0)
181                     , nModified(0)
182                 {}
183
184                 void operator()( bool bNew, pair_type& val )
185                 {
186                     std::unique_lock<typename value_type::lock_type>    ac( val.second.m_access );
187                     if ( bNew ) {
188                         ++nCreated;
189                         val.second.nKey = val.first;
190                         val.second.nData = val.first * 8;
191                         val.second.bInitialized.store( true, atomics::memory_order_relaxed);
192                     }
193                     else {
194                         val.second.nEnsureCall.fetch_add( 1, atomics::memory_order_relaxed );
195                         ++nModified;
196                     }
197                 }
198             private:
199                 ensure_functor(const ensure_functor& );
200             };
201
202         public:
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;
208
209         public:
210             Ensurer( CppUnitMini::ThreadPool& pool, Map& rMap )
211                 : CppUnitMini::TestThread( pool )
212                 , m_Map( rMap )
213             {}
214             Ensurer( Ensurer& src )
215                 : CppUnitMini::TestThread( src )
216                 , m_Map( src.m_Map )
217             {}
218
219             Map_InsDel_func&  getTest()
220             {
221                 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
222             }
223
224             virtual void init() { cds::threading::Manager::attachThread()   ; }
225             virtual void fini() { cds::threading::Manager::detachThread()   ; }
226
227             virtual void test()
228             {
229                 Map& rMap = m_Map;
230
231                 m_nEnsureCreated =
232                     m_nEnsureExisted =
233                     m_nEnsureFailed = 0;
234
235                 ensure_functor func;
236
237                 key_array const& arr = getTest().m_arrValues;
238
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 ) );
244                             if ( ret.first  ) {
245                                 if ( ret.second )
246                                     ++m_nEnsureCreated;
247                                 else
248                                     ++m_nEnsureExisted;
249                             }
250                             else
251                                 ++m_nEnsureFailed;
252                         }
253                     }
254                 }
255                 else {
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 ) );
260                             if ( ret.first  ) {
261                                 if ( ret.second )
262                                     ++m_nEnsureCreated;
263                                 else
264                                     ++m_nEnsureExisted;
265                             }
266                             else
267                                 ++m_nEnsureFailed;
268                         }
269                     }
270                 }
271
272                 m_nFunctorCreated = func.nCreated;
273                 m_nFunctorModified = func.nModified;
274             }
275         };
276
277         template <class Map>
278         class Deleter: public CppUnitMini::TestThread
279         {
280             Map&     m_Map;
281             typedef typename Map::mapped_type value_type;
282             typedef typename Map::value_type pair_type;
283
284             virtual Deleter *    clone()
285             {
286                 return new Deleter( *this );
287             }
288
289             struct value_container
290             {
291                 size_t      nKeyExpected;
292
293                 size_t      nSuccessItem;
294                 size_t      nFailedItem;
295
296                 value_container()
297                     : nSuccessItem(0)
298                     , nFailedItem(0)
299                 {}
300             };
301
302             struct erase_functor {
303                 value_container     m_cnt;
304
305                 void operator ()( pair_type& item )
306                 {
307                     while ( true ) {
308                         if ( item.second.bInitialized.load( atomics::memory_order_relaxed )) {
309                             std::unique_lock< typename value_type::lock_type>    ac( item.second.m_access );
310
311                             if ( m_cnt.nKeyExpected == item.second.nKey && m_cnt.nKeyExpected * 8 == item.second.nData )
312                                 ++m_cnt.nSuccessItem;
313                             else
314                                 ++m_cnt.nFailedItem;
315                             item.second.nData++;
316                             item.second.nKey = 0;
317                             break;
318                         }
319                         else
320                             cds::backoff::yield()();
321                     }
322                 }
323             };
324
325         public:
326             size_t  m_nDeleteSuccess;
327             size_t  m_nDeleteFailed;
328
329             size_t  m_nValueSuccess;
330             size_t  m_nValueFailed;
331
332         public:
333             Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
334                 : CppUnitMini::TestThread( pool )
335                 , m_Map( rMap )
336             {}
337             Deleter( Deleter& src )
338                 : CppUnitMini::TestThread( src )
339                 , m_Map( src.m_Map )
340             {}
341
342             Map_InsDel_func&  getTest()
343             {
344                 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
345             }
346
347             virtual void init() { cds::threading::Manager::attachThread()   ; }
348             virtual void fini() { cds::threading::Manager::detachThread()   ; }
349
350             virtual void test()
351             {
352                 Map& rMap = m_Map;
353
354                 m_nDeleteSuccess =
355                     m_nDeleteFailed = 0;
356
357                 erase_functor   func;
358                 key_array const& arr = getTest().m_arrValues;
359
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) ))
365                                 ++m_nDeleteSuccess;
366                             else
367                                 ++m_nDeleteFailed;
368                         }
369                     }
370                 }
371                 else {
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) ))
376                                 ++m_nDeleteSuccess;
377                             else
378                                 ++m_nDeleteFailed;
379                         }
380                     }
381                 }
382
383                 m_nValueSuccess = func.m_cnt.nSuccessItem;
384                 m_nValueFailed = func.m_cnt.nFailedItem;
385             }
386         };
387
388     protected:
389
390         template <class Map>
391         void do_test( Map& testMap )
392         {
393             typedef Inserter<Map>       InserterThread;
394             typedef Deleter<Map>        DeleterThread;
395             typedef Ensurer<Map>        EnsurerThread;
396             cds::OS::Timer    timer;
397
398             m_arrValues.clear();
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() );
403
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 );
408             pool.run();
409             CPPUNIT_MSG( "   Duration=" << pool.avgDuration() );
410
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;
423
424             for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
425                 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
426                 if ( pThread ) {
427                     nInsertSuccess += pThread->m_nInsertSuccess;
428                     nInsertFailed += pThread->m_nInsertFailed;
429                     nTestFunctorRef += pThread->m_nTestFunctorRef;
430                 }
431                 else {
432                     DeleterThread * p = dynamic_cast<DeleterThread *>( *it );
433                     if ( p ) {
434                         nDeleteSuccess += p->m_nDeleteSuccess;
435                         nDeleteFailed += p->m_nDeleteFailed;
436                         nDelValueSuccess += p->m_nValueSuccess;
437                         nDelValueFailed += p->m_nValueFailed;
438                     }
439                     else {
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;
446                     }
447                 }
448             }
449
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()
457                 );
458
459             CPPUNIT_CHECK_EX( nDelValueFailed == 0, "Functor del failed=" << nDelValueFailed );
460             CPPUNIT_CHECK_EX( nDelValueSuccess == nDeleteSuccess,  "Delete success=" << nDeleteSuccess << " functor=" << nDelValueSuccess );
461
462             CPPUNIT_CHECK( nEnsureFailed == 0 );
463
464             CPPUNIT_CHECK_EX( nEnsureCreated == nEnsFuncCreated, "Ensure created=" << nEnsureCreated << " functor=" << nEnsFuncCreated );
465             CPPUNIT_CHECK_EX( nEnsureModified == nEnsFuncModified, "Ensure modified=" << nEnsureModified << " functor=" << nEnsFuncModified );
466
467             // nTestFunctorRef is call count of insert functor
468             CPPUNIT_CHECK_EX( nTestFunctorRef == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nTestFunctorRef=" << nTestFunctorRef );
469
470             CPPUNIT_MSG( "  Clear map (single-threaded)..." );
471             timer.reset();
472             for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
473                 testMap.erase( nItem );
474             }
475             CPPUNIT_MSG( "   Duration=" << timer.duration() );
476             CPPUNIT_CHECK( testMap.empty() );
477
478             additional_check( testMap );
479             print_stat( testMap );
480             additional_cleanup( testMap );
481         }
482
483         template <class Map>
484         void test()
485         {
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
491                 );
492
493             for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
494                 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
495                 Map  testMap( c_nMapSize, nLoadFactor );
496                 do_test( testMap );
497                 if ( c_bPrintGCState )
498                     print_gc_state();
499             }
500
501         }
502
503         template <class Map>
504         void test_nolf()
505         {
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
511                 );
512
513             Map testMap;
514             do_test( testMap );
515             if ( c_bPrintGCState )
516                 print_gc_state();
517         }
518
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 );
527         }
528
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
537
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()
547
548     };
549
550     CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_func );
551 } // namespace map2