CPU Real-time utilization calculation method

Source: Internet
Author: User
Tags cpu usage

Http://blog.csdn.net/nineday/archive/2007/12/11/1928847.aspx

Under Linux, CPU utilization is divided into user state, System State and idle state, respectively, the CPU in user state execution time, system kernel execution time, and idle system process execution time, the sum of the three is CPU total time, when no user process, system process, etc. need to execute, The CPU performs the system default idle process. From the normal way of thinking, CPU utilization is the proportion of time that is not idle process, that is, the CPU time /CPU Total execution time of the non idle process.

In a Linux system, the allocation of CPU time is stored in the/proc/stat file, and utilization calculations should be obtained from this file. The first few lines of the file record the user state of each CPU, the system state, the idle state of the allocated time slice (unit is jiffies), which is from the CPU power to the current cumulative value. The commonly used monitoring software is the use of/proc/stat inside the data to calculate the CPU utilization.

Different versions of the Linux/proc/stat file are not the same content, with Linux 2.6来 said the/proc/stat file is as follows:

CPU 2032004 102648 238344 167130733 758440 15159 17878 0

Cpu0 1022597 63462 141826 83528451 366530 9362 15386 0

CPU1 1009407 39185 96518 83602282 391909 5796 2492 0

Intr 303194010 212852371 3 0 0 11 0 0 2 1 1 0 0 3 0 11097365 0 72615114 6628960 0 179 0 0 0 0 0 0 0 0 0 0 0 0 0-0 0 0 The 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0-----------------------+--- The 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0-----------------------+--- The 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0-----------------------+--- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0-0

Ctxt 236095529

Btime 1195210746

Processes 401389

Procs_running 1

procs_blocked 0

The number in the first row represents the total CPU usage, so we just use the first line of numbers to calculate. The following table resolves the meanings of the values in the first row:

Parameters

Resolution (unit: Jiffies)

User (2032004)

From the start of the system startup to the current moment, the user state CPU time, does not contain the Nice value negative process.

Nice (102648)

The amount of CPU time consumed by a negative process from the start of system startup to the current time

System (238344)

From the start of the system start to the current moment, the core time

Idle (167130733)

Cumulative from System startup to current moment, other wait times other than IO wait time

Iowait (758440)

Cumulative from System startup to current moment, IO wait time

IRQ (15159)

From the start of system startup to the current moment, hard interrupt Time

SOFTIRQ (17878)

From the start of system startup to the current moment, soft interrupt time

Because the values in the/proc/stat are accumulated from the start of the system to the current time of accumulation, so you need to T1 and T2 at different points in the comparison operation, when the interval of two points in a short time, you can see this calculation as the CPU real-time utilization.

Calculation formula for instantaneous CPU utilization:

CPU total usage time in T1 to t2 time = (user2+ nice2+ system2+ idle2+ iowait2+ irq2+ softirq2)-(user1+ nice1+ system1+ idle1+ I rq1+ softirq1)

CPU idle time in T1 to t2 period = (idle2-idle1)

CPU in T1 to T2 time period instant utilization = 1-CPU idle use time/CPU total usage time

Who are these values and when are they recorded?

Each timer interrupt will be recorded once, recorded in struct Cpu_usage_stat, implemented in Timer_tick->update_process_times.
Then its accuracy is Hz, if Hz is 100, it means that 100 times per s record. This precision is of course not high and error prone, and here is an example in Documentation/cpu-load.txt:
Time line between two timer interrupts
|--------------------------------------|
^                                    ^
|_ user Appa begins working |
|_ user Appa goes to sleep
As a result, the action of this A is not recorded, and this s is likely to be recorded on other heads. If the program you're doing is just the other one, you'll complain that it's a piece of crap.
So is there a record of high accuracy?
There are, but to write their own, even if you use oprofile and so on, his principle is also used timer_interrupt record, you can use other high-precision timer, but, frequent interruptions will kill the system. So write yourself, assuming there is a high-precision hardware Counter, like x86 under the timestamp Counter,
Record the idle time in the Cpu_idle, record the time to process the IRQ in the ASM_DO_IRQ, enter the process in the Context_switch record, and the time to record the processing __do_softirq in the SOFTIRQ. Record these things in a global array.
You can also refer to the implementation of top in BusyBox (BUSYBOX/PROCPS/TOP.C)

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.