-
Notifications
You must be signed in to change notification settings - Fork 5
/
jdt
executable file
·133 lines (102 loc) · 3.2 KB
/
jdt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# Java Dump Tool (jdt), a wrapper of jstack, jmap, jinfo, jstat, and so on
# Used to protect the scene before restart a problematic java application
# Usage: 1. if there is only one running java process, just: jdt
# 2. given a java pid: jdt pid
# Created by zhouwei on 2016-9-18
if (($# >= 2)) ; then
echo "Usage: jdt [pid]"
exit 1
fi
print_line() {
echo ""
echo "=================================================================================="
echo ""
}
# get a effective java pid
if [[ "$1" != "" ]]; then
if ! ps aux | grep -v grep | grep java | awk '{print $2}' | grep "^$1$" >&/dev/null ; then
echo "ERROR: $1 is not a java pid!"
exit 1
fi
PID="$1"
else
num_java_process="$(ps aux | grep -v grep | grep java | wc -l | awk '{print $1}')"
if [[ "$num_java_process" == "0" ]]; then
echo "please provide a pid: jdt pid"
exit 1
elif [[ "$num_java_process" == "1" ]]; then
PID="$(ps aux | grep -v grep | grep java | awk '{print $2}')"
elif [[ "$num_java_process" -gt "1" ]]; then
echo "please provide a pid: jdt pid"
echo "there are multiple java processes, pick one among them:"
echo ""
ps aux | head -1
ps aux | grep -v grep | grep --color=auto java
exit 1
fi
fi
echo "starting dump java process: $PID"
# make a dump directory
output_dir="$(pwd)/dump_java_$PID"
[[ -d $output_dir ]] || mkdir -p $output_dir
cd $output_dir
# ps aux, /proc, and top
echo "starting ps, /proc, and top ..."
exec 3>&1
exec 1>ps_proc_top.log
echo "date: $(date '+%Y-%m-%d %H:%M:%S')"
print_line
echo "ps aux - java:"
ps aux | head -1
ps aux | grep -v grep | grep $PID
print_line
echo "/proc/${PID}/status: "
cat /proc/${PID}/status
print_line
echo "top - java:"
top -b -n 4 -H -p $PID
# jstack (use -F to force a dump when it does not respond, see "man jstack")
exec 1>&3 ; echo "starting jstack ..."
exec 1>jstack.log
jstack $PID
exec 1>jstack_l.log
echo "jstack -l $PID:"
jstack -l $PID
# jmap (use -F to force a dump when it does not respond, see "man jmap")
exec 1>&3 ; echo "starting jmap ..."
exec 1>jmap_heap.log
jmap -heap $PID
exec 1>jmap_histo_live.log
jmap -histo:live $PID
# -clstats is for jdk8
exec 1>jmap_clstats.log
jmap -clstats $PID 2>/dev/null
exec 1>jmap_finalizerinfo.log
jmap -finalizerinfo $PID
jmap -dump:live,format=b,file=jmap_dump.bin $PID
# jinfo, print Java system properties and VM flags
exec 1>&3 ; echo "starting jinfo ..."
exec 1>jinfo.log
jinfo $PID
# jstat
exec 1>&3 ; echo "starting jstat ..."
jstat -gc $PID 250ms 20 > jstat_gc.log
jstat -gccapacity $PID 250ms 10 > jstat_gccapacity.log
jstat -gccause $PID 250ms 10 > jstat_gccause.log
jstat -class $PID 250ms 10 > jstat_class.log
jstat -gcnew $PID 250ms 10 > jstat_gcnew.log
jstat -gcnewcapacity $PID 250ms 10 > jstat_gcnewcapacity.log
jstat -gcold $PID 250ms 10 > jstat_gcold.log
jstat -gcoldcapacity $PID 250ms 10 > jstat_gcoldcapacity.log
# -gcmetacapacity is for jdk8
jstat -gcmetacapacity $PID 250ms 10 > jstat_gcmetacapacity.log 2>/dev/null
# -gcpermcapacity is for jdk version <= 7
jstat -gcpermcapacity $PID 250ms 10 > jstat_gcpermcapacity.log 2>/dev/null
exec 1>&3
exec 3>&-
echo ""
echo "Java Dump Tool (jdt) finished!"
echo "output directory is: $output_dir"
echo ""
exit 0