经验不在于年限,在于积累---专注互联网软件开发

把工作当事业做,把项目当作品做!

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  55 Posts :: 0 Stories :: 66 Comments :: 0 Trackbacks

一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环。

(友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hankchen

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

clip_image002

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

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

首先显示线程列表:

ps -mp pid -o THREAD,tid,time

1

找到了耗时最高的线程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命令。可以查看某个进程的当前线程栈运行情况。

(友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hankchen

posted on 2012-05-09 20:20 hankchen 阅读(41398) 评论(7)  编辑  收藏 所属分类: 工作感悟

Feedback

# 非常感谢![未登录] 2012-08-09 10:05 lk
最近我们的应用极不正常,CPU都占到700%了,用你的方法查了一下,终于找到了原因!非常感谢!  回复  更多评论
  

# re: 线上应用故障排查之一:高CPU占用 2013-03-01 17:11 netcat
您好,请教个问题:如何查看tomcat连接池的排队请求数。我email:297020555@qq.com,多谢。  回复  更多评论
  

# re: 线上应用故障排查之一:高CPU占用 2013-03-13 14:38 boys99@163.com
非常谢谢你,用你的方法帮我快速定位到问题所在点。谢谢!  回复  更多评论
  

# re: 线上应用故障排查之一:高CPU占用 2014-08-18 17:27 少林功夫好
如果不是java进程呢?如果是php-fpm,是nginx怎么办呢?新人,求一些指导。  回复  更多评论
  

# re: 线上应用故障排查之一:高CPU占用 2015-06-25 12:49 Jerry Lee
脚本show-busy-java-threads.sh https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#beer-show-busy-java-threadssh 可以一键找出出在运行的Java进程中,消耗CPU最多的线程栈。用于快速排查Java的性能问题。 可以试试 :)
  回复  更多评论
  

# re: 线上应用故障排查之一:高CPU占用 2015-08-06 16:41 王亮
很感谢您的这篇文章,谢谢  回复  更多评论
  

# re: 线上应用故障排查之一:高CPU占用[未登录] 2016-03-09 18:09 QQ
嗯 太赞了  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: