/
do-jstack-loop
executable file
·191 lines (141 loc) · 4.22 KB
/
do-jstack-loop
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl
use strict;
my (%options,$PID,$DELAY,$LOG_FILE,$jstack_dir);
sgetopt( [ 'h', 'p:', 'd:', 'o:' ], \%options );
usage() if (defined $options{'h'});
$PID=$options{'p'};
if (! defined $PID ) {
usage(" -p option is not defined");
}
$DELAY=10;
if (defined $options{'d'}) {
$DELAY = $options{'d'};
if ($DELAY <= 0) {
usage(" -d parameter must be positive integer");
}
}
if (defined $options{'o'}) {
$LOG_FILE=$options{'o'};
}
if (defined $LOG_FILE) {
open STDOUT, '>', $LOG_FILE or die "Can't open $LOG_FILE for writing\n";
}
checkEnvironment();
runLoop();
sub usage
{
my $msg=shift;
if ( defined $msg ) {
print "ERROR: $msg\n"
}
print <<EOF
Usage: do-jstack-loop -p <pid> [ -d <delay time> ] [ -o <log file> ]
Description
The script runs jstack in a loop with a period of <delay time> (default value 10 seconds).
Usage:
-p <pid> process to monitor, the process should host the hotspot JVM.
-d <delay> repeat jstack with this delay (in seconds)
-o <logfile> Logs all output to the <logfile>
The script also performs the following steps
- check is <pid> is that of a running process
- check of <pid> is running from the same user account as current account
- check is <pid> is that of a java process (running hostpot JVM)
- Find jstack from the same JVM as from running process.
- run jstack with that version of jvm.
For each JAVA thread it also shows the Operating system thread id (LWP id) and CPU Usage (in %) of that thread.
This information can be used to debug applications that got into infinite loops.
EOF
;
exit(1);
}
sub checkEnvironment
{
my ($user,$jvm_so);
if (! -d "/proc/$PID" ) {
print "Error: The process with pid $PID is not running\n";
exit(1);
}
$user=`stat -c "%U" /proc/$PID`;
chomp($user);
if ( $user ne $ENV{'USER'}) {
print "Error: The process with pid $PID runs under user $user, this script should run under the same account\n";
exit(1);
}
my @toks = split(" ",`grep libjvm.so /proc/$PID/maps | head -1`);
$jvm_so=pop( @toks );
$jstack_dir=$jvm_so;
while(! -f "${jstack_dir}/bin/jstack" ) {
if ($jstack_dir eq "" || $jstack_dir eq "/") {
print "The JDK in $jvm_so does not contain jstack, can't run.\n";
exit(1);
}
$jstack_dir=`dirname $jstack_dir`;
chomp($jstack_dir);
}
}
sub runLoop
{
my ($LINE,$tid,$cpu,$line,$HEADER_LINE,$ps_report_line,,@jstack_lines,@toks,%pid_to_info,$PSREPORT);
$HEADER_LINE=`ps -F -p $PID | head -1`;
chomp($HEADER_LINE);
while( -d "/proc/$PID/" ) {
%pid_to_info = ();
$ps_report_line = ` ps --no-headers -F -p $PID`;
$PSREPORT=`ps --no-headers -LF -p $PID`;
for $line (split("\n", $PSREPORT) ) {
@toks = split(" ",$line);
$tid = $toks[3];
$cpu = $toks[4];
$pid_to_info{ $tid } = "=== OS Thread Id: $tid CPU Usage: ${cpu}\% ===\n";
}
print <<EOF
------------------------------------------------------------------------------
$HEADER_LINE
$ps_report_line
EOF
;
@jstack_lines = split("\n",`$jstack_dir/bin/jstack -l $PID`);
for $line (@jstack_lines) {
if ($line =~/nid=(\S+)/) {
my $npid = hex( $1 );
if (defined $pid_to_info{ $npid } ) {
print $pid_to_info{ $npid };
}
}
print "$line\n";
}
sleep($DELAY);
}
}
# ----------------------------------------------------------------------------
sub sgetopt
{
my $opts = shift;
my $resref = shift;
my ($i,$argval,$optval,$has_value);
for($i=0; $i < scalar(@ARGV) ; ) {
$argval = $ARGV[ $i++ ];
next if (! substr($argval,0,1) eq "-" && length($argval) == 2);
$argval = substr($argval,1);
$has_value = 0;
foreach $optval (@{ $opts }) {
if ($argval eq substr($optval,0,1)) {
$has_value = 1;
if (index($optval,':') != -1) {
if ($i >= scalar(@ARGV)) {
print "ERROR: option -$argval must have a value\n";
exit(1);
}
$has_value = 1;
${ $resref } { $argval } = $ARGV[ $i ++ ];
} else {
${ $resref } { $argval } = 1;
}
}
}
if (! $has_value) {
print "ERROR: Command line option -$argval is not defined\n";
exit(1);
}
}
}