[v3,1/6] eal: add function that sets thread name

Message ID 1629294247-5207-2-git-send-email-navasile@linux.microsoft.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series Enable the internal EAL thread API |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-testing warning apply patch failure

Commit Message

Narcisa Ana Maria Vasile Aug. 18, 2021, 1:44 p.m. UTC
  From: Narcisa Vasile <navasile@microsoft.com>

Implement function that sets the name of a thread.
On Windows, SetThreadDescription() is used. Use GetProcAddress()
to obtain the address of the function for MinGW compatibility.

Signed-off-by: Narcisa Vasile <navasile@microsoft.com>
---
 lib/eal/common/rte_thread.c  | 17 +++++++++
 lib/eal/include/rte_thread.h | 16 +++++++++
 lib/eal/version.map          |  1 +
 lib/eal/windows/rte_thread.c | 68 ++++++++++++++++++++++++++++++++++++
 4 files changed, 102 insertions(+)
  

Comments

Tal Shnaiderman Aug. 18, 2021, 7:42 p.m. UTC | #1
> Subject: [PATCH v3 1/6] eal: add function that sets thread name
> 
> External email: Use caution opening links or attachments
> 
> 
> From: Narcisa Vasile <navasile@microsoft.com>
> 
> Implement function that sets the name of a thread.
> On Windows, SetThreadDescription() is used. Use GetProcAddress() to obtain
> the address of the function for MinGW compatibility.
> 
> Signed-off-by: Narcisa Vasile <navasile@microsoft.com>
> ---
>  lib/eal/common/rte_thread.c  | 17 +++++++++  lib/eal/include/rte_thread.h |
> 16 +++++++++
>  lib/eal/version.map          |  1 +
>  lib/eal/windows/rte_thread.c | 68
> ++++++++++++++++++++++++++++++++++++
>  4 files changed, 102 insertions(+)

<snip>

> +RTE_INIT(rte_thread_description_ptr_init)
> +{
> +       HMODULE kernel_lib = NULL;
> +       static const char library_name[] = "kernel32.dll";
> +       static const char function[] = "SetThreadDescription";
> +
> +       kernel_lib = LoadLibraryA(library_name);
> +       if (kernel_lib == NULL) {
> +               (void)thread_log_last_error("LoadLibraryA(\"kernel32.dll\")");
> +               return;
> +       }
> +
> +       SetThreadDescription_ptr = (SetThreadDescription_type)(
> +                       (void *)GetProcAddress(kernel_lib, function));
> +       if (SetThreadDescription_ptr == NULL) {
> +               (void)thread_log_last_error("GetProcAddress(\"kernel32.dll\",
> \"SetThreadDescription\")");
> +               return;

You need to remove the return above to also free kernel32.dll in error flow.

> +       }
> +
> +       FreeLibrary(kernel_lib);
> +}
> +
> +int
> +rte_thread_name_set(rte_thread_t thread_id, const char *name) {
> +       int ret = 0;
> +       size_t count;
> +       HRESULT hr;
> +       HANDLE thread_handle = NULL;
> +       WCHAR w_name[16];
> +
> +       thread_handle = OpenThread(THREAD_SET_LIMITED_INFORMATION,
> FALSE,
> +                       thread_id.opaque_id);
> +       if (thread_handle == NULL) {
> +               ret = thread_log_last_error("OpenThread()");
> +               goto cleanup;
> +       }
> +
> +       count = mbstowcs(w_name, name, RTE_DIM(w_name));
> +       if (count == (size_t) (-1)) {
> +               RTE_LOG(DEBUG, EAL, "Invalid thread name!\n");
> +               ret = EINVAL;
> +               goto cleanup;
> +       }
> +
> +       if (SetThreadDescription_ptr == NULL) {
> +               RTE_LOG(DEBUG, EAL, "Invalid function pointer to
> SetThreadDescription()!\n");
> +               ret = EINVAL;
> +               goto cleanup;
> +       }
> +
> +       hr = SetThreadDescription_ptr(thread_handle, w_name);
> +       if (FAILED(hr)) {
> +               ret = thread_log_last_error("SetThreadDescription()");
> +               goto cleanup;
> +       }
> +
> +cleanup:
> +       if (thread_handle != NULL)
> +               CloseHandle(thread_handle);
> +       return ret;
> +}
> +
>  int
>  rte_thread_key_create(rte_thread_key *key,
>                 __rte_unused void (*destructor)(void *))
> --
> 2.31.0.vfs.0.1
  

Patch

diff --git a/lib/eal/common/rte_thread.c b/lib/eal/common/rte_thread.c
index 3fdb267337..c91ed3d433 100644
--- a/lib/eal/common/rte_thread.c
+++ b/lib/eal/common/rte_thread.c
@@ -373,6 +373,23 @@  rte_thread_barrier_destroy(rte_thread_barrier *barrier)
 	return ret;
 }
 
+int
+rte_thread_name_set(rte_thread_t thread_id, const char *name)
+{
+	int ret = ENOSYS;
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
+#if __GLIBC_PREREQ(2, 12)
+	char truncated[16];
+
+	memcpy(truncated, name, sizeof(truncated));
+	ret = pthread_setname_np((pthread_t)thread_id.opaque_id, truncated);
+#endif
+#endif
+	RTE_SET_USED(thread_id);
+	RTE_SET_USED(name);
+	return ret;
+}
+
 int
 rte_thread_key_create(rte_thread_key *key, void (*destructor)(void *))
 {
diff --git a/lib/eal/include/rte_thread.h b/lib/eal/include/rte_thread.h
index 40da83467b..2f6258e336 100644
--- a/lib/eal/include/rte_thread.h
+++ b/lib/eal/include/rte_thread.h
@@ -439,6 +439,22 @@  int rte_thread_barrier_wait(rte_thread_barrier *barrier);
 __rte_experimental
 int rte_thread_barrier_destroy(rte_thread_barrier *barrier);
 
+/**
+ * Set the name of the thread represented by 'thread_id'.
+ *
+ * @param thread_id
+ *   The id of the thread.
+ *
+ * @param name
+ *   Thread name to set.
+ *
+ * @return
+ *   On success, return 0.
+ *   On failure, return a positive errno-style error number.
+ */
+__rte_experimental
+int rte_thread_name_set(rte_thread_t thread_id, const char *name);
+
 /**
  * Create a TLS data key visible to all threads in the process.
  * the created key is later used to get/set a value.
diff --git a/lib/eal/version.map b/lib/eal/version.map
index 541dc13053..7ce8dcea07 100644
--- a/lib/eal/version.map
+++ b/lib/eal/version.map
@@ -446,6 +446,7 @@  EXPERIMENTAL {
 	rte_thread_barrier_init;
 	rte_thread_barrier_wait;
 	rte_thread_barrier_destroy;
+	rte_thread_name_set;
 };
 
 INTERNAL {
diff --git a/lib/eal/windows/rte_thread.c b/lib/eal/windows/rte_thread.c
index b2ff16f51f..ba543c4b2a 100644
--- a/lib/eal/windows/rte_thread.c
+++ b/lib/eal/windows/rte_thread.c
@@ -556,6 +556,74 @@  rte_thread_barrier_destroy(rte_thread_barrier *barrier)
 	return 0;
 }
 
+typedef HRESULT
+(*SetThreadDescription_type)(HANDLE thread_handle, PCWSTR thread_description);
+
+static SetThreadDescription_type SetThreadDescription_ptr;
+
+RTE_INIT(rte_thread_description_ptr_init)
+{
+	HMODULE kernel_lib = NULL;
+	static const char library_name[] = "kernel32.dll";
+	static const char function[] = "SetThreadDescription";
+
+	kernel_lib = LoadLibraryA(library_name);
+	if (kernel_lib == NULL) {
+		(void)thread_log_last_error("LoadLibraryA(\"kernel32.dll\")");
+		return;
+	}
+
+	SetThreadDescription_ptr = (SetThreadDescription_type)(
+			(void *)GetProcAddress(kernel_lib, function));
+	if (SetThreadDescription_ptr == NULL) {
+		(void)thread_log_last_error("GetProcAddress(\"kernel32.dll\", \"SetThreadDescription\")");
+		return;
+	}
+
+	FreeLibrary(kernel_lib);
+}
+
+int
+rte_thread_name_set(rte_thread_t thread_id, const char *name)
+{
+	int ret = 0;
+	size_t count;
+	HRESULT hr;
+	HANDLE thread_handle = NULL;
+	WCHAR w_name[16];
+
+	thread_handle = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE,
+			thread_id.opaque_id);
+	if (thread_handle == NULL) {
+		ret = thread_log_last_error("OpenThread()");
+		goto cleanup;
+	}
+
+	count = mbstowcs(w_name, name, RTE_DIM(w_name));
+	if (count == (size_t) (-1)) {
+		RTE_LOG(DEBUG, EAL, "Invalid thread name!\n");
+		ret = EINVAL;
+		goto cleanup;
+	}
+
+	if (SetThreadDescription_ptr == NULL) {
+		RTE_LOG(DEBUG, EAL, "Invalid function pointer to SetThreadDescription()!\n");
+		ret = EINVAL;
+		goto cleanup;
+	}
+
+	hr = SetThreadDescription_ptr(thread_handle, w_name);
+	if (FAILED(hr)) {
+		ret = thread_log_last_error("SetThreadDescription()");
+		goto cleanup;
+	}
+
+cleanup:
+	if (thread_handle != NULL)
+		CloseHandle(thread_handle);
+	return ret;
+}
+
 int
 rte_thread_key_create(rte_thread_key *key,
 		__rte_unused void (*destructor)(void *))