MINOR: cfgparse: use hap_cpuset for parse_cpu_set

Replace the unsigned long parameter by a hap_cpuset. This allows to
address CPU with index greater than LONGBITS.

This function is used to parse the 'cpu-map' statement. However at the
moment, the result is casted back to a long to store it in the global
structure. The next step is to replace ulong in in cpu_map in the
global structure with hap_cpuset.
This commit is contained in:
Amaury Denoyelle 2021-04-14 16:16:03 +02:00
parent f75c640f7b
commit c90932bc8e
3 changed files with 31 additions and 12 deletions

View File

@ -26,6 +26,8 @@
#include <haproxy/errors.h> #include <haproxy/errors.h>
#include <haproxy/proxy.h> #include <haproxy/proxy.h>
struct hap_cpuset;
/* configuration sections */ /* configuration sections */
#define CFG_NONE 0 #define CFG_NONE 0
#define CFG_GLOBAL 1 #define CFG_GLOBAL 1
@ -112,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_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 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_process_number(const char *arg, unsigned long *proc, int max, int *autoinc, char **err);
unsigned long parse_cpu_set(const char **args, unsigned long *cpu_set, char **err); unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err);
void free_email_alert(struct proxy *p); 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); const char *cfg_find_best_match(const char *word, const struct list *list, int section, const char **extra);

View File

@ -1,3 +1,4 @@
#define _GNU_SOURCE /* for CPU_* from cpuset.h */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -13,6 +14,7 @@
#include <haproxy/buf.h> #include <haproxy/buf.h>
#include <haproxy/cfgparse.h> #include <haproxy/cfgparse.h>
#include <haproxy/cpuset.h>
#include <haproxy/compression.h> #include <haproxy/compression.h>
#include <haproxy/global.h> #include <haproxy/global.h>
#include <haproxy/log.h> #include <haproxy/log.h>
@ -1027,7 +1029,8 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
#ifdef USE_CPU_AFFINITY #ifdef USE_CPU_AFFINITY
char *slash; char *slash;
unsigned long proc = 0, thread = 0, cpus; unsigned long proc = 0, thread = 0, cpus;
int i, j, n, autoinc; int i, j, n, k, autoinc;
struct hap_cpuset cpuset;
if (!*args[1] || !*args[2]) { if (!*args[1] || !*args[2]) {
ha_alert("parsing [%s:%d] : %s expects a process number " ha_alert("parsing [%s:%d] : %s expects a process number "
@ -1068,12 +1071,23 @@ 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, &cpuset, &errmsg)) {
ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;
goto out; goto out;
} }
#if defined(CPUSET_USE_CPUSET)
k = 0;
while (CPU_COUNT(&cpuset.cpuset)) {
while (!CPU_ISSET(k, &cpuset.cpuset))
++k;
cpus |= 1 << k;
CPU_CLR(k, &cpuset.cpuset);
++k;
}
#elif defined(CPUSET_USE_ULONG)
cpus = cpuset.cpuset;
#endif
if (autoinc && if (autoinc &&
my_popcountl(proc) != my_popcountl(cpus) && my_popcountl(proc) != my_popcountl(cpus) &&
my_popcountl(thread) != my_popcountl(cpus)) { my_popcountl(thread) != my_popcountl(cpus)) {

View File

@ -43,6 +43,7 @@
#include <haproxy/channel.h> #include <haproxy/channel.h>
#include <haproxy/check.h> #include <haproxy/check.h>
#include <haproxy/chunk.h> #include <haproxy/chunk.h>
#include <haproxy/cpuset.h>
#include <haproxy/connection.h> #include <haproxy/connection.h>
#include <haproxy/errors.h> #include <haproxy/errors.h>
#include <haproxy/filters.h> #include <haproxy/filters.h>
@ -460,27 +461,28 @@ int parse_process_number(const char *arg, unsigned long *proc, int max, int *aut
#ifdef USE_CPU_AFFINITY #ifdef USE_CPU_AFFINITY
/* Parse cpu sets. Each CPU set is either a unique number between 0 and /* Parse cpu sets. Each CPU set is either a unique number between 0 and
* <LONGBITS> or a range with two such numbers delimited by a dash * 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 * ('-'). Multiple CPU numbers or ranges may be specified. On success, it
* returns 0. otherwise it returns 1 with an error message in <err>. * returns 0. otherwise it returns 1 with an error message in <err>.
*/ */
unsigned long parse_cpu_set(const char **args, unsigned long *cpu_set, char **err) unsigned long parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err)
{ {
int cur_arg = 0; int cur_arg = 0;
*cpu_set = 0; ha_cpuset_zero(cpu_set);
while (*args[cur_arg]) { while (*args[cur_arg]) {
char *dash; char *dash;
unsigned int low, high; unsigned int low, high;
if (!isdigit((unsigned char)*args[cur_arg])) { if (!isdigit((unsigned char)*args[cur_arg])) {
memprintf(err, "'%s' is not a CPU range.\n", args[cur_arg]); memprintf(err, "'%s' is not a CPU range.", args[cur_arg]);
return -1; return -1;
} }
low = high = str2uic(args[cur_arg]); low = high = str2uic(args[cur_arg]);
if ((dash = strchr(args[cur_arg], '-')) != NULL) if ((dash = strchr(args[cur_arg], '-')) != NULL)
high = ((!*(dash+1)) ? LONGBITS-1 : str2uic(dash + 1)); high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1;
if (high < low) { if (high < low) {
unsigned int swap = low; unsigned int swap = low;
@ -488,13 +490,14 @@ unsigned long parse_cpu_set(const char **args, unsigned long *cpu_set, char **er
high = swap; high = swap;
} }
if (high >= LONGBITS) { if (high >= ha_cpuset_size()) {
memprintf(err, "supports CPU numbers from 0 to %d.\n", LONGBITS - 1); memprintf(err, "supports CPU numbers from 0 to %d.",
ha_cpuset_size() - 1);
return 1; return 1;
} }
while (low <= high) while (low <= high)
*cpu_set |= 1UL << low++; ha_cpuset_set(cpu_set, low++);
cur_arg++; cur_arg++;
} }