Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ source <(curl -fsSL https://raw.githubusercontent.com/oldratlee/useful-scripts/m

1. [show-busy-java-threads.sh](docs/java.md#beer-show-busy-java-threadssh)
打印出在运行的`Java`进程中,消耗`CPU`最多的线程栈。用于快速排查`Java`的性能问题。
如果执行脚本的用户不是root,需要前面加上sudo
1. [show-duplicate-java-classes](docs/java.md#beer-show-duplicate-java-classes)
找出`jar`文件和`class`目录中的重复类。用于排查`Java`类冲突问题。
1. [find-in-jars.sh](docs/java.md#beer-find-in-jarssh)
Expand Down
22 changes: 14 additions & 8 deletions show-busy-java-threads.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,36 @@ cleanupWhenExit() {
trap "cleanupWhenExit" EXIT

printStackOfThread() {
local threadLine
local line
local count=1
while read threadLine ; do
local pid=`echo ${threadLine} | awk '{print $1}'`
local threadId=`echo ${threadLine} | awk '{print $2}'`
while IFS=" " read -a line ; do
local pid=${line[0]}
local threadId=${line[1]}
local threadId0x=`printf %x ${threadId}`
local user=`echo ${threadLine} | awk '{print $3}'`
local pcpu=`echo ${threadLine} | awk '{print $5}'`
local user=${line[2]}
local pcpu=${line[4]}

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

[ ! -f "${jstackFile}" ] && {
jstack ${pid} > ${jstackFile} || {
{
if [ "${user}" == "${USER}" ];then
jstack ${pid} > ${jstackFile}
else
sudo -u ${user} jstack ${pid} > ${jstackFile}
fi
} || {
redEcho "Fail to jstack java process ${pid}!"
rm ${jstackFile}
continue
}
}

redEcho "[$((count++))] 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}' ||
Expand Down