//#define _WIN32_WINNT 0x500
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
//#include <Winternl.h>
#define SystemBasicInformation 0
#define SystemPerformanceInformation 2
#define SystemTimeInformation 3
#define SystemProcessorPerformanceInformation 8
#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
typedef struct
{
DWORD dwUnknown1;
ULONG uKeMaximumIncrement;
ULONG uPageSize;
ULONG uMmNumberOfPhysicalPages;
ULONG uMmLowestPhysicalPage;
ULONG uMmHighestPhysicalPage;
ULONG uAllocationGranularity;
PVOID pLowestUserAddress;
PVOID pMmHighestUserAddress;
ULONG uKeActiveProcessors;
BYTE bKeNumberProcessors;
BYTE bUnknown2;
WORD wUnknown3;
} SYSTEM_BASIC_INFORMATION;
//typedef struct
//{
// LARGE_INTEGER liIdleTime;
// DWORD dwSpare[76];
//} SYSTEM_PERFORMANCE_INFORMATION;
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, *PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
typedef struct
{
LARGE_INTEGER liKeBootTime;
LARGE_INTEGER liKeSystemTime;
LARGE_INTEGER liExpTimeZoneBias;
ULONG uCurrentTimeZoneId;
DWORD dwReserved;
} SYSTEM_TIME_INFORMATION;
typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);
PROCNTQSI NtQuerySysInformation;
//返回CPU数目
//参数Used为接收使用率的指针,len为可接收的个数
int GetCPUUsed(BYTE *Used, int len)
{
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SysProPerfInfo[8] = {0};
// SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo[8] = {0};
SYSTEM_TIME_INFORMATION SysTimeInfo = {0};
SYSTEM_BASIC_INFORMATION SysBaseInfo = {0};
double dbIdleTime = 0.0;
double dbSystemTime = 0.0;
LONG status;
static LARGE_INTEGER OldIdleTime[8] = {0};
static LARGE_INTEGER OldKernelTime[8] = {0};
// static LARGE_INTEGER liOldSystemTime = {0,0};
NtQuerySysInformation = (PROCNTQSI)GetProcAddress(GetModuleHandle("ntdll"),"NtQuerySystemInformation");
if (!NtQuerySysInformation) return -1;
// get number of processors in the system
status = NtQuerySysInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR) return -1;
// status = NtQuerySysInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
// if (status!=NO_ERROR) return -1;
// status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo[0])*SysBaseInfo.bKeNumberProcessors,NULL);
// if (status != NO_ERROR) return -1;
//
status = NtQuerySysInformation(SystemProcessorPerformanceInformation,
SysProPerfInfo,
sizeof(SysProPerfInfo[0])*SysBaseInfo.bKeNumberProcessors,
NULL);
if (status!=NO_ERROR) return -1;
if (OldIdleTime[0].QuadPart != 0)
{
for (int i = 0; i < SysBaseInfo.bKeNumberProcessors && i < len; i ++)
{
double dbKernelTime = Li2Double(SysProPerfInfo[i].KernelTime) - Li2Double(OldKernelTime[i]);
double dbIdleTime = Li2Double(SysProPerfInfo[i].IdleTime) - Li2Double(OldIdleTime[i]);
dbIdleTime = dbIdleTime / dbKernelTime;
double dbUsedTime = 100.0 - dbIdleTime * 100.0; // (double)SysBaseInfo.bKeNumberProcessors + 0.5;
if (dbUsedTime < 0) dbUsedTime = 0;
Used[i] = (BYTE)dbUsedTime;
// char msg[12] = {0};
// sprintf(msg, "%d ", Used[i]);
// printf(msg);
// OutputDebugString(msg);
}
printf("\n");
}
//改变新的CPU使用率和系统时间
for (int i = 0; i < SysBaseInfo.bKeNumberProcessors && i < len; i ++)
{
OldIdleTime[i] = SysProPerfInfo[i].IdleTime;
OldKernelTime[i] = SysProPerfInfo[i].KernelTime;
}
return SysBaseInfo.bKeNumberProcessors < len ? SysBaseInfo.bKeNumberProcessors : len;
// return 4;
}
int main()
{
while (1)
{
BYTE Used[8] = {0};
GetCPUUsed(Used, 8);
Sleep(1000);
}
return 0;
}
cpu-use_CPU使用率_源码
版权申诉
9 浏览量
2021-10-04
00:11:48
上传
评论
收藏 53KB RAR 举报
何欣颜
- 粉丝: 69
- 资源: 4732
最新资源
- 筷手引流工具.apk
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0