Reprinted from: http://blog.csdn.net/pppjob/article/details/4060336
Under Linux, CPU utilization is divided into user state, System State and idle state, respectively, the CPU is in user state execution time, system kernel execution time, and idle system process execution time, the sum of which is the total CPU time, when there is no user process, system process, etc. need to execute, The CPU executes the system default idle process. From the normal way of thinking, CPU utilization is the proportion of idle process time, that is, the time the CPU executes the non-idle process / CPU Total execution time.
In a Linux system, the allocation of CPU time is stored in the/proc/stat file, and the utilization calculation should fetch the data 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 time slice (in jiffies), the data is from the CPU power to the current cumulative value. The common monitoring software is to use the data inside the/proc/stat to calculate the CPU utilization.
Different versions of the Linux/proc/stat file content, as Linux 2.6来 said, the contents of the/proc/stat file are 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 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 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 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 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 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 on the first line represents the total CPU usage, so we just use the first line of numbers to calculate it. The following table parses the meaning of the first row values (injiffies):
User (2032004) |
From the beginning of the system start up to the current moment, the user-state CPU time, does not contain the nice value for a negative process. |
Nice (102648) |
The CPU time occupied by a process that has a negative nice value from the beginning of the system startup to the current moment |
System (238344) |
Accumulated from the start of the system to the current time, the core time |
Idle (167130733) |
Accumulate from the start of the system to the current moment, waiting time other than IO wait time |
Iowait (758440) |
Accumulate from the start of the system to the current time, IO wait time |
IRQ (15159) |
Accumulated from the start of the system to the current moment, hard interrupt Time |
SOFTIRQ (17878) |
Accumulated from the start of the system to the current moment, soft interrupt time |
Since the values in/proc/stat are accumulated from the start of the system to the current time, it is necessary to compare the values of T1 and T2 at different points in time, and when the interval of two points is shorter, the result can be considered as the instantaneous utilization of the CPU.
CPU calculation formula for instant utilization:
Total usage time of CPU in T1 to t2 period = (user2+ nice2+ system2+ idle2+ iowait2+ irq2+ softirq2)-(user1+ nice1+ system1+ idle1+ iowait1+ 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 the struct Cpu_usage_stat, implemented in the Timer_tick->update_process_times.
Then its accuracy is Hz, if the Hz is 100, it means that every s record 100 times. This accuracy is of course not high, and error prone, here is an example in Documentation/cpu-load.txt:
Time line between a timer interrupts
|--------------------------------------|
^ ^
|_ user AppA begins working |
|_ user AppA goes to sleep
As a result, the action of 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 precision?
Counter ,
In the Cpu_idle record idle time, in the ASM_DO_IRQ record processing IRQ time, in the Context_switch record entered that process, as well as the time, in the __DO_SOFTIRQ record processing SOFTIRQ time, Record these things in a global array.
[Linux Memory] calculates CPU occupancy with/proc/stat