From eabb909877ef5f4c7159fb67c3b5a1b2061c017e Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 2 Jan 2014 19:10:16 +0100 Subject: OPTIM: add support for accept4() This one saves one call to fcntl(). --- Makefile | 4 ++-- httpterm.c | 33 ++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 4e4d7c0..c7002fb 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,8 @@ CPU = generic CC = gcc LD = gcc -# This is for recent Linux 2.6 with splice -COPTS.linux26s = -DENABLE_POLL -DENABLE_EPOLL -DENABLE_SPLICE +# This is for recent Linux 2.6 with splice and accept4() +COPTS.linux26s = -DENABLE_POLL -DENABLE_EPOLL -DENABLE_SPLICE -DENABLE_ACCEPT4 LIBS.linux26s = # This is for standard Linux 2.6 with epoll() diff --git a/httpterm.c b/httpterm.c index f13d615..af87f11 100644 --- a/httpterm.c +++ b/httpterm.c @@ -37,6 +37,10 @@ * */ +#ifdef ENABLE_ACCEPT4 +#define _GNU_SOURCE +#endif + #include #include #include @@ -1892,6 +1896,12 @@ int event_accept(int fd) { int cfd; int max_accept; +#ifdef ENABLE_ACCEPT4 + static int use_accept = 0; +#else + static int use_accept = 1; +#endif + if (global.nbproc > 1) max_accept = 8; /* let other processes catch some connections too */ else @@ -1901,7 +1911,20 @@ int event_accept(int fd) { struct sockaddr_storage addr; socklen_t laddr = sizeof(addr); - if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { +#ifdef ENABLE_ACCEPT4 + if (!use_accept) { + cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK); + if (cfd == -1 && errno == ENOSYS) + use_accept = 1; + } +#endif + if (use_accept) { + cfd = accept(fd, (struct sockaddr *)&addr, &laddr); + if (cfd != -1) + fcntl(cfd, F_SETFL, O_NONBLOCK); + } + + if (cfd == -1) { switch (errno) { case EAGAIN: case EINTR: @@ -1945,14 +1968,6 @@ int event_accept(int fd) { return 0; } - if (fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) { - Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); - close(cfd); - pool_free(task, t); - pool_free(session, s); - return 0; - } - if (p->options & PR_O_TCP_CLI_KA) setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); -- 1.7.12.1