]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cpu-topo: allocate and initialize the ha_cpu_topo array.
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Jul 2023 13:48:27 +0000 (15:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 14 Mar 2025 17:30:30 +0000 (18:30 +0100)
This does the bare minimum to allocate and initialize a global
ha_cpu_topo array for the number of supported CPUs and release
it at deinit time.

Makefile
include/haproxy/cpu_topo.h [new file with mode: 0644]
src/cpu_topo.c [new file with mode: 0644]

index 759f91f72f9ebfa17253a71ad7433c3939f91822..72a696b2a1c7e4bfd11d9f5c397e11add44bf1bf 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -596,6 +596,7 @@ endif
 
 ifneq ($(USE_CPU_AFFINITY:0=),)
   OPTIONS_OBJS   += src/cpuset.o
+  OPTIONS_OBJS   += src/cpu_topo.o
 endif
 
 # OpenSSL is packaged in various forms and with various dependencies.
diff --git a/include/haproxy/cpu_topo.h b/include/haproxy/cpu_topo.h
new file mode 100644 (file)
index 0000000..139bc81
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef _HAPROXY_CPU_TOPO_H
+#define _HAPROXY_CPU_TOPO_H
+
+#include <haproxy/api.h>
+#include <haproxy/cpuset.h>
+#include <haproxy/cpu_topo-t.h>
+
+extern struct ha_cpu_topo *ha_cpu_topo;
+
+#endif /* _HAPROXY_CPU_TOPO_H */
diff --git a/src/cpu_topo.c b/src/cpu_topo.c
new file mode 100644 (file)
index 0000000..7d60617
--- /dev/null
@@ -0,0 +1,40 @@
+#define _GNU_SOURCE
+
+#include <haproxy/api.h>
+#include <haproxy/cpu_topo.h>
+
+/* CPU topology information, ha_cpuset_size() entries, allocated at boot */
+struct ha_cpu_topo *ha_cpu_topo = NULL;
+
+/* Allocates everything needed to store CPU topology at boot.
+ * Returns non-zero on success, zero on failure.
+ */
+static int cpu_topo_alloc(void)
+{
+       int maxcpus = ha_cpuset_size();
+       int cpu;
+
+       /* allocate the structures used to store CPU topology info */
+       ha_cpu_topo = (struct ha_cpu_topo*)malloc(maxcpus * sizeof(*ha_cpu_topo));
+       if (!ha_cpu_topo)
+               return 0;
+
+       /* preset all fields to -1 except the index and the state flags which
+        * are assumed to all be bound and online unless detected otherwise.
+        */
+       for (cpu = 0; cpu < maxcpus; cpu++) {
+               memset(&ha_cpu_topo[cpu], 0xff, sizeof(*ha_cpu_topo));
+               ha_cpu_topo[cpu].st  = 0;
+               ha_cpu_topo[cpu].idx = cpu;
+       }
+
+       return 1;
+}
+
+static void cpu_topo_deinit(void)
+{
+       ha_free(&ha_cpu_topo);
+}
+
+INITCALL0(STG_ALLOC, cpu_topo_alloc);
+REGISTER_POST_DEINIT(cpu_topo_deinit);