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
312
313
|
/**
* 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::kDefault
#include "mongo/platform/basic.h"
#include "mongo/util/diagnostic_info.h"
#include <forward_list>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include "mongo/base/init.h"
#include "mongo/db/client.h"
#include "mongo/platform/mutex.h"
#include "mongo/util/clock_source.h"
#include "mongo/util/interruptible.h"
#include "mongo/util/log.h"
using namespace fmt::literals;
namespace mongo {
namespace {
MONGO_FAIL_POINT_DEFINE(currentOpSpawnsThreadWaitingForLatch);
constexpr auto kBlockedOpMutexName = "BlockedOpForTestLatch"_sd;
constexpr auto kBlockedOpInterruptibleName = "BlockedOpForTestInterruptible"_sd;
class BlockedOp {
public:
void start(ServiceContext* serviceContext);
void join();
void setIsContended(bool value);
void setIsWaiting(bool value);
private:
stdx::condition_variable _cv;
stdx::mutex _m; // NOLINT
struct LatchState {
bool isContended = false;
boost::optional<stdx::thread> thread{boost::none};
Mutex mutex = MONGO_MAKE_LATCH(kBlockedOpMutexName);
};
LatchState _latchState;
struct InterruptibleState {
bool isWaiting = false;
boost::optional<stdx::thread> thread{boost::none};
stdx::condition_variable cv;
Mutex mutex = MONGO_MAKE_LATCH(kBlockedOpInterruptibleName);
bool isDone = false;
};
InterruptibleState _interruptibleState;
} gBlockedOp;
// This function causes us to make an additional thread with a self-contended lock so that
// $currentOp can observe its DiagnosticInfo. Note that we track each thread that called us so that
// we can join the thread when they are gone.
void BlockedOp::start(ServiceContext* serviceContext) {
stdx::unique_lock<stdx::mutex> lk(_m);
invariant(!_latchState.thread);
invariant(!_interruptibleState.thread);
_latchState.mutex.lock();
_latchState.thread = stdx::thread([this, serviceContext]() mutable {
ThreadClient tc("DiagnosticCaptureTestLatch", serviceContext);
log() << "Entered currentOpSpawnsThreadWaitingForLatch thread";
stdx::lock_guard testLock(_latchState.mutex);
log() << "Joining currentOpSpawnsThreadWaitingForLatch thread";
});
_interruptibleState.thread = stdx::thread([this, serviceContext]() mutable {
ThreadClient tc("DiagnosticCaptureTestInterruptible", serviceContext);
auto opCtx = tc->makeOperationContext();
log() << "Entered currentOpSpawnsThreadWaitingForLatch thread for interruptibles";
stdx::unique_lock lk(_interruptibleState.mutex);
opCtx->waitForConditionOrInterrupt(
_interruptibleState.cv, lk, [&] { return _interruptibleState.isDone; });
_interruptibleState.isDone = false;
log() << "Joining currentOpSpawnsThreadWaitingForLatch thread for interruptibles";
});
_cv.wait(lk, [this] { return _latchState.isContended && _interruptibleState.isWaiting; });
log() << "Started threads for currentOpSpawnsThreadWaitingForLatch";
}
// This function unlocks testMutex and joins if there are no more callers of BlockedOp::start()
// remaining
void BlockedOp::join() {
decltype(_latchState.thread) latchThread;
decltype(_interruptibleState.thread) interruptibleThread;
{
stdx::lock_guard<stdx::mutex> lk(_m);
invariant(_latchState.thread);
invariant(_interruptibleState.thread);
_latchState.mutex.unlock();
_latchState.isContended = false;
{
stdx::lock_guard lk(_interruptibleState.mutex);
_interruptibleState.isDone = true;
_interruptibleState.cv.notify_one();
}
_interruptibleState.isWaiting = false;
std::swap(_latchState.thread, latchThread);
std::swap(_interruptibleState.thread, interruptibleThread);
}
latchThread->join();
interruptibleThread->join();
}
void BlockedOp::setIsContended(bool value) {
log() << "Setting isContended to " << (value ? "true" : "false");
stdx::lock_guard lk(_m);
_latchState.isContended = value;
_cv.notify_one();
}
void BlockedOp::setIsWaiting(bool value) {
log() << "Setting isWaiting to " << (value ? "true" : "false");
stdx::lock_guard lk(_m);
_interruptibleState.isWaiting = value;
_cv.notify_one();
}
struct DiagnosticInfoHandle {
stdx::mutex mutex; // NOLINT
std::forward_list<DiagnosticInfo> list;
};
const auto getDiagnosticInfoHandle = Client::declareDecoration<DiagnosticInfoHandle>();
MONGO_INITIALIZER(LockListener)(InitializerContext* context) {
class LockListener : public Mutex::LockListener {
void onContendedLock(const Mutex::Identity& id) override {
if (auto client = Client::getCurrent()) {
auto& handle = getDiagnosticInfoHandle(client);
stdx::lock_guard<stdx::mutex> lk(handle.mutex);
handle.list.emplace_front(DiagnosticInfo::capture(id.name));
if (currentOpSpawnsThreadWaitingForLatch.shouldFail() &&
(id.name == kBlockedOpMutexName)) {
gBlockedOp.setIsContended(true);
}
}
}
void onQuickLock(const Mutex::Identity&) override {
// Do nothing
}
void onSlowLock(const Mutex::Identity& id) override {
if (auto client = Client::getCurrent()) {
auto& handle = getDiagnosticInfoHandle(client);
stdx::lock_guard<stdx::mutex> lk(handle.mutex);
invariant(!handle.list.empty());
handle.list.pop_front();
}
}
void onUnlock(const Mutex::Identity&) override {
// Do nothing
}
};
// Intentionally leaked, people use Latches in detached threads
static auto& listener = *new LockListener;
Mutex::addLockListener(&listener);
return Status::OK();
}
MONGO_INITIALIZER(InterruptibleWaitListener)(InitializerContext* context) {
class WaitListener : public Interruptible::WaitListener {
using WakeReason = Interruptible::WakeReason;
using WakeSpeed = Interruptible::WakeSpeed;
void addInfo(const StringData& name) {
if (auto client = Client::getCurrent()) {
auto& handle = getDiagnosticInfoHandle(client);
stdx::lock_guard<stdx::mutex> lk(handle.mutex);
handle.list.emplace_front(DiagnosticInfo::capture(name));
if (currentOpSpawnsThreadWaitingForLatch.shouldFail() &&
(name == kBlockedOpInterruptibleName)) {
gBlockedOp.setIsWaiting(true);
}
}
}
void removeInfo(const StringData& name) {
if (auto client = Client::getCurrent()) {
auto& handle = getDiagnosticInfoHandle(client);
stdx::lock_guard<stdx::mutex> lk(handle.mutex);
invariant(!handle.list.empty());
handle.list.pop_front();
}
}
void onLongSleep(const StringData& name) override {
addInfo(name);
}
void onWake(const StringData& name, WakeReason, WakeSpeed speed) override {
if (speed == WakeSpeed::kSlow) {
removeInfo(name);
}
}
};
// Intentionally leaked, people can use in detached threads
static auto& listener = *new WaitListener();
Interruptible::addWaitListener(&listener);
return Status::OK();
}
} // namespace
bool operator==(const DiagnosticInfo& info1, const DiagnosticInfo& info2) {
return info1._captureName == info2._captureName && info1._timestamp == info2._timestamp &&
info1._backtrace.data == info2._backtrace.data;
}
std::string DiagnosticInfo::toString() const {
return "{{ \"name\": \"{}\", \"time\": \"{}\", \"backtraceSize\": {} }}"_format(
_captureName.toString(), _timestamp.toString(), _backtrace.data.size());
}
DiagnosticInfo DiagnosticInfo::capture(const StringData& captureName, Options options) {
// Since we don't have a fast enough backtrace implementation at the moment, the Backtrace is
// always empty. If SERVER-44091 happens, this should branch on options.shouldTakeBacktrace
auto backtrace = Backtrace{};
auto currentTime = getGlobalServiceContext()->getFastClockSource()->now();
return DiagnosticInfo(currentTime, captureName, std::move(backtrace));
}
DiagnosticInfo::BlockedOpGuard::~BlockedOpGuard() {
gBlockedOp.join();
}
auto DiagnosticInfo::maybeMakeBlockedOpForTest(Client* client) -> std::unique_ptr<BlockedOpGuard> {
std::unique_ptr<BlockedOpGuard> guard;
currentOpSpawnsThreadWaitingForLatch.executeIf(
[&](const BSONObj&) {
gBlockedOp.start(client->getServiceContext());
guard = std::make_unique<BlockedOpGuard>();
},
[&](const BSONObj& data) {
return data.hasField("clientName") &&
(data.getStringField("clientName") == client->desc());
});
return guard;
}
boost::optional<DiagnosticInfo> DiagnosticInfo::get(Client& client) {
auto& handle = getDiagnosticInfoHandle(client);
stdx::lock_guard<stdx::mutex> lk(handle.mutex);
if (handle.list.empty()) {
return boost::none;
}
return handle.list.front();
}
} // namespace mongo
|