]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: cpu-topo: make sure to properly assign CPUs to threads as a fallback
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Mar 2025 13:04:54 +0000 (14:04 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 14 Mar 2025 17:30:30 +0000 (18:30 +0100)
If no cpu-map is done and no cpu-policy could be enforced, we still need
to count the number of usable CPUs, assign them to all threads and set
the nbthread value accordingly.

This already handles the part that was done in check_config_validity()
via thread_cpus_enabled_at_boot.

src/thread.c

index deb4bc22d179fecd0774950f21122211ed0c1bcd..9d347553f0440532bc8fb7acea7d1b3400686308 100644 (file)
@@ -1613,6 +1613,36 @@ void thread_detect_count(void)
         * capacity order until we reach at least thr_min, then continue
         * on the same cluster _capacity_ up to thr_max.
         */
+
+       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.
+                */
+               struct hap_cpuset node_cpu_set;
+               int thr, cpu, grp, cpu_count;
+
+               ha_cpuset_zero(&node_cpu_set);
+
+               for (cpu = cpu_count = 0; cpu <= cpu_topo_lastcpu; cpu++) {
+                       if (ha_cpu_topo[cpu].st & HA_CPU_F_EXCL_MASK)
+                               continue;
+
+                       ha_cpuset_set(&node_cpu_set, ha_cpu_topo[cpu].idx);
+                       cpu_count++;
+               }
+
+               /* assign all threads of all thread groups to this node */
+               for (grp = 0; grp < MAX_TGROUPS; grp++)
+                       for (thr = 0; thr < MAX_THREADS_PER_GROUP; thr++)
+                               ha_cpuset_assign(&cpu_map[grp].thread[thr], &node_cpu_set);
+
+               /* if the number of CPUs is within the allowed thread range,
+                * automatically set the max thread count to the number of CPUs
+                * as this will be used as the final number of threads.
+                */
+               if (thr_min <= cpu_count && cpu_count <= thr_max)
+                       thr_max = cpu_count;
+       }
 #endif // USE_THREAD && USE_CPU_AFFINITY
 
        if (!global.nbthread)