]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: cpuset: move parse_cpu_set() and parse_cpumap() to cpuset.c
authorWilly Tarreau <w@1wt.eu>
Thu, 6 Jul 2023 12:35:11 +0000 (14:35 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 8 Sep 2023 14:25:19 +0000 (16:25 +0200)
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.

include/haproxy/cfgparse.h
include/haproxy/cpuset.h
src/cfgparse.c
src/cpuset.c

index 20d72972d8982fcbc62967677d3203e0cfa67ff0..adcabb397ee9762bdf94ee404f774398356a5d3d 100644 (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);
index e4811bcd6c18d00fe56b840cc0a0294fdfd9e8b6..87c4ece8281aec3aab63416d8f02e0db6f005be0 100644 (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.
  */
index bb7fb71ed7d9c7e34163e8d095c3ea4f05231ac1..42ff8a8025698e57d55bc3d36598645f096544b9 100644 (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
index 3ce6cf22310f57532d2f398667202befa7fbef82..82e350f132801926fa71d01f77ef3a7834e30949 100644 (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.
  */