Skip to content

Commit

Permalink
Optimize the compilation detection script for io_uring, fix #5653
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jan 7, 2025
1 parent 016bec5 commit 58c870f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 32 deletions.
73 changes: 47 additions & 26 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,42 @@ AC_DEFUN([AC_SWOOLE_HAVE_BOOST_STACKTRACE],
AC_LANG_POP([C++])
])

AC_DEFUN([AC_SWOOLE_HAVE_IOURING_FUTEX],
[
AC_MSG_CHECKING([for io_uring futex])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#define _GNU_SOURCE
#include <liburing.h>
]], [[
int op = IORING_OP_FUTEX_WAIT;
]])],[
AC_DEFINE([HAVE_IOURING_FUTEX], 1, [have io_uring futex?])
AC_MSG_RESULT([yes])
],[
AC_MSG_RESULT([no])
])
])

AC_DEFUN([AC_SWOOLE_HAVE_IOURING_STATX],
[
AC_MSG_CHECKING([for io_uring statx])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#define _GNU_SOURCE
#include <sys/stat.h>
#include <string.h>
#include <liburing.h>
]], [[
struct statx _statxbuf;
memset(&_statxbuf, 0, sizeof(_statxbuf));
int op = IORING_OP_STATX;
]])],[
AC_DEFINE([HAVE_IOURING_STATX], 1, [have io_uring statx?])
AC_MSG_RESULT([yes])
],[
AC_MSG_RESULT([no])
])
])

AC_DEFUN([AC_SWOOLE_CHECK_SOCKETS], [
AC_CHECK_FUNCS([hstrerror socketpair if_nametoindex if_indextoname])
AC_CHECK_HEADERS([netdb.h netinet/tcp.h sys/un.h sys/sockio.h])
Expand Down Expand Up @@ -615,6 +651,7 @@ EOF
dnl odbc end

dnl SWOOLE_ORACLE start

if test -z "$SED"; then
SWOOLE_PDO_OCI_SED="sed";
else
Expand Down Expand Up @@ -674,9 +711,9 @@ EOF
PHP_ARG_WITH([swoole-oracle],
[whether to enable oracle build flags],
[AS_HELP_STRING([[--with-swoole-oracle[=DIR]]],
[PDO: Oracle OCI support. DIR defaults to $ORACLE_HOME. Use
["PDO: Oracle OCI support. DIR defaults to ${ORACLE_HOME}. Use
--with-swoole-oracle=instantclient,/path/to/instant/client/lib for an Oracle
Instant Client installation.])], [no], [no])
Instant Client installation."])], [no], [no])

if test "$PHP_SWOOLE_ORACLE" != "no"; then
if test "$PHP_PDO" = "no" && test "$ext_shared" = "no"; then
Expand Down Expand Up @@ -814,7 +851,7 @@ EOF

dnl sqlite start
PHP_ARG_ENABLE([swoole-sqlite],
[for sqlite 3 support for PDO],
["for sqlite 3 support for PDO"],
[AS_HELP_STRING([--enable-swoole-sqlite],
[PDO: sqlite 3 support.])], [no], [no])

Expand Down Expand Up @@ -962,35 +999,19 @@ EOF
CFLAGS="-Wall -pthread $CFLAGS"
LDFLAGS="$LDFLAGS -lpthread"

dnl Check should we link to librt

if test "$PHP_IOURING" = "yes" && test "$SW_OS" = "LINUX"; then
PKG_CHECK_MODULES([URING], [liburing >= 2.5])
PKG_CHECK_MODULES([URING], [liburing >= 2.0])

AC_SWOOLE_HAVE_IOURING_STATX
AC_SWOOLE_HAVE_IOURING_FUTEX

PHP_EVAL_LIBLINE($URING_LIBS, SWOOLE_SHARED_LIBADD)
PHP_EVAL_INCLINE($URING_CFLAGS)
AC_DEFINE(SW_USE_IOURING, 1, [have io_uring])

LINUX_VERSION=`uname -r | cut -d '-' -f 1`
LINUX_MAJOR_VERSION=`echo $LINUX_VERSION | cut -d '.' -f 1`
LINUX_MINOR_VERSION=`echo $LINUX_VERSION | cut -d '.' -f 2`

_PKG_CONFIG(URING_VERSION, [modversion], [liburing])
IOURING_MAJOR_VERSION=`echo $pkg_cv_URING_VERSION | cut -d '.' -f 1`
IOURING_MINOR_VERSION=`echo $pkg_cv_URING_VERSION | cut -d '.' -f 2`

AC_MSG_CHECKING([checking for io_uring futex feature])
if test $IOURING_MAJOR_VERSION -gt 2 || (test $IOURING_MAJOR_VERSION -eq 2 && test $IOURING_MINOR_VERSION -ge 6); then
if test $LINUX_MAJOR_VERSION -gt 6 || (test $LINUX_MAJOR_VERSION -eq 6 && test $LINUX_MINOR_VERSION -ge 7); then
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_IOURING_FUTEX, 1, [have io_uring futex feature])
else
AC_MSG_RESULT([no, Linux $LINUX_VERSION is too old])
fi
else
AC_MSG_RESULT([no, liburing $IOURING_MAJOR_VERSION.$IOURING_MINOR_VERSION is too old])
fi
fi

dnl Check should we link to librt

if test "$SW_OS" = "LINUX"; then
GLIBC_VERSION=$(getconf GNU_LIBC_VERSION | awk '{print $2}')
AC_MSG_NOTICE([glibc version: $GLIBC_VERSION])
Expand Down
2 changes: 2 additions & 0 deletions include/swoole_coroutine_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ ssize_t swoole_coroutine_iouring_write(int sockfd, const void *buf, size_t count
int swoole_coroutine_iouring_rename(const char *oldpath, const char *newpath);
int swoole_coroutine_iouring_mkdir(const char *pathname, mode_t mode);
int swoole_coroutine_iouring_unlink(const char *pathname);
#ifdef HAVE_IOURING_STATX
int swoole_coroutine_iouring_fstat(int fd, struct stat *statbuf);
int swoole_coroutine_iouring_stat(const char *path, struct stat *statbuf);
int swoole_coroutine_iouring_lstat(const char *path, struct stat *statbuf);
#endif
int swoole_coroutine_iouring_rmdir(const char *pathname);
int swoole_coroutine_iouring_fsync(int fd);
int swoole_coroutine_iouring_fdatasync(int fd);
Expand Down
16 changes: 10 additions & 6 deletions include/swoole_file_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
#define rename(oldpath, newpath) swoole_coroutine_iouring_rename(oldpath, newpath)
#define mkdir(pathname, mode) swoole_coroutine_iouring_mkdir(pathname, mode)
#define unlink(pathname) swoole_coroutine_iouring_unlink(pathname)
#define fstat(fd, statbuf) swoole_coroutine_iouring_fstat(fd, statbuf)
#define stat(path, statbuf) swoole_coroutine_iouring_stat(path, statbuf)
#define lstat(path, statbuf) swoole_coroutine_iouring_lstat(path, statbuf)
#define rmdir(pathname) swoole_coroutine_iouring_rmdir(pathname)
#define fsync(fd) swoole_coroutine_iouring_fsync(fd)
#define fdatasync(fd) swoole_coroutine_iouring_fdatasync(fd)
Expand All @@ -39,9 +36,6 @@
#define read(fd, buf, count) swoole_coroutine_read(fd, buf, count)
#define write(fd, buf, count) swoole_coroutine_write(fd, buf, count)
#define lseek(fd, offset, whence) swoole_coroutine_lseek(fd, offset, whence)
#define fstat(fd, statbuf) swoole_coroutine_fstat(fd, statbuf)
#define stat(path, statbuf) swoole_coroutine_stat(path, statbuf)
#define lstat(path, statbuf) swoole_coroutine_lstat(path, statbuf)
#define readlink(fd, buf, size) swoole_coroutine_readlink(fd, buf, size)
#define unlink(pathname) swoole_coroutine_unlink(pathname)
#define mkdir(pathname, mode) swoole_coroutine_mkdir(pathname, mode)
Expand All @@ -51,6 +45,16 @@
#define fdatasync(fd) swoole_coroutine_fdatasync(fd)
#endif

#ifdef HAVE_IOURING_STATX
#define fstat(fd, statbuf) swoole_coroutine_iouring_fstat(fd, statbuf)
#define stat(path, statbuf) swoole_coroutine_iouring_stat(path, statbuf)
#define lstat(path, statbuf) swoole_coroutine_iouring_lstat(path, statbuf)
#else
#define fstat(fd, statbuf) swoole_coroutine_fstat(fd, statbuf)
#define stat(path, statbuf) swoole_coroutine_stat(path, statbuf)
#define lstat(path, statbuf) swoole_coroutine_lstat(path, statbuf)
#endif

#define access(pathname, mode) swoole_coroutine_access(pathname, mode)
#define fopen(pathname, mode) swoole_coroutine_fopen(pathname, mode)
#define fdopen(fd, mode) swoole_coroutine_fdopen(fd, mode)
Expand Down
2 changes: 2 additions & 0 deletions include/swoole_iouring.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ class Iouring {
static ssize_t rename(const char *oldpath, const char *newpath);
static int mkdir(const char *pathname, mode_t mode);
static int unlink(const char *pathname);
#ifdef HAVE_IOURING_STATX
static int fstat(int fd, struct stat *statbuf);
static int stat(const char *path, struct stat *statbuf);
#endif
static int rmdir(const char *pathname);
static int fsync(int fd);
static int fdatasync(int fd);
Expand Down
9 changes: 9 additions & 0 deletions scripts/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ if [ -n "$__HAVE_ZTS__" ]; then
COMPILE_PARAMS="$COMPILE_PARAMS --enable-swoole-thread"
fi

if [ "$(uname)" = "Linux" ]; then
COMPILE_PARAMS="$COMPILE_PARAMS --enable-iouring"
fi

if [ "$(uname | grep -i darwin)"x != ""x ]; then
CPU_COUNT="$(sysctl -n machdep.cpu.core_count)"
else
Expand Down Expand Up @@ -84,13 +88,18 @@ if [ "$1" = "help" ] ;then
fi

phpize

if [ "$1" = "debug" ] ;then
./configure ${COMPILE_PARAMS} --enable-debug-log
elif [ "$1" = "trace" ] ;then
./configure ${COMPILE_PARAMS} --enable-trace-log
elif [ "$1" = "config" ] ;then
./configure ${COMPILE_PARAMS}
exit 0
else
./configure ${COMPILE_PARAMS}
fi

make clean
make -j ${CPU_COUNT}
make install
2 changes: 2 additions & 0 deletions src/coroutine/hook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ int swoole_coroutine_iouring_unlink(const char *pathname) {
return Iouring::unlink(pathname);
}

#ifdef HAVE_IOURING_STATX
int swoole_coroutine_iouring_fstat(int fd, struct stat *statbuf) {
if (sw_unlikely(is_no_coro())) {
return fstat(fd, statbuf);
Expand All @@ -683,6 +684,7 @@ int swoole_coroutine_iouring_lstat(const char *path, struct stat *statbuf) {
// Iouring cannot distinguish between lstat and stat; these two operations are the same
return Iouring::stat(path, statbuf);
}
#endif

int swoole_coroutine_iouring_rmdir(const char *pathname) {
if (sw_unlikely(is_no_coro())) {
Expand Down
2 changes: 2 additions & 0 deletions src/coroutine/iouring.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ int Iouring::fdatasync(int fd) {
return execute(&event);
}

#ifdef HAVE_IOURING_STATX
static void swoole_statx_to_stat(const struct statx *statxbuf, struct stat *statbuf) {
statbuf->st_dev = (((unsigned int) statxbuf->stx_dev_major) << 8) | (unsigned int) statxbuf->stx_dev_minor;
statbuf->st_mode = statxbuf->stx_mode;
Expand Down Expand Up @@ -500,6 +501,7 @@ int Iouring::stat(const char *path, struct stat *statbuf) {
}
return retval;
}
#endif

#ifdef HAVE_IOURING_FUTEX
int Iouring::futex_wait(uint32_t *futex) {
Expand Down

0 comments on commit 58c870f

Please sign in to comment.