Bug 773518 - Platform code for lowering IO prioritization. r=bsmedberg
authorBrian R. Bondy <netzen@gmail.com>
Wed, 25 Jul 2012 10:25:08 -0400
changeset 100574 0a9061addc84cfd68125813899b6804047b1e811
parent 100573 33b40911b9bad70c0ae8359ea30120ed54a00a8e
child 100575 f544b4af9a0fef91a6c2837d8315415e96ac2db7
push id23185
push usermbrubeck@mozilla.com
push dateThu, 26 Jul 2012 20:58:28 +0000
treeherdermozilla-central@8a7ad0adcccf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs773518
milestone17.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 773518 - Platform code for lowering IO prioritization. r=bsmedberg
xpcom/glue/nsThreadUtils.cpp
xpcom/glue/nsThreadUtils.h
--- a/xpcom/glue/nsThreadUtils.cpp
+++ b/xpcom/glue/nsThreadUtils.cpp
@@ -12,16 +12,19 @@
 #else
 # include "nsXPCOMCIDInternal.h"
 # include "nsIThreadManager.h"
 # include "nsServiceManagerUtils.h"
 #endif
 
 #ifdef XP_WIN
 #include <windows.h>
+#include "nsWindowsHelpers.h"
+#elif defined(XP_MACOSX)
+#include <sys/resource.h>
 #endif
 
 #include <pratom.h>
 #include <prthread.h>
 
 #ifndef XPCOM_GLUE_AVOID_NSPR
 
 NS_IMPL_THREADSAFE_ISUPPORTS1(nsRunnable, nsIRunnable)
@@ -284,8 +287,41 @@ nsThreadPoolNaming::SetThreadPoolName(co
     // Set on the target thread
     NS_SetThreadName(aThread, name);
   }
   else {
     // Set on the current thread
     PR_SetCurrentThreadName(name.BeginReading());
   }
 }
+
+// nsAutoLowPriorityIO
+nsAutoLowPriorityIO::nsAutoLowPriorityIO()
+{
+#if defined(XP_WIN)
+  lowIOPrioritySet = IsVistaOrLater() &&
+                     SetThreadPriority(GetCurrentThread(),
+                                       THREAD_MODE_BACKGROUND_BEGIN);
+#elif defined(XP_MACOSX)
+  oldPriority = getiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD);
+  lowIOPrioritySet = oldPriority != -1 &&
+                     setiopolicy_np(IOPOL_TYPE_DISK,
+                                    IOPOL_SCOPE_THREAD,
+                                    IOPOL_THROTTLE) != -1;
+#else
+  lowIOPrioritySet = false;
+#endif
+}
+
+nsAutoLowPriorityIO::~nsAutoLowPriorityIO()
+{
+#if defined(XP_WIN)
+  if (NS_LIKELY(lowIOPrioritySet)) {
+    // On Windows the old thread priority is automatically restored
+    SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
+  }
+#elif defined(XP_MACOSX)
+  if (NS_LIKELY(lowIOPrioritySet)) {
+    setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD, oldPriority);
+  }
+#endif
+}
+
--- a/xpcom/glue/nsThreadUtils.h
+++ b/xpcom/glue/nsThreadUtils.h
@@ -489,9 +489,30 @@ public:
 
 private:
   volatile PRUint32 mCounter;
 
   nsThreadPoolNaming(const nsThreadPoolNaming &) MOZ_DELETE;
   void operator=(const nsThreadPoolNaming &) MOZ_DELETE;
 };
 
+/**
+ * Thread priority in most operating systems affect scheduling, not IO.  This
+ * helper is used to set the current thread to low IO priority for the lifetime
+ * of the created object.  You can only use this low priority IO setting within
+ * the context of the current thread.
+ */
+class NS_STACK_CLASS nsAutoLowPriorityIO
+{
+public:
+  nsAutoLowPriorityIO();
+  ~nsAutoLowPriorityIO();
+
+private:
+  bool lowIOPrioritySet;
+#if defined(XP_MACOSX)
+  int oldPriority;
+#endif
+};
+
+
+
 #endif  // nsThreadUtils_h__