[PATCH 1/5] eal: add lcore set name and get name API
Tyler Retzlaff
roretzla at linux.microsoft.com
Wed Dec 7 20:00:13 CET 2022
Add two new APIs that allow set and get of a name for an lcore that do
not explose platform thread details via the public interface.
If the underlying platform thread API does allow set of a name for the
lcore thread id it is called as a side-effect of set but lack of support
or failure from the platform-specific API does will not cause the call
to fail allowing for "debugging" on platforms that happen to support the
feature without exposing it as a part of the EAL.
Signed-off-by: Tyler Retzlaff <roretzla at linux.microsoft.com>
---
lib/eal/common/eal_common_lcore.c | 32 +++++++++++++++++++++++++++
lib/eal/common/eal_common_thread.c | 15 ++++++-------
lib/eal/common/eal_common_trace.c | 2 +-
lib/eal/freebsd/eal.c | 5 ++++-
lib/eal/freebsd/eal_thread.c | 6 ++++++
lib/eal/include/rte_lcore.h | 44 ++++++++++++++++++++++++++++++++++++++
lib/eal/include/rte_thread.h | 8 +++++++
lib/eal/linux/eal.c | 5 ++---
lib/eal/linux/eal_thread.c | 19 ++++++++++++++++
lib/eal/version.map | 5 +++++
lib/eal/windows/rte_thread.c | 41 +++++++++++++++++++++++++++++++++++
11 files changed, 169 insertions(+), 13 deletions(-)
diff --git a/lib/eal/common/eal_common_lcore.c b/lib/eal/common/eal_common_lcore.c
index 06c594b..700362a 100644
--- a/lib/eal/common/eal_common_lcore.c
+++ b/lib/eal/common/eal_common_lcore.c
@@ -14,6 +14,38 @@
#include "eal_private.h"
#include "eal_thread.h"
+static char lcore_names[RTE_MAX_LCORE][RTE_LCORE_NAME_MAX_LEN];
+
+int
+rte_lcore_set_name(unsigned int lcore_id, const char *name)
+{
+ if (unlikely(lcore_id >= RTE_MAX_LCORE))
+ return -EINVAL;
+
+ if (strlen(name) >= RTE_LCORE_NAME_MAX_LEN)
+ return -ERANGE;
+
+ (void)strcpy(&lcore_names[lcore_id][0], name);
+
+ rte_thread_set_name((rte_thread_t){lcore_config[lcore_id].thread_id}, name);
+
+ return 0;
+}
+
+int
+rte_lcore_get_name(unsigned int lcore_id, char *name, size_t len)
+{
+ if (unlikely(lcore_id >= RTE_MAX_LCORE))
+ return -EINVAL;
+
+ if (len < RTE_LCORE_NAME_MAX_LEN)
+ return -EINVAL;
+
+ (void)strcpy(name, &lcore_names[lcore_id][0]);
+
+ return 0;
+}
+
unsigned int rte_get_main_lcore(void)
{
return rte_eal_get_configuration()->main_lcore;
diff --git a/lib/eal/common/eal_common_thread.c b/lib/eal/common/eal_common_thread.c
index c5d8b43..91066e6 100644
--- a/lib/eal/common/eal_common_thread.c
+++ b/lib/eal/common/eal_common_thread.c
@@ -272,6 +272,7 @@ static void *ctrl_thread_init(void *arg)
const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
+ rte_thread_t id;
struct rte_thread_ctrl_params *params;
enum __rte_ctrl_thread_status ctrl_thread_status;
int ret;
@@ -285,18 +286,16 @@ static void *ctrl_thread_init(void *arg)
params->ret = 0;
params->ctrl_thread_status = CTRL_THREAD_LAUNCHING;
- ret = pthread_create(thread, attr, ctrl_thread_init, (void *)params);
+ ret = pthread_create((pthread_t *)&id.opaque_id, attr, ctrl_thread_init, params);
if (ret != 0) {
free(params);
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(id, name);
+
+ *thread = (pthread_t)id.opaque_id;
/* Wait for the control thread to initialize successfully */
while ((ctrl_thread_status =
@@ -312,7 +311,7 @@ static void *ctrl_thread_init(void *arg)
/* Check if the control thread encountered an error */
if (ctrl_thread_status == CTRL_THREAD_ERROR) {
/* ctrl thread is exiting */
- pthread_join(*thread, NULL);
+ (void)rte_thread_join(id, NULL);
}
ret = params->ret;
diff --git a/lib/eal/common/eal_common_trace.c b/lib/eal/common/eal_common_trace.c
index 5caaac8..03c1055 100644
--- a/lib/eal/common/eal_common_trace.c
+++ b/lib/eal/common/eal_common_trace.c
@@ -356,7 +356,7 @@ rte_trace_mode rte_trace_mode_get(void)
/* Store the thread name */
char *name = header->stream_header.thread_name;
memset(name, 0, __RTE_TRACE_EMIT_STRING_LEN_MAX);
- rte_thread_getname(pthread_self(), name,
+ (void)rte_lcore_get_name(rte_lcore_id(), name,
__RTE_TRACE_EMIT_STRING_LEN_MAX);
trace->lcore_meta[count].mem = header;
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 607684c..e6a76de 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -817,7 +817,10 @@ 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(lcore_config[i].thread_id, thread_name);
+ ret = rte_lcore_set_name(i, thread_name);
+ if (ret != 0)
+ RTE_LOG(DEBUG, EAL,
+ "Cannot set name for lcore\n");
ret = pthread_setaffinity_np(lcore_config[i].thread_id,
sizeof(rte_cpuset_t), &lcore_config[i].cpuset);
diff --git a/lib/eal/freebsd/eal_thread.c b/lib/eal/freebsd/eal_thread.c
index ab81b52..af85c48 100644
--- a/lib/eal/freebsd/eal_thread.c
+++ b/lib/eal/freebsd/eal_thread.c
@@ -32,6 +32,12 @@ int rte_sys_gettid(void)
return (int)lwpid;
}
+void rte_thread_set_name(rte_thread_t id, const char *name)
+{
+ /* this BSD function returns no error */
+ pthread_set_name_np(id.opaque_id, name);
+}
+
int rte_thread_setname(pthread_t id, const char *name)
{
/* this BSD function returns no error */
diff --git a/lib/eal/include/rte_lcore.h b/lib/eal/include/rte_lcore.h
index 6938c3f..251208f 100644
--- a/lib/eal/include/rte_lcore.h
+++ b/lib/eal/include/rte_lcore.h
@@ -13,6 +13,7 @@
*/
#include <stdio.h>
+#include <rte_common.h>
#include <rte_compat.h>
#include <rte_config.h>
#include <rte_per_lcore.h>
@@ -25,6 +26,7 @@
#endif
#define LCORE_ID_ANY UINT32_MAX /**< Any lcore. */
+#define RTE_LCORE_NAME_MAX_LEN RTE_MAX_THREAD_NAME_LEN
RTE_DECLARE_PER_LCORE(unsigned, _lcore_id); /**< Per thread "lcore id". */
@@ -90,6 +92,48 @@ enum rte_lcore_role_t {
unsigned int rte_get_main_lcore(void);
/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Sets the name of an lcore and (if supported) implicitly sets the
+ * debugging name of the lcores thread.
+ *
+ * @param lcore_id
+ * The targeted lcore.
+ *
+ * @param name
+ * The name to set for the specified lcore_id. strlen(name) shall be
+ * no more than RTE_LCORE_NAME_MAX_LEN - 1 long.
+ *
+ * @return
+ * 0 on success or, -errno on failure.
+ */
+__rte_experimental
+int rte_lcore_set_name(unsigned int lcore_id, const char *name);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Gets the name of an lcore thread.
+ *
+ * @param lcore_id
+ * The targeted lcore.
+ *
+ * @param name
+ * The buffer used to receive the lcore name.
+ *
+ * @param len
+ * The length of the buffer name. The buffer len shall be at
+ * least RTE_LCORE_NAME_MAX_LEN.
+ *
+ * @return
+ * 0 on success or, -errno on failure.
+ */
+__rte_experimental
+int rte_lcore_get_name(unsigned int lcore_id, char *name, size_t len);
+
+/**
* Return the number of execution units (lcores) on the system.
*
* @return
diff --git a/lib/eal/include/rte_thread.h b/lib/eal/include/rte_thread.h
index b9edf70..713330c 100644
--- a/lib/eal/include/rte_thread.h
+++ b/lib/eal/include/rte_thread.h
@@ -143,6 +143,14 @@ int rte_thread_create(rte_thread_t *thread_id,
rte_thread_t rte_thread_self(void);
/**
+ * @internal
+ * Set the name of the thread.
+ */
+__rte_internal
+void
+rte_thread_set_name(rte_thread_t id, const char *name);
+
+/**
* @warning
* @b EXPERIMENTAL: this API may change without prior notice.
*
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 8c118d0..97e2fec 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -1255,11 +1255,10 @@ 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(lcore_config[i].thread_id,
- thread_name);
+ ret = rte_lcore_set_name(i, thread_name);
if (ret != 0)
RTE_LOG(DEBUG, EAL,
- "Cannot set name for lcore thread\n");
+ "Cannot set name for lcore\n");
ret = pthread_setaffinity_np(lcore_config[i].thread_id,
sizeof(rte_cpuset_t), &lcore_config[i].cpuset);
diff --git a/lib/eal/linux/eal_thread.c b/lib/eal/linux/eal_thread.c
index 625bde6..2413bc6 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,24 @@ int rte_sys_gettid(void)
return (int)syscall(SYS_gettid);
}
+void rte_thread_set_name(rte_thread_t id, const char *name)
+{
+ int ret = ENOSYS;
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
+#if __GLIBC_PREREQ(2, 12)
+ char truncated[16];
+
+ strlcpy(truncated, name, sizeof(truncated));
+ ret = pthread_setname_np(id.opaque_id, truncated);
+#endif
+#endif
+ RTE_SET_USED(id);
+ RTE_SET_USED(name);
+
+ if (ret != 0)
+ RTE_LOG(DEBUG, EAL, "Cannot 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..4110f75 100644
--- a/lib/eal/version.map
+++ b/lib/eal/version.map
@@ -440,6 +440,10 @@ EXPERIMENTAL {
rte_thread_detach;
rte_thread_equal;
rte_thread_join;
+
+ # added in 23.03
+ rte_lcore_get_name;
+ rte_lcore_set_name;
};
INTERNAL {
@@ -483,4 +487,5 @@ INTERNAL {
rte_mem_map;
rte_mem_page_size;
rte_mem_unmap;
+ rte_thread_set_name;
};
diff --git a/lib/eal/windows/rte_thread.c b/lib/eal/windows/rte_thread.c
index 1c1e9d0..51028ef 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,45 @@ struct thread_routine_ctx {
return thread_id;
}
+void
+rte_thread_set_name(rte_thread_t id, const char *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, id.opaque_id);
+ if (thread_handle == NULL) {
+ ret = thread_log_last_error("OpenThread()");
+ goto cleanup;
+ }
+
+ rv = mbsrtowcs(wname, &name, sizeof(wname) / sizeof(wname[0]), &state);
+ if (rv == (size_t)-1) {
+ ret = EILSEQ;
+ goto cleanup;
+ }
+
+ if (name != NULL) {
+ ret = ERANGE;
+ goto cleanup;
+ }
+
+ if (FAILED(SetThreadDescription(thread_handle, wname))) {
+ ret = EINVAL;
+ goto cleanup;
+ }
+
+cleanup:
+ if (thread_handle != NULL)
+ CloseHandle(thread_handle);
+
+ if (ret != 0)
+ RTE_LOG(DEBUG, EAL, "Cannot set name for lcore\n");
+}
+
int
rte_thread_get_priority(rte_thread_t thread_id,
enum rte_thread_priority *priority)
--
1.8.3.1
More information about the dev
mailing list