Retrieving Current CPU Clock Speed Under Windows
About 4 years ago I finally bit the bullet and purchased my first laptop. I was always bothered that I could build a top-of-the-line desktop computer for so much cheaper (about a third of the cost of this particular laptop), which is mainly why I've mostly been a desktop guy my whole life. I really needed portability though so I finally caved.
I was aware that laptop CPUs employed frequency scaling to conserve power (i.e. SpeedStep). When my AC adapter wasn't plugged in I noticed that my CPU speed was much lower than its "native" clock speed under Windows System Properties. I thought it might be fun to write a program to give me my current clock speed, because I found myself repeating the same steps often to pull up System Properties because I was curious what the current clock speed was. I originally intended to make a little applet that ran in the system tray that would give me a tooltip with the current CPU speed or something, but I never ended up completing it. I did, however, find out how to get this information programatically through the Win32 API and made a small command-line program to test it.
Initially I had a real problem finding any really
useful information. After lots of googling I must have
hit the right combination of search keywords, because I
finally stumbled upon
CallNtPowerInformation. To use it, one
must include powrprof.h and link with
powrprof.lib. However, there are a few
catches...
First, powrprof.h hasn't been C++-proofed
like most of the other headers in the Platform SDK.
You'll get linker errors if you don't take care before
including it in a C++ program:
extern "C" { #include <powrprof.h> }
The second issue is a structure that is for some reason
missing from powrprof.h,
PROCESSOR_POWER_INFORMATION. I couldn't
find it anywhere 4 years ago, and apparently it's still
missing from my current copy of the Platform SDK that
comes with Visual C++ 2008 Express Edition. In order to
retrieve current processor speed, we need to call
CallNtPowerInformation with the
ProcessorInformation enumerated value as
the first parameter and an array of
PROCESSOR_POWER_INFORMATION structures as
the output buffer (one for each installed
processor/core in the system). Luckily for us, the
structure is defined in MSDN so we can just define it
ourselves:
typedef struct _PROCESSOR_POWER_INFORMATION { ULONG Number; ULONG MaxMhz; ULONG CurrentMhz; ULONG MhzLimit; ULONG MaxIdleState; ULONG CurrentIdleState; } PROCESSOR_POWER_INFORMATION , *PPROCESSOR_POWER_INFORMATION;
As you can see from the definition of
PROCESSOR_POWER_INFORMATION, this
structure provides with CPU speed information such as
the maximum speed, current speed, and idle states
(although I admit I don't know much about the idle
states bit--I should probably research it).
A related API function that is also useful is
GetPwrCapabilities, which tells us whether
or not the processor supports frequency
throttling/scaling/SpeedStep and what the throttling
percentages are.
Comments
5 comments:
where would one download the 'and made a small command-line program to test it' that you speak of?
I don't have it available for download at the present time. Feel free to email me (brad.fish at gmail.com) and maybe I can dig it up for you.
A number of people have emailed me regarding the source code. It's simple enough, so I'll just post it here.
First, the header:
/** cpustat.h -- Header for cpustat.cpp. * Copyright (c) 2004 Brad Fish (brad.fish@gmail.com). */ #if !defined(MAIN_H) #define MAIN_H #include <windows.h> // missing Windows processor power information struct typedef struct _PROCESSOR_POWER_INFORMATION { ULONG Number; ULONG MaxMhz; ULONG CurrentMhz; ULONG MhzLimit; ULONG MaxIdleState; ULONG CurrentIdleState; } PROCESSOR_POWER_INFORMATION , *PPROCESSOR_POWER_INFORMATION; int main (int argc, char *argv[]); #endif // MAIN_HSee the next comment for the implementation (apparently Django's comment package doesn't like comments larger than 3000 characters).
And the implementation file:
Hi,
Thanks for code. There is a bug detecting clock speed when BIOS EISP function is enabled.
Post a Comment
Fields marked with * are required.