Get CPU usage and memory usage for the Windows operating system

Source: Internet
Author: User

Read Catalogue

    • Declaration definition
    • Referencing Lib Files
    • Get operating System CPU utilization
    • Get operating system memory size
    • Get Operating system memory utilization

This feature references the code for the Processhacker project.

Back to top declaration definition 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;
Back to top reference lib file

The project needs to reference the ntdll.lib file, which can be found from the Processhacker project.

Get back to the top for operating system CPU utilization 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_PER formance_information) * (ULONG) sys_info.dwnumberofprocessors, NULL); for (int i = 0; i < (int) sys_info.dwnumberof processors;    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;}
Back to top get OS memory size Collapse
Static system_basic_information system_basic_info;static Long Long mem_size = 0;if (mem_size! = 0)    return mem_size;st Atic 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); Mbreturn mem_size;
Back to top get operating system memory utilization 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); Mblong 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

Get CPU usage and memory usage for the Windows operating system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.