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