cbd142b1efc8d828df0f513ea2012b5d67240c57
[libcds.git] / tests / unit / alloc / larson.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.     
29 */
30
31 // Larson allocator test
32
33 #include "alloc/michael_allocator.h"
34 #include "alloc/random_gen.h"
35
36 #include <cds/os/timer.h>
37 #include <cds/os/topology.h>
38
39 #include "cppunit/thread.h"
40
41 namespace memory {
42
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;
48
49     static size_t s_nPassPerThread;
50
51
52 #    define TEST_ALLOC(X, CLASS)    void X() { test< CLASS >(false)    ; }
53 #    define TEST_ALLOC_STAT(X, CLASS)    void X() { test< CLASS >(true)    ; }
54
55     /*
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.
65     */
66     class Larson: public CppUnitMini::TestCase
67     {
68         typedef char ** thread_data;
69
70         thread_data *   m_aThreadData;
71
72
73         template <class ALLOC>
74         class Thread: public CppUnitMini::TestThread
75         {
76             ALLOC&      m_Alloc;
77             typedef typename ALLOC::value_type value_type;
78
79             virtual Thread *    clone()
80             {
81                 return new Thread( *this );
82             }
83
84             randomGen<size_t>   m_rndGen;
85         public:
86             thread_data     m_arr;
87
88         public:
89             Thread( CppUnitMini::ThreadPool& pool, ALLOC& a )
90                 : CppUnitMini::TestThread( pool )
91                 , m_Alloc( a )
92             {}
93             Thread( Thread& src )
94                 : CppUnitMini::TestThread( src )
95                 , m_Alloc( src.m_Alloc )
96             {}
97
98             Larson&  getTest()
99             {
100                 return reinterpret_cast<Larson&>( m_Pool.m_Test );
101             }
102
103             virtual void init() { cds::threading::Manager::attachThread()   ; }
104             virtual void fini() { cds::threading::Manager::detachThread()   ; }
105
106             virtual void test()
107             {
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 );
113                 }
114             }
115         };
116
117         template <class ALLOC>
118         void test( size_t nThreadCount )
119         {
120             ALLOC alloc;
121
122             CPPUNIT_MSG( "Thread count=" << nThreadCount );
123             CPPUNIT_MSG("Initialize data..." );
124
125             randomGen<unsigned int> rndGen;
126
127             s_nPassPerThread = s_nPassCount / nThreadCount;
128
129             size_t nThread;
130             m_aThreadData = new thread_data[ nThreadCount ];
131             for ( nThread = 0; nThread < nThreadCount; ++nThread ) {
132                 thread_data thData
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 );
138                     }
139             }
140             CPPUNIT_MSG("Initializatin done" );
141
142             CppUnitMini::ThreadPool pool( *this );
143             pool.add( new Thread<ALLOC>( pool, alloc ), nThreadCount );
144             nThread = 0;
145             for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it )
146                 static_cast<Thread<ALLOC> *>(*it)->m_arr = m_aThreadData[nThread++];
147
148             cds::OS::Timer    timer;
149             pool.run();
150             CPPUNIT_MSG( "  Duration=" << pool.avgDuration() );
151
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 );
156                 }
157                 delete [] thData;
158             }
159             delete [] m_aThreadData;
160         }
161
162         template <class ALLOC>
163         void test( bool bStat )
164         {
165             CPPUNIT_MSG( "Block size=" << s_nMinBlockSize << "-" << s_nMaxBlockSize
166                 << ", block count per thread=" << s_nBlocksPerThread << ", pass count=" << s_nPassCount );
167
168             for ( size_t nThreadCount = 2; nThreadCount <= s_nMaxThreadCount; nThreadCount *= 2 ) {
169                 summary_stat stBegin;
170                 if ( bStat )
171                     ALLOC::stat(stBegin);
172
173                 test<ALLOC>( nThreadCount );
174
175                 summary_stat    stEnd;
176                 if ( bStat ) {
177                     ALLOC::stat( stEnd );
178
179                     std::cout << "\nStatistics:\n"
180                         << stEnd;
181                     stEnd -= stBegin;
182                     std::cout << "\nDelta statistics:\n"
183                         << stEnd;
184                 }
185             }
186         }
187
188         void setUpParams( const CppUnitMini::TestCfg& cfg )
189         {
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;
201         }
202
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;
206
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> )
210
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 )
214
215         CPPUNIT_TEST_SUITE( Larson )
216             CPPUNIT_TEST( michael_heap_stat )
217             CPPUNIT_TEST( michael_heap_nostat )
218             CPPUNIT_TEST( std_alloc )
219
220             CPPUNIT_TEST( system_aligned_alloc )
221             CPPUNIT_TEST( michael_alignheap_stat )
222             CPPUNIT_TEST( michael_alignheap_nostat )
223
224         CPPUNIT_TEST_SUITE_END();
225     };
226
227 }   // namespace memory
228 CPPUNIT_TEST_SUITE_REGISTRATION( memory::Larson );