From 421f02e738999dec9f52665023918e22580197fd Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sat, 20 Jan 2018 18:19:22 +0100 Subject: [PATCH] MINOR: threads: add a MAX_THREADS define instead of LONGBITS This one allows not to inflate some structures when threads are disabled. Now struct global is 1.4 kB instead of 33 kB. Should be backported to 1.8 for ease of backporting of upcoming patches. --- include/common/hathreads.h | 4 ++++ include/types/global.h | 2 +- src/cfgparse.c | 14 +++++++------- src/haproxy.c | 2 +- src/listener.c | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/common/hathreads.h b/include/common/hathreads.h index 5f0b969544..f819536862 100644 --- a/include/common/hathreads.h +++ b/include/common/hathreads.h @@ -30,6 +30,8 @@ extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the threa #ifndef USE_THREAD +#define MAX_THREADS 1 + #define __decl_hathreads(decl) #define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);}) @@ -95,6 +97,8 @@ extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the threa #include #include +#define MAX_THREADS LONGBITS + #define __decl_hathreads(decl) decl /* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to diff --git a/include/types/global.h b/include/types/global.h index 6f3fedc336..5c5cf732e0 100644 --- a/include/types/global.h +++ b/include/types/global.h @@ -168,7 +168,7 @@ struct global { #ifdef USE_CPU_AFFINITY struct { unsigned long proc[LONGBITS]; /* list of CPU masks for the 32/64 first processes */ - unsigned long thread[LONGBITS][LONGBITS]; /* list of CPU masks for the 32/64 first threads per process */ + unsigned long thread[LONGBITS][MAX_THREADS]; /* list of CPU masks for the 32/64 first threads per process */ } cpu_map; #endif }; diff --git a/src/cfgparse.c b/src/cfgparse.c index baf9e60f74..eea820aa5f 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -1175,12 +1175,6 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm) goto out; } global.nbthread = atol(args[1]); - if (global.nbthread < 1 || global.nbthread > LONGBITS) { - ha_alert("parsing [%s:%d] : '%s' must be between 1 and %d (was %d).\n", - file, linenum, args[0], LONGBITS, global.nbthread); - err_code |= ERR_ALERT | ERR_FATAL; - goto out; - } #ifndef USE_THREAD if (global.nbthread > 1) { ha_alert("HAProxy is not compiled with threads support, please check build options for USE_THREAD.\n"); @@ -1189,6 +1183,12 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm) goto out; } #endif + if (global.nbthread < 1 || global.nbthread > MAX_THREADS) { + ha_alert("parsing [%s:%d] : '%s' must be between 1 and %d (was %d).\n", + file, linenum, args[0], MAX_THREADS, global.nbthread); + err_code |= ERR_ALERT | ERR_FATAL; + goto out; + } } else if (!strcmp(args[0], "maxconn")) { if (alertif_too_many_args(1, file, linenum, args, &err_code)) @@ -1801,7 +1801,7 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm) } /* Mapping at the thread level */ - for (j = 0; j < LONGBITS; j++) { + for (j = 0; j < MAX_THREADS; j++) { /* Np mapping for this thread */ if (!(thread & (1UL << j))) continue; diff --git a/src/haproxy.c b/src/haproxy.c index 20b18f8541..a8d0fad87f 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -2986,7 +2986,7 @@ int main(int argc, char **argv) if (global.cpu_map.proc[relative_pid-1]) global.cpu_map.thread[relative_pid-1][i] &= global.cpu_map.proc[relative_pid-1]; - if (i < LONGBITS && /* only the first 32/64 threads may be pinned */ + if (i < MAX_THREADS && /* only the first 32/64 threads may be pinned */ global.cpu_map.thread[relative_pid-1][i]) {/* only do this if the thread has a THREAD map */ #if defined(__FreeBSD__) || defined(__NetBSD__) cpuset_t cpuset; diff --git a/src/listener.c b/src/listener.c index dfd36cca34..fd70726846 100644 --- a/src/listener.c +++ b/src/listener.c @@ -963,7 +963,7 @@ static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct conf->bind_proc |= proc; if (thread) { - for (i = 0; i < LONGBITS; i++) + for (i = 0; i < MAX_THREADS; i++) if (!proc || (proc & (1UL << i))) conf->bind_thread[i] |= thread; } -- 2.39.5