From 5119109e3feb25a1f9d5acbc14375047c0446f79 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 12 Jul 2023 11:35:32 +0200 Subject: [PATCH] MINOR: cpuset: dynamically allocate cpu_map cpu_map is 8.2kB/entry and there's one such entry per group, that's ~520kB total. In addition, the init code is still in haproxy.c enclosed in ifdefs. Let's make this a dynamically allocated array in the cpuset code and remove that init code. Later we may even consider reallocating it once the number of threads and groups is known, in order to shrink it a little bit, as the typical setup with a single group will only need 8.2kB, thus saving half a MB of RAM. This would require that the upper bound is placed in a variable though. --- include/haproxy/cpuset.h | 2 +- src/cpuset.c | 23 ++++++++++++++++++++++- src/haproxy.c | 13 ------------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/include/haproxy/cpuset.h b/include/haproxy/cpuset.h index 78c4df2707..e4811bcd6c 100644 --- a/include/haproxy/cpuset.h +++ b/include/haproxy/cpuset.h @@ -3,7 +3,7 @@ #include -extern struct cpu_map cpu_map[MAX_TGROUPS]; +extern struct cpu_map *cpu_map; /* Unset all indexes in . */ diff --git a/src/cpuset.c b/src/cpuset.c index 73b97b9759..3ce6cf2231 100644 --- a/src/cpuset.c +++ b/src/cpuset.c @@ -5,7 +5,7 @@ #include #include -struct cpu_map cpu_map[MAX_TGROUPS]; +struct cpu_map *cpu_map; void ha_cpuset_zero(struct hap_cpuset *set) { @@ -185,3 +185,24 @@ int cpu_map_configured(void) } return 0; } + +/* Allocates everything needed to store CPU information at boot. + * Returns non-zero on success, zero on failure. + */ +static int cpuset_alloc(void) +{ + /* allocate the structures used to store CPU topology info */ + cpu_map = (struct cpu_map*)calloc(MAX_TGROUPS, sizeof(*cpu_map)); + if (!cpu_map) + return 0; + + return 1; +} + +static void cpuset_deinit(void) +{ + ha_free(&cpu_map); +} + +INITCALL0(STG_ALLOC, cpuset_alloc); +REGISTER_POST_DEINIT(cpuset_deinit); diff --git a/src/haproxy.c b/src/haproxy.c index 18308bc59a..383d1e47dc 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -1530,19 +1530,6 @@ static void init_early(int argc, char **argv) exit(EXIT_FAILURE); } - /* Some CPU affinity stuff may have to be initialized */ -#ifdef USE_CPU_AFFINITY - { - int g, i; - - for (g = 0; g < MAX_TGROUPS; g++) { - for (i = 0; i < MAX_THREADS_PER_GROUP; ++i) { - ha_cpuset_zero(&cpu_map[g].thread[i]); - } - } - } -#endif - /* extract the program name from argv[0], it will be used for the logs * and error messages. */ -- 2.47.3