一、具体步骤

shift+p 按照cpu排序
shift+m按照内存排序
1、查看进程下所有线程 top -H -p  pid 
2、将十进制数换成16进制:print "%x/n" 线程id
3、查看进程下的线程正在执行的方法:  jstack  pid|grep  0X70d4

以我们最近出现的一个实际故障为例,介绍怎么定位和解决这类问题。

clip_image002

根据top命令,发现PID为28555的Java进程占用CPU高达200%,出现故障。

通过ps aux | grep PID命令,可以进一步确定是tomcat进程出现了问题。但是,怎么定位到具体线程或者代码呢?

首先显示线程列表:

ps -mp pid -o THREAD,tid,time

找到了耗时最高的线程28802,占用CPU时间快两个小时了!

其次将需要的线程ID转换为16进制格式:

printf "%x\n" tid

2

最后打印线程的堆栈信息:

jstack pid |grep tid -A 30

3

找到出现问题的代码了!

现在来分析下具体的代码: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 系统性能分析 命令

1. 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'

2. cs sy消耗比较高 
上下文切换性能偏高, jstack -l pid, 查看on object monitor

3. io消耗 
pidstat -d -t -p pid 1 100 
iostat

4. 网络io消耗 
cat /proc/interruptes 
sar -n FULL 1 2

最新文章

  1. 把url参数转化成一个对象返回
  2. python3+ 模块学习 之 re
  3. .NET (四)委托第四讲:内置委托Comparison
  4. app:transformClassesWithJarMergingForDebug uplicate entry: android/support/v4/app/BackStackState$1.class
  5. 【BZOJ1008】【HNOI2008】越狱
  6. Android addRule()
  7. 第二百一十天 how can I 坚持
  8. 菜鸟学习 git
  9. String str 与 String str=new String("") 区别
  10. C# 将datatable 转换json
  11. hive 中的Sort By、 Order By、Cluster By、Distribute By 区别
  12. SpringMVC经典系列-13使用SpringMVC处理Ajax请求---【LinusZhu】
  13. MySQL特殊语法---replace into
  14. LeetCode_Set Matrix Zeroes
  15. php 抽奖概率 随机数
  16. 版本控制工具——Git常用操作(下)
  17. Python之find命令中的位置的算法
  18. [原创]K8Cscan插件之C段旁站扫描\子域名扫描
  19. python中的zip、map、reduce 、lambda、filter函数的使用
  20. Tone Mapping算法系列二:一种自适应对数映射的高对比度图像显示技术及其速度优化。

热门文章

  1. 上手Keras
  2. c# 图片加密解密的实例代码
  3. Differences between Python2 and Python3
  4. python提示AttributeError: 'NoneType' object has no attribute 'append'
  5. CSS Id 和 Class选择器
  6. Android Gradle 构建工具(Android Gradle Build Tools)是什么?
  7. pexpect的pxssh类实现远程操作
  8. 俄罗斯最新开源的牛掰数据库ClickHouse
  9. The remote end hung up unexpectedly while git cloning
  10. docker 使用mysql