Skip to content

Commit 4a87077

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22872: log: improve checkaddrman logging with duration in milliseconds
22b44fc p2p: improve checkaddrman logging with duration in milliseconds (Jon Atack) ec65bed log, timer: add LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE macro (Jon Atack) 325da75 log, timer: allow not repeating log message on completion (Jon Atack) Pull request description: This patch: - updates the `logging/timer.h::Timer` class to allow not repeating the log message on completion - adds a `LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE` macro that prints the descriptive message when logging the start but not when logging the completion - updates the checkaddrman logging to log the duration, and renames the function like the `-checkaddrman` configuration option in order to prefix every log message with `CheckAddrman` instead of the longer, less pleasant, and different-from-checkaddrman `ForceCheckAddrman` (the Doxygen documentation on the function already makes clear that it is unaffected by `m_consistency_check_ratio`). before ``` 2021-09-21T18:42:50Z [opencon] Addrman checks started: new 64864, tried 1690, total 66554 2021-09-21T18:42:50Z [opencon] Addrman checks completed successfully ``` after ``` 2021-09-21T18:42:50Z [opencon] CheckAddrman: new 64864, tried 1690, total 66554 started 2021-09-21T18:42:50Z [opencon] CheckAddrman: completed (76.21ms) ``` To test, build and run bitcoind with `-debug=addrman -checkaddrman=<n>` for a value of `n` in the range of, say, 10 to 40. ACKs for top commit: laanwj: Code review ACK 22b44fc Tree-SHA512: 658c0dfaaa9d07092e2418f2d05007c58cc35be6593f22b3c592ce793334a885dd92dacc46bdeddc9d37939cf11174660a094c07c0fa117fbb282953aa45a94d
2 parents 38b2a0a + 22b44fc commit 4a87077

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

src/addrman.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <addrman_impl.h>
88

99
#include <hash.h>
10+
#include <logging.h>
11+
#include <logging/timer.h>
1012
#include <netaddress.h>
1113
#include <protocol.h>
1214
#include <random.h>
@@ -387,7 +389,7 @@ void AddrManImpl::Unserialize(Stream& s_)
387389
LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions or invalid addresses\n", nLostUnk, nLost);
388390
}
389391

390-
const int check_code{ForceCheckAddrman()};
392+
const int check_code{CheckAddrman()};
391393
if (check_code != 0) {
392394
throw std::ios_base::failure(strprintf(
393395
"Corrupt data. Consistency check failed with code %s",
@@ -937,18 +939,19 @@ void AddrManImpl::Check() const
937939
if (m_consistency_check_ratio == 0) return;
938940
if (insecure_rand.randrange(m_consistency_check_ratio) >= 1) return;
939941

940-
const int err{ForceCheckAddrman()};
942+
const int err{CheckAddrman()};
941943
if (err) {
942944
LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
943945
assert(false);
944946
}
945947
}
946948

947-
int AddrManImpl::ForceCheckAddrman() const
949+
int AddrManImpl::CheckAddrman() const
948950
{
949951
AssertLockHeld(cs);
950952

951-
LogPrint(BCLog::ADDRMAN, "Addrman checks started: new %i, tried %i, total %u\n", nNew, nTried, vRandom.size());
953+
LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
954+
strprintf("new %i, tried %i, total %u", nNew, nTried, vRandom.size()), BCLog::ADDRMAN);
952955

953956
std::unordered_set<int> setTried;
954957
std::unordered_map<int, int> mapNew;
@@ -1028,7 +1031,6 @@ int AddrManImpl::ForceCheckAddrman() const
10281031
if (nKey.IsNull())
10291032
return -16;
10301033

1031-
LogPrint(BCLog::ADDRMAN, "Addrman checks completed successfully\n");
10321034
return 0;
10331035
}
10341036

src/addrman_impl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_ADDRMAN_IMPL_H
77

88
#include <logging.h>
9+
#include <logging/timer.h>
910
#include <netaddress.h>
1011
#include <protocol.h>
1112
#include <serialize.h>
@@ -265,12 +266,13 @@ class AddrManImpl
265266

266267
std::pair<CAddress, int64_t> SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs);
267268

268-
//! Consistency check, taking into account m_consistency_check_ratio. Will std::abort if an inconsistency is detected.
269+
//! Consistency check, taking into account m_consistency_check_ratio.
270+
//! Will std::abort if an inconsistency is detected.
269271
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
270272

271273
//! Perform consistency check, regardless of m_consistency_check_ratio.
272274
//! @returns an error code or zero.
273-
int ForceCheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs);
275+
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs);
274276
};
275277

276278
#endif // BITCOIN_ADDRMAN_IMPL_H

src/logging/timer.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,24 @@ class Timer
2727
Timer(
2828
std::string prefix,
2929
std::string end_msg,
30-
BCLog::LogFlags log_category = BCLog::LogFlags::ALL) :
30+
BCLog::LogFlags log_category = BCLog::LogFlags::ALL,
31+
bool msg_on_completion = true) :
3132
m_prefix(std::move(prefix)),
3233
m_title(std::move(end_msg)),
33-
m_log_category(log_category)
34+
m_log_category(log_category),
35+
m_message_on_completion(msg_on_completion)
3436
{
3537
this->Log(strprintf("%s started", m_title));
3638
m_start_t = GetTime<std::chrono::microseconds>();
3739
}
3840

3941
~Timer()
4042
{
41-
this->Log(strprintf("%s completed", m_title));
43+
if (m_message_on_completion) {
44+
this->Log(strprintf("%s completed", m_title));
45+
} else {
46+
this->Log("completed");
47+
}
4248
}
4349

4450
void Log(const std::string& msg)
@@ -74,14 +80,17 @@ class Timer
7480
std::chrono::microseconds m_start_t{};
7581

7682
//! Log prefix; usually the name of the function this was created in.
77-
const std::string m_prefix{};
83+
const std::string m_prefix;
7884

7985
//! A descriptive message of what is being timed.
80-
const std::string m_title{};
86+
const std::string m_title;
8187

8288
//! Forwarded on to LogPrint if specified - has the effect of only
8389
//! outputting the timing log when a particular debug= category is specified.
84-
const BCLog::LogFlags m_log_category{};
90+
const BCLog::LogFlags m_log_category;
91+
92+
//! Whether to output the message again on completion.
93+
const bool m_message_on_completion;
8594
};
8695

8796
} // namespace BCLog
@@ -91,6 +100,8 @@ class Timer
91100
BCLog::Timer<std::chrono::microseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
92101
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
93102
BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
103+
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category) \
104+
BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category, /* msg_on_completion=*/false)
94105
#define LOG_TIME_SECONDS(end_msg) \
95106
BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg)
96107

test/functional/feature_asmap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def test_asmap_interaction_with_addrman_containing_entries(self):
8989
self.restart_node(0, ["-asmap", "-checkaddrman=1"])
9090
with self.node.assert_debug_log(
9191
expected_msgs=[
92-
"Addrman checks started: new 1, tried 1, total 2",
93-
"Addrman checks completed successfully",
92+
"CheckAddrman: new 1, tried 1, total 2 started",
93+
"CheckAddrman: completed",
9494
]
9595
):
9696
self.node.getnodeaddresses() # getnodeaddresses re-runs the addrman checks

test/functional/rpc_net.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def test_addpeeraddress(self):
260260

261261
self.log.debug("Test that adding a valid address to the tried table succeeds")
262262
assert_equal(node.addpeeraddress(address="1.2.3.4", tried=True, port=8333), {"success": True})
263-
with node.assert_debug_log(expected_msgs=["Addrman checks started: new 0, tried 1, total 1"]):
263+
with node.assert_debug_log(expected_msgs=["CheckAddrman: new 0, tried 1, total 1 started"]):
264264
addrs = node.getnodeaddresses(count=0) # getnodeaddresses re-runs the addrman checks
265265
assert_equal(len(addrs), 1)
266266
assert_equal(addrs[0]["address"], "1.2.3.4")
@@ -273,7 +273,7 @@ def test_addpeeraddress(self):
273273

274274
self.log.debug("Test that adding a second address, this time to the new table, succeeds")
275275
assert_equal(node.addpeeraddress(address="2.0.0.0", port=8333), {"success": True})
276-
with node.assert_debug_log(expected_msgs=["Addrman checks started: new 1, tried 1, total 2"]):
276+
with node.assert_debug_log(expected_msgs=["CheckAddrman: new 1, tried 1, total 2 started"]):
277277
addrs = node.getnodeaddresses(count=0) # getnodeaddresses re-runs the addrman checks
278278
assert_equal(len(addrs), 2)
279279

0 commit comments

Comments
 (0)