Skip to content

Commit f0e9f6c

Browse files
committed
Generation of flamegraphs for rootbenchmarks (script)
1 parent a689b1c commit f0e9f6c

File tree

2 files changed

+59
-86
lines changed

2 files changed

+59
-86
lines changed

cmake/modules/AddRootBench.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function(RB_ADD_GBENCHMARK benchmark)
2020
set(postcmd POSTCMD ${ARG_POSTCMD})
2121
endif()
2222
if(flamegraph)
23-
set(postcmd ${postcmd} "${PROJECT_SOURCE_DIR}/rootbench-scripts/flamegraph.sh")
23+
set(postcmd ${postcmd} "${PROJECT_SOURCE_DIR}/rootbench-scripts/flamegraph.sh ${PROJECT_BINARY_DIR} -b ${CMAKE_CURRENT_BINARY_DIR}/${benchmark}")
2424
add_dependencies(${benchmark} flamegraph-download)
2525
endif()
2626
if(${ARG_LABEL} STREQUAL "long")

rootbench-scripts/flamegraph.sh

Lines changed: 58 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
#!/bin/bash
22

3-
43
BENCHMARKPATTERN="*benchmark*"
5-
ROOTBENCHBUILDDIR=$1 #/home/lharutyu/ROOT/rootbench-build2 # Enter path to rootbench-build folder
6-
shift
7-
FLAMEGRAPHBASEDIR=$ROOTBENCHBUILDDIR/FlameGraph
8-
94
MKDIR=/bin/mkdir
105
BASENAME=/usr/bin/basename
116
DIRNAME=/usr/bin/dirname
127
FIND=/usr/bin/find
138
SED=/bin/sed
149
PERF=/usr/bin/perf
15-
STACKCOLLASE=stackcollapse-perf.pl
16-
FLAMEGRAPG=flamegraph.pl
17-
10+
STACKCOLLASE=stackcollapse-perf.pl
11+
FLAMEGRAPG=flamegraph.pl
1812

1913
usage()
2014
{
@@ -23,30 +17,42 @@ usage()
2317
echo
2418
echo "Usage: $0 [OPTION]..."
2519
echo "This script generates FlameGraphs for benchmarks !!!"
26-
echo " Enter path/to/rootbench/build/dir"
2720
echo "OPTIONS"
2821
echo " -b, --benchmarkfile path Location of ROOT benchmark file"
2922
echo " -a, --all Create all benchmarks."
23+
echo " -d, --builddir path Create all benchmarks."
3024
echo " -h, --help Display this help and exit"
3125
}
3226

3327
usage_short()
3428
{
35-
echo "FlameGraph Generator"
36-
echo "Usage: $0 [path/to/rootbench/build/dir] [-b |--benchmarkfile filepath] [-a | --all] [-h | --help]"
29+
# echo "FlameGraph Generator"
30+
echo "Usage: $0 -d | --builddir path [-b | --benchmarkfile filepath] [-a | --all] [-h | --help]"
31+
exit 1
3732
}
3833

3934
get_bm_fn()
4035
{
4136
if [ ! -f $1 ] ; then
42-
echo "Can't find the executable"
37+
echo "Can't find the benchmark file"
4338
exit 1
4439
else
4540
bm_fn_full=$1
4641
bm_fn=`$BASENAME $bm_fn_full`
4742
fi
4843
}
4944

45+
get_build_dir()
46+
{
47+
if [ ! -d $1 ] ; then
48+
echo "Can't find the build directory. Exiting..."
49+
exit 1
50+
else
51+
build_dir=$1
52+
flamegraph_base_dir=$build_dir/FlameGraph
53+
fi
54+
}
55+
5056
get_bm_fn_interactive()
5157
{
5258
read -p "Enter benchmark filename: " bm_fn_full
@@ -57,84 +63,53 @@ get_bm_fn_interactive()
5763
get_bm_fn $bm_fn_full
5864
}
5965

60-
61-
6266
perf_record()
6367
{
64-
$PERF record -F 50 --call-graph dwarf $1 --benchmark_filter=$2
68+
$PERF record -F 50 --call-graph dwarf $1 --benchmark_filter=${2}$
6569
}
6670

6771
perf_script()
6872
{
69-
$PERF script | stackcollapse-perf.pl | flamegraph.pl > $1.svg
73+
$PERF script | stackcollapse-perf.pl | flamegraph.pl --title $1 > $2.svg
7074
}
7175

72-
perf_rec_scr()
76+
perf_rec_scr()
7377
{
74-
perf_record $1 $2
75-
perf_script $3
76-
# $PERF record --call-graph dwarf $1 | $PERF script | $STACKCOLLASE | $FLAMEGRAPG > $2
78+
perf_record $1 $2
79+
perf_script $2 $3
80+
# $PERF record --call-graph dwarf $1 | $PERF script | $STACKCOLLASE | $FLAMEGRAPG > $2
7781
}
7882

79-
8083
get_bm_files()
8184
{
82-
bm_file_list=`$FIND $ROOTBENCHBUILDDIR/root -iname "$BENCHMARKPATTERN" |grep -v "CMakeFiles"`
83-
85+
bm_file_list=`$FIND $build_dir/root -iname "$BENCHMARKPATTERN" |grep -v "CMakeFiles"`
8486
}
8587

86-
run_all_bm()
88+
run_bm()
8789
{
88-
get_bm_files
89-
for bm_fn_full in $bm_file_list
90+
for bm_fn_full in $bm_file_list
91+
do
92+
bm_fn=`$BASENAME $bm_fn_full`
93+
bm_dn=`$DIRNAME $bm_fn_full`
94+
bm_path=`echo "$bm_dn" | $SED -n "s|^$build_dir/||p"`
95+
bm_sub_list=`$bm_fn_full --benchmark_list_tests`
96+
outputdir_full=${flamegraph_base_dir}/$bm_path/$bm_fn
97+
$MKDIR -p $outputdir_full
98+
if [ $? -ne 0 ]; then
99+
echo "Can't create directory $1. Exiting..."
100+
exit 1
101+
fi
102+
for bm in $bm_sub_list
90103
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
104+
bm_modified_fn=`echo "$bm" | $SED "s|[/:]|-|g"` #replacing all "/" and ":" with "-"
105+
perf_rec_scr $bm_fn_full $bm ${outputdir_full}/${bm_modified_fn}_FlameGraph
105106
done
107+
done
106108
}
107109

108110

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-
125111
##### Main #####
126112

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-
138113
[ $# -eq 0 ] && usage_short
139114

140115
while [ $# -gt 0 ]; do
@@ -143,6 +118,11 @@ while [ $# -gt 0 ]; do
143118
shift
144119
get_bm_fn $1
145120
;;
121+
-d | --builddir )
122+
shift
123+
get_build_dir $1
124+
;;
125+
146126
-a | --all )
147127
all=y
148128
;;
@@ -158,26 +138,19 @@ while [ $# -gt 0 ]; do
158138
shift
159139
done
160140

161-
if [ "$all" = "y" ] ; then
162-
run_all_bm
141+
if [ -z $build_dir ] ; then
142+
echo "************* Rootbench Build dir is mandatory *****************"
143+
usage_short
163144
fi
164145

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
146+
if [ "$all" = "y" ] ; then
147+
get_bm_files
148+
run_bm
149+
else
150+
if [ ! -z $bm_fn_full ] ; then
151+
bm_file_list=$bm_fn_full
152+
run_bm
153+
fi
172154
fi
173155

174156
exit $?
175-
176-
177-
178-
179-
180-
181-
182-
183-

0 commit comments

Comments
 (0)