Minor fixes
[can-benchmark.git] / tests / _lib.sh
1
2 while [ $# -gt 0 ]; do
3     case "$1" in
4         -p) OPT_PLOT_ONLY=1; shift;;
5         -X|--no-x11-plot) OPT_NO_X11=1; shift;;
6     esac
7 done
8
9 #set -x
10 set -e
11
12 PATH=$PWD/../_compiled/bin/:$PATH
13
14 RTPRIO=${RTPRIO:-60}
15 COUNT=${COUNT:-10000}
16
17 boost_irq_prio() {
18     local PRIO=${1:-50}
19     CAN_IRQ=`sed -ne '/can/ s/:.*//p' /proc/interrupts`
20     if [ -n "$CAN_IRQ" ]; then
21         IRQ_PID=`pidof IRQ-${CAN_IRQ/ /}` || true
22         if [ -n "$IRQ_PID" ]; then
23             schedtool -F -p $PRIO $IRQ_PID
24             return 0
25         fi
26     fi
27     return 1
28 }
29
30 rmmod_can() {
31     modprobe -r lincan kvaser_pci vcan
32 }
33
34 prepare_lincan_hw() {
35     rmmod_can
36     modprobe -C /dev/null lincan hw=pcican-q io=1 baudrate=1000,1000,1000,1000 processlocal=0
37     CAN0=/dev/can0
38     CAN1=/dev/can1
39     DRIVER=lincan
40     sleep 0.1
41 }
42
43 prepare_lincan_virtual() {
44     rmmod_can
45     modprobe -C /dev/null lincan hw=virtual io=0 baudrate=0
46     CAN0=/dev/can0
47     DRIVER=lincan-virtual
48     sleep 0.1
49 }
50
51 prepare_socketcan_hw() {
52     rmmod_can
53     modprobe kvaser_pci
54     echo 1000000 > /sys/class/net/can0/can_bittiming/bitrate
55     ip link set up dev can0
56     echo 1000000 > /sys/class/net/can1/can_bittiming/bitrate
57     ip link set up dev can1
58     echo 1000000 > /sys/class/net/can2/can_bittiming/bitrate
59     ip link set up dev can2
60     echo 1000000 > /sys/class/net/can3/can_bittiming/bitrate
61     ip link set up dev can3
62     CAN0=socketcan:can0
63     CAN1=socketcan:can1
64     DRIVER=socketcan
65 }
66
67 prepare_socketcan_virtual() {
68     rmmod_can
69     modprobe vcan
70     ip link add dev vcan0 type vcan
71     ip link set up dev vcan0
72     CAN0=socketcan:vcan0
73     DRIVER=socketcan-virtual
74 }
75
76 run_tests() {
77     tests=${TESTS:-socketcan_hw lincan_hw}
78     for tst in $tests; do
79         boost_irq_prio 50 || true
80         prepare_$tst
81         echo "----------------------"
82         t
83     done
84 }
85
86 plot() {
87     for i in "${!PLOT_CMD[@]}"; do
88         cmd="${PLOT_CMD[$i]}";
89         cmd=$(echo "$cmd" | sed -e "/set title/ s/[\"']\(.*\)[\"']/'\1 ($(uname -r), CPU\\\\@${clk}Mhz)'/")
90         if [ -z "$OPT_NO_X11" ]; then
91             echo "set terminal x11 enhanced; ${cmd}" | gnuplot -persist
92         fi
93         I=${i/0/}
94         echo "set terminal postscript color eps enhanced;
95               ${cmd}" | gnuplot | epstopdf --filter > `basename $0 .sh`$I.pdf
96         mkdir -p thumb
97         convert -geometry 1000x100 -gamma 0.3 `basename $0 .sh`$I.pdf thumb/`basename $0 .sh`$I.png
98         convert -density 200 -gamma 0.7 -geometry 1000x500 `basename $0 .sh`$I.pdf `basename $0 .sh`$I.png
99     done
100 }
101
102 create_dirs_and_links() {
103     local test=$1
104     local kver=$2
105     local clck=$3
106
107     local  d=results/by-test/$test/$kver/$clck
108     mkdir -p $d
109     mkdir -p results/by-test/$test/$clck # 1
110     mkdir -p results/by-kern/$kver/$clck # 2
111     mkdir -p results/by-kern/$kver/$test # 3
112     mkdir -p results/by-clck/$clck/$kver # 4
113     mkdir -p results/by-clck/$clck/$test # 5
114     ln -sfT ../../../${d#results/} results/by-test/$test/$clck/$kver # 1
115     ln -sfT ../../../${d#results/} results/by-kern/$kver/$clck/$test # 2
116     ln -sfT ../../../${d#results/} results/by-kern/$kver/$test/$clck # 3
117     ln -sfT ../../../${d#results/} results/by-clck/$clck/$kver/$test # 4
118     ln -sfT ../../../${d#results/} results/by-clck/$clck/$test/$kver # 5
119     echo $d
120 }
121
122 go() {
123     echo "####################"
124     mhz=${MHZ:-2400 300}
125     for clk in $mhz; do
126     echo "===================="
127         dir=$(create_dirs_and_links `basename $0 .sh` `uname -r` $clk)
128         set -x
129         pushd $dir
130
131         if [ ! -n "$OPT_PLOT_ONLY" ]; then
132             cpufreq-set -f ${clk}MHz
133             run_tests
134             cpufreq-set -f 2400MHz
135         fi
136         plot
137         popd
138         set +x
139     done
140
141 }
142
143 trap 'go' EXIT