File: push_pop.cpp

package info (click to toggle)
libcds 2.3.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,632 kB
  • sloc: cpp: 135,002; ansic: 7,234; perl: 243; sh: 237; makefile: 6
file content (261 lines) | stat: -rw-r--r-- 8,770 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
// 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 "stack_type.h"

namespace {

    static size_t s_nPushThreadCount = 4;
    static size_t s_nPopThreadCount = 4;
    static size_t s_nStackSize = 1000000;
    static size_t s_nEliminationSize = 4;

    static atomics::atomic<size_t>  s_nWorkingProducers( 0 );

    class stack_push_pop : public cds_test::stress_fixture
    {
    protected:
        enum thread_type
        {
            producer_thread,
            consumer_thread
        };

        struct value_type {
            size_t      nNo;
            size_t      nThread;

            value_type()
                : nNo( 0 )
                , nThread( 0 )
            {}
            value_type( size_t n )
                : nNo( n )
                , nThread( 0 )
            {}
        };

        static size_t const c_nValArraySize = 1024;

        template <class Stack>
        class Producer: public cds_test::thread
        {
            typedef cds_test::thread base_class;

        public:
            Producer( cds_test::thread_pool& pool, Stack& stack, size_t push_count )
                : base_class( pool, producer_thread )
                , m_stack( stack )
                , m_nItemCount( push_count )
                , m_nPushError( 0 )
            {}

            Producer( Producer& src )
                : base_class( src )
                , m_stack( src.m_stack )
                , m_nItemCount( src.m_nItemCount )
                , m_nPushError( 0 )
            {}

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

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

                value_type v;
                v.nThread = id();
                for ( size_t i = 0; i < m_nItemCount; ++i ) {
                    v.nNo = i % c_nValArraySize;
                    if ( m_stack.push( v ))
                        ++m_arrPush[v.nNo];
                    else
                        ++m_nPushError;
                }

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

        public:
            Stack&  m_stack;
            size_t const m_nItemCount;
            size_t  m_nPushError;
            size_t  m_arrPush[c_nValArraySize];
        };

        template <class Stack>
        class Consumer : public cds_test::thread
        {
            typedef cds_test::thread base_class;

        public:
            Consumer( cds_test::thread_pool& pool, Stack& stack )
                : base_class( pool, consumer_thread )
                , m_stack( stack )
                , 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()
            {
                memset( m_arrPop, 0, sizeof( m_arrPop ));

                value_type v;
                while ( !( s_nWorkingProducers.load( atomics::memory_order_acquire ) == 0 && m_stack.empty())) {
                    if ( m_stack.pop( v )) {
                        ++m_nPopCount;
                        if ( v.nNo < sizeof( m_arrPop ) / sizeof( m_arrPop[0] ))
                            ++m_arrPop[v.nNo];
                        else
                            ++m_nDirtyPop;
                    }
                    else
                        ++m_nPopEmpty;
                }
            }

        public:
            Stack& m_stack;
            size_t m_nPopCount;
            size_t m_nPopEmpty;
            size_t m_arrPop[c_nValArraySize];
            size_t m_nDirtyPop;
        };

    protected:
        static void SetUpTestCase()
        {
            cds_test::config const& cfg = get_config("Stack_PushPop");

            s_nPushThreadCount = cfg.get_size_t( "PushThreadCount", s_nPushThreadCount );
            s_nPopThreadCount  = cfg.get_size_t( "PopThreadCount",  s_nPopThreadCount );
            s_nStackSize       = cfg.get_size_t( "StackSize",       s_nStackSize );
            s_nEliminationSize = cfg.get_size_t( "EliminationSize", s_nEliminationSize );

            if ( s_nPushThreadCount == 0 )
                s_nPushThreadCount = 1;
            if ( s_nPopThreadCount == 0 )
                s_nPopThreadCount = 1;
        }

        //static void TearDownTestCase();

        template <typename Stack>
        void test( Stack& stack )
        {
            cds_test::thread_pool& pool = get_pool();
            size_t const nPushCount = s_nStackSize / s_nPushThreadCount;

            pool.add( new Producer<Stack>( pool, stack, nPushCount ), s_nPushThreadCount );
            pool.add( new Consumer<Stack>( pool, stack ), s_nPopThreadCount );

            s_nWorkingProducers.store( s_nPushThreadCount );
            s_nStackSize = nPushCount * s_nPushThreadCount;

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

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

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

            analyze( stack );

            propout() << stack.statistics();
        }

        template <typename Stack>
        void test_elimination( Stack& stack )
        {
            test( stack );
            check_elimination_stat( stack.statistics());
        }

        void check_elimination_stat( cds::container::treiber_stack::empty_stat const& )
        {}

        void check_elimination_stat( cds::container::treiber_stack::stat<> const& s )
        {
            EXPECT_EQ( s.m_PushCount.get() + s.m_ActivePushCollision.get() + s.m_PassivePushCollision.get(), s_nStackSize );
            EXPECT_EQ( s.m_PopCount.get() + s.m_ActivePopCollision.get() + s.m_PassivePopCollision.get(), s_nStackSize );
            EXPECT_EQ( s.m_PushCount.get(), s.m_PopCount.get());
            EXPECT_EQ( s.m_ActivePopCollision.get(), s.m_PassivePushCollision.get());
            EXPECT_EQ( s.m_ActivePushCollision.get(), s.m_PassivePopCollision.get());
        }

        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 < c_nValArraySize; ++i )
                        arrVal[i] += producer.m_arrPush[i];
                }
                else {
                    ASSERT_EQ( 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 < c_nValArraySize; ++i )
                        arrVal[i] -= consumer.m_arrPop[i];
                }
            }

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

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

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

    CDSSTRESS_TreiberStack( stack_push_pop )
    CDSSTRESS_EliminationStack( stack_push_pop )
    CDSSTRESS_FCStack( stack_push_pop )
    CDSSTRESS_FCDeque( stack_push_pop )
    CDSSTRESS_StdStack( stack_push_pop )

} // namespace