#!/bin/sh # kernbench by Con Kolivas # based on a benchmark by Martin J. Bligh trap 'echo "ABORTING";exit' 1 2 15 VERSION=0.30 num_runs=5 single_runs=0 half_runs=1 opti_runs=1 max_runs=1 fast_run=0 while getopts vsHOMn:o:hf i do case $i in h) echo "kernbench v$VERSION by Con Kolivas " echo "Usage:" echo "kernbench [-n runs] [-o jobs] [-s] [-H] [-O] [-M] [-h] [-v]" echo "n : number of times to perform benchmark (default 5)" echo "o : number of jobs for optimal run (default 4 * cpu)" echo "s : perform single threaded runs (default don't)" echo "H : don't perform half load runs (default do)" echo "O : don't perform optimal load runs (default do)" echo "M : don't perform maximal load runs (default do)" echo "f : fast run" echo "h : print this help" echo "v : print version number" exit ;; v) echo "kernbench Version $VERSION by Con Kolivas " ; exit ;; n) nruns=$OPTARG ;; o) optijobs=$OPTARG ;; s) single_runs=1 ;; H) half_runs=0 ;; O) opti_runs=0 ;; M) max_runs=0 ;; f) fast_run=1 ;; esac done if [[ ! -f include/linux/kernel.h ]] ; then echo "No kernel source found; exiting" exit fi time=`which time` if [[ ! -a $time ]] ; then echo "time not found in path, please install it; exiting" exit fi if [[ $nruns -gt 0 ]] ; then num_runs=$nruns elif [[ $fast_run -eq 1 ]]; then echo "Dropping to 3 runs for fast run" num_runs=3 fi if (($num_runs < 1)) ; then echo "Nothing to do; exiting" exit fi if (($num_runs > 10)) ; then echo "Trimming number of runs to 10" num_runs=10 fi if [[ ! -d /proc ]] ; then echo "Can't find proc filesystem; exiting" exit fi awk=`which awk` if [[ ! -a $awk ]] ; then echo "awk not found in path, please install it; exiting" exit fi mem=`awk '/MemTotal/ {print $2}' /proc/meminfo` if [[ $mem -lt 2000000 && $max_runs -gt 0 ]] ; then echo Less than 2Gb ram detected! echo Maximal loads will not measure cpu throughput and may cause a swapstorm! echo If you did not plan this, -M flag is recommended to bypass maximal load. fi (( single_runs *= $num_runs )) (( half_runs *= $num_runs )) (( opti_runs *= $num_runs )) (( max_runs *= $num_runs )) cpus=`grep -c processor /proc/cpuinfo` echo $cpus cpus found echo Cleaning source tree... make clean > /dev/null 2>&1 if [[ $fast_run -eq 0 ]] ; then make mrproper > /dev/null 2>&1 echo Making defconfig... make defconfig > /dev/null 2>&1 echo Making dep if needed... make dep > /dev/null 2>&1 echo Caching kernel source in ram... for i in `find -type f` do cat $i > /dev/null done fi halfjobs=$(( $cpus / 2 )) optijobs=${optijobs:=$(( $cpus * 4 ))} if [[ $halfjobs -lt 2 ]] ; then echo "Half load is no greater than single; disabling" half_runs=0 elif [[ $halfjobs -eq 2 ]] ; then echo "Half load is 2 jobs, changing to 3 as a kernel compile won't guarantee 2 jobs" halfjobs=3 fi echo echo Performing $num_runs runs of if [[ $single_runs -gt 0 ]] ; then echo make fi if [[ $half_runs -gt 0 ]] ; then echo make -j $halfjobs fi if [[ $opti_runs -gt 0 ]] ; then echo make -j $optijobs fi if [[ $max_runs -gt 0 ]] ; then echo make -j fi echo if [[ ! $single_runs -eq 0 && $fast_run -eq 0 ]] ; then echo Warmup single threaded run... make > /dev/null 2>&1 fi temp_runs=$single_runs single_elapsed="a" for (( i=1 ; i <= temp_runs ; i++ )) do echo Single threaded run number $i... make clean > /dev/null 2>&1 sync if [[ $fast_run -eq 0 ]] ; then sleep 5 fi $time -f "%e %U %S %P %c %w" -o timelog make > /dev/null 2>&1 read elapsed_time user_time sys_time percent ctx sleeps /dev/null 2>&1 fi temp_runs=$half_runs half_elapsed="a" for (( i=1 ; i <= temp_runs ; i++ )) do echo Half load -j$halfjobs run number $i... make clean > /dev/null 2>&1 sync if [[ $fast_run -eq 0 ]] ; then sleep 5 fi $time -f "%e %U %S %P %c %w" -o timelog make -j $halfjobs> /dev/null 2>&1 read elapsed_time user_time sys_time percent ctx sleeps /dev/null 2>&1 fi temp_runs=$opti_runs opti_elapsed="a" for (( i=1 ; i <= temp_runs ; i++ )) do echo Optimal load -j$optijobs run number $i... make clean > /dev/null 2>&1 sync if [[ $fast_run -eq 0 ]] ; then sleep 5 fi $time -f "%e %U %S %P %c %w" -o timelog make -j $optijobs > /dev/null 2>&1 read elapsed_time user_time sys_time percent ctx sleeps /dev/null 2>&1 fi temp_runs=$max_runs max_elapsed="a" for (( i=1 ; i <= temp_runs ; i++ )) do echo Maximal load -j run number $i... make clean > /dev/null 2>&1 sync if [[ $fast_run -eq 0 ]] ; then sleep 5 fi $time -f "%e %U %S %P %c %w" -o timelog make -j > /dev/null 2>&1 read elapsed_time user_time sys_time percent ctx sleeps