From 8c312c54ed5382776be7854c600bf6ce168d5223 Mon Sep 17 00:00:00 2001 From: Sean Bright <sean.bright@gmail.com> Date: Fri, 7 Dec 2018 07:57:48 -0500 Subject: [PATCH] utils: Wrap socket() and pipe() to reduce syscalls Some platforms provide an implementation of socket() and pipe2() that allow the caller to specify that the resulting file descriptors should be non-blocking. Using these allows us to potentially elide 3 calls into 1 by avoiding extraneous calls to fcntl() to set the O_NONBLOCK flag afterwards. In passing, change ast_alertpipe_init() to use pipe2() directly instead of the wrapper if it is available. Change-Id: I3ebe654fb549587537161506c6c950f4ab298bb0 --- configure | 75 ++++++++++++++++++++++++++++---- configure.ac | 25 ++++++++++- include/asterisk/autoconfig.h.in | 6 +++ include/asterisk/utils.h | 34 +++++++++++++++ main/alertpipe.c | 11 +++++ main/utils.c | 36 +++++++++++++++ 6 files changed, 177 insertions(+), 10 deletions(-) diff --git a/configure b/configure index 9eafe668db..1a3493cf77 100755 --- a/configure +++ b/configure @@ -1323,6 +1323,7 @@ infodir docdir oldincludedir includedir +runstatedir localstatedir sharedstatedir sysconfdir @@ -1509,6 +1510,7 @@ datadir='${datarootdir}' sysconfdir='${prefix}/etc' sharedstatedir='${prefix}/com' localstatedir='${prefix}/var' +runstatedir='${localstatedir}/run' includedir='${prefix}/include' oldincludedir='/usr/include' docdir='${datarootdir}/doc/${PACKAGE_TARNAME}' @@ -1761,6 +1763,15 @@ do | -silent | --silent | --silen | --sile | --sil) silent=yes ;; + -runstatedir | --runstatedir | --runstatedi | --runstated \ + | --runstate | --runstat | --runsta | --runst | --runs \ + | --run | --ru | --r) + ac_prev=runstatedir ;; + -runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \ + | --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \ + | --run=* | --ru=* | --r=*) + runstatedir=$ac_optarg ;; + -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb) ac_prev=sbindir ;; -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \ @@ -1898,7 +1909,7 @@ fi for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \ datadir sysconfdir sharedstatedir localstatedir includedir \ oldincludedir docdir infodir htmldir dvidir pdfdir psdir \ - libdir localedir mandir + libdir localedir mandir runstatedir do eval ac_val=\$$ac_var # Remove trailing slashes. @@ -2051,6 +2062,7 @@ Fine tuning of the installation directories: --sysconfdir=DIR read-only single-machine data [PREFIX/etc] --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] --localstatedir=DIR modifiable single-machine data [PREFIX/var] + --runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run] --libdir=DIR object code libraries [EPREFIX/lib] --includedir=DIR C header files [PREFIX/include] --oldincludedir=DIR C header files for non-gcc [/usr/include] @@ -13275,7 +13287,7 @@ $as_echo "#define HAVE_SYS_WAIT_H 1" >>confdefs.h fi -for ac_header in arpa/inet.h fcntl.h inttypes.h libintl.h limits.h locale.h malloc.h netdb.h netinet/in.h stddef.h stdint.h stdlib.h string.h strings.h sys/event.h sys/file.h sys/ioctl.h sys/param.h sys/socket.h sys/time.h syslog.h termios.h unistd.h utime.h arpa/nameser.h sys/io.h +for ac_header in arpa/inet.h fcntl.h inttypes.h libintl.h limits.h locale.h malloc.h netdb.h netinet/in.h stddef.h stdint.h stdlib.h string.h strings.h sys/event.h sys/file.h sys/ioctl.h sys/param.h sys/socket.h sys/time.h sys/types.h syslog.h termios.h unistd.h utime.h arpa/nameser.h sys/io.h do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" @@ -15116,7 +15128,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15162,7 +15174,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15186,7 +15198,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15231,7 +15243,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15255,7 +15267,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -16555,6 +16567,8 @@ main () if (*(data + i) != *(data3 + i)) return 14; close (fd); + free (data); + free (data3); return 0; } _ACEOF @@ -17095,7 +17109,7 @@ fi done -for ac_func in asprintf atexit closefrom dup2 eaccess endpwent euidaccess ffsll ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday glob ioperm inet_ntoa isascii memchr memmove memset mkdir mkdtemp munmap newlocale ppoll putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtod strtol strtold strtoq unsetenv uselocale utime vasprintf getpeereid sysctl swapctl malloc_trim +for ac_func in asprintf atexit closefrom dup2 eaccess endpwent euidaccess ffsll ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday glob ioperm inet_ntoa isascii memchr memmove memset mkdir mkdtemp munmap newlocale pipe2 ppoll putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtod strtol strtold strtoq unsetenv uselocale utime vasprintf getpeereid sysctl swapctl malloc_trim do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" @@ -18154,6 +18168,51 @@ rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \ fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if socket() accepts SOCK_NONBLOCK" >&5 +$as_echo_n "checking if socket() accepts SOCK_NONBLOCK... " >&6; } +if test "$cross_compiling" = yes; then : + { $as_echo "$as_me:${as_lineno-$LINENO}: result: cross-compile" >&5 +$as_echo "cross-compile" >&6; } + +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + #ifdef HAVE_SYS_TYPES_H + # include <sys/types.h> + #endif + #ifdef HAVE_SYS_SOCKET_H + # include <sys/socket.h> + #endif + +int +main () +{ + + if (socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0) < 0) { + return 1; + } + return 0; + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_run "$LINENO"; then : + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + +$as_echo "#define HAVE_SOCK_NONBLOCK 1" >>confdefs.h + +else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } +fi +rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \ + conftest.$ac_objext conftest.beam conftest.$ac_ext +fi + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking if we can increase the maximum select-able file descriptor" >&5 $as_echo_n "checking if we can increase the maximum select-able file descriptor... " >&6; } if test "$cross_compiling" = yes; then : diff --git a/configure.ac b/configure.ac index 94a66336e5..3fcdced6f7 100644 --- a/configure.ac +++ b/configure.ac @@ -603,7 +603,7 @@ AC_FUNC_ALLOCA AC_HEADER_DIRENT AC_HEADER_STDC AC_HEADER_SYS_WAIT -AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h libintl.h limits.h locale.h malloc.h netdb.h netinet/in.h stddef.h stdint.h stdlib.h string.h strings.h sys/event.h sys/file.h sys/ioctl.h sys/param.h sys/socket.h sys/time.h syslog.h termios.h unistd.h utime.h arpa/nameser.h sys/io.h]) +AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h libintl.h limits.h locale.h malloc.h netdb.h netinet/in.h stddef.h stdint.h stdlib.h string.h strings.h sys/event.h sys/file.h sys/ioctl.h sys/param.h sys/socket.h sys/time.h sys/types.h syslog.h termios.h unistd.h utime.h arpa/nameser.h sys/io.h]) PKG_PROG_PKG_CONFIG() @@ -794,7 +794,7 @@ AC_FUNC_STRNLEN AC_FUNC_STRTOD AC_FUNC_UTIME_NULL AC_FUNC_VPRINTF -AC_CHECK_FUNCS([asprintf atexit closefrom dup2 eaccess endpwent euidaccess ffsll ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday glob ioperm inet_ntoa isascii memchr memmove memset mkdir mkdtemp munmap newlocale ppoll putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtod strtol strtold strtoq unsetenv uselocale utime vasprintf getpeereid sysctl swapctl malloc_trim]) +AC_CHECK_FUNCS([asprintf atexit closefrom dup2 eaccess endpwent euidaccess ffsll ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday glob ioperm inet_ntoa isascii memchr memmove memset mkdir mkdtemp munmap newlocale pipe2 ppoll putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtod strtol strtold strtoq unsetenv uselocale utime vasprintf getpeereid sysctl swapctl malloc_trim]) AC_MSG_CHECKING(for htonll) AC_LINK_IFELSE( @@ -1124,6 +1124,27 @@ AC_RUN_IFELSE( AC_MSG_RESULT(unknown) ) +AC_MSG_CHECKING([if socket() accepts SOCK_NONBLOCK]) +AC_RUN_IFELSE( +[AC_LANG_PROGRAM([ + #ifdef HAVE_SYS_TYPES_H + # include <sys/types.h> + #endif + #ifdef HAVE_SYS_SOCKET_H + # include <sys/socket.h> + #endif + ], [ + if (socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0) < 0) { + return 1; + } + return 0; + ])], + AC_MSG_RESULT(yes) + AC_DEFINE([HAVE_SOCK_NONBLOCK], 1, [Define to 1 if your socket() implementation can accept SOCK_NONBLOCK.]), + AC_MSG_RESULT(no), + AC_MSG_RESULT(cross-compile) +) + AC_MSG_CHECKING(if we can increase the maximum select-able file descriptor) AC_RUN_IFELSE( [AC_LANG_PROGRAM([ diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in index f401e57270..9925ef02a9 100644 --- a/include/asterisk/autoconfig.h.in +++ b/include/asterisk/autoconfig.h.in @@ -588,6 +588,9 @@ /* Define to indicate presence of the pg_encoding_to_char API. */ #undef HAVE_PGSQL_pg_encoding_to_char +/* Define to 1 if you have the `pipe2' function. */ +#undef HAVE_PIPE2 + /* Define if your system has the PJPROJECT libraries. */ #undef HAVE_PJPROJECT @@ -841,6 +844,9 @@ /* Define to 1 if you have the `socket' function. */ #undef HAVE_SOCKET +/* Define to 1 if your socket() implementation can accept SOCK_NONBLOCK. */ +#undef HAVE_SOCK_NONBLOCK + /* Define to 1 if your system has soxmix application. */ #undef HAVE_SOXMIX diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h index bcb5b24f3f..288bff1812 100644 --- a/include/asterisk/utils.h +++ b/include/asterisk/utils.h @@ -1195,6 +1195,40 @@ enum ast_fd_flag_operation { int __ast_fd_set_flags(int fd, int flags, enum ast_fd_flag_operation op, const char *file, int lineno, const char *function); +/*! + * \brief Create a non-blocking socket + * \since 13.25 + * + * Wrapper around socket(2) that sets the O_NONBLOCK flag on the resulting + * socket. + * + * \details + * For parameter and return information, see the man page for + * socket(2). + */ +#ifdef HAVE_SOCK_NONBLOCK +# define ast_socket_nonblock(domain, type, protocol) socket((domain), (type) | SOCK_NONBLOCK, (protocol)) +#else +int ast_socket_nonblock(int domain, int type, int protocol); +#endif + +/*! + * \brief Create a non-blocking pipe + * \since 13.25 + * + * Wrapper around pipe(2) that sets the O_NONBLOCK flag on the resulting + * file descriptors. + * + * \details + * For parameter and return information, see the man page for + * pipe(2). + */ +#ifdef HAVE_PIPE2 +# define ast_pipe_nonblock(filedes) pipe2((filedes), O_NONBLOCK) +#else +int ast_pipe_nonblock(int filedes[2]); +#endif + /*! * \brief Set the current thread's user interface status. * diff --git a/main/alertpipe.c b/main/alertpipe.c index 7932a73468..9ff399dfd6 100644 --- a/main/alertpipe.c +++ b/main/alertpipe.c @@ -51,6 +51,15 @@ int ast_alertpipe_init(int alert_pipe[2]) #endif +#ifdef HAVE_PIPE2 + + if (pipe2(alert_pipe, O_NONBLOCK)) { + ast_log(LOG_WARNING, "Failed to create alert pipe: %s\n", strerror(errno)); + return -1; + } + +#else + if (pipe(alert_pipe)) { ast_log(LOG_WARNING, "Failed to create alert pipe: %s\n", strerror(errno)); return -1; @@ -62,6 +71,8 @@ int ast_alertpipe_init(int alert_pipe[2]) } } +#endif + return 0; } diff --git a/main/utils.c b/main/utils.c index c82ecb9bed..915026745c 100644 --- a/main/utils.c +++ b/main/utils.c @@ -2833,6 +2833,42 @@ int __ast_fd_set_flags(int fd, int flags, enum ast_fd_flag_operation op, return 0; } +#ifndef HAVE_SOCK_NONBLOCK +int ast_socket_nonblock(int domain, int type, int protocol) +{ + int s = socket(domain, type, protocol); + if (s < 0) { + return -1; + } + + if (ast_fd_set_flags(s, O_NONBLOCK)) { + close(s); + return -1; + } + + return s; +} +#endif + +#ifndef HAVE_PIPE2 +int ast_pipe_nonblock(int filedes[2]) +{ + int p = pipe(filedes); + if (p < 0) { + return -1; + } + + if (ast_fd_set_flags(filedes[0], O_NONBLOCK) + || ast_fd_set_flags(filedes[1], O_NONBLOCK)) { + close(filedes[0]); + close(filedes[1]); + return -1; + } + + return 0; +} +#endif + /*! * \brief A thread local indicating whether the current thread is a user interface. */ -- GitLab