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;
}
相關文章
- C++獲取記憶體大小和使用率C++記憶體
- 如何獲取 Android CPU 核心數 (Java/C++)AndroidJavaC++
- mac獲取cpu資訊Mac
- java透過ip獲取遠端伺服器cpu使用率的程式碼Java伺服器
- Python 獲取檔案系統使用率Python
- Linux cpuinfo cpu資訊獲取詳解LinuxUI
- [Android]獲取整體APP CPU資訊AndroidAPP
- 實時獲取最耗CPU的SQLSQL
- Python 指令碼之獲取CPU資訊Python指令碼
- CPU使用率低負載高負載
- Java程式CPU使用率高排查Java
- Linux檢視CPU使用率Linux
- Android程式碼獲取CPU執行情況Android
- Win10怎麼看cpu使用率?Win10檢視CPU使用率的方法Win10
- ASP.NET獲取CPU序列號,硬碟序列號ID,獲取網路卡編號ASP.NET硬碟
- cpu使用率過高問題(Java)Java
- weblogic程式高CPU使用率分析WebC程式
- 如何在Ubuntu Linux中獲取CPU溫度UbuntuLinux
- 7種Linux中獲取CPU速度的方法Linux
- C++獲取陣列的長度C++陣列
- C++獲取硬體相關資訊C++
- c++ 如何獲取系統時間C++
- win10怎麼減少cpu使用率_win10cpu使用率過高的解決方法Win10
- Linux中如何檢視CPU使用率?Linux
- cpu使用率低負載高,原因分析負載
- 關於CPU使用率高的awr分析
- python kubernetes 獲取 pod 的 cpu 佔用率Python
- 獲取top N cpu pid的sql資訊指令碼SQL指令碼
- 在Linux中,如何獲取CPU的總核心數?Linux
- CPU快取快取
- Oracle CPU使用率過高問題處理Oracle
- .netcore利用perf分析高cpu使用率NetCore
- c++任意變數型別獲取相關C++變數型別
- C/C++獲取二維陣列行列數C++陣列
- C++ 獲取指定的過載函式地址C++函式
- 獲取C/C++字串、字元陣列長度C++字串字元陣列
- windows C++ 獲取使用者桌面路徑WindowsC++
- Winrm 獲取遠端windows伺服器 cpu、磁碟、記憶體Windows伺服器記憶體