show-busy-java-threads

直接clone工程
使用简单、方便更新,不过要安装有git。

git clone git://github.com/oldratlee/useful-scripts.git

cd useful-scripts

# 使用Release分支的内容
git checkout release

# 更新脚本
git pull
包含2个分支:

master:开发分支
release:发布分支,功能稳定的脚本
当然如果你不想安装git,github是支持svn的:

svn co https://github.com/oldratlee/useful-scripts/branches/release

cd useful-scripts

# 更新脚本
svn up
PS:
我的做法是把useful-scripts checkout到$HOME/bin/useful-scripts目录下,再把$HOME/bin/useful-scripts配置到PATH变量上,这样方便我本地使用所有的脚本。

打包下载
下载文件release.zip:

wget --no-check-certificate https://github.com/oldratlee/useful-scripts/archive/release.zip

unzip release.zip
下载和运行单个文件
以show-busy-java-threads为例。

curl文件直接用bash运行
curl -sLk 'https://raw.github.com/oldratlee/useful-scripts/release/show-busy-java-threads' | bash
下载单个文件
wget --no-check-certificate https://raw.github.com/oldratlee/useful-scripts/release/show-busy-java-threads
chmod +x show-busy-java-threads

./show-busy-java-threads

show-busy-java-threads

# 从所有运行的Java进程中找出最消耗CPU的线程(缺省5个),打印出其线程栈

# 缺省会自动从所有的Java进程中找出最消耗CPU的线程,这样用更方便
# 当然你可以手动指定要分析的Java进程Id,以保证只会显示你关心的那个Java进程的信息
show-busy-java-threads -p <指定的Java进程Id>

show-busy-java-threads -c <要显示的线程栈数>

show-busy-java-threads <重复执行的间隔秒数> [<重复执行的次数>]
# 多次执行;这2个参数的使用方式类似vmstat命令

show-busy-java-threads -a <运行输出的记录到的文件>
# 记录到文件以方便回溯查看

show-busy-java-threads -S <存储jstack输出文件的目录>
# 指定jstack输出文件的存储目录,方便记录以后续分析

##############################
# 注意:
##############################
# 如果Java进程的用户 与 执行脚本的当前用户 不同,则jstack不了这个Java进程
# 为了能切换到Java进程的用户,需要加sudo来执行,即可以解决:
sudo show-busy-java-threads

show-busy-java-threads -s <指定jstack命令的全路径>
# 对于sudo方式的运行,JAVA_HOME环境变量不能传递给root,
# 而root用户往往没有配置JAVA_HOME且不方便配置,
# 显式指定jstack命令的路径就反而显得更方便了

# -m选项:执行jstack命令时加上-m选项,显示上Native的栈帧,一般应用排查不需要使用
show-busy-java-threads -m
# -F选项:执行jstack命令时加上 -F 选项(如果直接jstack无响应时,用于强制jstack),一般情况不需要使用
show-busy-java-threads -F
# -l选项:执行jstack命令时加上 -l 选项,显示上更多相关锁的信息,一般情况不需要使用
# 注意:和 -m -F 选项一起使用时,可能会大大增加jstack操作的耗时
show-busy-java-threads -l

# 帮助信息
$ show-busy-java-threads -h
Usage: show-busy-java-threads [OPTION]... [delay [count]]
Find out the highest cpu consumed threads of java processes,
and print the stack of these threads.

Example:
show-busy-java-threads # show busy java threads info
show-busy-java-threads 1 # update every 1 second, (stop by eg: CTRL+C)
show-busy-java-threads 3 10 # update every 3 seconds, update 10 times

Output control:
-p, --pid <java pid> find out the highest cpu consumed threads from
the specified java process.
default from all java process.
-c, --count <num> set the thread count to show, default is 5.
-a, --append-file <file> specifies the file to append output as log.
-S, --store-dir <dir> specifies the directory for storing
the intermediate files, and keep files.
default store intermediate files at tmp dir,
and auto remove after run. use this option to keep
files so as to review jstack/top/ps output later.
delay the delay between updates in seconds.
count the number of updates.
delay/count arguments imitates the style of
vmstat command.

jstack control:
-s, --jstack-path <path> specifies the path of jstack command.
-F, --force set jstack to force a thread dump. use when jstack
does not respond (process is hung).
-m, --mix-native-frames set jstack to print both java and native frames
(mixed mode).
-l, --lock-info set jstack with long listing.
prints additional information about locks.

CPU usage calculation control:
-d, --top-delay specifies the delay between top samples.
default is 0.5 (second). get thread cpu percentage
during this delay interval.
more info see top -d option. eg: -d 1 (1 second).


-P, --use-ps use ps command to find busy thread(cpu usage)
instead of top command.
default use top command, because cpu usage of
ps command is expressed as the percentage of
time spent running during the *entire lifetime*
of a process, this is not ideal in general.

Miscellaneous:
-h, --help display this help and exit.

原文地址:https://www.cnblogs.com/walkersss/p/14324478.html