blob: ab02d39ada0c3ec450e05d15d5e4c79568a16db8 [file] [log] [blame]
bartf33ce892008-06-07 11:40:14 +00001#!/bin/bash
2
3########################
4# Function definitions #
5########################
6
7## Print the logarithm base 2 of $1 on stdout.
8function log2 {
9 local i
10
11 for ((i=0;i<64;i++))
12 do
13 if [ $((2**i)) = $1 ]; then
14 echo $i
15 return 0
16 fi
17 done
18 echo error
19 return 1
20}
21
22## Print the size of the level 2 cache in bytes on stdout.
23function get_cache_size {
24 local s
25 s=$(</sys/devices/system/cpu/cpu0/cache/index2/size)
26 if [ "${s%M}" != "$s" ]; then
27 echo $((${s%M}*1024*1024))
28 elif [ "${s%K}" != "$s" ]; then
29 echo $((${s%K}*1024))
30 else
31 echo $s
32 fi
33}
34
35## Read a stream of numbers from stdin (one per line), and print the average
36# and standard deviation.
37function avgstddev {
bart838262f2008-06-18 14:14:03 +000038 awk '{n++;m=NF;for(i=1;i<=NF;i++){sum[i]+=$i;sumsq[i]+=$i*$i}}END{for(i=1;i<=m;i++){d=sumsq[i]/n-sum[i]*sum[i]/n/n;printf "%.2f %.2f ",sum[i]/n,(d>0?sqrt(d):0)}}'
bartee17ad62008-06-18 13:31:05 +000039}
40
41## Query the virtual memory size for the last invocation of command $1 from
42# the information logged by the kernel (BSD process accounting).
43function query_cmd_vsz {
44 if [ ! -e /usr/sbin/dump-acct ]; then
45 echo "Error: userspace tools for BSD process accounting have not been" >&2
46 echo "installed. Please install the acct package (Debian systems)." >&2
47 return 1
48 fi
49
50 /usr/sbin/dump-acct /var/log/account/pacct | \
51 grep -- "^$(basename "$1")" | \
52 cut -f8 -d'|' | \
53 tail -n 1
54}
55
56## Query the virtual memory size for the last invocation of command $1 from
57# the information logged by the kernel (BSD process accounting).
58function query_vsz {
59 local cmd tool
60
61 cmd="$(basename "$1")"
62 if [ "${cmd}" = "vg-in-place" ]; then
63 tool="tool-not-found"
64 for arg in "${@}"
65 do
66 if [ "${arg#--tool=}" != "${arg}" ]; then
67 tool="${arg#--tool=}"
68 break
69 fi
70 done
71 vsz_tool="$(query_cmd_vsz "${tool}")"
72 awk "END{print $(query_cmd_vsz ${cmd}) + ${vsz_tool:-0}}" \
73 </dev/null
74 else
75 query_cmd_vsz "${cmd}"
76 fi
bartf33ce892008-06-07 11:40:14 +000077}
78
79## Echo all arguments on stderr, run the command passed in $1 .. ${$#} three
80# times, pass the file specified in ${test_input} to the command, write the
81# command output to the file specified in ${test_output}, and print the
82# runtime of the command on stdout.
83function measure_runtime {
bartee17ad62008-06-18 13:31:05 +000084 local i
85
bartf33ce892008-06-07 11:40:14 +000086 echo "$@" >&2
87 for ((i=0;i<3;i++))
88 do
bartee17ad62008-06-18 13:31:05 +000089 echo -n "$(cat "${test_input:-/dev/null}" | \
bartf33ce892008-06-07 11:40:14 +000090 /usr/bin/time --format="%e" "$@" 2>&1 | \
91 tee "${test_output:-/dev/null}" | \
bartee17ad62008-06-18 13:31:05 +000092 tail -n 1) "
93 query_vsz "$@"
bartf33ce892008-06-07 11:40:14 +000094 done
95}
96
bartee17ad62008-06-18 13:31:05 +000097## Print the average runtime of the command passed in $5 .. ${$#}, the ratio
98# of the runtime to $1 +/- $2 and the ratio of the VSZ to $3 +/- $4.
bartf33ce892008-06-07 11:40:14 +000099function print_runtime_ratio {
bartee17ad62008-06-18 13:31:05 +0000100 local tmp avg1="$1" stddev1="$2" vsz1="$3" vszdev1="$4"
101 local avg2 stddev2 vsz2 vszdev2
barte7b5c272008-06-18 08:56:04 +0000102
bartee17ad62008-06-18 13:31:05 +0000103 if [ "${avg1}" = "" -o "${stddev1}" = "" -o "${vsz1}" = "" -o "${vszdev1}" = "" ]; then
104 echo "Error: invalid arguments ($@)."
105 exit 1
106 fi
107
108 shift
109 shift
barte7b5c272008-06-18 08:56:04 +0000110 shift
111 shift
bartf33ce892008-06-07 11:40:14 +0000112
113 tmp="/tmp/test-timing.$$"
114 rm -f "${tmp}"
115
116 measure_runtime "$@" | avgstddev > "$tmp"
bartee17ad62008-06-18 13:31:05 +0000117 read avg2 stddev2 vsz2 vszdev2 < "$tmp"
118 echo "Average time: ${avg2} +/- ${stddev2} seconds / VSZ ${vsz2} +/- ${vszdev2} KB"
bart838262f2008-06-18 14:14:03 +0000119 awk "END{printf "'"'"Ratio = %.2f +/- %.2f; VSZ ratio: %.2f +/- %.2f\n"'"'", ${avg2}/${avg1}, ${avg2}/${avg1}*(${stddev1}/${avg1}+${stddev2}/${avg2}), ${vsz2}/${vsz1}, ${vsz2}/${vsz1}*(${vszdev1}/${vsz1}+${vszdev2}/${vsz2})}" </dev/null
bartf33ce892008-06-07 11:40:14 +0000120}
121