]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: cfgparse: numa detect topology on FreeBSD.
authorDavid CARLIER <devnexen@gmail.com>
Mon, 6 Dec 2021 11:00:10 +0000 (11:00 +0000)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 15 Dec 2021 10:05:51 +0000 (11:05 +0100)
allowing for all platforms supporting cpu affinity to have a chance
 to detect the cpu topology from a given valid node (e.g.
 DragonflyBSD seems to be NUMA aware from a kernel's perspective
 and seems to be willing start to provide userland means to get
 proper info).

include/haproxy/cpuset-t.h
src/cfgparse.c

index 541fb75cd9e8ac55dcd3c0192de4f97242c50c4c..b26da724545a66aea62bc3d36c1a4b2e180cfa00 100644 (file)
@@ -9,6 +9,7 @@
 #ifdef __FreeBSD__
 #include <sys/_cpuset.h>
 #include <sys/cpuset.h>
+#include <sys/sysctl.h>
 #include <strings.h>
 #endif
 #endif
index 7b350529d1ad10d30d53bd4f81b2732ac67877c1..77af77323b80aea7055e327bc4286cf9c5728543 100644 (file)
@@ -2375,6 +2375,50 @@ static int numa_detect_topology()
        return ha_cpuset_count(&node_cpu_set);
 }
 
+#elif defined(__FreeBSD__)
+static int numa_detect_topology()
+{
+       struct hap_cpuset node_cpu_set;
+       int ndomains = 0, i;
+       size_t len = sizeof(ndomains);
+
+       if (sysctlbyname("vm.ndomains", &ndomains, &len, NULL, 0) == -1) {
+               ha_notice("Cannot assess the number of CPUs domains\n");
+               return 0;
+       }
+
+       BUG_ON(ndomains > MAXMEMDOM);
+       ha_cpuset_zero(&node_cpu_set);
+
+       /*
+        * We retrieve the first active valid CPU domain
+        * with active cpu and binding it, we returns
+        * the number of cpu from the said domain
+        */
+       for (i = 0; i < ndomains; i ++) {
+               struct hap_cpuset dom;
+               ha_cpuset_zero(&dom);
+               if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_DOMAIN, i, sizeof(dom.cpuset), &dom.cpuset) == -1)
+                       continue;
+
+               if (!ha_cpuset_count(&dom))
+                       continue;
+
+               ha_cpuset_assign(&node_cpu_set, &dom);
+
+               ha_diag_warning("Multi-socket cpu detected, automatically binding on active CPUs of '%d' (%u active cpu(s))\n", i, ha_cpuset_count(&node_cpu_set));
+               if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(node_cpu_set.cpuset), &node_cpu_set.cpuset) == -1) {
+                       ha_warning("Cannot set the cpu affinity for this multi-cpu machine\n");
+
+                       /* clear the cpuset used as return value */
+                       ha_cpuset_zero(&node_cpu_set);
+               }
+               break;
+       }
+
+       return ha_cpuset_count(&node_cpu_set);
+}
+
 #else
 static int numa_detect_topology()
 {