DPDK patches and discussions
 help / color / mirror / Atom feed
From: Tyler Retzlaff <roretzla@linux.microsoft.com>
To: dev@dpdk.org
Cc: thomas@monjalon.net, david.marchand@redhat.com,
	jerinjacobk@gmail.com, mb@smartsharesystems.com,
	Tyler Retzlaff <roretzla@linux.microsoft.com>
Subject: [PATCH v10 1/4] eal: add thread set name API operating on rte thread
Date: Tue, 24 Jan 2023 10:12:18 -0800	[thread overview]
Message-ID: <1674583941-16964-2-git-send-email-roretzla@linux.microsoft.com> (raw)
In-Reply-To: <1674583941-16964-1-git-send-email-roretzla@linux.microsoft.com>

Add a rte_thread_set_name that sets the name of an rte_thread_t thread.
This is a replacement for the rte_thread_setname(pthread_t, ...) which
exposes platform-specific details.

Introduces Windows implementation for rte_thread_set_name not previously
available on Windows.

Adapt drivers/mlx5 to use the new internal rte_thread_set_name API
instead of the soon to be deprecated rte_thread_setname API.

Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
 drivers/net/mlx5/mlx5_hws_cnt.c     |  3 ++-
 drivers/vdpa/mlx5/mlx5_vdpa_event.c |  3 +--
 lib/eal/common/eal_common_thread.c  |  8 ++-----
 lib/eal/freebsd/eal.c               |  3 +--
 lib/eal/freebsd/eal_thread.c        | 11 ++++++++++
 lib/eal/include/rte_thread.h        | 20 +++++++++++++++++
 lib/eal/linux/eal.c                 |  6 +-----
 lib/eal/linux/eal_thread.c          | 22 +++++++++++++++++++
 lib/eal/version.map                 |  3 +++
 lib/eal/windows/rte_thread.c        | 43 +++++++++++++++++++++++++++++++++++++
 10 files changed, 106 insertions(+), 16 deletions(-)

diff --git a/drivers/net/mlx5/mlx5_hws_cnt.c b/drivers/net/mlx5/mlx5_hws_cnt.c
index 51704ef..05cc954 100644
--- a/drivers/net/mlx5/mlx5_hws_cnt.c
+++ b/drivers/net/mlx5/mlx5_hws_cnt.c
@@ -465,7 +465,8 @@ struct mlx5_hws_cnt_pool *
 	}
 	snprintf(name, CNT_THREAD_NAME_MAX - 1, "%s/svc@%d",
 		 sh->ibdev_name, service_core);
-	rte_thread_setname(sh->cnt_svc->service_thread, name);
+	rte_thread_set_name((rte_thread_t){(uintptr_t)sh->cnt_svc->service_thread},
+		name);
 	CPU_SET(service_core, &cpuset);
 	pthread_setaffinity_np(sh->cnt_svc->service_thread, sizeof(cpuset),
 				&cpuset);
diff --git a/drivers/vdpa/mlx5/mlx5_vdpa_event.c b/drivers/vdpa/mlx5/mlx5_vdpa_event.c
index 4d81976..f3d392c 100644
--- a/drivers/vdpa/mlx5/mlx5_vdpa_event.c
+++ b/drivers/vdpa/mlx5/mlx5_vdpa_event.c
@@ -547,8 +547,7 @@
 		goto out;
 	}
 	snprintf(name, sizeof(name), "vDPA-mlx5-%d", priv->vid);
-	if (rte_thread_setname(priv->timer_tid, name) != 0)
-		DRV_LOG(DEBUG, "Cannot set timer thread name.");
+	rte_thread_set_name((rte_thread_t){(uintptr_t)priv->timer_tid}, name);
 out:
 	if (attrp != NULL)
 		pthread_attr_destroy(attrp);
diff --git a/lib/eal/common/eal_common_thread.c b/lib/eal/common/eal_common_thread.c
index 38d83a6..3181515 100644
--- a/lib/eal/common/eal_common_thread.c
+++ b/lib/eal/common/eal_common_thread.c
@@ -288,12 +288,8 @@ static void *ctrl_thread_init(void *arg)
 		return -ret;
 	}
 
-	if (name != NULL) {
-		ret = rte_thread_setname(*thread, name);
-		if (ret < 0)
-			RTE_LOG(DEBUG, EAL,
-				"Cannot set name for ctrl thread\n");
-	}
+	if (name != NULL)
+		rte_thread_set_name((rte_thread_t){(uintptr_t)*thread}, name);
 
 	/* Wait for the control thread to initialize successfully */
 	while ((ctrl_thread_status =
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 8db5007..9303401 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -817,8 +817,7 @@ static void rte_eal_init_alert(const char *msg)
 		/* Set thread_name for aid in debugging. */
 		snprintf(thread_name, sizeof(thread_name),
 				"rte-worker-%d", i);
-		rte_thread_setname((pthread_t)lcore_config[i].thread_id.opaque_id,
-			thread_name);
+		rte_thread_set_name(lcore_config[i].thread_id, thread_name);
 
 		ret = rte_thread_set_affinity_by_id(lcore_config[i].thread_id,
 			&lcore_config[i].cpuset);
diff --git a/lib/eal/freebsd/eal_thread.c b/lib/eal/freebsd/eal_thread.c
index ab81b52..b69f5d3 100644
--- a/lib/eal/freebsd/eal_thread.c
+++ b/lib/eal/freebsd/eal_thread.c
@@ -32,6 +32,17 @@ int rte_sys_gettid(void)
 	return (int)lwpid;
 }
 
+void rte_thread_set_name(rte_thread_t thread_id, const char *thread_name)
+{
+	char truncated[RTE_MAX_THREAD_NAME_LEN];
+	const size_t truncatedsz = sizeof(truncated);
+
+	if (strlcpy(truncated, thread_name, truncatedsz) >= truncatedsz)
+		RTE_LOG(DEBUG, EAL, "Truncated thread name\n");
+
+	pthread_set_name_np((pthread_t)thread_id.opaque_id, truncated);
+}
+
 int rte_thread_setname(pthread_t id, const char *name)
 {
 	/* this BSD function returns no error */
diff --git a/lib/eal/include/rte_thread.h b/lib/eal/include/rte_thread.h
index b9edf70..d247930 100644
--- a/lib/eal/include/rte_thread.h
+++ b/lib/eal/include/rte_thread.h
@@ -146,6 +146,26 @@ int rte_thread_create(rte_thread_t *thread_id,
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice.
  *
+ * Set the name of the thread.
+ * This API is a noop if the underlying platform does not
+ * support setting the thread name or the platform-specific
+ * API used to set the thread name fails.
+ *
+ * @param thread_id
+ *    The id of the thread to set name.
+ *
+ * @param thread_name
+ *    The name to set. Truncated to RTE_MAX_THREAD_NAME_LEN,
+ *    including terminating NUL if necessary.
+ */
+__rte_experimental
+void
+rte_thread_set_name(rte_thread_t thread_id, const char *thread_name);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
  * Check if 2 thread ids are equal.
  *
  * @param t1
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 60298c0..d4c3507 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -1261,11 +1261,7 @@ static void rte_eal_init_alert(const char *msg)
 		/* Set thread_name for aid in debugging. */
 		snprintf(thread_name, sizeof(thread_name),
 			"rte-worker-%d", i);
-		ret = rte_thread_setname((pthread_t)lcore_config[i].thread_id.opaque_id,
-			thread_name);
-		if (ret != 0)
-			RTE_LOG(DEBUG, EAL,
-				"Cannot set name for lcore thread\n");
+		rte_thread_set_name(lcore_config[i].thread_id, thread_name);
 
 		ret = rte_thread_set_affinity_by_id(lcore_config[i].thread_id,
 			&lcore_config[i].cpuset);
diff --git a/lib/eal/linux/eal_thread.c b/lib/eal/linux/eal_thread.c
index 625bde6..d5fddab 100644
--- a/lib/eal/linux/eal_thread.c
+++ b/lib/eal/linux/eal_thread.c
@@ -10,6 +10,7 @@
 
 #include <rte_eal.h>
 #include <rte_lcore.h>
+#include <rte_log.h>
 #include <rte_string_fns.h>
 
 /* require calling thread tid by gettid() */
@@ -18,6 +19,27 @@ int rte_sys_gettid(void)
 	return (int)syscall(SYS_gettid);
 }
 
+void rte_thread_set_name(rte_thread_t thread_id, const char *thread_name)
+{
+	int ret = ENOSYS;
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
+#if __GLIBC_PREREQ(2, 12)
+	char truncated[RTE_MAX_THREAD_NAME_LEN];
+	const size_t truncatedsz = sizeof(truncated);
+
+	if (strlcpy(truncated, thread_name, truncatedsz) >= truncatedsz)
+		RTE_LOG(DEBUG, EAL, "Truncated thread name\n");
+
+	ret = pthread_setname_np((pthread_t)thread_id.opaque_id, truncated);
+#endif
+#endif
+	RTE_SET_USED(thread_id);
+	RTE_SET_USED(thread_name);
+
+	if (ret != 0)
+		RTE_LOG(DEBUG, EAL, "Failed to set thread name\n");
+}
+
 int rte_thread_setname(pthread_t id, const char *name)
 {
 	int ret = ENOSYS;
diff --git a/lib/eal/version.map b/lib/eal/version.map
index 7ad12a7..c98ba71 100644
--- a/lib/eal/version.map
+++ b/lib/eal/version.map
@@ -440,6 +440,9 @@ EXPERIMENTAL {
 	rte_thread_detach;
 	rte_thread_equal;
 	rte_thread_join;
+
+	# added in 23.03
+	rte_thread_set_name;
 };
 
 INTERNAL {
diff --git a/lib/eal/windows/rte_thread.c b/lib/eal/windows/rte_thread.c
index 1c1e9d0..8556a84 100644
--- a/lib/eal/windows/rte_thread.c
+++ b/lib/eal/windows/rte_thread.c
@@ -4,7 +4,9 @@
  */
 
 #include <errno.h>
+#include <wchar.h>
 
+#include <rte_eal.h>
 #include <rte_common.h>
 #include <rte_errno.h>
 #include <rte_thread.h>
@@ -305,6 +307,47 @@ struct thread_routine_ctx {
 	return thread_id;
 }
 
+void
+rte_thread_set_name(rte_thread_t thread_id, const char *thread_name)
+{
+	int ret = 0;
+	wchar_t wname[RTE_MAX_THREAD_NAME_LEN];
+	mbstate_t state = {0};
+	size_t rv;
+	HANDLE thread_handle;
+
+	thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE,
+		thread_id.opaque_id);
+	if (thread_handle == NULL) {
+		ret = thread_log_last_error("OpenThread()");
+		goto cleanup;
+	}
+
+	memset(wname, 0, sizeof(wname));
+	rv = mbsrtowcs(wname, &thread_name, RTE_DIM(wname) - 1, &state);
+	if (rv == (size_t)-1) {
+		ret = EILSEQ;
+		goto cleanup;
+	}
+
+#ifndef RTE_TOOLCHAIN_GCC
+	if (FAILED(SetThreadDescription(thread_handle, wname))) {
+		ret = EINVAL;
+		goto cleanup;
+	}
+#else
+	ret = ENOTSUP;
+	goto cleanup;
+#endif
+
+cleanup:
+	if (thread_handle != NULL)
+		CloseHandle(thread_handle);
+
+	if (ret != 0)
+		RTE_LOG(DEBUG, EAL, "Failed to set thread name\n");
+}
+
 int
 rte_thread_get_priority(rte_thread_t thread_id,
 	enum rte_thread_priority *priority)
-- 
1.8.3.1


  reply	other threads:[~2023-01-24 18:12 UTC|newest]

Thread overview: 91+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-07 19:00 [PATCH 0/5] add lcore set name and get name API Tyler Retzlaff
2022-12-07 19:00 ` [PATCH 1/5] eal: " Tyler Retzlaff
2022-12-07 21:03   ` Stephen Hemminger
2022-12-07 22:33     ` Tyler Retzlaff
2022-12-08  4:07       ` Stephen Hemminger
2022-12-08 17:11         ` Tyler Retzlaff
2022-12-07 19:00 ` [PATCH 2/5] eal: set lcore config thread for lcore main Tyler Retzlaff
2022-12-07 19:00 ` [PATCH 3/5] test: add a unit test for set and get lcore name APIs Tyler Retzlaff
2022-12-07 19:00 ` [PATCH 4/5] eal: remove thread getname API Tyler Retzlaff
2022-12-07 19:00 ` [PATCH 5/5] eal: deprecate rte thread setname API Tyler Retzlaff
2022-12-14 16:47 ` [PATCH v2 0/4] add rte_thread_set_name API for rte_thread_t Tyler Retzlaff
2022-12-14 16:47   ` [PATCH v2 1/4] eal: add thread set name API operating on rte thread Tyler Retzlaff
2023-01-11 16:09     ` David Marchand
2023-01-12 21:32       ` Tyler Retzlaff
2023-01-13  4:36       ` Jerin Jacob
2023-01-13 17:09         ` Tyler Retzlaff
2023-01-13 18:56           ` Tyler Retzlaff
2022-12-14 16:47   ` [PATCH v2 2/4] eal: remove thread getname API Tyler Retzlaff
2022-12-14 16:47   ` [PATCH v2 3/4] eal: deprecate rte thread setname API Tyler Retzlaff
2022-12-14 16:47   ` [PATCH v2 4/4] drivers: mlx5 use rte thread set name Tyler Retzlaff
2022-12-14 16:51     ` Morten Brørup
2023-01-10 20:53   ` [PATCH v2 0/4] add rte_thread_set_name API for rte_thread_t Tyler Retzlaff
2023-01-11  7:55     ` Morten Brørup
2023-01-12 21:55 ` [PATCH v3 " Tyler Retzlaff
2023-01-12 21:55   ` [PATCH v3 1/4] eal: add thread set name API operating on rte thread Tyler Retzlaff
2023-01-12 21:55   ` [PATCH v3 2/4] eal: remove thread getname API Tyler Retzlaff
2023-01-12 21:55   ` [PATCH v3 3/4] drivers: mlx5 use rte thread set name Tyler Retzlaff
2023-01-12 21:55   ` [PATCH v3 4/4] eal: deprecate rte thread setname API Tyler Retzlaff
2023-01-13 18:51 ` [PATCH v4 0/4] add rte_thread_set_name API for rte_thread_t Tyler Retzlaff
2023-01-13 18:52   ` [PATCH v4 1/4] eal: add thread set name API operating on rte thread Tyler Retzlaff
2023-01-13 18:52   ` [PATCH v4 2/4] eal: remove thread getname API Tyler Retzlaff
2023-01-17  7:49     ` Jerin Jacob
2023-01-17 18:22       ` Tyler Retzlaff
2023-01-13 18:52   ` [PATCH v4 3/4] drivers: mlx5 use rte thread set name Tyler Retzlaff
2023-01-13 18:52   ` [PATCH v4 4/4] eal: deprecate rte thread setname API Tyler Retzlaff
2023-01-17 18:21 ` [PATCH v5 0/4] add rte_thread_set_name API for rte_thread_t Tyler Retzlaff
2023-01-17 18:21   ` [PATCH v5 1/4] eal: add thread set name API operating on rte thread Tyler Retzlaff
2023-01-18 14:50     ` David Marchand
2023-01-17 18:21   ` [PATCH v5 2/4] eal: remove thread getname API Tyler Retzlaff
2023-01-18  6:28     ` Jerin Jacob
2023-01-18 14:51     ` David Marchand
2023-01-17 18:21   ` [PATCH v5 3/4] drivers: mlx5 use rte thread set name Tyler Retzlaff
2023-01-17 18:21   ` [PATCH v5 4/4] eal: deprecate rte thread setname API Tyler Retzlaff
2023-01-18 11:49     ` David Marchand
2023-01-18 19:54 ` [PATCH v6 0/5] add rte_thread_set_name API for rte_thread_t Tyler Retzlaff
2023-01-18 19:54   ` [PATCH v6 1/5] eal: add thread set name API operating on rte thread Tyler Retzlaff
2023-01-18 23:13     ` Stephen Hemminger
2023-01-18 23:44       ` Tyler Retzlaff
2023-01-18 19:54   ` [PATCH v6 2/5] eal: remove thread getname API Tyler Retzlaff
2023-01-18 19:54   ` [PATCH v6 3/5] eal: set thread name on Windows worker threads Tyler Retzlaff
2023-01-18 19:54   ` [PATCH v6 4/5] drivers: mlx5 use rte thread set name Tyler Retzlaff
2023-01-18 19:54   ` [PATCH v6 5/5] eal: deprecation notice for rte thread setname API Tyler Retzlaff
2023-01-22 13:55   ` [PATCH v6 0/5] add rte_thread_set_name API for rte_thread_t David Marchand
2023-01-23 18:57     ` Tyler Retzlaff
2023-01-23 19:39 ` [PATCH v7 " Tyler Retzlaff
2023-01-23 19:39   ` [PATCH v7 1/5] eal: add thread set name API operating on rte thread Tyler Retzlaff
2023-01-24 15:21     ` Thomas Monjalon
2023-01-23 19:39   ` [PATCH v7 2/5] eal: remove thread getname API Tyler Retzlaff
2023-01-24 15:24     ` Thomas Monjalon
2023-01-24 15:33       ` Tyler Retzlaff
2023-01-23 19:39   ` [PATCH v7 3/5] eal: set thread name on Windows worker threads Tyler Retzlaff
2023-01-24 15:25     ` Thomas Monjalon
2023-01-24 15:33       ` Tyler Retzlaff
2023-01-24 15:47         ` Thomas Monjalon
2023-01-23 19:39   ` [PATCH v7 4/5] drivers: mlx5 use rte thread set name Tyler Retzlaff
2023-01-24 15:50     ` Thomas Monjalon
2023-01-23 19:39   ` [PATCH v7 5/5] eal: deprecation notice for rte thread setname API Tyler Retzlaff
2023-01-24 16:03     ` Thomas Monjalon
2023-01-24 17:42 ` [PATCH v8 0/4] add rte_thread_set_name API for rte_thread_t Tyler Retzlaff
2023-01-24 17:42   ` [PATCH v8 1/4] eal: add thread set name API operating on rte thread Tyler Retzlaff
2023-01-24 17:42   ` [PATCH v8 2/4] eal: remove thread getname API Tyler Retzlaff
2023-01-24 17:42   ` [PATCH v8 3/4] eal: set thread name on Windows worker threads Tyler Retzlaff
2023-01-24 17:42   ` [PATCH v8 4/4] eal: deprecation notice for rte thread setname API Tyler Retzlaff
2023-01-24 18:02 ` [PATCH v9 0/4] add rte_thread_set_name API for rte_thread_t Tyler Retzlaff
2023-01-24 18:02   ` [PATCH v9 1/4] eal: add thread set name API operating on rte thread Tyler Retzlaff
2023-01-24 18:02   ` [PATCH v9 2/4] eal: remove thread getname API Tyler Retzlaff
2023-01-24 18:02   ` [PATCH v9 3/4] eal: set thread name on Windows worker threads Tyler Retzlaff
2023-01-24 18:02   ` [PATCH v9 4/4] eal: deprecation notice for rte thread setname API Tyler Retzlaff
2023-01-24 18:05     ` Thomas Monjalon
2023-01-24 18:21       ` Tyler Retzlaff
2023-01-24 18:12 ` [PATCH v10 0/4] add rte_thread_set_name API for rte_thread_t Tyler Retzlaff
2023-01-24 18:12   ` Tyler Retzlaff [this message]
2023-01-24 18:12   ` [PATCH v10 2/4] eal: remove thread getname API Tyler Retzlaff
2023-01-24 18:12   ` [PATCH v10 3/4] eal: set thread name on Windows worker threads Tyler Retzlaff
2023-01-24 18:12   ` [PATCH v10 4/4] eal: deprecation notice for rte thread setname API Tyler Retzlaff
2023-01-30 15:46     ` Thomas Monjalon
2023-01-30 16:19       ` David Marchand
2023-01-30 20:34         ` Tyler Retzlaff
2023-01-31  9:53           ` David Marchand
2023-01-31 17:09             ` Tyler Retzlaff
2023-01-31  9:54   ` [PATCH v10 0/4] add rte_thread_set_name API for rte_thread_t David Marchand

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=1674583941-16964-2-git-send-email-roretzla@linux.microsoft.com \
    --to=roretzla@linux.microsoft.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=jerinjacobk@gmail.com \
    --cc=mb@smartsharesystems.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).