unix: accept back-off patch causes regression in tcp_multi_accept2 #624

Closed
bnoordhuis opened this Issue Nov 14, 2012 · 3 comments

Comments

Projects
None yet
1 participant
Contributor

bnoordhuis commented Nov 14, 2012

$ UV_TCP_SINGLE_ACCEPT=0 out/Release/run-benchmarks tcp_multi_accept2 tcp_multi_accept2
accept2: 20267 accepts/sec (250000 total)
  thread #0: 14902 accepts/sec (183816 total, 73.5%)
  thread #1: 5363 accepts/sec (66158 total, 26.5%)
$ UV_TCP_SINGLE_ACCEPT=1 out/Release/run-benchmarks tcp_multi_accept2 tcp_multi_accept2
# aeons pass...
^C

When accept back-off is disabled, the test maxes out two or three cores. When it's enabled, CPU usage hovers at 7% max - and usually much less.

Happens with both the master and the rm-ev branch. (Not applicable to v0.8, it doesn't contain the patch.)

bnoordhuis was assigned Nov 14, 2012

Contributor

bnoordhuis commented Nov 14, 2012

Okay, it happens because of this code in src/unix/stream.c:

    if (stream->type == UV_TCP && (stream->flags & UV_TCP_SINGLE_ACCEPT)) {
      /* Give other processes a chance to accept connections. */
      struct timespec timeout = { 0, 1 };
      nanosleep(&timeout, NULL);
    }

That nanosleep() works great when there are three or more processes but it's actively harmful when there are only two...

/cc @piscisaureus - ideas?

Contributor

bnoordhuis commented Dec 8, 2012

Turns out that setting SO_LINGER to 1 second fixes the issue. Why it's only an issue with tcp_multi_accept2 and not the other tests remains to be investigated...

diff --git a/src/unix/core.c b/src/unix/core.c
index ece9673..6bbf621 100644
--- a/src/unix/core.c
+++ b/src/unix/core.c
@@ -338,6 +338,11 @@ int uv__socket(int domain, int type, int protocol) {
 #endif

 out:
+  if (sockfd != -1 && domain == AF_INET && type == SOCK_STREAM) {
+    struct linger linger = { 1, 1 };
+    setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
+  }
+
   return sockfd;
 }

bnoordhuis referenced this issue in nodejs/node-v0.x-archive Dec 12, 2012

Closed

Improve cluster load balancing #3241

Contributor

bnoordhuis commented Feb 25, 2013

Closing. Accept back-off a.k.a. relaxed accept has been disabled in dc559a5.

bnoordhuis closed this Feb 25, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment