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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kCommand
#include "mongo/platform/basic.h"
#include <boost/version.hpp>
#include <iostream>
#include "mongo/config.h"
#include "mongo/db/client.h"
#include "mongo/dbtests/dbtests.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/platform/bits.h"
#include "mongo/stdx/functional.h"
#include "mongo/stdx/thread.h"
#include "mongo/util/concurrency/thread_pool.h"
#include "mongo/util/concurrency/ticketholder.h"
#include "mongo/util/log.h"
#include "mongo/util/timer.h"
namespace ThreadedTests {
using std::unique_ptr;
using std::cout;
using std::endl;
using std::string;
template <int nthreads_param = 10>
class ThreadedTest {
public:
virtual void setup() {} // optional
virtual void subthread(int remaining) = 0; // each thread whatever test work you want done
virtual void validate() = 0; // after work is done
static const int nthreads = nthreads_param;
void run() {
setup();
launch_subthreads(nthreads);
validate();
}
virtual ~ThreadedTest(){}; // not necessary, but makes compilers happy
private:
void launch_subthreads(int remaining) {
if (!remaining)
return;
stdx::thread athread([=] { subthread(remaining); });
launch_subthreads(remaining - 1);
athread.join();
}
};
template <typename _AtomicUInt>
class IsAtomicWordAtomic : public ThreadedTest<> {
static const int iterations = 1000000;
typedef typename _AtomicUInt::WordType WordType;
_AtomicUInt target;
void subthread(int) {
for (int i = 0; i < iterations; i++) {
target.fetchAndAdd(WordType(1));
}
}
void validate() {
ASSERT_EQUALS(target.load(), unsigned(nthreads * iterations));
_AtomicUInt u;
ASSERT_EQUALS(0u, u.load());
ASSERT_EQUALS(0u, u.fetchAndAdd(WordType(1)));
ASSERT_EQUALS(2u, u.addAndFetch(WordType(1)));
ASSERT_EQUALS(2u, u.fetchAndSubtract(WordType(1)));
ASSERT_EQUALS(0u, u.subtractAndFetch(WordType(1)));
ASSERT_EQUALS(0u, u.load());
u.fetchAndAdd(WordType(1));
ASSERT_GREATER_THAN(u.load(), WordType(0));
u.fetchAndSubtract(WordType(1));
ASSERT_NOT_GREATER_THAN(u.load(), WordType(0));
}
};
class ThreadPoolTest {
static const unsigned iterations = 10000;
static const unsigned nThreads = 8;
AtomicWord<unsigned> counter;
void increment(unsigned n) {
for (unsigned i = 0; i < n; i++) {
counter.fetchAndAdd(1);
}
}
public:
void run() {
ThreadPool::Options options;
options.maxThreads = options.minThreads = nThreads;
ThreadPool tp(options);
tp.startup();
for (unsigned i = 0; i < iterations; i++) {
tp.schedule([=](auto status) {
ASSERT_OK(status);
increment(2);
});
}
tp.waitForIdle();
tp.shutdown();
tp.join();
ASSERT_EQUALS(counter.load(), iterations * 2);
}
};
void sleepalittle() {
Timer t;
while (1) {
stdx::this_thread::yield();
if (t.micros() > 8)
break;
}
}
int once;
/* This test is to see how long it takes to get a lock after there has been contention -- the OS
will need to reschedule us. if a spinlock, it will be fast of course, but these aren't spin
locks. Experimenting with different # of threads would be a good idea.
*/
template <class whichmutex, class scoped>
class Slack : public ThreadedTest<17> {
public:
Slack() {
k.store(0);
done.store(false);
a = b = 0;
locks = 0;
}
private:
whichmutex m;
char pad1[128];
unsigned a, b;
char pad2[128];
unsigned locks;
char pad3[128];
AtomicWord<int> k;
virtual void validate() {
if (once++ == 0) {
// <= 1.35 we use a different rwmutex impl so worth noting
cout << "Boost version : " << BOOST_VERSION << endl;
}
cout << typeid(whichmutex).name() << " Slack useful work fraction: " << ((double)a) / b
<< " locks:" << locks << endl;
}
void watch() {
while (1) {
b++;
//__sync_synchronize();
if (k.load()) {
a++;
}
sleepmillis(0);
if (done.load())
break;
}
}
AtomicWord<bool> done;
virtual void subthread(int x) {
if (x == 1) {
watch();
return;
}
Timer t;
unsigned lks = 0;
while (1) {
scoped lk(m);
k.store(1);
// not very long, we'd like to simulate about 100K locks per second
sleepalittle();
lks++;
if (done.load() || t.millis() > 1500) {
locks += lks;
k.store(0);
break;
}
k.store(0);
//__sync_synchronize();
}
done.store(true);
}
};
// Tests waiting on the TicketHolder by running many more threads than can fit into the "hotel", but
// only max _nRooms threads should ever get in at once
class TicketHolderWaits : public ThreadedTest<10> {
static const int checkIns = 1000;
static const int rooms = 3;
public:
TicketHolderWaits() : _hotel(rooms), _tickets(_hotel._nRooms) {}
private:
class Hotel {
public:
Hotel(int nRooms) : _nRooms(nRooms), _checkedIn(0), _maxRooms(0) {}
void checkIn() {
stdx::lock_guard<stdx::mutex> lk(_frontDesk);
_checkedIn++;
verify(_checkedIn <= _nRooms);
if (_checkedIn > _maxRooms)
_maxRooms = _checkedIn;
}
void checkOut() {
stdx::lock_guard<stdx::mutex> lk(_frontDesk);
_checkedIn--;
verify(_checkedIn >= 0);
}
stdx::mutex _frontDesk;
int _nRooms;
int _checkedIn;
int _maxRooms;
};
Hotel _hotel;
TicketHolder _tickets;
virtual void subthread(int x) {
string threadName = (str::stream() << "ticketHolder" << x);
Client::initThread(threadName.c_str());
for (int i = 0; i < checkIns; i++) {
_tickets.waitForTicket();
TicketHolderReleaser whenDone(&_tickets);
_hotel.checkIn();
sleepalittle();
if (i == checkIns - 1)
sleepsecs(2);
_hotel.checkOut();
if ((i % (checkIns / 10)) == 0)
mongo::unittest::log() << "checked in " << i << " times..." << endl;
}
}
virtual void validate() {
// This should always be true, assuming that it takes < 1 sec for the hardware to process a
// check-out/check-in Time for test is then ~ #threads / _nRooms * 2 seconds
verify(_hotel._maxRooms == _hotel._nRooms);
}
};
class All : public Suite {
public:
All() : Suite("threading") {}
void setupTests() {
// Slack is a test to see how long it takes for another thread to pick up
// and begin work after another relinquishes the lock. e.g. a spin lock
// would have very little slack.
add<Slack<SimpleMutex, stdx::lock_guard<SimpleMutex>>>();
add<IsAtomicWordAtomic<AtomicWord<unsigned>>>();
add<IsAtomicWordAtomic<AtomicWord<unsigned long long>>>();
add<ThreadPoolTest>();
add<TicketHolderWaits>();
}
};
SuiteInstance<All> myall;
} // namespace ThreadedTests
|