java抓火焰图方法
wget https://github.com/brendangregg/FlameGraph/archive/v1.0.zip
unzip v1.0.zip
mv FlameGraph-1.0/ FlameGraph
touch flame-gen.sh flame-svg-gen.sh
./flame-gen.sh 21800
flame-gen.sh
#!/bin/sh
TMPDIR=./tmp_flame
rm -rf $TMPDIR 2> /dev/null
mkdir $TMPDIR 2> /dev/null
trap ctrl_c INT
profiling=""
function ctrl_c() {
if [ "$profiling" != "" ]
then
sh ./flame-svg-gen.sh $TMPTRACE
fi
exit
}
TMPTRACE=./tmp_flame/trace.txt
rm $TMPTRACE 2> /dev/null
pid=""
if [ "$1" == "" ]
then
echo "waiting for java process..."
while [ "$pid" == "" ]
do
#For Windows with Cygwin use this
#pid=`ps -W | grep java | grep -o [0-9]* | head -1`
pid=`ps | grep java | grep -v grep | grep -o [0-9]* | head -1`
if [ "$pid" != "" ]
then
break
fi
done
else
pid=$1;
fi
#For Windows with Cygwin use this
#processExists=`ps -pW $pid | grep $pid`
processExists=`ps -p $pid | grep $pid`
if [ "$processExists" != "" ]
then
echo "process found pid: $pid"
echo "profiling started, press ctrl+c to end and generate report"
profiling="true"
while true;
do
jstack $pid >> $TMPTRACE && sleep 0.001;
done
sh ./flame-svg-gen.sh $TMPTRACE
else
echo "process with id $pid doesn't exists"
fi
#echo "Done! please check Flame.svg"
flame-svg-gen.sh
#!/bin/sh
echo "wait generating report..."
TMPDIR=./tmp_flame
TMPSTACKS=$TMPDIR/flamegraph-stacks-collapsed.txt
TMPPALETTE=$TMPDIR/flamegraph-palette.map
perl ./FlameGraph/stackcollapse-jstack.pl $1 > $TMPSTACKS
# 1st run - hot: default
if [ "$2" == "" ]
then
perl ./FlameGraph/flamegraph.pl --cp $TMPSTACKS > stacks.html
else
grep $2 $TMPSTACKS | perl ./FlameGraph/flamegraph.pl --cp > stacks.html
fi
# 2nd run - blue: I/O
mv palette.map $TMPPALETTE
cat $TMPPALETTE | grep -v '\.read' | grep -v '\.write' | grep -v 'socketRead' | grep -v 'socketWrite' | grep -v 'socketAccept' > palette.map
if [ "$2" == "" ]
then
perl ./FlameGraph/flamegraph.pl --cp --colors=io $TMPSTACKS > flame.html
else
grep $2 $TMPSTACKS | perl ./FlameGraph/flamegraph.pl --cp --colors=io > flame.html
fi
#echo "done!"
#For winodws with cygwin use this
#./flame.html
open ./flame.html