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

net: implement poll #14336

Merged
merged 5 commits into from Jan 2, 2019

Conversation

Projects
None yet
@pstratem
Copy link
Contributor

commented Sep 26, 2018

Implement poll() on systems which support it properly.

This eliminates the restriction on maximum socket descriptor number.

@fanquake fanquake added the P2P label Sep 26, 2018

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch 3 times, most recently Sep 27, 2018

@DrahtBot

This comment has been minimized.

Copy link
Contributor

commented Sep 27, 2018

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch Sep 27, 2018

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch 5 times, most recently Sep 27, 2018

@pstratem

This comment has been minimized.

Copy link
Contributor Author

commented Sep 29, 2018

It seems that lots of the functional tests have races cause this pull request keeps triggering random seeming failures.

Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
@jimpo
Copy link
Contributor

left a comment

Concept ACK

Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
int nErr = WSAGetLastError();
LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
for (unsigned int i = 0; i <= hSocketMax; i++)
FD_SET(i, &fdsetRecv);

This comment has been minimized.

Copy link
@jimpo

jimpo Sep 29, 2018

Contributor

I see this is the same as the code currently, but it seems like this should be fdsetError instead of fdsetRecv, even though they are handled exactly the same.

This comment has been minimized.

Copy link
@pstratem

pstratem Oct 1, 2018

Author Contributor

I don't think it should be there at all really. select() failures dont mean the sockets themselves have failed, but i guess it could mean we made a mistake and called select() with a closed socket?

This comment has been minimized.

Copy link
@jimpo

jimpo Oct 2, 2018

Contributor

Yeah, then iterating through each one and calling recv would identify which peer to disconnect. Though, if it's fdsetError and not fdsetRecv, it probably shouldn't disconnect when nBytes == 0.

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch 3 times, most recently Oct 1, 2018

@laanwj

This comment has been minimized.

Copy link
Member

commented Oct 2, 2018

Concept ACK, will review in detail when my brain works again.

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch Oct 2, 2018

@theuni

This comment has been minimized.

Copy link
Member

commented Oct 2, 2018

Concept ACK. Will also review shortly.

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch 2 times, most recently Oct 16, 2018

@jamesob
Copy link
Member

left a comment

Some minor comments and a question about conservatively falling back to select for MacOS systems. Code looks good!

Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch 5 times, most recently Nov 11, 2018

Show resolved Hide resolved src/net.h Outdated
Show resolved Hide resolved src/netbase.h Outdated
Show resolved Hide resolved src/init.cpp
Show resolved Hide resolved src/net.cpp
Show resolved Hide resolved src/net.cpp Outdated
Show resolved Hide resolved src/net.cpp Outdated

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch 4 times, most recently Nov 29, 2018

pstratem added some commits Sep 25, 2018

Move GenerateSelectSet logic to private method.
This separates the socket event collection logic from the logic
deciding which events we're interested in at all.
Move SocketEvents logic to private method.
This separates the select() logic from the socket handling logic, setting up
for a switch to poll().

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch Nov 30, 2018

@pstratem

This comment has been minimized.

Copy link
Contributor Author

commented Nov 30, 2018

With 87 connections the effect of generating the pollfd list shows up on a profile but isn't significant.

The poll() implementation is on the order of 1000 ns slower per call than the select() implementation.

I don't think that's significant.

@jamesob

jamesob approved these changes Dec 3, 2018

Copy link
Member

left a comment

utACK 20c9601

Code looks good, feel free to ignore the small comments. I'm running tests at the moment and will report back with results within the next day - testnet happy-path behavior looks good so far.

Show resolved Hide resolved src/net.cpp
}
}
#else
void CConnman::SocketEvents(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set)

This comment has been minimized.

Copy link
@jamesob

jamesob Dec 3, 2018

Member

(non-blocking)

diff --git a/src/net.cpp b/src/net.cpp
index 7b8b6e5ea2..03d165d701 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -1355,6 +1355,7 @@ void CConnman::SocketEvents(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_s
     }
 }
 #else
+// Use select() on platforms where poll() is unavailable.
 void CConnman::SocketEvents(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set)
 {
     std::set<SOCKET> recv_select_set, send_select_set, error_select_set;
Show resolved Hide resolved src/netbase.cpp Outdated
struct timeval tval = MillisToTimeval(std::min(endTime - curTime, maxWait));
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(hSocket, &fdset);
int nRet = select(hSocket + 1, &fdset, nullptr, nullptr, &tval);
#endif
if (nRet == SOCKET_ERROR) {

This comment has been minimized.

Copy link
@jamesob

jamesob Dec 3, 2018

Member

(non-blocking) This might be academic, but since this clause is handling both select() and poll() might be safer to say if (nRet < 0)? Though right now there's no difference in the return value for both APIs (and I doubt that'll ever change...).

pstratem added some commits Sep 27, 2018

Implement poll() on systems which support it properly.
This eliminates the restriction on maximum socket descriptor number.

@pstratem pstratem force-pushed the pstratem:2018-09-24-socket-handler-poll branch to 4927bf2 Dec 3, 2018

@jamesob

jamesob approved these changes Dec 4, 2018

Copy link
Member

left a comment

Tested ACK 4927bf2

Tested on Linux 4.15.0-39-generic and macOS 10.12.1. Tested IBD on testnet as well as tweaking -maxconnections and observing connections with lsof -a -itcp -p $(pidof bitcoind).

Only code change since my last utACK is a small cleanup in netbase.cpp.

Thanks for the work here @pstratem.

@laanwj

This comment has been minimized.

Copy link
Member

commented Dec 13, 2018

utACK 4927bf2

@laanwj laanwj merged commit 4927bf2 into bitcoin:master Jan 2, 2019

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

laanwj added a commit that referenced this pull request Jan 2, 2019

Merge #14336: net: implement poll
4927bf2 Increase maxconnections limit when using poll. (Patrick Strateman)
11cc491 Implement poll() on systems which support it properly. (Patrick Strateman)
28211a4 Move SocketEvents logic to private method. (Patrick Strateman)
7e403c0 Move GenerateSelectSet logic to private method. (Patrick Strateman)
1e6afd0 Introduce and use constant SELECT_TIMEOUT_MILLISECONDS. (Patrick Strateman)

Pull request description:

  Implement poll() on systems which support it properly.

  This eliminates the restriction on maximum socket descriptor number.

Tree-SHA512: b945cd9294afdafcce96d547f67679d5cdd684cf257904a239cd1248de3b5e093b8d6d28d8d1b7cc923dc0b2b5723faef9bc9bf118a9ce1bdcf357c2323f5573

@fanquake fanquake removed this from Blockers in High-priority for review Jan 3, 2019

@lavie

This comment has been minimized.

Copy link

commented on src/compat.h in 11cc491 Jan 3, 2019

nit: broke -> broken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.