]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: thread: split out thread_bind_to_cpu_map()
authorOlivier Houchard <ohouchard@haproxy.com>
Wed, 7 Oct 2026 12:41:12 +0000 (14:41 +0200)
committerOlivier Houchard <cognet@ci0.org>
Wed, 21 Oct 2026 13:13:20 +0000 (15:13 +0200)
set_thread_cpu_affinity() binds the current thread to the CPUs that
the cpu_map assigns to it. The next commit will need to bind to
another thread's CPUs, around the first touch of an allocation, so
split a thread_bind_to_cpu_map(tgrp, ltid) helper out of it, binding
the current thread to the CPUs assigned to local thread <ltid> of
thread group <tgrp> (0-based). set_thread_cpu_affinity() keeps its
master-process check and delegates to the new function.

include/haproxy/thread.h
src/thread.c

index c97566b92a6b9f743daea84f2433b71047ea5dc3..f3f49daf0fafce871326d2d8d7d695915260f04d 100644 (file)
@@ -205,6 +205,7 @@ void ha_rwlock_init(HA_RWLOCK_T *l);
 void setup_extra_threads(void *(*handler)(void *));
 void wait_for_threads_completion();
 void set_thread_cpu_affinity();
+void thread_bind_to_cpu_map(uint tgrp, uint ltid);
 unsigned long long ha_get_pthread_id(unsigned int thr);
 
 extern volatile unsigned int rdv_requests;
index 6006e8cb2faea5cfb90930fdfda0789d42389bbd..431c0eb5de63fda975aa6f197c5f66c9a08a1675 100644 (file)
@@ -318,19 +318,16 @@ void wait_for_threads_completion()
 #endif
 }
 
-/* Tries to set the current thread's CPU affinity according to the cpu_map */
-void set_thread_cpu_affinity()
+/* Tries to bind the current thread to the CPUs the cpu_map assigns to
+ * the provided thread and thread group.
+ */
+void thread_bind_to_cpu_map(uint tgrp, uint ltid)
 {
 #if defined(USE_CPU_AFFINITY)
-       /* no affinity setting for the master process */
-       if (master)
-               return;
-
-       /* Now the CPU affinity for all threads */
-       if (ha_cpuset_count(&cpu_map[tgid - 1].thread[ti->ltid])) {/* only do this if the thread has a THREAD map */
+       if (ha_cpuset_count(&cpu_map[tgrp].thread[ltid])) {/* only do this if the thread has a THREAD map */
 #  if defined(__APPLE__)
                /* Note: this API is limited to the first 32/64 CPUs */
-               unsigned long set = cpu_map[tgid - 1].thread[ti->ltid].cpuset;
+               unsigned long set = cpu_map[tgrp].thread[ltid].cpuset;
                int j;
 
                while ((j = ffsl(set)) > 0) {
@@ -342,7 +339,7 @@ void set_thread_cpu_affinity()
                        set &= ~(1UL << (j - 1));
                }
 #  else
-               struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid];
+               struct hap_cpuset *set = &cpu_map[tgrp].thread[ltid];
 
                pthread_setaffinity_np(pthread_self(), sizeof(set->cpuset), &set->cpuset);
 #  endif
@@ -350,6 +347,17 @@ void set_thread_cpu_affinity()
 #endif /* USE_CPU_AFFINITY */
 }
 
+/* Tries to set the current thread's CPU affinity according to the cpu_map */
+void set_thread_cpu_affinity()
+{
+       /* no affinity setting for the master process */
+       if (master)
+               return;
+
+       /* Now the CPU affinity for all threads */
+       thread_bind_to_cpu_map(tgid - 1, ti->ltid);
+}
+
 /* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
  * since POSIX took great care of not specifying its representation, making it
  * hard to export for post-mortem analysis. For this reason we copy it into a