From a80823543c15ac51dcfa6c442b77172f544ce60c Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Tue, 6 Apr 2021 16:46:15 +0200 Subject: [PATCH] MINOR: cfgparse: support the comma separator on parse_cpu_set Allow to specify multiple cpu ids/ranges in parse_cpu_set separated by a comma. This is optional and must be activated by a parameter. The comma support is disabled for the parsing of the 'cpu-map' config statement. However, it will be useful to parse files in sysfs when inspecting the cpus topology for NUMA automatic process binding. --- include/haproxy/cfgparse.h | 2 +- src/cfgparse-global.c | 2 +- src/cfgparse.c | 29 ++++++++++++++++++++--------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/haproxy/cfgparse.h b/include/haproxy/cfgparse.h index 8af4e47cd1..2266ac6d92 100644 --- a/include/haproxy/cfgparse.h +++ b/include/haproxy/cfgparse.h @@ -114,7 +114,7 @@ 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); -unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err); +unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, int comma_allowed, 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); diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c index 694cd70776..89e3b10b0a 100644 --- a/src/cfgparse-global.c +++ b/src/cfgparse-global.c @@ -1070,7 +1070,7 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm) } } - if (parse_cpu_set((const char **)args+2, &cpus, &errmsg)) { + if (parse_cpu_set((const char **)args+2, &cpus, 0, &errmsg)) { ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); err_code |= ERR_ALERT | ERR_FATAL; goto out; diff --git a/src/cfgparse.c b/src/cfgparse.c index dd8a2c13cf..f37b74894c 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -462,26 +462,35 @@ int parse_process_number(const char *arg, unsigned long *proc, int max, int *aut #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 - * ('-'). Multiple CPU numbers or ranges may be specified. On success, it - * returns 0. otherwise it returns 1 with an error message in . + * ('-'). If is set, 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 . */ -unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err) +unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, + int comma_allowed, char **err) { int cur_arg = 0; + const char *arg; ha_cpuset_zero(cpu_set); - while (*args[cur_arg]) { - char *dash; + 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.", args[cur_arg]); + memprintf(err, "'%s' is not a CPU range.", arg); return -1; } - low = high = str2uic(args[cur_arg]); - if ((dash = strchr(args[cur_arg], '-')) != NULL) + low = high = str2uic(arg); + + comma = comma_allowed ? strchr(arg, ',') : NULL; + dash = strchr(arg, '-'); + + if (dash && (!comma || dash < comma)) high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1; if (high < low) { @@ -499,7 +508,9 @@ unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char while (low <= high) ha_cpuset_set(cpu_set, low++); - cur_arg++; + /* 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; } -- 2.39.5