2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
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.
31 // Larson allocator test
33 #include "alloc/michael_allocator.h"
34 #include "alloc/random_gen.h"
36 #include <cds/os/timer.h>
37 #include <cds/os/topology.h>
39 #include "cppunit/thread.h"
43 static size_t s_nMaxThreadCount = 32;
44 static unsigned int s_nMinBlockSize = 8;
45 static unsigned int s_nMaxBlockSize = 1024;
46 static size_t s_nBlocksPerThread = 1000;
47 static size_t s_nPassCount = 100000;
49 static size_t s_nPassPerThread;
52 # define TEST_ALLOC(X, CLASS) void X() { test< CLASS >(false) ; }
53 # define TEST_ALLOC_STAT(X, CLASS) void X() { test< CLASS >(true) ; }
56 In this test, initially one thread allocates and frees random
57 sized blocks (s_nMinBlockSize to s_nMaxBlockSize bytes) in random order, then an
58 equal number of blocks (s_nBlocksPerThread) is handed over to each of the
59 remaining threads. In the parallel phase, each thread randomly selects a block and
60 frees it, then allocates a new random-sized block in its place.
61 The benchmark measures the duration of s_nPassCount free/malloc pairs
62 during the parallel phase. Larson captures the robustness of malloc
\92s latency
63 and scalability under irregular allocation patterns with respect to block-size
64 and order of deallocation over a long period of time.
66 class Larson: public CppUnitMini::TestCase
68 typedef char ** thread_data;
70 thread_data * m_aThreadData;
73 template <class ALLOC>
74 class Thread: public CppUnitMini::TestThread
77 typedef typename ALLOC::value_type value_type;
79 virtual Thread * clone()
81 return new Thread( *this );
84 randomGen<size_t> m_rndGen;
89 Thread( CppUnitMini::ThreadPool& pool, ALLOC& a )
90 : CppUnitMini::TestThread( pool )
94 : CppUnitMini::TestThread( src )
95 , m_Alloc( src.m_Alloc )
100 return reinterpret_cast<Larson&>( m_Pool.m_Test );
103 virtual void init() { cds::threading::Manager::attachThread() ; }
104 virtual void fini() { cds::threading::Manager::detachThread() ; }
108 for ( size_t nPass = 0; nPass < s_nPassPerThread; ++nPass ) {
109 size_t nItem = m_rndGen( size_t(1), s_nBlocksPerThread ) - 1;
110 m_Alloc.deallocate( reinterpret_cast<value_type *>(m_arr[nItem]), 1 );
111 m_arr[nItem] = reinterpret_cast<char *>(m_Alloc.allocate( m_rndGen( s_nMinBlockSize, s_nMaxBlockSize ), nullptr ));
112 CPPUNIT_ASSERT( (reinterpret_cast<uintptr_t>(m_arr[nItem]) & (ALLOC::alignment - 1)) == 0 );
117 template <class ALLOC>
118 void test( size_t nThreadCount )
122 CPPUNIT_MSG( "Thread count=" << nThreadCount );
123 CPPUNIT_MSG("Initialize data..." );
125 randomGen<unsigned int> rndGen;
127 s_nPassPerThread = s_nPassCount / nThreadCount;
130 m_aThreadData = new thread_data[ nThreadCount ];
131 for ( nThread = 0; nThread < nThreadCount; ++nThread ) {
133 = m_aThreadData[nThread]
134 = new char *[ s_nBlocksPerThread ];
135 for ( size_t i = 0; i < s_nBlocksPerThread; ++i ) {
136 thData[i] = reinterpret_cast<char *>(alloc.allocate( rndGen( s_nMinBlockSize, s_nMaxBlockSize ), nullptr ));
137 CPPUNIT_ASSERT( (reinterpret_cast<uintptr_t>(thData[i]) & (ALLOC::alignment - 1)) == 0 );
140 CPPUNIT_MSG("Initializatin done" );
142 CppUnitMini::ThreadPool pool( *this );
143 pool.add( new Thread<ALLOC>( pool, alloc ), nThreadCount );
145 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it )
146 static_cast<Thread<ALLOC> *>(*it)->m_arr = m_aThreadData[nThread++];
148 cds::OS::Timer timer;
150 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
152 for ( nThread = 0; nThread < nThreadCount; ++nThread ) {
153 thread_data thData = m_aThreadData[nThread];
154 for ( size_t i = 0; i < s_nBlocksPerThread; ++i ) {
155 alloc.deallocate( reinterpret_cast<typename ALLOC::value_type *>(thData[i]), 1 );
159 delete [] m_aThreadData;
162 template <class ALLOC>
163 void test( bool bStat )
165 CPPUNIT_MSG( "Block size=" << s_nMinBlockSize << "-" << s_nMaxBlockSize
166 << ", block count per thread=" << s_nBlocksPerThread << ", pass count=" << s_nPassCount );
168 for ( size_t nThreadCount = 2; nThreadCount <= s_nMaxThreadCount; nThreadCount *= 2 ) {
169 summary_stat stBegin;
171 ALLOC::stat(stBegin);
173 test<ALLOC>( nThreadCount );
177 ALLOC::stat( stEnd );
179 std::cout << "\nStatistics:\n"
182 std::cout << "\nDelta statistics:\n"
188 void setUpParams( const CppUnitMini::TestCfg& cfg )
190 s_nPassCount = cfg.getULong( "PassCount", 100000 );
191 s_nMinBlockSize = cfg.getUInt( "MinBlockSize", 8 );
192 s_nMaxBlockSize = cfg.getUInt( "MaxBlockSize", 1024 );
193 s_nBlocksPerThread = cfg.getUInt( "BlocksPerThread", 10000 );
194 s_nMaxThreadCount = cfg.getUInt( "MaxThreadCount", 32 );
195 if ( s_nMaxThreadCount == 0 )
196 s_nMaxThreadCount = cds::OS::topology::processor_count() * 2;
197 if ( s_nMaxThreadCount < 2 )
198 s_nMaxThreadCount = 2;
199 if ( s_nPassCount < s_nBlocksPerThread )
200 s_nBlocksPerThread = s_nPassCount;
203 typedef MichaelAlignHeap_Stat<int, 64> t_MichaelAlignHeap_Stat;
204 typedef MichaelAlignHeap_NoStat<int,64> t_MichaelAlignHeap_NoStat;
205 typedef system_aligned_allocator<int, 64> t_system_aligned_allocator;
207 TEST_ALLOC_STAT( michael_heap_stat, MichaelHeap_Stat<int> )
208 TEST_ALLOC( michael_heap_nostat, MichaelHeap_NoStat<int> )
209 TEST_ALLOC( std_alloc, std_allocator<int> )
211 TEST_ALLOC_STAT( michael_alignheap_stat, t_MichaelAlignHeap_Stat )
212 TEST_ALLOC( michael_alignheap_nostat, t_MichaelAlignHeap_NoStat )
213 TEST_ALLOC( system_aligned_alloc, t_system_aligned_allocator )
215 CPPUNIT_TEST_SUITE( Larson )
216 CPPUNIT_TEST( michael_heap_stat )
217 CPPUNIT_TEST( michael_heap_nostat )
218 CPPUNIT_TEST( std_alloc )
220 CPPUNIT_TEST( system_aligned_alloc )
221 CPPUNIT_TEST( michael_alignheap_stat )
222 CPPUNIT_TEST( michael_alignheap_nostat )
224 CPPUNIT_TEST_SUITE_END();
227 } // namespace memory
228 CPPUNIT_TEST_SUITE_REGISTRATION( memory::Larson );