From: Olivier Houchard Date: Wed, 7 Oct 2026 12:41:12 +0000 (+0200) Subject: MINOR: thread: split out thread_bind_to_cpu_map() X-Git-Tag: v3.5-dev10~94 X-Git-Url: https://git.kaiwu.me/?a=commitdiff_plain;h=32003c9a913cbad590d3fc4471a520bd90231f8c;p=haproxy.git MINOR: thread: split out thread_bind_to_cpu_map() 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 of thread group (0-based). set_thread_cpu_affinity() keeps its master-process check and delegates to the new function. --- diff --git a/include/haproxy/thread.h b/include/haproxy/thread.h index c97566b92..f3f49daf0 100644 --- a/include/haproxy/thread.h +++ b/include/haproxy/thread.h @@ -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; diff --git a/src/thread.c b/src/thread.c index 6006e8cb2..431c0eb5d 100644 --- a/src/thread.c +++ b/src/thread.c @@ -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 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