@@ -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;
@@ -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;
@@ -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;
@@ -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);
@@ -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 */
@@ -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
@@ -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.
*
@@ -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);
@@ -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;
@@ -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;
};
@@ -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)