Skip to content

Commit cdc8321

Browse files
committed
merge bitcoin#22872: improve checkaddrman logging with duration in milliseconds
1 parent 8d22fe9 commit cdc8321

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>
@@ -389,7 +391,7 @@ void AddrManImpl::Unserialize(Stream& s_)
389391
LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions or invalid addresses\n", nLostUnk, nLost);
390392
}
391393

392-
const int check_code{ForceCheckAddrman()};
394+
const int check_code{CheckAddrman()};
393395
if (check_code != 0) {
394396
throw DbInconsistentError(strprintf(
395397
"Corrupt data. Consistency check failed with code %s",
@@ -956,18 +958,19 @@ void AddrManImpl::Check() const
956958
if (m_consistency_check_ratio == 0) return;
957959
if (insecure_rand.randrange(m_consistency_check_ratio) >= 1) return;
958960

959-
const int err{ForceCheckAddrman()};
961+
const int err{CheckAddrman()};
960962
if (err) {
961963
LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
962964
assert(false);
963965
}
964966
}
965967

966-
int AddrManImpl::ForceCheckAddrman() const
968+
int AddrManImpl::CheckAddrman() const
967969
{
968970
AssertLockHeld(cs);
969971

970-
LogPrint(BCLog::ADDRMAN, "Addrman checks started: new %i, tried %i, total %u\n", nNew, nTried, vRandom.size());
972+
LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
973+
strprintf("new %i, tried %i, total %u", nNew, nTried, vRandom.size()), BCLog::ADDRMAN);
971974

972975
std::unordered_set<int> setTried;
973976
std::unordered_map<int, int> mapNew;
@@ -1047,7 +1050,6 @@ int AddrManImpl::ForceCheckAddrman() const
10471050
if (nKey.IsNull())
10481051
return -16;
10491052

1050-
LogPrint(BCLog::ADDRMAN, "Addrman checks completed successfully\n");
10511053
return 0;
10521054
}
10531055

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>
@@ -269,12 +270,13 @@ class AddrManImpl
269270

270271
std::pair<CAddress, int64_t> SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs);
271272

272-
//! Consistency check, taking into account m_consistency_check_ratio. Will std::abort if an inconsistency is detected.
273+
//! Consistency check, taking into account m_consistency_check_ratio.
274+
//! Will std::abort if an inconsistency is detected.
273275
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
274276

275277
//! Perform consistency check, regardless of m_consistency_check_ratio.
276278
//! @returns an error code or zero.
277-
int ForceCheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs);
279+
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs);
278280
};
279281

280282
#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> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)
92101
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
93102
BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)
103+
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category) \
104+
BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category, /* msg_on_completion=*/false)
94105
#define LOG_TIME_SECONDS(end_msg) \
95106
BCLog::Timer<std::chrono::seconds> UNIQUE_NAME(logging_timer)(__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
@@ -280,7 +280,7 @@ def test_addpeeraddress(self):
280280

281281
self.log.debug("Test that adding a valid address to the tried table succeeds")
282282
assert_equal(node.addpeeraddress(address="1.2.3.4", tried=True, port=8333), {"success": True})
283-
with node.assert_debug_log(expected_msgs=["Addrman checks started: new 0, tried 1, total 1"]):
283+
with node.assert_debug_log(expected_msgs=["CheckAddrman: new 0, tried 1, total 1 started"]):
284284
addrs = node.getnodeaddresses(count=0) # getnodeaddresses re-runs the addrman checks
285285
assert_equal(len(addrs), 1)
286286
assert_equal(addrs[0]["address"], "1.2.3.4")
@@ -293,7 +293,7 @@ def test_addpeeraddress(self):
293293

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

0 commit comments

Comments
 (0)