Skip to content

Commit

Permalink
net: Use actual memory size in receive buffer accounting
Browse files Browse the repository at this point in the history
Add a method CNetMessage::GetMemoryUsage and use this for accounting of
the size of the process receive queue instead of the raw message size.

This ensures that allocation and deserialization overhead is taken into
account.
  • Loading branch information
laanwj committed Nov 4, 2024
1 parent 047b5e2 commit d22a234
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ size_t CSerializedNetMsg::GetMemoryUsage() const noexcept
return sizeof(*this) + memusage::DynamicUsage(m_type) + memusage::DynamicUsage(data);
}

size_t CNetMessage::GetMemoryUsage() const noexcept
{
return sizeof(*this) + memusage::DynamicUsage(m_type) + m_recv.GetMemoryUsage();
}

void CConnman::AddAddrFetch(const std::string& strDest)
{
LOCK(m_addr_fetches_mutex);
Expand Down Expand Up @@ -3769,7 +3774,7 @@ void CNode::MarkReceivedMsgsForProcessing()
for (const auto& msg : vRecvMsg) {
// vRecvMsg contains only completed CNetMessage
// the single possible partially deserialized message are held by TransportDeserializer
nSizeAdded += msg.m_raw_message_size;
nSizeAdded += msg.GetMemoryUsage();
}

LOCK(m_msg_process_queue_mutex);
Expand All @@ -3786,7 +3791,7 @@ std::optional<std::pair<CNetMessage, bool>> CNode::PollMessage()
std::list<CNetMessage> msgs;
// Just take one message
msgs.splice(msgs.begin(), m_msg_process_queue, m_msg_process_queue.begin());
m_msg_process_queue_size -= msgs.front().m_raw_message_size;
m_msg_process_queue_size -= msgs.front().GetMemoryUsage();
fPauseRecv = m_msg_process_queue_size > m_recv_flood_size;

return std::make_pair(std::move(msgs.front()), !m_msg_process_queue.empty());
Expand Down
3 changes: 3 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ class CNetMessage
CNetMessage(const CNetMessage&) = delete;
CNetMessage& operator=(CNetMessage&&) = default;
CNetMessage& operator=(const CNetMessage&) = delete;

/** Compute total memory usage of this object (own memory + any dynamic memory). */
size_t GetMemoryUsage() const noexcept;
};

/** The Transport converts one connection's sent messages to wire bytes, and received bytes back. */
Expand Down

0 comments on commit d22a234

Please sign in to comment.