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

Posted 朝闻道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取Windows操作系统的CPU使用率以及内存使用率相关的知识,希望对你有一定的参考价值。

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

声明定义

 Collapse
复制代码
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使用率

 Collapse
复制代码
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;
}
复制代码

获取操作系统内存大小

 Collapse
复制代码
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;
复制代码

获取操作系统内存使用率

 Collapse
复制代码
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;
}
复制代码

http://www.cnblogs.com/hbccdf/p/get_sys_cpu_usage_and_mem_usage.html

以上是关于获取Windows操作系统的CPU使用率以及内存使用率的主要内容,如果未能解决你的问题,请参考以下文章

Windows系统CPU内存网络性能统计第一篇 内存

Windows下获取系统信息(CPU速度-总内存-显卡型号等)

请问怎么用c语言获取电脑(windows)的cpu,内存,硬盘利用率等信息?Linux系统一样吗?

python使用psutil获取系统(Windows Linux)所有运行进程信息实战:CPU时间内存使用量内存占用率PID名称创建时间等;

java如何获取系统内存、cpu等信息。

如何分析 Windows 和 Linux 上的应用程序以获取 CPU 和 RAM 使用值? [关闭]