Only call SendMessages when ProcessMessages did some work or when some time passed

Invoking SendMessages for hundreds of peers while we know that there is no
work to do give a lot of overhead on the message handler thread.
This commit is contained in:
Alexander Block 2020-04-07 06:43:13 +02:00
parent 79f0bb1033
commit 24ead62905
4 changed files with 21 additions and 6 deletions

View File

@ -2436,11 +2436,20 @@ void CConnman::OpenMasternodeConnection(const CAddress &addrConnect, bool probe)
void CConnman::ThreadMessageHandler()
{
int64_t nLastForceSendMessages = 0;
while (!flagInterruptMsgProc)
{
std::vector<CNode*> vNodesCopy = CopyNodeVector();
int64_t nNow = GetTimeMillis();
bool fMoreWork = false;
bool fForceSendMessages = false;
if (nNow - nLastForceSendMessages >= 100) {
fForceSendMessages = true;
nLastForceSendMessages = nNow;
}
for (CNode* pnode : vNodesCopy)
{
@ -2448,12 +2457,13 @@ void CConnman::ThreadMessageHandler()
continue;
// Receive messages
bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc);
bool fDidWork = false;
bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc, fDidWork);
fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
if (flagInterruptMsgProc)
return;
// Send messages
{
if (fDidWork || fForceSendMessages) {
LOCK(pnode->cs_sendProcessing);
m_msgproc->SendMessages(pnode, flagInterruptMsgProc);
}

View File

@ -633,7 +633,7 @@ struct CombinerAll
class NetEventsInterface
{
public:
virtual bool ProcessMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0;
virtual bool ProcessMessages(CNode* pnode, std::atomic<bool>& interrupt, bool &fRetDidWork) = 0;
virtual bool SendMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0;
virtual void InitializeNode(CNode* pnode) = 0;
virtual void FinalizeNode(NodeId id, bool& update_connection_time) = 0;

View File

@ -3529,7 +3529,7 @@ static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman* connman)
return false;
}
bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc, bool &fRetDidWork)
{
const CChainParams& chainparams = Params();
//
@ -3541,13 +3541,17 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter
// (x) data
//
bool fMoreWork = false;
fRetDidWork = false;
if (!pfrom->vRecvGetData.empty())
if (!pfrom->vRecvGetData.empty()) {
ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
fRetDidWork = true;
}
if (!pfrom->orphan_work_set.empty()) {
LOCK2(cs_main, g_cs_orphans);
ProcessOrphanTx(connman, pfrom->orphan_work_set);
fRetDidWork = true;
}
if (pfrom->fDisconnect)
@ -3571,6 +3575,7 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter
pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman->GetReceiveFloodSize();
fMoreWork = !pfrom->vProcessMsg.empty();
fRetDidWork = true;
}
CNetMessage& msg(msgs.front());

View File

@ -53,7 +53,7 @@ public:
void InitializeNode(CNode* pnode) override;
void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) override;
/** Process protocol messages received from a given node */
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt, bool &fRetDidWork) override;
/**
* Send queued protocol messages to be sent to a give node.
*