Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move cs_vSend into SocketSendData and resolve RecordBytesSent lock inconsistency #19673

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/net.cpp
Expand Up @@ -747,8 +747,9 @@ void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vec
CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, header, 0, hdr};
}

size_t CConnman::SocketSendData(CNode *pnode) const EXCLUSIVE_LOCKS_REQUIRED(pnode->cs_vSend)
size_t CConnman::SocketSendData(CNode *pnode) const
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9d47d6d

nit, clang-format suggests:

Suggested change
size_t CConnman::SocketSendData(CNode *pnode) const
size_t CConnman::SocketSendData(CNode* pnode) const

{
LOCK(pnode->cs_vSend);
auto it = pnode->vSendMsg.begin();
size_t nSentSize = 0;

Expand Down Expand Up @@ -1445,11 +1446,8 @@ void CConnman::SocketHandler()
//
if (sendSet)
{
Comment on lines 1447 to 1448
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9d47d6d, nit:

        if (sendSet) {

LOCK(pnode->cs_vSend);
size_t nBytes = SocketSendData(pnode);
if (nBytes) {
RecordBytesSent(nBytes);
}
const size_t bytes_sent{SocketSendData(pnode)};
if (bytes_sent) RecordBytesSent(bytes_sent);
}

InactivityCheck(pnode);
Expand Down Expand Up @@ -2821,10 +2819,10 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
pnode->m_serializer->prepareForTransport(msg, serialized_header);
const size_t total_size{message_size + serialized_header.size()};

size_t bytes_sent{0};
bool optimistic_send{false};
{
LOCK(pnode->cs_vSend);
bool optimistic_send{pnode->vSendMsg.empty()};
optimistic_send = pnode->vSendMsg.empty();

//log total amount of bytes per message type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you retouch the branch, you may as well touch every line in this function:

Suggested change
//log total amount of bytes per message type
// Log total amount of bytes per message type

pnode->mapSendBytesPerMsgCmd[msg.m_type] += total_size;
Expand All @@ -2837,13 +2835,12 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
if (message_size) {
pnode->vSendMsg.push_back(std::move(msg.data));
}

// If write queue empty, attempt "optimistic write"
if (optimistic_send) {
bytes_sent = SocketSendData(pnode);
}
}
if (bytes_sent) RecordBytesSent(bytes_sent);
// If write queue was empty, attempt "optimistic write"
if (optimistic_send) {
const size_t bytes_sent{SocketSendData(pnode)};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed? Previously we'd optimistically write only one message, now we might write more than one message when other messages have been pushed by other threads.

if (bytes_sent) RecordBytesSent(bytes_sent);
}
}

bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
Expand Down