Skip to content

Commit

Permalink
Bug 1796525 - Set the QoS of background content processes to 'eco', r…
Browse files Browse the repository at this point in the history
…=gsvelto.

Differential Revision: https://phabricator.services.mozilla.com/D159835
  • Loading branch information
fqueze committed Oct 20, 2022
1 parent f01697c commit b443270
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion hal/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ 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.
# WindowsProcessPriority.cpp needs to set _WIN32_WINNT to Windows 8
SOURCES += [
"windows/WindowsBattery.cpp",
"windows/WindowsProcessPriority.cpp",
]
elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa":
UNIFIED_SOURCES += [
Expand Down
39 changes: 39 additions & 0 deletions hal/windows/WindowsProcessPriority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
* 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/. */

// SetProcessInformation is only defined for Win8 and newer.
#if defined(_WIN32_WINNT)
# undef _WIN32_WINNT
# define _WIN32_WINNT _WIN32_WINNT_WIN8
#endif // defined(_WIN32_WINNT)

#include "Hal.h"
#include "HalLog.h"
#include "nsWindowsHelpers.h" // for nsAutoHandle and nsModuleHandle

#include <windows.h>

Expand All @@ -30,6 +37,38 @@ void SetProcessPriority(int aPid, ProcessPriority aPriority) {
HAL_LOG("WindowsProcessPriority - priority set to %d for pid %d\n",
aPriority, aPid);
}

// Set the process into or out of EcoQoS.
static bool alreadyInitialized = false;
static decltype(::SetProcessInformation)* setProcessInformation = nullptr;
if (!alreadyInitialized) {
alreadyInitialized = true;
// SetProcessInformation only exists on Windows 8 and later.
nsModuleHandle module(LoadLibrary(L"Kernel32.dll"));
if (module) {
setProcessInformation =
(decltype(::SetProcessInformation)*)GetProcAddress(
module, "SetProcessInformation");
}
}
if (!setProcessInformation) {
return;
}

PROCESS_POWER_THROTTLING_STATE PowerThrottling;
RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
PowerThrottling.StateMask = aPriority == PROCESS_PRIORITY_BACKGROUND
? PROCESS_POWER_THROTTLING_EXECUTION_SPEED
: 0;
if (setProcessInformation(processHandle, ProcessPowerThrottling,
&PowerThrottling, sizeof(PowerThrottling))) {
HAL_LOG("SetProcessInformation(%d, %s)\n", aPid,
aPriority == PROCESS_PRIORITY_BACKGROUND ? "eco" : "normal");
} else {
HAL_LOG("SetProcessInformation failed for %d\n", aPid);
}
}
}

Expand Down

0 comments on commit b443270

Please sign in to comment.