From: Weili Qian <qianweili@huawei.com> 1.In the original approach, using sched_getcpu() followed by numa_node_of_cpu() requires two system calls, resulting in low efficiency.By adopting the new getcpu() method, only one system call is needed, and in some cases, the information can even be directly obtained from process data without any system call. 2.Use getcpu() to directly obtain the node id,instead of first obtaining the cpu id and then the node id, to reduce the number of system calls. Signed-off-by: Longfang Liu <liulongfang@huawei.com> Signed-off-by: Weili Qian <qianweili@huawei.com> --- include/wd.h | 1 + wd.c | 11 ++++++++--- wd_bmm.c | 13 +++++++++---- wd_sched.c | 20 +++++++++++++------- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/include/wd.h b/include/wd.h index abc745d..7e92e41 100644 --- a/include/wd.h +++ b/include/wd.h @@ -31,6 +31,7 @@ extern "C" { #define WD_CTX_CNT_NUM 1024 #define WD_IPC_KEY 0x500011 #define CRYPTO_MAX_ALG_NAME 128 +#define NUMA_NO_NODE (-1) typedef unsigned char __u8; typedef unsigned int __u32; diff --git a/wd.c b/wd.c index bf83ca1..7f21dc0 100644 --- a/wd.c +++ b/wd.c @@ -828,19 +828,24 @@ struct uacce_dev *wd_get_accel_dev(const char *alg_name) { struct uacce_dev_list *list, *head; struct uacce_dev *dev = NULL, *target = NULL; - int cpu = sched_getcpu(); - int node = numa_node_of_cpu(cpu); + unsigned int node; int ctx_num, tmp; int dis = 1024; int max = 0; + /* Under default conditions in a VM, the node value is 0 */ + if (getcpu(NULL, &node) || node == (unsigned int)NUMA_NO_NODE) { + WD_ERR("invalid: failed to get numa node id for uacce device!\n"); + return NULL; + } + head = wd_get_accel_list(alg_name); if (!head) return NULL; list = head; while (list) { - tmp = numa_distance(node, list->dev->numa_id); + tmp = numa_distance((int)node, list->dev->numa_id); ctx_num = wd_get_avail_ctx(list->dev); if ((dis > tmp && ctx_num > 0) || (dis == tmp && ctx_num > max)) { diff --git a/wd_bmm.c b/wd_bmm.c index 12c3bcf..462a638 100644 --- a/wd_bmm.c +++ b/wd_bmm.c @@ -100,9 +100,8 @@ handle_t wd_find_ctx(const char *alg_name) struct mem_ctx_node *close_node = NULL; struct mem_ctx_node *node; int min_distance = 0xFFFF; - int cpu = sched_getcpu(); - int nid = numa_node_of_cpu(cpu); handle_t h_ctx = 0; + unsigned int nid; int numa_dis; if (!alg_name) { @@ -110,17 +109,23 @@ handle_t wd_find_ctx(const char *alg_name) return 0; } + /* Under default conditions in a VM, the node value is 0 */ + if (getcpu(NULL, &nid) || nid == (unsigned int)NUMA_NO_NODE) { + WD_ERR("invalid: failed to get numa node for memory pool!\n"); + return 0; + } + pthread_mutex_lock(&g_mem_ctx_mutex); TAILQ_FOREACH(node, &g_mem_ctx_list, list_node) { if (node->used == false && strstr(node->alg_name, alg_name)) { - if (node->numa_id == nid) { + if (node->numa_id == (int)nid) { h_ctx = node->h_ctx; node->used = true; break; } /* Query the queue with the shortest NUMA distance */ - numa_dis = numa_distance(nid, node->numa_id); + numa_dis = numa_distance((int)nid, node->numa_id); if (numa_dis < min_distance) { min_distance = numa_dis; close_node = node; diff --git a/wd_sched.c b/wd_sched.c index ec1e7b6..19936fd 100644 --- a/wd_sched.c +++ b/wd_sched.c @@ -176,11 +176,14 @@ static handle_t session_sched_init(handle_t h_sched_ctx, void *sched_param) { struct wd_sched_ctx *sched_ctx = (struct wd_sched_ctx *)h_sched_ctx; struct sched_params *param = (struct sched_params *)sched_param; - int cpu = sched_getcpu(); - int node = numa_node_of_cpu(cpu); struct sched_key *skey; + unsigned int node; - if (node < 0) { + if (getcpu(NULL, &node)) { + WD_ERR("failed to get node, errno %d!\n", errno); + return (handle_t)(-errno); + } + if (node == (unsigned int)NUMA_NO_NODE) { WD_ERR("invalid: failed to get numa node!\n"); return (handle_t)(-WD_EINVAL); } @@ -538,12 +541,15 @@ static handle_t session_dev_sched_init(handle_t h_sched_ctx, void *sched_param) { struct wd_sched_ctx *sched_ctx = (struct wd_sched_ctx *)h_sched_ctx; struct sched_params *param = (struct sched_params *)sched_param; - int cpu = sched_getcpu(); - int node = numa_node_of_cpu(cpu); struct sched_key *skey; + unsigned int node; - if (node < 0) { - WD_ERR("invalid: failed to get numa node!\n"); + if (getcpu(NULL, &node)) { + WD_ERR("failed to get numa node, errno %d!\n", errno); + return (handle_t)(-errno); + } + if (node == (unsigned int)NUMA_NO_NODE) { + WD_ERR("invalid: failed to get numa node for dev sched init!\n"); return (handle_t)(-WD_EINVAL); } -- 2.33.0