From 49716c97e9244e8962486af81318a6e0adf92c30 Mon Sep 17 00:00:00 2001 From: hantianfeng Date: Fri, 24 May 2024 19:53:46 +0800 Subject: [PATCH] revert iouring --- config.m4 | 12 - ext-src/php_swoole.cc | 3 - ext-src/php_swoole_private.h | 4 - ext-src/swoole_async_coro.cc | 6 - include/swoole.h | 13 -- include/swoole_async.h | 106 --------- include/swoole_coroutine.h | 16 -- include/swoole_coroutine_c_api.h | 19 -- include/swoole_file_hook.h | 16 -- scripts/make.sh | 1 - src/coroutine/iouring.cc | 158 ------------- src/coroutine/system.cc | 93 -------- src/os/iouring.cc | 376 ------------------------------- 13 files changed, 823 deletions(-) delete mode 100644 src/coroutine/iouring.cc delete mode 100644 src/os/iouring.cc diff --git a/config.m4 b/config.m4 index b7db6b93ea6..9458be30016 100644 --- a/config.m4 +++ b/config.m4 @@ -56,11 +56,6 @@ PHP_ARG_ENABLE([cares], [AS_HELP_STRING([--enable-cares], [Enable cares])], [no], [no]) -PHP_ARG_ENABLE([iouring], - [enable io-uring support], - [AS_HELP_STRING([--enable-iouring], - [Enable io-uring (Experimental)])], [no], [no]) - PHP_ARG_WITH([openssl_dir], [dir of openssl], [AS_HELP_STRING([[--with-openssl-dir[=DIR]]], @@ -903,13 +898,6 @@ EOF PHP_ADD_LIBRARY(cares, 1, SWOOLE_SHARED_LIBADD) fi - if test "$PHP_IOURING" = "yes"; then - AC_CHECK_LIB(uring, io_uring_queue_init, [ - AC_DEFINE(SW_USE_IOURING, 1, [have io_uring]) - PHP_ADD_LIBRARY(uring, 1, SWOOLE_SHARED_LIBADD) - ]) - fi - AC_SWOOLE_CPU_AFFINITY AC_SWOOLE_HAVE_REUSEPORT AC_SWOOLE_HAVE_FUTEX diff --git a/ext-src/php_swoole.cc b/ext-src/php_swoole.cc index 17046b423cf..b313f6cd58f 100644 --- a/ext-src/php_swoole.cc +++ b/ext-src/php_swoole.cc @@ -906,9 +906,6 @@ PHP_MINFO_FUNCTION(swoole) { #endif #ifdef SW_USE_SQLITE php_info_print_table_row(2, "coroutine_sqlite", "enabled"); -#endif -#ifdef SW_USE_IOURING - php_info_print_table_row(2, "io_uring", "enabled"); #endif php_info_print_table_end(); diff --git a/ext-src/php_swoole_private.h b/ext-src/php_swoole_private.h index 2b05b58b885..c762021e964 100644 --- a/ext-src/php_swoole_private.h +++ b/ext-src/php_swoole_private.h @@ -108,10 +108,6 @@ extern PHPAPI int php_array_merge(zend_array *dest, zend_array *src); #error "thread context cannot be used with ZTS" #endif -#if defined(SW_USE_IOURING) && !defined(__linux__) -#error "only linux support iouring" -#endif - //-------------------------------------------------------- #define SW_MAX_FIND_COUNT 100 // for swoole_server::connection_list #define SW_PHP_CLIENT_BUFFER_SIZE 65535 diff --git a/ext-src/swoole_async_coro.cc b/ext-src/swoole_async_coro.cc index 98466be5aba..af7240ad6be 100644 --- a/ext-src/swoole_async_coro.cc +++ b/ext-src/swoole_async_coro.cc @@ -60,12 +60,6 @@ void php_swoole_set_aio_option(HashTable *vht) { if (php_swoole_array_get_value(vht, "aio_max_idle_time", ztmp)) { SwooleG.aio_max_idle_time = zval_get_double(ztmp); } -#if defined(__linux__) && defined(SW_USE_IOURING) - if (php_swoole_array_get_value(vht, "iouring_entries", ztmp)) { - zend_long v = zval_get_long(ztmp); - SwooleG.iouring_entries = SW_MAX(0, SW_MIN(v, UINT32_MAX)); - } -#endif } PHP_FUNCTION(swoole_async_set) { diff --git a/include/swoole.h b/include/swoole.h index ee12b283ab3..0d3aa2100bc 100644 --- a/include/swoole.h +++ b/include/swoole.h @@ -61,10 +61,6 @@ #include #include -#ifdef SW_USE_IOURING -#include -#endif - typedef unsigned long ulong_t; #ifndef PRId64 @@ -204,9 +200,6 @@ struct Socket; struct Address; } // namespace network class AsyncThreads; -#ifdef SW_USE_IOURING -class AsyncIouring; -#endif namespace async { class ThreadPool; } @@ -667,9 +660,6 @@ struct ThreadGlobal { Reactor *reactor; Timer *timer; AsyncThreads *async_threads; -#ifdef SW_USE_IOURING - AsyncIouring *async_iouring; -#endif uint32_t signal_listener_num; uint32_t co_signal_listener_num; int error; @@ -750,9 +740,6 @@ struct Global { //-----------------------[AIO]-------------------------- uint32_t aio_core_worker_num; uint32_t aio_worker_num; -#ifdef SW_USE_IOURING - uint32_t iouring_entries; -#endif double aio_max_wait_time; double aio_max_idle_time; network::Socket *aio_default_socket; diff --git a/include/swoole_async.h b/include/swoole_async.h index 7f16bc1d0a0..f659fddf280 100644 --- a/include/swoole_async.h +++ b/include/swoole_async.h @@ -24,11 +24,6 @@ #include #include -#ifdef SW_USE_IOURING -#include "linux/version.h" -#include -#endif - #ifndef O_DIRECT #define O_DIRECT 040000 #endif @@ -42,32 +37,16 @@ enum AsyncFlag { struct AsyncEvent { size_t task_id; -#ifdef SW_USE_IOURING - size_t count; -#endif uint8_t canceled; int error; /** * input & output */ void *data; -#ifdef SW_USE_IOURING - const char *pathname; - const char *pathname2; - struct statx *statxbuf; - void *rbuf; - const void *wbuf; -#endif /** * output */ ssize_t retval; -#ifdef SW_USE_IOURING - int fd; - int flags; - int opcode; - mode_t mode; -#endif /** * internal use only */ @@ -124,91 +103,6 @@ class AsyncThreads { std::mutex init_lock; }; -#ifdef SW_USE_IOURING -class AsyncIouring { - private: - int ring_fd; - uint64_t task_num = 0; - uint64_t entries = 8192; - struct io_uring ring; - std::queue waitEvents; - network::Socket *iou_socket = nullptr; - Reactor *reactor = nullptr; - - inline struct io_uring_sqe *get_iouring_sqe() { - struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); - // We need to reset the values of each sqe structure so that they can be used in a loop. - if (sqe) { - memset(sqe, 0, sizeof(struct io_uring_sqe)); - } - return sqe; - } - - inline void set_iouring_sqe_data(struct io_uring_sqe *sqe, void *data) { - io_uring_sqe_set_data(sqe, data); - } - - inline void *get_iouring_cqe_data(struct io_uring_cqe *cqe) { - return io_uring_cqe_get_data(cqe); - } - - inline int get_iouring_cqes(struct io_uring_cqe **cqe_ptr, unsigned count) { - return io_uring_peek_batch_cqe(&ring, cqe_ptr, count); - } - - inline void finish_iouring_cqes(unsigned count) { - io_uring_cq_advance(&ring, count); - } - - inline bool submit_iouring_sqe() { - return io_uring_submit(&ring); - } - - public: - AsyncIouring(Reactor *reactor_); - ~AsyncIouring(); - - enum opcodes { - SW_IORING_OP_OPENAT = IORING_OP_OPENAT, - SW_IORING_OP_CLOSE = IORING_OP_CLOSE, - SW_IORING_OP_STATX = IORING_OP_STATX, - SW_IORING_OP_READ = IORING_OP_READ, - SW_IORING_OP_WRITE = IORING_OP_WRITE, - SW_IORING_OP_RENAMEAT = IORING_OP_RENAMEAT, - SW_IORING_OP_UNLINKAT = IORING_OP_UNLINKAT, - SW_IORING_OP_MKDIRAT = IORING_OP_MKDIRAT, - - SW_IORING_OP_FSTAT = 1000, - SW_IORING_OP_LSTAT = 1001, - SW_IORING_OP_UNLINK_FILE = 1002, - SW_IORING_OP_UNLINK_DIR = 1003, - SW_IORING_OP_FSYNC = 1004, - SW_IORING_OP_FDATASYNC = 1005, - }; - - void add_event(); - void delete_event(); - bool wakeup(); - bool open(AsyncEvent *event); - bool close(AsyncEvent *event); - bool wr(AsyncEvent *event); - bool statx(AsyncEvent *event); - bool mkdir(AsyncEvent *event); - bool unlink(AsyncEvent *event); - bool rename(AsyncEvent *event); - bool fsync(AsyncEvent *event); - inline bool is_empty_wait_events() { - return waitEvents.size() == 0; - } - - inline uint64_t get_task_num() { - return task_num; - } - - static int callback(Reactor *reactor, Event *event); -}; -#endif - namespace async { typedef void (*Handler)(AsyncEvent *event); diff --git a/include/swoole_coroutine.h b/include/swoole_coroutine.h index f83453005f7..a773f233010 100644 --- a/include/swoole_coroutine.h +++ b/include/swoole_coroutine.h @@ -304,22 +304,6 @@ class Coroutine { namespace coroutine { bool async(async::Handler handler, AsyncEvent &event, double timeout = -1); bool async(const std::function &fn, double timeout = -1); -#ifdef SW_USE_IOURING -int async(AsyncIouring::opcodes opcode, - const char *pathname, - const char *pathname2 = nullptr, - mode_t mode = 0, - int flags = 0, - struct statx *statxbuf = nullptr, - double timeout = -1); -int async(AsyncIouring::opcodes opcode, - int fd, - void *rbuf = nullptr, - const void *wbuf = nullptr, - struct statx *statxbuf = nullptr, - size_t count = 0, - double timeout = -1); -#endif bool run(const CoroutineFunc &fn, void *arg = nullptr); } // namespace coroutine //------------------------------------------------------------------------------- diff --git a/include/swoole_coroutine_c_api.h b/include/swoole_coroutine_c_api.h index 5655e7f20e1..ab22867eb2c 100644 --- a/include/swoole_coroutine_c_api.h +++ b/include/swoole_coroutine_c_api.h @@ -65,25 +65,6 @@ int swoole_coroutine_statvfs(const char *path, struct statvfs *buf); int swoole_coroutine_close_file(int fd); int swoole_coroutine_fsync(int fd); int swoole_coroutine_fdatasync(int fd); -/** - * io_uring - */ -#ifdef SW_USE_IOURING -int swoole_coroutine_iouring_open(const char *pathname, int flags, mode_t mode); -int swoole_coroutine_iouring_close_file(int fd); -ssize_t swoole_coroutine_iouring_read(int sockfd, void *buf, size_t count); -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); -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); -int swoole_coroutine_iouring_rmdir(const char *pathname); -int swoole_coroutine_iouring_fsync(int fd); -int swoole_coroutine_iouring_fdatasync(int fd); -void swoole_statx_to_stat(const struct statx *statxbuf, struct stat *statbuf); -#endif /** * stdio */ diff --git a/include/swoole_file_hook.h b/include/swoole_file_hook.h index 5dd5d5f072c..d67e8d8e528 100644 --- a/include/swoole_file_hook.h +++ b/include/swoole_file_hook.h @@ -19,21 +19,6 @@ #include "swoole_coroutine_c_api.h" -#ifdef SW_USE_IOURING -#define open(pathname, flags, mode) swoole_coroutine_iouring_open(pathname, flags, mode) -#define close_file(fd) swoole_coroutine_iouring_close_file(fd) -#define read(fd, buf, count) swoole_coroutine_iouring_read(fd, buf, count) -#define write(fd, buf, count) swoole_coroutine_iouring_write(fd, buf, count) -#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) -#else #define open(pathname, flags, mode) swoole_coroutine_open(pathname, flags, mode) #define close_file(fd) swoole_coroutine_close_file(fd) #define read(fd, buf, count) swoole_coroutine_read(fd, buf, count) @@ -49,7 +34,6 @@ #define rename(oldpath, newpath) swoole_coroutine_rename(oldpath, newpath) #define fsync(fd) swoole_coroutine_fsync(fd) #define fdatasync(fd) swoole_coroutine_fdatasync(fd) -#endif #define access(pathname, mode) swoole_coroutine_access(pathname, mode) #define fopen(pathname, mode) swoole_coroutine_fopen(pathname, mode) diff --git a/scripts/make.sh b/scripts/make.sh index d72285c3617..5fcc751ab35 100755 --- a/scripts/make.sh +++ b/scripts/make.sh @@ -7,7 +7,6 @@ COMPILE_PARAMS="--enable-openssl \ --enable-swoole-curl \ --enable-cares \ --enable-swoole-pgsql \ ---enable-iouring \ --with-swoole-odbc=unixODBC,/usr \ --enable-swoole-sqlite" diff --git a/src/coroutine/iouring.cc b/src/coroutine/iouring.cc deleted file mode 100644 index f1de793e0ec..00000000000 --- a/src/coroutine/iouring.cc +++ /dev/null @@ -1,158 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | Swoole | - +----------------------------------------------------------------------+ - | This source file is subject to version 2.0 of the Apache license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.apache.org/licenses/LICENSE-2.0.html | - | If you did not receive a copy of the Apache2.0 license and are unable| - | to obtain it through the world-wide-web, please send a note to | - | license@swoole.com so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: NathanFreeman | - +----------------------------------------------------------------------+ - */ -#include -#include -#include - -#include "swoole_coroutine_socket.h" -#include "swoole_coroutine_system.h" - -#ifdef SW_USE_IOURING -using swoole::AsyncIouring; -using swoole::Coroutine; -using swoole::coroutine::async; - -static sw_inline bool is_no_coro() { - return SwooleTG.reactor == nullptr || !Coroutine::get_current(); -} - -int swoole_coroutine_iouring_open(const char *pathname, int flags, mode_t mode) { - if (sw_unlikely(is_no_coro())) { - return open(pathname, flags, mode); - } - return async(AsyncIouring::SW_IORING_OP_OPENAT, pathname, nullptr, mode, flags); -} - -int swoole_coroutine_iouring_close_file(int fd) { - if (sw_unlikely(is_no_coro())) { - return close(fd); - } - return async(AsyncIouring::SW_IORING_OP_CLOSE, fd); -} - -ssize_t swoole_coroutine_iouring_read(int sockfd, void *buf, size_t count) { - if (sw_unlikely(is_no_coro())) { - return read(sockfd, buf, count); - } - - return async(AsyncIouring::SW_IORING_OP_READ, sockfd, buf, nullptr, nullptr, count); -} - -ssize_t swoole_coroutine_iouring_write(int sockfd, const void *buf, size_t count) { - if (sw_unlikely(is_no_coro())) { - return write(sockfd, buf, count); - } - - return async(AsyncIouring::SW_IORING_OP_WRITE, sockfd, nullptr, buf, nullptr, count);; -} - -int swoole_coroutine_iouring_rename(const char *oldpath, const char *newpath) { - if (sw_unlikely(is_no_coro())) { - return rename(oldpath, newpath); - } - return async(AsyncIouring::SW_IORING_OP_RENAMEAT, oldpath, newpath); -} - -int swoole_coroutine_iouring_mkdir(const char *pathname, mode_t mode) { - if (sw_unlikely(is_no_coro())) { - return mkdir(pathname, mode); - } - return async(AsyncIouring::SW_IORING_OP_MKDIRAT, pathname, nullptr, mode); -} - -int swoole_coroutine_iouring_unlink(const char *pathname) { - if (sw_unlikely(is_no_coro())) { - return unlink(pathname); - } - return async(AsyncIouring::SW_IORING_OP_UNLINK_FILE, pathname); -} - -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; - statbuf->st_nlink = statxbuf->stx_nlink; - statbuf->st_uid = statxbuf->stx_uid; - statbuf->st_gid = statxbuf->stx_gid; - statbuf->st_rdev = (((unsigned int) statxbuf->stx_rdev_major) << 8) | (unsigned int) statxbuf->stx_rdev_minor; - statbuf->st_ino = statxbuf->stx_ino; - statbuf->st_size = statxbuf->stx_size; - statbuf->st_blksize = statxbuf->stx_blksize; - statbuf->st_blocks = statxbuf->stx_blocks; - statbuf->st_atim.tv_sec = statxbuf->stx_atime.tv_sec; - statbuf->st_atim.tv_nsec = statxbuf->stx_atime.tv_nsec; - statbuf->st_mtim.tv_sec = statxbuf->stx_mtime.tv_sec; - statbuf->st_mtim.tv_nsec = statxbuf->stx_mtime.tv_nsec; - statbuf->st_ctim.tv_sec = statxbuf->stx_ctime.tv_sec; - statbuf->st_ctim.tv_nsec = statxbuf->stx_ctime.tv_nsec; -} - -int swoole_coroutine_iouring_fstat(int fd, struct stat *statbuf) { - if (sw_unlikely(is_no_coro())) { - return fstat(fd, statbuf); - } - - struct statx statxbuf = {}; - int retval = async(AsyncIouring::SW_IORING_OP_FSTAT, fd, nullptr, nullptr, &statxbuf); - swoole_statx_to_stat(&statxbuf, statbuf); - return retval; -} - -int swoole_coroutine_iouring_stat(const char *path, struct stat *statbuf) { - if (sw_unlikely(is_no_coro())) { - return stat(path, statbuf); - } - - struct statx statxbuf = {}; - int retval = async(AsyncIouring::SW_IORING_OP_LSTAT, path, nullptr, 0, 0, &statxbuf); - swoole_statx_to_stat(&statxbuf, statbuf); - return retval; -} - -int swoole_coroutine_iouring_lstat(const char *path, struct stat *statbuf) { - if (sw_unlikely(is_no_coro())) { - return lstat(path, statbuf); - } - - struct statx statxbuf = {}; - int retval = async(AsyncIouring::SW_IORING_OP_LSTAT, path, nullptr, 0, 0, &statxbuf); - swoole_statx_to_stat(&statxbuf, statbuf); - return retval; -} - -int swoole_coroutine_iouring_rmdir(const char *pathname) { - if (sw_unlikely(is_no_coro())) { - return rmdir(pathname); - } - - return async(AsyncIouring::SW_IORING_OP_UNLINK_DIR, pathname); -} - -int swoole_coroutine_iouring_fsync(int fd) { - if (sw_unlikely(is_no_coro())) { - return fsync(fd); - } - - return async(AsyncIouring::SW_IORING_OP_FSYNC, fd); -} - -int swoole_coroutine_iouring_fdatasync(int fd) { - if (sw_unlikely(is_no_coro())) { - return fdatasync(fd); - } - - return async(AsyncIouring::SW_IORING_OP_FDATASYNC, fd); -} -#endif diff --git a/src/coroutine/system.cc b/src/coroutine/system.cc index c108d971943..001d66592f3 100644 --- a/src/coroutine/system.cc +++ b/src/coroutine/system.cc @@ -614,9 +614,6 @@ void System::init_reactor(Reactor *reactor) { reactor->set_handler(SW_FD_CO_EVENT | SW_EVENT_ERROR, event_waiter_error_callback); reactor->set_handler(SW_FD_AIO | SW_EVENT_READ, AsyncThreads::callback); -#ifdef SW_USE_IOURING - reactor->set_handler(SW_FD_IOURING | SW_EVENT_READ, AsyncIouring::callback); -#endif } static void async_task_completed(AsyncEvent *event) { @@ -698,96 +695,6 @@ bool async(const std::function &fn, double timeout) { } } -#ifdef SW_USE_IOURING -int async(AsyncIouring::opcodes opcode, - const char *pathname, - const char *pathname2, - mode_t mode, - int flags, - struct statx *statxbuf, - double timeout) { - if (SwooleTG.async_iouring == nullptr) { - SwooleTG.async_iouring = new AsyncIouring(SwooleTG.reactor); - SwooleTG.async_iouring->add_event(); - } - - AsyncEvent event{}; - AsyncLambdaTask task{Coroutine::get_current_safe(), nullptr}; - - event.object = &task; - event.callback = async_lambda_callback; - event.opcode = opcode; - event.pathname = pathname; - event.pathname2 = pathname2; - event.mode = mode; - event.flags = flags; - event.statxbuf = statxbuf; - - bool result = false; - AsyncIouring *iouring = SwooleTG.async_iouring; - if (opcode == AsyncIouring::SW_IORING_OP_OPENAT) { - result = iouring->open(&event); - } else if (opcode == AsyncIouring::SW_IORING_OP_MKDIRAT) { - result = iouring->mkdir(&event); - } else if (opcode == AsyncIouring::SW_IORING_OP_UNLINK_FILE || opcode == AsyncIouring::SW_IORING_OP_UNLINK_DIR) { - result = iouring->unlink(&event); - } else if (opcode == AsyncIouring::SW_IORING_OP_RENAMEAT) { - result = iouring->rename(&event); - } else if (opcode == AsyncIouring::SW_IORING_OP_FSTAT || opcode == AsyncIouring::SW_IORING_OP_LSTAT) { - result = iouring->statx(&event); - } - - if (!result || !task.co->yield_ex(timeout)) { - return 0; - } - - return event.retval; -} - -int async(AsyncIouring::opcodes opcode, - int fd, - void *rbuf, - const void *wbuf, - struct statx *statxbuf, - size_t count, - double timeout) { - if (SwooleTG.async_iouring == nullptr) { - SwooleTG.async_iouring = new AsyncIouring(SwooleTG.reactor); - SwooleTG.async_iouring->add_event(); - } - - AsyncEvent event{}; - AsyncLambdaTask task{Coroutine::get_current_safe(), nullptr}; - - event.object = &task; - event.callback = async_lambda_callback; - event.opcode = opcode; - event.fd = fd; - event.rbuf = rbuf; - event.wbuf = wbuf; - event.statxbuf = statxbuf; - event.count = count; - - bool result = false; - AsyncIouring *iouring = SwooleTG.async_iouring; - if (opcode == AsyncIouring::SW_IORING_OP_READ || opcode == AsyncIouring::SW_IORING_OP_WRITE) { - result = iouring->wr(&event); - } else if (opcode == AsyncIouring::SW_IORING_OP_CLOSE) { - result = iouring->close(&event); - } else if (opcode == AsyncIouring::SW_IORING_OP_FSTAT) { - result = iouring->statx(&event); - } else if (opcode == AsyncIouring::SW_IORING_OP_FSYNC || opcode == AsyncIouring::SW_IORING_OP_FDATASYNC) { - result = iouring->fsync(&event); - } - - if (!result || !task.co->yield_ex(timeout)) { - return 0; - } - - return event.retval; -} -#endif - AsyncLock::AsyncLock(void *resource) { resource_ = resource; async_resource_map.emplace(resource, Coroutine::get_current_cid()); diff --git a/src/os/iouring.cc b/src/os/iouring.cc deleted file mode 100644 index eb806f3cdfb..00000000000 --- a/src/os/iouring.cc +++ /dev/null @@ -1,376 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | Swoole | - +----------------------------------------------------------------------+ - | This source file is subject to version 2.0 of the Apache license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.apache.org/licenses/LICENSE-2.0.html | - | If you did not receive a copy of the Apache2.0 license and are unable| - | to obtain it through the world-wide-web, please send a note to | - | license@swoole.com so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: Tianfeng Han | - +----------------------------------------------------------------------+ - */ - -#include "swoole.h" -#include "swoole_api.h" -#include "swoole_socket.h" -#include "swoole_reactor.h" -#include "swoole_string.h" -#include "swoole_signal.h" -#include "swoole_pipe.h" -#include "swoole_async.h" -#include "swoole_util.h" -#include "swoole_coroutine.h" - -#ifdef SW_USE_IOURING -namespace swoole { -//------------------------------------------------------------------------------- -AsyncIouring::AsyncIouring(Reactor *reactor_) { - if (!SwooleTG.reactor) { - swoole_warning("no event loop, cannot initialized"); - throw swoole::Exception(SW_ERROR_WRONG_OPERATION); - } - - reactor = reactor_; - if (SwooleG.iouring_entries > 0) { - uint32_t i = 6; - while ((1U << i) < SwooleG.iouring_entries) { - i++; - } - entries = 1 << i; - } - -#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) - int ret = io_uring_queue_init(entries, &ring, IORING_SETUP_COOP_TASKRUN | IORING_SETUP_SUBMIT_ALL); -#elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) - int ret = io_uring_queue_init( - entries, &ring, IORING_SETUP_COOP_TASKRUN | IORING_SETUP_SUBMIT_ALL | IORING_SETUP_SINGLE_ISSUER); -#else - int ret = io_uring_queue_init(entries, &ring, 0); -#endif - - if (ret < 0) { - swoole_warning("create io_uring failed"); - throw swoole::Exception(SW_ERROR_WRONG_OPERATION); - return; - } - ring_fd = ring.ring_fd; - - iou_socket = make_socket(ring_fd, SW_FD_IOURING); - if (!iou_socket) { - swoole_sys_warning("create io_uring socket failed"); - return; - } - - reactor->set_exit_condition(Reactor::EXIT_CONDITION_IOURING, [](Reactor *reactor, size_t &event_num) -> bool { - if (SwooleTG.async_iouring && SwooleTG.async_iouring->get_task_num() == 0 && - SwooleTG.async_iouring->is_empty_wait_events()) { - event_num--; - } - return true; - }); - - reactor->add_destroy_callback([](void *data) { - if (!SwooleTG.async_iouring) { - return; - } - SwooleTG.async_iouring->delete_event(); - delete SwooleTG.async_iouring; - SwooleTG.async_iouring = nullptr; - }); -} - -AsyncIouring::~AsyncIouring() { - if (ring_fd >= 0) { - ::close(ring_fd); - } - - if (iou_socket) { - delete iou_socket; - } - - io_uring_queue_exit(&ring); -} - -void AsyncIouring::add_event() { - reactor->add(iou_socket, SW_EVENT_READ); -} - -void AsyncIouring::delete_event() { - reactor->del(iou_socket); -} - -bool AsyncIouring::wakeup() { - unsigned num = 8192; - struct io_uring_cqe *cqes[num]; - size_t cqes_size = num * sizeof(struct io_uring_cqe *); - unsigned count = 0; - - unsigned i = 0; - void *data = nullptr; - AsyncEvent *task = nullptr; - struct io_uring_cqe *cqe = nullptr; - AsyncEvent *waitEvent = nullptr; - - while (true) { - memset(cqes, 0, cqes_size); - count = get_iouring_cqes(cqes, num); - if (count == 0) { - return true; - } - - for (i = 0; i < count; i++) { - cqe = cqes[i]; - data = get_iouring_cqe_data(cqe); - task = reinterpret_cast(data); - task->retval = (cqe->res >= 0 ? cqe->res : -1); - if (cqe->res < 0) { - errno = abs(cqe->res); - } - - task_num--; - - if (is_empty_wait_events()) { - task->callback(task); - continue; - } - - waitEvent = waitEvents.front(); - waitEvents.pop(); - if (waitEvent->opcode == AsyncIouring::SW_IORING_OP_OPENAT) { - open(waitEvent); - } else if (waitEvent->opcode == AsyncIouring::SW_IORING_OP_CLOSE) { - close(waitEvent); - } else if (waitEvent->opcode == AsyncIouring::SW_IORING_OP_FSTAT || - waitEvent->opcode == AsyncIouring::SW_IORING_OP_LSTAT) { - statx(waitEvent); - } else if (waitEvent->opcode == AsyncIouring::SW_IORING_OP_READ || - waitEvent->opcode == AsyncIouring::SW_IORING_OP_WRITE) { - wr(waitEvent); - } else if (waitEvent->opcode == AsyncIouring::SW_IORING_OP_RENAMEAT) { - rename(waitEvent); - } else if (waitEvent->opcode == AsyncIouring::SW_IORING_OP_UNLINK_FILE || - waitEvent->opcode == AsyncIouring::SW_IORING_OP_UNLINK_DIR) { - unlink(waitEvent); - } else if (waitEvent->opcode == AsyncIouring::SW_IORING_OP_MKDIRAT) { - mkdir(waitEvent); - } else if (waitEvent->opcode == AsyncIouring::SW_IORING_OP_FSYNC || - waitEvent->opcode == AsyncIouring::SW_IORING_OP_FDATASYNC) { - fsync(waitEvent); - } - - task->callback(task); - } - finish_iouring_cqes(count); - } -} - -bool AsyncIouring::open(AsyncEvent *event) { - struct io_uring_sqe *sqe = get_iouring_sqe(); - if (!sqe) { - waitEvents.push(event); - return true; - } - - set_iouring_sqe_data(sqe, (void *) event); - sqe->addr = (uintptr_t) event->pathname; - sqe->fd = AT_FDCWD; - sqe->len = event->mode; - sqe->opcode = SW_IORING_OP_OPENAT; - sqe->open_flags = event->flags | O_CLOEXEC; - - bool result = submit_iouring_sqe(); - - if (!result) { - return false; - } - - task_num++; - return true; -} - -bool AsyncIouring::close(AsyncEvent *event) { - struct io_uring_sqe *sqe = get_iouring_sqe(); - if (!sqe) { - waitEvents.push(event); - return true; - } - - set_iouring_sqe_data(sqe, (void *) event); - sqe->fd = event->fd; - sqe->opcode = SW_IORING_OP_CLOSE; - - bool result = submit_iouring_sqe(); - - if (!result) { - return false; - } - - task_num++; - return true; -} - -bool AsyncIouring::wr(AsyncEvent *event) { - struct io_uring_sqe *sqe = get_iouring_sqe(); - if (!sqe) { - waitEvents.push(event); - return true; - } - - set_iouring_sqe_data(sqe, (void *) event); - sqe->fd = event->fd; - sqe->addr = event->opcode == SW_IORING_OP_READ ? (uintptr_t) event->rbuf : (uintptr_t) event->wbuf; - sqe->len = event->count; - sqe->off = -1; - sqe->opcode = event->opcode; - - bool result = submit_iouring_sqe(); - - if (!result) { - return false; - } - - task_num++; - return true; -} - -bool AsyncIouring::statx(AsyncEvent *event) { - struct io_uring_sqe *sqe = get_iouring_sqe(); - if (!sqe) { - waitEvents.push(event); - return true; - } - - set_iouring_sqe_data(sqe, (void *) event); - if (event->opcode == SW_IORING_OP_FSTAT) { - sqe->addr = (uintptr_t) ""; - sqe->fd = event->fd; - sqe->statx_flags |= AT_EMPTY_PATH; - } else { - sqe->addr = (uintptr_t) event->pathname; - sqe->fd = AT_FDCWD; - sqe->statx_flags |= AT_SYMLINK_NOFOLLOW; - } - // sqe->len = 0xFFF; - sqe->opcode = SW_IORING_OP_STATX; - sqe->off = (uintptr_t) event->statxbuf; - - bool result = submit_iouring_sqe(); - - if (!result) { - return false; - } - - task_num++; - return true; -} - -bool AsyncIouring::mkdir(AsyncEvent *event) { - struct io_uring_sqe *sqe = get_iouring_sqe(); - if (!sqe) { - waitEvents.push(event); - return true; - } - - set_iouring_sqe_data(sqe, (void *) event); - sqe->addr = (uintptr_t) event->pathname; - sqe->fd = AT_FDCWD; - sqe->len = event->mode; - sqe->opcode = SW_IORING_OP_MKDIRAT; - bool result = submit_iouring_sqe(); - - if (!result) { - return false; - } - - task_num++; - return true; -} - -bool AsyncIouring::unlink(AsyncEvent *event) { - struct io_uring_sqe *sqe = get_iouring_sqe(); - if (!sqe) { - waitEvents.push(event); - return true; - } - - set_iouring_sqe_data(sqe, (void *) event); - - sqe->addr = (uintptr_t) event->pathname; - sqe->fd = AT_FDCWD; - sqe->opcode = SW_IORING_OP_UNLINKAT; - if (event->opcode == SW_IORING_OP_UNLINK_DIR) { - sqe->unlink_flags |= AT_REMOVEDIR; - } - bool result = submit_iouring_sqe(); - - if (!result) { - return false; - } - - task_num++; - return true; -} - -bool AsyncIouring::rename(AsyncEvent *event) { - struct io_uring_sqe *sqe = get_iouring_sqe(); - if (!sqe) { - waitEvents.push(event); - return true; - } - - set_iouring_sqe_data(sqe, (void *) event); - - sqe->addr = (uintptr_t) event->pathname; - sqe->addr2 = (uintptr_t) event->pathname2; - sqe->fd = AT_FDCWD; - sqe->len = AT_FDCWD; - sqe->opcode = SW_IORING_OP_RENAMEAT; - bool result = submit_iouring_sqe(); - - if (!result) { - return false; - } - - task_num++; - return true; -} - -bool AsyncIouring::fsync(AsyncEvent *event) { - struct io_uring_sqe *sqe = get_iouring_sqe(); - if (!sqe) { - waitEvents.push(event); - return true; - } - - set_iouring_sqe_data(sqe, (void *) event); - sqe->fd = event->fd; - sqe->addr = (unsigned long) nullptr; - sqe->opcode = IORING_OP_FSYNC; - sqe->len = 0; - sqe->off = 0; - sqe->fsync_flags = 0; - - if (event->opcode == SW_IORING_OP_FDATASYNC) { - sqe->fsync_flags = IORING_FSYNC_DATASYNC; - } - - bool result = submit_iouring_sqe(); - - if (!result) { - return false; - } - - task_num++; - return true; -} - -int AsyncIouring::callback(Reactor *reactor, Event *event) { - AsyncIouring *iouring = SwooleTG.async_iouring; - return iouring->wakeup() ? 1 : 0; -} -} // namespace swoole -#endif