排查 Linux 系统 CPU 100% 异常问题

1.定位高负载进程pid

首先到服务器确认服务器情况,根据具体情况进行分析。

使用top命令

通过观察负载平均值,以及负载评估标准(8核),可以确认服务器存在负载低于以下情况;

观察各个进程的资源使用情况,我们发现该进程id为682的进程,其中绝大多数是CPU占用

2.定位具体的异常业务

这里我们使用 pwdx 命令根据 pid 找到业务进程路径,定位到负责人和项目:

可得出结论:该进程对应的就是数据平台的网络服务。

3 定位异常线程及具体代码行

传统的方案一般是4步:

1、top oder by with P: 1040 //首先按进程负载排序找到maxLoad(pid)

2、top -Hp 进程PID: 1073 // 找到相关负载 进程PID

3、printf “0x%x ”ThreadPID:  0x431 //将ThreadPID转换为16进制,为后面的任务 jstack 日志表现出

4、jstack 进程PID | vim +/十六进制线程PID – // 例如:jstack 1040|vim +/0x431 –

但是对于线上问题定位来说,分秒必争,上面4步还是太繁琐耗时了,之前介绍过淘宝的oldratlee同学就将上面的流程封装为一个工具:show-busy-java-threads.sh,可以很方便的定位线上的创新问题:

可得出结论:这是该系统一个时间工具类方法执行的CPU占比比较高,定位到具体方法后,查看代码逻辑是否存在问题。

※ 如果线上问题比较紧急,可以省略1、2直接执行3,这里从多角度剖析应当深入呈现一个完整的分析思路。

附上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, --help      display 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
eval set -- "${ARGS}"

while true; do
case "$1" in
    -c|--count)
        count="$2"
shift 2
        ;;
    -p|--pid)
        pid="$2"
shift 2
        ;;
    -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!"
exit 1
    }
    ! [ -f "$JAVA_HOME/bin/jstack" ] && {
        redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack file does NOT exists!"
exit 1
    }
    ! [ -x "$JAVA_HOME/bin/jstack" ] && {
        redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack is NOT executalbe!"
exit 1
    }
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