最近在写一个网络和Cpu负荷率的实时监控程序,上网查了很多资料,实现方法也比较多,可以用GetIfTable/GetIfTable2、Performance Counter等,最后比较了下,选择了Performance Counter。MSDN上有详细介绍其API。
以下是我参考的一个写法:(http://en.literateprograms.org/CPU_usage_using_performance_counters_(C,_Windows_2000)#chunk use:cpuusageperfcnt.c)
主要是使用了PdhOpenQuery、PdhOpenQuery、PdhCollectQueryData、PdhGetFormattedCounterValue等几个API。
//------------------------------------------------------------------------------------------------------------------
// getcpuload()
// directly prints the CPU usage on screen. This function need to be called twice with a minimum of 1 seconds
// delay (msdn guideline) to display something usefull.
// Also returns the usage in percent 0-100 where 100 means the system is working at maximum capacity.
// Note for multiprocessor systems:
// If one CPU is working at max capacity the result will (if using (_total) for PdhAddCounter() ) show a maximum
// workload of 50% unless the other CPU(s) is also working at max load.
//------------------------------------------------------------------------------------------------------------------
INT getcpuload()
{
static PDH_STATUS status;
static PDH_FMT_COUNTERVALUE value;
static HQUERY query;
static HCOUNTER counter;
static DWORD ret;
static char runonce=1;
int cput=0;
if(runonce)
{
status = PdhOpenQuery(NULL, 0, &query);
if(status != ERROR_SUCCESS)
{
printf("PdhOpenQuery() ***Error: 0x%X\n",status);
return 0;
}
PdhOpenQuery(query, TEXT("\\Processor Information(_Total)\\% Processor Time"),0,&counter); // A total of ALL CPU's in the system(1)
//PdhAddCounter(query, TEXT("\\Processor(0)\\% Processor Time"),0,&counter); // For systems with more than one CPU (Cpu0)
//PdhAddCounter(query, TEXT("\\Processor(1)\\% Processor Time"),0,&counter); // For systems with more than one CPU (Cpu1)
runonce=0;
PdhCollectQueryData(query); // No error checking here
return 0;
}
status = PdhCollectQueryData(query);
if(status != ERROR_SUCCESS)
{
printf("PhdCollectQueryData() ***Error: 0x%X\n",status);
if(status==PDH_INVALID_HANDLE)
printf("PDH_INVALID_HANDLE\n");
else if(status==PDH_NO_DATA)
printf("PDH_NO_DATA\n");
else
printf("Unknown error\n");
return 0;
}
status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100 ,&ret, &value);
if(status != ERROR_SUCCESS)
{
printf("PdhGetFormattedCounterValue() ***Error: 0x%X\n",status);
return 0;
}
cput = value.doubleValue;
printf("\n\n" "CPU Total usage: %3d%%\n",cput);
qDebug()<<"\n CPU Total usage: "<<cput<<endl;
return cput;
}
int main(int argc, char *argv[])
{
//Entry point
while(1)
{
getcpuload();
Sleep(1000);
}
return a.exec();
}
上述代码是实现CPU占用率的实时监视,如果实现网络流量使用率只需将上述代码(1)处的绿色加深代码换成网络性能计数器(Performance Counter)的名字即可;
详细的性能计数器名字可以通过“控制面板-》管理工具-》性能监视器”添加相应的性能计数器即可,如下图所示:
如果需要监视网络流量,可以添加Network Interface性能计数器,添加后,选择Bytes Total/sec,如下图所示:
“最新”一栏显示的就是当前网络总的使用率,单位为Bytes,乘以8换算为bit,再除以总带宽,就是网络负荷率。总带宽可在任务管理器中查看。
另外可参考:http://bbs.csdn.net/topics/330221603
http://bbs.csdn.net/topics/310060693
http://www.cnblogs.com/upDOoGIS/archive/2010/11/19/1881970.html
http://hakuyo.blog.51cto.com/6207832/1218147
http://blog.csdn.net/morewindows/article/details/8678359