Skip to content

Commit f62e47d

Browse files
author
Eduard Wirch
committed
stackcollapse-jstack supports command line parameters now.
1 parent 4d7e64e commit f62e47d

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

stackcollapse-jstack.pl

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,39 @@
5757

5858
use strict;
5959

60+
use Getopt::Long;
61+
62+
# tunables
63+
my $include_tname = 1; # include thread names in stacks
64+
my $include_tid = 0; # include thread IDs in stacks
65+
my $shorten_pkgs = 0; # shorten package names
66+
my $help = 0;
67+
68+
sub usage {
69+
die <<USAGE_END;
70+
USAGE: $0 [options] infile > outfile\n
71+
--include-tname
72+
--no-include-tname # include/omit thread names in stacks (default: include)
73+
--include-tid
74+
--no-include-tid # include/omit thread IDs in stacks (default: omit)
75+
--shorten-pkgs
76+
--no-shorten-pkgs # (don't) shorten package names (default: don't shorten)
77+
78+
eg,
79+
$0 --no-include-tname stacks.txt > collapsed.txt
80+
USAGE_END
81+
}
82+
83+
GetOptions(
84+
'include-tname!' => \$include_tname,
85+
'include-tid!' => \$include_tid,
86+
'shorten-pkgs!' => \$shorten_pkgs,
87+
'help' => \$help,
88+
) or usage();
89+
$help && usage();
90+
91+
92+
# internals
6093
my %collapsed;
6194

6295
sub remember_stack {
@@ -65,10 +98,7 @@ sub remember_stack {
6598
}
6699

67100
my @stack;
68-
my $pname;
69-
my $include_pname = 1; # include process names in stacks
70-
my $include_tid = 0; # include thread IDs in stacks
71-
my $shorten_pkgs = 0; # shorten package names
101+
my $tname;
72102
my $state = "?";
73103

74104
foreach (<>) {
@@ -80,11 +110,11 @@ sub remember_stack {
80110
goto clear if $state ne "RUNNABLE";
81111

82112
# save stack
83-
if (defined $pname) { unshift @stack, $pname; }
113+
if (defined $tname) { unshift @stack, $tname; }
84114
remember_stack(join(";", @stack), 1) if @stack;
85115
clear:
86116
undef @stack;
87-
undef $pname;
117+
undef $tname;
88118
$state = "?";
89119
next;
90120
}
@@ -98,10 +128,10 @@ sub remember_stack {
98128
if (/^"([^"]*)/) {
99129
my $name = $1;
100130

101-
if ($include_pname) {
102-
$pname = $name;
131+
if ($include_tname) {
132+
$tname = $name;
103133
unless ($include_tid) {
104-
$pname =~ s/-\d+$//;
134+
$tname =~ s/-\d+$//;
105135
}
106136
}
107137

0 commit comments

Comments
 (0)