Welcome! Log In Create A New Profile

Advanced

[PATCH] Make thread affinity work on FreeBSD

Posted by Olivier Houchard 
Olivier Houchard
[PATCH] Make thread affinity work on FreeBSD
December 01, 2017 06:30PM
Hi,

The attached patch makes the call to pthread_setaffinity_np() work on
FreeBSD.

Regards,

Olivier
From fc204ac3d7f9323b6583465ff5b42a0cfa46b8b1 Mon Sep 17 00:00:00 2001
From: Olivier Houchard <[email protected]>
Date: Fri, 1 Dec 2017 18:19:43 +0100
Subject: [PATCH] MINOR: threads: Fix pthread_setaffinity_np on FreeBSD.

As with the call to cpuset_setaffinity(), FreeBSD expects the argument to
pthread_setaffinity_np() to be a cpuset_t, not an unsigned long, so the call
was silently failing.

This should probably be backported to 1.8.
---
src/haproxy.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/haproxy.c b/src/haproxy.c
index a1fe550e1..4e3c82b86 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -2900,10 +2900,24 @@ int main(int argc, char **argv)
global.cpu_map.thread[relative_pid-1] &= global.cpu_map.proc[relative_pid-1];

if (i < LONGBITS && /* only the first 32/64 threads may be pinned */
- global.cpu_map.thread[relative_pid-1]) /* only do this if the thread has a THREAD map */
+ global.cpu_map.thread[relative_pid-1]) {/* only do this if the thread has a THREAD map */
+#if defined(__FreeBSD__) || defined(__NetBSD__)
+ cpuset_t cpuset;
+#else
+ cpu_set_t cpuset;
+#endif
+ int j;
+ unsigned long cpu_map = global.cpu_map.thread[relative_pid-1];
+
+ CPU_ZERO(&cpuset);
+
+ while ((j = ffsl(cpu_map)) > 0) {
+ CPU_SET(j - 1, &cpuset);
+ cpu_map &= ~(1 << (j - 1));
+ }
pthread_setaffinity_np(threads,
- sizeof(unsigned long),
- (void *)&global.cpu_map.thread[relative_pid-1]);
+ sizeof(cpuset), &cpuset);
+ }
}
#endif /* !USE_CPU_AFFINITY */

--
2.14.3
Willy Tarreau
Re: [PATCH] Make thread affinity work on FreeBSD
December 02, 2017 02:30PM
On Fri, Dec 01, 2017 at 06:23:01PM +0100, Olivier Houchard wrote:
>
> Hi,
>
> The attached patch makes the call to pthread_setaffinity_np() work on
> FreeBSD.

Thanks Olivier, now applied.

Willy
Sorry, only registered users may post in this forum.

Click here to login