tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WindowsProcessPriority.cpp (2974B)


      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/. */
      4 
      5 #include "Hal.h"
      6 #include "HalLog.h"
      7 #include "nsWindowsHelpers.h"  // for nsAutoHandle and nsModuleHandle
      8 #include "mozilla/StaticPrefs_dom.h"
      9 
     10 #include <windows.h>
     11 
     12 using namespace mozilla::hal;
     13 
     14 namespace mozilla {
     15 namespace hal_impl {
     16 
     17 void SetProcessPriority(int aPid, ProcessPriority aPriority) {
     18  HAL_LOG("WindowsProcessPriority - SetProcessPriority(%d, %s)\n", aPid,
     19          ProcessPriorityToString(aPriority));
     20 
     21  nsAutoHandle processHandle(
     22      ::OpenProcess(PROCESS_SET_INFORMATION, FALSE, aPid));
     23  if (processHandle) {
     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;
     29    } else if (aPriority == PROCESS_PRIORITY_FOREGROUND_HIGH) {
     30      priority = ABOVE_NORMAL_PRIORITY_CLASS;
     31    }
     32 
     33    if (::SetPriorityClass(processHandle, priority)) {
     34      HAL_LOG("WindowsProcessPriority - priority set to %d for pid %d\n",
     35              aPriority, aPid);
     36    }
     37 
     38    // Set the process into or out of EcoQoS.
     39    static bool alreadyInitialized = false;
     40    static decltype(::SetProcessInformation)* setProcessInformation = nullptr;
     41    if (!alreadyInitialized) {
     42      if (aPriority == PROCESS_PRIORITY_PARENT_PROCESS ||
     43          !StaticPrefs::dom_ipc_processPriorityManager_backgroundUsesEcoQoS()) {
     44        return;
     45      }
     46 
     47      alreadyInitialized = true;
     48      // SetProcessInformation only exists on Windows 8 and later.
     49      nsModuleHandle module(LoadLibrary(L"Kernel32.dll"));
     50      if (module) {
     51        setProcessInformation =
     52            (decltype(::SetProcessInformation)*)GetProcAddress(
     53                module, "SetProcessInformation");
     54      }
     55    }
     56    if (!setProcessInformation) {
     57      return;
     58    }
     59 
     60    PROCESS_POWER_THROTTLING_STATE PowerThrottling;
     61    RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
     62    PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
     63    PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
     64    PowerThrottling.StateMask =
     65        (aPriority == PROCESS_PRIORITY_BACKGROUND) &&
     66                StaticPrefs::
     67                    dom_ipc_processPriorityManager_backgroundUsesEcoQoS()
     68            ? PROCESS_POWER_THROTTLING_EXECUTION_SPEED
     69            : 0;
     70    if (setProcessInformation(processHandle, ProcessPowerThrottling,
     71                              &PowerThrottling, sizeof(PowerThrottling))) {
     72      HAL_LOG("SetProcessInformation(%d, %s)\n", aPid,
     73              aPriority == PROCESS_PRIORITY_BACKGROUND ? "eco" : "normal");
     74    } else {
     75      HAL_LOG("SetProcessInformation failed for %d\n", aPid);
     76    }
     77  }
     78 }
     79 
     80 }  // namespace hal_impl
     81 }  // namespace mozilla