Linux 系統 CPU 100% 異常問(wèn)題,能否用一個(gè) Shell 腳本完美解決?
2、排查思路
2.1 定位高負載進(jìn)程 pid
觀(guān)察各個(gè)進(jìn)程資源使用情況,可以看出進(jìn)程id為682的進(jìn)程,有著(zhù)較高的CPU占比
2.2 定位具體的異常業(yè)務(wù)
可得出結論:該進(jìn)程對應的就是數據平臺的web服務(wù)。
2.3 定位異常線(xiàn)程及具體代碼行
傳統的方案一般是4步:
1、top oder by with P:1040 // 首先按進(jìn)程負載排序找到 maxLoad(pid)
2、top -Hp 進(jìn)程PID:1073 // 找到相關(guān)負載 線(xiàn)程PID
3、printf “0x%x ”線(xiàn)程PID:0x431 // 將線(xiàn)程PID轉換為 16進(jìn)制,為后面查找 jstack 日志做準備
4、jstack 進(jìn)程PID | vim +/十六進(jìn)制線(xiàn)程PID - // 例如:jstack 1040|vim +/0x431 -
但是對于線(xiàn)上問(wèn)題定位來(lái)說(shuō),分秒必爭,上面的 4 步還是太繁瑣耗時(shí)了,之前介紹過(guò)淘寶的 oldratlee 同學(xué)就將上面的流程封裝為了一個(gè)工具:show-busy-java-threads.sh,可以很方便的定位線(xiàn)上的這類(lèi)問(wèn)題:
可得出結論:是系統中一個(gè)時(shí)間工具類(lèi)方法的執行cpu占比較高,定位到具體方法后,查看代碼邏輯是否存在性能問(wèn)題。
※ 如果線(xiàn)上問(wèn)題比較緊急,可以省略 2.1、2.2 直接執行 2.3,這里從多角度剖析只是為了給大家呈現一個(gè)完整的分析思路。
3、根因分析
異常方法邏輯:是把時(shí)間戳轉成對應的具體的日期時(shí)間格式; 上層調用:計算當天凌晨至當前時(shí)間所有秒數,轉化成對應的格式放入到set中返回結果; 邏輯層:對應的是數據平臺實(shí)時(shí)報表的查詢(xún)邏輯,實(shí)時(shí)報表會(huì )按照固定的時(shí)間間隔來(lái),并且在一次查詢(xún)中有多次(n次)方法調用。
4、解決方案
5、總結
在編碼的過(guò)程中,除了要實(shí)現業(yè)務(wù)的邏輯,也要注重代碼性能的優(yōu)化。一個(gè)業(yè)務(wù)需求,能實(shí)現,和能實(shí)現的更高效、更優(yōu)雅其實(shí)是兩種截然不同的工程師能力和境界的體現,而后者也是工程師的核心競爭力。 在代碼編寫(xiě)完成之后,多做 review,多思考是不是可以用更好的方式來(lái)實(shí)現。 線(xiàn)上問(wèn)題不放過(guò)任何一個(gè)小細節!細節是魔鬼,技術(shù)的同學(xué)需要有刨根問(wèn)題的求知欲和追求卓越的精神,只有這樣,才能不斷的成長(cháng)和提升。
附上show-busy-java-threads.sh腳本:
#!/bin/bash
# @Function
# Find out the highest cpu consumed threads of java, and print the stack of these threads.
#
# @Usage
# $ ./show-busy-java-threads.sh
#
# @author Jerry Lee
readonly PROG=`basename$0`
readonly-a COMMAND_LINE=("$0""$@")
usage(){
cat<<EOF
Usage: ${PROG}[OPTION]...
Find out the highest cpu consumed threads of java, and print the stack of these threads.
Example: ${PROG}-c 10
Options:
-p,--pid find out the highest cpu consumed threads from the specifed java process,
default from all java process.
-c,--count set the thread count to show, default is 5
-h,--helpdisplay this help and exit
EOF
exit$1
}
readonly ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`
[ $?-ne 0]&& usage 1
evalset--"${ARGS}"
whiletrue;do
case"$1"in
-c|--count)
count="$2"
shift2
;;
-p|--pid)
pid="$2"
shift2
;;
-h|--help)
usage
;;
--)
shift
break
;;
esac
done
count=${count:-5}
redEcho(){
[-c /dev/stdout ]&&{
# if stdout is console, turn on color output.
echo-ne "\033[1;31m"
echo-n "$@"
echo-e "\033[0m"
}||echo"$@"
}
yellowEcho(){
[-c /dev/stdout ]&&{
# if stdout is console, turn on color output.
echo-ne "\033[1;33m"
echo-n "$@"
echo-e "\033[0m"
}||echo"$@"
}
blueEcho(){
[-c /dev/stdout ]&&{
# if stdout is console, turn on color output.
echo-ne "\033[1;36m"
echo-n "$@"
echo-e "\033[0m"
}||echo"$@"
}
# Check the existence of jstack command!
if!which jstack &>/dev/null;then
[-z "$JAVA_HOME"]&&{
redEcho "Error: jstack not found on PATH!"
exit1
}
![-f "$JAVA_HOME/bin/jstack"]&&{
redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack file does NOT exists!"
exit1
}
![-x "$JAVA_HOME/bin/jstack"]&&{
redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack is NOT executalbe!"
exit1
}
export PATH="$JAVA_HOME/bin:$PATH"
fi
readonly uuid=`date +%s`_${RANDOM}_$$
cleanupWhenExit(){
rm/tmp/${uuid}_*&>/dev/null
}
trap"cleanupWhenExit" EXIT
printStackOfThreads(){
local line
local count=1
while IFS=" "read-a line ;do
local pid=${line[0]}
local threadId=${line[1]}
local threadId0x="0x`printf %x ${threadId}`"
local user=${line[2]}
local pcpu=${line[4]}
local jstackFile=/tmp/${uuid}_${pid}
[!-f "${jstackFile}"]&&{
{
if["${user}"=="${USER}"];then
jstack ${pid}>${jstackFile}
else
if[$UID==0];then
sudo -u ${user} jstack ${pid}>${jstackFile}
else
redEcho "[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
redEcho "User of java process($user) is not current user($USER), need sudo to run again:"
yellowEcho "sudo ${COMMAND_LINE[@]}"
echo
continue
fi
fi
}||{
redEcho "[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
echo
rm${jstackFile}
continue
}
}
blueEcho "[$((count++))] Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user}):"
sed "/nid=${threadId0x} /,/^$/p"-n ${jstackFile}
done
}
ps -Leo pid,lwp,user,comm,pcpu --no-headers |{
[-z "${pid}"]&&
awk '$4=="java"{print $0}'||
awk -v "pid=${pid}"'$1==pid,$4=="java"{print $0}'
}|sort-k5 -r -n |head--lines "${count}"| printStackOfThreads