一、問題描述
生產環境cpu過高問題,排查雖然不易,但是是有一定的套路,可以加快問題定位從而排查的。
二、步驟
1.查看cpu佔用高的進程
top
2. ps -ef或者jps進一步定位,得知是一個怎麼樣的一個後臺程序
3.查看對應進程下,每個線程的運行情況
ps -mp pid -o THREAD,tid,time
例如
ps -mp 29556 -o THREAD,tid,time
ps -mp 11823 -o THREAD,tid,time
4.將佔用資源奪得線程threadpid:進制轉換,2HEX
printf "%x\n" threadpid
5.查看對應線程存在的問題
jstack 進程ID | grep tid(16進制線程ID小寫英文) -A60
三、編寫cpu高消耗代碼並且部署服務器
1.java代碼
package com.ray.cpu;
public class cpuOccupy {
public void cpuCircle() {
int num = 0;
long start = System.currentTimeMillis() / 1000;
while (true) {
num = num + 1;
if (num == Integer.MAX_VALUE) {
System.out.println("reset");
num = 0;
}
if ((System.currentTimeMillis() / 1000) - start > 1000) {
return;
}
}
}
public static void main(String[] args) {
new cpuOccupy().cpuCircle();
}
}
2.配置maven相關配置:打jar包並且指定運行主類
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ray.cpu</groupId>
<artifactId>occupied</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
</dependencies>
<build>
<!--<pluginManagement><!– lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) –>-->
<plugins>
<plugin>
<!--<groupId>org.apache.maven.plugins</groupId>-->
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin </artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.ray.cpu.cpuOccupy</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!--</pluginManagement>-->
</build>
</project>
關鍵點:打包jar包插件,以及指定主類
3.打包
4.上傳linux服務器並且運行
# java -jar occupied-1.0-SNAPSHOT-jar-with-dependencies.jar
四、排查問題
1. top查看佔用cpu資源較多的程序PID:13645
2. 查看對應進程下,每個線程的運行情況
[root@cloudera ~]# ps -mp 13645 -o THREAD,tid,time
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
root 98.7 - - - - - - 00:00:36
root 0.0 19 - futex_ - - 13645 00:00:00
root 98.6 19 - - - - 13648 00:00:36
root 0.0 19 - futex_ - - 13650 00:00:00
root 0.0 19 - futex_ - - 13651 00:00:00
root 0.0 19 - futex_ - - 13653 00:00:00
root 0.0 19 - futex_ - - 13655 00:00:00
root 0.0 19 - futex_ - - 13658 00:00:00
root 0.0 19 - futex_ - - 13664 00:00:00
root 0.0 19 - futex_ - - 13665 00:00:00
root 0.0 19 - futex_ - - 13666 00:00:00
root 0.0 19 - futex_ - - 13669 00:00:00
root 0.0 19 - futex_ - - 13670 00:00:00
root 0.0 19 - futex_ - - 13673 00:00:00
root 0.0 19 - futex_ - - 13674 00:00:00
root 0.0 19 - futex_ - - 13678 00:00:00
root 0.0 19 - futex_ - - 13679 00:00:00
root 0.0 19 - futex_ - - 13680 00:00:00
root 0.0 19 - futex_ - - 13685 00:00:00
root 0.0 19 - futex_ - - 13687 00:00:00
root 0.0 19 - futex_ - - 13688 00:00:00
3. 將佔用資源奪得線程threadpid:進制轉換16進制,2HEX
[root@cloudera ~]# printf "%x\n" 13648
3550
4.定位線程問題
jstack 13645 | grep 3550 -A60
5.定位java程序位置(非常詳細)
五、參考
1. Java CPU 佔用高模擬及排查
https://blog.csdn.net/jiankunking/article/details/79749836
2.排查Java的CPU性能問題
https://blog.csdn.net/jiankunking/article/details/80297136