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