C++获取CPU使用率

1、使用Ntdll.dll函数

NTSTATUS WINAPI NtQuerySystemInformation(

  _In_       SYSTEM_INFORMATION_CLASS SystemInformationClass,
  _Inout_    PVOID SystemInformation,
  _In_       ULONG SystemInformationLength,
  _Out_opt_  PULONG ReturnLength
);

计算CPU使用率的方法为查询SystemBasicInformation和SystemPerformanceInformation两个参数进行计算。

SYSTEM_BASIC_INFORMATION

When the SystemInformationClass parameter is SystemBasicInformation, the buffer pointed to by the SystemInformation parameter should be large enough to hold a singleSYSTEM_BASIC_INFORMATION structure having the following layout:

typedef struct _SYSTEM_BASIC_INFORMATION {
    BYTE Reserved1[24];
    PVOID Reserved2[4];
    CCHAR NumberOfProcessors;
} SYSTEM_BASIC_INFORMATION;

The NumberOfProcessors member contains the number of processors present in the system. Use GetSystemInfo instead to retrieve this information.

The other members of the structure are reserved for internal use by the operating system.


SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION

When the SystemInformationClass parameter is SystemProcessorPerformanceInformation, the buffer pointed to by the SystemInformation parameter should be large enough to hold an array that contains as many SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION structures as there are processors (CPUs) installed in the system. Each structure has the following layout:

typedef struct
_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
    LARGE_INTEGER IdleTime;
    LARGE_INTEGER KernelTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER Reserved1[2];
    ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;

The IdleTime member contains the amount of time that the system has been idle, in 100-nanosecond intervals.

The KernelTime member contains the amount of time that the system has spent executing in Kernel mode (including all threads in all processes, on all processors), in 100-nanosecond intervals.

The UserTime member contains the amount of time that the system has spent executing in User mode (including all threads in all processes, on all processors), in 100-nanosecond intervals.

Use GetSystemTimes instead to retrieve this information.

Remarks

The NtQuerySystemInformation function and the structures that it returns are internal to the operating system and subject to change from one release of Windows to another. To maintain the compatibility of your application, it is better to use the alternate functions previously mentioned instead.

If you do use NtQuerySystemInformation, access the function through run-time dynamic linking. This gives your code an opportunity to respond gracefully if the function has been changed or removed from the operating system. Signature changes, however, may not be detectable.

This function has no associated import library. You must use the LoadLibrary and GetProcAddress functions to dynamically link to Ntdll.dll.

使用该函数必须动态加载Ntdll.dll库和导出函数,使用起来比较麻烦。微软在XP sp1以后提供了新的API接口函数。

上述两个参数可分别使用GetSystemInfo和GetSystemTimes函数代替。

详情参看MSDN:http://msdn.microsoft.com/en-us/library/ms724509.aspx

具体实现方法可以百度。

2、使用GetSystemTimes函数

//GetCpuUseage.h
#include <Windows.h>

class GetCpuUseage
{
public:
GetCpuUseage();
~GetCpuUseage();
public:
double CpuUseage();


private:
FILETIME m_preidleTime;
FILETIME m_prekernelTime;
FILETIME m_preuserTime;


};

//GetCpuUseage.cpp
#include "stdafx.h"
#include "GetCpuUseage.h"


__int64 CompareFileTime ( FILETIME time1, FILETIME time2 )
{
__int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime ;
__int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime ;


return   (b - a);
}


GetCpuUseage::GetCpuUseage()
{
GetSystemTimes( &m_preidleTime, &m_prekernelTime, &m_preuserTime);
Sleep(1000);
}


GetCpuUseage::~GetCpuUseage()
{


}


double GetCpuUseage::CpuUseage()
{
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
GetSystemTimes( &idleTime, &kernelTime, &userTime );


int idle = CompareFileTime( m_preidleTime,idleTime);
int kernel = CompareFileTime( m_prekernelTime, kernelTime);
int user = CompareFileTime(m_preuserTime, userTime);


if(kernel+user == 0)
return 0.0;
//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率
double cpu = (kernel +user - idle) *100/(kernel+user);

m_preidleTime = idleTime;
m_prekernelTime = kernelTime;
m_preuserTime = userTime ;
return cpu;
}

调用方法:

#include "GetCpuUseage.h"

#include <stdio.h>

int main()

{

GetCpuUseage getCpuUseage;

while(true)

{

printf("CPU使用率:%0.2f\n",getCpuUseage.CpuUseage());

sleep(1000);

}

return 0;

}



  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值