]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/Thread: allow io priority to be set for a Thread
authorSage Weil <sage@redhat.com>
Wed, 18 Jun 2014 18:01:42 +0000 (11:01 -0700)
committerSage Weil <sage@redhat.com>
Thu, 14 Aug 2014 00:21:18 +0000 (17:21 -0700)
Ideally, set this before starting the thread.  If you set it after, we
could potentially race with create() itself.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit 1b8741022c5a2ebae38905215dadee696433e931)

src/common/Thread.cc
src/common/Thread.h

index 0f4e322b27a71870d9c85dc8cf201edf7932b044..7be0013a6676bc98a5d35515994a6f817aa69c9b 100644 (file)
@@ -16,6 +16,7 @@
 #include "common/code_environment.h"
 #include "common/debug.h"
 #include "common/signal.h"
+#include "common/io_priority.h"
 
 #include <dirent.h>
 #include <errno.h>
 
 
 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;
+}
index 4bc0254b1c26d6121aff69af24b95ff3ebb727be..95f63b46c5ca69cc498865a116bd07e79c35e0f0 100644 (file)
 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