diff --git a/include/haproxy/cpuset.h b/include/haproxy/cpuset.h index 327d3aee4..8d3b67869 100644 --- a/include/haproxy/cpuset.h +++ b/include/haproxy/cpuset.h @@ -54,6 +54,12 @@ int ha_cpuset_size(void); */ int ha_cpuset_detect_bound(struct hap_cpuset *set); +/* Detects CPUs that are online on the system. It may rely on FS access (e.g. + * /sys on Linux). Returns the number of CPUs detected or 0 if the detection + * failed. + */ +int ha_cpuset_detect_online(struct hap_cpuset *set); + /* Detects the CPUs that will be used based on the ones the process is bound to. * Returns non-zero on success, zero on failure. Note that it may not be * performed in the function above because some calls may rely on other items diff --git a/src/cpuset.c b/src/cpuset.c index ed6c0fd37..52e6b4974 100644 --- a/src/cpuset.c +++ b/src/cpuset.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -176,6 +177,32 @@ int ha_cpuset_detect_bound(struct hap_cpuset *set) return ha_cpuset_count(set); } +/* Detects CPUs that are online on the system. It may rely on FS access (e.g. + * /sys on Linux). Returns the number of CPUs detected or 0 if the detection + * failed. + */ +int ha_cpuset_detect_online(struct hap_cpuset *set) +{ +#if defined(__linux__) + + ha_cpuset_zero(set); + + /* contains a list of CPUs in the format [-][,...] */ + if (read_line_to_trash("%s/cpu/online", NUMA_DETECT_SYSTEM_SYSFS_PATH) == 0) { + const char *parse_cpu_set_args[2] = { trash.area, "\0" }; + + if (parse_cpu_set(parse_cpu_set_args, set, NULL) != 0) + ha_cpuset_zero(set); + } + +#else // !__linux__ + + ha_cpuset_zero(set); + +#endif + return ha_cpuset_count(set); +} + /* Detects the CPUs that will be used based on the ones the process is bound to * at boot. The principle is the following: all CPUs from the boot cpuset will * be used since we don't know upfront how individual threads will be mapped to