判断系统CPU是否空闲

编代码时用到判断系统是否处于空闲状态,要在Linux和Windows下都能使用。最初使用 GetSystemTimes函数进行判断,后来发现不能在Win2000下用。后来转用Performance API, Performance API提供了四种接口: WMI, ODBC, Registery, PDH。 Registry 感觉是最好用的,但实际上找到的资料太少,没用成。WMI是基于COM接口的,用C++调用 比较复杂。最后研究了半天,还是使用PDH接口。

bool systemIdle()
{
#ifdef WIN32
 DWORD   counterType;
 HQUERY          hQuery;
 PDH_STATUS      pdhStatus;
 HCOUNTER        hCounter;
 static PDH_RAW_COUNTER lastrawdata;
 static bool first = true;


 TCHAR           szCounterPath[] =
  TEXT("//Processor(_Total)//% Processor Time");

 /* We can use "% Idle Time" to retrieve IDL time directly, but this counter is available
  * on WindowsXP only. Fortunatelly, we can get IDL=100-Processer
  */
 //TCHAR           szCounterPath[] =
 // TEXT("//Processor(0)//% Idle Time");

 // Open a query object.
 pdhStatus = PdhOpenQuery (NULL, 0, &hQuery);
 if( pdhStatus != ERROR_SUCCESS )
 {
  return true;
 }

 // Add one counter that will provide the data.
 pdhStatus = PdhAddCounter (hQuery,
  szCounterPath,
  0,
  &hCounter);
 if( pdhStatus != ERROR_SUCCESS )
 {
  PdhCloseQuery(hQuery);
  return true;
 }


 PDH_RAW_COUNTER rawdata2;

 pdhStatus = PdhCollectQueryData(hQuery);
 if (pdhStatus != ERROR_SUCCESS)
 {
  PdhCloseQuery(hQuery);
  return true;
 }
 PdhGetRawCounterValue(hCounter, &counterType, &rawdata2);
 if(first)
 {
  lastrawdata = rawdata2;
  first = false;
  PdhCloseQuery(hQuery);
  return true;
 }

 pdhStatus = PdhCollectQueryData(hQuery);
 if (pdhStatus != ERROR_SUCCESS)
 {
  PdhCloseQuery(hQuery);
  return true;
 }
 PdhGetRawCounterValue(hCounter, &counterType, &rawdata2);


 PDH_FMT_COUNTERVALUE fmtValue;
 pdhStatus = PdhCalculateCounterFromRawValue(hCounter,
  PDH_FMT_DOUBLE,
  &rawdata2,
  &lastrawdata,
  &fmtValue);
 if (pdhStatus != ERROR_SUCCESS)
 {
  PdhCloseQuery(hQuery);
  return true;
 }
 lastrawdata = rawdata2;

 pdhStatus = PdhCloseQuery(hQuery);
 return fmtValue.doubleValue <= 20;
/*
 //Method 2, use GetSystemTimes, this is more Simple, but
 //GetSystemTimes is not available on Window2000 OS
 static __int64 last_idle = 0,last_kernel = 0,last_user = 0;
 __int64 idle, kernel, user;
 if(! GetSystemTimes((LPFILETIME)&idle,(LPFILETIME)&kernel,(LPFILETIME)&user))
 {
  ptrTrace("fail to call GetSystemTimes/r/n");
  return false;
 }
 //PdhOpenQuery
 double rate =  (double)(idle-last_idle)/(user-last_user+kernel-last_kernel);
 last_idle=idle;
 last_kernel = kernel;
 last_user = user;
 if( rate >= 0.8 )//more than 80% of system is in idle
  return true;
 return false;
*/
#else //Linux & UNIX
#define MAXLOAD 20 //that's to say, if the system overload is no more than 20%, it's idle
 char buf[4];
 int f=open("/proc/loadavg", O_RDONLY);
 if(f<0)
 {
  ptrTrace("fail to open file /proc/loadavg");
  return false;
 }
 if (read(f, buf, 4)!=4)
 {
  ptrTrace("fail to read file /proc/loadavg");
  close(f);
  return false;
 }
 close(f);
 return ((buf[0]=='0') && (atoi(buf+2))<MAXLOAD);
#endif
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值