Skip to content

Commit

Permalink
Add extra options in photon::init
Browse files Browse the repository at this point in the history
Signed-off-by: Coldwings <coldwings@me.com>
  • Loading branch information
Coldwings committed Jan 10, 2024
1 parent 5b92ecc commit 406fc9b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions io/aio-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion io/aio-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions photon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;

Expand All @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion photon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();


}
12 changes: 5 additions & 7 deletions thread/test/test-pooled-stack-allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,31 @@ 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);
LOG_TEMP("Spent ` us", spend);
}

TEST(Normal, ThreadPool) {
photon::set_photon_thread_stack_allocator();
photon::init();
DEFER(photon::fini());
auto spend = do_test(64);
LOG_TEMP("Spent ` us", spend);
}

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);
Expand Down

0 comments on commit 406fc9b

Please sign in to comment.