Skip to content

Instantly share code, notes, and snippets.

@potix2
Last active December 14, 2015 16:18
Show Gist options
  • Save potix2/5113777 to your computer and use it in GitHub Desktop.
Save potix2/5113777 to your computer and use it in GitHub Desktop.
スレッドダンプからRUNNABLEなものだけ抽出する
#!/usr/bin/perl -w
#usage: perl td2json.pl < jvm-thread-dump.log
use strict;
use warnings;
use JSON;
my $buf = '';
my $row = 0;
my $thread_info = {};
my @threads = ();
while(<>) {
s/\r//;
s/\n//;
$buf .= $_ . "\n";
$row++;
if( $_ eq '') {
$thread_info->{ALL} = $buf;
push @threads, $thread_info;
$buf = '';
$thread_info = {};
$row = 0;
}
elsif ( $row == 1 ) {
tr/ //;
$thread_info->{THREAD_SUMMARY} = $_;
}
elsif ( $row == 2 ) {
$thread_info->{THREAD_STATE} = $1 if (m/java\.lang\.Thread\.State: ([A-Z]+)/);
}
elsif ( $row == 3 ) {
$thread_info->{STACK_TOP} = $1 if (m/^\s+at\s([a-zA-Z0-9_.\-]+)/);
}
}
print encode_json(\@threads);
#!/usr/bin/perl -w
use strict;
use warnings;
use JSON;
#available keys: STACK_TOP, THREAD_SUMMARY, THREAD_STATE, ALL
my $filter_key = "THREAD_STATE";
#avaiable state: RUNNABLE, WAITING, BLOCKED
my $filter_value = "RUNNABLE";
my $print_key = "STACK_TOP";
my $buf = '';
while(<>) {
$buf .= $_;
}
map {
print $_->{$print_key} . "\n";
}
grep {
$_->{$filter_key} =~ m/$filter_value/;
} @{decode_json($buf)};
$perl td2json.pl < thread-dump.log | perl td_filter.pl | sort
@chokkoyamada
Copy link

perlキタ━━━━(゚∀゚)━━━━!!

@amasho
Copy link

amasho commented Mar 8, 2013

やはり頼れるPerl

@hatone
Copy link

hatone commented Mar 8, 2013

perl(・∀・)イイ!!

@kazshu
Copy link

kazshu commented Mar 8, 2013

こう、さっとジョブチェンジできるのがうらやましす

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment