Page 1 of 1

Windows 11 'Efficiency Mode'

Posted: Sat Jul 12, 2025 3:00 am
by Hi-Toro
Hi all,

Just a quick test here, but seems to work for placing an application into what the Task Manager calls 'Efficiency mode' -- you can right-click a task, choose 'Efficiency mode' and it goes into a special low-resource usage state.

I wanted this as it seems to make a huge difference to MEGASync on startup, where it absolutely thrashes the CPU, but (of course) there's no way to set it permanently... hopefully this will allow me to run a little program on startup, wait for MEGA to appear, set it to Efficency mode and be done.

(Also... use Copilot or other AI for C/C++ to PB problem-solving!)

* UPDATED *
Now includes option to enable or disable. Note it assumes default priority class when turning off, not whatever the program was using -- might adapt later.

Code: Select all


; Sets 'Efficiency Mode' in Windows 11...

; Based on https://stackoverflow.com/questions/75049477/effects-of-programmatically-enabling-efficiency-mode-for-services-in-windows-1

; Thanks, Copilot!

Structure PROCESS_POWER_THROTTLING_STATE
  Version.l        ; ULONG
  ControlMask.l    ; ULONG
  StateMask.l      ; ULONG
EndStructure

; Thanks, infratec!

Import ""
  SetProcessInformation.l (hProcess.l, ProcessInformationClass.l, *ProcessInformation, ProcessInformationSize.l)
  GetProcessInformation.l (hProcess.l, ProcessInformationClass.l, *ProcessInformation, ProcessInformationSize.l)
EndImport

#PROCESS_POWER_THROTTLING_EXECUTION_SPEED = 1
#PROCESS_POWER_THROTTLING_CURRENT_VERSION = 1

Enumeration PROCESS_INFORMATION_CLASS
  #ProcessMemoryPriority
  #ProcessMemoryExhaustionInfo
  #ProcessAppMemoryInfo
  #ProcessInPrivateInfo
  #ProcessPowerThrottling
  #ProcessReservedValue1
  #ProcessTelemetryCoverageInfo
  #ProcessProtectionLevelInfo
  #ProcessLeapSecondInfo
  #ProcessMachineTypeInfo
  #ProcessOverrideSubsequentPrefetchParameter
  #ProcessMaxOverridePrefetchParameter
  #ProcessInformationClassMax
EndEnumeration

Structure EfficiencyMode
	class.i
	procinfo.i
EndStructure

; WIP!

; Procedure GetEfficiencyMode (process_id.i)
; 
; 	Define ppts.PROCESS_POWER_THROTTLING_STATE
; 	
; 		ppts\Version = #PROCESS_POWER_THROTTLING_CURRENT_VERSION
; 		ppts\ControlMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
; 		ppts\StateMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
; 
; 	process_handle.i = OpenProcess_ (#PROCESS_QUERY_INFORMATION, #False, process_id)
; 	
; 	If process_handle
; 
; 		class.i = GetPriorityClass_ (process_handle)
; 		
; 		GetProcessInformation (process_handle, #ProcessPowerThrottling, ppts, SizeOf (ppts))
; 		
; 		Debug ppts\Version
; 		Debug ppts\ControlMask
; 		Debug ppts\StateMask
; 
; 	EndIf
; 
; EndProcedure

; Enable = #True for on, #False for off...

Procedure SetEfficiencyMode (process_id.i, enable.i)

	Define ppts.PROCESS_POWER_THROTTLING_STATE
	
	process_handle.i = OpenProcess_ (#PROCESS_SET_INFORMATION | #PROCESS_QUERY_INFORMATION, #False, process_id)
	
	If process_handle

		GetProcessInformation (process_handle, #ProcessPowerThrottling, ppts, SizeOf (ppts))
		
		If enable
		
			ppts\Version = #PROCESS_POWER_THROTTLING_CURRENT_VERSION
			ppts\ControlMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
			ppts\StateMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED

			SetPriorityClass_ (process_handle, #IDLE_PRIORITY_CLASS)
			SetProcessInformation (process_handle, #ProcessPowerThrottling, ppts, SizeOf (ppts))
			
		Else
		
			; https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setprocessinformation
			; See: "The following example shows how to call SetProcessInformation with ProcessPowerThrottling to control the Quality of Service of a process."

			ppts\Version = #PROCESS_POWER_THROTTLING_CURRENT_VERSION
			ppts\ControlMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
			ppts\StateMask = 0

			SetPriorityClass_ (process_handle, #NORMAL_PRIORITY_CLASS) ; Ideally allow storage
			SetProcessInformation (process_handle, #ProcessPowerThrottling, ppts, SizeOf (ppts))
			
		EndIf

	EndIf
	
EndProcedure

; D E M O . . .

Global Window_0

Procedure OpenWindow_0(x = 0, y = 0, width = 400, height = 267)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Efficiency Mode - see Task Manager!", #PB_Window_SystemMenu)
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0 ()

;process_id.l = 6952 ; Current MEGASync!

process_id.l = GetCurrentProcessId_ () ; Or insert a PID from Task Manager

; GO!

SetEfficiencyMode (process_id, #True)

Repeat

	If Window_0_Events (WaitWindowEvent ()) = #False
		
		; Turn it back off!
		
		SetEfficiencyMode (process_id, #False)

		MessageRequester ("Efficiency mode!", "Turned off -- see in Task Manager before closing.")
		End
		
	EndIf
	
ForEver

You can see the result in Task Manager, where the Status 'leaf' appears and it greys-out the details...

Image

UPDATED to include option to disable it.

Re: Windows 11 'Efficiency Mode'

Posted: Sat Jul 12, 2025 11:56 am
by Quin
Nice, thanks for sharing! :)

Re: Windows 11 'Efficiency Mode'

Posted: Sat Jul 12, 2025 12:06 pm
by infratec
You don't need to open and close a dll ...

Code: Select all

Import ""
  SetProcessInformation.l(hProcess.l, ProcessInformationClass.l, *ProcessInformation, ProcessInformationSize.l)
EndImport


Procedure.i SetEfficiencyMode (process.i)
  
  Protected res.i
  Protected ppts.PROCESS_POWER_THROTTLING_STATE
  
  ppts\ControlMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
  ppts\StateMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
  ppts\Version = #PROCESS_POWER_THROTTLING_CURRENT_VERSION
  
  SetPriorityClass_ (process, #IDLE_PRIORITY_CLASS)
  
  res = SetProcessInformation(process, #ProcessPowerThrottling, @ppts, SizeOf (ppts))
  Debug res
  
  ProcedureReturn res
  
EndProcedure

Re: Windows 11 'Efficiency Mode'

Posted: Sat Jul 12, 2025 5:00 pm
by Hi-Toro
Thanks for posting that -- I had to open the DLL as my version doesn't seem to include SetProcessInformation_, so sounds like it's been added in an update. I'll update mine soon!

Re: Windows 11 'Efficiency Mode'

Posted: Sat Jul 12, 2025 5:06 pm
by Hi-Toro
Also, whoa... never seen this!

Code: Select all

Import ""
  SetProcessInformation.l(hProcess.l, ProcessInformationClass.l, *ProcessInformation, ProcessInformationSize.l)
EndImport
Interesting, will need to look into this option, thank you.

Re: Windows 11 'Efficiency Mode'

Posted: Sat Jul 12, 2025 7:35 pm
by Hi-Toro
Updated to allow enabling/disabling, see first post with * UPDATED * notes.

You can pass a process ID (PID in Task Manager) to amend other programs' states (may be admin/user-dependent) or use GetCurrentProcessId_ () to set your own program's state.

The function is now:

Code: Select all

SetEfficiencyMode (process_id.i, enable.i)
'enable' param:

#True for Efficiency Mode on
#False for Efficiency Mode off...

Re: Windows 11 'Efficiency Mode'

Posted: Mon Jul 14, 2025 11:00 am
by Axolotl
Thanks for sharing.

To be able to use the code as include file without deleting the demo program there is the following trick.
Enclose your demo program with the two instructions.

Code: Select all

CompilerIf #PB_Compiler_IsMainFile 

; your demo code goes here... 

CompilerEndIf