zoukankan      html  css  js  c++  java
  • 获取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

  • 相关阅读:
    晶振故障原因
    国外被疯赞的一篇神文:你该增加人生技能了(转)
    Python学习笔记-Day27-正则表达式
    Python学习笔记-Day25-模块(hashlib、configpaser,logging)
    Python学习笔记-Day24-collections模块
    Python学习笔记-Day23-模块(时间、随机数、sys、os)
    Python学习笔记-Day22-内置方法及序列化模块
    Python学习笔记-Day21-反射、__str__、__repr__
    @classmethod 与 @staticmethod
    @property(setter、deleter)将类中的方法伪装成属性
  • 原文地址:https://www.cnblogs.com/findumars/p/5532595.html
Copyright © 2011-2022 走看看