关于 perf相关内容,抓取命令较多,当需要大量数据时每次输入命令会比较麻烦。
perf与火焰图使用介绍_perf 火焰图_cocoti的博客-CSDN博客
根据一些流程,我们可以将个命令合入到一个脚本中,进行一次性抓取perf
target_ip=10.8.104.xx
target_port=22
target_name=arvin
target_workspace="~/perf"
local_workspace=$(pwd)
target_process=0
running_time_s=30
outputfile="out"
设置目标IP,端口,用户名即可远程抓取perf
支持功能如下:
自动在远端Target上判断是否有可用perf,没有本地上传
自定义远端Target上工作目录
可抓取所有进程,或单进程名字 ./auto-perf.sh xxx
自定义抓取时间(秒) ./auto-perf.sh xxx yyy
抓取完后自动调用浏览器查看数据
将压缩包解压到ubuntu系统,或服务器上(服务器不支持图形化,解析完后无法自动打开perf图)
配置好远端的工作目录,及相关ip、端口、用户名
./autoperf.sh 运行。根据提示输入远端密码,由于要配置一些环境,会有多次输入
完成后会在out目录中生成相应的perf数据以及svg可视化文件下载即可分析
暂时无法在飞书文档外展示此内容
脚本源码如下:
#!/bin/sh
target_ip=10.8.104.xx
target_port=22
target_name=arvin
target_workspace="~/perf"
local_workspace=$(pwd)
target_process=0
running_time_s=30
outputfile="out"echo "current local workspace:\033[34m$local_workspace\033[0m"
mkdir -p $local_workspace
if [ $# -le 2 ]; thenfor arg in $* doif [ -n "$(echo $arg| sed -n "/^[0-9]\+$/p")" ]; thenrunning_time_s=$argelsetarget_process=$argfidone
elseecho "Usage:"echo " ./autoperf.sh [process_name] [capture_time]"exit
fiecho "target_ip:\033[35m$target_ip\033[0m"
echo "target_port:\033[35m$target_port\033[0m"
echo "target_name:\033[35m$target_name\033[0m"
echo "target_process:\033[32m$target_process\033[0m"
echo "time:\033[32m$running_time_s(S)\033[0m"flamegraph_path=$local_workspace"/FlameGraph"
if [ ! -d $flamegraph_path ];thenecho "Install FlameGraph..."mkdir -p $flamegraph_pathunzip FlameGraph-master.zip -d $flamegraph_path > /dev/null 2>&1if [ $? -ne 0 ]; thenecho "\033[31mInstall FlameGraph failed.\033[0m"exitfi
fi# upload the ssh-key to target
if [ ! -f "$HOME/.ssh/id_rsa.pub" ];thenecho "Install ssh key..."ssh-keygen -t rsa
fi
echo "target_ip:\033[33mUpdate ssh key to target_ip:${target_ip} ...\033[0m"
ssh-copy-id -i ~/.ssh/id_rsa.pub -p ${target_port} ${target_name}@${target_ip}ssh -p ${target_port} ${target_name}@${target_ip} "mkdir -p $target_workspace"# upload the perf to target, if not the perf
target_return=$(ssh -p ${target_port} ${target_name}@${target_ip} "which perf" | tr -d "\r\n")
if [ "$target_return" = "" ];thenecho "target_ip:\033[33mNo perf on target, push a new one, make sure the perf is OK...\033[0m"target_return=$(ssh -p ${target_port} ${target_name}@${target_ip} "sudo -S uname --m" | tr -d "\r\n")echo "current operator system:\033[35m${target_return}\033[0m"if [ "$target_return" = "x86_64" ] || [ "$target_return" = "aarch64" ];thenscp -r -P ${target_port} $target_return/perf ${target_name}@${target_ip}:${target_workspace}ssh -p ${target_port} ${target_name}@${target_ip} "chmod 777 ${target_workspace}/perf; sudo -S cp ${target_workspace}/perf /usr/bin; which perf"if [ $? -ne 0 ]; thenecho "\033[31mPush perf into the target failed.\033[0m"exitfielseecho "\033[31mNot have the perf tool for ${target_return}\033[0m"exitfi
fi# str_grep="command not found"
# target_return=$(ssh -p ${target_port} ${target_name}@${target_ip} "perf" | tr -d "\r\n")
# result=$(echo $target_return | grep "${str_grep}")
# if [ "$result" != "" ]; then
# echo "target_ip:\033[33mNo perf on target, push a new one, make sure the perf is OK...\033[0m"
# scp -r -P ${target_port} .ws/tools/perf ${target_name}@${target_ip}:/usr/bin/
# ssh -p ${target_port} ${target_name}@${target_ip} "chmod 777 /usr/bin/perf; which perf"
# if [ $? -ne 0 ]; then
# echo "\033[31mPush perf into the target failed.\033[0m"
# exit
# fi
# target_return=$(ssh -p ${target_port} ${target_name}@${target_ip} "perf" | tr -d "\r\n")
# if echo "$target_return" | grep -q "command not found"; then
# cho "\033[31mThe perf is not support for target.\033[0m"
# exit
# fi
# fiflamegraph=$flamegraph_path"/FlameGraph-master"
echo "Capturing perf data, just wait for $running_time_s seconds..."
# Run perf
if [ "$target_process" != "0" ]; thenpid=$(ssh -p ${target_port} ${target_name}@${target_ip} "ps -aux | grep $target_process" | awk 'NR==1 {print $2}' | tr -d "\r\n")echo "Will capture the pid : \033[32m"$pid"\033[0m"if [ "$pid" = "" ]; thenecho "\033[31mNo process is found for $target_process.\033[0m"exitfiif [ -n "$(echo $pid | sed -n "/^[0-9]\+$/p")" ]; thencontinueelseecho "\033[31mGet the pid of $target_process failed, a non-numeric is got.\033[0m"exitfiecho "sudo -S perf record -F 99 -p $pid --call-graph=dwarf --proc-map-timeout 10000 -o $target_workspace/perf.data -- sleep $running_time_s"ssh -p ${target_port} ${target_name}@${target_ip} "sudo -S perf record -F 99 -p $pid --call-graph=dwarf --proc-map-timeout 10000 -o $target_workspace/perf.data -- sleep $running_time_s"outputfile=$target_process
elsessh -p ${target_port} ${target_name}@${target_ip} "sudo -S perf record -F 99 -a --call-graph=dwarf --proc-map-timeout 10000 -o $target_workspace/perf.data -- sleep $running_time_s"
fi# Dump perf data
cur_datetime="`date +%Y%m%d%H%M%S`"
outputfile=$outputfile".$cur_datetime.perf"
echo $target_workspace$outputfile
ssh -p ${target_port} ${target_name}@${target_ip} "sudo -S perf script -i $target_workspace/perf.data > $target_workspace/$outputfile"
if [ ! -d "output" ];thenmkdir output
fi
cd output
scp -r -P ${target_port} ${target_name}@${target_ip}:$target_workspace/$outputfile .
$local_workspace/FlameGraph/FlameGraph-master/stackcollapse-perf.pl $outputfile | $local_workspace/FlameGraph/FlameGraph-master/flamegraph.pl > $outputfile".svg"
ssh -p ${target_port} ${target_name}@${target_ip} "rm $target_workspace -rf"# Auto show the svg
which google-chrome > /dev/null
if [ $? -eq 0 ]; thengoogle-chrome file:///`pwd`/$outputfile".svg" &
elsewhich firefoxif [ $? -eq 0 ]; thenfirefox file:///`pwd`/$outputfile".svg" &fi
fi
cd -