Skip to content

Commit a1f005e

Browse files
committed
merge bitcoin#24157: Replace RecursiveMutex cs_totalBytesSent with Mutex and rename it
1 parent de4b4bf commit a1f005e

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

src/net.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,8 @@ void CConnman::SocketEvents(const std::vector<CNode*>& nodes,
18061806

18071807
void CConnman::SocketHandler(CMasternodeSync& mn_sync)
18081808
{
1809+
AssertLockNotHeld(m_total_bytes_sent_mutex);
1810+
18091811
std::set<SOCKET> recv_set;
18101812
std::set<SOCKET> send_set;
18111813
std::set<SOCKET> error_set;
@@ -1865,6 +1867,8 @@ void CConnman::SocketHandlerConnected(const std::set<SOCKET>& recv_set,
18651867
const std::set<SOCKET>& send_set,
18661868
const std::set<SOCKET>& error_set)
18671869
{
1870+
AssertLockNotHeld(m_total_bytes_sent_mutex);
1871+
18681872
if (interruptNet) return;
18691873

18701874
std::vector<CNode*> vErrorNodes;
@@ -2098,6 +2102,8 @@ size_t CConnman::SocketRecvData(CNode *pnode)
20982102

20992103
void CConnman::ThreadSocketHandler(CMasternodeSync& mn_sync)
21002104
{
2105+
AssertLockNotHeld(m_total_bytes_sent_mutex);
2106+
21012107
int64_t nLastCleanupNodes = 0;
21022108

21032109
while (!interruptNet)
@@ -3293,6 +3299,7 @@ bool CConnman::InitBinds(
32933299
bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync,
32943300
CScheduler& scheduler, const Options& connOptions)
32953301
{
3302+
AssertLockNotHeld(m_total_bytes_sent_mutex);
32963303
Init(connOptions);
32973304

32983305
#ifdef USE_KQUEUE
@@ -3954,7 +3961,9 @@ void CConnman::RecordBytesRecv(uint64_t bytes)
39543961

39553962
void CConnman::RecordBytesSent(uint64_t bytes)
39563963
{
3957-
LOCK(cs_totalBytesSent);
3964+
AssertLockNotHeld(m_total_bytes_sent_mutex);
3965+
LOCK(m_total_bytes_sent_mutex);
3966+
39583967
nTotalBytesSent += bytes;
39593968
statsClient.count("bandwidth.bytesSent", bytes, 0.01f);
39603969
statsClient.gauge("bandwidth.totalBytesSent", nTotalBytesSent, 0.01f);
@@ -3972,7 +3981,8 @@ void CConnman::RecordBytesSent(uint64_t bytes)
39723981

39733982
uint64_t CConnman::GetMaxOutboundTarget() const
39743983
{
3975-
LOCK(cs_totalBytesSent);
3984+
AssertLockNotHeld(m_total_bytes_sent_mutex);
3985+
LOCK(m_total_bytes_sent_mutex);
39763986
return nMaxOutboundLimit;
39773987
}
39783988

@@ -3983,7 +3993,15 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const
39833993

39843994
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
39853995
{
3986-
LOCK(cs_totalBytesSent);
3996+
AssertLockNotHeld(m_total_bytes_sent_mutex);
3997+
LOCK(m_total_bytes_sent_mutex);
3998+
return GetMaxOutboundTimeLeftInCycle_();
3999+
}
4000+
4001+
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle_() const
4002+
{
4003+
AssertLockHeld(m_total_bytes_sent_mutex);
4004+
39874005
if (nMaxOutboundLimit == 0)
39884006
return 0s;
39894007

@@ -3997,14 +4015,15 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
39974015

39984016
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
39994017
{
4000-
LOCK(cs_totalBytesSent);
4018+
AssertLockNotHeld(m_total_bytes_sent_mutex);
4019+
LOCK(m_total_bytes_sent_mutex);
40014020
if (nMaxOutboundLimit == 0)
40024021
return false;
40034022

40044023
if (historicalBlockServingLimit)
40054024
{
40064025
// keep a large enough buffer to at least relay each block once
4007-
const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
4026+
const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle_();
40084027
const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MaxBlockSize(fDIP0001ActiveAtTip);
40094028
if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
40104029
return true;
@@ -4017,7 +4036,8 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
40174036

40184037
uint64_t CConnman::GetOutboundTargetBytesLeft() const
40194038
{
4020-
LOCK(cs_totalBytesSent);
4039+
AssertLockNotHeld(m_total_bytes_sent_mutex);
4040+
LOCK(m_total_bytes_sent_mutex);
40214041
if (nMaxOutboundLimit == 0)
40224042
return 0;
40234043

@@ -4031,7 +4051,8 @@ uint64_t CConnman::GetTotalBytesRecv() const
40314051

40324052
uint64_t CConnman::GetTotalBytesSent() const
40334053
{
4034-
LOCK(cs_totalBytesSent);
4054+
AssertLockNotHeld(m_total_bytes_sent_mutex);
4055+
LOCK(m_total_bytes_sent_mutex);
40354056
return nTotalBytesSent;
40364057
}
40374058

@@ -4083,6 +4104,7 @@ bool CConnman::NodeFullyConnected(const CNode* pnode)
40834104

40844105
void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
40854106
{
4107+
AssertLockNotHeld(m_total_bytes_sent_mutex);
40864108
size_t nMessageSize = msg.data.size();
40874109
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.m_type), nMessageSize, pnode->GetId());
40884110
if (gArgs.GetBoolArg("-capturemessages", false)) {

src/net.h

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,10 @@ friend class CNode;
857857
bool m_i2p_accept_incoming;
858858
};
859859

860-
void Init(const Options& connOptions) {
860+
void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
861+
{
862+
AssertLockNotHeld(m_total_bytes_sent_mutex);
863+
861864
nLocalServices = connOptions.nLocalServices;
862865
nMaxConnections = connOptions.nMaxConnections;
863866
m_max_outbound_full_relay = std::min(connOptions.m_max_outbound_full_relay, connOptions.nMaxConnections);
@@ -873,7 +876,7 @@ friend class CNode;
873876
nReceiveFloodSize = connOptions.nReceiveFloodSize;
874877
m_peer_connect_timeout = std::chrono::seconds{connOptions.m_peer_connect_timeout};
875878
{
876-
LOCK(cs_totalBytesSent);
879+
LOCK(m_total_bytes_sent_mutex);
877880
nMaxOutboundLimit = connOptions.nMaxOutboundLimit;
878881
}
879882
vWhitelistedRange = connOptions.vWhitelistedRange;
@@ -888,7 +891,7 @@ friend class CNode;
888891
CConnman(uint64_t seed0, uint64_t seed1, CAddrMan& addrman, bool network_active = true);
889892
~CConnman();
890893
bool Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync,
891-
CScheduler& scheduler, const Options& options);
894+
CScheduler& scheduler, const Options& options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
892895

893896
void StopThreads();
894897
void StopNodes();
@@ -956,7 +959,7 @@ friend class CNode;
956959

957960
bool IsMasternodeOrDisconnectRequested(const CService& addr);
958961

959-
void PushMessage(CNode* pnode, CSerializedNetMsg&& msg);
962+
void PushMessage(CNode* pnode, CSerializedNetMsg&& msg) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
960963

961964
template<typename Condition, typename Callable>
962965
bool ForEachNodeContinueIf(const Condition& cond, Callable&& func)
@@ -1141,24 +1144,22 @@ friend class CNode;
11411144
//! that peer during `net_processing.cpp:PushNodeVersion()`.
11421145
ServiceFlags GetLocalServices() const;
11431146

1144-
uint64_t GetMaxOutboundTarget() const;
1147+
uint64_t GetMaxOutboundTarget() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
11451148
std::chrono::seconds GetMaxOutboundTimeframe() const;
11461149

11471150
//! check if the outbound target is reached
11481151
//! if param historicalBlockServingLimit is set true, the function will
11491152
//! response true if the limit for serving historical blocks has been reached
1150-
bool OutboundTargetReached(bool historicalBlockServingLimit) const;
1153+
bool OutboundTargetReached(bool historicalBlockServingLimit) const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
11511154

11521155
//! response the bytes left in the current max outbound cycle
11531156
//! in case of no limit, it will always response 0
1154-
uint64_t GetOutboundTargetBytesLeft() const;
1157+
uint64_t GetOutboundTargetBytesLeft() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
11551158

1156-
//! returns the time left in the current max outbound cycle
1157-
//! in case of no limit, it will always return 0
1158-
std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const;
1159+
std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
11591160

11601161
uint64_t GetTotalBytesRecv() const;
1161-
uint64_t GetTotalBytesSent() const;
1162+
uint64_t GetTotalBytesSent() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
11621163

11631164
/** Get a unique deterministic randomizer. */
11641165
CSipHasher GetDeterministicRandomizer(uint64_t id) const;
@@ -1209,6 +1210,10 @@ friend class CNode;
12091210
NetPermissionFlags m_permissions;
12101211
};
12111212

1213+
//! returns the time left in the current max outbound cycle
1214+
//! in case of no limit, it will always return 0
1215+
std::chrono::seconds GetMaxOutboundTimeLeftInCycle_() const EXCLUSIVE_LOCKS_REQUIRED(m_total_bytes_sent_mutex);
1216+
12121217
bool BindListenPort(const CService& bindAddr, bilingual_str& strError, NetPermissionFlags permissions);
12131218
bool Bind(const CService& addr, unsigned int flags, NetPermissionFlags permissions);
12141219
bool InitBinds(
@@ -1300,7 +1305,7 @@ friend class CNode;
13001305
/**
13011306
* Check connected and listening sockets for IO readiness and process them accordingly.
13021307
*/
1303-
void SocketHandler(CMasternodeSync& mn_sync);
1308+
void SocketHandler(CMasternodeSync& mn_sync) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
13041309

13051310
/**
13061311
* Do the read/write for connected sockets that are ready for IO.
@@ -1310,15 +1315,16 @@ friend class CNode;
13101315
*/
13111316
void SocketHandlerConnected(const std::set<SOCKET>& recv_set,
13121317
const std::set<SOCKET>& send_set,
1313-
const std::set<SOCKET>& error_set);
1318+
const std::set<SOCKET>& error_set)
1319+
EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
13141320

13151321
/**
13161322
* Accept incoming connections, one from each read-ready listening socket.
13171323
* @param[in] recv_set Sockets that are ready for read.
13181324
*/
13191325
void SocketHandlerListening(const std::set<SOCKET>& recv_set, CMasternodeSync& mn_sync);
13201326

1321-
void ThreadSocketHandler(CMasternodeSync& mn_sync);
1327+
void ThreadSocketHandler(CMasternodeSync& mn_sync) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
13221328
void ThreadDNSAddressSeed();
13231329
void ThreadOpenMasternodeConnections(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman,
13241330
CMasternodeSync& mn_sync);
@@ -1350,7 +1356,7 @@ friend class CNode;
13501356

13511357
// Network stats
13521358
void RecordBytesRecv(uint64_t bytes);
1353-
void RecordBytesSent(uint64_t bytes);
1359+
void RecordBytesSent(uint64_t bytes) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
13541360

13551361
/**
13561362
* Return vector of current BLOCK_RELAY peers.
@@ -1364,14 +1370,14 @@ friend class CNode;
13641370
void UnregisterEvents(CNode* pnode);
13651371

13661372
// Network usage totals
1367-
mutable RecursiveMutex cs_totalBytesSent;
1373+
mutable Mutex m_total_bytes_sent_mutex;
13681374
std::atomic<uint64_t> nTotalBytesRecv{0};
1369-
uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent) {0};
1375+
uint64_t nTotalBytesSent GUARDED_BY(m_total_bytes_sent_mutex) {0};
13701376

13711377
// outbound limit & stats
1372-
uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0};
1373-
std::chrono::seconds nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0};
1374-
uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent);
1378+
uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(m_total_bytes_sent_mutex) {0};
1379+
std::chrono::seconds nMaxOutboundCycleStartTime GUARDED_BY(m_total_bytes_sent_mutex) {0};
1380+
uint64_t nMaxOutboundLimit GUARDED_BY(m_total_bytes_sent_mutex);
13751381

13761382
// P2P timeout in seconds
13771383
std::chrono::seconds m_peer_connect_timeout;

0 commit comments

Comments
 (0)