bart | f33ce89 | 2008-06-07 11:40:14 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | ######################## |
| 4 | # Function definitions # |
| 5 | ######################## |
| 6 | |
| 7 | ## Print the logarithm base 2 of $1 on stdout. |
| 8 | function 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. |
| 23 | function 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. |
| 37 | function avgstddev { |
bart | 838262f | 2008-06-18 14:14:03 +0000 | [diff] [blame^] | 38 | 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)}}' |
bart | ee17ad6 | 2008-06-18 13:31:05 +0000 | [diff] [blame] | 39 | } |
| 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). |
| 43 | function 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). |
| 58 | function 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 |
bart | f33ce89 | 2008-06-07 11:40:14 +0000 | [diff] [blame] | 77 | } |
| 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. |
| 83 | function measure_runtime { |
bart | ee17ad6 | 2008-06-18 13:31:05 +0000 | [diff] [blame] | 84 | local i |
| 85 | |
bart | f33ce89 | 2008-06-07 11:40:14 +0000 | [diff] [blame] | 86 | echo "$@" >&2 |
| 87 | for ((i=0;i<3;i++)) |
| 88 | do |
bart | ee17ad6 | 2008-06-18 13:31:05 +0000 | [diff] [blame] | 89 | echo -n "$(cat "${test_input:-/dev/null}" | \ |
bart | f33ce89 | 2008-06-07 11:40:14 +0000 | [diff] [blame] | 90 | /usr/bin/time --format="%e" "$@" 2>&1 | \ |
| 91 | tee "${test_output:-/dev/null}" | \ |
bart | ee17ad6 | 2008-06-18 13:31:05 +0000 | [diff] [blame] | 92 | tail -n 1) " |
| 93 | query_vsz "$@" |
bart | f33ce89 | 2008-06-07 11:40:14 +0000 | [diff] [blame] | 94 | done |
| 95 | } |
| 96 | |
bart | ee17ad6 | 2008-06-18 13:31:05 +0000 | [diff] [blame] | 97 | ## 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. |
bart | f33ce89 | 2008-06-07 11:40:14 +0000 | [diff] [blame] | 99 | function print_runtime_ratio { |
bart | ee17ad6 | 2008-06-18 13:31:05 +0000 | [diff] [blame] | 100 | local tmp avg1="$1" stddev1="$2" vsz1="$3" vszdev1="$4" |
| 101 | local avg2 stddev2 vsz2 vszdev2 |
bart | e7b5c27 | 2008-06-18 08:56:04 +0000 | [diff] [blame] | 102 | |
bart | ee17ad6 | 2008-06-18 13:31:05 +0000 | [diff] [blame] | 103 | if [ "${avg1}" = "" -o "${stddev1}" = "" -o "${vsz1}" = "" -o "${vszdev1}" = "" ]; then |
| 104 | echo "Error: invalid arguments ($@)." |
| 105 | exit 1 |
| 106 | fi |
| 107 | |
| 108 | shift |
| 109 | shift |
bart | e7b5c27 | 2008-06-18 08:56:04 +0000 | [diff] [blame] | 110 | shift |
| 111 | shift |
bart | f33ce89 | 2008-06-07 11:40:14 +0000 | [diff] [blame] | 112 | |
| 113 | tmp="/tmp/test-timing.$$" |
| 114 | rm -f "${tmp}" |
| 115 | |
| 116 | measure_runtime "$@" | avgstddev > "$tmp" |
bart | ee17ad6 | 2008-06-18 13:31:05 +0000 | [diff] [blame] | 117 | read avg2 stddev2 vsz2 vszdev2 < "$tmp" |
| 118 | echo "Average time: ${avg2} +/- ${stddev2} seconds / VSZ ${vsz2} +/- ${vszdev2} KB" |
bart | 838262f | 2008-06-18 14:14:03 +0000 | [diff] [blame^] | 119 | 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 |
bart | f33ce89 | 2008-06-07 11:40:14 +0000 | [diff] [blame] | 120 | } |
| 121 | |