#ubuntu# #perf# 一键远程自动抓取perf火焰图
创始人
2025-06-01 02:57:33

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

相关内容

热门资讯

舆论战升级!巨子生物深夜回应,... 2025.06.02本文字数:2490,阅读时长大约4分钟作者 |第一财经 刘晓颖重组胶原蛋白成分之...
财经时评|以创新厚度重塑汽车产... 作者 远山中国汽车工业协会与工业和信息化部近期针对行业“内卷式”竞争的联合发声,为持续蔓延的价格战按...
恒生指数午盘下跌2.20%,恒... 6月2日午盘,香港恒生指数下跌2.20%,报22778.45点;恒生科技指数下跌2.43%,报504...
“以旧换新”带货1万亿,中国何... “美国想让制造业回流成为中国的样子,一个踏实劳作的‘生产者社会’;而中国想努力扩大消费成为美国的样子...
恒指半日跌2.2% 恒指半日跌... 【恒指半日跌2.2%】截至午间收盘,香港恒生指数下跌2.20%,报22778.45点;恒生科技指数下...
“成分之争”舆论战升级,巨子生... 重组胶原蛋白成分之争的舆论战再度升级。美妆博主 "大嘴博士" (郝宇)近日连续发文质疑,巨子生物(0...
汇川技术新注册《InoCube... 证券之星消息,近日汇川技术(300124)新注册了《InoCube-InoData数据分析系统V1....
博将控股多家所投企业荣登202... 博将控股多家所投企业荣登2025杭州独角兽与准独角兽榜单 2025年4月24日,由民建浙江省委会、浙...
刚刚,A50直线跳水!亚太股市... 6月2日,亚太股市开盘后持续走弱。富时中国A50期货开盘跌0.6%,截至目前跌幅1.91%。 截至...
问界、智界、享界、尊界,202... 2025鸿蒙智行:四界表现鸿蒙智行作为国内造车新势力的主流品牌,一共规划了“五界”车型,包括:问界、...
花样年控股:重组支持协议的最后... 6月1日,花样年控股(01777.HK)公告称,公司2024年4月29日所订立的重组支持协议的最终截...
蜜雪集团股价创上市以来新高 蜜... 新京报贝壳财经讯 6月2日,港股蜜雪集团盘中涨超7%,股价刷新上市新高。
美单边关税让全球经济面临更大不... 美国征收关税的对象和标准可能随意变更,其关税政策具有不可预测性。无论是外国企业,还是美国本土企业,都...
桃李面包创始人向其两儿子转让3... 5月30日晚间,桃李面包(沪市代码:603866)公告称,公司控股股东及实际控制人吴志刚通过大宗交易...
前4月东莞重大项目完成投资42... 本期看点:前4月东莞重大项目完成投资429.09亿元;长联科技募投项目提升年产至2.77万吨;广东省...
恒生指数跌幅扩大至2%,医药、... 6月2日,恒生指数跌幅扩大至2%,医药、地产、能源板块跌幅居前,美中嘉和跌超14%,石四药集团跌近1...
港股、A50飘绿,亚太市场多数... 早间,亚太市场多数下跌。港股、A50集体下跌其中,恒生指数、恒生科技指数开盘跌幅扩大, 港股生物技术...
雷军看好的两兄弟,要IPO了 ... 2021年夏,小米产业园办公室内,雷军饶有兴致地打量眼前一对兄弟,“为什么张波是创始人,董事长却是张...
圣阳股份涨1.66%,成交额9... 5月30日,圣阳股份涨1.66%,成交额9.82亿元,换手率15.03%,总市值66.67亿元。 异...
“龙舟溪游”点燃江南西商圈消费... 5月31日至6月2日,海珠区江南中街道一年一度的节假日消费体验活动如约而至。今年“龙舟溪游・与你童在...
恒生指数开盘跌1.06%,恒生... 6月2日,恒生指数开盘跌1.06%报23043.10点,恒生科技指数跌1.33%,恒生中国企业指数跌...
*ST天喻实控人被刑事立案 此... 5月30日,*ST天喻(300205.SZ)发布公告称,公司收到武汉市公安局送达的《立案告知书》,公...
“玩”出更多花样——“六一”礼... 能交流、讲故事的毛绒玩偶,融入中国文化、科技元素的拼插积木,电影《哪吒2》衍生开发的各类公仔……“六...
5月十大牛股出炉:中邮科技逾1... 截至5月30日收盘,沪指月内累计涨2.09%,深证成指累计涨1.42%,创业板指累计涨2.32%。在...
恒生指数止步周线七连阳,IPO... 南方财经全媒体记者 袁思杰 实习生武桐羽 香港报道上周(5月26日-5月30日),港股震荡回调,主要...
新势力车企5月放榜:零跑汽车登... 近期,国内多家主流自主汽车企业纷纷发布了5月份的销售数据。造车新势力5月交付成绩也出炉,第一名依然是...
最新!2025新势力5月销量出... 5月新势力表现如何?2025年5月的销售周期刚刚过去,不少造车新势力厂商的销量数据,已经新鲜出炉。数...
两家A股公司,收终止上市决定 又有两家A股上市公司收到股票终止上市决定,6月10日进入退市整理期。 上述自律监管决定书指出,因2...
重磅,事关教育强国,主力提前埋... 数据是个宝数据宝投资少烦恼这些产业的景气度处于上升期。《求是》杂志发表文章《加快建设教育强国》6月1...