]> rtime.felk.cvut.cz Git - can-benchmark.git/blobdiff - gw-tests/lib.sh
Plot graphs with cairo - it is faster and nicer
[can-benchmark.git] / gw-tests / lib.sh
index 3ea41531bf94a5f60d097999bdaaf4b0b03b51df..70f013212c281b18e1a92455263fbb81d13132fe 100644 (file)
 set -e
 
-PATH=$PWD/../_compiled/bin/:$PATH
+COUNT=10000
+
+OPT_TRAFFIC=oneatatime
+OPT_LOAD=none
 
 error() {
     echo $1 >&2
     exit 1
 }
 
+while [ $# -gt 0 ]; do
+    case "$1" in
+       -P|--no-plot) OPT_PLOT_DISABLE=1; shift;;
+       -p|--plot) OPT_PLOT_ONLY=1; shift;;
+       -X|--no-x11-plot) OPT_NO_X11=1; shift;;
+       -t) case "$2" in
+               all|flood|50|oneatatime) OPT_TRAFFIC=$2;;
+               *) error "Unknown traffic specification: $2";;
+           esac;
+           shift 2;;
+       -l) case "$2" in
+               all|none|cpu|eth|can) OPT_LOAD=$2;;
+               *) error "Unknown load specification: $2";;
+           esac;
+           shift 2;;
+    esac
+done
+
+PATH=$PWD/../_compiled/bin/:$PATH
+
 sshgw() {
     local socket="$HOME/.ssh/cangw-connection"
 
-    if [[ ! -S $socket ]]; then
-        # Create master connection to speed up subsequenct command.
-        # The ssh is put into background and the connection is closed
-        # after 10 minutes)
-       ssh -f -M -S $socket root@192.168.2.3 sleep 600 > /dev/null 2>&1
+    if [[ ! -S $socket ]] || ! ssh -x -a -S $socket root@192.168.2.3 true; then
+       # Create master connection to speed up subsequenct command.
+       ssh -N -f -M -S $socket root@192.168.2.3 >/dev/null 2>&1
     fi
     ssh -x -a -S $socket root@192.168.2.3 "$@"
 }
 
 cleanupgw() {
-    # Set can interfaces up
-    sshgw 'for i in 0 1; do ip link show dev can$i|grep -q UP || ip link set can$i up type can bitrate 1000000; done'
-    # Delete all GW rules
-    sshgw 'eval $(cangw -L|sed -e s/-A/-D/ -e "s/#.*/;/")'
+    sshgw 'cangw -F'
 }
 
-plot() {
-#    for i in "${!PLOT_CMD[@]}"; do
+_plot() {
     local testname=`basename $0 .sh`
-    for i in 0; do
-       cmd=`p`                 # Get plot commands
-       cmd=$(echo "$cmd" | sed -e "/set title/ s/[\"']\(.*\)[\"']/\"\1\\\\n($(kvers), CPU\\\\\\\\@${clk}MHz)\"/")
-       if [ -z "$OPT_NO_X11" ]; then
-           echo "set terminal x11 enhanced; ${cmd}" | gnuplot -persist
-       fi
-       I=${i/0/}
-       echo 'set terminal postscript color eps enhanced size 6cm,4cm lw 1 "Times-Roman" 10;' \
-            'set lmargin 8;' \
-             "${cmd}" | gnuplot > ${testname}$I.eps
-       echo "set terminal postscript color eps enhanced;" \
-            "${cmd}" | gnuplot | epstopdf --filter > ${testname}$I.pdf
-       mkdir -p thumb
-       convert -density 30  -gamma 0.5 -quality 90 -type Palette -depth 8 ${testname}$I.pdf thumb/${testname}$I.png
-       convert -density 150 -gamma 0.7 -quality 90 -type Palette -depth 8 ${testname}$I.pdf ${testname}$I.png
-    done
+
+    plot_cmds | sed -e "/set title/ s/[\"']\(.*\)[\"']/\"\1\\\\n(GW kernel $kvers, Traffic $traffic, Load $load)\"/" > plot.gp
+    if [[ ! -s plot.gp ]]; then return; fi
+    if [ -z "$OPT_NO_X11" ]; then
+       echo "set terminal x11 enhanced; $(< plot.gp)" | gnuplot -persist
+    fi
+    I=''
+    echo 'set terminal pdfcairo color enhanced;' \
+       "$(< plot.gp)" | gnuplot > ${testname}$I.pdf
+    echo 'set terminal pngcairo color enhanced size 750,525 font ",10" ;' \
+       "$(< plot.gp)" | gnuplot > ${testname}$I.png
+    mkdir -p thumb
+    convert -size 150x105 -quality 90 -type Palette -depth 8 ${testname}$I.png thumb/${testname}$I.png
+#     echo 'set terminal pngcairo color enhanced size 150,105 font ",1";' \
+#      "$(< plot.gp)" | gnuplot > thumb/${testname}$I.png
+
+}
+
+echo_plot() {
+    plot=$1
+    [[ "$_plot_separator" ]] && echo ", \\"
+    _plot_separator=t
+    echo -n "    " $plot
 }
 
-create_dirs_and_links() {
-    local test=$1
-    local kver=$2
+traffic_and_length() {
+    local opts
+    case $traffic in
+       flood) opts='';;
+       50)        opts="-p $((2*(44+$1*8)))";;
+       oneatatime)        opts="-o";;
+       *) error "Unknown traffic specification: $traffic"
+    esac
+    echo $opts -l $1
+}
 
-    set -x
-    local  d=results/by-kern/$kver/$test
-    mkdir -p $d
-    mkdir -p results/by-test/$test
-    ln -sfT ../../${d#results/} results/by-test/$test/$kver
-    echo $d
+start_load() {
+    case $load in
+       none) ;;
+       cpu) sshgw 'hackbench -g 3 -l 10000' & loadpid=$!;;
+       eth) ping -f -s 60000 -q 192.168.2.3 & loadpid=$!;; # TODO: Generate eth load from another computer
+       can) latester -q -d can1 -i 0x7ff & loadpid=$!;;
+       *) error "Unknown load specification: $load"
+    esac
 }
 
-_run() {
+kill_load() {
+    case $load in
+       none) ;;
+       cpu) kill $loadpid; sshgw "killall -q hackbench || :";;
+       eth) kill $loadpid;;
+       can) kill $loadpid;;
+       *) error "Unknown load specification: $load"
+    esac
+}
+
+_measure() {
+    # Remove data from the last measurement
+    rm -rf *
+    # Kill load generators left possibly from the past runs
+    killall -q ping || :
+    killall -q latester || :
+    sshgw 'killall -q hackbench || :'
+    # Set can interfaces up
+    sshgw 'for i in 0 1; do ip link show dev can$i|grep -q UP || ip link set can$i up type can bitrate 1000000; done'
+    # Delete all vcan interfaces
+    sshgw 'for dev in $(ip l|grep -o vcan[^:]\\+); do ip link del dev $dev; done'
+    # Reset priorities
+    sshgw 'if pid=`pidof irq/145-can0`; then chrt -p -f 50 $pid > /dev/null; fi'
+    sshgw 'if pid=`pidof irq/146-can1`; then chrt -p -f 50 $pid > /dev/null; fi'
+    sshgw 'if pid=`pidof sirq-net-rx/0`; then chrt -p -f 49 $pid > /dev/null; fi'
+    sshgw 'if pid=`pidof sirq-net-tx/0`; then chrt -p -f 49 $pid > /dev/null; fi'
+    # Set the length of qdisc queue to avoid ENOBUFS errors
+    ifconfig can0 txqueuelen 200
+    ifconfig can1 txqueuelen 200
     cleanupgw
-    dir=$(create_dirs_and_links $(basename $0 .sh)  $(sshgw uname -r))
-    cd $dir
+
+    type prepare >/dev/null 2>&1 && prepare || :
+    start_load
     main
+    kill_load
+
+    # Create a plot script at the end to signalize that the
+    # measurement was successfully finished
+    cat > plot.sh <<-EOF
+       #!/bin/bash
+       export kvers=$kvers
+       export hostkvers=$hostkvers
+       export traffic=$traffic
+       export load=$load
+       cd \$(dirname \$0)/$(dirname $script)
+       exec ./$(basename $script) --plot "\$@"
+       EOF
+    chmod +x plot.sh
+}
+
+
+_run() {
+    if [[ $OPT_TRAFFIC = all ]]
+    then traffics="flood 50 oneatatime"
+    else traffics=${traffic:-$OPT_TRAFFIC}
+    fi
+    if [[ $OPT_LOAD = all ]]
+    then loads="none cpu eth" # TODO: can
+    else loads=${load:-$OPT_LOAD}
+    fi
+    hostkvers=${hostkvers:-host-$(uname -r)}
+    kvers=${kvers:-$(sshgw uname -r)}
+    test=$(basename $0 .sh)
+    for load in $loads; do
+    for traffic in $traffics; do
+       dir="results/$hostkvers/$kvers/$traffic/$load/$test"
+       mkdir -p $dir
+       script=$(echo $dir | sed -e 's/[^/]*/../g')/${test}.sh
+       cd $dir
+       if [[ ! "$OPT_PLOT_ONLY" ]]; then
+           echo "Working directory: $dir"
+           _measure
+       fi
+       if [[ ! "$OPT_PLOT_DISABLE" ]]; then
+           _plot
+       fi
+       cd - > /dev/null
+    done
+    done
 }
 
 test_end() {
     test_end_called=t
-    trap '' DEBUG
+    exit_ok=
     _run
+    exit_ok=t
 }
 
-exit() {
+_myexit() {
+    code=$?
+    cmd=$BASH_COMMAND
     if [[ ! "$test_end_called" ]]; then
        test_end_called=t
        error "bug in the test script: No test_end called"
     fi
+    if ! test -n "$exit_ok"; then
+       error "FATAL: Command '$cmd' exit with code $code"
+    fi
 }
 
-trap 'exit' EXIT
+trap '_myexit' EXIT