diff --git a/include/haproxy/cpuset.h b/include/haproxy/cpuset.h index 2d1b5d375..78c4df270 100644 --- a/include/haproxy/cpuset.h +++ b/include/haproxy/cpuset.h @@ -48,6 +48,11 @@ void ha_cpuset_assign(struct hap_cpuset *dst, struct hap_cpuset *src); */ int ha_cpuset_size(void); +/* Detects CPUs that are bound to the current process. Returns the number of + * CPUs detected or 0 if the detection failed. + */ +int ha_cpuset_detect_bound(struct hap_cpuset *set); + /* Returns true if at least one cpu-map directive was configured, otherwise * false. */ diff --git a/src/cpuset.c b/src/cpuset.c index 42daca463..73b97b975 100644 --- a/src/cpuset.c +++ b/src/cpuset.c @@ -147,6 +147,30 @@ int ha_cpuset_size() #endif } +/* Detects CPUs that are bound to the current process. Returns the number of + * CPUs detected or 0 if the detection failed. + */ +int ha_cpuset_detect_bound(struct hap_cpuset *set) +{ + ha_cpuset_zero(set); + + /* detect bound CPUs depending on the OS's API */ + if (0 +#if defined(__linux__) + || sched_getaffinity(0, sizeof(set->cpuset), &set->cpuset) != 0 +#elif defined(__FreeBSD__) + || cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, sizeof(set->cpuset), &set->cpuset) != 0 +#else + || 1 // unhandled platform +#endif + ) { + /* detection failed */ + return 0; + } + + return ha_cpuset_count(set); +} + /* Returns true if at least one cpu-map directive was configured, otherwise * false. */