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