C++获取CPU使用率
Posted anda0109
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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++获取CPU使用率的主要内容,如果未能解决你的问题,请参考以下文章
如何在 C++ 中获取当前的 CPU 和 RAM 使用情况?
使用 C++ 获取 Linux 中进程的 RAM 和 CPU 使用率
如何使用 C++ 从 macOS 的 Activity Monitor 应用程序获取 CPU、GPU 和 RAM 使用情况?