diff --git a/include/haproxy/cfgparse.h b/include/haproxy/cfgparse.h index 20d72972d8..adcabb397e 100644 --- a/include/haproxy/cfgparse.h +++ b/include/haproxy/cfgparse.h @@ -126,7 +126,6 @@ int too_many_args(int maxarg, char **args, char **msg, int *err_code); int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code); int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code); int parse_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err); -int parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err); void free_email_alert(struct proxy *p); const char *cfg_find_best_match(const char *word, const struct list *list, int section, const char **extra); int warnifnotcap(struct proxy *proxy, int cap, const char *file, int line, const char *arg, const char *hint); diff --git a/include/haproxy/cpuset.h b/include/haproxy/cpuset.h index e4811bcd6c..87c4ece828 100644 --- a/include/haproxy/cpuset.h +++ b/include/haproxy/cpuset.h @@ -53,6 +53,21 @@ int ha_cpuset_size(void); */ int ha_cpuset_detect_bound(struct hap_cpuset *set); +/* Parse cpu sets. Each CPU set is either a unique number between 0 and + * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash + * ('-'). Each CPU set can be a list of unique numbers or ranges separated by + * a comma. It is also possible to specify multiple cpu numbers or ranges in + * distinct argument in . On success, it returns 0, otherwise it returns + * 1 with an error message in . + */ +int parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err); + +/* Parse a linux cpu map string representing to a numeric cpu mask map + * The cpu map string is a list of 4-byte hex strings separated by commas, with + * most-significant byte first, one bit per cpu number. + */ +void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set); + /* Returns true if at least one cpu-map directive was configured, otherwise * false. */ diff --git a/src/cfgparse.c b/src/cfgparse.c index bb7fb71ed7..42ff8a8025 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -520,62 +520,6 @@ int parse_process_number(const char *arg, unsigned long *proc, int max, int *aut return 0; } -#ifdef USE_CPU_AFFINITY -/* Parse cpu sets. Each CPU set is either a unique number between 0 and - * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash - * ('-'). Each CPU set can be a list of unique numbers or ranges separated by - * a comma. It is also possible to specify multiple cpu numbers or ranges in - * distinct argument in . On success, it returns 0, otherwise it returns - * 1 with an error message in . - */ -int parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err) -{ - int cur_arg = 0; - const char *arg; - - ha_cpuset_zero(cpu_set); - - arg = args[cur_arg]; - while (*arg) { - const char *dash, *comma; - unsigned int low, high; - - if (!isdigit((unsigned char)*args[cur_arg])) { - memprintf(err, "'%s' is not a CPU range.", arg); - return 1; - } - - low = high = str2uic(arg); - - comma = strchr(arg, ','); - dash = strchr(arg, '-'); - - if (dash && (!comma || dash < comma)) - high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1; - - if (high < low) { - unsigned int swap = low; - low = high; - high = swap; - } - - if (high >= ha_cpuset_size()) { - memprintf(err, "supports CPU numbers from 0 to %d.", - ha_cpuset_size() - 1); - return 1; - } - - while (low <= high) - ha_cpuset_set(cpu_set, low++); - - /* if a comma is present, parse the rest of the arg, else - * skip to the next arg */ - arg = comma ? comma + 1 : args[++cur_arg]; - } - return 0; -} -#endif - /* Allocate and initialize the frontend of a "peers" section found in * file at line with as ID. * Return 0 if succeeded, -1 if not. @@ -2585,38 +2529,6 @@ static int numa_filter(const struct dirent *dir) return 1; } -/* Parse a linux cpu map string representing to a numeric cpu mask map - * The cpu map string is a list of 4-byte hex strings separated by commas, with - * most-significant byte first, one bit per cpu number. - */ -static void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set) -{ - unsigned long cpumap; - char *start, *endptr, *comma; - int i, j; - - ha_cpuset_zero(cpu_set); - - i = 0; - do { - /* reverse-search for a comma, parse the string after the comma - * or at the beginning if no comma found - */ - comma = strrchr(cpumap_str, ','); - start = comma ? comma + 1 : cpumap_str; - - cpumap = strtoul(start, &endptr, 16); - for (j = 0; cpumap; cpumap >>= 1, ++j) { - if (cpumap & 0x1) - ha_cpuset_set(cpu_set, j + i * 32); - } - - if (comma) - *comma = '\0'; - ++i; - } while (comma); -} - /* Inspect the cpu topology of the machine on startup. If a multi-socket * machine is detected, try to bind on the first node with active cpu. This is * done to prevent an impact on the overall performance when the topology of diff --git a/src/cpuset.c b/src/cpuset.c index 3ce6cf2231..82e350f132 100644 --- a/src/cpuset.c +++ b/src/cpuset.c @@ -1,9 +1,11 @@ #define _GNU_SOURCE #include +#include #include #include #include +#include struct cpu_map *cpu_map; @@ -171,6 +173,92 @@ int ha_cpuset_detect_bound(struct hap_cpuset *set) return ha_cpuset_count(set); } +/* Parse cpu sets. Each CPU set is either a unique number between 0 and + * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash + * ('-'). Each CPU set can be a list of unique numbers or ranges separated by + * a comma. It is also possible to specify multiple cpu numbers or ranges in + * distinct argument in . On success, it returns 0, otherwise it returns + * 1, optionally with an error message in if is not NULL. + */ +int parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err) +{ + int cur_arg = 0; + const char *arg; + + ha_cpuset_zero(cpu_set); + + arg = args[cur_arg]; + while (*arg) { + const char *dash, *comma; + unsigned int low, high; + + if (!isdigit((unsigned char)*args[cur_arg])) { + memprintf(err, "'%s' is not a CPU range.", arg); + return 1; + } + + low = high = str2uic(arg); + + comma = strchr(arg, ','); + dash = strchr(arg, '-'); + + if (dash && (!comma || dash < comma)) + high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1; + + if (high < low) { + unsigned int swap = low; + low = high; + high = swap; + } + + if (high >= ha_cpuset_size()) { + memprintf(err, "supports CPU numbers from 0 to %d.", + ha_cpuset_size() - 1); + return 1; + } + + while (low <= high) + ha_cpuset_set(cpu_set, low++); + + /* if a comma is present, parse the rest of the arg, else + * skip to the next arg */ + arg = comma ? comma + 1 : args[++cur_arg]; + } + return 0; +} + +/* Parse a linux cpu map string representing to a numeric cpu mask map + * The cpu map string is a list of 4-byte hex strings separated by commas, with + * most-significant byte first, one bit per cpu number. + */ +void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set) +{ + unsigned long cpumap; + char *start, *endptr, *comma; + int i, j; + + ha_cpuset_zero(cpu_set); + + i = 0; + do { + /* reverse-search for a comma, parse the string after the comma + * or at the beginning if no comma found + */ + comma = strrchr(cpumap_str, ','); + start = comma ? comma + 1 : cpumap_str; + + cpumap = strtoul(start, &endptr, 16); + for (j = 0; cpumap; cpumap >>= 1, ++j) { + if (cpumap & 0x1) + ha_cpuset_set(cpu_set, j + i * 32); + } + + if (comma) + *comma = '\0'; + ++i; + } while (comma); +} + /* Returns true if at least one cpu-map directive was configured, otherwise * false. */