-
-
Save conf/e7cb108de9841d284daf98fb58ffbbed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Usage example: ./dump2pdf thread-dump.txt # will create thread-dump.pdf | |
ruby tdg.rb $1 > /tmp/graph.dot | |
dot -Tpdf /tmp/graph.dot -o ${1%.*}.pdf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby -w | |
# | |
# tdg - Thread Dump Grapher (any better name?) | |
# | |
# Given a Java Thread Dump file as argument, it prints out | |
# a Grapviz (.dot) to generate a graph that shows which threads | |
# are waiting on others for a lock. | |
# (tested with a Sun JDK 1.4 thread dump) | |
# | |
# Transform the DOT file to PDF: | |
# dot -Tpdf graph.dot -o graph.pdf | |
# | |
# from: http://neuroning.com/2005/11/24/graphical-thread-dumps | |
# use: http://code.google.com/p/jrfonseca/wiki/XDot (to view the generated dot file) | |
class ThreadInfo | |
attr_accessor :stack, :name, :locks, :to_lock, :tid, :runnable | |
def initialize(id, name) | |
@tid = id | |
@name = name.gsub(/[{}<>|]/, '\\\\\1') # we need to escape special characters with backslashes | |
@runnable = false | |
@locks = [] | |
@to_lock= nil | |
end | |
end | |
### Main | |
filename = ARGV.shift | |
if filename == nil || ! File.readable?(filename) | |
fail( "Usage: $0 <thread_dump_file>") | |
end | |
# Parse thread dump file | |
lock_to_thread = {} | |
threads = [] | |
File.open(filename) do |file| | |
processing = false | |
currentThread = nil | |
file.each_line do |line| | |
# skip heading lines that are not part of the Thread dump | |
processing ||= ( line =~ /Full thread dump/ ) | |
next if not processing | |
case line | |
when /^"(.*)".*tid=(\w+) +nid=\w+ (runnable)?/ | |
currentThread = ThreadInfo.new($2, $1) | |
currentThread.runnable = ($3 != nil) | |
threads << currentThread | |
when /- (?:locked )?<(\w+)>/ | |
if currentThread != nil then | |
currentThread.locks << $1 | |
lock_to_thread[$1] = currentThread.tid | |
end | |
when /waiting to lock <(\w+)>/ | |
currentThread.to_lock = $1 if currentThread != nil | |
when /parking to wait for <(\w+)>/ | |
currentThread.to_lock = $1 if currentThread != nil | |
end | |
end | |
end | |
## Generate output | |
print <<EOS | |
digraph G { | |
edge [fontname="Helvetica",fontsize=10,labelfontname="Helvetica",labelfontsize=10]; | |
node [fontname="Helvetica",fontsize=10,shape=record,style=filled]; | |
rankdir=LR; | |
ranksep=1; | |
bgcolor=white; | |
EOS | |
threads.each do |t| | |
# we ignore threads that are not involved in lockings | |
next if t.locks.empty? and t.to_lock == nil | |
if t.runnable | |
color = "green" | |
else | |
color = t.to_lock == nil ? "pink" : "red" | |
end | |
print <<-EOS | |
Thread#{t.tid} [label="#{t.name}|Owns: #{t.locks.join(",") }" fillcolor=#{color} ]; | |
EOS | |
# print the Lock dependencies between threads (edges of the graph) | |
if t.to_lock != nil then | |
lockOwner = lock_to_thread[t.to_lock] || "Unknown" | |
print <<-EOS | |
Thread#{t.tid} -> Thread#{lockOwner} [taillabel="#{t.to_lock}" arrowhead=normal,arrowtail=none]; | |
EOS | |
end | |
end | |
print "}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment