merge bitcoin#24157: Replace RecursiveMutex cs_totalBytesSent with Mutex and rename it

This commit is contained in:
Kittywhiskers Van Gogh 2024-04-29 17:53:13 +00:00
parent de4b4bf9ee
commit a1f005ee71
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
2 changed files with 55 additions and 27 deletions

View File

@ -1806,6 +1806,8 @@ void CConnman::SocketEvents(const std::vector<CNode*>& nodes,
void CConnman::SocketHandler(CMasternodeSync& mn_sync) void CConnman::SocketHandler(CMasternodeSync& mn_sync)
{ {
AssertLockNotHeld(m_total_bytes_sent_mutex);
std::set<SOCKET> recv_set; std::set<SOCKET> recv_set;
std::set<SOCKET> send_set; std::set<SOCKET> send_set;
std::set<SOCKET> error_set; std::set<SOCKET> error_set;
@ -1865,6 +1867,8 @@ void CConnman::SocketHandlerConnected(const std::set<SOCKET>& recv_set,
const std::set<SOCKET>& send_set, const std::set<SOCKET>& send_set,
const std::set<SOCKET>& error_set) const std::set<SOCKET>& error_set)
{ {
AssertLockNotHeld(m_total_bytes_sent_mutex);
if (interruptNet) return; if (interruptNet) return;
std::vector<CNode*> vErrorNodes; std::vector<CNode*> vErrorNodes;
@ -2098,6 +2102,8 @@ size_t CConnman::SocketRecvData(CNode *pnode)
void CConnman::ThreadSocketHandler(CMasternodeSync& mn_sync) void CConnman::ThreadSocketHandler(CMasternodeSync& mn_sync)
{ {
AssertLockNotHeld(m_total_bytes_sent_mutex);
int64_t nLastCleanupNodes = 0; int64_t nLastCleanupNodes = 0;
while (!interruptNet) while (!interruptNet)
@ -3293,6 +3299,7 @@ bool CConnman::InitBinds(
bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync, bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync,
CScheduler& scheduler, const Options& connOptions) CScheduler& scheduler, const Options& connOptions)
{ {
AssertLockNotHeld(m_total_bytes_sent_mutex);
Init(connOptions); Init(connOptions);
#ifdef USE_KQUEUE #ifdef USE_KQUEUE
@ -3954,7 +3961,9 @@ void CConnman::RecordBytesRecv(uint64_t bytes)
void CConnman::RecordBytesSent(uint64_t bytes) void CConnman::RecordBytesSent(uint64_t bytes)
{ {
LOCK(cs_totalBytesSent); AssertLockNotHeld(m_total_bytes_sent_mutex);
LOCK(m_total_bytes_sent_mutex);
nTotalBytesSent += bytes; nTotalBytesSent += bytes;
statsClient.count("bandwidth.bytesSent", bytes, 0.01f); statsClient.count("bandwidth.bytesSent", bytes, 0.01f);
statsClient.gauge("bandwidth.totalBytesSent", nTotalBytesSent, 0.01f); statsClient.gauge("bandwidth.totalBytesSent", nTotalBytesSent, 0.01f);
@ -3972,7 +3981,8 @@ void CConnman::RecordBytesSent(uint64_t bytes)
uint64_t CConnman::GetMaxOutboundTarget() const uint64_t CConnman::GetMaxOutboundTarget() const
{ {
LOCK(cs_totalBytesSent); AssertLockNotHeld(m_total_bytes_sent_mutex);
LOCK(m_total_bytes_sent_mutex);
return nMaxOutboundLimit; return nMaxOutboundLimit;
} }
@ -3983,7 +3993,15 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
{ {
LOCK(cs_totalBytesSent); AssertLockNotHeld(m_total_bytes_sent_mutex);
LOCK(m_total_bytes_sent_mutex);
return GetMaxOutboundTimeLeftInCycle_();
}
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle_() const
{
AssertLockHeld(m_total_bytes_sent_mutex);
if (nMaxOutboundLimit == 0) if (nMaxOutboundLimit == 0)
return 0s; return 0s;
@ -3997,14 +4015,15 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
{ {
LOCK(cs_totalBytesSent); AssertLockNotHeld(m_total_bytes_sent_mutex);
LOCK(m_total_bytes_sent_mutex);
if (nMaxOutboundLimit == 0) if (nMaxOutboundLimit == 0)
return false; return false;
if (historicalBlockServingLimit) if (historicalBlockServingLimit)
{ {
// keep a large enough buffer to at least relay each block once // keep a large enough buffer to at least relay each block once
const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle(); const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle_();
const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MaxBlockSize(fDIP0001ActiveAtTip); const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MaxBlockSize(fDIP0001ActiveAtTip);
if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer) if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
return true; return true;
@ -4017,7 +4036,8 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
uint64_t CConnman::GetOutboundTargetBytesLeft() const uint64_t CConnman::GetOutboundTargetBytesLeft() const
{ {
LOCK(cs_totalBytesSent); AssertLockNotHeld(m_total_bytes_sent_mutex);
LOCK(m_total_bytes_sent_mutex);
if (nMaxOutboundLimit == 0) if (nMaxOutboundLimit == 0)
return 0; return 0;
@ -4031,7 +4051,8 @@ uint64_t CConnman::GetTotalBytesRecv() const
uint64_t CConnman::GetTotalBytesSent() const uint64_t CConnman::GetTotalBytesSent() const
{ {
LOCK(cs_totalBytesSent); AssertLockNotHeld(m_total_bytes_sent_mutex);
LOCK(m_total_bytes_sent_mutex);
return nTotalBytesSent; return nTotalBytesSent;
} }
@ -4083,6 +4104,7 @@ bool CConnman::NodeFullyConnected(const CNode* pnode)
void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg) void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
{ {
AssertLockNotHeld(m_total_bytes_sent_mutex);
size_t nMessageSize = msg.data.size(); size_t nMessageSize = msg.data.size();
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.m_type), nMessageSize, pnode->GetId()); LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.m_type), nMessageSize, pnode->GetId());
if (gArgs.GetBoolArg("-capturemessages", false)) { if (gArgs.GetBoolArg("-capturemessages", false)) {

View File

@ -857,7 +857,10 @@ public:
bool m_i2p_accept_incoming; bool m_i2p_accept_incoming;
}; };
void Init(const Options& connOptions) { void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
{
AssertLockNotHeld(m_total_bytes_sent_mutex);
nLocalServices = connOptions.nLocalServices; nLocalServices = connOptions.nLocalServices;
nMaxConnections = connOptions.nMaxConnections; nMaxConnections = connOptions.nMaxConnections;
m_max_outbound_full_relay = std::min(connOptions.m_max_outbound_full_relay, connOptions.nMaxConnections); m_max_outbound_full_relay = std::min(connOptions.m_max_outbound_full_relay, connOptions.nMaxConnections);
@ -873,7 +876,7 @@ public:
nReceiveFloodSize = connOptions.nReceiveFloodSize; nReceiveFloodSize = connOptions.nReceiveFloodSize;
m_peer_connect_timeout = std::chrono::seconds{connOptions.m_peer_connect_timeout}; m_peer_connect_timeout = std::chrono::seconds{connOptions.m_peer_connect_timeout};
{ {
LOCK(cs_totalBytesSent); LOCK(m_total_bytes_sent_mutex);
nMaxOutboundLimit = connOptions.nMaxOutboundLimit; nMaxOutboundLimit = connOptions.nMaxOutboundLimit;
} }
vWhitelistedRange = connOptions.vWhitelistedRange; vWhitelistedRange = connOptions.vWhitelistedRange;
@ -888,7 +891,7 @@ public:
CConnman(uint64_t seed0, uint64_t seed1, CAddrMan& addrman, bool network_active = true); CConnman(uint64_t seed0, uint64_t seed1, CAddrMan& addrman, bool network_active = true);
~CConnman(); ~CConnman();
bool Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync, bool Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync,
CScheduler& scheduler, const Options& options); CScheduler& scheduler, const Options& options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
void StopThreads(); void StopThreads();
void StopNodes(); void StopNodes();
@ -956,7 +959,7 @@ public:
bool IsMasternodeOrDisconnectRequested(const CService& addr); bool IsMasternodeOrDisconnectRequested(const CService& addr);
void PushMessage(CNode* pnode, CSerializedNetMsg&& msg); void PushMessage(CNode* pnode, CSerializedNetMsg&& msg) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
template<typename Condition, typename Callable> template<typename Condition, typename Callable>
bool ForEachNodeContinueIf(const Condition& cond, Callable&& func) bool ForEachNodeContinueIf(const Condition& cond, Callable&& func)
@ -1141,24 +1144,22 @@ public:
//! that peer during `net_processing.cpp:PushNodeVersion()`. //! that peer during `net_processing.cpp:PushNodeVersion()`.
ServiceFlags GetLocalServices() const; ServiceFlags GetLocalServices() const;
uint64_t GetMaxOutboundTarget() const; uint64_t GetMaxOutboundTarget() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
std::chrono::seconds GetMaxOutboundTimeframe() const; std::chrono::seconds GetMaxOutboundTimeframe() const;
//! check if the outbound target is reached //! check if the outbound target is reached
//! if param historicalBlockServingLimit is set true, the function will //! if param historicalBlockServingLimit is set true, the function will
//! response true if the limit for serving historical blocks has been reached //! response true if the limit for serving historical blocks has been reached
bool OutboundTargetReached(bool historicalBlockServingLimit) const; bool OutboundTargetReached(bool historicalBlockServingLimit) const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
//! response the bytes left in the current max outbound cycle //! response the bytes left in the current max outbound cycle
//! in case of no limit, it will always response 0 //! in case of no limit, it will always response 0
uint64_t GetOutboundTargetBytesLeft() const; uint64_t GetOutboundTargetBytesLeft() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
//! returns the time left in the current max outbound cycle std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
//! in case of no limit, it will always return 0
std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const;
uint64_t GetTotalBytesRecv() const; uint64_t GetTotalBytesRecv() const;
uint64_t GetTotalBytesSent() const; uint64_t GetTotalBytesSent() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
/** Get a unique deterministic randomizer. */ /** Get a unique deterministic randomizer. */
CSipHasher GetDeterministicRandomizer(uint64_t id) const; CSipHasher GetDeterministicRandomizer(uint64_t id) const;
@ -1209,6 +1210,10 @@ private:
NetPermissionFlags m_permissions; NetPermissionFlags m_permissions;
}; };
//! returns the time left in the current max outbound cycle
//! in case of no limit, it will always return 0
std::chrono::seconds GetMaxOutboundTimeLeftInCycle_() const EXCLUSIVE_LOCKS_REQUIRED(m_total_bytes_sent_mutex);
bool BindListenPort(const CService& bindAddr, bilingual_str& strError, NetPermissionFlags permissions); bool BindListenPort(const CService& bindAddr, bilingual_str& strError, NetPermissionFlags permissions);
bool Bind(const CService& addr, unsigned int flags, NetPermissionFlags permissions); bool Bind(const CService& addr, unsigned int flags, NetPermissionFlags permissions);
bool InitBinds( bool InitBinds(
@ -1300,7 +1305,7 @@ private:
/** /**
* Check connected and listening sockets for IO readiness and process them accordingly. * Check connected and listening sockets for IO readiness and process them accordingly.
*/ */
void SocketHandler(CMasternodeSync& mn_sync); void SocketHandler(CMasternodeSync& mn_sync) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
/** /**
* Do the read/write for connected sockets that are ready for IO. * Do the read/write for connected sockets that are ready for IO.
@ -1310,7 +1315,8 @@ private:
*/ */
void SocketHandlerConnected(const std::set<SOCKET>& recv_set, void SocketHandlerConnected(const std::set<SOCKET>& recv_set,
const std::set<SOCKET>& send_set, const std::set<SOCKET>& send_set,
const std::set<SOCKET>& error_set); const std::set<SOCKET>& error_set)
EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
/** /**
* Accept incoming connections, one from each read-ready listening socket. * Accept incoming connections, one from each read-ready listening socket.
@ -1318,7 +1324,7 @@ private:
*/ */
void SocketHandlerListening(const std::set<SOCKET>& recv_set, CMasternodeSync& mn_sync); void SocketHandlerListening(const std::set<SOCKET>& recv_set, CMasternodeSync& mn_sync);
void ThreadSocketHandler(CMasternodeSync& mn_sync); void ThreadSocketHandler(CMasternodeSync& mn_sync) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
void ThreadDNSAddressSeed(); void ThreadDNSAddressSeed();
void ThreadOpenMasternodeConnections(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, void ThreadOpenMasternodeConnections(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman,
CMasternodeSync& mn_sync); CMasternodeSync& mn_sync);
@ -1350,7 +1356,7 @@ private:
// Network stats // Network stats
void RecordBytesRecv(uint64_t bytes); void RecordBytesRecv(uint64_t bytes);
void RecordBytesSent(uint64_t bytes); void RecordBytesSent(uint64_t bytes) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
/** /**
* Return vector of current BLOCK_RELAY peers. * Return vector of current BLOCK_RELAY peers.
@ -1364,14 +1370,14 @@ private:
void UnregisterEvents(CNode* pnode); void UnregisterEvents(CNode* pnode);
// Network usage totals // Network usage totals
mutable RecursiveMutex cs_totalBytesSent; mutable Mutex m_total_bytes_sent_mutex;
std::atomic<uint64_t> nTotalBytesRecv{0}; std::atomic<uint64_t> nTotalBytesRecv{0};
uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent) {0}; uint64_t nTotalBytesSent GUARDED_BY(m_total_bytes_sent_mutex) {0};
// outbound limit & stats // outbound limit & stats
uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0}; uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(m_total_bytes_sent_mutex) {0};
std::chrono::seconds nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0}; std::chrono::seconds nMaxOutboundCycleStartTime GUARDED_BY(m_total_bytes_sent_mutex) {0};
uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent); uint64_t nMaxOutboundLimit GUARDED_BY(m_total_bytes_sent_mutex);
// P2P timeout in seconds // P2P timeout in seconds
std::chrono::seconds m_peer_connect_timeout; std::chrono::seconds m_peer_connect_timeout;