]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: config: make MAX_PROCS configurable at build time
authorWilly Tarreau <w@1wt.eu>
Thu, 7 Feb 2019 09:39:36 +0000 (10:39 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 7 Feb 2019 14:10:19 +0000 (15:10 +0100)
For some embedded systems, it's pointless to have 32- or even 64- large
arrays of processes when it's known that much fewer processes will be
used in the worst case. Let's introduce this MAX_PROCS define which
contains the highest number of processes allowed to run at once. It
still defaults to LONGBITS but may be lowered.

include/common/defaults.h
include/types/global.h
include/types/listener.h
src/cfgparse-global.c
src/cfgparse-listen.c
src/cfgparse.c
src/cli.c
src/haproxy.c
src/listener.c

index b2c2583540a353d16094bd2ab0ec3b46d3cf5001..cdc66650bb4af23552e126ba45715af194e894bb 100644 (file)
 #ifndef _COMMON_DEFAULTS_H
 #define _COMMON_DEFAULTS_H
 
+/* MAX_PROCS defines the highest limit for the global "nbproc" value. It
+ * defaults to the number of bits in a long integer but may be lowered to save
+ * resources on embedded systems.
+ */
+#ifndef MAX_PROCS
+#define MAX_PROCS LONGBITS
+#endif
+
 /*
  * BUFSIZE defines the size of a read and write buffer. It is the maximum
  * amount of bytes which can be stored by the proxy for each stream. However,
index 178b2c14dab257dc8e463908c1837be8971c34b4..b26553bc28d83c8d5f54c26f58ba6257cac24795 100644 (file)
@@ -173,8 +173,8 @@ struct global {
        struct vars   vars;         /* list of variables for the process scope. */
 #ifdef USE_CPU_AFFINITY
        struct {
-               unsigned long proc[LONGBITS];             /* list of CPU masks for the 32/64 first processes */
-               unsigned long thread[LONGBITS][MAX_THREADS]; /* list of CPU masks for the 32/64 first threads per process */
+               unsigned long proc[MAX_PROCS];             /* list of CPU masks for the 32/64 first processes */
+               unsigned long thread[MAX_PROCS][MAX_THREADS]; /* list of CPU masks for the 32/64 first threads per process */
        } cpu_map;
 #endif
 };
index df0dd367c31e875215e70b2241bb0341a4b42400..9c0ebde0693a7cc44a0b04c5496dd4dc750d384b 100644 (file)
@@ -166,7 +166,7 @@ struct bind_conf {
        int is_ssl;                /* SSL is required for these listeners */
        int generate_certs;        /* 1 if generate-certificates option is set, else 0 */
        unsigned long bind_proc;   /* bitmask of processes allowed to use these listeners */
-       unsigned long bind_thread[LONGBITS]; /* bitmask of threads (per processes) allowed to use these listeners */
+       unsigned long bind_thread[MAX_PROCS]; /* bitmask of threads (per processes) allowed to use these listeners */
        struct {                   /* UNIX socket permissions */
                uid_t uid;         /* -1 to leave unchanged */
                gid_t gid;         /* -1 to leave unchanged */
index 820c7b6a0635932a8f789af897481866280d149b..e1835e7b687ad3a3d7a2c2ad4399c49389c62a8c 100644 (file)
@@ -490,9 +490,9 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                }
                global.nbproc = atol(args[1]);
                all_proc_mask = nbits(global.nbproc);
-               if (global.nbproc < 1 || global.nbproc > LONGBITS) {
+               if (global.nbproc < 1 || global.nbproc > MAX_PROCS) {
                        ha_alert("parsing [%s:%d] : '%s' must be between 1 and %d (was %d).\n",
-                                file, linenum, args[0], LONGBITS, global.nbproc);
+                                file, linenum, args[0], MAX_PROCS, global.nbproc);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }
@@ -942,7 +942,7 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                        ha_alert("parsing [%s:%d] : %s expects a process number "
                                 " ('all', 'odd', 'even', a number from 1 to %d or a range), "
                                 " followed by a list of CPU ranges with numbers from 0 to %d.\n",
-                                file, linenum, args[0], LONGBITS, LONGBITS - 1);
+                                file, linenum, args[0], MAX_PROCS, LONGBITS - 1);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }
@@ -950,7 +950,7 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                if ((slash = strchr(args[1], '/')) != NULL)
                        *slash = 0;
 
-               if (parse_process_number(args[1], &proc, LONGBITS, &autoinc, &errmsg)) {
+               if (parse_process_number(args[1], &proc, MAX_PROCS, &autoinc, &errmsg)) {
                        ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
@@ -989,7 +989,7 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                        goto out;
                }
 
-               for (i = n = 0; i < LONGBITS; i++) {
+               for (i = n = 0; i < MAX_PROCS; i++) {
                        /* No mapping for this process */
                        if (!(proc & (1UL << i)))
                                continue;
index 74218b5e65cbdb1d185546d2fe888bac36135906..5f44cfdf02cc839d6d684bb93562472a96f344c9 100644 (file)
@@ -922,7 +922,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
                                set = 0;
                                break;
                        }
-                       if (parse_process_number(args[cur_arg], &set, LONGBITS, NULL, &errmsg)) {
+                       if (parse_process_number(args[cur_arg], &set, MAX_PROCS, NULL, &errmsg)) {
                                ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
                                err_code |= ERR_ALERT | ERR_FATAL;
                                goto out;
index 1afb49c89a6843d3791539963ba1d35cf35325ea..d7d18c6b125622ac89b7f2576dcea7fee06f2369 100644 (file)
@@ -2296,7 +2296,7 @@ int check_config_validity()
                                        mask >>= global.nbthread;
                                }
 
-                               for (nbproc = 0; nbproc < LONGBITS; nbproc++) {
+                               for (nbproc = 0; nbproc < MAX_PROCS; nbproc++) {
                                        if (!bind_conf->bind_proc || (bind_conf->bind_proc & (1UL << nbproc)))
                                                bind_conf->bind_thread[nbproc] = new_mask;
                                }
index d1d1e9625d08ecece35bda6fa92f3f2d2ff919c4..3c2a55d75da89d0bd3d56ed81e804ecf94ad750e 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -359,7 +359,7 @@ static int stats_parse_global(char **args, int section_type, struct proxy *curpx
                                set = 0;
                                break;
                        }
-                       if (parse_process_number(args[cur_arg], &set, LONGBITS, NULL, err)) {
+                       if (parse_process_number(args[cur_arg], &set, MAX_PROCS, NULL, err)) {
                                memprintf(err, "'%s %s' : %s", args[0], args[1], *err);
                                return -1;
                        }
index ce4cde3111ad7e39749dac0e059406b4922c6fbf..7c434b361f6d6f901b220c50ecf6014f6f2f1dca 100644 (file)
@@ -2739,6 +2739,16 @@ int main(int argc, char **argv)
 
        setvbuf(stdout, NULL, _IONBF, 0);
 
+       /* this can only safely be done here, though it's optimized away by
+        * the compiler.
+        */
+       if (MAX_PROCS < 1 || MAX_PROCS > LONGBITS) {
+               ha_alert("MAX_PROCS value must be between 1 and %d inclusive; "
+                        "HAProxy was built with value %d, please fix it and rebuild.\n",
+                        LONGBITS, MAX_PROCS);
+               exit(1);
+       }
+
        /* process all initcalls in order of potential dependency */
        RUN_INITCALLS(STG_PREPARE);
        RUN_INITCALLS(STG_LOCK);
@@ -3059,7 +3069,7 @@ int main(int argc, char **argv)
 
 #ifdef USE_CPU_AFFINITY
                if (proc < global.nbproc &&  /* child */
-                   proc < LONGBITS &&       /* only the first 32/64 processes may be pinned */
+                   proc < MAX_PROCS &&       /* only the first 32/64 processes may be pinned */
                    global.cpu_map.proc[proc])    /* only do this if the process has a CPU map */
 #ifdef __FreeBSD__
                {
index cb8cbda1d2fc70b0286d1923a72d00e42fabf6f6..e9ace41f52ec21eaff27411072906b25c402387c 100644 (file)
@@ -962,7 +962,7 @@ static int bind_parse_process(char **args, int cur_arg, struct proxy *px, struct
        if ((slash = strchr(args[cur_arg + 1], '/')) != NULL)
                *slash = 0;
 
-       if (parse_process_number(args[cur_arg + 1], &proc, LONGBITS, NULL, err)) {
+       if (parse_process_number(args[cur_arg + 1], &proc, MAX_PROCS, NULL, err)) {
                memprintf(err, "'%s' : %s", args[cur_arg], *err);
                return ERR_ALERT | ERR_FATAL;
        }
@@ -977,7 +977,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_PROCS; i++)
                        if (!proc || (proc & (1UL << i)))
                                conf->bind_thread[i] |= thread;
        }