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