返回顶部

收藏

分析java进程cpu使用率过高的shell脚本

更多

分析java进程cpu使用率过高的shell脚本

#!/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

PROG=`basename $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
}

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 "$@"
}

# 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

uuid=`date +%s`_${RANDOM}_$$

cleanupWhenExit() {
    rm /tmp/${uuid}_* &> /dev/null
}
trap "cleanupWhenExit" EXIT

printStackOfThread() {
    while read threadLine ; do
        pid=`echo ${threadLine} | awk '{print $1}'`
        threadId=`echo ${threadLine} | awk '{print $2}'`
        threadId0x=`printf %x ${threadId}`
        user=`echo ${threadLine} | awk '{print $3}'`
        pcpu=`echo ${threadLine} | awk '{print $5}'`

        jstackFile=/tmp/${uuid}_${pid}

        [ ! -f "${jstackFile}" ] && {
            jstack ${pid} > ${jstackFile} || {
                redEcho "Fail to jstack java process ${pid}!"
                rm ${jstackFile}
                continue
            }
        }

        redEcho "Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"
        sed "/nid=0x${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}" | printStackOfThread

标签:shell,linux,java

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. zhuangli 发表 2016-12-25 15:09:39 shell中>/dev/null 2>&1是什么鬼?
  2. bystander 发表 2013-08-11 08:55:33 [译]Unix sed实用教程第八篇–CSV文件操作
  3. 王爵 发表 2015-10-06 10:27:00 java、Android、Jquery、Struts、Linux等资源
  4. 上官 江 发表 2012-11-24 03:39:43 你可能不知道的Shell
  5. inter12 发表 2017-01-20 04:51:06 glances 简介
  6. 吞拿鱼手卷 发表 2017-03-23 09:34:19 Linux运维工程师笔试题系列3(30题)
  7. nanxiao 发表 2016-01-20 02:00:45 Bash quoting简介
  8. icycandy 发表 2012-04-26 01:45:01 在Fedora 17上安装Sun JDK 7
  9. Qing 发表 2014-04-30 22:21:32 从新手到系统管理员(二):Linux新手学习Shell脚本编程的五个例子
  10. starsea 发表 2013-07-21 09:24:41 linux(mac)下的字符串处理
  11. master 发表 2017-05-10 07:34:27 使用gitweb展示git仓库
  12. fish 发表 2014-07-30 09:16:18 linux shell 用sed进行文本替换

发表评论