Convert plot commands to be returned by function p()
[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     for i in 0; do
105         cmd=`p`                 # Get plot commands
106         cmd=$(echo "$cmd" | sed -e "/set title/ s/[\"']\(.*\)[\"']/'\1 ($(kvers), CPU\\\\@${clk}Mhz)'/")
107         if [ -z "$OPT_NO_X11" ]; then
108             echo "set terminal x11 enhanced; ${cmd}" | gnuplot -persist
109         fi
110         I=${i/0/}
111         echo "set terminal postscript color eps enhanced;
112               ${cmd}" | gnuplot | epstopdf --filter > `basename $0 .sh`$I.pdf
113         mkdir -p thumb
114         convert -geometry 1000x100 -gamma 0.3 `basename $0 .sh`$I.pdf thumb/`basename $0 .sh`$I.png
115         convert -density 200 -gamma 0.7 -geometry 1000x500 `basename $0 .sh`$I.pdf `basename $0 .sh`$I.png
116     done
117 }
118
119 create_dirs_and_links() {
120     local test=$1
121     local kver=$2
122     local clck=$3
123
124     local  d=results/by-test/$test/$kver/$clck
125     mkdir -p $d
126     mkdir -p results/by-test/$test/$clck # 1
127     mkdir -p results/by-kern/$kver/$clck # 2
128     mkdir -p results/by-kern/$kver/$test # 3
129     mkdir -p results/by-clck/$clck/$kver # 4
130     mkdir -p results/by-clck/$clck/$test # 5
131     ln -sfT ../../../${d#results/} results/by-test/$test/$clck/$kver # 1
132     ln -sfT ../../../${d#results/} results/by-kern/$kver/$clck/$test # 2
133     ln -sfT ../../../${d#results/} results/by-kern/$kver/$test/$clck # 3
134     ln -sfT ../../../${d#results/} results/by-clck/$clck/$kver/$test # 4
135     ln -sfT ../../../${d#results/} results/by-clck/$clck/$test/$kver # 5
136     echo $d
137 }
138
139 go() {
140     echo "####################"
141     mhz=`sed -ne '/^cpu MHz/ s/.*:\([^.]+\).*/\1/p' /proc/cpuinfo|head -n 1`
142     if [ -r /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq ]; then
143         local max=$((`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`/1000))
144         local min=$((`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`/1000))
145         mhz="$max $min"
146     fi
147     for clk in $mhz; do
148     echo "===================="
149         dir=$(create_dirs_and_links `basename $0 .sh` `kvers` $clk)
150         set -x
151         pushd $dir
152
153         if [ ! -n "$OPT_PLOT_ONLY" ]; then
154             cpufreq-set -f ${clk}MHz
155             run_tests
156             cpufreq-set -f 2400MHz
157         fi
158         plot
159         popd
160         set +x
161     done
162
163 }
164
165 trap 'go' EXIT