获取Windows操作系统的CPU使用率以及内存使用率

时间:2022-09-05 18:51:08

此功能参考了ProcessHacker项目的代码。

声明定义

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) typedef struct _SYSTEM_PERFORMANCE_INFORMATION
{
LARGE_INTEGER IdleProcessTime;
LARGE_INTEGER IoReadTransferCount;
LARGE_INTEGER IoWriteTransferCount;
LARGE_INTEGER IoOtherTransferCount;
ULONG IoReadOperationCount;
ULONG IoWriteOperationCount;
ULONG IoOtherOperationCount;
ULONG AvailablePages;
ULONG CommittedPages;
ULONG CommitLimit;
ULONG PeakCommitment;
ULONG PageFaultCount;
ULONG CopyOnWriteCount;
ULONG TransitionCount;
ULONG CacheTransitionCount;
ULONG DemandZeroCount;
ULONG PageReadCount;
ULONG PageReadIoCount;
ULONG CacheReadCount;
ULONG CacheIoCount;
ULONG DirtyPagesWriteCount;
ULONG DirtyWriteIoCount;
ULONG MappedPagesWriteCount;
ULONG MappedWriteIoCount;
ULONG PagedPoolPages;
ULONG NonPagedPoolPages;
ULONG PagedPoolAllocs;
ULONG PagedPoolFrees;
ULONG NonPagedPoolAllocs;
ULONG NonPagedPoolFrees;
ULONG FreeSystemPtes;
ULONG ResidentSystemCodePage;
ULONG TotalSystemDriverPages;
ULONG TotalSystemCodePages;
ULONG NonPagedPoolLookasideHits;
ULONG PagedPoolLookasideHits;
ULONG AvailablePagedPoolPages;
ULONG ResidentSystemCachePage;
ULONG ResidentPagedPoolPage;
ULONG ResidentSystemDriverPage;
ULONG CcFastReadNoWait;
ULONG CcFastReadWait;
ULONG CcFastReadResourceMiss;
ULONG CcFastReadNotPossible;
ULONG CcFastMdlReadNoWait;
ULONG CcFastMdlReadWait;
ULONG CcFastMdlReadResourceMiss;
ULONG CcFastMdlReadNotPossible;
ULONG CcMapDataNoWait;
ULONG CcMapDataWait;
ULONG CcMapDataNoWaitMiss;
ULONG CcMapDataWaitMiss;
ULONG CcPinMappedDataCount;
ULONG CcPinReadNoWait;
ULONG CcPinReadWait;
ULONG CcPinReadNoWaitMiss;
ULONG CcPinReadWaitMiss;
ULONG CcCopyReadNoWait;
ULONG CcCopyReadWait;
ULONG CcCopyReadNoWaitMiss;
ULONG CcCopyReadWaitMiss;
ULONG CcMdlReadNoWait;
ULONG CcMdlReadWait;
ULONG CcMdlReadNoWaitMiss;
ULONG CcMdlReadWaitMiss;
ULONG CcReadAheadIos;
ULONG CcLazyWriteIos;
ULONG CcLazyWritePages;
ULONG CcDataFlushes;
ULONG CcDataPages;
ULONG ContextSwitches;
ULONG FirstLevelTbFills;
ULONG SecondLevelTbFills;
ULONG SystemCalls;
} SYSTEM_PERFORMANCE_INFORMATION, *PSYSTEM_PERFORMANCE_INFORMATION; typedef struct _SYSTEM_BASIC_INFORMATION
{
ULONG Reserved;
ULONG TimerResolution;
ULONG PageSize;
ULONG NumberOfPhysicalPages;
ULONG LowestPhysicalPageNumber;
ULONG HighestPhysicalPageNumber;
ULONG AllocationGranularity;
ULONG_PTR MinimumUserModeAddress;
ULONG_PTR MaximumUserModeAddress;
ULONG_PTR ActiveProcessorsAffinityMask;
CCHAR NumberOfProcessors;
} SYSTEM_BASIC_INFORMATION, *PSYSTEM_BASIC_INFORMATION;

引用lib文件

项目中需要引用ntdll.lib文件,此文件可以从ProcessHacker项目中找到。

获取操作系统CPU使用率

ULONG64 total_time = 0;
ULONG64 sys_time = 0;
static SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION CpuInformation[1024];
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_kernel_delta.Delta + cpu_user_delta.Delta; if (total_time)
{
return sys_time * 100.0 / total_time;
}
else
{
return 0.0;
}

获取操作系统内存大小

static SYSTEM_BASIC_INFORMATION system_basic_info;
static long long mem_size = 0;
if (mem_size != 0)
return mem_size; static SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info); NtQuerySystemInformation(
SystemBasicInformation,
&system_basic_info,
sizeof(SYSTEM_BASIC_INFORMATION),
NULL);
mem_size = system_basic_info.NumberOfPhysicalPages;
mem_size *= sys_info.dwPageSize;
mem_size /= (1024 * 1024); //MB
return mem_size;

获取操作系统内存使用率

static SYSTEM_PERFORMANCE_INFORMATION perf_info;
static SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info); NtQuerySystemInformation(
SystemPerformanceInformation,
&perf_info,
sizeof(SYSTEM_PERFORMANCE_INFORMATION),
NULL);
long long available_size = perf_info.AvailablePages;
available_size *= sys_info.dwPageSize;
available_size /= (1024 * 1024); //MB
long long mem_size = sys_mem_size();
if (mem_size != 0)
{
return (mem_size - available_size) * 100.0 / mem_size;
}
else
{
return 0.0;
}

获取Windows操作系统的CPU使用率以及内存使用率的更多相关文章

  1. Windows Server 2008 R2 服务器内存使用率过高几乎耗光

    系统环境: Windows Server 2008 R2 Enterprise 搭建有 web服务器(iis) 和  文件服务   问题描述: Windows Server 2008 R2系统内存耗光 ...

  2. C&num; 获取Windows系统:Cpu使用率,内存使用率,Mac地址,磁盘使用率

    一.获取CPU使用率: #region 获取CPU使用率         #region AIP声明          [DllImport("IpHlpApi.dll")]   ...

  3. PHP 之获取Windows下CPU、内存的使用率

    <?php /** * Created by PhpStorm. * User: 25754 * Date: 2019/5/4 * Time: 13:42 */ class SystemInfo ...

  4. linux下实现CPU使用率和内存使用率获取方法

    想获取一下目标机运行时linux系统的硬件占用情况,写了这几个小程序,以后直接用了. 方法就是读取proc下的文件来获取了. cpu使用率:    /proc/stat ,内存使用情况:     /p ...

  5. C&sol;C&plus;&plus;获取Windows系统CPU和内存及硬盘使用情况

    //1.获取Windows系统内存使用率 //windows 内存 使用率 DWORD getWin_MemUsage(){ MEMORYSTATUS ms; ::GlobalMemoryStatus ...

  6. 【Azure Developer】通过Azure提供的Azue Java JDK 查询虚拟机的CPU使用率和内存使用率

    问题描述 在Azure上创建虚拟机(VM)后,在门户上可以查看监控指标(Metrics),如CPU Usage,Memory,Disk I/O等.那如何通过Java 代码获取到这些指标呢? 关于VM ...

  7. Windows Server 2008 R2服务器内存使用率过高,但与任务管理器中进程占用内存和不一致

    系统环境: Windows Server 2008 R2 + Sql Server 2008 R2   问题描述: Windows Server 2008 R2系统内存占用率过大,而在任务管理器中各进 ...

  8. 编程获取linux的CPU使用的内存使用情况

    Linux可用下top.ps命令检查当前的cpu.mem用法.下面简单的例子: 一.采用ps查看资源消耗的过程 ps -aux 当您查看进程信息,第三列是CPU入住. [root@localhost ...

  9. qt 获取磁盘空间大小,cpu利用率,内存使用率

    转自:http://www.qtcn.org/bbs/read-htm-tid-60613.html. 1:封装成一个类,直接调用即可.已经在多个商业项目中使用.2:所有功能全平台 win linux ...

随机推荐

  1. uva146 ID码

    /*极水的题...*/ #include"iostream"#include"stdio.h"#include"stdlib.h"#incl ...

  2. 实现multbandblend

    一.首先实现 laplacian金字塔的分割和重构 #include "stdafx.h" #include <iostream> #include <vecto ...

  3. &lbrack;主席树&rsqb;HDOJ4417 Super Mario

    题意:n个数 m个询问  ($n.m \le 10^5$) 每个询问有l, r, k  问的是[l, r]区间内有多少个数小于等于k 用主席树做的话查询第i小的数与k比较即可 #define lson ...

  4. Pycharm配置(一)

    Pycharm作为一款强力的Python IDE,在使用过程中感觉一直找不到全面完整的参考手册,因此决定对官网的Pycharm教程进行简要翻译,与大家分享. 1.准备工作 官网下载 2.如何选择Pyc ...

  5. Libevent源码分析 &lpar;1&rpar; hello-world

    Libevent源码分析 (1) hello-world ⑨月份接触了久闻大名的libevent,当时想读读源码,可是由于事情比较多一直没有时间,现在手头的东西基本告一段落了,我准备读读libeven ...

  6. SecurityError:Error &num;2048:安全沙箱冲突

    1.错误描述 SecurityError:Error #2048:安全沙箱冲突:http://localhost:8080/YHD/flash/YHD.swf 不能从 http://123.89.45 ...

  7. 【linux】Can&&num;39&semi;t connect to local MySQL server through socket和Plugin &&num;39&semi;auth&lowbar;socket&&num;39&semi; is not loaded报错

    真的是一次吐血的经历,弄了两个多小时才弄好. 问题1:直接登陆root用户报错 ERROR 2002 (HY000): Can't connect to local MySQL server thro ...

  8. SPSS简单使用

    当我们的调查问卷在把调查数据拿回来后,我们该做的工作就是用相关的统计软件进行处理,在此,我们以spss为处理软件,来简要说明一下问卷的处理过程,它的过程大致可分为四个过程:定义变量.数据录入.统计分析 ...

  9. 【Redis】命令学习笔记——字符串(String)(23个超全字典版)

    Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 本篇基于redis 4.0.11版本,学习字符串( ...

  10. jsp 的 4 种基本语法

    1.JSP 注释 2.JSP 声明 3.JSP 表达式 4.JSP 脚本 JSP 注释: 注释格式: <%-- 注释内容 --%> 需要注意的是,JSP 的注释不会输出到 HTML 中. ...