Bug 1394710 - Set process priority class on Windows r=aklotz
authorDoug Thayer <dothayer@mozilla.com>
Fri, 08 Jun 2018 14:54:11 -0700
changeset 427194 4e0b454aac6e84553ad41aae55db940a86c7bf16
parent 427193 9582213d1360ec17eeb3c75fe2b21069eb3c441a
child 427195 01417121e3985f5b0313a31092952cc795c9ef85
push id34296
push userrgurzau@mozilla.com
push dateThu, 19 Jul 2018 09:52:36 +0000
treeherdermozilla-central@25c83a66f294 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1394710
milestone63.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 1394710 - Set process priority class on Windows r=aklotz Fairly straightforward. Using an idle priority class for background, which mirrors what I am observing of Chromium in Process Explorer. MozReview-Commit-ID: 2mimYN7aJOy
hal/moz.build
hal/windows/WindowsProcessPriority.cpp
--- a/hal/moz.build
+++ b/hal/moz.build
@@ -33,77 +33,82 @@ SOURCES += [
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android':
     LOCAL_INCLUDES += [
         '/widget/android',
     ]
     UNIFIED_SOURCES += [
         'android/AndroidSensor.cpp',
+        'fallback/FallbackProcessPriority.cpp',
     ]
     # AndroidHal.cpp cannot be built in unified mode because it relies on HalImpl.h.
     SOURCES += [
         'android/AndroidHal.cpp',
     ]
 elif CONFIG['OS_TARGET'] == 'Linux':
     UNIFIED_SOURCES += [
+        'fallback/FallbackProcessPriority.cpp',
         'fallback/FallbackScreenConfiguration.cpp',
         'fallback/FallbackSensor.cpp',
         'fallback/FallbackVibration.cpp',
     ]
     if CONFIG['MOZ_ENABLE_DBUS']:
         UNIFIED_SOURCES += [
             'linux/UPowerClient.cpp',
         ]
     else:
         UNIFIED_SOURCES += [
             'fallback/FallbackBattery.cpp',
         ]
 elif CONFIG['OS_TARGET'] == 'WINNT':
     UNIFIED_SOURCES += [
         'fallback/FallbackScreenConfiguration.cpp',
         'fallback/FallbackVibration.cpp',
+        'windows/WindowsProcessPriority.cpp',
         'windows/WindowsSensor.cpp',
     ]
     # WindowsBattery.cpp cannot be built in unified mode because it relies on HalImpl.h.
     SOURCES += [
         'windows/WindowsBattery.cpp',
     ]
 elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
     UNIFIED_SOURCES += [
         'cocoa/CocoaBattery.cpp',
+        'fallback/FallbackProcessPriority.cpp',
         'fallback/FallbackScreenConfiguration.cpp',
         'fallback/FallbackVibration.cpp',
     ]
 elif CONFIG['OS_TARGET'] in ('OpenBSD', 'NetBSD', 'FreeBSD', 'DragonFly'):
     UNIFIED_SOURCES += [
+        'fallback/FallbackProcessPriority.cpp',
         'fallback/FallbackScreenConfiguration.cpp',
         'fallback/FallbackSensor.cpp',
         'fallback/FallbackVibration.cpp',
     ]
     if CONFIG['MOZ_ENABLE_DBUS']:
         UNIFIED_SOURCES += [
             'linux/UPowerClient.cpp',
         ]
     else:
         UNIFIED_SOURCES += [
             'fallback/FallbackBattery.cpp',
         ]
 else:
     UNIFIED_SOURCES += [
         'fallback/FallbackBattery.cpp',
+        'fallback/FallbackProcessPriority.cpp',
         'fallback/FallbackScreenConfiguration.cpp',
         'fallback/FallbackSensor.cpp',
         'fallback/FallbackVibration.cpp',
     ]
 
 # Fallbacks for backends no longer implemented.
 UNIFIED_SOURCES += [
     'fallback/FallbackDiskSpaceWatcher.cpp',
-    'fallback/FallbackProcessPriority.cpp',
 ]
 
 # Fallbacks for backends implemented on Android only.
 if CONFIG['MOZ_WIDGET_TOOLKIT'] != 'android':
     UNIFIED_SOURCES += [
         'fallback/FallbackNetwork.cpp',
     ]
 
new file mode 100644
--- /dev/null
+++ b/hal/windows/WindowsProcessPriority.cpp
@@ -0,0 +1,43 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "Hal.h"
+#include "HalLog.h"
+
+#include <Windows.h>
+
+using namespace mozilla::hal;
+
+namespace mozilla {
+namespace hal_impl {
+
+bool
+SetProcessPrioritySupported()
+{
+  return true;
+}
+
+void
+SetProcessPriority(int aPid, ProcessPriority aPriority)
+{
+  HAL_LOG("WindowsProcessPriority - SetProcessPriority(%d, %s)\n",
+          aPid, ProcessPriorityToString(aPriority));
+
+  nsAutoHandle processHandle(::OpenProcess(PROCESS_SET_INFORMATION, FALSE, aPid));
+  MOZ_ASSERT(processHandle);
+  if (processHandle) {
+    DWORD priority = NORMAL_PRIORITY_CLASS;
+    if (aPriority == PROCESS_PRIORITY_BACKGROUND ||
+        aPriority == PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) {
+      priority = IDLE_PRIORITY_CLASS;
+    }
+    ::SetPriorityClass(processHandle, priority);
+  }
+
+  HAL_LOG("WindowsProcessPriority - priority set to %d for pid %d\n",
+          aPriority, aPid);
+}
+
+} // namespace hal_impl
+} // namespace mozilla