From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id BAFEC43F0C; Fri, 26 Apr 2024 09:51:09 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 02A1643C71; Fri, 26 Apr 2024 09:51:07 +0200 (CEST) Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) by mails.dpdk.org (Postfix) with ESMTP id 1DE5C40284 for ; Thu, 25 Apr 2024 13:11:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=ONpW0 y6IPKOUIFRJZa7aA9pIt8R55Inv7Mky6UPee5Y=; b=iAwEiJlUsD/7yyrNjPdXF cXJ6USDugpVxSunWRtiZ4ah2AYaOi5mGC9Vy/zs+SiCmqIRZ78M5FxbSEf10CIG1 +E0WYuftfEyjAlahW4BvVZGi77cqzkmE2YQ+dMx3DuhQ6SWUGAd40UJMfmNfYmks uxu2+JELo3Qt0amZvV0oIk= Received: from ubuntu2204.. (unknown [36.24.144.104]) by gzga-smtp-mta-g2-2 (Coremail) with SMTP id _____wD3v4tjOipm2aFICQ--.39805S2; Thu, 25 Apr 2024 19:11:31 +0800 (CST) From: Jianyue Wu To: stephen@networkplumber.org, ferruh.yigit@amd.com, dev@dpdk.org Cc: Jianyue Wu Subject: [PATCH v2] eal/linux: enhanced error handling for affinity Date: Thu, 25 Apr 2024 19:11:30 +0800 Message-Id: <20240425111130.8306-1-wujianyue000@163.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wD3v4tjOipm2aFICQ--.39805S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxXryxAw4UCF15uw15Zr17trb_yoW5ZrWDpF yjka4Utr4FyrsF9343Ja1kZa45tFyfWa17JrWxCr1fA39rWa1rZry5KFy5W3W5ur4UGan8 ZrW3urZ09r4DJFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07U_EfrUUUUU= X-Originating-IP: [36.24.144.104] X-CM-SenderInfo: 5zxmxtpq1xviiqq6il2tof0z/1tbisAXLemV4JEEU-wAAsX X-Mailman-Approved-At: Fri, 26 Apr 2024 09:51:04 +0200 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Jianyue Wu Improve the robustness of setting thread affinity in DPDK by adding detailed error logging. Changes: 1. Check the return value of pthread_setaffinity_np() and log an error if the call fails. 2. Include the current thread name, the intended CPU set, and a detailed error message in the log. Sample prints: EAL: Cannot set affinity for thread dpdk-test with cpus 0, ret: 22, errno: 0, error description: Success EAL: Cannot set affinity for thread dpdk-worker1 with cpus 1, ret: 22, errno: 0, error description: Success Signed-off-by: Jianyue Wu --- lib/eal/common/eal_common_thread.c | 2 +- lib/eal/common/eal_thread.h | 2 +- lib/eal/unix/rte_thread.c | 27 +++++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/eal/common/eal_common_thread.c b/lib/eal/common/eal_common_thread.c index a53bc639ae..31a2fab2a7 100644 --- a/lib/eal/common/eal_common_thread.c +++ b/lib/eal/common/eal_common_thread.c @@ -103,7 +103,7 @@ rte_thread_get_affinity(rte_cpuset_t *cpusetp) } int -eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size) +eal_thread_dump_affinity(const rte_cpuset_t *cpuset, char *str, unsigned int size) { unsigned cpu; int ret; diff --git a/lib/eal/common/eal_thread.h b/lib/eal/common/eal_thread.h index 1c3c3442d3..85ab84baa5 100644 --- a/lib/eal/common/eal_thread.h +++ b/lib/eal/common/eal_thread.h @@ -50,7 +50,7 @@ unsigned eal_cpu_socket_id(unsigned cpu_id); * 0 for success, -1 if truncation happens. */ int -eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size); +eal_thread_dump_affinity(const rte_cpuset_t *cpuset, char *str, unsigned int size); /** * Dump the current thread cpuset. diff --git a/lib/eal/unix/rte_thread.c b/lib/eal/unix/rte_thread.c index 1b4c73f58e..34ac0eabbf 100644 --- a/lib/eal/unix/rte_thread.c +++ b/lib/eal/unix/rte_thread.c @@ -369,8 +369,31 @@ int rte_thread_set_affinity_by_id(rte_thread_t thread_id, const rte_cpuset_t *cpuset) { - return pthread_setaffinity_np((pthread_t)thread_id.opaque_id, - sizeof(*cpuset), cpuset); + int ret; +#if defined(__linux__) && defined(_GNU_SOURCE) + char cpus_str[RTE_CPU_AFFINITY_STR_LEN] = {'\0'}; + char thread_name[RTE_MAX_THREAD_NAME_LEN] = {'\0'}; + errno = 0; +#endif + + ret = pthread_setaffinity_np((pthread_t)thread_id.opaque_id, + sizeof(*cpuset), cpuset); + +#if defined(__linux__) && defined(_GNU_SOURCE) + if (ret != 0) { + if (pthread_getname_np((pthread_t)thread_id.opaque_id, + thread_name, sizeof(thread_name)) != 0) + EAL_LOG(ERR, "pthread_getname_np failed!"); + if (eal_thread_dump_affinity(cpuset, cpus_str, RTE_CPU_AFFINITY_STR_LEN) != 0) + EAL_LOG(ERR, "eal_thread_dump_affinity failed!"); + EAL_LOG(ERR, "Cannot set affinity for thread %s with cpus %s, " + "ret: %d, errno: %d, error description: %s", + thread_name, cpus_str, + ret, errno, strerror(errno)); + } +#endif + + return ret; } int -- 2.34.1