CPU power/frequency control (Windows)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

CPU power/frequency control (Windows)

Post by Lunasole »

Hi.
The following small code does what you can do manually through Windows control panel/power settings (Advanced Power Settings).
It changes the minimum/maximum allowed CPU frequency/power consumption.

I don't know for what you may need it^^ Maybe to artificially slow down your PC for some tests, maybe to make it silent as maximum fan speed is also reduced, or just to make it consuming less power when maximum performance is not needed. Doing that with a small program looks anyway some more reliable than changing this param manually every time.
It should work in Vista and higher.

PS. This is NOT related to overclocking, etc. This is built-in Windows CPU power management, it can't harm your hardware (unlike changing something in BIOS or using overclocking utility for example).
(also I hope this theme is not a duplicate. I'd like to post all of power management APIs which are not added to PB yet, but there are lot of them and I only used few)

Code: Select all

EnableExplicit

;{ GUIDS }
	
	Macro DEFINE_GUID(NAME, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11)
		Global NAME.GUID
		NAME\Data1 = v1
		NAME\Data2 = v2
		NAME\Data3 = v3
		NAME\Data4[0] = v4
		NAME\Data4[1] = v5
		NAME\Data4[2] = v6
		NAME\Data4[3] = v7
		NAME\Data4[4] = v8
		NAME\Data4[5] = v9
		NAME\Data4[6] = v10
		NAME\Data4[7] = v11
	EndMacro
	
	
	; Power schemes GUIDS
	DEFINE_GUID(GUID_TYPICAL_POWER_SAVINGS, $381B4222, $F694, $41F0, $96, $85, $FF, $5B, $B2, $60, $DF, $2E)
	DEFINE_GUID(GUID_MAX_POWER_SAVINGS, $A1841308, $3541, $4FAB, $BC, $81, $F7, $15, $56, $F2, $0B, $4A)
	DEFINE_GUID(ALL_POWERSCHEMES_GUID, $68A1E95E, $13EA, $41E1, $80, $11, $0C, $49, $6C, $A4, $90, $B0)
	
	; Processor settings group guid
	DEFINE_GUID(GUID_PROCESSOR_SETTINGS_SUBGROUP, $54533251, $82BE, $4824, $96, $C1, $47, $B6, $0B, $74, $0D, $00)
	
	; Processor settings values
	DEFINE_GUID(GUID_PROCESSOR_THROTTLE_MAXIMUM, $BC5038F7, $23E0, $4960, $96, $DA, $33, $AB, $AF, $59, $35, $EC)
	DEFINE_GUID(GUID_PROCESSOR_THROTTLE_MINIMUM, $893DEE8E, $2BEF, $41E0, $89, $C6, $B5, $5D, $09, $29, $96, $4C)
	
	
	; See all the guids in winnt.h (Windows SDK)
	
;}

;{ WinAPI }
	
	Prototype PowerWriteDCValueIndex(RootPowerKey.l, *SchemeGuid.GUID, *SubGroupOfPowerSettingsGuid.GUID, *PowerSettingGuid.GUID, DcValueIndex.l)
	Prototype PowerWriteACValueIndex(RootPowerKey.l, *SchemeGuid.GUID, *SubGroupOfPowerSettingsGuid.GUID, *PowerSettingGuid.GUID, AcValueIndex.l)
	Prototype GetActivePwrScheme(*puiID.Long)
	Prototype SetActivePwrScheme(puiID.l, *pGlobalPowerPolicy, *pPowerPolicy)
	
	Global GetActivePwrScheme.GetActivePwrScheme
	Global SetActivePwrScheme.SetActivePwrScheme
	Global PowerWriteDCValueIndex.PowerWriteDCValueIndex
	Global PowerWriteACValueIndex.PowerWriteDCValueIndex
	
	Define HLib = OpenLibrary(#PB_Any, "PowrProf.dll")
	If HLib
		GetActivePwrScheme = GetFunction(HLib, "GetActivePwrScheme")
		SetActivePwrScheme = GetFunction(HLib, "SetActivePwrScheme")
		PowerWriteDCValueIndex = GetFunction(HLib, "PowerWriteDCValueIndex")
		PowerWriteACValueIndex = GetFunction(HLib, "PowerWriteACValueIndex")
	EndIf
	
;}

; Set allowed CPU frequency
; minPC, maxPC		percentage of minimum and maximum CPU frequency (0 to 100)
; RETURN:			none
Procedure PowerSetCPUThrotle (minPC, maxPC)
	Protected T
	Debug GetActivePwrScheme(@T)
	; This is default if your PC uses AC power 
	Debug PowerWriteACValueIndex(0, ALL_POWERSCHEMES_GUID, GUID_PROCESSOR_SETTINGS_SUBGROUP, GUID_PROCESSOR_THROTTLE_MINIMUM, minPC)
	Debug PowerWriteACValueIndex(0, ALL_POWERSCHEMES_GUID, GUID_PROCESSOR_SETTINGS_SUBGROUP, GUID_PROCESSOR_THROTTLE_MAXIMUM, maxPC)
	; This should be power setting used for example with notebook battery (I'm not sure)
	Debug PowerWriteDCValueIndex(0, ALL_POWERSCHEMES_GUID, GUID_PROCESSOR_SETTINGS_SUBGROUP, GUID_PROCESSOR_THROTTLE_MINIMUM, minPC)
	Debug PowerWriteDCValueIndex(0, ALL_POWERSCHEMES_GUID, GUID_PROCESSOR_SETTINGS_SUBGROUP, GUID_PROCESSOR_THROTTLE_MAXIMUM, maxPC)
	Debug SetActivePwrScheme(T, 0, 0)
EndProcedure


; Usage
; Should be better to check which values are defined in your power scheme before call^^
; PowerSetCPUThrotle(5, 100)
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"