c++ 获取windows cpu利用率

c++ 获取windows cpu利用率

工作中遇到,需要获取本机的cpu利用率,网上查了很多,获取数值大多不准确。在window10系统上得到的cpu利用率都不准确,window7上的准确。

#include <iostream>
#include <Windows.h>
#include<string>
using namespace std;
__int64 Filetime2Int64(const FILETIME &ftime)
{
	LARGE_INTEGER li;
	li.LowPart = ftime.dwLowDateTime;
	li.HighPart = ftime.dwHighDateTime;
	return li.QuadPart;
}

__int64 CompareFileTime2(const FILETIME &preTime, const FILETIME &nowTime)
{
	return Filetime2Int64(nowTime) - Filetime2Int64(preTime);
}
void get_CpuUsage()
{
	FILETIME preIdleTime;
	FILETIME preKernelTime;
	FILETIME preUserTime;
	GetSystemTimes(&preIdleTime, &preKernelTime, &preUserTime);
	while (true)
	{
		Sleep(1000);

		FILETIME idleTime;
		FILETIME kernelTime;
		FILETIME userTime;
		GetSystemTimes(&idleTime, &kernelTime, &userTime);

		auto idle = CompareFileTime2(preIdleTime, idleTime);
		auto kernel = CompareFileTime2(preKernelTime, kernelTime);
		auto user = CompareFileTime2(preUserTime, userTime);

		__int64 cpusage = 100*(kernel + user - idle) / (kernel + user);
		std::cout << "cpu利用率:" << cpusage << "%" << std::endl;
	}
}

后来上司出手教导之下,写出来的程序,发现在我这里依然不准确,同事那边可以,我怀疑是我电脑的问题了。

#define HARDWARE_PLATFORM_STAT_SYSTEMINFO
#include <winsock2.h> // include must before window.h
#include <windows.h>  
#include <iostream>
#include <sstream>

#include<stdio.h>

#include <stdlib.h>   //该头文件定义了一些通用函数
#include <httpext.h>   //该头文件支持HTTP请求
#include <windef.h>   //该头文件定义了Windows的所有数据基本型态
#include <Nb30.h>   //该头文件声明了netbios的所有的函数 
#include<string>
#include <Iphlpapi.h>
#include <Assert.h>
#include <Winternl.h>
#include<iostream>
using namespace std;
#include "windows.h"
#include "Wlanapi.h"
#pragma comment(lib, "Wlanapi.lib")
#pragma comment(lib,"ws2_32.lib")    //连接ws2_32.lib库.只要程序中用到Winsock API 函数,都要用到 Ws2_32.lib
#pragma comment(lib,"netapi32.lib")   //连接Netapi32.lib库,MAC获取中用到了NetApi32.DLL的功能
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib,"ntdll.lib")
#pragma warning(disable : 4996)
typedef struct _UINT64_DELTA
{
	ULONG64 Value;
	ULONG64 Delta;
} UINT64_DELTA, *PUINT64_DELTA;

typedef struct _UINTPTR_DELTA
{
	ULONG_PTR Value;
	ULONG_PTR Delta;
} UINTPTR_DELTA, *PUINTPTR_DELTA;

#define InitializeDelta(DltMgr) \
((DltMgr)->Value = 0, (DltMgr)->Delta = 0)

#define UpdateDelta(DltMgr, NewValue) \
((DltMgr)->Delta = (NewValue) - (DltMgr)->Value, \
(DltMgr)->Value = (NewValue), (DltMgr)->Delta)
ULONG64 total_time = 0;
ULONG64 sys_time = 0;
/*上述头文件和宏定义,不知道那个可用,哪个不可用,直接拿过来了*/
string GetSystemCpuUsePrecent()
{
	
	static SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION CpuInformation[1024] = { 0 };
	static SYSTEM_INFO sys_info;

	static UINT64_DELTA cpu_kernel_delta;
	static UINT64_DELTA cpu_user_delta;
	static UINT64_DELTA cpu_idle_delta;
	static SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION cpu_totals;
	memset(&cpu_totals, 0, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));

	GetSystemInfo(&sys_info);

	NtQuerySystemInformation(
		SystemProcessorPerformanceInformation,
		&CpuInformation,
		sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * (ULONG)sys_info.dwNumberOfProcessors,
		NULL
	);

	for (int i = 0; i < (int)sys_info.dwNumberOfProcessors; i++)
	{
		SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION& cpu_info = CpuInformation[i];

		// KernelTime includes idle time.
		LONGLONG dpc_time = cpu_info.Reserved1[0].QuadPart;
		LONGLONG interrupt_time = cpu_info.Reserved1[i].QuadPart;
		cpu_info.KernelTime.QuadPart -= cpu_info.IdleTime.QuadPart;
		cpu_info.KernelTime.QuadPart += dpc_time + interrupt_time;

		cpu_totals.Reserved1[0].QuadPart += dpc_time;
		cpu_totals.IdleTime.QuadPart += cpu_info.IdleTime.QuadPart;
		cpu_totals.Reserved2 += cpu_info.Reserved2;
		cpu_totals.Reserved1[1].QuadPart += cpu_info.Reserved1[1].QuadPart;
		cpu_totals.KernelTime.QuadPart += cpu_info.KernelTime.QuadPart;
		cpu_totals.UserTime.QuadPart += cpu_info.UserTime.QuadPart;
	}

	UpdateDelta(&cpu_kernel_delta, cpu_totals.KernelTime.QuadPart);
	UpdateDelta(&cpu_user_delta, cpu_totals.UserTime.QuadPart);
	UpdateDelta(&cpu_idle_delta, cpu_totals.IdleTime.QuadPart);

	total_time = cpu_kernel_delta.Delta + cpu_user_delta.Delta + cpu_idle_delta.Delta;
	sys_time = cpu_idle_delta.Delta;
	int cpu = 0;
	if (total_time)
	{
		cpu = 110 - sys_time * 100 / total_time;
		if (cpu < 0) {
			cpu = 0;
		}
		if (cpu > 100) {
			cpu = 100;
		}
	}
	return std::to_string(cpu);
}

博客做个记录,防止以后忘了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值