This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runone_profiling.sh
executable file
·193 lines (155 loc) · 5.54 KB
/
runone_profiling.sh
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
#
# runone.sh
# Run a single query with profiling
#
# This file is part of the AXLE Healthcare Benchmark.
#
# Copyright (c) 2013, Portavita BV Netherlands
#
set -e
BASEDIR="$(pwd)/database"
# ingest part of the default_settings include makefile
sed -e 's/(/{/g' -e 's/)/}/g' ./default_settings | sed '/shell/d' | sed -n '/^define/,$!p' > /tmp/default_settings_bash
source /tmp/default_settings_bash
PGBINDIR=${PGSERVER}/bin
PGDATADIR=${PGDATA}
PGPORT=${LAKEPORT}
PGUSER=${USER}
SUDO=/usr/bin/sudo
usage() {
echo "USAGE: $0 <QUERY> <DWHDB> <PERFDATADIR>"
echo "e.g. $0 1 lake perfresults"
exit 1
}
fail() {
echo $1
exit 1
}
#
# Set up perf
#
perf_set_kernel_params() {
if [ -r /proc/sys/kernel/kptr_restrict ] && [ $(cat /proc/sys/kernel/kptr_restrict) -ne 0 ]; then
echo "Perf requires reading kernel symbols."
echo 0 | ${SUDO} tee /proc/sys/kernel/kptr_restrict
fi
if [ -r /proc/sys/kernel/perf_event_paranoid ] && [ $(cat /proc/sys/kernel/perf_event_paranoid) -ne -1 ]; then
echo "Need to enable the reading of performance events."
echo -1 | ${SUDO} tee /proc/sys/kernel/perf_event_paranoid
fi
if [ -r /proc/sys/kernel/perf_event_mlock_kb ] && [ $(cat /proc/sys/kernel/perf_event_mlock_kb) -lt 1024 ]; then
echo "Need to give more memory to perf."
echo 1024 | ${SUDO} tee /proc/sys/kernel/perf_event_mlock_kb
fi
}
# Restart and drop caches
restart_drop_caches() {
echo "Restart postgres and drop caches."
${SUDO} -u $PGUSER $PGBINDIR/pg_ctl stop -D $PGDATADIR
sync && echo 3 | ${SUDO} tee /proc/sys/vm/drop_caches
${SUDO} -u $PGUSER taskset -c 2 $PGBINDIR/postgres -D "$PGDATADIR" -p $PGPORT &
PGPID=$!
while ! ${SUDO} -u $PGUSER $PGBINDIR/pg_ctl status -D $PGDATADIR | grep "server is running" -q; do
echo "Waiting for the Postgres server to start"
sleep 3
done
}
# Calculates elapsed time
timer() {
if [[ $# -eq 0 ]]; then
echo $(date '+%s')
else
local stime=$1
etime=$(date '+%s')
if [[ -z "$stime" ]]; then stime=$etime; fi
dt=$((etime - stime))
ds=$((dt % 60))
dm=$(((dt / 60) % 60))
dh=$((dt / 3600))
printf '%d:%02d:%02d' $dh $dm $ds
fi
}
BASEDIR=$(dirname "$0")
BASEDIR=$(cd "$BASEDIR"; pwd)
if [ $# -lt 3 ]; then
usage
else
QUERY=$(printf "%02d" $1)
PWD=`pwd`
DWHDB=$2;
PERFDATADIR=$3;
fi
test "${PGDATADIR:0:1}" == "/" || PGDATADIR="$PWD/$PGDATADIR"
DB_NAME=${DWHDB}
### Query to be executed
f=`ls queries/${QUERY}_*.sql`
Q=`mktemp`
echo "set search_path to rim2011, public, hl7, hdl, r1;" | cat - "$BASEDIR/$f" > $Q
### From here more or less follows tpch_runone
perf_set_kernel_params
## Start a new instance of Postgres
${SUDO} -u $PGUSER taskset -c 2 $PGBINDIR/postgres -D "$PGDATADIR" -p $PGPORT &
PGPID=$!
while ! ${SUDO} -u $PGUSER $PGBINDIR/pg_ctl status -D $PGDATADIR | grep "server is running" -q; do
echo "Waiting for the Postgres server to start"
sleep 1
done
# wait for it to finish starting
sleep 5
echo "Postgres running"
# Get size
SIZE=`$PGBINDIR/psql -p $PGPORT -d $DB_NAME -tAc "select pg_size_pretty(pg_database_size('${DB_NAME}'));"`
dir="$PERFDATADIR/${QUERY}"
mkdir -p $dir
cd "$dir"
### Execute query with explain analyze to get query plan
#echo "Execute query with explain analyze to get query plan"
#${SUDO} -u $PGUSER $PGBINDIR/psql -h /tmp -p $PGPORT -d $DB_NAME <"$BASEDIR/$fa" > analyze.txt
#restart_drop_caches
### Get execution time without perf
/usr/bin/time -f '%e\n%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k'\
-o exectime.txt \
$PGBINDIR/psql -h /tmp -p $PGPORT -d $DB_NAME < $Q 2> exectime.txt
restart_drop_caches
### Collect data with perf to generate callgraph
echo "Collect data with perf to generate callgraph"
/usr/bin/time -f '%e\n%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k'\
${SUDO} -u $PGUSER perf record -a -C 2 -s -g -m 512 --\
$PGBINDIR/psql -h /tmp -p $PGPORT -d $DB_NAME < $Q 2> exectime_perf.txt
tail -n 2 exectime_perf.txt > exectime_p.txt && mv exectime_p.txt exectime_perf.txt
### Call the query second time and record in perf.data.warm
/usr/bin/time -f '%e\n%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k'\
${SUDO} -u $PGUSER perf record -o perf.data.warm -a -C 2 -s -g -m 512 --\
$PGBINDIR/psql -h /tmp -p $PGPORT -d $DB_NAME < $Q 2> /dev/null
restart_drop_caches
### Collect basic stats with perf
echo "Collect basic stats with perf"
${SUDO} -u $PGUSER perf stat -a -C 2 -B --log-fd 2 --\
$PGBINDIR/psql -h /tmp -p $PGPORT -d $DB_NAME < $Q 2> stats.txt
restart_drop_caches
${SUDO} chown $USER:$USER *
chmod 775 .
cgf="${QUERY}-callgraph"
echo "Creating the call graph: $cgf"
perf script | python "$BASEDIR/gprof2dot.py" -f perf | dot -Tpdf -o "../${cgf}.pdf" &
perf script | python "$BASEDIR/gprof2dot.py" -f perf | dot -Tpng -o "../${cgf}.png" &
fgf="${QUERY}-flamegraph.svg"
fgfw="${QUERY}-flamegraph-warm.svg"
echo "Creating the flame graph: $fgf"
perf script | "$BASEDIR/stackcollapse-perf.pl" | \
"$BASEDIR/flamegraph.pl" --title "Portavita Benchmark ${SIZE} Query ${QUERY} cold" > ../$fgf
perf script -i perf.data.warm | "$BASEDIR/stackcollapse-perf.pl" | \
"$BASEDIR/flamegraph.pl" --title "Portavita Benchmark ${SIZE} Query ${QUERY} warm" > ../$fgfw
echo "<p>Query ${QUERY}</p>" >> ../index.html
###echo "<img width=100% src=\"${cgf}.png\">" >> ../index.html
echo "<img src=\"${fgf}\">" >> ../index.html
echo "<img src=\"${fgfw}\">" >> ../index.html
cd - >/dev/null
# Stop the server
${SUDO} -u $PGUSER $PGBINDIR/pg_ctl stop -D $PGDATADIR
for p in $(jobs -p);
do
wait $p
done
rm $Q