3 ########################
4 # Function definitions #
5 ########################
7 ## Print the logarithm base 2 of $1 on stdout.
13 if [ $
((2**i
)) = "$1" ]; then
22 ## Print the size of the level 2 cache in bytes on stdout.
23 function get_cache_size
{
25 if [ -e /sys
/devices
/system
/cpu
/cpu
0/cache
/index
2/size
]; then
26 s
="$(</sys/devices/system/cpu/cpu0/cache/index2/size)"
28 s
="$(cat /proc/cpuinfo|while read a b c d e ; do if [ "$a" = cache -a "$b" = size ]; then echo $d $e; break; fi; done)"
31 if [ "${s%M}" != "$s" ]; then
32 echo $
((${s%M}*1024*1024))
33 elif [ "${s%K}" != "$s" ]; then
40 ## Read zero or more lines from stdin, and print the average and standard
41 # deviation per column. n is the number of lines, m the number of columns.
42 # Each line must have the same number of columns.
44 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.0001?sqrt(d):0.01)}}'
47 ## Query the virtual memory size for the last invocation of command $1 from
48 # the information logged by the kernel (BSD process accounting).
49 function query_cmd_vsz
{
52 if [ ! -e /usr
/sbin
/dump-acct
]; then
53 echo "Error: userspace tools for BSD process accounting have not been" >&2
54 echo "installed. Please install the acct package (Debian systems)." >&2
58 if ! { dump-acct
-h 2>&1 |
grep -q -w format
; }; then
59 echo "Error: the installed version of dump-acct is not recent enough." >&2
63 if [ -e /var
/log
/account
/pacct
]; then
64 pacct
=/var
/log
/account
/pacct
65 elif [ -e /var
/account
/pacct
]; then
66 pacct
=/var
/account
/pacct
68 echo "Where is the pacct file ?" >&2
71 /usr
/sbin
/dump-acct
"${pacct}" | \
72 grep -- "^$(basename "$1").*|v3|" | \
75 { read vsz
; echo ${vsz:-0}; }
78 ## Query the virtual memory size for the last invocation of command $1 from
79 # the information logged by the kernel (BSD process accounting).
83 cmd
="$(basename "$1")"
84 if [ "${cmd}" = "vg-in-place" ]; then
88 if [ "${arg#--tool=}" != "${arg}" ]; then
93 vsz_tool
="$(query_cmd_vsz "${tool}")"
94 awk "END{print $(query_cmd_vsz ${cmd}) + ${vsz_tool:-0}}" \
97 query_cmd_vsz
"${cmd}"
101 ## Echo all arguments on stderr, run the command passed in $1 .. ${$#} three
102 # times, pass the output of ${test_input} -p${p} to the command, write the
103 # command output to the file specified in ${test_output}, and print the
104 # runtime of the command on stdout.
105 function measure_runtime
{
109 if [ "${test_output}" != "" ]; then
110 echo "$@" >"${test_output}"
114 echo -n "$("${test_input:-true}" $p | \
115 /usr/bin/time --format="%e
" "$@
" 2>&1 | \
116 tee -a "${test_output:-/dev/null}" | \
122 ## Print the average runtime of the command passed in $5 .. ${$#}, the ratio
123 # of the runtime to $1 +/- $2 and the ratio of the VSZ to $3 +/- $4.
124 function print_runtime_ratio
{
125 local tmp avg1
="$1" stddev1
="$2" vsz1
="$3" vszdev1
="$4"
126 local avg2 stddev2 vsz2 vszdev2
128 if [ "${avg1}" = "" -o "${stddev1}" = "" -o "${vsz1}" = "" -o "${vszdev1}" = "" ]; then
129 echo "Error: invalid arguments ($@)."
138 tmp
="/tmp/test-timing.$$"
141 measure_runtime
"$@" | avgstddev
> "$tmp"
142 read avg2 stddev2 vsz2 vszdev2
< "$tmp"
143 echo "Average time: ${avg2} +/- ${stddev2} seconds / VSZ ${vsz2} +/- ${vszdev2} KB"
144 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