]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: thread: reimplement first numa node detection
authorWilly Tarreau <w@1wt.eu>
Thu, 20 Jul 2023 14:40:25 +0000 (16:40 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 14 Mar 2025 17:30:30 +0000 (18:30 +0100)
Let's reimplement automatic binding to the first NUMA node when thread
count is not forced. It's the same thing as is already done in
check_config_validity() except that this time it's based on the
collected CPU information. The threads are automatically counted
and CPUs from non-first node(s) are evicted.

src/thread.c

index 9d347553f0440532bc8fb7acea7d1b3400686308..0227d4a16f921a5af1897a663c825a288211733c 100644 (file)
@@ -1614,6 +1614,43 @@ void thread_detect_count(void)
         * on the same cluster _capacity_ up to thr_max.
         */
 
+       /* Let's implement here the automatic binding to the first available
+        * NUMA node when thread count is not set, taskset is not used and
+        * no cpu-map directive is present.
+        */
+
+       if (global.numa_cpu_mapping && !global.nbthread && !cpu_mask_forced && !cpu_map_configured()) {
+               int first_node_id = -1;
+               int second_node_id = -1;
+               int cpu_count = 0;
+
+               /* determine first node with usable CPUs */
+               for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++) {
+                       if (ha_cpu_topo[cpu].st & HA_CPU_F_EXCL_MASK)
+                               continue;
+
+                       if (ha_cpu_topo[cpu].no_id >= 0) {
+                               if (first_node_id < 0)
+                                       first_node_id = ha_cpu_topo[cpu].no_id;
+                               else {
+                                       second_node_id = ha_cpu_topo[cpu].no_id;
+                                       break;
+                               }
+                       }
+               }
+
+               if (second_node_id >= 0) {
+                       /* ignore all CPUs of other nodes, and count them */
+                       for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++) {
+                               if (ha_cpu_topo[cpu].no_id != first_node_id)
+                                       ha_cpu_topo[cpu].st |= HA_CPU_F_IGNORED;
+                               else if (!(ha_cpu_topo[cpu].st & HA_CPU_F_EXCL_MASK))
+                                       cpu_count++;
+                       }
+                       ha_diag_warning("Multi-socket cpu detected, automatically binding on active CPUs of '%d' (%u active cpu(s))\n", first_node_id, cpu_count);
+               }
+       }
+
        if (!ha_cpuset_count(&cpu_map[0].thread[0])) {
                /* thread 1 is not mapped, no policy was applied, so we have to
                 * count the threads ourselves.