blob: dae5696166d2cf3f834c753ce445b3e844204624 [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 {
bartc7eb5912008-06-18 16:07:00 +000044 local pacct
45
bartee17ad62008-06-18 13:31:05 +000046 if [ ! -e /usr/sbin/dump-acct ]; then
47 echo "Error: userspace tools for BSD process accounting have not been" >&2
48 echo "installed. Please install the acct package (Debian systems)." >&2
49 return 1
50 fi
51
bartc7eb5912008-06-18 16:07:00 +000052 if [ -e /var/log/account/pacct ]; then
53 pacct=/var/log/account/pacct
54 elif [ -e /var/account/pacct ]; then
55 pacct=/var/account/pacct
56 else
57 echo "Where is the pacct file ?" >&2
58 return 1
59 fi
60 /usr/sbin/dump-acct "${pacct}" | \
61 grep -- "^$(basename "$1").*|v3|" | \
bartee17ad62008-06-18 13:31:05 +000062 cut -f8 -d'|' | \
63 tail -n 1
64}
65
66## Query the virtual memory size for the last invocation of command $1 from
67# the information logged by the kernel (BSD process accounting).
68function query_vsz {
69 local cmd tool
70
71 cmd="$(basename "$1")"
72 if [ "${cmd}" = "vg-in-place" ]; then
73 tool="tool-not-found"
74 for arg in "${@}"
75 do
76 if [ "${arg#--tool=}" != "${arg}" ]; then
77 tool="${arg#--tool=}"
78 break
79 fi
80 done
81 vsz_tool="$(query_cmd_vsz "${tool}")"
82 awk "END{print $(query_cmd_vsz ${cmd}) + ${vsz_tool:-0}}" \
83 </dev/null
84 else
85 query_cmd_vsz "${cmd}"
86 fi
bartf33ce892008-06-07 11:40:14 +000087}
88
89## Echo all arguments on stderr, run the command passed in $1 .. ${$#} three
90# times, pass the file specified in ${test_input} to the command, write the
91# command output to the file specified in ${test_output}, and print the
92# runtime of the command on stdout.
93function measure_runtime {
bartee17ad62008-06-18 13:31:05 +000094 local i
95
bartf33ce892008-06-07 11:40:14 +000096 echo "$@" >&2
97 for ((i=0;i<3;i++))
98 do
bartee17ad62008-06-18 13:31:05 +000099 echo -n "$(cat "${test_input:-/dev/null}" | \
bartf33ce892008-06-07 11:40:14 +0000100 /usr/bin/time --format="%e" "$@" 2>&1 | \
101 tee "${test_output:-/dev/null}" | \
bartee17ad62008-06-18 13:31:05 +0000102 tail -n 1) "
103 query_vsz "$@"
bartf33ce892008-06-07 11:40:14 +0000104 done
105}
106
bartee17ad62008-06-18 13:31:05 +0000107## Print the average runtime of the command passed in $5 .. ${$#}, the ratio
108# of the runtime to $1 +/- $2 and the ratio of the VSZ to $3 +/- $4.
bartf33ce892008-06-07 11:40:14 +0000109function print_runtime_ratio {
bartee17ad62008-06-18 13:31:05 +0000110 local tmp avg1="$1" stddev1="$2" vsz1="$3" vszdev1="$4"
111 local avg2 stddev2 vsz2 vszdev2
barte7b5c272008-06-18 08:56:04 +0000112
bartee17ad62008-06-18 13:31:05 +0000113 if [ "${avg1}" = "" -o "${stddev1}" = "" -o "${vsz1}" = "" -o "${vszdev1}" = "" ]; then
114 echo "Error: invalid arguments ($@)."
115 exit 1
116 fi
117
118 shift
119 shift
barte7b5c272008-06-18 08:56:04 +0000120 shift
121 shift
bartf33ce892008-06-07 11:40:14 +0000122
123 tmp="/tmp/test-timing.$$"
124 rm -f "${tmp}"
125
126 measure_runtime "$@" | avgstddev > "$tmp"
bartee17ad62008-06-18 13:31:05 +0000127 read avg2 stddev2 vsz2 vszdev2 < "$tmp"
128 echo "Average time: ${avg2} +/- ${stddev2} seconds / VSZ ${vsz2} +/- ${vszdev2} KB"
bart838262f2008-06-18 14:14:03 +0000129 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 +0000130}
131