Skip to content

Commit f331cbe

Browse files
committed
merge bitcoin#24770: Put lock logging behind DEBUG_LOCKCONTENTION preprocessor directive
1 parent d9cc2ea commit f331cbe

File tree

10 files changed

+35
-6
lines changed

10 files changed

+35
-6
lines changed

doc/developer-notes.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Developer Notes
1717
- [`debug.log`](#debuglog)
1818
- [Testnet and Regtest modes](#testnet-and-regtest-modes)
1919
- [DEBUG_LOCKORDER](#debug_lockorder)
20+
- [DEBUG_LOCKCONTENTION](#debug_lockcontention)
2021
- [Valgrind suppressions file](#valgrind-suppressions-file)
2122
- [Compiling for test coverage](#compiling-for-test-coverage)
2223
- [Performance profiling with perf](#performance-profiling-with-perf)
@@ -412,6 +413,19 @@ configure option adds `-DDEBUG_LOCKORDER` to the compiler flags. This inserts
412413
run-time checks to keep track of which locks are held and adds warnings to the
413414
`debug.log` file if inconsistencies are detected.
414415

416+
### DEBUG_LOCKCONTENTION
417+
418+
Defining `DEBUG_LOCKCONTENTION` adds a "lock" logging category to the logging
419+
RPC that, when enabled, logs the location and duration of each lock contention
420+
to the `debug.log` file.
421+
422+
To enable it, run configure with `-DDEBUG_LOCKCONTENTION` added to your
423+
CPPFLAGS, e.g. `CPPFLAGS="-DDEBUG_LOCKCONTENTION"`, then build and run dashd.
424+
425+
You can then use the `-debug=lock` configuration option at dashd startup or
426+
`dash-cli logging '["lock"]'` at runtime to turn on lock contention logging.
427+
It can be toggled off again with `dash-cli logging [] '["lock"]'`.
428+
415429
### Assertions and Checks
416430

417431
The util file `src/util/check.h` offers helpers to protect against coding and

src/evo/creditpool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <serialize.h>
1111
#include <sync.h>
1212
#include <threadsafety.h>
13+
#include <tinyformat.h>
1314
#include <unordered_lru_cache.h>
1415
#include <util/ranges_set.h>
1516

src/llmq/quorums.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <saltedhasher.h>
1616
#include <threadinterrupt.h>
1717
#include <unordered_lru_cache.h>
18+
#include <util/time.h>
1819

1920
#include <atomic>
2021
#include <map>

src/logging.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ const CLogCategoryDesc LogCategories[] =
160160
{BCLog::VALIDATION, "validation"},
161161
{BCLog::I2P, "i2p"},
162162
{BCLog::IPC, "ipc"},
163+
#ifdef DEBUG_LOCKCONTENTION
163164
{BCLog::LOCK, "lock"},
165+
#endif
164166
{BCLog::BLOCKSTORE, "blockstorage"},
165167
{BCLog::TXRECONCILIATION, "txreconciliation"},
166168
{BCLog::ALL, "1"},

src/logging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ namespace BCLog {
6161
VALIDATION = (1 << 21),
6262
I2P = (1 << 22),
6363
IPC = (1 << 23),
64+
#ifdef DEBUG_LOCKCONTENTION
6465
LOCK = (1 << 24),
66+
#endif
6567
BLOCKSTORE = (1 << 26),
6668
TXRECONCILIATION = (1 << 27),
6769

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <hash.h>
1717
#include <i2p.h>
1818
#include <limitedmap.h>
19+
#include <logging.h>
1920
#include <net_permissions.h>
2021
#include <netaddress.h>
2122
#include <netbase.h>
@@ -42,6 +43,7 @@
4243
#include <cstdint>
4344
#include <deque>
4445
#include <functional>
46+
#include <list>
4547
#include <map>
4648
#include <memory>
4749
#include <optional>

src/stats/rawsender.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <stats/rawsender.h>
77

8+
#include <logging.h>
89
#include <netaddress.h>
910
#include <netbase.h>
1011
#include <util/sock.h>

src/sync.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
#ifndef BITCOIN_SYNC_H
77
#define BITCOIN_SYNC_H
88

9+
#ifdef DEBUG_LOCKCONTENTION
910
#include <logging.h>
1011
#include <logging/timer.h>
12+
#endif
13+
1114
#include <threadsafety.h>
1215
#include <util/macros.h>
1316

@@ -161,8 +164,10 @@ class SCOPED_LOCKABLE UniqueLock : public Base
161164
void Enter(const char* pszName, const char* pszFile, int nLine)
162165
{
163166
EnterCritical(pszName, pszFile, nLine, Base::mutex());
167+
#ifdef DEBUG_LOCKCONTENTION
164168
if (Base::try_lock()) return;
165169
LOG_TIME_MICROS_WITH_CATEGORY(strprintf("lock contention %s, %s:%d", pszName, pszFile, nLine), BCLog::LOCK);
170+
#endif
166171
Base::lock();
167172
}
168173

src/test/checkqueue_tests.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
#include <vector>
1919

2020
/**
21-
* Identical to TestingSetup but excludes lock contention logging, as some of
22-
* these tests are designed to be heavily contested to trigger race conditions
23-
* or other issues.
21+
* Identical to TestingSetup but excludes lock contention logging if
22+
* `DEBUG_LOCKCONTENTION` is defined, as some of these tests are designed to be
23+
* heavily contested to trigger race conditions or other issues.
2424
*/
2525
struct NoLockLoggingTestingSetup : public TestingSetup {
2626
NoLockLoggingTestingSetup()
27+
#ifdef DEBUG_LOCKCONTENTION
2728
: TestingSetup{CBaseChainParams::MAIN, /*extra_args=*/{"-debugexclude=lock"}} {}
29+
#else
30+
: TestingSetup{CBaseChainParams::MAIN} {}
31+
#endif
2832
};
2933

3034
BOOST_FIXTURE_TEST_SUITE(checkqueue_tests, NoLockLoggingTestingSetup)

test/functional/rpc_misc.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ def run_test(self):
5656

5757
self.log.info("test logging rpc and help")
5858

59-
# Test logging RPC returns the expected number of logging categories.
60-
assert_equal(len(node.logging()), 40)
61-
6259
# Test toggling a logging category on/off/on with the logging RPC.
6360
assert_equal(node.logging()['qt'], True)
6461
node.logging(exclude=['qt'])

0 commit comments

Comments
 (0)