Skip to content

Commit 597b8be

Browse files
committed
Merge bitcoin#34025: net: Waste less time in socket handling
5f5c1ea net: Cache -capturemessages setting (Anthony Towns) cea443e net: Pass time to InactivityChecks fuctions (Anthony Towns) Pull request description: Cuts out some wasted time in net socket handling. First, only calculates the current time once every 50ms, rather than once for each peer, which given we only care about second-level precision seems more than adequate. Second, caches the value of the `-capturemessages` setting in `CConnman` rather than re-evaluating it every time we invoke `PushMessaage`. ACKs for top commit: maflcko: review ACK 5f5c1ea 🏣 vasild: ACK 5f5c1ea sedited: ACK 5f5c1ea mzumsande: ACK 5f5c1ea Tree-SHA512: 0194143a3a4481c6355ac9eab27ce6ae4bed5db1d483ba5d06288dd92f195ccb9f0f055a9eb9d7e16e9bbf72f145eca1ff17c6700ee9aa42730103a8f047b32c
2 parents d155fc1 + 5f5c1ea commit 597b8be

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
20432043
connOptions.m_peer_connect_timeout = peer_connect_timeout;
20442044
connOptions.whitelist_forcerelay = args.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY);
20452045
connOptions.whitelist_relay = args.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY);
2046+
connOptions.m_capture_messages = args.GetBoolArg("-capturemessages", false);
20462047

20472048
// Port to bind to if `-bind=addr` is provided without a `:port` suffix.
20482049
const uint16_t default_bind_port =

src/net.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,16 +2000,15 @@ void CConnman::NotifyNumConnectionsChanged()
20002000
}
20012001
}
20022002

2003-
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const
2003+
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const
20042004
{
20052005
return node.m_connected + m_peer_connect_timeout < now;
20062006
}
20072007

2008-
bool CConnman::InactivityCheck(const CNode& node) const
2008+
bool CConnman::InactivityCheck(const CNode& node, std::chrono::microseconds now) const
20092009
{
20102010
// Tests that see disconnects after using mocktime can start nodes with a
20112011
// large timeout. For example, -peertimeout=999999999.
2012-
const auto now{GetTime<std::chrono::seconds>()};
20132012
const auto last_send{node.m_last_send.load()};
20142013
const auto last_recv{node.m_last_recv.load()};
20152014

@@ -2033,15 +2032,15 @@ bool CConnman::InactivityCheck(const CNode& node) const
20332032

20342033
if (now > last_send + TIMEOUT_INTERVAL) {
20352034
LogDebug(BCLog::NET,
2036-
"socket sending timeout: %is, %s\n", count_seconds(now - last_send),
2035+
"socket sending timeout: %is, %s\n", Ticks<std::chrono::seconds>(now - last_send),
20372036
node.DisconnectMsg(fLogIPs)
20382037
);
20392038
return true;
20402039
}
20412040

20422041
if (now > last_recv + TIMEOUT_INTERVAL) {
20432042
LogDebug(BCLog::NET,
2044-
"socket receive timeout: %is, %s\n", count_seconds(now - last_recv),
2043+
"socket receive timeout: %is, %s\n", Ticks<std::chrono::seconds>(now - last_recv),
20452044
node.DisconnectMsg(fLogIPs)
20462045
);
20472046
return true;
@@ -2123,6 +2122,8 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
21232122
{
21242123
AssertLockNotHeld(m_total_bytes_sent_mutex);
21252124

2125+
auto now = GetTime<std::chrono::microseconds>();
2126+
21262127
for (CNode* pnode : nodes) {
21272128
if (m_interrupt_net->interrupted()) {
21282129
return;
@@ -2214,7 +2215,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
22142215
}
22152216
}
22162217

2217-
if (InactivityCheck(*pnode)) pnode->fDisconnect = true;
2218+
if (InactivityCheck(*pnode, now)) pnode->fDisconnect = true;
22182219
}
22192220
}
22202221

@@ -3900,7 +3901,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
39003901
AssertLockNotHeld(m_total_bytes_sent_mutex);
39013902
size_t nMessageSize = msg.data.size();
39023903
LogDebug(BCLog::NET, "sending %s (%d bytes) peer=%d\n", msg.m_type, nMessageSize, pnode->GetId());
3903-
if (gArgs.GetBoolArg("-capturemessages", false)) {
3904+
if (m_capture_messages) {
39043905
CaptureMessage(pnode->addr, msg.m_type, msg.data, /*is_incoming=*/false);
39053906
}
39063907

src/net.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@ class CConnman
10861086
bool m_i2p_accept_incoming;
10871087
bool whitelist_forcerelay = DEFAULT_WHITELISTFORCERELAY;
10881088
bool whitelist_relay = DEFAULT_WHITELISTRELAY;
1089+
bool m_capture_messages = false;
10891090
};
10901091

10911092
void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_total_bytes_sent_mutex)
@@ -1123,8 +1124,12 @@ class CConnman
11231124
m_onion_binds = connOptions.onion_binds;
11241125
whitelist_forcerelay = connOptions.whitelist_forcerelay;
11251126
whitelist_relay = connOptions.whitelist_relay;
1127+
m_capture_messages = connOptions.m_capture_messages;
11261128
}
11271129

1130+
// test only
1131+
void SetCaptureMessages(bool cap) { m_capture_messages = cap; }
1132+
11281133
CConnman(uint64_t seed0,
11291134
uint64_t seed1,
11301135
AddrMan& addrman,
@@ -1314,7 +1319,7 @@ class CConnman
13141319
void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
13151320

13161321
/** Return true if we should disconnect the peer for failing an inactivity check. */
1317-
bool ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const;
1322+
bool ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const;
13181323

13191324
bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex);
13201325

@@ -1364,7 +1369,7 @@ class CConnman
13641369
void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
13651370
void NotifyNumConnectionsChanged();
13661371
/** Return true if the peer is inactive and should be disconnected. */
1367-
bool InactivityCheck(const CNode& node) const;
1372+
bool InactivityCheck(const CNode& node, std::chrono::microseconds now) const;
13681373

13691374
/**
13701375
* Generate a collection of sockets to check for IO readiness.
@@ -1666,6 +1671,11 @@ class CConnman
16661671
*/
16671672
bool whitelist_relay;
16681673

1674+
/**
1675+
* flag for whether messages are captured
1676+
*/
1677+
bool m_capture_messages{false};
1678+
16691679
/**
16701680
* Mutex protecting m_i2p_sam_sessions.
16711681
*/

src/test/net_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
814814
// Pretend that we bound to this port.
815815
const uint16_t bind_port = 20001;
816816
m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
817-
m_node.args->ForceSetArg("-capturemessages", "1");
817+
m_node.connman->SetCaptureMessages(true);
818818

819819
// Our address:port as seen from the peer - 2.3.4.5:20002 (different from the above).
820820
in_addr peer_us_addr;
@@ -892,7 +892,7 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
892892

893893
CaptureMessage = CaptureMessageOrig;
894894
chainman.ResetIbd();
895-
m_node.args->ForceSetArg("-capturemessages", "0");
895+
m_node.connman->SetCaptureMessages(false);
896896
m_node.args->ForceSetArg("-bind", "");
897897
}
898898

0 commit comments

Comments
 (0)