Obtain CPU usage in Windows NT/2000

Source: Internet
Author: User
The method for obtaining the CPU usage in Windows NT/2000 is different from that in Windows 9X. The "performance counter" is not used for obtaining the CPU usage in Windows NT/2000 ". Instead, it uses ntquerysysteminformation, an API function not publicly available in Ntdll. dll. For more information about its usage, see Article : How to obtain the system startup time in Windows NT/2000.

To calculate the CPU usage in Windows NT/2000, use the following formula:

Cpuusageinpercent = 100-(cputimen-CpuTimen-1-cputime0)/(systemtimen-SystemTimen-1-systemtime0)/numberofprocessors * 100

Here
CPU time is the idle time of the CPU (in milliseconds );
Systemtime is the system time (MS );
Numberofprocessors indicates the number of processors in the system;
0, 1, n indicates the number of samples (0 indicates the oldest sampling, 1 indicates the newest sampling, and N indicates the newest sampling );

The following describes the required parameter values for calling the ntquerysysteminformation function. First, obtain the number of processors. In the system_basic_information structure, there is a bkenumberprocessors Member, which is what we need.

# Define systembasicinformation 0

Typedef struct
{
DWORD dwunknown1;
Ulong ukemaximumincrement;
Ulong upagesize;
Ulong ummnumberofphysicalpages;
Ulong ummlowestphysicalpage;
Ulong ummhighestphysicalpage;
Ulong uallocationgranularity;
Pvoid plowestuseraddress;
Pvoid pmmhighestuseraddress;
Ulong ukeactiveprocessors;
Byte bkenumberprocessors;
Byte bunknown2;
Word wunknown3;
} System_basic_information;

Long status;
System_basic_information SBI;

Status = ntquerysysteminformation (systembasicinformation, & SBI, sizeof (SBI), 0 );

To implement multiple sampling, We need to query the cputime and systemtime values in a loop and store their old values in temporary variables. For how to get system time, see <a href = "http://www.vckbase.com/bbs/prime/viewprime_mng.asp? Id = 348 "> how to obtain the system startup time in Windows NT/2000 System </a>. The idle time of the CPU is stored in the liidletime Member of the system_performance_information structure.

# Define systemperformanceinformation 2

Typedef struct
{
Large_integer liidletime;
DWORD dwspare [76];
} System_performance_information;

Large_integer lioldidletime = {0, 0 };
Large_integer lioldsystemtime = {0, 0 };
System_performance_information SPI;

While (1 ){
Status = ntquerysysteminformation (systemtimeinformation, & STI, sizeof (STI), 0 );
Status = ntquerysysteminformation (systemperformanceinformation, & SPI, sizeof (SPI), 0 );

// Calculate the CPU usage here

Lioldidletime = SPI. liidletime;
Lioldsystemtime = STI. likesystemtime;

Sleep (1000 );
}

Related Article

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.