From: Sage Weil Date: Wed, 18 Jun 2014 18:01:42 +0000 (-0700) Subject: common/Thread: allow io priority to be set for a Thread X-Git-Tag: v0.80.6~33^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b75f85a2c4dd9807947862f7b89a5f25dfa1defe;p=ceph.git common/Thread: allow io priority to be set for a Thread Ideally, set this before starting the thread. If you set it after, we could potentially race with create() itself. Signed-off-by: Sage Weil (cherry picked from commit 1b8741022c5a2ebae38905215dadee696433e931) --- diff --git a/src/common/Thread.cc b/src/common/Thread.cc index 0f4e322b27a7..7be0013a6676 100644 --- a/src/common/Thread.cc +++ b/src/common/Thread.cc @@ -16,6 +16,7 @@ #include "common/code_environment.h" #include "common/debug.h" #include "common/signal.h" +#include "common/io_priority.h" #include #include @@ -29,7 +30,10 @@ Thread::Thread() - : thread_id(0) + : thread_id(0), + pid(0), + ioprio_class(-1), + ioprio_priority(-1) { } @@ -38,10 +42,24 @@ Thread::~Thread() } void *Thread::_entry_func(void *arg) { - void *r = ((Thread*)arg)->entry(); + void *r = ((Thread*)arg)->entry_wrapper(); return r; } +void *Thread::entry_wrapper() +{ + int p = ceph_gettid(); // may return -ENOSYS on other platforms + if (p > 0) + pid = p; + if (ioprio_class >= 0 && + ioprio_priority >= 0) { + ceph_ioprio_set(IOPRIO_WHO_PROCESS, + pid, + IOPRIO_PRIO_VALUE(ioprio_class, ioprio_priority)); + } + return entry(); +} + const pthread_t &Thread::get_thread_id() { return thread_id; @@ -128,3 +146,15 @@ int Thread::detach() { return pthread_detach(thread_id); } + +int Thread::set_ioprio(int cls, int prio) +{ + // fixme, maybe: this can race with create() + ioprio_class = cls; + ioprio_priority = prio; + if (pid && cls >= 0 && prio >= 0) + return ceph_ioprio_set(IOPRIO_WHO_PROCESS, + pid, + IOPRIO_PRIO_VALUE(cls, prio)); + return 0; +} diff --git a/src/common/Thread.h b/src/common/Thread.h index 4bc0254b1c26..95f63b46c5ca 100644 --- a/src/common/Thread.h +++ b/src/common/Thread.h @@ -21,6 +21,10 @@ class Thread { private: pthread_t thread_id; + pid_t pid; + int ioprio_class, ioprio_priority; + + void *entry_wrapper(); public: Thread(const Thread& other); @@ -44,6 +48,7 @@ class Thread { void create(size_t stacksize = 0); int join(void **prval = 0); int detach(); + int set_ioprio(int cls, int prio); }; #endif