summaryrefslogtreecommitdiffstats
path: root/hal/windows/WindowsProcessPriority.cpp
blob: fe9b78133b32bb62f086776eef67bde24d1e393e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* 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 "nsWindowsHelpers.h"  // for nsAutoHandle and nsModuleHandle
#include "mozilla/StaticPrefs_dom.h"

#include <windows.h>

using namespace mozilla::hal;

namespace mozilla {
namespace hal_impl {

void SetProcessPriority(int aPid, ProcessPriority aPriority) {
  HAL_LOG("WindowsProcessPriority - SetProcessPriority(%d, %s)\n", aPid,
          ProcessPriorityToString(aPriority));

  nsAutoHandle processHandle(
      ::OpenProcess(PROCESS_SET_INFORMATION, FALSE, aPid));
  if (processHandle) {
    DWORD priority = NORMAL_PRIORITY_CLASS;
    if (aPriority == PROCESS_PRIORITY_BACKGROUND) {
      priority = IDLE_PRIORITY_CLASS;
    } else if (aPriority == PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) {
      priority = BELOW_NORMAL_PRIORITY_CLASS;
    }

    if (::SetPriorityClass(processHandle, priority)) {
      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) {
      if (aPriority == PROCESS_PRIORITY_PARENT_PROCESS ||
          !StaticPrefs::dom_ipc_processPriorityManager_backgroundUsesEcoQoS()) {
        return;
      }

      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) &&
                StaticPrefs::
                    dom_ipc_processPriorityManager_backgroundUsesEcoQoS()
            ? 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);
    }
  }
}

}  // namespace hal_impl
}  // namespace mozilla