代码拉取完成,页面将自动刷新
From f132c0c51e362909081cf0b156ebe093000aa82b Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Tue, 10 Dec 2024 17:12:18 +0800
Subject: [PATCH] fix rpc pool leak, when thread exits
---
src/lstack/core/lstack_thread_rpc.c | 56 +++++++++++++++++++++++------
1 file changed, 45 insertions(+), 11 deletions(-)
diff --git a/src/lstack/core/lstack_thread_rpc.c b/src/lstack/core/lstack_thread_rpc.c
index 26bd16a..d342af4 100644
--- a/src/lstack/core/lstack_thread_rpc.c
+++ b/src/lstack/core/lstack_thread_rpc.c
@@ -24,6 +24,15 @@
#include "lstack_epoll.h"
#include "lstack_lwip.h"
+struct rpc_pool_array {
+#define RPC_POOL_MAX_COUNT 1024
+ struct rpc_msg_pool *array[RPC_POOL_MAX_COUNT];
+ pthread_mutex_t lock;
+ int cur_count;
+};
+
+static struct rpc_pool_array g_rpc_pool_array;
+
static PER_THREAD struct rpc_msg_pool *g_rpc_pool = NULL;
static struct rpc_stats g_rpc_stats;
@@ -32,6 +41,13 @@ struct rpc_stats *rpc_stats_get(void)
return &g_rpc_stats;
}
+static inline void rpc_pool_array_add(struct rpc_msg_pool *pool)
+{
+ pthread_mutex_lock(&g_rpc_pool_array.lock);
+ g_rpc_pool_array.array[g_rpc_pool_array.cur_count++] = pool;
+ pthread_mutex_unlock(&g_rpc_pool_array.lock);
+}
+
__rte_always_inline
static struct rpc_msg *get_rpc_msg(struct rpc_msg_pool *rpc_pool)
{
@@ -54,23 +70,41 @@ static void rpc_msg_init(struct rpc_msg *msg, rpc_func_t func, struct rpc_msg_po
pthread_spin_init(&msg->lock, PTHREAD_PROCESS_PRIVATE);
}
+static struct rpc_msg_pool *rpc_msg_pool_init(void)
+{
+ struct rpc_msg_pool *rpc_pool;
+ if (g_rpc_pool_array.cur_count >= RPC_POOL_MAX_COUNT) {
+ return g_rpc_pool_array.array[rte_gettid() % RPC_POOL_MAX_COUNT];
+ }
+
+ rpc_pool = calloc(1, sizeof(struct rpc_msg_pool));
+ if (rpc_pool == NULL) {
+ LSTACK_LOG(INFO, LSTACK, "g_rpc_pool calloc failed\n");
+ goto END;
+ }
+ rpc_pool->mempool =
+ create_mempool("rpc_pool", get_global_cfg_params()->rpc_msg_max, sizeof(struct rpc_msg), 0, rte_gettid());
+ if (rpc_pool->mempool == NULL) {
+ LSTACK_LOG(INFO, LSTACK, "rpc_pool create failed, errno is %d\n", errno);
+ free(rpc_pool);
+ goto END;
+ }
+
+ rpc_pool_array_add(rpc_pool);
+ return rpc_pool;
+END:
+ g_rpc_stats.call_alloc_fail++;
+ return NULL;
+}
+
+
static struct rpc_msg *rpc_msg_alloc(rpc_func_t func)
{
struct rpc_msg *msg;
if (unlikely(g_rpc_pool == NULL)) {
- g_rpc_pool = calloc(1, sizeof(struct rpc_msg_pool));
+ g_rpc_pool = rpc_msg_pool_init();
if (g_rpc_pool == NULL) {
- LSTACK_LOG(INFO, LSTACK, "g_rpc_pool calloc failed\n");
- g_rpc_stats.call_alloc_fail++;
- exit(-1);
- }
-
- g_rpc_pool->mempool =
- create_mempool("rpc_pool", get_global_cfg_params()->rpc_msg_max, sizeof(struct rpc_msg), 0, rte_gettid());
- if (g_rpc_pool->mempool == NULL) {
- LSTACK_LOG(INFO, LSTACK, "rpc_pool create failed, errno is %d\n", errno);
- g_rpc_stats.call_alloc_fail++;
exit(-1);
}
}
--
2.33.0
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。