Take the CPU usage of the system in Windows 2000

Source: Internet
Author: User

Take the CPU usage of the system in Windows 2000

I. application background

In many practical applications, you need to know the current CPU usage of the system to determine whether to perform certain operations. For example, in a large system, data backup is often performed, but the business is not affected at the same time, so it is generally performed when the current CPU share of the system is low.

II. Implementation Principle

In Windows NT/2000, how does one obtain the CPU usage? Open APIs provided by Microsoft to developers (Win32
API). Therefore, native APIs must be used to obtain system information. Native
In Windows user mode, the API is the upper-layer Win32
API provides local system services for interfaces. A local system service function ntquerysysteminformation is used here, which provides us with a wealth of system information
Information, including some information control and settings. The following is the prototype of this function:

Typedef ntstatus (_ stdcall * ntquerysysteminformation)

(In system_information_class systeminformationclass,

In out pvoid systeminformation,

In ulong systeminformationlength,

Out Pulong returnlength optional );

Ntquerysysteminformation;


We can see that systeminformationclass is a type information, which provides more than 50 types of information, that is, we can use this function for more than 50 systems.
System Information for testing or setting.
Systeminformation is an lpvoid pointer that provides us with the information we need to obtain or the system information we need to set.
Systeminformationlength is the length of systeminformation, which is determined by the information type of the test. As
Returnlength indicates the length of the data returned by the system. It can be set to null ).



The process of using this function to obtain the CPU share of the system is as follows:

1. Obtain the number of CPUs in the system;

2. Extract the current system time (number of ticks), subtract the original system time, and obtain the time difference from the last value to the current system time;

3. Extract the current idle time of the system, and subtract the original idle time to get the idle time in this period.

4. Divide the idle time in this time period by the system time to obtain the CPU idle rate during this time period.

5. the value obtained from 100-(idle rate of the system)/(number of CPUs) is the CPU usage.

 

 

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.