SO_BUSY_POLL for latency critical cases

Donatas Abraitis donatas.abraitis at gmail.com
Thu Feb 2 07:40:56 UTC 2017


Hi,

I do not cover any tests, just putting this patchset and wanna ask if
someone had tried this? I tried this for Redis and changes are really
reasonably visible like here: https://github.com/antirez/redis/pull/3773

# HG changeset patch
# User Donatas Abraitis <donatas.abraitis at gmail.com>
# Date 1485982537 -7200
#      Wed Feb 01 22:55:37 2017 +0200
# Node ID aa29306b9ff2ef6a72919d7cc8ace72e3dd3a3aa
# Parent  d2b2ff157da53260b2b1c414792100ff0cd1377d
Set SO_BUSY_POLL option for socket if specified

diff -r d2b2ff157da5 -r aa29306b9ff2 src/core/ngx_connection.c
--- a/src/core/ngx_connection.c    Tue Jan 31 21:19:58 2017 +0300
+++ b/src/core/ngx_connection.c    Wed Feb 01 22:55:37 2017 +0200
@@ -497,6 +497,25 @@
             }
 #endif

+        if (ls[i].busypoll) {
+            if (setsockopt(s, SOL_SOCKET, SO_BUSY_POLL,
+                   &ls[i].busypoll, sizeof(ls[i].busypoll))
+            == -1)
+        {
+            ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
+                  "setsockopt(SO_BUSY_POLL) %V failed, ignored",
+                  &ls[i].addr_text);
+
+            if (ngx_close_socket(s) == -1) {
+                ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
+                      ngx_close_socket_n " %V failed",
+                      &ls[i].addr_text);
+            }
+
+            return NGX_ERROR;
+        }
+        }
+
 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)

             if (ls[i].sockaddr->sa_family == AF_INET6) {
diff -r d2b2ff157da5 -r aa29306b9ff2 src/core/ngx_connection.h
--- a/src/core/ngx_connection.h    Tue Jan 31 21:19:58 2017 +0300
+++ b/src/core/ngx_connection.h    Wed Feb 01 22:55:37 2017 +0200
@@ -70,6 +70,7 @@
     unsigned            ipv6only:1;
 #endif
     unsigned            reuseport:1;
+    int                 busypoll;
     unsigned            add_reuseport:1;
     unsigned            keepalive:2;

diff -r d2b2ff157da5 -r aa29306b9ff2 src/http/ngx_http.c
--- a/src/http/ngx_http.c    Tue Jan 31 21:19:58 2017 +0300
+++ b/src/http/ngx_http.c    Wed Feb 01 22:55:37 2017 +0200
@@ -1741,6 +1741,7 @@
 #endif

     ls->backlog = addr->opt.backlog;
+    ls->busypoll = addr->opt.busypoll;
     ls->rcvbuf = addr->opt.rcvbuf;
     ls->sndbuf = addr->opt.sndbuf;

diff -r d2b2ff157da5 -r aa29306b9ff2 src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c    Tue Jan 31 21:19:58 2017 +0300
+++ b/src/http/ngx_http_core_module.c    Wed Feb 01 22:55:37 2017 +0200
@@ -2984,6 +2984,7 @@
         lsopt.socklen = sizeof(struct sockaddr_in);

         lsopt.backlog = NGX_LISTEN_BACKLOG;
+    lsopt.busypoll = 0;
         lsopt.rcvbuf = -1;
         lsopt.sndbuf = -1;
 #if (NGX_HAVE_SETFIB)
@@ -3946,6 +3947,7 @@

     lsopt.socklen = u.socklen;
     lsopt.backlog = NGX_LISTEN_BACKLOG;
+    lsopt.busypoll = 0;
     lsopt.rcvbuf = -1;
     lsopt.sndbuf = -1;
 #if (NGX_HAVE_SETFIB)
@@ -4009,6 +4011,19 @@
         }
 #endif

+    if (ngx_strncmp(value[n].data, "busypoll=", 9) == 0) {
+        lsopt.busypoll = ngx_atoi(value[n].data + 9, value[n].len - 9);
+        lsopt.set = 1;
+        lsopt.bind = 1;
+
+        if (lsopt.busypoll == NGX_ERROR || lsopt.busypoll < 0) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                   "invalid busypoll value \"%V\"", &value[n]);
+        return NGX_CONF_ERROR;
+        }
+        continue;
+    }
+
         if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) {
             lsopt.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8);
             lsopt.set = 1;
diff -r d2b2ff157da5 -r aa29306b9ff2 src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h    Tue Jan 31 21:19:58 2017 +0300
+++ b/src/http/ngx_http_core_module.h    Wed Feb 01 22:55:37 2017 +0200
@@ -83,6 +83,7 @@
     unsigned                   proxy_protocol:1;

     int                        backlog;
+    int                        busypoll;
     int                        rcvbuf;
     int                        sndbuf;
 #if (NGX_HAVE_SETFIB)

Waiting for comments,
Donatas.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20170202/08c19d4b/attachment.html>


More information about the nginx mailing list