Generate all formats
[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 size 6cm,4cm lw 1 "Times-Roman" 10;' \
125              'set lmargin 8;' \
126               "${cmd}" | gnuplot > ${testname}$I.eps
127         echo "set terminal postscript color eps enhanced;" \
128              "${cmd}" | gnuplot | epstopdf --filter > ${testname}$I.pdf
129         mkdir -p thumb
130         convert -density 30  -gamma 0.5 -quality 90 -type Palette -depth 8 ${testname}$I.pdf thumb/${testname}$I.png
131         convert -density 150 -gamma 0.7 -quality 90 -type Palette -depth 8 ${testname}$I.pdf ${testname}$I.png
132     done
133 }
134
135 create_dirs_and_links() {
136     local test=$1
137     local kver=$2
138     local clck=$3
139
140     local  d=results/by-kern/$kver/$clck/$test
141     mkdir -p $d
142     mkdir -p results/by-kern/$kver/$test # 1
143     mkdir -p results/by-test/$test/$kver # 2
144     mkdir -p results/by-test/$test/$clck # 3
145     mkdir -p results/by-clck/$clck/$kver # 4
146     mkdir -p results/by-clck/$clck/$test # 5
147     ln -sfT ../../../${d#results/} results/by-kern/$kver/$test/$clck # 1
148     ln -sfT ../../../${d#results/} results/by-test/$test/$kver/$clck # 2
149     ln -sfT ../../../${d#results/} results/by-test/$test/$clck/$kver # 3
150     ln -sfT ../../../${d#results/} results/by-clck/$clck/$kver/$test # 4
151     ln -sfT ../../../${d#results/} results/by-clck/$clck/$test/$kver # 5
152     echo $d
153 }
154
155 go() {
156     echo "####################"
157     mhz=`sed -ne '/^cpu MHz/ s/.*:\([^.]+\).*/\1/p' /proc/cpuinfo|head -n 1`
158     if [ -r /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq ]; then
159         local max=$((`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`/1000))
160         local min=$((`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`/1000))
161         mhz="$max $min"
162     fi
163     for clk in $mhz; do
164     echo "===================="
165         dir=$(create_dirs_and_links `basename $0 .sh` `kvers` $clk)
166         set -x
167         pushd $dir
168
169         if [ ! -n "$OPT_PLOT_ONLY" ]; then
170             cpufreq-set -f ${clk}MHz
171             run_tests
172             cpufreq-set -f ${mhz}MHz
173         else
174             unset OPT_PLOT_DISABLE
175         fi
176         if [ ! -n "$OPT_PLOT_DISABLE" ]; then
177             plot
178         fi
179         popd
180         set +x
181     done
182
183 }
184
185 trap 'go' EXIT