当前位置:网站首页>How to use different tools to analyze and optimize code performance when CPU utilization is high
How to use different tools to analyze and optimize code performance when CPU utilization is high
2022-07-26 17:11:00 【No regrets!】
List of articles
C P U send use rate = 1 − ( empty Leisure when between n e w − empty Leisure when between o l d ) / ( total C P U when between n e w − total C P U when between o l d ) CPU Usage rate = 1-( free time _{new} - free time _{old})/( total CPU Time _{new}- total CPU Time _{old}) CPU send use rate =1−( empty Leisure when between new− empty Leisure when between old)/( total CPU when between new− total CPU when between old)
1. procc see CPU Usage rate
1.1 proc/stat Parameter details in
Linux adopt /proc Virtual file system , Provide system internal status information to user space , and /proc/stat What is provided is the system CPU And task Statistics , The specific analysis is as follows :
$ man proc
/proc/stat
kernel/system statistics. Varies with architecture. Common
entries include:
cpu 10132153 290696 3084719 46828483 16683 0 25195 0 175628 0
cpu0 1393280 32966 572056 13343292 6130 0 17875 0 23933 0
The amount of time, measured in units of USER_HZ
(1/100ths of a second on most architectures, use
sysconf(_SC_CLK_TCK) to obtain the right value), that the
system ("cpu" line) or the specific CPU ("cpuN" line)
spent in various states:
user (1) Time spent in user mode.
nice (2) Time spent in user mode with low priority
(nice).
system (3) Time spent in system mode.
idle (4) Time spent in the idle task. This value
/proc/stat # Check by shortcut after entering
According to the above proc/stat The relationship between each column is
user( abbreviationus) User mode CPU Time , It doesn't contain nice Time , Contains only guestnice( abbreviationni) Low priority user mode CPU Time , Process nice Adjusted to 1-19, The higher the priority, the lowersystem( abbreviationsys) Kernel mode CPU Timeidle( abbreviationid) free time , Not including waiting IO Time iowaitiowait( abbreviationwa) wait for IO Of CPU Timeirq( abbreviationhi) Hard interrupt CPU Timesoftirq( abbreviationsi) Soft interrupt CPU Timesteal( abbreviationst) When the system is running on a virtual machine , Occupied by other virtual machines CPU TimeguestVirtualization running other operating system time , Running virtual machines CPU Timeguest_niceIndicates the time of running the virtual machine with low priority
1.2 utilize proc/stat see
If we only focus on CPU, You can see this with the following command
$ cat /proc/stat | grep ^cpu
cpu 2400 0 4299 20978 0 624 0 0 0 0
cpu0 234 0 675 2551 0 293 0 0 0 0
cpu1 150 0 100 3210 0 18 0 0 0 0
# Column information : User mode , Weak user state , Kernel mode , Space time , IO wait for Hard interrupt ...
2. top see CPU Usage rate
top Display the overall system CPU And memory usage , And the resource usage of each process
$ top # The default for each 3s Refresh once
top - 22:50:25 up 15 min, 0 users, load average: 0.52, 0.58, 0.59
Tasks: 4 total, 1 running, 3 sleeping, 0 stopped, 0 zombie
# The following line is CPU Usage rate , The meaning of each column is 1.1 Has been given in , Just put CPU Change the time to CPU Usage rate , Such as 8.4 Indicates that the user status is occupied 8.4% Of CPU Usage rate
%Cpu(s): 8.4 us, 12.4 sy, 0.0 ni, 77.9 id, 0.0 wa, 1.4 hi, 0.0 si, 0.0 st
KiB Mem : 8271548 total, 3149636 free, 4892560 used, 229352 buff/cache
KiB Swap: 25165824 total, 24378928 free, 786896 used. 3245256 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 8948 396 324 S 0.0 0.0 0:00.14 init
10 root 20 0 9296 228 180 S 0.0 0.0 0:00.01 init
11 zjq 20 0 17032 3644 3544 S 0.0 0.0 0:00.38 bash
86 zjq 20 0 17644 2044 1492 R 0.0 0.0 0:00.03 top
# Be careful top The default display is CPU Average , Press down 1, Switch to each CPU Usage rate
top - 22:56:33 up 22 min, 0 users, load average: 0.52, 0.58, 0.59
Tasks: 4 total, 1 running, 3 sleeping, 0 stopped, 0 zombie
%Cpu0 : 1.9 us, 12.3 sy, 0.0 ni, 80.4 id, 0.0 wa, 5.4 hi, 0.0 si, 0.0 st
%Cpu1 : 2.0 us, 1.7 sy, 0.0 ni, 96.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8271548 total, 3127012 free, 4915184 used, 229352 buff/cache
KiB Swap: 25165824 total, 24396948 free, 768876 used. 3222632 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 8948 396 324 S 0.0 0.0 0:00.14 init
10 root 20 0 9296 228 180 S 0.0 0.0 0:00.01 init
11 zjq 20 0 17032 3644 3548 S 0.0 0.0 0:00.39 bash
87 zjq 20 0 17644 2044 1492 R 0.0 0.0 0:00.04 top
Above we find , There is a process %CPU Column , Representing a process CPU Usage rate , It is user mode and kernel mode CPU The sum of usage , Contains the process user space used CPU, Execute kernel space through system calls CPU, And the ready queue waiting to run CPU; In a virtualized environment , It also includes the CPU.
because top There is no segmentation of process user mode and kernel mode CPU, How to view the details of each process ?
3. pidstat Command view CPU Usage rate
install : sudo apt install sysstat
$ pidstat 1 5 # Output a set of data per second , Output 5 Time
23:03:04 UID PID %usr %system %guest %wait %CPU CPU Command
23:03:05 1000 492 0.00 1.00 0.00 0.00 1.00 0 pidstat
The information in each column is :
%usrUser mode CPU Usage rate%systemKernel mode CPU Usage rate%guestRunning virtual machines CPU Usage rate%waitwait for CPU Usage rate%CPUThe total CPU Usage rate
We can see it up here , The highest %CPU Occupied is pidstat process , That is to say PID=492 Number
4. pref location CPU High usage code location
Come here , Through the above three methods : proc, top, pidstat Can easily determine CPU High usage processes , Next , According to the process with high utilization , How to locate the occupancy code ?
pref yes Linux2.6.31 Later built-in performance analysis tools , Based on performance event sampling , It can not only analyze various events of the system and kernel performance , You can also analyze the performance problems of the specified application
install :
sudo apt install linux-tools-common linux-tools-5.4.0-120-generic linux-cloud-tools-5.4.0-120-generic
4.1 perf Use a : perf top

As shown in the above figure , The first row is three data , Number of samples (samples), Event type (event), The total number of events (event count)
It should be noted that : When sampling is too little , For example, dozens of , It's meaningless
The output of the above figure is an example , We see , Occupy CPU Most of all perf The tool itself , There is only a 7.28%, That means the system doesn't have CPU Performance issues .
4.2 perf Use two : perf record and perf report
self-examination
5. application
nginx+PHP Of web Service as an example
5.1 ab Apache bench A common HTTP Service performance testing tool
ab -c 10 -n 10000 http://www.baidu.com # Concurrent 10 Request tests nginx performance , A total of tests 1w Requests

5.2 Find the occupation in the service CPU Highly utilized code functions
| Service machine | The client |
|---|---|
| Provide Web service Such as www.baidu.com | Browser access www.baidu.com |
| Only per second 11.63 Responses to requests | ab -c 10 -n 10000 http://www.baidu.com |
| Start testing | |
top Press the number 1, Find out php-fpm Command to occupy %CPU The highest , And the corresponding process ID yes 22222 | |
| To analyze | |
perf top -g -p 22222 # -g Turn on call relationship analysis , -p Specify the process number | |
Replace the direction key with php-fpm, Press enter to expand the calling relationship , Find occupancy CPU Most of all sqrt and add_function Two functions | |
View the sqrt grep sqrt -r Source folder Found a place , Found a mistake |
6. summary
- user CPU and Nice CPU high , It indicates that user mode processes occupy a lot CPU, So focus on troubleshooting process performance problems
- System CPU high , It shows that the kernel state takes up more CPU, Focus on the performance problems of kernel threads or system calls
- IO wait for CPU high , Explain waiting IO Long time , Focus on System Storage IO problem
- Soft interrupt and hard interrupt high , It indicates that the interrupt handler takes more CPU, We should focus on checking the interrupt service program in the kernel
- Final : encounter CPU Increased usage , With the help of top pidstat, Confirm to trigger CPU Performance asked his source , Use perf Tools , Check the specific functions that cause performance problems , To optimize
边栏推荐
- Movable view component (it can be dragged up, down, left and right)
- Definition and relationship of derivative, differential, partial derivative, total derivative, directional derivative and gradient
- UPC 2022 summer personal training game 07 (part)
- Packet capturing and streaming software and network diagnosis
- Three misunderstandings of CRM implementation: lack of strategy, lack of identity, and technology first
- Marxan模型保护区优化与保护空缺甄选技术、InVEST生态系统中的应用
- How to ensure cache and database consistency
- Who is safe to open the VIP account of CICC securities?
- My SQL is OK. Why is it still so slow? MySQL locking rules
- The first self-developed embedded 40nm industrial scale memory chip in China was released, breaking the status quo that the localization rate is zero
猜你喜欢

Current limiting comparison: how to choose sentinel vs hystrix?

37.【重载运算符的类别】

Win11怎么重新安装系统?

Merge multiple row headers based on apache.poi operation

Oracle创建表分区后,查询的时候不给出partition,但是会给分区字段指定的值,会不会自动按照分区查询?

In May, 2022, video user insight: user use time increased, and the platform achieved initial results in cost reduction and efficiency increase

PyQt5快速开发与实战 3.4 信号与槽关联

Win11怎么自动清理回收站?

Alibaba side: analysis of ten classic interview questions

Relationship between standardization, normalization and regularization
随机推荐
Realizing DDD based on ABP -- related concepts of DDD
srec_ Use of common cat parameters
ES:Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
[ctfshow web] deserialization
【开发教程7】疯壳·开源蓝牙心率防水运动手环-电容触摸
Matlab论文插图绘制模板第40期—带偏移扇区的饼图
Anaconda download and Spyder error reporting solution
2022-2023 topic recommendation of information management graduation project
Guangzhou Municipal Safety Committee Office issued warnings and reminders on safety precautions in hot weather
Win11如何关闭共享文件夹
Sharing of 40 completed projects of high-quality information management specialty [source code + Thesis] (VI)
TCP 和 UDP 可以使用相同端口吗?
Heavy! Zeng Xuezhong was promoted to vice chairman and CEO of zhanrui, and Chu Qingren was appointed as co CEO!
Singleton mode
Execution process of select statement in MySQL
Current limiting comparison: how to choose sentinel vs hystrix?
Speaker recruitment | AI time recruit icml/ijcai 2022 as a Chinese speaker!!!
【开发教程9】疯壳·ARM功能手机-I2C教程
On the evolution of cloud native edge computing framework
In May, 2022, video user insight: user use time increased, and the platform achieved initial results in cost reduction and efficiency increase