Skip to content

Commit fa92777

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22736: log, sync: change lock contention from preprocessor directive to log category
7e69873 sync: remove DEBUG_LOCKCONTENTION preprocessor directives (Jon Atack) 9b08006 log, sync: improve lock contention logging and add time duration (Jon Atack) 3f4c6b8 log, timer: add timing macro in usec LOG_TIME_MICROS_WITH_CATEGORY (Jon Atack) b7a1744 log, sync: add LOCK logging category, apply it to lock contention (Jon Atack) Pull request description: To enable lock contention logging, `DEBUG_LOCKCONTENTION` has to be defined at compilation. Once built, the logging is not limited to a category and is high frequency, verbose and in all-caps. With these factors combined, it seems likely to be rarely used. This patch: - adds a `lock` logging category - adds a timing macro in microseconds, `LOG_TIME_MICROS_WITH_CATEGORY` - updates `BCLog::LogMsg()` to omit irrelevant decimals for microseconds and skip unneeded code and math - improves the lock contention logging, drops the all-caps, and displays the duration in microseconds - removes the conditional compilation directives - allows lock contentions to be logged on startup with `-debug=lock` or at run time with `bitcoin-cli logging '["lock"]'` ``` $ bitcoind -signet -debug=lock 2021-09-01T12:40:01Z LockContention: cs_vNodes, net.cpp:1920 started 2021-09-01T12:40:01Z LockContention: cs_vNodes, net.cpp:1920 completed (4μs) 2021-09-01T12:40:01Z LockContention: cs_vNodes, net.cpp:1302 started 2021-09-01T12:40:01Z LockContention: cs_vNodes, net.cpp:1302 completed (4μs) 2021-09-01T12:40:02Z LockContention: cs_vNodes, net.cpp:2242 started 2021-09-01T12:40:02Z LockContention: cs_vNodes, net.cpp:2242 completed (20μs) 2021-09-01T12:43:04Z LockContention: ::cs_main, validation.cpp:4980 started 2021-09-01T12:43:04Z LockContention: ::cs_main, validation.cpp:4980 completed (3μs) $ bitcoin-cli -signet logging "lock": true, $ bitcoin-cli -signet logging [] '["lock"]' "lock": false, $ bitcoin-cli -signet logging '["lock"]' "lock": true, ``` I've tested this with Clang 13 and GCC 10.2.1, on Debian, with and without `--enable-debug`. ACKs for top commit: hebasto: re-ACK 7e69873, added a contention duration to the log message since my [previous](bitcoin/bitcoin#22736 (review)) review. theStack: re-ACK 7e69873 🔏 ⏲️ Tree-SHA512: c4b5eb88d3a2c051acaa842b3055ce30efde1f114f61da6e55fcaa27476c1c33a60bc419f7f5ccda532e1bdbe70815222ec2b2b6d9226f29c8e94e598aacfee7
2 parents e567dd5 + 7e69873 commit fa92777

File tree

7 files changed

+22
-26
lines changed

7 files changed

+22
-26
lines changed

src/logging.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ const CLogCategoryDesc LogCategories[] =
159159
{BCLog::VALIDATION, "validation"},
160160
{BCLog::I2P, "i2p"},
161161
{BCLog::IPC, "ipc"},
162+
{BCLog::LOCK, "lock"},
162163
{BCLog::ALL, "1"},
163164
{BCLog::ALL, "all"},
164165
};

src/logging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace BCLog {
5959
VALIDATION = (1 << 21),
6060
I2P = (1 << 22),
6161
IPC = (1 << 23),
62+
LOCK = (1 << 24),
6263
ALL = ~(uint32_t)0,
6364
};
6465

src/logging/timer.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ class Timer
5858
return strprintf("%s: %s", m_prefix, msg);
5959
}
6060

61-
std::string units = "";
61+
if (std::is_same<TimeType, std::chrono::microseconds>::value) {
62+
return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
63+
}
64+
65+
std::string units;
6266
float divisor = 1;
6367

64-
if (std::is_same<TimeType, std::chrono::microseconds>::value) {
65-
units = "μs";
66-
} else if (std::is_same<TimeType, std::chrono::milliseconds>::value) {
68+
if (std::is_same<TimeType, std::chrono::milliseconds>::value) {
6769
units = "ms";
6870
divisor = 1000.;
6971
} else if (std::is_same<TimeType, std::chrono::seconds>::value) {
@@ -93,6 +95,8 @@ class Timer
9395
} // namespace BCLog
9496

9597

98+
#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category) \
99+
BCLog::Timer<std::chrono::microseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
96100
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
97101
BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
98102
#define LOG_TIME_SECONDS(end_msg) \

src/sync.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <sync.h>
1010

1111
#include <logging.h>
12+
#include <logging/timer.h>
1213
#include <tinyformat.h>
1314
#include <util/strencodings.h>
1415
#include <util/threadnames.h>
@@ -23,16 +24,10 @@
2324
#include <utility>
2425
#include <vector>
2526

26-
#ifdef DEBUG_LOCKCONTENTION
27-
#if !defined(HAVE_THREAD_LOCAL)
28-
static_assert(false, "thread_local is not supported");
29-
#endif
30-
void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
27+
void LockContention(const char* pszName, const char* pszFile, int nLine)
3128
{
32-
LogPrintf("LOCKCONTENTION: %s\n", pszName);
33-
LogPrintf("Locker: %s:%d\n", pszFile, nLine);
29+
LOG_TIME_MICROS_WITH_CATEGORY(strprintf("%s, %s:%d", pszName, pszFile, nLine), BCLog::LOCK);
3430
}
35-
#endif /* DEBUG_LOCKCONTENTION */
3631

3732
#ifdef DEBUG_LOCKORDER
3833
//

src/sync.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ using RecursiveMutex = AnnotatedMixin<std::recursive_mutex>;
126126
/** Wrapped mutex: supports waiting but not recursive locking */
127127
typedef AnnotatedMixin<std::mutex> Mutex;
128128

129-
#ifdef DEBUG_LOCKCONTENTION
130-
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
131-
#endif
129+
/** Prints a lock contention to the log */
130+
void LockContention(const char* pszName, const char* pszFile, int nLine);
132131

133132
/** Wrapper around std::unique_lock style lock for Mutex. */
134133
template <typename Mutex, typename Base = typename Mutex::UniqueLock>
@@ -138,22 +137,18 @@ class SCOPED_LOCKABLE UniqueLock : public Base
138137
void Enter(const char* pszName, const char* pszFile, int nLine)
139138
{
140139
EnterCritical(pszName, pszFile, nLine, Base::mutex());
141-
#ifdef DEBUG_LOCKCONTENTION
142-
if (!Base::try_lock()) {
143-
PrintLockContention(pszName, pszFile, nLine);
144-
#endif
145-
Base::lock();
146-
#ifdef DEBUG_LOCKCONTENTION
147-
}
148-
#endif
140+
if (Base::try_lock()) return;
141+
LockContention(pszName, pszFile, nLine); // log the contention
142+
Base::lock();
149143
}
150144

151145
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
152146
{
153147
EnterCritical(pszName, pszFile, nLine, Base::mutex(), true);
154148
Base::try_lock();
155-
if (!Base::owns_lock())
149+
if (!Base::owns_lock()) {
156150
LeaveCritical();
151+
}
157152
return Base::owns_lock();
158153
}
159154

src/test/logging_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(logging_timer)
2727
SetMockTime(1);
2828
auto micro_timer = BCLog::Timer<std::chrono::microseconds>("tests", "end_msg");
2929
SetMockTime(2);
30-
BOOST_CHECK_EQUAL(micro_timer.LogMsg("test micros"), "tests: test micros (1000000.00μs)");
30+
BOOST_CHECK_EQUAL(micro_timer.LogMsg("test micros"), "tests: test micros (1000000μs)");
3131
}
3232

3333
BOOST_AUTO_TEST_SUITE_END()

test/functional/rpc_misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def run_test(self):
5757
self.log.info("test logging rpc and help")
5858

5959
# Test logging RPC returns the expected number of logging categories.
60-
assert_equal(len(node.logging()), 24)
60+
assert_equal(len(node.logging()), 25)
6161

6262
# Test toggling a logging category on/off/on with the logging RPC.
6363
assert_equal(node.logging()['qt'], True)

0 commit comments

Comments
 (0)