]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUILD/MEDIUM: threads: enable cpu_affinity on osx
authorDavid Carlier <devnexen@gmail.com>
Fri, 13 Sep 2019 04:12:58 +0000 (05:12 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 17 Oct 2019 05:20:58 +0000 (07:20 +0200)
Enable it but on a per thread basis only using Darwin native API.

Makefile
src/haproxy.c

index 9cacb9330421fba2f3046df145ad6fbc8e579b19..fd8edc596390bd2afeee074dab90ea5dfa4e8d84 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -356,7 +356,7 @@ endif
 # Mac OS/X
 ifeq ($(TARGET),osx)
   set_target_defaults = $(call default_opts, \
-    USE_POLL USE_TPROXY USE_LIBCRYPT USE_THREAD USE_KQUEUE)
+    USE_POLL USE_TPROXY USE_LIBCRYPT USE_THREAD USE_CPU_AFFINITY USE_KQUEUE)
   EXPORT_SYMBOL  = -export_dynamic
 endif
 
index db6f21f560684efe13447c774f68abe08cf3073d..a4f2e0df538e88f122f51dc2b382fb68bc50f3d6 100644 (file)
 #endif
 #include <pthread_np.h>
 #endif
+#ifdef __APPLE__
+#include <mach/mach_types.h>
+#include <mach/thread_act.h>
+#include <mach/thread_policy.h>
+#endif
 #endif
 
 #if defined(USE_PRCTL)
@@ -3164,7 +3169,7 @@ int main(int argc, char **argv)
                        }
                        ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cpuset), &cpuset);
                }
-#else
+#elif defined(__linux__)
                        sched_setaffinity(0, sizeof(unsigned long), (void *)&global.cpu_map.proc[proc]);
 #endif
 #endif
@@ -3377,6 +3382,17 @@ int main(int argc, char **argv)
 
                        if (i < MAX_THREADS &&       /* only the first 32/64 threads may be pinned */
                            global.cpu_map.thread[i]) {/* only do this if the thread has a THREAD map */
+#if defined(__APPLE__)
+                               int j;
+                               unsigned long cpu_map = global.cpu_map.thread[i];
+
+                               while ((j = ffsl(cpu_map)) > 0) {
+                                       thread_affinity_policy_data_t cpu_set = { j - 1 };
+                                       thread_port_t mthread = pthread_mach_thread_np(ha_thread_info[i].pthread);
+                                       thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
+                                       cpu_map &= ~(1UL << (j - 1));
+                               }
+#else
 #if defined(__FreeBSD__) || defined(__NetBSD__)
                                cpuset_t cpuset;
 #else
@@ -3393,6 +3409,7 @@ int main(int argc, char **argv)
                                }
                                pthread_setaffinity_np(ha_thread_info[i].pthread,
                                                       sizeof(cpuset), &cpuset);
+#endif
                        }
                }
 #endif /* !USE_CPU_AFFINITY */