blob: 3c2c878c8509d0a1c17a09345005ed520f7062fe [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
bartb09132d2008-09-07 17:10:07 +000013 if [ $((2**i)) = "$1" ]; then
bartf33ce892008-06-07 11:40:14 +000014 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
bart114c9ba2009-04-25 11:39:20 +000040## 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.
bartf33ce892008-06-07 11:40:14 +000043function avgstddev {
bart15071eb2008-07-10 14:01:04 +000044 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)}}'
bartee17ad62008-06-18 13:31:05 +000045}
46
47## Query the virtual memory size for the last invocation of command $1 from
48# the information logged by the kernel (BSD process accounting).
49function query_cmd_vsz {
bartc7eb5912008-06-18 16:07:00 +000050 local pacct
51
bartee17ad62008-06-18 13:31:05 +000052 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
55 return 1
56 fi
57
bart114c9ba2009-04-25 11:39:20 +000058 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
60 return 1
61 fi
62
bartc7eb5912008-06-18 16:07:00 +000063 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
67 else
68 echo "Where is the pacct file ?" >&2
69 return 1
70 fi
71 /usr/sbin/dump-acct "${pacct}" | \
72 grep -- "^$(basename "$1").*|v3|" | \
bartee17ad62008-06-18 13:31:05 +000073 cut -f8 -d'|' | \
bart114c9ba2009-04-25 11:39:20 +000074 tail -n 1 | \
75 { read vsz; echo ${vsz:-0}; }
bartee17ad62008-06-18 13:31:05 +000076}
77
78## Query the virtual memory size for the last invocation of command $1 from
79# the information logged by the kernel (BSD process accounting).
80function query_vsz {
81 local cmd tool
82
83 cmd="$(basename "$1")"
84 if [ "${cmd}" = "vg-in-place" ]; then
85 tool="tool-not-found"
86 for arg in "${@}"
87 do
88 if [ "${arg#--tool=}" != "${arg}" ]; then
89 tool="${arg#--tool=}"
90 break
91 fi
92 done
93 vsz_tool="$(query_cmd_vsz "${tool}")"
94 awk "END{print $(query_cmd_vsz ${cmd}) + ${vsz_tool:-0}}" \
95 </dev/null
96 else
97 query_cmd_vsz "${cmd}"
98 fi
bartf33ce892008-06-07 11:40:14 +000099}
100
101## Echo all arguments on stderr, run the command passed in $1 .. ${$#} three
bart8a2cd9b2008-06-19 07:49:49 +0000102# times, pass the output of ${test_input} -p${p} to the command, write the
bartf33ce892008-06-07 11:40:14 +0000103# command output to the file specified in ${test_output}, and print the
104# runtime of the command on stdout.
105function measure_runtime {
bartee17ad62008-06-18 13:31:05 +0000106 local i
107
bartf33ce892008-06-07 11:40:14 +0000108 echo "$@" >&2
bart42438ac2008-07-10 13:57:56 +0000109 if [ "${test_output}" != "" ]; then
110 echo "$@" >"${test_output}"
111 fi
bartf33ce892008-06-07 11:40:14 +0000112 for ((i=0;i<3;i++))
113 do
bart8a2cd9b2008-06-19 07:49:49 +0000114 echo -n "$("${test_input:-true}" $p | \
bartf33ce892008-06-07 11:40:14 +0000115 /usr/bin/time --format="%e" "$@" 2>&1 | \
bart42438ac2008-07-10 13:57:56 +0000116 tee -a "${test_output:-/dev/null}" | \
bartee17ad62008-06-18 13:31:05 +0000117 tail -n 1) "
118 query_vsz "$@"
bartf33ce892008-06-07 11:40:14 +0000119 done
120}
121
bartee17ad62008-06-18 13:31:05 +0000122## 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.
bartf33ce892008-06-07 11:40:14 +0000124function print_runtime_ratio {
bartee17ad62008-06-18 13:31:05 +0000125 local tmp avg1="$1" stddev1="$2" vsz1="$3" vszdev1="$4"
126 local avg2 stddev2 vsz2 vszdev2
barte7b5c272008-06-18 08:56:04 +0000127
bartee17ad62008-06-18 13:31:05 +0000128 if [ "${avg1}" = "" -o "${stddev1}" = "" -o "${vsz1}" = "" -o "${vszdev1}" = "" ]; then
129 echo "Error: invalid arguments ($@)."
130 exit 1
131 fi
132
133 shift
134 shift
barte7b5c272008-06-18 08:56:04 +0000135 shift
136 shift
bartf33ce892008-06-07 11:40:14 +0000137
138 tmp="/tmp/test-timing.$$"
139 rm -f "${tmp}"
140
141 measure_runtime "$@" | avgstddev > "$tmp"
bartee17ad62008-06-18 13:31:05 +0000142 read avg2 stddev2 vsz2 vszdev2 < "$tmp"
143 echo "Average time: ${avg2} +/- ${stddev2} seconds / VSZ ${vsz2} +/- ${vszdev2} KB"
bart838262f2008-06-18 14:14:03 +0000144 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 +0000145}
146