xref: /aosp_15_r20/external/ltp/utils/benchmark/kernbench-0.42/kernbench (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker#!/bin/bash
2*49cdfc7eSAndroid Build Coastguard Worker# kernbench by Con Kolivas <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker# based on a benchmark by Martin J. Bligh
4*49cdfc7eSAndroid Build Coastguard Workertrap 'echo "ABORTING";exit' 1 2 15
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard WorkerVERSION=0.42
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Workernum_runs=5
9*49cdfc7eSAndroid Build Coastguard Workersingle_runs=0
10*49cdfc7eSAndroid Build Coastguard Workerhalf_runs=1
11*49cdfc7eSAndroid Build Coastguard Workeropti_runs=1
12*49cdfc7eSAndroid Build Coastguard Workermax_runs=1
13*49cdfc7eSAndroid Build Coastguard Workerfast_run=0
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Workerwhile getopts vsHOMn:o:hf i
16*49cdfc7eSAndroid Build Coastguard Workerdo
17*49cdfc7eSAndroid Build Coastguard Worker	case $i in
18*49cdfc7eSAndroid Build Coastguard Worker		 h) 	echo "kernbench v$VERSION by Con Kolivas <[email protected]>"
19*49cdfc7eSAndroid Build Coastguard Worker				echo "Usage:"
20*49cdfc7eSAndroid Build Coastguard Worker				echo "kernbench [-n runs] [-o jobs] [-s] [-H] [-O] [-M] [-h] [-v]"
21*49cdfc7eSAndroid Build Coastguard Worker				echo "n : number of times to perform benchmark (default 5)"
22*49cdfc7eSAndroid Build Coastguard Worker				echo "o : number of jobs for optimal run (default 4 * cpu)"
23*49cdfc7eSAndroid Build Coastguard Worker				echo "s : perform single threaded runs (default don't)"
24*49cdfc7eSAndroid Build Coastguard Worker				echo "H : don't perform half load runs (default do)"
25*49cdfc7eSAndroid Build Coastguard Worker				echo "O : don't perform optimal load runs (default do)"
26*49cdfc7eSAndroid Build Coastguard Worker				echo "M : don't perform maximal load runs (default do)"
27*49cdfc7eSAndroid Build Coastguard Worker				echo "f : fast run"
28*49cdfc7eSAndroid Build Coastguard Worker				echo "h : print this help"
29*49cdfc7eSAndroid Build Coastguard Worker				echo "v : print version number"
30*49cdfc7eSAndroid Build Coastguard Worker				exit ;;
31*49cdfc7eSAndroid Build Coastguard Worker		 v) echo "kernbench Version $VERSION by Con Kolivas <[email protected]>" ; exit ;;
32*49cdfc7eSAndroid Build Coastguard Worker		 n) nruns=$OPTARG ;;
33*49cdfc7eSAndroid Build Coastguard Worker		 o) optijobs=$OPTARG ;;
34*49cdfc7eSAndroid Build Coastguard Worker		 s) single_runs=1 ;;
35*49cdfc7eSAndroid Build Coastguard Worker		 H) half_runs=0 ;;
36*49cdfc7eSAndroid Build Coastguard Worker		 O) opti_runs=0 ;;
37*49cdfc7eSAndroid Build Coastguard Worker		 M) max_runs=0 ;;
38*49cdfc7eSAndroid Build Coastguard Worker		 f) fast_run=1 ;;
39*49cdfc7eSAndroid Build Coastguard Worker	esac
40*49cdfc7eSAndroid Build Coastguard Workerdone
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Workerif [[ ! -f include/linux/kernel.h ]] ; then
43*49cdfc7eSAndroid Build Coastguard Worker	echo "No kernel source found; exiting"
44*49cdfc7eSAndroid Build Coastguard Worker	exit
45*49cdfc7eSAndroid Build Coastguard Workerfi
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Workerfor i in time awk yes date
48*49cdfc7eSAndroid Build Coastguard Workerdo
49*49cdfc7eSAndroid Build Coastguard Worker	iname=`which $i`
50*49cdfc7eSAndroid Build Coastguard Worker	if [[ ! -a $iname ]] ; then
51*49cdfc7eSAndroid Build Coastguard Worker		echo "$i not found in path, please install it; exiting"
52*49cdfc7eSAndroid Build Coastguard Worker		exit
53*49cdfc7eSAndroid Build Coastguard Worker	fi
54*49cdfc7eSAndroid Build Coastguard Workerdone
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Workertime=`which time`
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Workerif [[ $nruns -gt 0 ]] ; then
59*49cdfc7eSAndroid Build Coastguard Worker	num_runs=$nruns
60*49cdfc7eSAndroid Build Coastguard Workerelif [[ $fast_run -eq 1 ]]; then
61*49cdfc7eSAndroid Build Coastguard Worker	echo "Dropping to 3 runs for fast run"
62*49cdfc7eSAndroid Build Coastguard Worker	num_runs=3
63*49cdfc7eSAndroid Build Coastguard Workerfi
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Workerif (($num_runs < 1)) ; then
66*49cdfc7eSAndroid Build Coastguard Worker	echo "Nothing to do; exiting"
67*49cdfc7eSAndroid Build Coastguard Worker	exit
68*49cdfc7eSAndroid Build Coastguard Workerfi
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Workerif (($num_runs > 10)) ; then
71*49cdfc7eSAndroid Build Coastguard Worker	echo "Are you crazy? trimming number of runs to 10"
72*49cdfc7eSAndroid Build Coastguard Worker	num_runs=10
73*49cdfc7eSAndroid Build Coastguard Workerfi
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Workerif [[ ! -d /proc ]] ; then
76*49cdfc7eSAndroid Build Coastguard Worker	echo "Can't find proc filesystem; exiting"
77*49cdfc7eSAndroid Build Coastguard Worker	exit
78*49cdfc7eSAndroid Build Coastguard Workerfi
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Workermem=`awk '/MemTotal/ {print $2}' /proc/meminfo`
81*49cdfc7eSAndroid Build Coastguard Workerif [[ $mem -lt 4000000 && $max_runs -gt 0 ]] ; then
82*49cdfc7eSAndroid Build Coastguard Worker	echo Less than 4Gb ram detected!
83*49cdfc7eSAndroid Build Coastguard Worker	echo Maximal loads will not measure cpu throughput and may cause a swapstorm!
84*49cdfc7eSAndroid Build Coastguard Worker	echo If you did not plan this, -M flag is recommended to bypass maximal load.
85*49cdfc7eSAndroid Build Coastguard Workerfi
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker(( single_runs *= $num_runs ))
88*49cdfc7eSAndroid Build Coastguard Worker(( half_runs *= $num_runs ))
89*49cdfc7eSAndroid Build Coastguard Worker(( opti_runs *= $num_runs ))
90*49cdfc7eSAndroid Build Coastguard Worker(( max_runs *= $num_runs ))
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Workercpus=`grep -c ^processor /proc/cpuinfo`
93*49cdfc7eSAndroid Build Coastguard Workerecho $cpus cpus found
94*49cdfc7eSAndroid Build Coastguard Workerecho Cleaning source tree...
95*49cdfc7eSAndroid Build Coastguard Workermake clean > /dev/null 2>&1
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Workerif [[ $fast_run -eq 0 ]] ; then
98*49cdfc7eSAndroid Build Coastguard Worker	echo Caching kernel source in ram...
99*49cdfc7eSAndroid Build Coastguard Worker	for i in `find -type f`
100*49cdfc7eSAndroid Build Coastguard Worker	do
101*49cdfc7eSAndroid Build Coastguard Worker		cat $i > /dev/null
102*49cdfc7eSAndroid Build Coastguard Worker	done
103*49cdfc7eSAndroid Build Coastguard Workerfi
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Workerif [[ ! -f .config ]] ; then
106*49cdfc7eSAndroid Build Coastguard Worker	echo No old config found, using defconfig
107*49cdfc7eSAndroid Build Coastguard Worker	echo Making mrproper
108*49cdfc7eSAndroid Build Coastguard Worker	make mrproper > /dev/null 2>&1
109*49cdfc7eSAndroid Build Coastguard Worker	echo Making defconfig...
110*49cdfc7eSAndroid Build Coastguard Worker	make defconfig > /dev/null 2>&1
111*49cdfc7eSAndroid Build Coastguard Workerelse
112*49cdfc7eSAndroid Build Coastguard Worker	echo Making oldconfig...
113*49cdfc7eSAndroid Build Coastguard Worker	yes "" | make oldconfig > /dev/null 2>&1
114*49cdfc7eSAndroid Build Coastguard Workerfi
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Workerhalfjobs=$(( $cpus / 2 ))
117*49cdfc7eSAndroid Build Coastguard Workeroptijobs=${optijobs:=$(( $cpus * 4 ))}
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Workerif [[ $halfjobs -lt 2 ]] ; then
120*49cdfc7eSAndroid Build Coastguard Worker	echo "Half load is no greater than single; disabling"
121*49cdfc7eSAndroid Build Coastguard Worker	half_runs=0
122*49cdfc7eSAndroid Build Coastguard Workerelif [[ $halfjobs -eq 2 ]] ; then
123*49cdfc7eSAndroid Build Coastguard Worker	echo "Half load is 2 jobs, changing to 3 as a kernel compile won't guarantee 2 jobs"
124*49cdfc7eSAndroid Build Coastguard Worker	halfjobs=3
125*49cdfc7eSAndroid Build Coastguard Workerfi
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Workerecho Kernel `uname -r`
128*49cdfc7eSAndroid Build Coastguard Workerecho Performing $num_runs runs of
129*49cdfc7eSAndroid Build Coastguard Workerif [[ $single_runs -gt 0 ]] ; then
130*49cdfc7eSAndroid Build Coastguard Worker	echo make
131*49cdfc7eSAndroid Build Coastguard Workerfi
132*49cdfc7eSAndroid Build Coastguard Workerif [[ $half_runs -gt 0 ]] ; then
133*49cdfc7eSAndroid Build Coastguard Worker	echo make -j $halfjobs
134*49cdfc7eSAndroid Build Coastguard Workerfi
135*49cdfc7eSAndroid Build Coastguard Workerif [[ $opti_runs -gt 0 ]] ; then
136*49cdfc7eSAndroid Build Coastguard Worker	echo make -j $optijobs
137*49cdfc7eSAndroid Build Coastguard Workerfi
138*49cdfc7eSAndroid Build Coastguard Workerif [[ $max_runs -gt 0 ]] ; then
139*49cdfc7eSAndroid Build Coastguard Worker	echo make -j
140*49cdfc7eSAndroid Build Coastguard Workerfi
141*49cdfc7eSAndroid Build Coastguard Workerecho
142*49cdfc7eSAndroid Build Coastguard Worker
143*49cdfc7eSAndroid Build Coastguard Workerecho All data logged to kernbench.log
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Workerif [[ $fast_run -eq 0 ]] ; then
146*49cdfc7eSAndroid Build Coastguard Worker	echo Warmup run...
147*49cdfc7eSAndroid Build Coastguard Worker	make -j $optijobs > /dev/null 2>&1
148*49cdfc7eSAndroid Build Coastguard Workerfi
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Workerdate >> kernbench.log
151*49cdfc7eSAndroid Build Coastguard Workeruname -r >> kernbench.log
152*49cdfc7eSAndroid Build Coastguard Worker
153*49cdfc7eSAndroid Build Coastguard Workeradd_data_point()
154*49cdfc7eSAndroid Build Coastguard Worker{
155*49cdfc7eSAndroid Build Coastguard Worker    echo $@ | awk '{printf "%.6f %.6f %d", $1 + $2, $1 * $1 + $3, $4 + 1}'
156*49cdfc7eSAndroid Build Coastguard Worker}
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Workershow_statistics()
159*49cdfc7eSAndroid Build Coastguard Worker{
160*49cdfc7eSAndroid Build Coastguard Worker    case $3 in
161*49cdfc7eSAndroid Build Coastguard Worker	0)
162*49cdfc7eSAndroid Build Coastguard Worker	    echo "No data"
163*49cdfc7eSAndroid Build Coastguard Worker	    ;;
164*49cdfc7eSAndroid Build Coastguard Worker	1)
165*49cdfc7eSAndroid Build Coastguard Worker	    echo $1
166*49cdfc7eSAndroid Build Coastguard Worker	    ;;
167*49cdfc7eSAndroid Build Coastguard Worker	*)
168*49cdfc7eSAndroid Build Coastguard Worker	    avg=`echo $1 $3 | awk '{print $1 / $2}'`
169*49cdfc7eSAndroid Build Coastguard Worker	    var=`echo $1 $2 $3 | awk '{print ($2 - ($1 * $1) / $3) / ($3 - 1)}'`
170*49cdfc7eSAndroid Build Coastguard Worker	    sdev=`echo $var | awk '{print $1^0.5}'`
171*49cdfc7eSAndroid Build Coastguard Worker	    echo "$avg ($sdev)"
172*49cdfc7eSAndroid Build Coastguard Worker	    ;;
173*49cdfc7eSAndroid Build Coastguard Worker    esac
174*49cdfc7eSAndroid Build Coastguard Worker}
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Workerdo_log()
177*49cdfc7eSAndroid Build Coastguard Worker{
178*49cdfc7eSAndroid Build Coastguard Worker	echo "Average $runname Run (std deviation):" > templog
179*49cdfc7eSAndroid Build Coastguard Worker	echo Elapsed Time  `show_statistics $temp_elapsed` >> templog
180*49cdfc7eSAndroid Build Coastguard Worker	echo User Time  `show_statistics $temp_user` >> templog
181*49cdfc7eSAndroid Build Coastguard Worker	echo System Time  `show_statistics $temp_sys` >> templog
182*49cdfc7eSAndroid Build Coastguard Worker	echo Percent CPU  `show_statistics $temp_percent` >> templog
183*49cdfc7eSAndroid Build Coastguard Worker	echo Context Switches  `show_statistics $temp_ctx` >> templog
184*49cdfc7eSAndroid Build Coastguard Worker	echo Sleeps  `show_statistics $temp_sleeps` >> templog
185*49cdfc7eSAndroid Build Coastguard Worker	echo >> templog
186*49cdfc7eSAndroid Build Coastguard Worker	cat templog
187*49cdfc7eSAndroid Build Coastguard Worker	cat templog >> kernbench.log
188*49cdfc7eSAndroid Build Coastguard Worker}
189*49cdfc7eSAndroid Build Coastguard Worker
190*49cdfc7eSAndroid Build Coastguard Workerdo_runs()
191*49cdfc7eSAndroid Build Coastguard Worker{
192*49cdfc7eSAndroid Build Coastguard Worker	temp_elapsed="a"
193*49cdfc7eSAndroid Build Coastguard Worker	for (( i=1 ; i <= temp_runs ; i++ ))
194*49cdfc7eSAndroid Build Coastguard Worker	do
195*49cdfc7eSAndroid Build Coastguard Worker		echo $runname run number $i...
196*49cdfc7eSAndroid Build Coastguard Worker		make clean > /dev/null 2>&1
197*49cdfc7eSAndroid Build Coastguard Worker		sync
198*49cdfc7eSAndroid Build Coastguard Worker		if [[ $fast_run -eq 0 ]] ; then
199*49cdfc7eSAndroid Build Coastguard Worker			sleep 5
200*49cdfc7eSAndroid Build Coastguard Worker		fi
201*49cdfc7eSAndroid Build Coastguard Worker		$time -f "%e %U %S %P %c %w" -o timelog make -j $tempjobs > /dev/null 2>&1
202*49cdfc7eSAndroid Build Coastguard Worker		read elapsed_time user_time sys_time percent ctx sleeps <timelog
203*49cdfc7eSAndroid Build Coastguard Worker		temp_elapsed=`add_data_point $elapsed_time $temp_elapsed`
204*49cdfc7eSAndroid Build Coastguard Worker		temp_user=`add_data_point $user_time $temp_user`
205*49cdfc7eSAndroid Build Coastguard Worker		temp_sys=`add_data_point $sys_time $temp_sys`
206*49cdfc7eSAndroid Build Coastguard Worker		temp_percent=`add_data_point $percent $temp_percent`
207*49cdfc7eSAndroid Build Coastguard Worker		temp_ctx=`add_data_point $ctx $temp_ctx`
208*49cdfc7eSAndroid Build Coastguard Worker		temp_sleeps=`add_data_point $sleeps $temp_sleeps`
209*49cdfc7eSAndroid Build Coastguard Worker	done
210*49cdfc7eSAndroid Build Coastguard Worker	if [[ $temp_runs -ne 0 ]] ; then
211*49cdfc7eSAndroid Build Coastguard Worker		do_log
212*49cdfc7eSAndroid Build Coastguard Worker	fi
213*49cdfc7eSAndroid Build Coastguard Worker}
214*49cdfc7eSAndroid Build Coastguard Worker
215*49cdfc7eSAndroid Build Coastguard Workertemp_runs=$single_runs
216*49cdfc7eSAndroid Build Coastguard Workertempjobs=1
217*49cdfc7eSAndroid Build Coastguard Workerrunname="Single threaded"
218*49cdfc7eSAndroid Build Coastguard Workerdo_runs
219*49cdfc7eSAndroid Build Coastguard Worker
220*49cdfc7eSAndroid Build Coastguard Workertemp_runs=$half_runs
221*49cdfc7eSAndroid Build Coastguard Workertempjobs=$halfjobs
222*49cdfc7eSAndroid Build Coastguard Workerrunname="Half load -j $halfjobs"
223*49cdfc7eSAndroid Build Coastguard Workerdo_runs
224*49cdfc7eSAndroid Build Coastguard Worker
225*49cdfc7eSAndroid Build Coastguard Workertemp_runs=$opti_runs
226*49cdfc7eSAndroid Build Coastguard Workertempjobs=$optijobs
227*49cdfc7eSAndroid Build Coastguard Workerrunname="Optimal load -j $optijobs"
228*49cdfc7eSAndroid Build Coastguard Workerdo_runs
229*49cdfc7eSAndroid Build Coastguard Worker
230*49cdfc7eSAndroid Build Coastguard Workertemp_runs=$max_runs
231*49cdfc7eSAndroid Build Coastguard Workertempjobs=""
232*49cdfc7eSAndroid Build Coastguard Workerrunname="Maximal load -j"
233*49cdfc7eSAndroid Build Coastguard Workerdo_runs
234