编代码时用到判断系统是否处于空闲状态,要在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
}




Logo

更多推荐