In Linux systems, the CPU is primarily used for interrupt, kernel, and user process task handling, with priority being interrupt > Kernel > User process. In the CPU consumption analysis, we also often encounter the following several concepts.
Each CPU can execute only one thread at a time, and Linux threads are preemptive-scheduled. That is, each thread allocates a certain amount of execution time, when the execution time is reached, there is an IO block in the thread, or when a high-priority thread is executed, Linux switches the executing thread, stores the execution state of the current thread at the time of the switchover, and restores the state of the thread to be executed, a process known as context switching. In Java applications, for file IO operations, network IO operations, lock waits, or thread sleep, the current thread goes into a blocking or hibernation state, triggering a context switch. Too much context switching causes the kernel to occupy more CPU usage, which slows down the response of the application.
Each CPU maintains a running thread queue, assuming that 16 threads are started in a 8-core Cpu,java application, and that if all 16 threads are in a running state, there will be 2 threads in each CPU's running queue if the average is allocated. Usually, the load of the system is mainly determined by the CPU's running queue, assuming the above condition is maintained for 1 minutes, then the system load is 2 in 1 minutes. In general, the larger the run queue value, the longer the thread will take to complete.
CPU utilization is a percentage of CPU usage in user processes, cores, interrupt processing, IO waits, and idle 5 parts, and these 5 values are key metrics used to analyze CPU consumption.
In Linux we can see the CPU consumption of the process through the top or PIDSTAT commands.
- Top
In the output of the top command, US represents the percentage of user process processing, SY represents the percentage of kernel threading, and NI represents the percentage of tasks that have been prioritized by the Nice command, the ID represents the percentage of CPU idle time, and WA indicates that it waits for the percent of IO to be accounted for during execution. Ratio, HI indicates the percentage of hardware interrupts, and SI represents the percentage of software outages.
2. Pidstat
Pidstat is a tool in SYSSTAT (installed under Ubuntu: sudo apt-get install SYSSTAT)
When CPU consumption is severe, it is mainly reflected in the value of us, sy, WA or Hi, and the value of WA is the IO waiting cause. For Java applications, CPU consumption is mainly manifested in us, SY
Two values are high.
Analysis of CPU consumption in Linux environment