3f9ef7c20ffcea288918e1dea61e81bc811b653c
[can-benchmark.git] / tests / _lib.sh
1
2 while [ $# -gt 0 ]; do
3     case "$1" in
4         -P) OPT_PLOT_DISABLE=1; shift;;
5         -p) OPT_PLOT_ONLY=1; shift;;
6         -X|--no-x11-plot) OPT_NO_X11=1; shift;;
7     esac
8 done
9
10 #set -x
11 set -e
12
13 PATH=$PWD/../_compiled/bin/:$PATH
14
15 RTPRIO=${RTPRIO:-60}
16 COUNT=${COUNT:-10000}
17 export CANPING_MS=1             # Generate plots in milliseconds
18
19 boost_irq_prio() {
20     local PRIO=${1:-50}
21     CAN_IRQ=`sed -ne '/can/ s/:.*//p' /proc/interrupts`
22     if [ -n "$CAN_IRQ" ]; then
23         IRQ_PID=`pidof IRQ-${CAN_IRQ/ /}` || true
24         if [ -n "$IRQ_PID" ]; then
25             schedtool -F -p $PRIO $IRQ_PID
26             return 0
27         fi
28     fi
29     return 1
30 }
31
32 rmmod_can() {
33     modprobe -r lincan kvaser_pci vcan
34 }
35
36 prepare_lincan_hw() {
37     rmmod_can
38     modprobe -C /dev/null lincan hw=pcican-q io=1 baudrate=1000,1000,1000,1000 processlocal=0
39     CAN0=/dev/can0
40     CAN1=/dev/can1
41     DRIVER=lincan
42     sleep 0.1
43 }
44
45 prepare_lincan_virtual() {
46     rmmod_can
47     modprobe -C /dev/null lincan hw=virtual io=0 baudrate=0
48     CAN0=/dev/can0
49     CAN1=/dev/can0
50     DRIVER=lincan-virtual
51     sleep 0.1
52 }
53
54 socketcan_set_bitrate() {
55     local canX=$1
56     if [ -f /sys/class/net/$canX/can_bittiming/bitrate ]; then
57         echo 1000000 > /sys/class/net/$canX/can_bittiming/bitrate
58     else
59         ip link set $canX type can bitrate 1000000
60     fi
61 }
62 prepare_socketcan_hw() {
63     rmmod_can
64     modprobe kvaser_pci
65     socketcan_set_bitrate can0
66     ip link set up dev can0
67     socketcan_set_bitrate can1
68     ip link set up dev can1 
69     socketcan_set_bitrate can2
70     ip link set up dev can2
71     socketcan_set_bitrate can3
72     ip link set up dev can3
73     CAN0=socketcan:can0
74     CAN1=socketcan:can1
75     DRIVER=socketcan
76 }
77
78 prepare_socketcan_virtual() {
79     rmmod_can
80     modprobe vcan
81     ip link add dev vcan0 type vcan
82     ip link set up dev vcan0
83     CAN0=socketcan:vcan0
84     CAN1=socketcan:vcan0
85     DRIVER=socketcan-virtual
86 }
87
88 run_tests() {
89     tests=${TESTS:-socketcan_hw lincan_hw}
90     for tst in $tests; do
91         echo 300 > /proc/sys/net/core/netdev_budget
92         boost_irq_prio 50 || true
93         prepare_$tst
94         echo "----------------------"
95         t
96         echo 300 > /proc/sys/net/core/netdev_budget
97     done
98 }
99
100 kvers() {
101     local cmdline=$(cat /proc/cmdline|sed -e 's/BOOT_IMAGE=[^ ]*//' \
102                                           -e 's/root=[^ ]*//' \
103                                           -e 's/ro//' \
104                                           -e 's/quiet//' \
105                                           -e 's/rootfstype=[^ ]*//' \
106                                           -e 's/elevator=[^ ]*//' \
107                                           -e 's/^ *//' \
108                                           -e 's/ *$//'|tr -s ' ' '_')
109     v=$(uname -r)
110     [ -n "$cmdline" ] && v="$v:$cmdline"
111     [ -n "$KVERS" ] && v=$KVERS
112     echo $v
113 }
114
115 plot() {
116 #    for i in "${!PLOT_CMD[@]}"; do
117     for i in 0; do
118         cmd=`p`                 # Get plot commands
119         cmd=$(echo "$cmd" | sed -e "/set title/ s/[\"']\(.*\)[\"']/\"\1\\\\n($(kvers), CPU\\\\\\\\@${clk}Mhz)\"/")
120         if [ -z "$OPT_NO_X11" ]; then
121             echo "set terminal x11 enhanced; ${cmd}" | gnuplot -persist
122         fi
123         I=${i/0/}
124         echo "set terminal postscript color eps enhanced;
125               ${cmd}" | gnuplot | epstopdf --filter > `basename $0 .sh`$I.pdf
126         mkdir -p thumb
127         convert -density 30  -gamma 0.5 -quality 90 -type Palette -depth 8 `basename $0 .sh`$I.pdf thumb/`basename $0 .sh`$I.png
128         convert -density 150 -gamma 0.7 -quality 90 -type Palette -depth 8 `basename $0 .sh`$I.pdf `basename $0 .sh`$I.png
129     done
130 }
131
132 create_dirs_and_links() {
133     local test=$1
134     local kver=$2
135     local clck=$3
136
137     local  d=results/by-kern/$kver/$clck/$test
138     mkdir -p $d
139     mkdir -p results/by-kern/$kver/$test # 1
140     mkdir -p results/by-test/$test/$kver # 2
141     mkdir -p results/by-test/$test/$clck # 3
142     mkdir -p results/by-clck/$clck/$kver # 4
143     mkdir -p results/by-clck/$clck/$test # 5
144     ln -sfT ../../../${d#results/} results/by-kern/$kver/$test/$clck # 1
145     ln -sfT ../../../${d#results/} results/by-test/$test/$kver/$clck # 2
146     ln -sfT ../../../${d#results/} results/by-test/$test/$clck/$kver # 3
147     ln -sfT ../../../${d#results/} results/by-clck/$clck/$kver/$test # 4
148     ln -sfT ../../../${d#results/} results/by-clck/$clck/$test/$kver # 5
149     echo $d
150 }
151
152 go() {
153     echo "####################"
154     mhz=`sed -ne '/^cpu MHz/ s/.*:\([^.]+\).*/\1/p' /proc/cpuinfo|head -n 1`
155     if [ -r /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq ]; then
156         local max=$((`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`/1000))
157         local min=$((`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`/1000))
158         mhz="$max $min"
159     fi
160     for clk in $mhz; do
161     echo "===================="
162         dir=$(create_dirs_and_links `basename $0 .sh` `kvers` $clk)
163         set -x
164         pushd $dir
165
166         if [ ! -n "$OPT_PLOT_ONLY" ]; then
167             cpufreq-set -f ${clk}MHz
168             run_tests
169             cpufreq-set -f ${mhz}MHz
170         else
171             unset OPT_PLOT_DISABLE
172         fi
173         if [ ! -n "$OPT_PLOT_DISABLE" ]; then
174             plot
175         fi
176         popd
177         set +x
178     done
179
180 }
181
182 trap 'go' EXIT