REORG: cpuset: move parse_cpu_set() and parse_cpumap() to cpuset.c

These ones were still in cfgparse.c but they're not specific to the
config at all and may actually be used even when parsing cpu list
entries in /sys. Better move them where they can be reused.
This commit is contained in:
Willy Tarreau 2023-07-06 14:35:11 +02:00
parent 5119109e3f
commit 4a18d9e560
4 changed files with 103 additions and 89 deletions

View File

@ -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);

View File

@ -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 <args>. On success, it returns 0, otherwise it returns
* 1 with an error message in <err>.
*/
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.
*/

View File

@ -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 <args>. On success, it returns 0, otherwise it returns
* 1 with an error message in <err>.
*/
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 <file> at line <linenum> with <id> 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

View File

@ -1,9 +1,11 @@
#define _GNU_SOURCE
#include <sched.h>
#include <ctype.h>
#include <haproxy/compat.h>
#include <haproxy/cpuset.h>
#include <haproxy/intops.h>
#include <haproxy/tools.h>
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 <args>. On success, it returns 0, otherwise it returns
* 1, optionally with an error message in <err> if <err> 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.
*/