2008年4月4日

特定进程cpu占用率

Introduction
The information in this article applies to Windows NT, Win2K/XP. There is no specific Win32 API that retrieves the CPU usage. An undocumented API, NtQuerySystemInformation in ntdll.dll, would help us retrieve the CPU usage. However, CPU usage can be retrieved by using performance counters. Since PDH.dll (Performance Data Helper) is not distributed with the Visual Studio, and not everyone has this file, I decided to do it without the help of PDH.dll.

The CPU usage counter is of type PERF_100NSEC_TIMER_INV which has the following calculation:


100*(1-(X1-X0)/(Y1-Y0))
X - CounterData
Y - 100NsTime
Time base - 100Ns


where the denominator (Y) represents the total elapsed time of the sample interval and the numerator (X) represents the time during the interval when the monitored components were inactive.

My CCpuUsage class has a method called GetCpuUsage which runs through the performance objects and counters and retrieves the CPU usage. Since the CPU usage can be determined by two samplings, the first call to GetCpuUsage() returns 0, and all calls thereafter returns the CPU usage.

Comment
On Windows NT, CPU usage counter is '% Total processor time' whose index is 240 under 'System' object whose index is 2. However, in Win2K/XP, Microsoft moved that counter to '% processor time' whose index is 6 under '_Total' instance of 'Processor' object whose index is 238. Read 'INFO: Percent Total Performance Counter Changes on Windows 2000' (Q259390) in MSDN.


There is no difference between WinNT and Win2K/XP in the performance counters for getting CPU usage for a specific process. The counter '% processor time' whose index is 6 under the object 'Process' whose index is 230.

The Sample


#include "CpuUsage.h"

int main(int argc, char* argv[])
{
int processID=0;
CCpuUsage usageA;
CCpuUsage usageB;
CCpuUsage usageC;

printf("SystemWide Cpu Usage "
"Explorer cpu usage "
"Cpu Usage for processID 0\n");
printf("==================== "
"================== "
"========================\n");
while (true)
{
// Display the system-wide cpu usage and the "Explorer" cpu usage

int SystemWideCpuUsage = usageA.GetCpuUsage();
int ProcessCpuUsageByName = usageB.GetCpuUsage("explorer");
int ProcessCpuUsageByID = usageC.GetCpuUsage(processID);
printf("%19d%%%22d%%%31d%%\r",SystemWideCpuUsage,
ProcessCpuUsageByName, ProcessCpuUsageByID);

Sleep(1000);
}
return 0;
}

src
demo

没有评论: