Bug 1394710 - Set process priority class on Windows r?aklotz draft
authorDoug Thayer <dothayer@mozilla.com>
Fri, 08 Jun 2018 14:54:11 -0700
changeset 819940 63f7e54f6831c2c4abf82eac0458a3de6df880ee
parent 817669 e951f4ad123aa87d1d392c286db14cabb41a8560
push id116702
push userbmo:dothayer@mozilla.com
push dateWed, 18 Jul 2018 20:10:36 +0000
reviewersaklotz
bugs1394710
milestone63.0a1
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