Murayama blog.

it works.

スレッドダンプの取り方

最近、いろいろと調べる必要があったので記念にメモします。

スレッドダンプとは

JVM上のすべてのスレッドの状態とスタックトレースを出力したものです、と。

コマンドラインからスレッドダンプを取得する

GUIで確認もいいけど、今回はコマンドラインからの実行で取得してみます。
とりあえず2つの方法があります。他にもあるのかも。

  • QUITシグナルによる取得
  • jstackによる取得
QUITシグナルによる取得
$ kill -QUIT <JavaのプロセスID>


ちなみにJavaのプロセスIDはjpsコマンドで調べると便利。

$ jps
10029 
12179 Main
10919 HMaster
13335 Jps
13333 Bootstrap
jstackによる取得
$ jstack <JavaのプロセスID>

どんなときに使うの

システムがトラブったとき。たとえば、

  • 無限ループしちゃってるスレッドを探す
  • 処理に時間のかかってるメソッドを探す
  • スレッドのロックの取得状況の確認

とかかな。

Tomcatのスレッドダンプの例

jstackでTomcatのスレッドダンプを取ってみた。

$ jstack 13333
2012-02-04 21:05:08
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.4-b02-402 mixed mode):

"Attach Listener" daemon prio=9 tid=7fca0347e000 nid=0x1133b6000 waiting on condition [00000000]
   java.lang.Thread.State: RUNNABLE

"TP-Monitor" daemon prio=5 tid=7fca01bbe000 nid=0x114ad2000 in Object.wait() [114ad1000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <7f3cdc590> (a org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable)
	at org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable.run(ThreadPool.java:565)
	- locked <7f3cdc590> (a org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable)
	at java.lang.Thread.run(Thread.java:680)

"TP-Processor4" daemon prio=5 tid=7fca01bbd000 nid=0x1149cf000 runnable [1149ce000]
   java.lang.Thread.State: RUNNABLE
	at java.net.PlainSocketImpl.socketAccept(Native Method)
	at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
	- locked <7f3cb45a8> (a java.net.SocksSocketImpl)
	at java.net.ServerSocket.implAccept(ServerSocket.java:462)
	at java.net.ServerSocket.accept(ServerSocket.java:430)
	at org.apache.jk.common.ChannelSocket.accept(ChannelSocket.java:311)
	at org.apache.jk.common.ChannelSocket.acceptConnections(ChannelSocket.java:668)
	at org.apache.jk.common.ChannelSocket$SocketAcceptor.runIt(ChannelSocket.java:879)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
	at java.lang.Thread.run(Thread.java:680)

"TP-Processor3" daemon prio=5 tid=7fca01bbc800 nid=0x1148cc000 in Object.wait() [1148cb000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <7f3cdbd60> (a org.apache.tomcat.util.threads.ThreadPool$ControlRunnable)
	at java.lang.Object.wait(Object.java:485)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:662)
	- locked <7f3cdbd60> (a org.apache.tomcat.util.threads.ThreadPool$ControlRunnable)
	at java.lang.Thread.run(Thread.java:680)

"TP-Processor2" daemon prio=5 tid=7fca01bbb800 nid=0x1147c9000 in Object.wait() [1147c8000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <7f3cdb970> (a org.apache.tomcat.util.threads.ThreadPool$ControlRunnable)
	at java.lang.Object.wait(Object.java:485)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:662)
	- locked <7f3cdb970> (a org.apache.tomcat.util.threads.ThreadPool$ControlRunnable)
	at java.lang.Thread.run(Thread.java:680)

"TP-Processor1" daemon prio=5 tid=7fca01bca000 nid=0x1146c6000 in Object.wait() [1146c5000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <7f3cdb580> (a org.apache.tomcat.util.threads.ThreadPool$ControlRunnable)
	at java.lang.Object.wait(Object.java:485)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:662)
	- locked <7f3cdb580> (a org.apache.tomcat.util.threads.ThreadPool$ControlRunnable)
	at java.lang.Thread.run(Thread.java:680)

"http-8080-1" daemon prio=5 tid=7fca03473000 nid=0x1145c3000 in Object.wait() [1145c2000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <7f395eb20> (a org.apache.tomcat.util.net.JIoEndpoint$Worker)
	at java.lang.Object.wait(Object.java:485)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.await(JIoEndpoint.java:458)
	- locked <7f395eb20> (a org.apache.tomcat.util.net.JIoEndpoint$Worker)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:484)
	at java.lang.Thread.run(Thread.java:680)

"http-8080-Acceptor-0" daemon prio=5 tid=7fca03472000 nid=0x1144c0000 runnable [1144bf000]
   java.lang.Thread.State: RUNNABLE
	at java.net.PlainSocketImpl.socketAccept(Native Method)
	at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
	- locked <7f3258670> (a java.net.SocksSocketImpl)
	at java.net.ServerSocket.implAccept(ServerSocket.java:462)
	at java.net.ServerSocket.accept(ServerSocket.java:430)
	at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61)
	at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:352)
	at java.lang.Thread.run(Thread.java:680)

"ContainerBackgroundProcessor[StandardEngine[Catalina]]" daemon prio=5 tid=7fca03226800 nid=0x1142fd000 waiting on condition [1142fc000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1631)
	at java.lang.Thread.run(Thread.java:680)

"Poller SunPKCS11-Darwin" daemon prio=1 tid=7fca01b4f800 nid=0x1141b7000 waiting on condition [1141b6000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at sun.security.pkcs11.SunPKCS11$TokenPoller.run(SunPKCS11.java:692)
	at java.lang.Thread.run(Thread.java:680)

"GC Daemon" daemon prio=2 tid=7fca0197d800 nid=0x1140b4000 in Object.wait() [1140b3000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <7f4693510> (a sun.misc.GC$LatencyLock)
	at sun.misc.GC$Daemon.run(GC.java:100)
	- locked <7f4693510> (a sun.misc.GC$LatencyLock)

"Low Memory Detector" daemon prio=5 tid=7fca01871800 nid=0x1138f4000 runnable [00000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=9 tid=7fca01870800 nid=0x1137f1000 waiting on condition [00000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=9 tid=7fca01870000 nid=0x1136ee000 waiting on condition [00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=9 tid=7fca0186f000 nid=0x1135eb000 runnable [00000000]
   java.lang.Thread.State: RUNNABLE

"Surrogate Locker Thread (Concurrent GC)" daemon prio=5 tid=7fca0305d800 nid=0x1134e8000 waiting on condition [00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=7fca03040000 nid=0x1131f8000 in Object.wait() [1131f7000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <7f44c0cf8> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
	- locked <7f44c0cf8> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)

"Reference Handler" daemon prio=10 tid=7fca0303f000 nid=0x1130f5000 in Object.wait() [1130f4000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <7f44c0bc0> (a java.lang.ref.Reference$Lock)
	at java.lang.Object.wait(Object.java:485)
	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
	- locked <7f44c0bc0> (a java.lang.ref.Reference$Lock)

"main" prio=5 tid=7fca03001000 nid=0x10c797000 runnable [10c795000]
   java.lang.Thread.State: RUNNABLE
	at java.net.PlainSocketImpl.socketAccept(Native Method)
	at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
	- locked <7f3cf49d0> (a java.net.SocksSocketImpl)
	at java.net.ServerSocket.implAccept(ServerSocket.java:462)
	at java.net.ServerSocket.accept(ServerSocket.java:430)
	at org.apache.catalina.core.StandardServer.await(StandardServer.java:431)
	at org.apache.catalina.startup.Catalina.await(Catalina.java:676)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:628)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

"VM Thread" prio=9 tid=7fca0303a800 nid=0x112ff2000 runnable 

"Gang worker#0 (Parallel GC Threads)" prio=9 tid=7fca03002800 nid=0x10fad0000 runnable 

"Gang worker#1 (Parallel GC Threads)" prio=9 tid=7fca03003000 nid=0x10fbd3000 runnable 

"Concurrent Mark-Sweep GC Thread" prio=9 tid=7fca01819800 nid=0x112c98000 runnable 
"VM Periodic Task Thread" prio=10 tid=7fca0187b000 nid=0x1139f7000 waiting on condition 

"Exception Catcher Thread" prio=10 tid=7fca03001800 nid=0x10c8f9000 runnable 
JNI global references: 1002