From 406fc9b93cd42594f4866a8effd7f94a2515c81f Mon Sep 17 00:00:00 2001 From: Coldwings Date: Wed, 10 Jan 2024 15:55:59 +0800 Subject: [PATCH] Add extra options in `photon::init` Signed-off-by: Coldwings --- io/aio-wrapper.cpp | 4 ++-- io/aio-wrapper.h | 2 +- photon.cpp | 16 +++++++++++++--- photon.h | 10 +++++++++- thread/test/test-pooled-stack-allocator.cpp | 12 +++++------- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/io/aio-wrapper.cpp b/io/aio-wrapper.cpp index 2ea2f2c2..6a33c6ec 100644 --- a/io/aio-wrapper.cpp +++ b/io/aio-wrapper.cpp @@ -366,7 +366,7 @@ namespace photon }; static thread_local AioResetHandle *reset_handler = nullptr; - int libaio_wrapper_init() + int libaio_wrapper_init(int iodepth) { if (libaio_ctx) return 0; @@ -376,7 +376,7 @@ namespace photon if (ctx->evfd < 0) LOG_ERRNO_RETURN(0, -1, "failed to create eventfd"); - int ret = io_setup(IODEPTH, &ctx->aio_ctx); + int ret = io_setup(iodepth < IODEPTH ? iodepth : IODEPTH, &ctx->aio_ctx); if (ret < 0) { LOG_ERROR("failed to create aio context by io_setup() ", ERRNO(), VALUE(ret)); diff --git a/io/aio-wrapper.h b/io/aio-wrapper.h index 762bc78b..1f0e660f 100644 --- a/io/aio-wrapper.h +++ b/io/aio-wrapper.h @@ -24,7 +24,7 @@ namespace photon { extern "C" { - int libaio_wrapper_init(); + int libaio_wrapper_init(int iodepth = 2048); int libaio_wrapper_fini(); // `fd` must be opened with O_DIRECT, and the buffers must be aligned diff --git a/photon.cpp b/photon.cpp index 3d1b67a0..331538c8 100644 --- a/photon.cpp +++ b/photon.cpp @@ -19,6 +19,9 @@ limitations under the License. #include "io/fd-events.h" #include "io/signal.h" #include "io/aio-wrapper.h" +#include "thread/thread.h" +#include "thread/thread-pool.h" +#include "thread/stack-allocator.h" #ifdef ENABLE_FSTACK_DPDK #include "io/fstack-dpdk.h" #endif @@ -35,7 +38,7 @@ using namespace net; static bool reset_handle_registed = false; static thread_local uint64_t g_event_engine = 0, g_io_engine = 0; -#define INIT_IO(name, prefix) if (INIT_IO_##name & io_engine) { if (prefix##_init() < 0) return -1; } +#define INIT_IO(name, prefix, ...) if (INIT_IO_##name & io_engine) { if (prefix##_init(__VA_ARGS__) < 0) return -1; } #define FINI_IO(name, prefix) if (INIT_IO_##name & g_io_engine) { prefix##_fini(); } // Try to init master engine with the recommended order @@ -45,7 +48,14 @@ static const int recommended_order[] = {INIT_EVENT_EPOLL, INIT_EVENT_IOURING, IN static const int recommended_order[] = {INIT_EVENT_KQUEUE, INIT_EVENT_SELECT}; #endif -int init(uint64_t event_engine, uint64_t io_engine) { +int init(uint64_t event_engine, uint64_t io_engine, const PhotonOptions& options) { + if (options.use_pooled_stack_allocator) { + use_pooled_stack_allocator(); + } + if (options.bypass_threadpool) { + set_bypass_threadpool(options.bypass_threadpool); + } + if (vcpu_init() < 0) return -1; @@ -71,7 +81,7 @@ int init(uint64_t event_engine, uint64_t io_engine) { INIT_IO(EXPORTFS, exportfs) INIT_IO(LIBCURL, libcurl) #ifdef __linux__ - INIT_IO(LIBAIO, libaio_wrapper) + INIT_IO(LIBAIO, libaio_wrapper, options.libaio_ctx) INIT_IO(SOCKET_EDGE_TRIGGER, et_poller) #endif g_event_engine = event_engine; diff --git a/photon.h b/photon.h index 418e9a1e..440841cb 100644 --- a/photon.h +++ b/photon.h @@ -48,17 +48,25 @@ const uint64_t INIT_IO_DEFAULT = INIT_IO_LIBCURL; #undef SHIFT +struct PhotonOptions { + int libaio_ctx = 2048; + bool use_pooled_stack_allocator = false; + bool bypass_threadpool = false; +}; + /** * @brief Initialize the main photon thread and ancillary threads by flags. * Ancillary threads will be running in background. * @return 0 for success */ int init(uint64_t event_engine = INIT_EVENT_DEFAULT, - uint64_t io_engine = INIT_IO_DEFAULT); + uint64_t io_engine = INIT_IO_DEFAULT, + const PhotonOptions& options = PhotonOptions()); /** * @brief Destroy/join ancillary threads, and finish the main thread. */ int fini(); + } \ No newline at end of file diff --git a/thread/test/test-pooled-stack-allocator.cpp b/thread/test/test-pooled-stack-allocator.cpp index 86155233..cc1789ee 100644 --- a/thread/test/test-pooled-stack-allocator.cpp +++ b/thread/test/test-pooled-stack-allocator.cpp @@ -37,7 +37,6 @@ uint64_t do_test(int mode) { } TEST(Normal, NoPool) { - photon::set_photon_thread_stack_allocator(); photon::init(); DEFER(photon::fini()); auto spend = do_test(0); @@ -45,7 +44,6 @@ TEST(Normal, NoPool) { } TEST(Normal, ThreadPool) { - photon::set_photon_thread_stack_allocator(); photon::init(); DEFER(photon::fini()); auto spend = do_test(64); @@ -53,17 +51,17 @@ TEST(Normal, ThreadPool) { } TEST(PooledAllocator, PooledStack) { - photon::use_pooled_stack_allocator(); - photon::init(); + photon::init(photon::INIT_EVENT_DEFAULT, photon::INIT_IO_DEFAULT, + {.use_pooled_stack_allocator = true}); DEFER(photon::fini()); auto spend = do_test(0); LOG_TEMP("Spent ` us", spend); } TEST(PooledAllocator, BypassThreadPool) { - photon::use_pooled_stack_allocator(); - photon::set_bypass_threadpool(); - photon::init(); + photon::init( + photon::INIT_EVENT_DEFAULT, photon::INIT_IO_DEFAULT, + {.use_pooled_stack_allocator = true, .bypass_threadpool = true}); DEFER(photon::fini()); auto spend = do_test(64); LOG_TEMP("Spent ` us", spend);