Get CPU usage and memory usage for the Windows operating system

Source: Internet
Author: User

This feature references the code for the Processhacker project.

Declaration definition
typedefstruct_uint64_delta{ULONG64 Value; ULONG64 Delta;} Uint64_delta,*Puint64_delta;typedefstruct_uintptr_delta{ulong_ptr Value; Ulong_ptr Delta;} Uintptr_delta,*Puintptr_delta;#defineInitializedelta (dltmgr) \((dltmgr)->value =0, (dltmgr)->delta =0)#defineUpdatedelta (dltmgr, newvalue) \((dltmgr)->delta = (newvalue)-(Dltmgr)Value, (dltmgr)->value = (newvalue), (Dltmgr),Delta) typedefstruct _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;typedefstruct_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;
Referencing Lib Files

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

Get operating System CPU utilization
ULONG64 Total_time =0; ULONG64 Sys_time=0;StaticSystem_processor_performance_information cpuinformation[1024x768];Staticsystem_info Sys_info;StaticUint64_delta Cpu_kernel_delta;StaticUint64_delta Cpu_user_delta;StaticUint64_delta Cpu_idle_delta;Staticsystem_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(inti =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) {returnSys_time *100.0/Total_time;}Else{    return 0.0;}
Get operating system memory size
Staticsystem_basic_information System_basic_info;Static Long LongMem_size =0;if(Mem_size! =0)    returnmem_size;StaticSystem_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/= (1024x768*1024x768);//MBreturnMem_size;
Get Operating system memory utilization
Staticsystem_performance_information Perf_info;StaticSystem_info Sys_info; GetSystemInfo (&sys_info); NtQuerySystemInformation (Systemperformanceinformation,&Perf_info,sizeof(system_performance_information), NULL);Long LongAvailable_size =Perf_info. Availablepages;available_size*=sys_info.dwpagesize;available_size/= (1024x768*1024x768);//MBLong LongMem_size =sys_mem_size ();if(Mem_size! =0){    return(mem_size-available_size) *100.0/mem_size;}Else{    return 0.0;}

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.