Hi,

Here a little patch proposal to give names to threads for debug builds.
If accepted it is not crucial can be seen after the soon 2.0 release.

Thanks.

Kind regards.
From 6b1e5d77e0ef8c29673a5da4b22978c052394a9c Mon Sep 17 00:00:00 2001
From: David Carlier <David Carlier devne...@gmail.com>
Date: Fri, 14 Jun 2019 10:00:06 +0000
Subject: [PATCH] BUILD/MINOR: hathread

For full debug builds, giving names to threads, for Linux and
FreeBSD.
---
 include/common/hathreads.h |  3 +++
 src/haproxy.c              |  4 +++-
 src/hathreads.c            | 15 +++++++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/include/common/hathreads.h b/include/common/hathreads.h
index a7c8dc93..d9e757f9 100644
--- a/include/common/hathreads.h
+++ b/include/common/hathreads.h
@@ -189,6 +189,8 @@ static inline void ha_tkillall(int sig)
 	raise(sig);
 }
 
+static inline void ha_tname(unsigned int);
+
 static inline void __ha_barrier_atomic_load(void)
 {
 }
@@ -425,6 +427,7 @@ void thread_release();
 void thread_sync_release();
 void ha_tkill(unsigned int thr, int sig);
 void ha_tkillall(int sig);
+void ha_tname(unsigned int thr);
 
 extern struct thread_info {
 	pthread_t pthread;
diff --git a/src/haproxy.c b/src/haproxy.c
index 9821765a..95a0efe2 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -3270,8 +3270,10 @@ int main(int argc, char **argv)
 
 		/* Create nbthread-1 thread. The first thread is the current process */
 		thread_info[0].pthread = pthread_self();
-		for (i = 1; i < global.nbthread; i++)
+		for (i = 1; i < global.nbthread; i++) {
 			pthread_create(&thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i);
+			ha_tname(i);
+		}
 
 #ifdef USE_CPU_AFFINITY
 		/* Now the CPU affinity for all threads */
diff --git a/src/hathreads.c b/src/hathreads.c
index 69385236..100f2271 100644
--- a/src/hathreads.c
+++ b/src/hathreads.c
@@ -21,6 +21,7 @@
 
 #ifdef __FreeBSD__
 #include <sys/cpuset.h>
+#include <pthread_np.h>
 #endif
 
 #include <common/cfgparse.h>
@@ -154,6 +155,20 @@ void ha_tkillall(int sig)
 	raise(sig);
 }
 
+void ha_tname(unsigned int thr)
+{
+#if defined(DEBUG_FULL) && (defined(__linux__) || defined(__FreeBSD__))
+	char buf[16] = {0};
+	snprintf(buf, sizeof(buf), "haproxy%u", thr);
+	buf[sizeof(buf)-1] = 0;
+#if defined(__linux__)
+	pthread_setname_np(thread_info[thr].pthread, buf);
+#elif defined(__FreeBSD__)
+	pthread_set_name_np(thread_info[thr].pthread, buf);
+#endif
+#endif
+}
+
 /* these calls are used as callbacks at init time */
 void ha_spin_init(HA_SPINLOCK_T *l)
 {
-- 
2.21.0

Reply via email to