Windows CPU Memory Network Performance Statistics Article 2 Overall CPU usage

Source: Internet
Author: User
Tags filetime

This article supporting procedures: http://download.csdn.net/detail/morewindows/5160810

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8678359

Welcome to Weibo: http://weibo.com/MoreWindows

 

The previous "Windows system CPU memory network performance statistics first memory" (http://blog.csdn.net/morewindows/article/details/8459219) introduces how to view the total memory in the Windows system, usage and other information. Next, we will use three blogs to introduce how to obtain the overall CPU usage and the usage of each core of a multi-core CPU.

Windows system CPU Memory Network Performance Statistics blog directory:

1. Windows system CPU Memory Network Performance Statistics Article 1 memory

Http://blog.csdn.net/morewindows/article/details/8459219

2. Windows CPU Memory Network Performance Statistics Article 2 Overall CPU usage

Http://blog.csdn.net/morewindows/article/details/8678359

3. Windows system CPU Memory Network Performance Statistics Article 3 CPU multi-core CPU core usage C #

Http://blog.csdn.net/morewindows/article/details/8678382

4. Windows system CPU Memory Network Performance Statistics Article 4 CPU multi-core CPU core usage C ++

Http://blog.csdn.net/morewindows/article/details/8678396

 

There have been many articles on how to obtain the overall CPU usage and the usage of each core of a multi-core CPU on the Internet, and many people have asked questions on various forums. I searched online. Many previous articles have introduced the use of an undisclosed API function called ntquerysysteminformation in Ntdll. dll. The basic code structure is as follows:

// Obtain system CPU usage by morewindows (http://blog.csdn.net/MoreWindows)

Typedef long (winapi * procntqsi) (uint, pvoid, ulong, Pulong );

Pfunntquerysysteminf = (procntqsi) getprocaddress (getmodulehandle ("NTDLL"), "ntquerysysteminformation ");

This method is definitely not good. Since this API function is not public, it indicates that Microsoft does not support using this function directly when developing applications, and NTDLL will be used in future system upgrades. DLL is likely to remove this API function. After my morewindows (http://weibo.com/MoreWindows) field test, the use of ntquerysysteminformation programs in win7 can not get CPU usage.

In order to make our code run normally in WINXP and win7 systems. I implemented a ccpuuserate class. The following is the implementation code of the ccpuuserate class:

// Get system CPU usage // http://blog.csdn.net/morewindows/article/details/8678359// by morewindows (http://blog.csdn.net/MoreWindows) // call initialize () first, then while (true) {sleep (1000); getcpuuserate ();} you can get the CPU usage. // After testing, it can be used in WINXP and win7. Class ccpuuserate {public: bool initialize () {filetime ftidle, ftkernel, ftuser; bool flag = false; If (flag = getsystemtimes (& ftidle, & ftkernel, & ftuser )) {m_foldcpuidletime = filetimetodouble (ftidle); m_foldcpukerneltime = filetimetodouble (ftkernel); m_foldcpuusertime = filetimetodouble (ftuser);} return flag ;} // After initialize is called, wait 1 second before calling this function int getcpuuserate () {int ncpuuserate =-1; filetime ftidle, ftkernel, ftuser; If (getsystemtimes (& ftidle, & ftkernel, & ftuser) {double primary = filetimetodouble (ftidle); double fcpukerneltime = filetimetodouble (ftkernel); double fcpuusertime = filetimetodouble (ftuser); ncpuuserate = (INT) (100.0-(fcpuidletime-percent)/(fcpukerneltime-percent + fcpuusertime-m_foldcpuusertime) * 100.0); percent = fcpuidletime; percent = fcpukerneltime; percent = fcpuusertime;} return ncpuuserate ;} PRIVATE: Double filetimetodouble (filetime & filetime) {return (double) (filetime. dwhighdatetime * 4.294967296e9) + (double) filetime. dwlowdatetime;} PRIVATE: Double m_foldcpuidletime; double m_foldcpukerneltime; double m_foldcpuusertime ;};

This class is very simple to use. Call initialize () first, and then use code similar to the following

While (true)

{

Sleep (1000 );

Getcpuuserate ();

} To get the CPU usage.

The following is an example of use, the program printf \ r can refer to the C/C ++ in the console to display the progress (http://blog.csdn.net/morewindows/article/details/6742078 ):

// Windows system CPU Memory Network Performance Statistics second article CPU overall usage // tested, can be used in WINXP and win7 // http://blog.csdn.net/morewindows/article/details/8678359#include <windows. h> # include <stdio. h> # include <conio. h> int main () {printf ("Windows system CPU Memory Network Performance Statistics second article CPU overall CPU usage \ n"); printf ("-http://blog.csdn.net/morewindows/article/details/8678359-\ n "); printf ("-- by morewindows (http://blog.csdn.net/MoreWindows) -- \ n"); ccpuuserate CPU Userate; If (! Cpuuserate. initialize () {printf ("error! % D \ n ", getlasterror (); getch (); Return-1 ;}else {While (true) {sleep (1000 ); printf ("\ r current CPU usage: % 4D %", cpuuserate. getcpuuserate () ;}} return 0 ;}

In WINXP system running results are as follows (this article supporting program: http://download.csdn.net/detail/morewindows/5160810 ):

In win7 system running results are as follows (this article supporting program: http://download.csdn.net/detail/morewindows/5160810 ):

 

This article "Windows system CPU memory network performance statistics of the second article CPU overall usage" (http://blog.csdn.net/morewindows/article/details/8678359) introduces how to obtain the overall CPU usage, most of the CPU is multi-core, therefore, how can we get the usage of each core of a multi-core CPU. See the following two articles --

3. Windows system CPU Memory Network Performance Statistics Article 3 CPU multi-core CPU core usage C #

Http://blog.csdn.net/morewindows/article/details/8678382

4. Windows system CPU Memory Network Performance Statistics Article 4 CPU multi-core CPU core usage C ++

Http://blog.csdn.net/morewindows/article/details/8678396

 

 

This article supporting procedures: http://download.csdn.net/detail/morewindows/5160810

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8678359

Welcome to Weibo: http://weibo.com/MoreWindows

 

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.