summaryrefslogtreecommitdiff
path: root/tools/run_flamegrah.sh
blob: 6bf1b129cfa93392db372b7f29e5af52f5216429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash

usage()
{
	printf "Usage: $0 [opt] [val]\n\n"
	printf "          -c cpuid       Collect data of the specified cpu id\n"
	printf "          -t threadid    Collect data of the specified thread id\n"
	printf "          -p processid   Collect data of the specified process id\n"
	printf "          -h             Help\e[0m\n\n"
	exit 0
}

if [ ! -n "$1" ] || [ ! -n "$2" ]; then
	usage
fi

opt_type="h"
while getopts c:t:p:h opt
do
	case $opt in
	c) opt_type="c" ;;
	t) opt_type="t" ;;
	p) opt_type="p" ;;
	h|?) usage ;;
	esac
done

# 执行后在当前目录下会生成采样数据 perf.data
rm -rf perf.data

if [ $opt_type == "c" ]; then
	printf "\e[32m Collect data on cpu $2\e[0m\n"
	perf record -e cpu-clock --call-graph dwarf -C $2 -- sleep 5
elif [ $opt_type == "t" ]; then
	printf "\e[32m Collect data on thread $2\e[0m\n"
	perf record -e cpu-clock --call-graph dwarf -t $2 -- sleep 5
elif [ $opt_type == "p" ]; then
	printf "\e[32m Collect data on process $2\e[0m\n"
	perf record -e cpu-clock --call-graph dwarf -p $2 -- sleep 5
else
	usage
fi

if [ ! -f "perf.data" ]; then
	echo "Please Collect data !"
	exit 0
fi

if [ ! -d FlameGraph ]; then
	echo "git clone https://github.com/brendangregg/FlameGraph.git"
	git clone https://github.com/brendangregg/FlameGraph.git
fi

rm -rf perf.unfold
rm -rf perf.folded
rm -rf perf.svg

printf "\e[32m Convert data to flame graph\e[0m\n"

perf script | ./FlameGraph/stackcollapse-perf.pl | ./FlameGraph/flamegraph.pl >perf.svg

# 使用浏览器查看 perf.svg, 在浏览器中使用 Ctrl + F 进行关键字搜索