5757
5858use 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
6093my %collapsed ;
6194
6295sub remember_stack {
@@ -65,10 +98,7 @@ sub remember_stack {
6598}
6699
67100my @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 ;
72102my $state = " ?" ;
73103
74104foreach (<>) {
@@ -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 ;
85115clear:
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