From 926cd40114e37a3f08d47795aaf2aac092b4591d Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sun, 12 Oct 2025 10:24:35 +0800 Subject: [PATCH] ioevent_init: set max entries for io_uring gracefully --- src/sf_global.c | 2 ++ src/sf_service.c | 35 ++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/sf_global.c b/src/sf_global.c index 9f5c174..f6a3aa1 100644 --- a/src/sf_global.c +++ b/src/sf_global.c @@ -907,6 +907,7 @@ static const char *get_address_family_caption( } } +#if IOEVENT_USE_URING static void get_io_uring_configs(const SFContext *sf_context, bool *use_io_uring, bool *use_send_zc) { @@ -929,6 +930,7 @@ static void get_io_uring_configs(const SFContext *sf_context, } } } +#endif void sf_context_config_to_string(const SFContext *sf_context, char *output, const int size) diff --git a/src/sf_service.c b/src/sf_service.c index f8ce26d..23de1b8 100644 --- a/src/sf_service.c +++ b/src/sf_service.c @@ -108,6 +108,7 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name, int result; int bytes; int extra_events; + int max_entries; int i; struct worker_thread_context *thread_contexts; struct worker_thread_context *thread_ctx; @@ -200,15 +201,35 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name, thread_data->arg = NULL; } - if (ioevent_init(&thread_data->ev_puller, sf_context->name, - 2 + sf_context->net_buffer_cfg.max_connections, - net_timeout_ms, extra_events) != 0) +#if IOEVENT_USE_URING + if (sf_context->net_buffer_cfg.max_connections < 16 * 1024) { + max_entries = 2 * sf_context->net_buffer_cfg.max_connections; + } else { + max_entries = sf_context->net_buffer_cfg.max_connections; + } +#else + max_entries = 2 + sf_context->net_buffer_cfg.max_connections; +#endif + if ((result=ioevent_init(&thread_data->ev_puller, sf_context->name, + max_entries, net_timeout_ms, extra_events)) != 0) { - result = errno != 0 ? errno : ENOMEM; + char prompt[256]; +#if IOEVENT_USE_URING + if (result == EPERM) { + strcpy(prompt, " make sure kernel.io_uring_disabled set to 0"); + } else if (result == EINVAL) { + sprintf(prompt, " maybe max_connections: %d is too large", + sf_context->net_buffer_cfg.max_connections); + } else { + *prompt = '\0'; + } +#else + *prompt = '\0'; +#endif + logError("file: "__FILE__", line: %d, " - "ioevent_init fail, " - "errno: %d, error info: %s", - __LINE__, result, strerror(result)); + "ioevent_init fail, errno: %d, error info: %s.%s" + , __LINE__, result, strerror(result), prompt); return result; }