]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Thread: Support set_affinity to bind core
authorHaomai Wang <haomaiwang@gmail.com>
Sun, 15 Mar 2015 16:06:05 +0000 (00:06 +0800)
committerHaomai Wang <haomaiwang@gmail.com>
Sun, 15 Mar 2015 16:12:44 +0000 (00:12 +0800)
Signed-off-by: Haomai Wang <haomaiwang@gmail.com>
src/common/Thread.cc
src/common/Thread.h

index 584e97bbd824772157b060b662d364fe0d53a03a..188d94a789b4a224a5e093a3f6c91f003ce23f4e 100644 (file)
@@ -1,4 +1,4 @@
- // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
 // vim: ts=8 sw=2 smarttab
 /*
  * Ceph - scalable distributed file system
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
+#ifdef HAVE_SCHED
+#include <sched.h>
+#endif
 
 
 Thread::Thread()
   : thread_id(0),
     pid(0),
     ioprio_class(-1),
-    ioprio_priority(-1)
+    ioprio_priority(-1),
+    cpuid(-1)
 {
 }
 
@@ -58,6 +62,8 @@ void *Thread::entry_wrapper()
                    pid,
                    IOPRIO_PRIO_VALUE(ioprio_class, ioprio_priority));
   }
+  if (pid && cpuid >= 0)
+    set_affinity(cpuid);
   return entry();
 }
 
@@ -159,3 +165,22 @@ int Thread::set_ioprio(int cls, int prio)
                           IOPRIO_PRIO_VALUE(cls, prio));
   return 0;
 }
+
+int Thread::set_affinity(int id)
+{
+#ifdef HAVE_SCHED
+  if (pid && id >= 0 && id < CPU_SETSIZE) {
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+
+    CPU_SET(id, &cpuset);
+
+    if (sched_setaffinity(0, sizeof(cpuset), &cpuset) < 0)
+      return -errno;
+    /* guaranteed to take effect immediately */
+    sched_yield();
+  }
+#endif
+  cpuid = id;
+  return 0;
+}
index 7889c913456772901fbb8ba72bc95fbb029ab308..26c3d07c8c94488108f0468f28e10b90a2f1d2a3 100644 (file)
@@ -24,6 +24,7 @@ class Thread {
   pthread_t thread_id;
   pid_t pid;
   int ioprio_class, ioprio_priority;
+  int cpuid;
 
   void *entry_wrapper();
 
@@ -51,6 +52,7 @@ class Thread {
   int join(void **prval = 0);
   int detach();
   int set_ioprio(int cls, int prio);
+  int set_affinity(int cpuid);
 };
 
 #endif