From 97148f60b8feec39b76768d1bcfab6d755c12164 Mon Sep 17 00:00:00 2001 From: Olivier Houchard Date: Wed, 16 Aug 2017 17:29:11 +0200 Subject: [PATCH] MINOR: init: Fix CPU affinity setting on FreeBSD. Use a cpuset_t instead of assuming the cpu mask is an unsigned long. This should fix setting the CPU affinity on FreeBSD >= 11. This patch should be backported to stable releases. --- src/haproxy.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/haproxy.c b/src/haproxy.c index 7af4ab479..30e850c4a 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -2558,7 +2558,18 @@ int main(int argc, char **argv) proc < LONGBITS && /* only the first 32/64 processes may be pinned */ global.cpu_map[proc]) /* only do this if the process has a CPU map */ #ifdef __FreeBSD__ - cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(unsigned long), (void *)&global.cpu_map[proc]); + { + cpuset_t cpuset; + int i; + unsigned long cpu_map = global.cpu_map[proc]; + + CPU_ZERO(&cpuset); + while ((i = ffsl(cpu_map)) > 0) { + CPU_SET(i - 1, &cpuset); + cpu_map &= ~(1 << (i - 1)); + } + ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset); + } #else sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map[proc]); #endif