BUG/MINOR: init: properly detect NUMA bindings on large systems

The NUMA detection code tries not to interfer with any taskset the user
could have specified in init scripts. For this it compares the number of
CPUs available with the number the process is bound to. However, the CPU
count is retrieved after being applied an upper bound of MAX_THREADS, so
if the machine has more than 64 CPUs, the comparison always fails and
makes haproxy think the user has already enforced a binding, and it does
not pin it anymore to a single NUMA node.

This can be verified by issuing:

  $ socat /path/to/sock - <<< "show info" | grep thread

On a dual 48-CPU machine it reports 64, implying that threads are allowed
to run on the second socket:

  Nbthread: 64

With this fix, the function properly reports 96, and the output shows 48,
indicating that a single NUMA node was used:

  Nbthread: 48

Of course nothing is changed when "no numa-cpu-mapping" is specified:

  Nbthread: 64

This can be backported to 2.4.

(cherry picked from commit f5b63277f416376b54276d4f0f9ea7999525180e)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit b5ed7e86ca5bbceb6440f5dba8e88513f90f8c0c)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 18521f34fd340db7eb2a71b350e5fe1dba8674b9)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 6f6704621d917a897627d3631c04a406a7ec8d06)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/thread.c b/src/thread.c
index bc655bd..262c3bf 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -169,7 +169,9 @@
 	HA_RWLOCK_INIT(l);
 }
 
-/* returns the number of CPUs the current process is enabled to run on */
+/* returns the number of CPUs the current process is enabled to run on,
+ * regardless of any MAX_THREADS limitation.
+ */
 static int thread_cpus_enabled()
 {
 	int ret = 1;
@@ -190,7 +192,6 @@
 #endif
 #endif
 	ret = MAX(ret, 1);
-	ret = MIN(ret, MAX_THREADS);
 	return ret;
 }
 
@@ -243,6 +244,7 @@
 	preload_libgcc_s();
 
 	thread_cpus_enabled_at_boot = thread_cpus_enabled();
+	thread_cpus_enabled_at_boot = MIN(thread_cpus_enabled_at_boot, MAX_THREADS);
 
 	memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
 		  MAX_THREADS, thread_cpus_enabled_at_boot);