From: Haomai Wang Date: Sun, 15 Mar 2015 16:06:05 +0000 (+0800) Subject: Thread: Support set_affinity to bind core X-Git-Tag: v9.0.0~137^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b70bd6def73de72703b01835bbc56a78cff1523e;p=ceph.git Thread: Support set_affinity to bind core Signed-off-by: Haomai Wang --- diff --git a/src/common/Thread.cc b/src/common/Thread.cc index 584e97bbd824..188d94a789b4 100644 --- a/src/common/Thread.cc +++ b/src/common/Thread.cc @@ -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 @@ -27,13 +27,17 @@ #include #include #include +#ifdef HAVE_SCHED +#include +#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; +} diff --git a/src/common/Thread.h b/src/common/Thread.h index 7889c9134567..26c3d07c8c94 100644 --- a/src/common/Thread.h +++ b/src/common/Thread.h @@ -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