OEMIdle(  DWORD dwIdleParam )

This function is called by the kernel to place the CPU in the idle state when there are no threads ready to run.

The number of milliseconds that the system has been idle. If GetIdleTime returns MAXDWORD, this functionality is  

not supported by your platform.

GetIdleTime(void)

This function returns the amount of time, in milliseconds, that the system has been idle.

在OEMIdle()函数中OEM根据(int) (dwRescheduleTime – CurMSec) 计算值判断是否让CPU进入idle状态,因此我们可以利用此函数来计算cpu的loading. 根据MSDN ,可以写个应用程序,每100ms去计算一次idle的时间,10次评价后,在UI上更新,以下是应用程序的一个线程!

static void CallCountCpuIdleThread(CCountCpuDlg *pCCountCpuDlg)
{
DWORD dwStartTick;
DWORD dwIdleStart;
DWORD dwStopTick;
DWORD dwIdleEnd;
int   nPresentIdle;
int   nCpuUage[10]={0};
int   nCount=0;
int   ave=0;
DWORD sum=0;
int i;

while(1)
{
  dwStartTick=GetTickCount();
  dwIdleStart=GetIdleTime();
  Sleep(100);
  dwStopTick=GetTickCount();
  dwIdleEnd=GetIdleTime();

  nPresentIdle=(100*(dwIdleEnd-dwIdleStart))/((dwStopTick-dwStartTick));
  if(nCount<10)
  {
   nCpuUage[nCount]=nPresentIdle;
   nCount++;
   if(nCount==10)
   {
    for(i=0;i<10;i++)
     sum+=nCpuUage[i];
    ave=sum/10;
    // g_nCpuIdle only read in the main thread
    g_nCpuIdle=ave;
    nCount=0;
    sum=0;
    //RETAILMSG(1,(TEXT("CPU laoding is %d \r\n"),ave));
    } 
  }
}
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yjy889/archive/2009/11/29/4902234.aspx

posted on 2010-05-31 10:45  xilentz  阅读(933)  评论(0编辑  收藏  举报