340 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.03 4# Testing and monitor the cpu desire performance, frequency, load,5# power consumption and throughput etc.when this script trigger tbench6# test cases.7# 1) Run tbench benchmark on specific governors, ondemand or schedutil.8# 2) Run tbench benchmark comparative test on acpi-cpufreq kernel driver.9# 3) Get desire performance, frequency, load by perf.10# 4) Get power consumption and throughput by amd_pstate_trace.py.11# 5) Analyse test results and save it in file selftest.tbench.csv.12# 6) Plot png images about performance, energy and performance per watt for each test.13 14# protect against multiple inclusion15if [ $FILE_TBENCH ]; then16 return 017else18 FILE_TBENCH=DONE19fi20 21tbench_governors=("ondemand" "schedutil")22 23# $1: governor, $2: round, $3: des-perf, $4: freq, $5: load, $6: performance, $7: energy, $8: performance per watt24store_csv_tbench()25{26 echo "$1, $2, $3, $4, $5, $6, $7, $8" | tee -a $OUTFILE_TBENCH.csv > /dev/null 2>&127}28 29# clear some special lines30clear_csv_tbench()31{32 if [ -f $OUTFILE_TBENCH.csv ]; then33 sed -i '/Comprison(%)/d' $OUTFILE_TBENCH.csv34 sed -i "/$(scaling_name)/d" $OUTFILE_TBENCH.csv35 fi36}37 38# find string $1 in file csv and get the number of lines39get_lines_csv_tbench()40{41 if [ -f $OUTFILE_TBENCH.csv ]; then42 return `grep -c "$1" $OUTFILE_TBENCH.csv`43 else44 return 045 fi46}47 48pre_clear_tbench()49{50 post_clear_tbench51 rm -rf tbench_*.png52 clear_csv_tbench53}54 55post_clear_tbench()56{57 rm -rf results/tracer-tbench*58 rm -rf $OUTFILE_TBENCH*.log59 rm -rf $OUTFILE_TBENCH*.result60 61}62 63# $1: governor, $2: loop64run_tbench()65{66 echo "Launching amd pstate tracer for $1 #$2 tracer_interval: $TRACER_INTERVAL"67 $TRACER -n tracer-tbench-$1-$2 -i $TRACER_INTERVAL > /dev/null 2>&1 &68 69 printf "Test tbench for $1 #$2 time_limit: $TIME_LIMIT procs_num: $PROCESS_NUM\n"70 tbench_srv > /dev/null 2>&1 &71 $PERF stat -a --per-socket -I 1000 -e power/energy-pkg/ tbench -t $TIME_LIMIT $PROCESS_NUM > $OUTFILE_TBENCH-perf-$1-$2.log 2>&172 73 pid=`pidof tbench_srv`74 kill $pid75 76 for job in `jobs -p`77 do78 echo "Waiting for job id $job"79 wait $job80 done81}82 83# $1: governor, $2: loop84parse_tbench()85{86 awk '{print $5}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-des-perf-$1-$2.log87 avg_des_perf=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_TBENCH-des-perf-$1-$2.log)88 printf "Tbench-$1-#$2 avg des perf: $avg_des_perf\n" | tee -a $OUTFILE_TBENCH.result89 90 awk '{print $7}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-freq-$1-$2.log91 avg_freq=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_TBENCH-freq-$1-$2.log)92 printf "Tbench-$1-#$2 avg freq: $avg_freq\n" | tee -a $OUTFILE_TBENCH.result93 94 awk '{print $11}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-load-$1-$2.log95 avg_load=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_TBENCH-load-$1-$2.log)96 printf "Tbench-$1-#$2 avg load: $avg_load\n" | tee -a $OUTFILE_TBENCH.result97 98 grep Throughput $OUTFILE_TBENCH-perf-$1-$2.log | awk '{print $2}' > $OUTFILE_TBENCH-throughput-$1-$2.log99 tp_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_TBENCH-throughput-$1-$2.log)100 printf "Tbench-$1-#$2 throughput(MB/s): $tp_sum\n" | tee -a $OUTFILE_TBENCH.result101 102 grep Joules $OUTFILE_TBENCH-perf-$1-$2.log | awk '{print $4}' > $OUTFILE_TBENCH-energy-$1-$2.log103 en_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_TBENCH-energy-$1-$2.log)104 printf "Tbench-$1-#$2 power consumption(J): $en_sum\n" | tee -a $OUTFILE_TBENCH.result105 106 # Permance is throughput per second, denoted T/t, where T is throught rendered in t seconds.107 # It is well known that P=E/t, where P is power measured in watts(W), E is energy measured in joules(J),108 # and t is time measured in seconds(s). This means that performance per watt becomes109 # T/t T/t T110 # --- = --- = ---111 # P E/t E112 # with unit given by MB per joule.113 ppw=`echo "scale=4;($TIME_LIMIT-1)*$tp_sum/$en_sum" | bc | awk '{printf "%.4f", $0}'`114 printf "Tbench-$1-#$2 performance per watt(MB/J): $ppw\n" | tee -a $OUTFILE_TBENCH.result115 printf "\n" | tee -a $OUTFILE_TBENCH.result116 117 driver_name=`echo $(scaling_name)`118 store_csv_tbench "$driver_name-$1" $2 $avg_des_perf $avg_freq $avg_load $tp_sum $en_sum $ppw119}120 121# $1: governor122loop_tbench()123{124 printf "\nTbench total test times is $LOOP_TIMES for $1\n\n"125 for i in `seq 1 $LOOP_TIMES`126 do127 run_tbench $1 $i128 parse_tbench $1 $i129 done130}131 132# $1: governor133gather_tbench()134{135 printf "Tbench test result for $1 (loops:$LOOP_TIMES)" | tee -a $OUTFILE_TBENCH.result136 printf "\n--------------------------------------------------\n" | tee -a $OUTFILE_TBENCH.result137 138 grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg des perf:" | awk '{print $NF}' > $OUTFILE_TBENCH-des-perf-$1.log139 avg_des_perf=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-des-perf-$1.log)140 printf "Tbench-$1 avg des perf: $avg_des_perf\n" | tee -a $OUTFILE_TBENCH.result141 142 grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg freq:" | awk '{print $NF}' > $OUTFILE_TBENCH-freq-$1.log143 avg_freq=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-freq-$1.log)144 printf "Tbench-$1 avg freq: $avg_freq\n" | tee -a $OUTFILE_TBENCH.result145 146 grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg load:" | awk '{print $NF}' > $OUTFILE_TBENCH-load-$1.log147 avg_load=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-load-$1.log)148 printf "Tbench-$1 avg load: $avg_load\n" | tee -a $OUTFILE_TBENCH.result149 150 grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "throughput(MB/s):" | awk '{print $NF}' > $OUTFILE_TBENCH-throughput-$1.log151 tp_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_TBENCH-throughput-$1.log)152 printf "Tbench-$1 total throughput(MB/s): $tp_sum\n" | tee -a $OUTFILE_TBENCH.result153 154 avg_tp=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-throughput-$1.log)155 printf "Tbench-$1 avg throughput(MB/s): $avg_tp\n" | tee -a $OUTFILE_TBENCH.result156 157 grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "power consumption(J):" | awk '{print $NF}' > $OUTFILE_TBENCH-energy-$1.log158 en_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_TBENCH-energy-$1.log)159 printf "Tbench-$1 total power consumption(J): $en_sum\n" | tee -a $OUTFILE_TBENCH.result160 161 avg_en=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-energy-$1.log)162 printf "Tbench-$1 avg power consumption(J): $avg_en\n" | tee -a $OUTFILE_TBENCH.result163 164 # Permance is throughput per second, denoted T/t, where T is throught rendered in t seconds.165 # It is well known that P=E/t, where P is power measured in watts(W), E is energy measured in joules(J),166 # and t is time measured in seconds(s). This means that performance per watt becomes167 # T/t T/t T168 # --- = --- = ---169 # P E/t E170 # with unit given by MB per joule.171 ppw=`echo "scale=4;($TIME_LIMIT-1)*$avg_tp/$avg_en" | bc | awk '{printf "%.4f", $0}'`172 printf "Tbench-$1 performance per watt(MB/J): $ppw\n" | tee -a $OUTFILE_TBENCH.result173 printf "\n" | tee -a $OUTFILE_TBENCH.result174 175 driver_name=`echo $(scaling_name)`176 store_csv_tbench "$driver_name-$1" "Average" $avg_des_perf $avg_freq $avg_load $avg_tp $avg_en $ppw177}178 179# $1: base scaling_driver $2: base governor $3: comparative scaling_driver $4: comparative governor180__calc_comp_tbench()181{182 base=`grep "$1-$2" $OUTFILE_TBENCH.csv | grep "Average"`183 comp=`grep "$3-$4" $OUTFILE_TBENCH.csv | grep "Average"`184 185 if [ -n "$base" -a -n "$comp" ]; then186 printf "\n==================================================\n" | tee -a $OUTFILE_TBENCH.result187 printf "Tbench comparison $1-$2 VS $3-$4" | tee -a $OUTFILE_TBENCH.result188 printf "\n==================================================\n" | tee -a $OUTFILE_TBENCH.result189 190 # get the base values191 des_perf_base=`echo "$base" | awk '{print $3}' | sed s/,//`192 freq_base=`echo "$base" | awk '{print $4}' | sed s/,//`193 load_base=`echo "$base" | awk '{print $5}' | sed s/,//`194 perf_base=`echo "$base" | awk '{print $6}' | sed s/,//`195 energy_base=`echo "$base" | awk '{print $7}' | sed s/,//`196 ppw_base=`echo "$base" | awk '{print $8}' | sed s/,//`197 198 # get the comparative values199 des_perf_comp=`echo "$comp" | awk '{print $3}' | sed s/,//`200 freq_comp=`echo "$comp" | awk '{print $4}' | sed s/,//`201 load_comp=`echo "$comp" | awk '{print $5}' | sed s/,//`202 perf_comp=`echo "$comp" | awk '{print $6}' | sed s/,//`203 energy_comp=`echo "$comp" | awk '{print $7}' | sed s/,//`204 ppw_comp=`echo "$comp" | awk '{print $8}' | sed s/,//`205 206 # compare the base and comp values207 des_perf_drop=`echo "scale=4;($des_perf_comp-$des_perf_base)*100/$des_perf_base" | bc | awk '{printf "%.4f", $0}'`208 printf "Tbench-$1 des perf base: $des_perf_base comprison: $des_perf_comp percent: $des_perf_drop\n" | tee -a $OUTFILE_TBENCH.result209 210 freq_drop=`echo "scale=4;($freq_comp-$freq_base)*100/$freq_base" | bc | awk '{printf "%.4f", $0}'`211 printf "Tbench-$1 freq base: $freq_base comprison: $freq_comp percent: $freq_drop\n" | tee -a $OUTFILE_TBENCH.result212 213 load_drop=`echo "scale=4;($load_comp-$load_base)*100/$load_base" | bc | awk '{printf "%.4f", $0}'`214 printf "Tbench-$1 load base: $load_base comprison: $load_comp percent: $load_drop\n" | tee -a $OUTFILE_TBENCH.result215 216 perf_drop=`echo "scale=4;($perf_comp-$perf_base)*100/$perf_base" | bc | awk '{printf "%.4f", $0}'`217 printf "Tbench-$1 perf base: $perf_base comprison: $perf_comp percent: $perf_drop\n" | tee -a $OUTFILE_TBENCH.result218 219 energy_drop=`echo "scale=4;($energy_comp-$energy_base)*100/$energy_base" | bc | awk '{printf "%.4f", $0}'`220 printf "Tbench-$1 energy base: $energy_base comprison: $energy_comp percent: $energy_drop\n" | tee -a $OUTFILE_TBENCH.result221 222 ppw_drop=`echo "scale=4;($ppw_comp-$ppw_base)*100/$ppw_base" | bc | awk '{printf "%.4f", $0}'`223 printf "Tbench-$1 performance per watt base: $ppw_base comprison: $ppw_comp percent: $ppw_drop\n" | tee -a $OUTFILE_TBENCH.result224 printf "\n" | tee -a $OUTFILE_TBENCH.result225 226 store_csv_tbench "$1-$2 VS $3-$4" "Comprison(%)" "$des_perf_drop" "$freq_drop" "$load_drop" "$perf_drop" "$energy_drop" "$ppw_drop"227 fi228}229 230# calculate the comparison(%)231calc_comp_tbench()232{233 # acpi-cpufreq-ondemand VS acpi-cpufreq-schedutil234 __calc_comp_tbench ${all_scaling_names[0]} ${tbench_governors[0]} ${all_scaling_names[0]} ${tbench_governors[1]}235 236 # amd-pstate-ondemand VS amd-pstate-schedutil237 __calc_comp_tbench ${all_scaling_names[1]} ${tbench_governors[0]} ${all_scaling_names[1]} ${tbench_governors[1]}238 239 # acpi-cpufreq-ondemand VS amd-pstate-ondemand240 __calc_comp_tbench ${all_scaling_names[0]} ${tbench_governors[0]} ${all_scaling_names[1]} ${tbench_governors[0]}241 242 # acpi-cpufreq-schedutil VS amd-pstate-schedutil243 __calc_comp_tbench ${all_scaling_names[0]} ${tbench_governors[1]} ${all_scaling_names[1]} ${tbench_governors[1]}244}245 246# $1: file_name, $2: title, $3: ylable, $4: column247plot_png_tbench()248{249 # all_scaling_names[1] all_scaling_names[0] flag250 # amd-pstate acpi-cpufreq251 # N N 0252 # N Y 1253 # Y N 2254 # Y Y 3255 ret=`grep -c "${all_scaling_names[1]}" $OUTFILE_TBENCH.csv`256 if [ $ret -eq 0 ]; then257 ret=`grep -c "${all_scaling_names[0]}" $OUTFILE_TBENCH.csv`258 if [ $ret -eq 0 ]; then259 flag=0260 else261 flag=1262 fi263 else264 ret=`grep -c "${all_scaling_names[0]}" $OUTFILE_TBENCH.csv`265 if [ $ret -eq 0 ]; then266 flag=2267 else268 flag=3269 fi270 fi271 272 gnuplot << EOF273 set term png274 set output "$1"275 276 set title "$2"277 set xlabel "Test Cycles (round)"278 set ylabel "$3"279 280 set grid281 set style data histogram282 set style fill solid 0.5 border283 set boxwidth 0.8284 285 if ($flag == 1) {286 plot \287 "<(sed -n -e 's/,//g' -e '/${all_scaling_names[0]}-${tbench_governors[0]}/p' $OUTFILE_TBENCH.csv)" using $4:xtic(2) title "${all_scaling_names[0]}-${tbench_governors[0]}", \288 "<(sed -n -e 's/,//g' -e '/${all_scaling_names[0]}-${tbench_governors[1]}/p' $OUTFILE_TBENCH.csv)" using $4:xtic(2) title "${all_scaling_names[0]}-${tbench_governors[1]}"289 } else {290 if ($flag == 2) {291 plot \292 "<(sed -n -e 's/,//g' -e '/${all_scaling_names[1]}-${tbench_governors[0]}/p' $OUTFILE_TBENCH.csv)" using $4:xtic(2) title "${all_scaling_names[1]}-${tbench_governors[0]}", \293 "<(sed -n -e 's/,//g' -e '/${all_scaling_names[1]}-${tbench_governors[1]}/p' $OUTFILE_TBENCH.csv)" using $4:xtic(2) title "${all_scaling_names[1]}-${tbench_governors[1]}"294 } else {295 if ($flag == 3 ) {296 plot \297 "<(sed -n -e 's/,//g' -e '/${all_scaling_names[0]}-${tbench_governors[0]}/p' $OUTFILE_TBENCH.csv)" using $4:xtic(2) title "${all_scaling_names[0]}-${tbench_governors[0]}", \298 "<(sed -n -e 's/,//g' -e '/${all_scaling_names[0]}-${tbench_governors[1]}/p' $OUTFILE_TBENCH.csv)" using $4:xtic(2) title "${all_scaling_names[0]}-${tbench_governors[1]}", \299 "<(sed -n -e 's/,//g' -e '/${all_scaling_names[1]}-${tbench_governors[0]}/p' $OUTFILE_TBENCH.csv)" using $4:xtic(2) title "${all_scaling_names[1]}-${tbench_governors[0]}", \300 "<(sed -n -e 's/,//g' -e '/${all_scaling_names[1]}-${tbench_governors[1]}/p' $OUTFILE_TBENCH.csv)" using $4:xtic(2) title "${all_scaling_names[1]}-${tbench_governors[1]}"301 }302 }303 }304 quit305EOF306}307 308amd_pstate_tbench()309{310 printf "\n---------------------------------------------\n"311 printf "*** Running tbench ***"312 printf "\n---------------------------------------------\n"313 314 pre_clear_tbench315 316 get_lines_csv_tbench "Governor"317 if [ $? -eq 0 ]; then318 # add titles and unit for csv file319 store_csv_tbench "Governor" "Round" "Des-perf" "Freq" "Load" "Performance" "Energy" "Performance Per Watt"320 store_csv_tbench "Unit" "" "" "GHz" "" "MB/s" "J" "MB/J"321 fi322 323 backup_governor324 for governor in ${tbench_governors[*]} ; do325 printf "\nSpecified governor is $governor\n\n"326 switch_governor $governor327 loop_tbench $governor328 gather_tbench $governor329 done330 restore_governor331 332 plot_png_tbench "tbench_perfromance.png" "Tbench Benchmark Performance" "Performance" 6333 plot_png_tbench "tbench_energy.png" "Tbench Benchmark Energy" "Energy (J)" 7334 plot_png_tbench "tbench_ppw.png" "Tbench Benchmark Performance Per Watt" "Performance Per Watt (MB/J)" 8335 336 calc_comp_tbench337 338 post_clear_tbench339}340