2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 class Set_InsDel_func: public cds_test::stress_fixture
38 static size_t s_nSetSize; // set size
39 static size_t s_nInsertThreadCount; // count of insertion thread
40 static size_t s_nDeleteThreadCount; // count of deletion thread
41 static size_t s_nUpdateThreadCount; // count of updating thread
42 static size_t s_nThreadPassCount; // pass count for each thread
43 static size_t s_nFeldmanThreadPassCount; // pass count for Feldman
44 static size_t s_nMaxLoadFactor; // maximum load factor
46 static size_t s_nCuckooInitialSize; // initial size for CuckooSet
47 static size_t s_nCuckooProbesetSize; // CuckooSet probeset size (only for list-based probeset)
48 static size_t s_nCuckooProbesetThreshold; // CUckooSet probeset threshold (0 - use default)
50 static size_t s_nFeldmanSet_HeadBits;
51 static size_t s_nFeldmanSet_ArrayBits;
53 static size_t s_nLoadFactor;
55 static void SetUpTestCase();
56 //static void TearDownTestCase();
59 typedef size_t key_type;
64 atomics::atomic<size_t> nUpdateCall;
65 bool volatile bInitialized;
66 cds::OS::ThreadId threadId; // insert thread id
68 typedef cds::sync::spin_lock< cds::backoff::pause > lock_type;
69 mutable lock_type m_access;
75 , bInitialized( false )
76 , threadId( cds::OS::get_current_thread_id())
79 value( value const& s )
82 , nUpdateCall(s.nUpdateCall.load(atomics::memory_order_relaxed))
83 , bInitialized( s.bInitialized )
84 , threadId( cds::OS::get_current_thread_id())
88 // boost::container::flat_map requires operator =
89 // cppcheck-suppress operatorEqVarError
90 value& operator=( value const& v )
94 threadId = v.threadId;
95 nUpdateCall.store( v.nUpdateCall.load(atomics::memory_order_relaxed), atomics::memory_order_relaxed );
96 bInitialized = v.bInitialized;
102 size_t * m_pKeyFirst;
104 std::unique_ptr< size_t[] > m_pKeyArr;
113 class Inserter: public cds_test::thread
115 typedef cds_test::thread base_class;
118 typedef typename Set::value_type keyval_type;
120 struct insert_functor {
121 size_t nTestFunctorRef;
126 insert_functor( insert_functor const& ) = delete;
128 void operator()( keyval_type& val )
130 std::unique_lock< typename value::lock_type> ac( val.val.m_access );
132 val.val.nKey = val.key;
133 val.val.nData = val.key * 8;
136 val.val.bInitialized = true;
141 size_t m_nInsertSuccess = 0;
142 size_t m_nInsertFailed = 0;
143 size_t m_nTestFunctorRef = 0;
146 Inserter( cds_test::thread_pool& pool, Set& set )
147 : base_class( pool, insert_thread )
151 Inserter( Inserter& src )
156 virtual thread * clone()
158 return new Inserter( *this );
164 Set_InsDel_func& fixture = pool().template fixture<Set_InsDel_func>();
166 size_t * pKeyFirst = fixture.m_pKeyFirst;
167 size_t * pKeyLast = fixture.m_pKeyLast;
168 size_t const nPassCount = fixture.s_nThreadPassCount;
170 // func is passed by reference
172 for (size_t *p = pKeyFirst; p < pKeyLast; ++p) {
173 if (rSet.insert(*p, std::ref(func)))
182 class Updater: public cds_test::thread
184 typedef cds_test::thread base_class;
187 typedef typename Set::value_type keyval_type;
189 struct update_functor {
191 size_t nModified = 0;
194 update_functor( const update_functor& ) = delete;
196 void operator()( bool bNew, keyval_type& val, size_t /*nKey*/ )
198 std::unique_lock<typename value::lock_type> ac( val.val.m_access );
199 if ( !val.val.bInitialized )
201 val.val.nKey = val.key;
202 val.val.nData = val.key * 8;
203 val.val.bInitialized = true;
210 val.val.nUpdateCall.fetch_add( 1, atomics::memory_order_relaxed );
215 void operator()( keyval_type& cur, keyval_type * old )
217 operator()( old == nullptr, cur, 0 );
222 size_t m_nUpdateFailed = 0;
223 size_t m_nUpdateCreated = 0;
224 size_t m_nUpdateExisted = 0;
225 size_t m_nFunctorCreated = 0;
226 size_t m_nFunctorModified = 0;
229 Updater( cds_test::thread_pool& pool, Set& set )
230 : base_class( pool, update_thread )
234 Updater( Updater& src )
239 virtual thread * clone()
241 return new Updater( *this );
248 Set_InsDel_func& fixture = pool().template fixture<Set_InsDel_func>();
249 size_t * pKeyFirst = fixture.m_pKeyFirst;
250 size_t * pKeyLast = fixture.m_pKeyLast;
251 size_t const nPassCount = fixture.s_nThreadPassCount;
255 for (size_t *p = pKeyFirst; p < pKeyLast; ++p) {
256 std::pair<bool, bool> ret =
257 rSet.update(*p, std::ref(func), true);
270 class Deleter: public cds_test::thread
272 typedef cds_test::thread base_class;
275 typedef typename Set::value_type keyval_type;
277 struct value_container
281 size_t nSuccessItem = 0;
282 size_t nFailedItem = 0;
285 struct erase_functor {
286 value_container m_cnt;
288 void operator ()( keyval_type const& itm )
290 keyval_type& item = const_cast<keyval_type&>(itm);
294 std::unique_lock< typename value::lock_type> ac( item.val.m_access );
295 if ( item.val.bInitialized ) {
296 if ( m_cnt.nKeyExpected == item.val.nKey && m_cnt.nKeyExpected * 8 == item.val.nData )
297 ++m_cnt.nSuccessItem;
308 cds::backoff::yield()();
314 size_t m_nDeleteSuccess = 0;
315 size_t m_nDeleteFailed = 0;
316 size_t m_nValueSuccess = 0;
317 size_t m_nValueFailed = 0;
320 Deleter( cds_test::thread_pool& pool, Set& set )
321 : base_class( pool, delete_thread )
325 Deleter( Deleter& src )
330 virtual thread * clone()
332 return new Deleter( *this );
339 Set_InsDel_func& fixture = pool().template fixture<Set_InsDel_func>();
340 size_t * pKeyFirst = fixture.m_pKeyFirst;
341 size_t * pKeyLast = fixture.m_pKeyLast;
342 size_t const nPassCount = fixture.s_nThreadPassCount;
346 for (size_t *p = pKeyFirst; p < pKeyLast; ++p) {
347 func.m_cnt.nKeyExpected = *p;
348 if (rSet.erase(*p, std::ref(func)))
359 void run_test( Set& testSet, size_t pass_count)
361 typedef Inserter<Set> InserterThread;
362 typedef Deleter<Set> DeleterThread;
363 typedef Updater<Set> UpdaterThread;
365 m_pKeyArr.reset( new size_t[ s_nSetSize ] );
366 m_pKeyFirst = m_pKeyArr.get();
367 m_pKeyLast = m_pKeyFirst + s_nSetSize;
368 for ( size_t i = 0; i < s_nSetSize; ++i )
370 shuffle( m_pKeyFirst, m_pKeyLast );
372 cds_test::thread_pool &pool = get_pool();
373 std::unique_ptr<InserterThread> inserter(
374 new InserterThread(pool, testSet));
375 std::unique_ptr<DeleterThread> deleter(
376 new DeleterThread(pool, testSet));
377 std::unique_ptr<UpdaterThread> updater(
378 new UpdaterThread(pool, testSet));
380 for (size_t i = 0; i < pass_count; i++) {
387 for (size_t *p = m_pKeyFirst; p != m_pKeyLast; ++p)
389 EXPECT_TRUE( testSet.empty());
390 EXPECT_EQ( testSet.size(), 0u );
397 run_test(s, s_nThreadPassCount);
404 run_test(s, s_nFeldmanThreadPassCount);
411 run_test( s, s_nThreadPassCount);
415 class Set_InsDel_func_LF: public Set_InsDel_func
416 , public ::testing::WithParamInterface<size_t>
422 s_nLoadFactor = GetParam();
423 propout() << std::make_pair( "load_factor", s_nLoadFactor );
424 Set_InsDel_func::run_test<Set>();
430 s_nLoadFactor = GetParam();
431 propout() << std::make_pair( "load_factor", s_nLoadFactor );
432 Set_InsDel_func::run_test2<Set>();
435 static std::vector<size_t> get_load_factors();