Java 線程棧數據收集

最近做技術支持碰到一個問題,tc server 啓動快速死掉了,不知道什麼出錯,日誌都很乾淨,看不出來什麼異常,於是自己想寫一個程序來分析當時的線程棧,寫了個shell來抓:重點講一下我的思路:
1> 用jps抓運行的Java進程。
jps是jdk提供的一個查看當前Java進程的小工具, 可以看做是JavaVirtual Machine Process Status Tool的縮寫。使用官方說明:
jps General Commands Manual
Name
jps - Java Virtual Machine Process Status Tool
SYNOPSIS
jps [ options ] [ hostid ]
PARAMETERS
options
Command-line options.
hostid
The host identifier of the host for which the process report should be generated. The hostid may include optional components that indicate the communications protocol, port number, and other implementation specific
data.

重點看看它所支持的參數:
-q Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM identifiers.
-m Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l Output the full package name for the application’s main class or the full path name to the application’s JAR file.
-v Output the arguments passed to the JVM.
-V Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags= argument).
-Joption
Pass option to the java launcher called by jps. For example, -J-Xms48m sets the startup memory to 48 megabytes. It is a common convention for -J to pass options to the underlying VM executing applications written in
Java.

HOST IDENTIFIER
The host identifier, or hostid is a string that indicates the target system. The syntax of the hostid string largely corresponds to the syntax of a URI:
[protocol:][[//]hostname][:port][/servername]
protocol
The communications protocol. the default protocol is rmi.
hostname
A hostname or IP address indicating the target host. If hostname is omitted, then the target host is the local host.
參看實例:
1)jps
bash-4.2$ jps
3064 ProviderImpl
11666 activemq.jar
10768 Locator
23951 Bootstrap
23891 WrapperStartStopApp
2)jps –l:輸出主類或者jar的完全路徑名
3)jps –v :輸出jvm參數
4)jps –q :僅僅顯示java進程號
5) jps -m:輸出啓動參數。

在實際使用中,如果有多個類似的實例啓動,爲了更好的定位,往往需要多個參數輸出,然後根據輸出進行篩選才找到你需要定位的Java 進程。

2> 用jstack抓運行的Java thread。
jstack General Commands Manual
Name
jstack - Stack Trace
SYNOPSIS
jstack [ option ] pid
jstack [ option ] executable core
jstack [ option ] [server-id@]remote-hostname-or-IP
PARAMETERS
Options are mutually exclusive. Option, if used, should follow immediately after the command name. See OPTIONS.
DESCRIPTION
jstack prints Java stack traces of Java threads for a given Java process or core file or a remote debug server. For each Java frame, the full class name, method name, ‘bci’ (byte code index) and line number, if available,
are printed. With the -m option, jstack prints both Java and native frames of all threads along with the ‘pc’ (program counter). For each native frame, the closest native symbol to ‘pc’, if available, is printed. C++ mangled
names are not demangled. To demangle C++ names, the output of this command may be piped to c++filt. If the given process is running on a 64-bit VM, you may need to specify the -J-d64 option, e.g.:
jstack -J-d64 -m pid
OPTIONS
-F Force a stack dump when ‘jstack [-l] pid’ does not respond.
-l Long listing. Prints additional information about locks such as list of owned java.util.concurrent ownable synchronizers @
-m prints mixed mode (both Java and native C/C++ frames) stack trace.
-h prints a help message.

jstack使用很簡單,但是線程是在不斷地在變化的,爲了捕抓住更多的信息,一般都是要多次定時去檢查才能準確的拿到更多有用的信息。
我寫了一個腳本就是反覆的檢查進程,然後輸出10次線程棧,需要的可以點擊 下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章