排查高CPU占用

一個(gè)應(yīng)用占用CPU很高,除了確實(shí)是計(jì)算密集型應(yīng)用之外,通常原因都是出現(xiàn)了死循環(huán)。

(友情提示:本博文章歡迎轉(zhuǎn)載,但請(qǐng)注明出處:hankchen,http://www.blogjava.net/hankchen

以我們最近出現(xiàn)的一個(gè)實(shí)際故障為例,介紹怎么定位和解決這類問(wèn)題。

圖1

根據(jù)top命令,發(fā)現(xiàn)PID為28555的Java進(jìn)程占用CPU高達(dá)200%,出現(xiàn)故障。

通過(guò)ps aux | grep PID命令,可以進(jìn)一步確定是tomcat進(jìn)程出現(xiàn)了問(wèn)題。但是,怎么定位到具體線程或者代碼呢?

首先顯示線程列表:

ps -mp pid -o THREAD,tid,time

圖2

找到了耗時(shí)最高的線程28802,占用CPU時(shí)間快兩個(gè)小時(shí)了!

其次將需要的線程ID轉(zhuǎn)換為16進(jìn)制格式:

printf "%x\n" TID


圖3

最后打印線程的堆棧信息:

jstack pid |grep tid -A 30

圖4

找到出現(xiàn)問(wèn)題的代碼了!

現(xiàn)在來(lái)分析下具體的代碼:ShortSocketIO.readBytes(ShortSocketIO.java:106)

ShortSocketIO是應(yīng)用封裝的一個(gè)用短連接Socket通信的工具類。readBytes函數(shù)的代碼如下:

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;

??????? }

?? }

}

問(wèn)題就出在斜體標(biāo)黑的代碼部分。如果this.in.read()返回的數(shù)據(jù)小于等于0時(shí),循環(huán)就一直進(jìn)行下去了。而這種情況在網(wǎng)絡(luò)擁塞的時(shí)候是可能發(fā)生的。

至于具體怎么修改就看業(yè)務(wù)邏輯應(yīng)該怎么對(duì)待這種特殊情況了。


最后,總結(jié)下排查CPU故障的方法和技巧有哪些:

1、top命令:Linux命令??梢圆榭磳?shí)時(shí)的CPU使用情況。也可以查看最近一段時(shí)間的CPU使用情況。

2、PS命令:Linux命令。強(qiáng)大的進(jìn)程狀態(tài)監(jiān)控命令??梢圆榭催M(jìn)程以及進(jìn)程中線程的當(dāng)前CPU使用情況。屬于當(dāng)前狀態(tài)的采樣數(shù)據(jù)。

3、jstack:Java提供的命令??梢圆榭茨硞€(gè)進(jìn)程的當(dāng)前線程棧運(yùn)行情況。根據(jù)這個(gè)命令的輸出可以定位某個(gè)進(jìn)程的所有線程的當(dāng)前運(yùn)行狀態(tài)、運(yùn)行代碼,以及是否死鎖等等。

4、pstack:Linux命令??梢圆榭茨硞€(gè)進(jìn)程的當(dāng)前線程棧運(yùn)行情況。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容