jstack是java虚拟机自带的一种线程堆栈跟踪工具。
/opt/java8/bin/jstack
Usage:
jstack [-l] <pid>
(to connect to running process) #连接活动线程
jstack -F [-m] [-l] <pid>
(to connect to a hung process) #连接阻塞线程
jstack [-m] [-l] <executable> <core>
(to connect to a core file) #连接dump的文件
jstack [-m] [-l] [server_id@]<remote server IP or hostname>
(to connect to a remote debug server) #连接远程服务器
Options:
-F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
-m to print both java and native frames (mixed mode)
-l long listing. Prints additional information about locks
-h or -help to print this help message
$ jstack 5611
2021-06-06 22:05:43
Full thread dump Java HotSpot(TM) Client VM (25.271-b09 mixed mode):
"RemoteInvocationHandler [#2]" #63304 daemon prio=5 os_prio=0 tid=0xe3f4bc00 nid=0x240 in Object.wait() [0xe3b3e000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0xea7480c8> (a java.lang.ref.ReferenceQueue$Lock)
at hudson.remoting.RemoteInvocationHandler$Unexporter.run(RemoteInvocationHandler.java:603)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.AtmostOneThreadExecutor$Worker.run(AtmostOneThreadExecutor.java:118)
at java.lang.Thread.run(Thread.java:748)
"Attach Listener" #63199 daemon prio=9 os_prio=0 tid=0xe2a52800 nid=0x7938 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Ping thread for channel hudson.remoting.Channel@4afd8d:channel" #24 daemon prio=5 os_prio=0 tid=0xe4637000 nid=0x1617 waiting on condition [0xe34b9000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at hudson.remoting.PingThread.run(PingThread.java:95)
"Channel reader thread: channel" #9 prio=5 os_prio=0 tid=0xe3f50800 nid=0x15fa runnable [0xe3aed000]
java.lang.Thread.State: RUNNABLE
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:255)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
- locked <0xea4b13b0> (a java.io.BufferedInputStream)
at hudson.remoting.FlightRecorderInputStream.read(FlightRecorderInputStream.java:92)
at hudson.remoting.ChunkedInputStream.readHeader(ChunkedInputStream.java:72)
at hudson.remoting.ChunkedInputStream.readUntilBreak(ChunkedInputStream.java:103)
at hudson.remoting.ChunkedCommandTransport.readBlock(ChunkedCommandTransport.java:39)
at hudson.remoting.AbstractSynchronousByteArrayCommandTransport.read(AbstractSynchronousByteArrayCommandTransport.java:34)
at hudson.remoting.SynchronousCommandTransport$ReaderThread.run(SynchronousCommandTransport.java:63)
"Service Thread" #6 daemon prio=9 os_prio=0 tid=0xf7085400 nid=0x15f2 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0xf7082000 nid=0x15f1 waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0xf7080800 nid=0x15f0 runnable [0x00000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0xf7066400 nid=0x15ef in Object.wait() [0xe45ad000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0xea486e40> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0xf7063800 nid=0x15ee in Object.wait() [0xe45fe000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0xea486fe0> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0xf7007400 nid=0x15ec in Object.wait() [0xf717d000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at hudson.remoting.Channel.join(Channel.java:1187)
- locked <0xea747e30> (a hudson.remoting.Channel)
at hudson.remoting.Launcher.main(Launcher.java:796)
at hudson.remoting.Launcher.runWithStdinStdout(Launcher.java:718)
at hudson.remoting.Launcher.run(Launcher.java:398)
at hudson.remoting.Launcher.main(Launcher.java:298)
"VM Thread" os_prio=0 tid=0xf705e000 nid=0x15ed runnable
"VM Periodic Task Thread" os_prio=0 tid=0xf7087800 nid=0x15f3 waiting on condition
JNI global references: 268
线程状态图
jstack统计线程数:
jstack 5611 | grep 'java.lang.Thread.State' | wc -l
举例说明CPU高获取其线程ID然后分析
以我们最近出现的一个实际故障为例,介绍怎么定位和解决这类问题。
根据top命令,发现PID为28555的Java进程占用CPU高达200%,出现故障。
通过ps aux | grep PID命令,可以进一步确定是tomcat进程出现了问题。但是,怎么定位到具体线程或者代码呢?
首先显示线程列表:
ps -mp pid -o THREAD,tid,time
找到了耗时最高的线程28802,占用CPU时间快两个小时了!
其次将需要的线程ID转换为16进制格式:
printf "%x
" tid
最后打印线程的堆栈信息:
jstack pid |grep tid -A 30
找到出现问题的代码了!
现在来分析下具体的代码:ShortSocketIO.readBytes(ShortSocketIO.java:106)
ShortSocketIO是应用封装的一个用短连接Socket通信的工具类。readBytes函数的代码如下:
public byte[] readBytes(int length) throws IOException {
if ((this.socket == null) || (!this.socket.isConnected())) {
throw new IOException("++++ attempting to read from closed socket");
}
byte[] result = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (this.recIndex >= length) {
bos.write(this.recBuf, 0, length);
byte[] newBuf = new byte[this.recBufSize];
if (this.recIndex > length) {
System.arraycopy(this.recBuf, length, newBuf, 0, this.recIndex - length);
}
this.recBuf = newBuf;
this.recIndex -= length;
} else {
int totalread = length;
if (this.recIndex > 0) {
totalread -= this.recIndex;
bos.write(this.recBuf, 0, this.recIndex);
this.recBuf = new byte[this.recBufSize];
this.recIndex = 0;
}
int readCount = 0;
** while (totalread > 0) {
if ((readCount = this.in.read(this.recBuf)) > 0) **{
if (totalread > readCount) {
bos.write(this.recBuf, 0, readCount);
this.recBuf = new byte[this.recBufSize];
this.recIndex = 0;
} else {
bos.write(this.recBuf, 0, totalread);
byte[] newBuf = new byte[this.recBufSize];
System.arraycopy(this.recBuf, totalread, newBuf, 0, readCount - totalread);
this.recBuf = newBuf;
this.recIndex = (readCount - totalread);
}
totalread -= readCount;
}
}
}
问题就出在加粗的代码部分。如果this.in.read()返回的数据小于等于0时,循环就一直进行下去了。而这种情况在网络拥塞的时候是可能发生的。
至于具体怎么修改就看业务逻辑应该怎么对待这种特殊情况了。
最后,总结下排查CPU故障的方法和技巧有哪些:
1、top命令:Linux命令。可以查看实时的CPU使用情况。也可以查看最近一段时间的CPU使用情况。
2、PS命令:Linux命令。强大的进程状态监控命令。可以查看进程以及进程中线程的当前CPU使用情况。属于当前状态的采样数据。
3、jstack:Java提供的命令。可以查看某个进程的当前线程栈运行情况。根据这个命令的输出可以定位某个进程的所有线程的当前运行状态、运行代码,以及是否死锁等等。
4、pstack:Linux命令。可以查看某个进程的当前线程栈运行情况。
Java 系统性能分析 命令
- cpu分析
top , pidstat(sysstat)
pid -p PID -t 1 10
vmstat 1 CPU上下文切换、运行队列、利用率
ps Hh -eo tid
pcpu 查看具体线程的CPU消耗
sar 来查看一定世界范围内以及历史的cpu消耗情况信息
查看java线程信息
jstack pid | grep 'nid=0x9999'
-
cs sy消耗比较高
上下文切换性能偏高, jstack -l pid, 查看on object monitor -
io消耗
pidstat -d -t -p pid 1 100
iostat -
网络io消耗
cat /proc/interruptes
sar -n FULL 1 2