kernbench 5.3 KB

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