C++獲取CPU使用率

anda0109發表於2014-10-14

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;

}



相關文章