File: intrusive_stack_push_pop.h

package info (click to toggle)
libcds 2.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 15,564 kB
  • sloc: cpp: 135,002; ansic: 7,218; perl: 243; sh: 237; makefile: 6
file content (272 lines) | stat: -rw-r--r-- 9,502 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "intrusive_stack_type.h"

namespace cds_test {

    class intrusive_stack_push_pop : public cds_test::stress_fixture
    {
    protected:
        static size_t s_nPushThreadCount;
        static size_t s_nPopThreadCount;
        static size_t s_nStackSize;
        static size_t s_nEliminationSize;
        static bool s_bFCIterative;
        static unsigned int s_nFCCombinePassCount;
        static unsigned int s_nFCCompactFactor;

        static atomics::atomic<size_t>  s_nWorkingProducers;

        static constexpr const size_t c_nValArraySize = 1024;
        static constexpr const size_t c_nBadConsumer = 0xbadc0ffe;

        enum thread_type
        {
            producer_thread,
            consumer_thread
        };

        struct empty
        {};

        template <typename Base = empty >
        struct value_type : public Base
        {
            atomics::atomic<size_t> nNo;
            size_t      nProducer;
            size_t      nConsumer;

            value_type() {}
            value_type( size_t n ) : nNo( n ) {}
        };


        template <class Stack>
        class Producer : public cds_test::thread
        {
            typedef cds_test::thread base_class;
        public:
            Stack&              m_Stack;
            size_t              m_nPushError;
            size_t              m_arrPush[c_nValArraySize];

            // Interval in m_arrValue
            typename Stack::value_type *       m_pStart;
            typename Stack::value_type *       m_pEnd;

        public:
            Producer( cds_test::thread_pool& pool, Stack& s )
                : base_class( pool, producer_thread )
                , m_Stack( s )
                , m_nPushError( 0 )
                , m_pStart( nullptr )
                , m_pEnd( nullptr )
            {}

            Producer( Producer& src )
                : base_class( src )
                , m_Stack( src.m_Stack )
                , m_nPushError( 0 )
                , m_pStart( nullptr )
                , m_pEnd( nullptr )
            {}

            virtual thread * clone()
            {
                return new Producer( *this );
            }

            virtual void test()
            {
                m_nPushError = 0;
                memset( m_arrPush, 0, sizeof( m_arrPush ));

                size_t i = 0;
                for ( typename Stack::value_type * p = m_pStart; p < m_pEnd; ++p, ++i ) {
                    p->nProducer = id();
                    size_t no;
                    p->nNo.store( no = i % c_nValArraySize, atomics::memory_order_release );
                    if ( m_Stack.push( *p ))
                        ++m_arrPush[no];
                    else
                        ++m_nPushError;
                }

                s_nWorkingProducers.fetch_sub( 1, atomics::memory_order_release );
            }
        };

        template <class Stack>
        class Consumer : public cds_test::thread
        {
            typedef cds_test::thread base_class;
        public:
            Stack&              m_Stack;
            size_t              m_nPopCount;
            size_t              m_nPopEmpty;
            size_t              m_arrPop[c_nValArraySize];
            size_t              m_nDirtyPop;
        public:
            Consumer( cds_test::thread_pool& pool, Stack& s )
                : base_class( pool, consumer_thread )
                , m_Stack( s )
                , m_nPopCount( 0 )
                , m_nPopEmpty( 0 )
                , m_nDirtyPop( 0 )
            {}

            Consumer( Consumer& src )
                : base_class( src )
                , m_Stack( src.m_Stack )
                , m_nPopCount( 0 )
                , m_nPopEmpty( 0 )
                , m_nDirtyPop( 0 )
            {}

            virtual thread * clone()
            {
                return new Consumer( *this );
            }

            virtual void test()
            {
                m_nPopEmpty = 0;
                m_nPopCount = 0;
                m_nDirtyPop = 0;
                memset( m_arrPop, 0, sizeof( m_arrPop ));

                while ( !(s_nWorkingProducers.load( atomics::memory_order_acquire ) == 0 && m_Stack.empty())) {
                    typename Stack::value_type * p = m_Stack.pop();
                    if ( p ) {
                        p->nConsumer = id();
                        ++m_nPopCount;
                        size_t no = p->nNo.load( atomics::memory_order_acquire );
                        if ( no < sizeof( m_arrPop ) / sizeof( m_arrPop[0] ))
                            ++m_arrPop[no];
                        else
                            ++m_nDirtyPop;
                    }
                    else
                        ++m_nPopEmpty;
                }
            }
        };

        template <typename T>
        class value_array
        {
            std::unique_ptr< T[] > m_pArr;

        public:
            value_array( size_t nSize )
                : m_pArr( new T[nSize] )
            {}

            T * get() const { return m_pArr.get(); }
        };

    public:
        static void SetUpTestCase();
        //static void TearDownTestCase();

    protected:
        template <class Stack>
        void analyze( Stack& /*stack*/ )
        {
            cds_test::thread_pool& pool = get_pool();

            size_t nPushError = 0;
            size_t nPopEmpty = 0;
            size_t nPopCount = 0;
            size_t arrVal[c_nValArraySize];
            memset( arrVal, 0, sizeof( arrVal ));
            size_t nDirtyPop = 0;

            for ( size_t threadNo = 0; threadNo < pool.size(); ++threadNo ) {
                cds_test::thread& thread = pool.get( threadNo );
                if ( thread.type() == producer_thread ) {
                    Producer<Stack>& producer = static_cast<Producer<Stack>&>(thread);
                    nPushError += producer.m_nPushError;
                    for ( size_t i = 0; i < sizeof( arrVal ) / sizeof( arrVal[0] ); ++i )
                        arrVal[i] += producer.m_arrPush[i];
                }
                else {
                    ASSERT_TRUE( thread.type() == consumer_thread );
                    Consumer<Stack>& consumer = static_cast<Consumer<Stack>&>(thread);
                    nPopEmpty += consumer.m_nPopEmpty;
                    nPopCount += consumer.m_nPopCount;
                    nDirtyPop += consumer.m_nDirtyPop;
                    for ( size_t i = 0; i < sizeof( arrVal ) / sizeof( arrVal[0] ); ++i )
                        arrVal[i] -= consumer.m_arrPop[i];
                }
            }

            EXPECT_EQ( nPopCount, s_nStackSize );
            EXPECT_EQ( nDirtyPop, 0u );
            EXPECT_EQ( nPushError, 0u );

            for ( size_t i = 0; i < sizeof( arrVal ) / sizeof( arrVal[0] ); ++i ) {
                EXPECT_EQ( arrVal[i], 0u ) << "i=" << i;
            }

            propout() << std::make_pair( "push_count", s_nStackSize )
                << std::make_pair( "push_error", nPushError )
                << std::make_pair( "pop_count", nPopCount )
                << std::make_pair( "pop_empty", nPopEmpty )
                << std::make_pair( "dirty_pop", nDirtyPop )
;

        }

        template <typename Stack>
        void do_test( Stack& stack, value_array<typename Stack::value_type>& arrValue )
        {
            cds_test::thread_pool& pool = get_pool();

            s_nWorkingProducers.store( s_nPushThreadCount, atomics::memory_order_release );
            size_t const nPushCount = s_nStackSize / s_nPushThreadCount;

            typename Stack::value_type * pValStart = arrValue.get();
            typename Stack::value_type * pValEnd = pValStart + s_nStackSize;

            pool.add( new Producer<Stack>( pool, stack ), s_nPushThreadCount );
            {
                for ( typename Stack::value_type * it = pValStart; it != pValEnd; ++it )
                    it->nConsumer = c_nBadConsumer;

                typename Stack::value_type * pStart = pValStart;
                for ( size_t thread_no = 0; thread_no < pool.size(); ++thread_no ) {
                    static_cast<Producer<Stack>&>(pool.get( thread_no )).m_pStart = pStart;
                    pStart += nPushCount;
                    static_cast<Producer<Stack>&>(pool.get( thread_no )).m_pEnd = pStart;
                }
            }
            pool.add( new Consumer<Stack>( pool, stack ), s_nPopThreadCount );

            propout() << std::make_pair( "producer_thread_count", s_nPushThreadCount )
                << std::make_pair( "consumer_thread_count", s_nPopThreadCount )
                << std::make_pair( "push_count", nPushCount * s_nPushThreadCount )
;

            std::chrono::milliseconds duration = pool.run();

            propout() << std::make_pair( "duration", duration );

            s_nStackSize = nPushCount * s_nPushThreadCount;

            {
                typename Stack::value_type * pEnd = pValStart + s_nStackSize;
                size_t const nBadConsumer = c_nBadConsumer;
                for ( typename Stack::value_type * it = pValStart; it != pEnd; ++it )
                    EXPECT_NE( it->nConsumer, nBadConsumer );
            }

            analyze( stack );

            propout() << stack.statistics();
        }
    };
} // namespace cds_test