CPU Analysis of Linux concepts

Source: Internet
Author: User
Tags switches

Http://ilinuxkernel.com/?cat=4

Linux CPU Utilization principle and accuracy analysis
1 CPU Usage Calculation principle
Under Linux/unix, CPU utilization is divided into user state, System State and idle state, which indicate the time that the CPU is in user state execution, the time the system kernel executes, and the time the idle system process executes.
The following is the value of the top display
1.1%us, 1.6%sy, 0.0%ni, 97.2%id, 0.0%wa, 0.0%hi, 0.1%si, 0.0%st
Us:user Time User times
Sy:system Time System
Ni:nice time
Id:idle Time Idle
Wa:waiting Time Wait
Hi:hard IRQ time Hard Interrupt processing
SI:SOFTIRQ Time Soft Interrupt processing
St:steal Time lost
Represents the time that the CPU executes the user process, including the nices time. It is generally expected that the higher the user space CPU the better.
Represents the CPU uptime in the kernel, including IRQ and SOFTIRQ time. The high CPU utilization of the system indicates that there is a bottleneck in some parts of the system. Usually the lower the value, the better.
The time it takes the system to adjust the process priority.
The system is in an idle period and waits for the process to run.
The amount of time the CPU spends waiting for the I/O operation to complete. The system should not spend a lot of time waiting for I/O operations, otherwise it would indicate a bottleneck in I/O.
The time it takes the system to process a hard interrupt.
The time it takes for the system to handle soft interrupts.
The time that is forced to wait (involuntary wait) for the virtual CPU, at which point Hypervisor is serving another virtual processor.

1.2 CPU occupancy rate calculation
Linux CPU occupancy is calculated based on the contents of the/proc/stat file, the following is the stat file content sample, the kernel version is different, will be slightly different, but the content is basically the same.
CPU information, CPU for the total information, cpu0 ... cpun for each specific CPU information
CPU 661733 468 503925 233055573 548835 14244 15849 0
A total of 8 values (units: Ticks), respectively:
User time, 661733 nice time, 468
System time, 503925 Idle time, 233055573
Waiting time,548835 hard Irq time, 14244
SoftIRQ time,15849 Steal time,0

The CPU occupancy rate is calculated as follows:
CPU Time =user+system+nice+idle+iowait+irq+softirq+stl
%us = (User time + nice times)/cpu *100%
%sy= (System time + hard Irq times +softirq hours)/cpu *100%
%id= (Idle)/cpu time *100%
%ni= (Nice time)/cpu *100%
%wa= (Waiting time)/cpu *100%
%hi= (hard Irq time)/cpu *100%
%si= (SoftIRQ time)/cpu *100%
%st= (Steal time)/cpu *100%

2 CPU Utilization Kernel implementation
below to RHEL6 kernel source version 2.6.32-220.el6 x86_64 For example, to introduce the kernel source code implementation. The creation of the/proc/stat file is implemented by the function Proc_stat_init (), which is called in the file fs/proc/stat.c when the kernel is initialized. /proc/stat file Correlation function time is in the stat.c file.
3 Linux CPU Usage accuracy analysis
When you use the similar top command to observe the system and the CPU usage of each process, you can specify a refresh interval to refresh and observe CPU usage in real time. The top command, by default, is refreshed every 3 seconds. You can also specify the refresh frequency, such as top-d 0.1 or top-d 0.01, by top-d < refresh interval >. When top is executed, you can also press the "s" key to modify the time interval.
We can set the CPU usage refresh interval to very low, such as 0.01 seconds. But is too low a refresh rate to be able to more accurately observe CPU occupancy? Is the CPU usage information provided by the Linux system accurate enough?
Based on the previous analysis, we know that Linux calculates the CPU occupancy based on the contents of the/proc/stat file, which is related to the accuracy of the data provided by the/proc/stat. So
(1) What is the unit of content in the/proc/stat file?
(2) How long will the data in/proc/stat be refreshed?
CPU 926 0 4160 5894903 2028 0 7 0 0
Cpu0 80 0 473 367723 658 0 3 0 0
Data unit precision in 3.1/proc/stat
/proc/stat CPU data information in the Ticks unit. There is a global variable jiffies in the kernel to record the number of ticks that have been experienced since the system started.

[Email protected] boot]# Cat/proc/sched_debug|grep Ji
. jiffies:9645728927
[Email protected] boot]# Cat/proc/stat
CPU 216511 1316 58661 534345575 29219 2170 3034 0 0
Ticks (tick) is the time interval for the system clock interrupt, which is related to the HZ value in the kernel, ticks = 1/hz. The size of the Hz value, which can be configured when the kernel is compiled. On one machine is the RHEL6.1 core, configured with a Hz value of 1000.
[Email protected] boot]# uname-a
Linux SSD 2.6.32-131.0.15.el6.x86_64 #1 SMP Tue may 15:42:40 EDT x86_64 x86_64 x86_64 gnu/linux
[Email protected] boot]# Cat/boot/config-2.6.32-431.el6.x86_64|grep config_hz
# config_hz_100 is not set
# config_hz_250 is not set
# config_hz_300 is not set
Config_hz_1000=y
config_hz=1000
The value of HZ is the number of clock interrupts per second. You can observe the change in the clock interrupt value in/proc/interrupts to calculate the value of Hz. When the value of Hz is 1000, the unit of ticks is 1/1000 seconds, or 1ms.
[Email protected] boot]# cat/proc/interrupts |grep LOC
loc:30099358 Local Timer interrupts
3.2
The values in the/proc/stat file are updated when the program reads, and the kernel does not actively update the data in the/proc/stat.
The CPU information in/proc/stat is calculated by kernel_stat the values of the individual member variables.
3.3 CPU Utilization Accuracy Analysis
From the previous analysis, we can draw the following conclusions:
(1) The Linux CPU occupancy rate is calculated from the data in the/proc/stat file;
(2) The data precision in/proc/stat is ticks, that is 1/hz second;
(3) Each ticks of the kernel will update CPU usage information;
(4) The accuracy of CPU utilization is 1/hz seconds.
4 is Linux CPU usage accurate?
Occasionally, a similar problem occurs: The process CPU utilization is not stable under stable computing pressure, or the CPU usage of the feature process is obviously not allowed. That is, when the number of system switches is high, the CPU utilization of Linux computer system may be inaccurate. So is Linux CPU utilization calculated exactly? If this is not possible, what happens?
4.1 Linux CPU usage is not accurate
In the previous analysis, the Linux kernel updated the CPU usage every time the clock was interrupted, that is, 1/hz seconds. When the clock is interrupted, only the process information that is currently running is visible. For example, a red arrow indicates a clock interrupt (Timer Interrupt).
On the first interrupt, you see that process a is running. However, process A runs short and process B runs. On the second interrupt, process C runs; When the third interrupt arrives, schedule process A to execute again. The third time this interrupt occurs, process C runs.
According to the Linux kernel CPU utilization statistics method, during the 1th and 2nd interrupts, the kernel did not see process b running; so it missed process B's use of CPU information. Similarly, during the 2nd and 3rd interrupts, process B was omitted from CPU usage. This causes the Linux kernel CPU usage statistics to be inaccurate.
The reason for the inaccurate CPU usage is that multiple process scheduling occurs during a clock interrupt cycle. The precision of the clock interrupt is 1/hz seconds.
4.2 Top command CPU usage accurate?
It is only in a clock interrupt cycle that multiple process schedules occur that the CPU usage is not allowed. The accuracy of CPU usage in the top command is related to the process scheduling frequency. If the value of Hz is 250, the ticks value is 4ms, and if the Hz value is 1000, the ticks value is 1ms. At Hz 250, CPU usage is accurate as long as the process schedule interval is greater than 4ms. When Hz is 1000, the scheduling interval is greater than 1ms, and the CPU occupancy rate is calculated accurately. Less process scheduling, CPU utilization is accurate, scheduling interval is less than the clock interrupt, it may be inaccurate. So what is the timing of the process scheduling? How do I observe the number of process schedules?
4.2.1 Process Scheduling Timing
1. Time of process state transitions: process termination, Process sleep
The process calls a function such as sleep () or exit () to perform state transitions, which invoke the scheduler to schedule the process;
2. When the time slice of the current process runs out (current->counter=0)
Because the time slice of the process is updated by the clock interrupt
3. Device drivers
The dispatcher is called directly when the device driver CEO repeats the task. In each iteration, the driver checks the value of the need_resched and, if necessary, invokes the scheduler schedule () to voluntarily abandon the CPU.
4. When the process returns to the user state from interrupts, exceptions, and system calls
Regardless of whether it is returned from an interrupt, an exception, or a system call, the Ret_from_sys_call () is called, and the dispatch flag is detected by this function, and the scheduler is invoked if necessary. So why call the scheduler when it returns from the system call? This is of course from efficiency considerations. Returning from a system call means leaving the kernel state and returning to the user state, while the transformation of the State takes a certain amount of time, so before returning to the user state, the system completes all the processing in the kernel state.
Observation on the number of 4.2.2 process scheduling
The Vmstat command can be used to observe the number of process switches in the system, and the value of CS domain is the number of switches. The value of Hz can be determined by the kernel configuration file, if/proc/config.gz exists, export this file to view.
You can also observe the number of switches (nr_switches) by viewing the contents of the/proc/sched_debug file.
[[email protected] proc]# watch-d-N 1 ' cat/proc/sched_debug |grep nr_switches '

Is the process scheduling in our system really that frequent? In most cases, the computer system for CPU utilization in Linux is accurate.

CPU Analysis of Linux concepts

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.