]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - gw-tests/lib.sh
Set gw txqueuelen to avoid ENOBUFS errors
[can-benchmark.git] / gw-tests / lib.sh
1 set -e
2
3 COUNT=10000
4
5 OPT_TRAFFIC=oneatatime
6 OPT_LOAD=none
7
8 error() {
9     echo $1 >&2
10     exit 1
11 }
12
13 while [ $# -gt 0 ]; do
14     case "$1" in
15         -P|--no-plot) OPT_PLOT_DISABLE=1; shift;;
16         -p|--plot) OPT_PLOT_ONLY=1; shift;;
17         -X|--no-x11-plot) OPT_NO_X11=1; shift;;
18         -t) case "$2" in
19                 all|flood|50|oneatatime) OPT_TRAFFIC=$2;;
20                 *) error "Unknown traffic specification: $2";;
21             esac;
22             shift 2;;
23         -l) case "$2" in
24                 all|none|cpu|eth|can) OPT_LOAD=$2;;
25                 *) error "Unknown load specification: $2";;
26             esac;
27             shift 2;;
28     esac
29 done
30
31 PATH=$PWD/../_compiled/bin/:$PATH
32
33 sshgw() {
34     local socket="$HOME/.ssh/cangw-connection"
35
36     if [[ "$1" = "-f" ]]; then
37             shift
38     else
39         if [[ "$NO_GW_NEEDED" ]]; then return; fi
40     fi
41
42     if [[ ! -S $socket ]] || ! ssh -x -a -S $socket root@192.168.2.3 true; then
43         # Create master connection to speed up subsequenct command.
44         ssh -N -f -M -S $socket root@192.168.2.3 >/dev/null 2>&1
45     fi
46     ssh -x -a -S $socket root@192.168.2.3 "$@"
47 }
48
49 cleanupgw() {
50     sshgw 'cangw -F'
51 }
52
53 _plot() {
54     local testname=`basename $0 .sh`
55     rm -rf *.pdf *.png
56     for i in "" $ADDITIONAL_PLOTS; do
57         plot_cmds $i | sed -e "/set title/ s/[\"']\(.*\)[\"']/\"\1\\\\n{\/*0.75 (GW kernel $kvers, Traffic $traffic, Load $load)}\"/" > plot.gp
58         if [[ ! -s plot.gp ]]; then continue; fi
59         if [ -z "$OPT_NO_X11" ]; then
60             echo "set terminal x11 enhanced; $(< plot.gp)" | gnuplot -persist
61         fi
62         test "$(gnuplot --version)" = "gnuplot 4.2 patchlevel 5 " && continue
63         echo "set terminal pdfcairo solid color enhanced; " \
64              "set output \"graph$i.pdf\";" \
65              "set pointsize 0.5;" \
66             "$(< plot.gp)" | gnuplot
67         echo 'set terminal pngcairo color enhanced size 750,525 font ",10" ;' \
68             "$(< plot.gp)" | gnuplot > graph$i.png
69         convert -resize 150x105 -gamma 0.5 -quality 90 -type Palette -depth 8 graph$i.png tgraph$i.png
70 #     echo 'set terminal pngcairo color enhanced size 150,105 font ",1";' \
71 #       "$(< plot.gp)" | gnuplot > thumb/${testname}$i.png
72     done
73 }
74
75 echo_plot() {
76     plot=$1
77     [[ "$_plot_separator" ]] && echo ", \\"
78     _plot_separator=t
79     echo -n "    " $plot
80 }
81
82 traffic_and_length() {
83     local opts
84     case $traffic in
85         flood) opts='';;
86         50)        opts="-p $((2*(44+$1*8)))";;
87         oneatatime)        opts="-o";;
88         *) error "Unknown traffic specification: $traffic"
89     esac
90     echo $opts -l $1
91 }
92
93 start_load() {
94     case $load in
95         none) ;;
96         cpu) sshgw 'hackbench -g 3 -l 100000' & loadpid=$!;;
97         eth) ping -f -s 60000 -q 192.168.2.3 & loadpid=$!;; # TODO: Generate eth load from another computer
98         can) latester -q -d can1 -i 0x7ff & loadpid=$!;;
99         *) error "Unknown load specification: $load"
100     esac
101 }
102
103 kill_load() {
104     case $load in
105         none) ;;
106         cpu) kill $loadpid; sshgw "killall -q hackbench || :";;
107         eth) kill $loadpid;;
108         can) kill $loadpid;;
109         *) error "Unknown load specification: $load"
110     esac
111 }
112
113 _measure() {
114     # Remove data from the last measurement
115     rm -rf *
116     # Kill load generators left possibly from the past runs
117     killall -q ping || :
118     killall -q latester || :
119     if [[ ! "$NO_GW_NEEDED" ]]; then
120         sshgw 'killall -q hackbench || :'
121         # Set can interfaces up
122         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'
123         # Delete all vcan interfaces
124         sshgw 'for dev in $(ip l|grep -o vcan[^:]\\+); do ip link del dev $dev; done'
125         # Reset priorities
126         sshgw 'if pid=`pidof irq/145-can0`; then chrt -p -f 50 $pid > /dev/null; fi'
127         sshgw 'if pid=`pidof irq/146-can1`; then chrt -p -f 50 $pid > /dev/null; fi'
128         sshgw 'if pid=`pidof sirq-net-rx/0`; then chrt -p -f 49 $pid > /dev/null; fi'
129         sshgw 'if pid=`pidof sirq-net-tx/0`; then chrt -p -f 49 $pid > /dev/null; fi'
130         # Set the length of qdisc queue to avoid ENOBUFS errors
131         sshgw 'ifconfig can0 txqueuelen 1000; ifconfig can1 txqueuelen 1000'
132         ifconfig can0 txqueuelen 200
133         ifconfig can1 txqueuelen 200
134         cleanupgw
135
136         type prepare >/dev/null 2>&1 && prepare || :
137         start_load
138     fi
139
140     main
141
142     if [[ ! "$NO_GW_NEEDED" ]]; then
143         kill_load
144     fi
145
146     if [ ! "$SKIPPED" ]; then
147         # Create a plot script at the end to signalize that the
148         # measurement was successfully finished
149         cat > plot.sh <<-EOF
150         #!/bin/bash
151         export kvers=$kvers
152         export hostkvers=$hostkvers
153         export traffic=$traffic
154         export load=$load
155         cd \$(dirname \$0)/$(dirname $script)
156         exec ./$(basename $script) --plot "\$@"
157         EOF
158         chmod +x plot.sh
159     fi
160 }
161
162
163 _run() {
164     if [[ $OPT_TRAFFIC = all ]]
165     then traffics="flood 50 oneatatime"
166     else traffics=${traffic:-$OPT_TRAFFIC}
167     fi
168     if [[ $OPT_LOAD = all ]]
169     then loads="none cpu eth" # TODO: can
170     else loads=${load:-$OPT_LOAD}
171     fi
172     hostkvers=${hostkvers:-host-$(uname -r)}
173     kvers=${kvers:-$(sshgw -f uname -r)}
174     test=$(basename $0 .sh)
175     for load in $loads; do
176     for traffic in $traffics; do
177         dir="results/$hostkvers/$kvers/$traffic/$load/$test"
178         mkdir -p $dir
179         script=$(echo $dir | sed -e 's/[^/]*/../g')/${test}.sh
180         cd $dir
181         if [[ ! "$OPT_PLOT_ONLY" ]]; then
182             echo "Working directory: $dir"
183             _measure
184         fi
185         if [[ ! "$OPT_PLOT_DISABLE" ]]; then
186             _plot
187         fi
188         cd - > /dev/null
189     done
190     done
191 }
192
193 test_end() {
194     test_end_called=t
195     exit_ok=
196     _run
197     exit_ok=t
198 }
199
200 _myexit() {
201     code=$?
202     cmd=$BASH_COMMAND
203     if [[ ! "$test_end_called" ]]; then
204         test_end_called=t
205         error "bug in the test script: No test_end called"
206     fi
207     if ! test -n "$exit_ok"; then
208         error "FATAL: Command '$cmd' exit with code $code"
209     fi
210 }
211
212 trap '_myexit' EXIT