CPU usage Algorithm

Source: Internet
Author: User

The Windows Task Manager can view the CPU usage.
The CPU runs continuously. As long as the CPU is powered on, the command is executed, and the NOP operation is executed even if nothing can be done. Therefore, the low CPU usage of Windows does not mean that there is nothing to do with the CPU, but the amount of time that the CPU can spare from the current status to do the user's work.
In a multi-task operating system, processes generally achieve priority.AlgorithmThe preemptible real-time operating system's high-priority process can immediately seize the CPU of the low-priority process, while the time-sharing operating system generally waits until the CPU time slice of the current process is used up before scheduling, however, whether it is a real-time system or a time-sharing system, high-priority processes are more dominant in CPU allocation time than low-priority processes.
Priority_highest can be defined as the highest priority defined by the operating system. priority_lowest is the lowest priority defined by the operating system. Generally, the value of priority_highest is 0, and the value of priority_lowest is greater than 0, although this will confuse the meanings of highest and lowest more or less. In Windows, priority_highest is 0, which is a system process; priority_lowest does not know how much it is, but it must be a idle process, that is, the system idle process seen in the task manager. There is a statistical process in the (PRIORITY_LOWEST-1) level lower than priority_lowest, called osstat, that is used to count the CPU usage.
During OS initialization, only the system process is running for a period of time, because the initialization is not completed yet and the user process has not started scheduling. At this time, the OS can time the CPU and calculate the number of addition operations that can be performed by the CPU in one second. For example, you can writeCode

1 Osidlectr =   0l ; /* Clear idle counter */
2 Ostimedly (OS _ticks_per_sec ); /* Determine Max. Idle counter value for 1 second */
3 Osidlectrmax = Osidlectr; /* Store maximum idle counter count in 1 second */
4

Among them, ostimedly suspends OS _ticks_per_sec for all processes (except idle processes) of the system. The OS _ticks_per_sec value can be calculated by the frequency of the crystal oscillator to obtain the command cycle, which is then divided by the CPU, you can also obtain the instruction cycle from the instructions provided by the CPU, which is then divided by the CPU.
Add such code to the idle Process

Osidlectr ++ ;

here, the significance of ostimedly (OS _ticks_per_sec) is clear. Because other processes are suspended, only idle is running in the system, the accumulation of idle on osidlectr will not be interrupted by other processes with higher priority, resulting in inaccurate computing.
the maximum number of addition times that an idle process can execute in one second is obtained. Why is the accumulation of the idle process to the osidlectr the maximum number of addition times of the CPU in one second, because other processes are suspended for 1 second, the CPU only executes the idle code within this second. Therefore, the accumulation of the idle on osidlectr can represent the maximum execution capability of a system, there cannot be other ways to increase the value of osidlectr to more than 1 second.
In addition, other idle Code does not affect the accuracy of osidlectr, because you can change the definition 'maximum number of times the system executes the add' to 'maximum number of times the system executes the idle process ', the meaning remains unchanged and still represents the CPU execution capability.
In fact, idle is also a process, so it is also involved in the process scheduling algorithm. Since the osidlectr accumulation algorithm has been added to idle, then, the system only needs to initialize the osidlectr to 0 when every second is interrupted. Then, when the system runs normally (the user process has started) the accumulation of osidlectr by idle within one second can be seen as the processing capability of the CPU in the normal user State within one second, because the user process has started to participate in scheduling at this time, the idle process is the process with the lowest priority in the system. It is transferred only when there is no user task. As long as the user has other tasks, the idle will be hung up and waiting, therefore, the accumulation of osidlectr by idle indicates the CPU idleness under the current task pressure. Because the osidlectr is reset once every second, the CPU usage can also be written into the formula
cpuusage % = 100 * (1-osidlectr/osidlectrmax) Written in osstat, the CPU usage of Windows is formed.
In fact, the algorithm discussed above is the CPU usage algorithm of the ucos2 system. ucos2 is an open source real-time operating system and is usually used in embedded CPU.


Reprinted

Author: lingch
From: http://blog.csdn.net/sboom/archive/2005/12/25/561412.aspx

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.