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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
/**
* Copyright (C) 2019-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/latch_analyzer.h"
#include <deque>
#include <fmt/format.h>
#include "mongo/util/hierarchical_acquisition.h"
#include "mongo/base/init.h"
#include "mongo/db/client.h"
#include "mongo/db/commands/test_commands_enabled.h"
#include "mongo/db/service_context.h"
#include "mongo/platform/mutex.h"
#include "mongo/util/fail_point.h"
#include "mongo/util/latch_analyzer.h"
#include "mongo/util/log.h"
namespace mongo {
namespace {
auto kLatchAnalysisName = "latchAnalysis"_sd;
auto kLatchViolationKey = "hierarchicalAcquisitionLevelViolations"_sd;
// LatchAnalyzer Decoration getter
const auto getLatchAnalyzer = ServiceContext::declareDecoration<LatchAnalyzer>();
/**
* LockListener sub-class to implement updating set in LatchSetState
*/
class LockListener : public Mutex::LockListener {
public:
void onContendedLock(const Identity& id) override {
if (auto client = Client::getCurrent()) {
LatchAnalyzer::get(client).onContention(id);
}
}
void onQuickLock(const Identity& id) override {
if (auto client = Client::getCurrent()) {
LatchAnalyzer::get(client).onAcquire(id);
}
}
void onSlowLock(const Identity& id) override {
if (auto client = Client::getCurrent()) {
LatchAnalyzer::get(client).onAcquire(id);
}
}
void onUnlock(const Identity& id) override {
if (auto client = Client::getCurrent()) {
LatchAnalyzer::get(client).onRelease(id);
}
}
};
// Register our LockListener with the Mutex class
MONGO_INITIALIZER_GENERAL(LatchAnalysis, (/* NO PREREQS */), ("FinalizeLockListeners"))
(InitializerContext* context) {
// Intentionally leaked, people use Latches in detached threads
static auto& listener = *new LockListener;
Mutex::addLockListener(&listener);
return Status::OK();
}
// Create a FailPoint to analyze latches more seriously for diagnostic purposes. This can be used
// with a new set of test suites to verify our lock hierarchies.
MONGO_FAIL_POINT_DEFINE(enableLatchAnalysis);
bool shouldAnalyzeLatches() {
return enableLatchAnalysis.shouldFail();
}
// Define a new serverStatus section "latchAnalysis"
class LatchAnalysisSection final : public ServerStatusSection {
public:
LatchAnalysisSection() : ServerStatusSection(kLatchAnalysisName.toString()) {}
bool includeByDefault() const override {
return false;
}
BSONObj generateSection(OperationContext* opCtx, const BSONElement&) const override {
BSONObjBuilder analysis;
LatchAnalyzer::get(opCtx->getClient()).appendToBSON(analysis);
return analysis.obj();
};
} gLatchAnalysisSection;
// Latching state object to pin onto the Client (i.e. thread)
struct LatchSetState {
using LatchIdentitySet = std::deque<const latch_detail::Identity*>;
LatchSetState() {
if (getTestCommandsEnabled()) {
identities = std::make_unique<LatchIdentitySet>();
}
}
HierarchicalAcquisitionSet levelsHeld;
// This is a set of latches by unique Identity alone. It is not and cannot be in order of
// acquisition or release. We only populate this when shouldAnalyzeLatches() is true.
stdx::unordered_set<const latch_detail::Identity*> latchesHeld;
// This is an ordered list of latch Identities. Each acquired Latch will add itself to the end
// of this list and each released Latch will remove itself from the end. This is populated when
// getTestCommandsEnabled() is true, i.e. in a testing environment.
std::unique_ptr<LatchIdentitySet> identities;
};
const auto getLatchSetState = Client::declareDecoration<LatchSetState>();
void dumpLevels(const LatchSetState& state) {
if (!state.identities) {
return;
}
log() << "Dumping Latch Identities:";
auto& identities = *state.identities;
for (auto& identity : identities) {
log() << "- " << identity->name();
}
}
} // namespace
void LatchAnalyzer::setAllowExitOnViolation(bool allowExitOnViolation) {
_allowExitOnViolation.store(allowExitOnViolation);
}
bool LatchAnalyzer::allowExitOnViolation() {
return _allowExitOnViolation.load() && (getTestCommandsEnabled());
}
LatchAnalyzer& LatchAnalyzer::get(ServiceContext* serviceContext) {
return getLatchAnalyzer(serviceContext);
}
LatchAnalyzer& LatchAnalyzer::get(Client* client) {
return get(client->getServiceContext());
}
LatchAnalyzer& LatchAnalyzer::get() {
auto serviceContext = getCurrentServiceContext();
invariant(serviceContext);
return get(serviceContext);
}
void LatchAnalyzer::onContention(const latch_detail::Identity&) {
// Nothing at the moment
}
void LatchAnalyzer::onAcquire(const latch_detail::Identity& identity) {
auto client = Client::getCurrent();
if (!client) {
return;
}
if (shouldAnalyzeLatches()) {
// If we should analyze latches, annotate the Client state
auto& latchSet = getLatchSetState(client).latchesHeld;
stdx::lock_guard lk(_mutex);
for (auto otherIdentity : latchSet) {
auto& stat = _hierarchies[identity.index()][otherIdentity->index()];
stat.identity = otherIdentity;
++stat.acquiredAfter;
}
latchSet.insert(&identity);
}
if (!identity.level()) {
// If we weren't given a HierarchicalAcquisitionLevel, don't verify hierarchies
return;
}
auto level = *identity.level();
auto& handle = getLatchSetState(client);
auto result = handle.levelsHeld.add(level);
if (result != HierarchicalAcquisitionSet::AddResult::kValidWasAbsent) {
using namespace fmt::literals;
auto errorMessage =
"Theoretical deadlock alert - {} latch acquisition at {}:{:d} on latch {}"_format(
toString(result),
identity.sourceLocation()->file_name(),
identity.sourceLocation()->line(),
identity.name());
if (allowExitOnViolation()) {
dumpLevels(handle);
fassert(31360, Status(ErrorCodes::HierarchicalAcquisitionLevelViolation, errorMessage));
} else {
warning() << errorMessage;
{
stdx::lock_guard lk(_mutex);
auto& violation = _violations[identity.index()];
++violation.onAcquire;
}
}
}
if (handle.identities) {
// Since this latch has a verified Level, we can add it to the stack of identities
handle.identities->push_back(&identity);
}
}
void LatchAnalyzer::onRelease(const latch_detail::Identity& identity) {
auto client = Client::getCurrent();
if (!client) {
return;
}
if (shouldAnalyzeLatches()) {
// If we should analyze latches, annotate the Client state
auto& latchSet = getLatchSetState(client).latchesHeld;
latchSet.erase(&identity);
stdx::lock_guard lk(_mutex);
for (auto otherIdentity : latchSet) {
auto& stat = _hierarchies[identity.index()][otherIdentity->index()];
stat.identity = otherIdentity;
++stat.releasedBefore;
}
}
if (!identity.level()) {
// If we weren't given a HierarchicalAcquisitionLevel, don't verify hierarchies
return;
}
auto level = *identity.level();
auto& handle = getLatchSetState(client);
auto result = handle.levelsHeld.remove(level);
if (result != HierarchicalAcquisitionSet::RemoveResult::kValidWasPresent) {
using namespace fmt::literals;
auto errorMessage =
"Theoretical deadlock alert - {} latch release at {}:{} on latch {}"_format(
toString(result),
identity.sourceLocation()->file_name(),
identity.sourceLocation()->line(),
identity.name());
if (allowExitOnViolation()) {
dumpLevels(handle);
fassert(31361, Status(ErrorCodes::HierarchicalAcquisitionLevelViolation, errorMessage));
} else {
warning() << errorMessage;
{
stdx::lock_guard lk(_mutex);
auto& violation = _violations[identity.index()];
++violation.onRelease;
}
}
}
if (handle.identities) {
// Since this latch has a verified Level, we can remove it from the stack of identities
handle.identities->pop_back();
}
}
void LatchAnalyzer::appendToBSON(mongo::BSONObjBuilder& result) const {
for (auto iter = latch_detail::Catalog::get().iter(); iter.more();) {
auto data = iter.next();
if (!data) {
continue;
}
auto& identity = data->identity();
BSONObjBuilder latchObj = result.subobjStart(identity.name());
latchObj.append("created", data->counts().created.loadRelaxed());
latchObj.append("destroyed", data->counts().destroyed.loadRelaxed());
latchObj.append("acquired", data->counts().acquired.loadRelaxed());
latchObj.append("released", data->counts().released.loadRelaxed());
latchObj.append("contended", data->counts().contended.loadRelaxed());
auto appendViolations = [&] {
stdx::lock_guard lk(_mutex);
auto it = _violations.find(identity.index());
if (it == _violations.end()) {
return;
}
auto& violation = it->second;
BSONObjBuilder violationObj = latchObj.subobjStart(kLatchViolationKey);
violationObj.append("onAcquire", violation.onAcquire);
violationObj.append("onRelease", violation.onRelease);
};
appendViolations();
if (!shouldAnalyzeLatches()) {
// Only append hierarchical information if we should analyze latches
continue;
}
stdx::lock_guard lk(_mutex);
auto it = _hierarchies.find(identity.index());
if (it == _hierarchies.end()) {
continue;
}
auto& latchHierarchy = it->second;
if (latchHierarchy.empty()) {
continue;
}
{
BSONObjBuilder acquiredAfterObj = latchObj.subobjStart("acquiredAfter");
for (auto& [_, stat] : latchHierarchy) {
auto count = stat.acquiredAfter;
if (count == 0) {
continue;
}
acquiredAfterObj.append(stat.identity->name(), count);
}
}
{
BSONObjBuilder releasedBeforeObj = latchObj.subobjStart("releasedBefore");
for (auto& [_, stat] : latchHierarchy) {
auto count = stat.releasedBefore;
if (count == 0) {
continue;
}
releasedBeforeObj.append(stat.identity->name(), count);
}
}
}
}
void LatchAnalyzer::dump() {
if (!shouldAnalyzeLatches()) {
return;
}
BSONObjBuilder bob(1024 * 1024);
{
BSONObjBuilder analysis = bob.subobjStart("latchAnalysis");
appendToBSON(analysis);
}
auto obj = bob.done();
log().setIsTruncatable(false) << "=====LATCHES=====\n"
<< obj.jsonString(JsonStringFormat::LegacyStrict)
<< "\n===END LATCHES===";
}
LatchAnalyzerDisabledBlock::LatchAnalyzerDisabledBlock() {
LatchAnalyzer::get().setAllowExitOnViolation(false);
}
LatchAnalyzerDisabledBlock::~LatchAnalyzerDisabledBlock() {
LatchAnalyzer::get().setAllowExitOnViolation(true);
}
} // namespace mongo
|