java抓火焰图

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值