blob: 1964d125d183c86025d05404f29a075bc55449ba [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
bart3158c5d2008-06-21 08:07:40 +000025 if [ -e /sys/devices/system/cpu/cpu0/cache/index2/size ]; then
26 s="$(</sys/devices/system/cpu/cpu0/cache/index2/size)"
27 else
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)"
29 s="${s%B}"
30 fi
bartf33ce892008-06-07 11:40:14 +000031 if [ "${s%M}" != "$s" ]; then
32 echo $((${s%M}*1024*1024))
33 elif [ "${s%K}" != "$s" ]; then
34 echo $((${s%K}*1024))
35 else
36 echo $s
37 fi
38}
39
40## Read a stream of numbers from stdin (one per line), and print the average
41# and standard deviation.
42function avgstddev {
bart42438ac2008-07-10 13:57:56 +000043 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.01)}}'
bartee17ad62008-06-18 13:31:05 +000044}
45
46## Query the virtual memory size for the last invocation of command $1 from
47# the information logged by the kernel (BSD process accounting).
48function query_cmd_vsz {
bartc7eb5912008-06-18 16:07:00 +000049 local pacct
50
bartee17ad62008-06-18 13:31:05 +000051 if [ ! -e /usr/sbin/dump-acct ]; then
52 echo "Error: userspace tools for BSD process accounting have not been" >&2
53 echo "installed. Please install the acct package (Debian systems)." >&2
54 return 1
55 fi
56
bartc7eb5912008-06-18 16:07:00 +000057 if [ -e /var/log/account/pacct ]; then
58 pacct=/var/log/account/pacct
59 elif [ -e /var/account/pacct ]; then
60 pacct=/var/account/pacct
61 else
62 echo "Where is the pacct file ?" >&2
63 return 1
64 fi
65 /usr/sbin/dump-acct "${pacct}" | \
66 grep -- "^$(basename "$1").*|v3|" | \
bartee17ad62008-06-18 13:31:05 +000067 cut -f8 -d'|' | \
68 tail -n 1
69}
70
71## Query the virtual memory size for the last invocation of command $1 from
72# the information logged by the kernel (BSD process accounting).
73function query_vsz {
74 local cmd tool
75
76 cmd="$(basename "$1")"
77 if [ "${cmd}" = "vg-in-place" ]; then
78 tool="tool-not-found"
79 for arg in "${@}"
80 do
81 if [ "${arg#--tool=}" != "${arg}" ]; then
82 tool="${arg#--tool=}"
83 break
84 fi
85 done
86 vsz_tool="$(query_cmd_vsz "${tool}")"
87 awk "END{print $(query_cmd_vsz ${cmd}) + ${vsz_tool:-0}}" \
88 </dev/null
89 else
90 query_cmd_vsz "${cmd}"
91 fi
bartf33ce892008-06-07 11:40:14 +000092}
93
94## Echo all arguments on stderr, run the command passed in $1 .. ${$#} three
bart8a2cd9b2008-06-19 07:49:49 +000095# times, pass the output of ${test_input} -p${p} to the command, write the
bartf33ce892008-06-07 11:40:14 +000096# command output to the file specified in ${test_output}, and print the
97# runtime of the command on stdout.
98function measure_runtime {
bartee17ad62008-06-18 13:31:05 +000099 local i
100
bartf33ce892008-06-07 11:40:14 +0000101 echo "$@" >&2
bart42438ac2008-07-10 13:57:56 +0000102 if [ "${test_output}" != "" ]; then
103 echo "$@" >"${test_output}"
104 fi
bartf33ce892008-06-07 11:40:14 +0000105 for ((i=0;i<3;i++))
106 do
bart8a2cd9b2008-06-19 07:49:49 +0000107 echo -n "$("${test_input:-true}" $p | \
bartf33ce892008-06-07 11:40:14 +0000108 /usr/bin/time --format="%e" "$@" 2>&1 | \
bart42438ac2008-07-10 13:57:56 +0000109 tee -a "${test_output:-/dev/null}" | \
bartee17ad62008-06-18 13:31:05 +0000110 tail -n 1) "
111 query_vsz "$@"
bartf33ce892008-06-07 11:40:14 +0000112 done
113}
114
bartee17ad62008-06-18 13:31:05 +0000115## Print the average runtime of the command passed in $5 .. ${$#}, the ratio
116# of the runtime to $1 +/- $2 and the ratio of the VSZ to $3 +/- $4.
bartf33ce892008-06-07 11:40:14 +0000117function print_runtime_ratio {
bartee17ad62008-06-18 13:31:05 +0000118 local tmp avg1="$1" stddev1="$2" vsz1="$3" vszdev1="$4"
119 local avg2 stddev2 vsz2 vszdev2
barte7b5c272008-06-18 08:56:04 +0000120
bartee17ad62008-06-18 13:31:05 +0000121 if [ "${avg1}" = "" -o "${stddev1}" = "" -o "${vsz1}" = "" -o "${vszdev1}" = "" ]; then
122 echo "Error: invalid arguments ($@)."
123 exit 1
124 fi
125
126 shift
127 shift
barte7b5c272008-06-18 08:56:04 +0000128 shift
129 shift
bartf33ce892008-06-07 11:40:14 +0000130
131 tmp="/tmp/test-timing.$$"
132 rm -f "${tmp}"
133
134 measure_runtime "$@" | avgstddev > "$tmp"
bartee17ad62008-06-18 13:31:05 +0000135 read avg2 stddev2 vsz2 vszdev2 < "$tmp"
136 echo "Average time: ${avg2} +/- ${stddev2} seconds / VSZ ${vsz2} +/- ${vszdev2} KB"
bart838262f2008-06-18 14:14:03 +0000137 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 +0000138}
139