Skip to content

Commit e65f54c

Browse files
committed
MINOR: cpuset: centralize a reliable bound cpu detection
Till now the CPUs that were bound were only retrieved in thread_cpus_enabled() in order to count the number of CPUs allowed, and it relied on arch-specific code. Let's slightly arrange this into ha_cpuset_detect_bound() that reuses the ha_cpuset struct and the accompanying code. This makes the code much clearer without having to carry along some arch-specific stuff out of this area. Note that the macos-specific code used in thread.c to only count online CPUs but not retrieve a mask, so for now we can't infer anything from it and can't implement it. In addition and more importantly, this function is reliable in that it will only return a value when the detection is accurate, and will not return incomplete sets on operating systems where we don't have an exact list, such as online CPUs.
1 parent d3ecc67 commit e65f54c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/haproxy/cpuset.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ void ha_cpuset_assign(struct hap_cpuset *dst, struct hap_cpuset *src);
4848
*/
4949
int ha_cpuset_size(void);
5050

51+
/* Detects CPUs that are bound to the current process. Returns the number of
52+
* CPUs detected or 0 if the detection failed.
53+
*/
54+
int ha_cpuset_detect_bound(struct hap_cpuset *set);
55+
5156
/* Returns true if at least one cpu-map directive was configured, otherwise
5257
* false.
5358
*/

src/cpuset.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,30 @@ int ha_cpuset_size()
147147
#endif
148148
}
149149

150+
/* Detects CPUs that are bound to the current process. Returns the number of
151+
* CPUs detected or 0 if the detection failed.
152+
*/
153+
int ha_cpuset_detect_bound(struct hap_cpuset *set)
154+
{
155+
ha_cpuset_zero(set);
156+
157+
/* detect bound CPUs depending on the OS's API */
158+
if (0
159+
#if defined(__linux__)
160+
|| sched_getaffinity(0, sizeof(set->cpuset), &set->cpuset) != 0
161+
#elif defined(__FreeBSD__)
162+
|| cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, sizeof(set->cpuset), &set->cpuset) != 0
163+
#else
164+
|| 1 // unhandled platform
165+
#endif
166+
) {
167+
/* detection failed */
168+
return 0;
169+
}
170+
171+
return ha_cpuset_count(set);
172+
}
173+
150174
/* Returns true if at least one cpu-map directive was configured, otherwise
151175
* false.
152176
*/

0 commit comments

Comments
 (0)