Small fixes in graphs
[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         boost_irq_prio 50 || true
83         prepare_$tst
84         echo "----------------------"
85         t
86     done
87 }
88
89 kvers() {
90     local cmdline=$(cat /proc/cmdline|sed -e 's/BOOT_IMAGE=[^ ]*//' \
91                                           -e 's/root=[^ ]*//' \
92                                           -e 's/ro//' \
93                                           -e 's/quiet//' \
94                                           -e 's/rootfstype=[^ ]*//' \
95                                           -e 's/elevator=[^ ]*//' \
96                                           -e 's/^ *//' \
97                                           -e 's/ *$//'|tr -s ' ' '_')
98     v=$(uname -r)
99     [ -n "$cmdline" ] && v="$v:$cmdline"
100     echo $v
101 }
102
103 plot() {
104 #    for i in "${!PLOT_CMD[@]}"; do
105     for i in 0; do
106         cmd=`p`                 # Get plot commands
107         cmd=$(echo "$cmd" | sed -e "/set title/ s/[\"']\(.*\)[\"']/\"\1\\\\n($(kvers), CPU\\\\\\\\@${clk}Mhz)\"/")
108         if [ -z "$OPT_NO_X11" ]; then
109             echo "set terminal x11 enhanced; ${cmd}" | gnuplot -persist
110         fi
111         I=${i/0/}
112         echo "set terminal postscript color eps enhanced;
113               ${cmd}" | gnuplot | epstopdf --filter > `basename $0 .sh`$I.pdf
114         mkdir -p thumb
115         convert -geometry 1000x100 -gamma 0.3 `basename $0 .sh`$I.pdf thumb/`basename $0 .sh`$I.png
116         convert -density 200 -gamma 0.7 -geometry 1000x500 `basename $0 .sh`$I.pdf `basename $0 .sh`$I.png
117     done
118 }
119
120 create_dirs_and_links() {
121     local test=$1
122     local kver=$2
123     local clck=$3
124
125     local  d=results/by-kern/$kver/$clck/$test
126     mkdir -p $d
127     mkdir -p results/by-kern/$kver/$test # 1
128     mkdir -p results/by-test/$test/$kver # 2
129     mkdir -p results/by-test/$test/$clck # 3
130     mkdir -p results/by-clck/$clck/$kver # 4
131     mkdir -p results/by-clck/$clck/$test # 5
132     ln -sfT ../../../${d#results/} results/by-kern/$kver/$test/$clck # 1
133     ln -sfT ../../../${d#results/} results/by-test/$test/$kver/$clck # 2
134     ln -sfT ../../../${d#results/} results/by-test/$test/$clck/$kver # 3
135     ln -sfT ../../../${d#results/} results/by-clck/$clck/$kver/$test # 4
136     ln -sfT ../../../${d#results/} results/by-clck/$clck/$test/$kver # 5
137     echo $d
138 }
139
140 go() {
141     echo "####################"
142     mhz=`sed -ne '/^cpu MHz/ s/.*:\([^.]+\).*/\1/p' /proc/cpuinfo|head -n 1`
143     if [ -r /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq ]; then
144         local max=$((`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`/1000))
145         local min=$((`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`/1000))
146         mhz="$max $min"
147     fi
148     for clk in $mhz; do
149     echo "===================="
150         dir=$(create_dirs_and_links `basename $0 .sh` `kvers` $clk)
151         set -x
152         pushd $dir
153
154         if [ ! -n "$OPT_PLOT_ONLY" ]; then
155             cpufreq-set -f ${clk}MHz
156             run_tests
157             cpufreq-set -f 2400MHz
158         fi
159         plot
160         popd
161         set +x
162     done
163
164 }
165
166 trap 'go' EXIT