Updated Branches:
  refs/heads/master d19ba4618 -> eefd111a8

Port cpu sets to Solaris and FreeBSD

This code has been tested on Linux, FreeBSD and Solairs: It compiles.
Not tested on Mac OS X/darwin.

(by tested, I mean: It compiles)


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/eefd111a
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/eefd111a
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/eefd111a

Branch: refs/heads/master
Commit: eefd111a8e841b4a73d934ff81487317e640af13
Parents: d19ba46
Author: Igor Galić <[email protected]>
Authored: Wed Sep 19 00:23:18 2012 +0200
Committer: Igor Galić <[email protected]>
Committed: Wed Sep 19 00:23:18 2012 +0200

----------------------------------------------------------------------
 configure.ac                             |   17 +++++-
 iocore/eventsystem/UnixEventProcessor.cc |   77 ++++++++++++++++++++++---
 lib/ts/ink_config.h.in                   |    5 ++
 3 files changed, 90 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/eefd111a/configure.ac
----------------------------------------------------------------------
diff --git a/configure.ac b/configure.ac
index ff5747c..7b05146 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1169,6 +1169,10 @@ AC_SUBST(LUA_LUAJIT_LDFLAGS)
 
 TS_FLAG_HEADERS([sys/epoll.h \
                   sys/event.h \
+                  sys/param.h \
+                  sys/pset.h \
+                  sched.h \
+                  pthread_np.h \
                   machine/endian.h \
                   endian.h \
                   sys/sysinfo.h \
@@ -1179,7 +1183,7 @@ TS_FLAG_HEADERS([sys/epoll.h \
                   sys/ioctl.h \
                   sys/byteorder.h \
                   sys/sockio.h \
-                 sys/prctl.h \
+                  sys/prctl.h \
                   arpa/nameser.h \
                   arpa/nameser_compat.h \
                   execinfo.h \
@@ -1207,6 +1211,11 @@ AC_SUBST(sys_epollh)
 AC_SUBST(sys_eventh)
 AC_SUBST(machine_endianh)
 AC_SUBST(endianh)
+AC_SUBST(pthread_nph)
+AC_SUBST(sys_paramh)
+AC_SUBST(sys_cpuseth)
+AC_SUBST(sys_pseth)
+AC_SUBST(schedh)
 AC_SUBST(netinet_inh)
 AC_SUBST(netinet_in_systmh)
 AC_SUBST(netinet_tcph)
@@ -1246,6 +1255,12 @@ TS_FLAG_HEADERS([sys/sysctl.h], [], [],
                    #endif
                  ]])
 
+TS_FLAG_HEADERS([sys/cpuset.h], [], [],
+                 [[#ifdef HAVE_SYS_PARAM_H
+                    #include <sys/param.h>
+                   #endif
+                 ]])
+
 TS_FLAG_HEADERS([sys/mount.h], [], [],
                  [[#ifdef HAVE_SYS_PARAM_H
                     #include <sys/param.h>

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/eefd111a/iocore/eventsystem/UnixEventProcessor.cc
----------------------------------------------------------------------
diff --git a/iocore/eventsystem/UnixEventProcessor.cc 
b/iocore/eventsystem/UnixEventProcessor.cc
index 78f6f7d..5113353 100644
--- a/iocore/eventsystem/UnixEventProcessor.cc
+++ b/iocore/eventsystem/UnixEventProcessor.cc
@@ -24,16 +24,76 @@
 #include "P_EventSystem.h"      /* MAGIC_EDITING_TAG */
 #include <sched.h>
 #if TS_USE_HWLOC
+
+#if HAVE_HWLOC_H
 #include <hwloc.h>
-// TODO: (humbedooh): Make this build on Solaris with hwloc.
-#if defined(freebsd) || defined(solaris)
+#endif
+
+#if HAVE_SCHED_H
+#include <sched.h>
+#if !defined(solaris) && !defined(freebsd)
+typedef cpu_set_t ink_cpuset_t;
+#define PTR_FMT PRIuPTR
+#endif
+#endif
+
+#if HAVE_SYS_PARAM_H
+#include <sys/param.h>
+#endif
+
+#if HAVE_SYS_CPUSET_H
 #include <sys/cpuset.h>
-#define cpu_set_t cpuset_t
+typedef cpuset_t ink_cpuset_t;
+#define PTR_FMT "p"
+#endif
+
+#if HAVE_PTHREAD_NP_H
 #include <pthread_np.h>
 #endif
+
+#if HAVE_SYS_PSET_H
+#include <sys/pset.h>
+typedef psetid_t ink_cpuset_t;
+#define PTR_FMT PRIuPTR
+#endif
+
+#if HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
 #endif
 #include "ink_defs.h"
 
+#if TS_USE_HWLOC
+static void
+set_cpu(ink_cpuset_t *cpuset, int cpu)
+{
+#if !defined(solaris)
+  CPU_ZERO(cpuset);
+  CPU_SET(cpu, cpuset);
+#else
+  pset_create(cpuset);
+  pset_assign(*cpuset, cpu, NULL);
+#endif
+}
+
+
+static bool
+bind_cpu(ink_cpuset_t *cpuset, pthread_t tid)
+{
+  if ( 0 != 
+#if !defined(solaris)
+    pthread_setaffinity_np(tid, sizeof(ink_cpuset_t), cpuset)
+#else
+    pset_bind(*cpuset, P_LWPID, P_MYID, NULL)
+#endif
+    ){
+    return false;
+  }
+  return true;
+}
+#endif
+
 EventType
 EventProcessor::spawn_event_threads(int n_threads, const char* et_name)
 {
@@ -106,7 +166,7 @@ EventProcessor::start(int n_event_threads)
 #if TS_USE_HWLOC
   int affinity = 0;
   REC_ReadConfigInteger(affinity, "proxy.config.exec_thread.affinity");
-  cpu_set_t cpuset;
+  ink_cpuset_t cpuset;
   const hwloc_topology_t *topology = ink_get_topology();
   int cu = hwloc_get_nbobjs_by_type(*topology, HWLOC_OBJ_CORE);
   int pu = hwloc_get_nbobjs_by_type(*topology, HWLOC_OBJ_PU);
@@ -121,11 +181,12 @@ EventProcessor::start(int n_event_threads)
 
 #if TS_USE_HWLOC
     if (affinity == 1) {
-      CPU_ZERO(&cpuset);
       int cpu = (i - 1) % num_cpus;
-      CPU_SET(cpu, &cpuset);
-      Debug("iocore_thread", "setaffinity tid: %p, net thread: %d, cpu: %d", 
tid, i, cpu);
-      assert(pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpuset) == 0);
+      set_cpu(&cpuset, cpu);
+      Debug("iocore_thread", "setaffinity tid: %" PTR_FMT ", net thread: %u, 
cpu: %d", tid, i, cpu);
+      if (!bind_cpu(&cpuset, tid)){
+        Debug("iocore_thread", "setaffinity for tid: %" PTR_FMT ", net thread: 
%u, cpu: %d failed with: %d", tid, i, cpu, errno);
+      }
     }
 #endif
   }

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/eefd111a/lib/ts/ink_config.h.in
----------------------------------------------------------------------
diff --git a/lib/ts/ink_config.h.in b/lib/ts/ink_config.h.in
index 60bd779..83c328f 100644
--- a/lib/ts/ink_config.h.in
+++ b/lib/ts/ink_config.h.in
@@ -50,6 +50,11 @@
 #define TS_HAVE_NETINET_IN_H           @netinet_inh@
 #define TS_HAVE_NETINET_IN_SYSTM_H     @netinet_in_systmh@
 #define TS_HAVE_NETINET_TCP_H          @netinet_tcph@
+#define TS_HAVE_PTHREAD_NP_H           @pthread_nph@
+#define TS_HAVE_SYS_PARAM_H            @sys_paramh@
+#define TS_HAVE_SYS_CPUSET_H           @sys_cpuseth@
+#define TS_HAVE_SYS_PSET_H             @sys_pseth@
+#define TS_HAVE_SCHED_H                @schedh@
 #define TS_HAVE_SYS_IOCTL_H            @sys_ioctlh@
 #define TS_HAVE_SYS_BYTEORDER_H        @sys_byteorderh@
 #define TS_HAVE_SYS_SOCKIO_H           @sys_sockioh@

Reply via email to