Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting CPU usage from within an application

0 views
Skip to first unread message

Jason Phillips

unread,
Aug 23, 1999, 3:00:00 AM8/23/99
to
I need to determine what the current System CPU load is during the execution
of my application. I have been attempting to wade through MSDN and the
endless array of Performance Structures. I'm sure someone has conquered
this problem already and I was wondering if anyone had any code for this.

By the way, to make matters worse,I need to do this for both Win98 and NT
4.0, which means I will need this coded up twice since each one has its own
structures, etc.

Thanks for any help,
Jason Phillips
jphi...@tecomac.com


Jason Phillips

unread,
Aug 23, 1999, 3:00:00 AM8/23/99
to
O.K. I have found out how to do this in Win9x:

if(versionInformation.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
HKEY hkey;
DWORD dwType;
DWORD result;
DWORD cbResult = 4;
if (RegOpenKey(HKEY_DYN_DATA, "PerfStats\\StatData", &hkey) ==
ERROR_SUCCESS)
{
if
RegQueryValueEx( hkey,"KERNEL\\CPUUSAGE",NULL,&dwType,(BYTE*)(&result),&cbRe
sult) == ERROR_SUCCESS)
{
if (dwType == REG_BINARY)
{
RegCloseKey(hkey);
cpuUsage = result;
}
}
RegCloseKey(hkey);
}
}

I have also been able to retrieve the "% Processor Time" counter under NT4.0
using the "Navigating the Performance Registry" example in MSDN.
Unfortunately, even though I have retrieved a counter, none of the formulas
that would make sense to use to calculate the current CPU usage seem to
work. I grab the counter data and the 100ns time value on two consecutive
requests. I then divide the counter difference by the time difference. I
always get an extremely small number ~< 0.1 as a result.

Does anyone have any suggestions?

Thanks,
Jason

Axel

unread,
Aug 23, 1999, 3:00:00 AM8/23/99
to

Jason Phillips <jphi...@tecomac.com> wrote in message
news:uc3qyqX7#GA.197@cppssbbsa04...

> O.K. I have found out how to do this in Win9x:
>
> if(versionInformation.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
> {
> HKEY hkey;
> DWORD dwType;
> DWORD result;
> DWORD cbResult = 4;
> if (RegOpenKey(HKEY_DYN_DATA, "PerfStats\\StatData", &hkey) ==
> ERROR_SUCCESS)
> {
> if
>
egQueryValueEx( hkey,"KERNEL\\CPUUSAGE",NULL,&dwType,(BYTE*)(&result),&cbRe
> sult) == ERROR_SUCCESS)
> {
> if (dwType == REG_BINARY)
> {
> RegCloseKey(hkey);
> cpuUsage = result;
> }
> }
> RegCloseKey(hkey);
> }
> }
>
> I have also been able to retrieve the "% Processor Time" counter under
NT4.0
> using the "Navigating the Performance Registry" example in MSDN.
> Unfortunately, even though I have retrieved a counter, none of the
formulas
> that would make sense to use to calculate the current CPU usage seem to
> work. I grab the counter data and the 100ns time value on two consecutive
> requests. I then divide the counter difference by the time difference. I
> always get an extremely small number ~< 0.1 as a result.
>
> Does anyone have any suggestions?

NT: The source code for PerfMon is in the Platform SDK. You may want to look
at it. Also, the PDH library could be of some help. You can look up "Using
the Performance Data Helper Library" in MSDN for more information.

Hope this helps,
--Axel


0 new messages