Windows 下使用PDH 获取CPU 使用率,网络使用率,内存使用率等
一、概述
使用pdh,获取windows下CPU占用率、网络占用率、内存占用率、网络上下行速度等。
说明:本篇文章为在网上收集资料并写代码测试整理后形成的文档,在实际测试过程中同样遇到了很多问题,为方便以后使用,现整理如下。
1、CPU占用率
HQUERY query;
PDH_FMT_COUNTERVALUE pdhValue;
DWORD dwValue;
HCOUNTER cpuCounter;
PDH_STATUS status = PdhAddCounter(query,TEXT("\\Processor Information(_Total)\\% Processor Times"),NULL,&&cpuCounter);
status = PdhGetFormattedCounterValue(cpuCounter,PDH_FMT_DOUBLE,&dwValue,&pdhValue);
if(status==ERROR_SUCCESS)
qDebug() << "CPU占用率=" << pdhValue.doubleValue << "%";
2、内存占用率
status = PdhAddCounter(query,TEXT("\\PhysicalDisk(_Total)\\% Disk Time"),NULL,&&memCounter);
status = PdhGetFormattedCounterValue(memCounter,PDH_FMT_DOUBLE,&dwValue,&pdhValue);
if(status==ERROR_SUCCESS)
qDebug() << "内存占用率=" << pdhValue.doubleValue << "%";
3、可用内存
status = PdhAddCounter(query,TEXT("\\Memory\\Available MBytes"),NULL,&&memAvaCounter);
status = PdhGetFormattedCounterValue(memAvaCounter,PDH_FMT_LONG,&dwValue,&pdhValue);
if(status==ERROR_SUCCESS)
qDebug() << "可用内存=" << pdhValue.longValue << "MB";
4、磁盘读速度
status = PdhAddCounter(query,TEXT("\\PhysicalDisk(_Total)\\Disk Read Bytes/sec"),NULL,&&diskReadCounter);
status = PdhGetFormattedCounterValue(diskReadCounter,PDH_FMT_DOUBLE,&dwValue,&pdhValue);
if(status==ERROR_SUCCESS)
qDebug() << "磁盘读速度=" << pdhValue.doubleValue/1024.0 << "K/s";
5、磁盘写速度
status = PdhAddCounter(query,TEXT("\\PhysicalDisk(_Total)\\Disk Write Bytes/sec"),NULL,&&diskWriteCounter);
status = PdhGetFormattedCounterValue(diskWriteCounter,PDH_FMT_DOUBLE,&dwValue,&pdhValue);
if(status==ERROR_SUCCESS)
qDebug() << "磁盘写速度=" << pdhValue.doubleValue/1024.0 << "K/s";
以下讲述网络相关资源信息 ///
说明:此处仅归类网络相关计数器信息,内部网卡信息(strInterface)获取可从下篇博文获取
6、网络接收速度(下载速度)
status = PdhAddCounter(query,(strInterface + L"Bytes Received/sec").c_str(),NULL,&&netRcvCounter);
status = PdhGetFormattedCounterValue(netRcvCounter,PDH_FMT_LARGE,&dwValue,&pdhValue);
if(status==ERROR_SUCCESS)
qDebug() << "每秒接收字节数=" << pdhValue.largeValue;
7、网络发送速度(上传速度)
status = PdhAddCounter(query,(strInterface + L"Bytes Sent/sec").c_str(),NULL,&&netSentCounter);
status = PdhGetFormattedCounterValue(netSentCounter,PDH_FMT_LARGE,&dwValue,&pdhValue);
if(status==ERROR_SUCCESS)
qDebug() << "每秒发送字节数=" << pdhValue.largeValue;
8、网络线路速度(网络带宽、链路速度)
status = PdhAddCounter(query,(strInterface + L"Current Bandwidth").c_str(),NULL,&&netBwCounter);
status = PdhGetFormattedCounterValue(netBwCounter,PDH_FMT_LARGE,&dwValue,&pdhBwValue);
if(status==ERROR_SUCCESS)
{
qDebug() << "网络带宽=" << pdhBwValue.largeValue;
qDebug() << "网卡:" << QString::fromStdString(strGetNet) << ",当前链路速度=" << pdhBwValue.largeValue/1000/1000 << "Mbps";
}
9、网络速度(每间隔总字节数)(用于计算网络占用率)
status = PdhAddCounter(query,(strInterface + L"Bytes Total/sec").c_str(),NULL,&&netSpeedCounter);
status = PdhGetFormattedCounterValue(netSpeedCounter,PDH_FMT_DOUBLE,&dwValue,&pdhSpeedValue);
if(status==ERROR_SUCCESS)
{
qDebug() << "每秒总字节数=" << pdhValue.doubleValue;
double dNeUseRate = (double)pdhSpeedValue.doubleValue*8/pdhBwValue.largeValue * 100.0;
qDebug() << "网卡:" << QString::fromStdString(strGetNet) << ",网络使用率=" << dNeUseRate << "%";
}