From 656cedad424d5093647fea31205bb0528c956f73 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 11 Jul 2023 15:48:27 +0200 Subject: [PATCH] MINOR: cpu-topo: allocate and initialize the ha_cpu_topo array. 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 | 1 + include/haproxy/cpu_topo.h | 10 ++++++++++ src/cpu_topo.c | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 include/haproxy/cpu_topo.h create mode 100644 src/cpu_topo.c diff --git a/Makefile b/Makefile index 759f91f72..72a696b2a 100644 --- 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 index 000000000..139bc81ec --- /dev/null +++ b/include/haproxy/cpu_topo.h @@ -0,0 +1,10 @@ +#ifndef _HAPROXY_CPU_TOPO_H +#define _HAPROXY_CPU_TOPO_H + +#include +#include +#include + +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 index 000000000..7d60617ae --- /dev/null +++ b/src/cpu_topo.c @@ -0,0 +1,40 @@ +#define _GNU_SOURCE + +#include +#include + +/* 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);