]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cpu-set: add a new function to print cpu-sets in human-friendly mode
authorWilly Tarreau <w@1wt.eu>
Mon, 31 Mar 2025 12:35:09 +0000 (14:35 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 31 Mar 2025 14:21:37 +0000 (16:21 +0200)
The new function "print_cpu_set()" will print cpu sets in a human-friendly
way, with commas and dashes for intervals. The goal is to keep them compact
enough.

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

index 14c669c250a22380f6526ccefeb6d9d74ad42048..be54f5d11f61950c1a2808a1c326871f7457a920 100644 (file)
@@ -60,6 +60,14 @@ int ha_cpuset_size(void);
  */
 int parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err);
 
+/* Print a cpu-set as compactly as possible and returns the output length.
+ * Returns >size if it cannot emit anything due to length constraints, in which
+ * case it will match what is at least needed to go further, and may return 0
+ * for an empty set. It will emit series of comma-delimited ranges in the form
+ * "beg[-end]".
+ */
+int print_cpu_set(char *output, size_t size, const struct hap_cpuset *cpu_set);
+
 /* 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.
index b102d11d105b7e482e2f24518f4d16bb8a676baa..5b23f15031bf1c0ed0600448f7061a1d589daecc 100644 (file)
@@ -213,6 +213,60 @@ int parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err)
        return 0;
 }
 
+/* Print a cpu-set as compactly as possible and returns the output length.
+ * Returns >size if it cannot emit anything due to length constraints, in which
+ * case it will match what is at least needed to go further, and may return 0
+ * for an empty set. It will emit series of comma-delimited ranges in the form
+ * "beg[-end]".
+ */
+int print_cpu_set(char *output, size_t size, const struct hap_cpuset *cpu_set)
+{
+       struct hap_cpuset set = *cpu_set;
+       int cpus = ha_cpuset_size();
+       int first = -1;
+       int len = 0;
+       int cpu;
+
+       for (cpu = 0; cpu < cpus; cpu++) {
+               if (!ha_cpuset_isset(&set, cpu))
+                       continue;
+
+               ha_cpuset_clr(&set, cpu);
+
+               /* check if first of a series*/
+               if (first < 0) {
+                       first = cpu;
+                       len += snprintf(output + len, size - len, "%d", cpu);
+                       if (len >= size)
+                               return len + 1;
+
+                       /* check if belongs to a range */
+                       if (cpu < cpus - 1 && ha_cpuset_isset(&set, cpu + 1)) {
+                               if (len + 1 >= size)
+                                       return len + 2;
+                               output[len++] = '-';
+                               output[len] = 0;
+                       } else
+                               first = -1;
+               }
+               else if (cpu >= cpus - 1 || !ha_cpuset_isset(&set, cpu + 1)) {
+                       /* end of a series and not first */
+                       len += snprintf(output + len, size - len, "%d", cpu);
+                       if (len >= size)
+                               return len + 1;
+                       first = -1;
+               }
+
+               if (first < 0 && ha_cpuset_count(&set) > 0) {
+                       if (len + 1 >= size)
+                               return len + 2;
+                       output[len++] = ',';
+                       output[len] = 0;
+               }
+       }
+       return len;
+}
+
 /* 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.