Skip to content

Commit a689b1c

Browse files
committed
Flamegraph bash script
It could be invocated from Cmake or as a standalone.
1 parent 3c46195 commit a689b1c

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

rootbench-scripts/flamegraph.sh

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#!/bin/bash
2+
3+
4+
BENCHMARKPATTERN="*benchmark*"
5+
ROOTBENCHBUILDDIR=$1 #/home/lharutyu/ROOT/rootbench-build2 # Enter path to rootbench-build folder
6+
shift
7+
FLAMEGRAPHBASEDIR=$ROOTBENCHBUILDDIR/FlameGraph
8+
9+
MKDIR=/bin/mkdir
10+
BASENAME=/usr/bin/basename
11+
DIRNAME=/usr/bin/dirname
12+
FIND=/usr/bin/find
13+
SED=/bin/sed
14+
PERF=/usr/bin/perf
15+
STACKCOLLASE=stackcollapse-perf.pl
16+
FLAMEGRAPG=flamegraph.pl
17+
18+
19+
usage()
20+
{
21+
echo
22+
echo "--------------------------FlameGraph Generator---------------------------"
23+
echo
24+
echo "Usage: $0 [OPTION]..."
25+
echo "This script generates FlameGraphs for benchmarks !!!"
26+
echo " Enter path/to/rootbench/build/dir"
27+
echo "OPTIONS"
28+
echo " -b, --benchmarkfile path Location of ROOT benchmark file"
29+
echo " -a, --all Create all benchmarks."
30+
echo " -h, --help Display this help and exit"
31+
}
32+
33+
usage_short()
34+
{
35+
echo "FlameGraph Generator"
36+
echo "Usage: $0 [path/to/rootbench/build/dir] [-b |--benchmarkfile filepath] [-a | --all] [-h | --help]"
37+
}
38+
39+
get_bm_fn()
40+
{
41+
if [ ! -f $1 ] ; then
42+
echo "Can't find the executable"
43+
exit 1
44+
else
45+
bm_fn_full=$1
46+
bm_fn=`$BASENAME $bm_fn_full`
47+
fi
48+
}
49+
50+
get_bm_fn_interactive()
51+
{
52+
read -p "Enter benchmark filename: " bm_fn_full
53+
if [ -z $bm_fn_full ] ; then
54+
echo "You did not enter any filename. Exiting..."
55+
exit 1
56+
fi
57+
get_bm_fn $bm_fn_full
58+
}
59+
60+
61+
62+
perf_record()
63+
{
64+
$PERF record -F 50 --call-graph dwarf $1 --benchmark_filter=$2
65+
}
66+
67+
perf_script()
68+
{
69+
$PERF script | stackcollapse-perf.pl | flamegraph.pl > $1.svg
70+
}
71+
72+
perf_rec_scr()
73+
{
74+
perf_record $1 $2
75+
perf_script $3
76+
# $PERF record --call-graph dwarf $1 | $PERF script | $STACKCOLLASE | $FLAMEGRAPG > $2
77+
}
78+
79+
80+
get_bm_files()
81+
{
82+
bm_file_list=`$FIND $ROOTBENCHBUILDDIR/root -iname "$BENCHMARKPATTERN" |grep -v "CMakeFiles"`
83+
84+
}
85+
86+
run_all_bm()
87+
{
88+
get_bm_files
89+
for bm_fn_full in $bm_file_list
90+
do
91+
bm_fn=`$BASENAME $bm_fn_full`
92+
bm_dn=`$DIRNAME $bm_fn_full`
93+
bm_path=`echo "$bm_dn" | $SED -n "s|^$ROOTBENCHBUILDDIR/||p"`
94+
bm_list_exec=`./$bm_path/$bm_fn --benchmark_list_tests`
95+
outputdir_full=${FLAMEGRAPHBASEDIR}/$bm_path/$bm_fn
96+
$MKDIR -p $outputdir_full
97+
if [ $? -ne 0 ]; then
98+
echo "Can't create directory $1. Exiting..."
99+
exit 1
100+
fi
101+
for benchmark in $bm_list_exec
102+
do
103+
perf_rec_scr $bm_fn_full $benchmark ${outputdir_full}/${benchmark}_FlameGraph
104+
done
105+
done
106+
}
107+
108+
109+
run_spec_bm()
110+
{
111+
spec_bm_list_exec=`$ROOTBENCHBUILDDIR/$bm_fn_full --benchmark_list_tests`
112+
spec_outputdir_full=$ROOTBENCHBUILDDIR/$bm_fn
113+
$MKDIR $spec_outputdir_full
114+
if [ $? -ne 0 ]; then
115+
echo "Can't create directory $1. Exiting..."
116+
exit 1
117+
fi
118+
for benchmark in $spec_bm_list_exec
119+
do
120+
perf_rec_scr $ROOTBENCHBUILDDIR/$bm_fn_full $benchmark ${spec_outputdir_full}/${benchmark}_FlameGraph
121+
done
122+
}
123+
124+
125+
##### Main #####
126+
127+
128+
if [ \( "$ROOTBENCHBUILDDIR" = "-h" \) -o \( "$ROOTBENCHBUILDDIR" = "--help" \) ] ; then
129+
usage
130+
exit 1
131+
fi
132+
133+
if [ ! -d $ROOTBENCHBUILDDIR ] ; then
134+
echo "Can't find the build directory. Exiting..."
135+
exit 1
136+
fi
137+
138+
[ $# -eq 0 ] && usage_short
139+
140+
while [ $# -gt 0 ]; do
141+
case "$1" in
142+
-b | --benchmarkfile )
143+
shift
144+
get_bm_fn $1
145+
;;
146+
-a | --all )
147+
all=y
148+
;;
149+
-h | --help )
150+
usage
151+
exit
152+
;;
153+
* )
154+
usage
155+
exit 1
156+
;;
157+
esac
158+
shift
159+
done
160+
161+
if [ "$all" = "y" ] ; then
162+
run_all_bm
163+
fi
164+
165+
### If no "-b" option, we go with interactive mode. ###
166+
# if [ -z $bm_fn_full ] ; then
167+
# get_bm_fn_interactive
168+
# fi
169+
170+
if [ ! -z $bm_fn_full ] ; then
171+
run_spec_bm
172+
fi
173+
174+
exit $?
175+
176+
177+
178+
179+
180+
181+
182+
183+

0 commit comments

Comments
 (0)