All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: "Jianyue Wu (NSB)" <jianyue.wu@nokia-sbell.com>
To: "roretzla@linux.microsoft.com" <roretzla@linux.microsoft.com>,
	"thomas@monjalon.net" <thomas@monjalon.net>,
	"david.marchand@redhat.com" <david.marchand@redhat.com>
Cc: "'dev@dpdk.org'" <dev@dpdk.org>
Subject: [PATCH] eal/linux: enhanced error handling for affinity
Date: Mon, 22 Apr 2024 13:23:50 +0000	[thread overview]
Message-ID: <fa21c475248e4a42850697eb76b0de50@nokia-sbell.com> (raw)
In-Reply-To: <930beb572a814989b86186c55fa574be@nokia-sbell.com>


[-- Attachment #1.1: Type: text/plain, Size: 360 bytes --]

Hello,

Good day~
I hope this message finds you well. I am writing to submit a patch for consideration, which primarily adds enhanced error handling for affinity sets within the eal/linux of DPDK. Unfortunately, my current environment does not support git send-email, so I am sending this patch attached to this email.

Thank you~
Best regards,
Dave


[-- Attachment #1.2: Type: text/html, Size: 3047 bytes --]

[-- Attachment #2: 0001-eal-linux-enhanced-error-handling-for-affinity.patch --]
[-- Type: application/octet-stream, Size: 3326 bytes --]

From 89d879f762dc5e6901fc6cde06b8c36b641424de Mon Sep 17 00:00:00 2001
From: Jianyue Wu <jianyue.wu@nokia-sbell.com>
Date: Fri, 19 Apr 2024 13:57:35 +0300
Subject: [PATCH] eal/linux: enhanced error handling for affinity

Improve the robustness of setting thread affinity in DPDK
by adding detailed error logging.

Changes:
1. Set `errno` to 0 before calling `pthread_setaffinity_np()` to ensure
clean error status.
2. Check the return value of `pthread_setaffinity_np()` and log an error
if the call fails.
3. 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 <jianyue.wu@nokia-sbell.com>
---
 lib/eal/unix/rte_thread.c | 51 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 2 deletions(-)

diff --git a/lib/eal/unix/rte_thread.c b/lib/eal/unix/rte_thread.c
index 1b4c73f58e..56bc7995eb 100644
--- a/lib/eal/unix/rte_thread.c
+++ b/lib/eal/unix/rte_thread.c
@@ -5,6 +5,7 @@
 
 #include <errno.h>
 #include <pthread.h>
+#include <sched.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
@@ -114,6 +115,35 @@ thread_start_wrapper(void *arg)
 	return (void *)(uintptr_t)thread_func(thread_args);
 }
 
+/* Function to convert cpu_set_t to a string. */
+static void cpuset_to_string(const cpu_set_t *cpuset,
+			char *cpus_str, size_t cpus_str_size) {
+	int cpu;
+	// Track the current position in the string
+	size_t offset = 0;
+
+	// Clear the string buffer
+	memset(cpus_str, 0, cpus_str_size);
+	cpus_str_size = RTE_MAX_LCORE < cpus_str_size ?
+		RTE_MAX_LCORE : cpus_str_size;
+
+	// Iterate over each CPU core, and check if it is included in the set
+	for (cpu = 0; cpu < RTE_MAX_LCORE && offset < cpus_str_size - 1; ++cpu) {
+		if (CPU_ISSET(cpu, cpuset)) {
+			// Append the current CPU number to the string
+			int written = snprintf(cpus_str + offset, cpus_str_size - offset,
+				"%s%d", (offset > 0 ? "," : ""), cpu);
+			if (written > 0)
+				offset += written;
+			if (offset >= cpus_str_size - 1)
+				break;
+			}
+	}
+
+	// Ensure the string is properly terminated
+	cpus_str[cpus_str_size - 1] = '\0';
+}
+
 int
 rte_thread_create(rte_thread_t *thread_id,
 		const rte_thread_attr_t *thread_attr,
@@ -369,8 +399,25 @@ 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;
+	char cpus_str[RTE_MAX_LCORE + 1] = {'\0'};
+	char thread_name[RTE_MAX_THREAD_NAME_LEN] = {'\0'};
+
+	errno = 0;
+	ret = pthread_setaffinity_np((pthread_t)thread_id.opaque_id,
+				sizeof(*cpuset), cpuset);
+	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!");
+		cpuset_to_string(cpuset, cpus_str, sizeof(cpus_str));
+		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));
+	}
+
+	return ret;
 }
 
 int
-- 
2.42.0


  reply	other threads:[~2024-04-22 13:23 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-09 11:25 [dpdk-dev] Does memif support primary<->secondary process communication? Wu, Jianyue (NSB - CN/Hangzhou)
2021-06-09 12:07 ` Wu, Jianyue (NSB - CN/Hangzhou)
2024-04-22 13:23   ` Jianyue Wu (NSB) [this message]
2024-04-22 13:49     ` [PATCH] eal/linux: enhanced error handling for affinity Ferruh Yigit
2024-04-26 21:31       ` Patrick Robb
2024-04-22 13:50     ` Ferruh Yigit
2024-04-22 15:26     ` Stephen Hemminger
2024-04-22 15:27     ` Stephen Hemminger
2024-04-22 15:30     ` Stephen Hemminger
2024-04-23  3:01     ` Jianyue Wu (NSB)
2024-04-24  5:45     ` [PATCH v2] " Jianyue Wu (NSB)
  -- strict thread matches above, loose matches on Subject: below --
2024-04-23  3:02 [PATCH] " Jianyue Wu
2024-04-24 15:50 ` Stephen Hemminger
     [not found]   ` <3e9a9498.1535.18f12cc71dd.Coremail.wujianyue000@163.com>
     [not found]     ` <5def5f15.73a1.18f13c4e490.Coremail.wujianyue000@163.com>
2024-04-25 15:04       ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fa21c475248e4a42850697eb76b0de50@nokia-sbell.com \
    --to=jianyue.wu@nokia-sbell.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=roretzla@linux.microsoft.com \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.