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

Use SOCK_NONBLOCK to reduce system calls for outgoing connections #293

Merged
merged 4 commits into from Apr 30, 2024

Conversation

panjf2000
Copy link
Contributor

@panjf2000 panjf2000 commented Apr 11, 2024

What this PR mainly does is:

  1. Refactor the anetCreateSocket() to make it more generic for more socket arguments, and use SOCK_NONBLOCK if available, which will reduce two system calls (F_GETFL and F_SETFL) of enabling the non-blocking mode on each newly created socket.
  2. Clean up the unused anetUnixGenericConnect() that calls anetCreateSocket().

SOCK_NONBLOCK for system call socket() is supported on most UNIX-like platforms (linux, dragonfly, freebsd, netbsd, openbsd, solaris, etc.). This improvement will significantly reduce the system calls considering how massively anetTcpGenericConnect() will be called when needed.

As for the cleanup, anetUnixGenericConnect was introduced in c61e692 and the only reference back then was from the createClient() in redis-benchmark.c which had been removed in ec8f066 and made it the dead code. Most of that dead code was also cleaned up in f657315, and it seems that the anetUnixGenericConnect got left out. Therefore, I also cleaned it up, but I'm not so certain about doing this cleanup in this PR. Maybe you would prefer to do it in a separate PR?

References

panjf2000 and others added 3 commits April 11, 2024 11:57
@panjf2000
Copy link
Contributor Author

@madolson @zuiderkwast Got a minute for this?

src/anet.c Outdated Show resolved Hide resolved
src/anet.c Show resolved Hide resolved
Copy link
Contributor

@zuiderkwast zuiderkwast left a comment

Choose a reason for hiding this comment

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

It looks good in general. Just a few small suggestions.

I'm not sure it has a very big impact though. It's only used for creating the listening socket and for creating outgoing connections, which is basically only for replication and cluster nodes (maybe something more?) but we don't use it for valkey-cli, valkey-benchmark and (IIRC) sentinel which use hiredis for this.

What's more important is accept that we do for each client, but we already use accept4() when it's available (setting options at the same time as accepting).

src/anet.c Outdated Show resolved Hide resolved
src/anet.c Outdated Show resolved Hide resolved
src/anet.c Show resolved Hide resolved
Signed-off-by: Andy Pan <i@andypan.me>
Copy link

codecov bot commented Apr 25, 2024

Codecov Report

Attention: Patch coverage is 70.83333% with 7 lines in your changes are missing coverage. Please review.

❗ No coverage uploaded for pull request base (unstable@f4f1bd6). Click here to learn what that means.

Additional details and impacted files
@@             Coverage Diff             @@
##             unstable     #293   +/-   ##
===========================================
  Coverage            ?   68.44%           
===========================================
  Files               ?      108           
  Lines               ?    61558           
  Branches            ?        0           
===========================================
  Hits                ?    42136           
  Misses              ?    19422           
  Partials            ?        0           
Files Coverage Δ
src/unix.c 74.32% <ø> (ø)
src/anet.c 70.41% <70.83%> (ø)

@panjf2000
Copy link
Contributor Author

It looks good in general. Just a few small suggestions.

I'm not sure it has a very big impact though. It's only used for creating the listening socket and for creating outgoing connections, which is basically only for replication and cluster nodes (maybe something more?) but we don't use it for valkey-cli, valkey-benchmark and (IIRC) sentinel which use hiredis for this.

What's more important is accept that we do for each client, but we already use accept4() when it's available (setting options at the same time as accepting).

Speaking of which, check out #294 as well.

@zuiderkwast zuiderkwast added the to-be-merged Almost ready to merge label Apr 25, 2024
@panjf2000
Copy link
Contributor Author

Any updates?

@zuiderkwast zuiderkwast changed the title Use SOCK_NONBLOCK to reduce system calls from enabling non-blocking sockets Use SOCK_NONBLOCK to reduce system calls for outgoing connections Apr 30, 2024
@zuiderkwast zuiderkwast merged commit 948cd8f into valkey-io:unstable Apr 30, 2024
17 checks passed
@zuiderkwast
Copy link
Contributor

Any updates?

I left it open for a week to let others have time to review. The to-be-merged label makes sure we don't forget it.

@panjf2000 panjf2000 deleted the socket-opt branch April 30, 2024 09:52
@zuiderkwast zuiderkwast removed the to-be-merged Almost ready to merge label May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants