From: Willy Tarreau Date: Mon, 31 Mar 2025 12:35:09 +0000 (+0200) Subject: MINOR: cpu-set: add a new function to print cpu-sets in human-friendly mode X-Git-Tag: v3.2-dev9~37 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=571573874a2bfabc84d1fca10419cdf4a800d706;p=thirdparty%2Fhaproxy.git MINOR: cpu-set: add a new function to print cpu-sets in human-friendly mode 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. --- diff --git a/include/haproxy/cpuset.h b/include/haproxy/cpuset.h index 14c669c25..be54f5d11 100644 --- a/include/haproxy/cpuset.h +++ b/include/haproxy/cpuset.h @@ -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. diff --git a/src/cpuset.c b/src/cpuset.c index b102d11d1..5b23f1503 100644 --- a/src/cpuset.c +++ b/src/cpuset.c @@ -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.