1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsWindowsHelpers.h" // for nsAutoHandle and nsModuleHandle
8 #include "mozilla/StaticPrefs_dom.h"
12 using namespace mozilla::hal
;
17 void SetProcessPriority(int aPid
, ProcessPriority aPriority
) {
18 HAL_LOG("WindowsProcessPriority - SetProcessPriority(%d, %s)\n", aPid
,
19 ProcessPriorityToString(aPriority
));
21 nsAutoHandle
processHandle(
22 ::OpenProcess(PROCESS_SET_INFORMATION
, FALSE
, aPid
));
24 DWORD priority
= NORMAL_PRIORITY_CLASS
;
25 if (aPriority
== PROCESS_PRIORITY_BACKGROUND
) {
26 priority
= IDLE_PRIORITY_CLASS
;
27 } else if (aPriority
== PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE
) {
28 priority
= BELOW_NORMAL_PRIORITY_CLASS
;
31 if (::SetPriorityClass(processHandle
, priority
)) {
32 HAL_LOG("WindowsProcessPriority - priority set to %d for pid %d\n",
36 // Set the process into or out of EcoQoS.
37 static bool alreadyInitialized
= false;
38 static decltype(::SetProcessInformation
)* setProcessInformation
= nullptr;
39 if (!alreadyInitialized
) {
40 if (aPriority
== PROCESS_PRIORITY_PARENT_PROCESS
||
41 !StaticPrefs::dom_ipc_processPriorityManager_backgroundUsesEcoQoS()) {
45 alreadyInitialized
= true;
46 // SetProcessInformation only exists on Windows 8 and later.
47 nsModuleHandle
module(LoadLibrary(L
"Kernel32.dll"));
49 setProcessInformation
=
50 (decltype(::SetProcessInformation
)*)GetProcAddress(
51 module
, "SetProcessInformation");
54 if (!setProcessInformation
) {
58 PROCESS_POWER_THROTTLING_STATE PowerThrottling
;
59 RtlZeroMemory(&PowerThrottling
, sizeof(PowerThrottling
));
60 PowerThrottling
.Version
= PROCESS_POWER_THROTTLING_CURRENT_VERSION
;
61 PowerThrottling
.ControlMask
= PROCESS_POWER_THROTTLING_EXECUTION_SPEED
;
62 PowerThrottling
.StateMask
=
63 (aPriority
== PROCESS_PRIORITY_BACKGROUND
) &&
65 dom_ipc_processPriorityManager_backgroundUsesEcoQoS()
66 ? PROCESS_POWER_THROTTLING_EXECUTION_SPEED
68 if (setProcessInformation(processHandle
, ProcessPowerThrottling
,
69 &PowerThrottling
, sizeof(PowerThrottling
))) {
70 HAL_LOG("SetProcessInformation(%d, %s)\n", aPid
,
71 aPriority
== PROCESS_PRIORITY_BACKGROUND
? "eco" : "normal");
73 HAL_LOG("SetProcessInformation failed for %d\n", aPid
);
78 } // namespace hal_impl
79 } // namespace mozilla