[Openmp-commits] [openmp] [OpenMP] Rename worker threads for improved debuggability (PR #102065)

via Openmp-commits openmp-commits at lists.llvm.org
Thu Aug 8 15:42:48 PDT 2024


https://github.com/HighW4y2H3ll updated https://github.com/llvm/llvm-project/pull/102065

>From fe3bf2df926c90afe6bc9b2aa7253c63bee89771 Mon Sep 17 00:00:00 2001
From: h2h <h2h at meta.com>
Date: Mon, 5 Aug 2024 14:18:38 -0700
Subject: [PATCH 1/5] Rename worker threads for improved debuggability

---
 openmp/runtime/src/z_Linux_util.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index 7c90740ae5bdef..91d97372ff5344 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -878,6 +878,15 @@ void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
     KMP_SYSFAIL("pthread_create", status);
   }
 
+#ifdef _GNU_SOURCE
+ #if ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))
+  // Rename worker threads for improved debuggability
+  if (!KMP_UBER_GTID(gtid)) {
+    pthread_setname_np(handle, "openmp_worker");
+  }
+ #endif
+#endif
+
   th->th.th_info.ds.ds_thread = handle;
 
 #ifdef KMP_THREAD_ATTR

>From 115e97e1750d649b5edb30c806293ce1ad37e694 Mon Sep 17 00:00:00 2001
From: h2h <h2h at meta.com>
Date: Tue, 6 Aug 2024 14:54:33 -0700
Subject: [PATCH 2/5] use KMP macro

---
 openmp/runtime/src/z_Linux_util.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index 91d97372ff5344..3c65ae89446c32 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -878,7 +878,7 @@ void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
     KMP_SYSFAIL("pthread_create", status);
   }
 
-#ifdef _GNU_SOURCE
+#ifdef KMP_OS_LINUX
  #if ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))
   // Rename worker threads for improved debuggability
   if (!KMP_UBER_GTID(gtid)) {

>From 6bb262646a5a5b6f9d1c16d45133d3ca0083bc51 Mon Sep 17 00:00:00 2001
From: h2h <h2h at meta.com>
Date: Wed, 7 Aug 2024 09:55:00 -0700
Subject: [PATCH 3/5] fix indent

---
 openmp/runtime/src/z_Linux_util.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index 3c65ae89446c32..dc97f2baedd569 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -879,12 +879,12 @@ void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
   }
 
 #ifdef KMP_OS_LINUX
- #if ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))
+#if ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))
   // Rename worker threads for improved debuggability
   if (!KMP_UBER_GTID(gtid)) {
     pthread_setname_np(handle, "openmp_worker");
   }
- #endif
+#endif
 #endif
 
   th->th.th_info.ds.ds_thread = handle;

>From baac8daa942788852dee1dac78a0933607876457 Mon Sep 17 00:00:00 2001
From: HighW4y2H3ll <zhenghaohuu at gmail.com>
Date: Wed, 7 Aug 2024 14:26:43 -0700
Subject: [PATCH 4/5] simplify the checks

Co-authored-by: Matthias Braun <matze at braunis.de>
---
 openmp/runtime/src/z_Linux_util.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index dc97f2baedd569..02fdc64d38b29c 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -878,13 +878,11 @@ void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
     KMP_SYSFAIL("pthread_create", status);
   }
 
-#ifdef KMP_OS_LINUX
-#if ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))
+#if defined(KMP_OS_LINUX) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))
   // Rename worker threads for improved debuggability
   if (!KMP_UBER_GTID(gtid)) {
     pthread_setname_np(handle, "openmp_worker");
   }
-#endif
 #endif
 
   th->th.th_info.ds.ds_thread = handle;

>From 6c2ab7d90b061c63236532f8e403a53687bc1340 Mon Sep 17 00:00:00 2001
From: h2h <h2h at meta.com>
Date: Thu, 8 Aug 2024 14:04:11 -0700
Subject: [PATCH 5/5] check pthread_setname_np in CMakeLists

---
 openmp/runtime/cmake/LibompDefinitions.cmake | 2 ++
 openmp/runtime/cmake/config-ix.cmake         | 5 +++++
 openmp/runtime/src/z_Linux_util.cpp          | 2 +-
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/openmp/runtime/cmake/LibompDefinitions.cmake b/openmp/runtime/cmake/LibompDefinitions.cmake
index 46beec79f701b1..c894be5870f705 100644
--- a/openmp/runtime/cmake/LibompDefinitions.cmake
+++ b/openmp/runtime/cmake/LibompDefinitions.cmake
@@ -23,6 +23,8 @@ function(libomp_get_definitions_flags cppflags)
   else()
     libomp_append(cppflags_local "-D _GNU_SOURCE")
     libomp_append(cppflags_local "-D _REENTRANT")
+    # or use HAVE_PTHREAD_SETNAME_NP from top-level cmake/config-ix.cmake
+    libomp_append(cppflags_local "-D LIBOMP_HAVE_LINUX_PTHREAD_SETNAME" LIBOMP_HAVE_LINUX_PTHREAD_SETNAME)
   endif()
 
   # CMake doesn't include CPPFLAGS from environment, but we will.
diff --git a/openmp/runtime/cmake/config-ix.cmake b/openmp/runtime/cmake/config-ix.cmake
index 0568474f1d74ed..3adaf5bed68f33 100644
--- a/openmp/runtime/cmake/config-ix.cmake
+++ b/openmp/runtime/cmake/config-ix.cmake
@@ -101,6 +101,11 @@ if(${LIBOMP_FORTRAN_MODULES})
   libomp_check_fortran_flag(-m32 LIBOMP_HAVE_M32_FORTRAN_FLAG)
 endif()
 
+# Check non-posix pthread API here before CMAKE_REQUIRED_DEFINITIONS gets messed up
+if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
+  check_symbol_exists(pthread_setname_np "pthread.h" LIBOMP_HAVE_LINUX_PTHREAD_SETNAME)
+endif()
+
 # Check for Unix shared memory
 check_symbol_exists(shm_open "sys/mman.h" LIBOMP_HAVE_SHM_OPEN_NO_LRT)
 if (NOT LIBOMP_HAVE_SHM_OPEN_NO_LRT)
diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index 02fdc64d38b29c..337a34518da2f6 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -878,7 +878,7 @@ void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
     KMP_SYSFAIL("pthread_create", status);
   }
 
-#if defined(KMP_OS_LINUX) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))
+#ifdef LIBOMP_HAVE_LINUX_PTHREAD_SETNAME
   // Rename worker threads for improved debuggability
   if (!KMP_UBER_GTID(gtid)) {
     pthread_setname_np(handle, "openmp_worker");



More information about the Openmp-commits mailing list