CPU power state/speed, etc (Windows 2000/XP and PB 4)

Windows specific forum
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

CPU power state/speed, etc (Windows 2000/XP and PB 4)

Post by Hi-Toro »

I was reading about Intel SpeedStep and how to enable it on my new Core 2 Duo (if your motherboard/BIOS supports SpeedStep, you go to Control Panel -> Power Options and set the Power Scheme to Minimal Power Management). I found the Power Management API and ended up with this...

This demo obtains the current CPU power state (if SpeedStep is enabled and the Power Scheme is Minimal, it should show "dynamic throttling").

It also shows (and this 'should' work for any CPU on Windows 2000 or above) the maximum MHz and current MHz for each CPU in the system. On mine, with dynamic throttling enabled and low CPU usage, it correctly shows the current stepped-down speed. Running a CPU-hogging demo showed the cores stepping up (separately, interestingly, and it took a few seconds before they did so).

Not exactly sure if this is any use at all, but it was fun anyway. I *think* the demo will run on 2000, but you won't see the current power setting, that's all. If any of this doesn't work... oh well. ;)

Code: Select all

; Obtain CPU power state (XP only) and other useful information (2000 or above)...

; For PB 4...

; *** DEMO REQUIRES DEBUG ON! ***

; Processor Performance Control Policy Structures...

Structure PROCESSOR_POWER_POLICY_INFO
  TimeCheck.l
  DemoteLimit.l
  PromoteLimit.l
  DemotePercent.c
  PromotePercent.c
  Spare.c[2]
  AllowDemotion.l
  AllowPromotion.l
  Reserved.l
EndStructure

Structure PROCESSOR_POWER_POLICY
	Revision.l
	DynamicThrottle.b
	Spare.b[3]
	DisableCStates.l
	Reserved.l
	PolicyCount.l
	Policy.PROCESSOR_POWER_POLICY_INFO[3]
EndStructure

Structure MACHINE_PROCESSOR_POWER_POLICY
	Revision.l
	ProcessorPolicyAc.PROCESSOR_POWER_POLICY
	ProcessorPolicyDc.PROCESSOR_POWER_POLICY
EndStructure

Structure PROCESSOR_POWER_INFORMATION
	Number.l
	MaxMhz.l
	CurrentMhz.l
	MhzLimit.l
	MaxIdleState.l
	CurrentIdleState.l
EndStructure

; Processor Performance Control Policy Constants...

#PO_THROTTLE_NONE		= 0
#PO_THROTTLE_CONSTANT	= 1
#PO_THROTTLE_DEGRADE	= 2
#PO_THROTTLE_ADAPTIVE	= 3

Dim PowerState.s (3)

PowerState (0) = "No throttling"
PowerState (1) = "Constant throttling"
PowerState (2) = "Throttling according to battery, heat, etc"
PowerState (3) = "Dynamic throttling"

; D e m o . . .

Debug ""

; Power Management API reference:
; http://msdn2.microsoft.com/en-us/library/aa373163.aspx

pwr = OpenLibrary (#PB_Any, "powrprof.dll")

If pwr

	; Get functions...
	
	GetActivePwrScheme		= GetFunction (pwr, "GetActivePwrScheme")		; Requires 2000 or above
	CallNtPowerInformation	= GetFunction (pwr, "CallNtPowerInformation")	; Requires 2000 or above
	ReadProcessorPwrScheme	= GetFunction (pwr, "ReadProcessorPwrScheme")	; Requires *XP* or above

	; Get number of CPUs...
	
	GetSystemInfo_ (sys.SYSTEM_INFO)
	cpus = sys\dwNumberOfProcessors

	; Prepare buffer(s) for information...
	
	buffsize = SizeOf (PROCESSOR_POWER_INFORMATION) * cpus
	Dim buffer.PROCESSOR_POWER_INFORMATION (cpus - 1)

	; Get CPU information...
		
	CallFunctionFast (CallNtPowerInformation, 11, #Null, #Null, @buffer (0), buffsize)

	; Show CPU information...
	
	For cpu = 0 To cpus - 1
		Debug "CPU: " + Str (buffer (cpu)\Number)
		Debug ""
		Debug "Max MHz: " + Str (buffer (cpu)\MaxMhz)
		Debug "Current MHz: " + Str (buffer (cpu)\CurrentMhz)
		Debug "MHz Limit: " + Str (buffer (cpu)\MhzLimit)
		Debug "Max idle state: " + Str (buffer (cpu)\MaxIdleState)
		Debug "Current idle state: " + Str (buffer (cpu)\CurrentIdleState)
		Debug ""
	Next

	; Get CPU power state (XP only due to ReadProcessorPwrScheme () call)...
	
	If CallFunctionFast (GetActivePwrScheme, @pwrid)
	
		If ReadProcessorPwrScheme ; Should be Null on < XP!
		
			CallFunctionFast (ReadProcessorPwrScheme, pwrid, @policy.MACHINE_PROCESSOR_POWER_POLICY)
			Debug "CPU power state: " + PowerState (policy\ProcessorPolicyAc\DynamicThrottle)
			
		EndIf
		
	EndIf
		
	CloseLibrary (pwr)
	
EndIf

Debug ""
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Beautiful! 8)
BERESHEIT
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

CPU: 0

Max MHz: 2999
Current MHz: 2999
MHz Limit: 2999
Max idle state: 1
Current idle state: 0

CPU: 1

Max MHz: 2999
Current MHz: 2999
MHz Limit: 2999
Max idle state: 1
Current idle state: 0

CPU power state: No throttling
Sw33t!
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Love it!

- np
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Very good.

Enabled throttling on my core2duo, let it settle down and then ran a constant loop, one of the cores when back up to full speed like yours did but then after a few seconds it came back down again even though the loop was still running, probably cached or something.

Interesting though. :D
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Derek wrote:Very good.

Enabled throttling on my core2duo, let it settle down and then ran a constant loop, one of the cores when back up to full speed like yours did but then after a few seconds it came back down again even though the loop was still running, probably cached or something.

Interesting though. :D
If it was a normal looping, then that alone can cause it's own spikes. But
I have noticed that no matter what settings, or what way I test, even if
it's commercial software. It spikes now an again. One items that makes
mine go up every 8-10 seconds is my Norton AV/Internet Security.

The way that I'm going to test, using this code as my core. Is that I'll
use a timer, which will eliminate at least itself from spiking.

- np
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

What I did was just run a while-1:wend and ran the throttling code a few times and it jumped to full speed and then came back down, if I bring up the task manager and watch the cpu usage then one of the cores is basically being used completely (with a few little spikes).

I just wondered why that core would of throttled back, unless it just wasn't working enough to warrant the extra speed. It was only going at about 90%
Post Reply