#ubuntu# #perf# 一键远程自动抓取perf火焰图

关于 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

支持功能如下:

  1. 自动在远端Target上判断是否有可用perf,没有本地上传

  2. 自定义远端Target上工作目录

  3. 可抓取所有进程,或单进程名字 ./auto-perf.sh xxx

  4. 自定义抓取时间(秒) ./auto-perf.sh xxx yyy

  5. 抓取完后自动调用浏览器查看数据

使用方法

将压缩包解压到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 ]; then
	for arg in $*                     
	do
		if [ -n "$(echo $arg| sed -n "/^[0-9]\+$/p")" ]; then
			running_time_s=$arg
		else
			target_process=$arg
		fi
	done
else
	echo "Usage:"
	echo "    ./autoperf.sh [process_name] [capture_time]"
	exit
fi

echo "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 ];then
	echo "Install FlameGraph..."
	mkdir -p $flamegraph_path
	unzip FlameGraph-master.zip -d $flamegraph_path > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "\033[31mInstall FlameGraph failed.\033[0m"
		exit
	fi
fi


# upload the ssh-key to target
if  [ ! -f "$HOME/.ssh/id_rsa.pub" ];then
	echo "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" = "" ];then
	echo "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" ];then
		scp -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 ]; then
			echo "\033[31mPush perf into the target failed.\033[0m"
			exit
		fi
	else
		echo "\033[31mNot have the perf tool for ${target_return}\033[0m"
		exit
	fi
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
# fi


flamegraph=$flamegraph_path"/FlameGraph-master"
echo "Capturing perf data, just wait for $running_time_s seconds..."
# Run perf
if [ "$target_process" != "0" ]; then
	pid=$(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" = "" ]; then
		echo "\033[31mNo process is found for $target_process.\033[0m"
		exit
	fi
	if [ -n "$(echo $pid | sed -n "/^[0-9]\+$/p")" ]; then
		continue
	else
		echo "\033[31mGet the pid of $target_process failed, a non-numeric is got.\033[0m"
		exit
	fi
	echo "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
else
	ssh -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" ];then
	mkdir 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 ]; then
	google-chrome file:///`pwd`/$outputfile".svg" &
else
	which firefox
	if [ $? -eq 0 ]; then
		firefox file:///`pwd`/$outputfile".svg" &
	fi
fi
cd -

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值