Path: blob/master/tools/testing/selftests/amd-pstate/tbench.sh
26285 views
#!/bin/sh1# SPDX-License-Identifier: GPL-2.023# Testing and monitor the cpu desire performance, frequency, load,4# power consumption and throughput etc.when this script trigger tbench5# test cases.6# 1) Run tbench benchmark on specific governors, ondemand or schedutil.7# 2) Run tbench benchmark comparative test on acpi-cpufreq kernel driver.8# 3) Get desire performance, frequency, load by perf.9# 4) Get power consumption and throughput by amd_pstate_trace.py.10# 5) Analyse test results and save it in file selftest.tbench.csv.11# 6) Plot png images about performance, energy and performance per watt for each test.1213# protect against multiple inclusion14if [ $FILE_TBENCH ]; then15return 016else17FILE_TBENCH=DONE18fi1920tbench_governors=("ondemand" "schedutil")2122# $1: governor, $2: round, $3: des-perf, $4: freq, $5: load, $6: performance, $7: energy, $8: performance per watt23store_csv_tbench()24{25echo "$1, $2, $3, $4, $5, $6, $7, $8" | tee -a $OUTFILE_TBENCH.csv > /dev/null 2>&126}2728# clear some special lines29clear_csv_tbench()30{31if [ -f $OUTFILE_TBENCH.csv ]; then32sed -i '/Comprison(%)/d' $OUTFILE_TBENCH.csv33sed -i "/$(scaling_name)/d" $OUTFILE_TBENCH.csv34fi35}3637# find string $1 in file csv and get the number of lines38get_lines_csv_tbench()39{40if [ -f $OUTFILE_TBENCH.csv ]; then41return `grep -c "$1" $OUTFILE_TBENCH.csv`42else43return 044fi45}4647pre_clear_tbench()48{49post_clear_tbench50rm -rf tbench_*.png51clear_csv_tbench52}5354post_clear_tbench()55{56rm -rf results/tracer-tbench*57rm -rf $OUTFILE_TBENCH*.log58rm -rf $OUTFILE_TBENCH*.result5960}6162# $1: governor, $2: loop63run_tbench()64{65echo "Launching amd pstate tracer for $1 #$2 tracer_interval: $TRACER_INTERVAL"66$TRACER -n tracer-tbench-$1-$2 -i $TRACER_INTERVAL > /dev/null 2>&1 &6768printf "Test tbench for $1 #$2 time_limit: $TIME_LIMIT procs_num: $PROCESS_NUM\n"69tbench_srv > /dev/null 2>&1 &70$PERF stat -a --per-socket -I 1000 -e power/energy-pkg/ tbench -t $TIME_LIMIT $PROCESS_NUM > $OUTFILE_TBENCH-perf-$1-$2.log 2>&17172pid=`pidof tbench_srv`73kill $pid7475for job in `jobs -p`76do77echo "Waiting for job id $job"78wait $job79done80}8182# $1: governor, $2: loop83parse_tbench()84{85awk '{print $5}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-des-perf-$1-$2.log86avg_des_perf=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_TBENCH-des-perf-$1-$2.log)87printf "Tbench-$1-#$2 avg des perf: $avg_des_perf\n" | tee -a $OUTFILE_TBENCH.result8889awk '{print $7}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-freq-$1-$2.log90avg_freq=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_TBENCH-freq-$1-$2.log)91printf "Tbench-$1-#$2 avg freq: $avg_freq\n" | tee -a $OUTFILE_TBENCH.result9293awk '{print $11}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-load-$1-$2.log94avg_load=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_TBENCH-load-$1-$2.log)95printf "Tbench-$1-#$2 avg load: $avg_load\n" | tee -a $OUTFILE_TBENCH.result9697grep Throughput $OUTFILE_TBENCH-perf-$1-$2.log | awk '{print $2}' > $OUTFILE_TBENCH-throughput-$1-$2.log98tp_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_TBENCH-throughput-$1-$2.log)99printf "Tbench-$1-#$2 throughput(MB/s): $tp_sum\n" | tee -a $OUTFILE_TBENCH.result100101grep Joules $OUTFILE_TBENCH-perf-$1-$2.log | awk '{print $4}' > $OUTFILE_TBENCH-energy-$1-$2.log102en_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_TBENCH-energy-$1-$2.log)103printf "Tbench-$1-#$2 power consumption(J): $en_sum\n" | tee -a $OUTFILE_TBENCH.result104105# Permance is throughput per second, denoted T/t, where T is throught rendered in t seconds.106# It is well known that P=E/t, where P is power measured in watts(W), E is energy measured in joules(J),107# and t is time measured in seconds(s). This means that performance per watt becomes108# T/t T/t T109# --- = --- = ---110# P E/t E111# with unit given by MB per joule.112ppw=`echo "scale=4;($TIME_LIMIT-1)*$tp_sum/$en_sum" | bc | awk '{printf "%.4f", $0}'`113printf "Tbench-$1-#$2 performance per watt(MB/J): $ppw\n" | tee -a $OUTFILE_TBENCH.result114printf "\n" | tee -a $OUTFILE_TBENCH.result115116driver_name=`echo $(scaling_name)`117store_csv_tbench "$driver_name-$1" $2 $avg_des_perf $avg_freq $avg_load $tp_sum $en_sum $ppw118}119120# $1: governor121loop_tbench()122{123printf "\nTbench total test times is $LOOP_TIMES for $1\n\n"124for i in `seq 1 $LOOP_TIMES`125do126run_tbench $1 $i127parse_tbench $1 $i128done129}130131# $1: governor132gather_tbench()133{134printf "Tbench test result for $1 (loops:$LOOP_TIMES)" | tee -a $OUTFILE_TBENCH.result135printf "\n--------------------------------------------------\n" | tee -a $OUTFILE_TBENCH.result136137grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg des perf:" | awk '{print $NF}' > $OUTFILE_TBENCH-des-perf-$1.log138avg_des_perf=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-des-perf-$1.log)139printf "Tbench-$1 avg des perf: $avg_des_perf\n" | tee -a $OUTFILE_TBENCH.result140141grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg freq:" | awk '{print $NF}' > $OUTFILE_TBENCH-freq-$1.log142avg_freq=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-freq-$1.log)143printf "Tbench-$1 avg freq: $avg_freq\n" | tee -a $OUTFILE_TBENCH.result144145grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg load:" | awk '{print $NF}' > $OUTFILE_TBENCH-load-$1.log146avg_load=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-load-$1.log)147printf "Tbench-$1 avg load: $avg_load\n" | tee -a $OUTFILE_TBENCH.result148149grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "throughput(MB/s):" | awk '{print $NF}' > $OUTFILE_TBENCH-throughput-$1.log150tp_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_TBENCH-throughput-$1.log)151printf "Tbench-$1 total throughput(MB/s): $tp_sum\n" | tee -a $OUTFILE_TBENCH.result152153avg_tp=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-throughput-$1.log)154printf "Tbench-$1 avg throughput(MB/s): $avg_tp\n" | tee -a $OUTFILE_TBENCH.result155156grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "power consumption(J):" | awk '{print $NF}' > $OUTFILE_TBENCH-energy-$1.log157en_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_TBENCH-energy-$1.log)158printf "Tbench-$1 total power consumption(J): $en_sum\n" | tee -a $OUTFILE_TBENCH.result159160avg_en=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_TBENCH-energy-$1.log)161printf "Tbench-$1 avg power consumption(J): $avg_en\n" | tee -a $OUTFILE_TBENCH.result162163# Permance is throughput per second, denoted T/t, where T is throught rendered in t seconds.164# It is well known that P=E/t, where P is power measured in watts(W), E is energy measured in joules(J),165# and t is time measured in seconds(s). This means that performance per watt becomes166# T/t T/t T167# --- = --- = ---168# P E/t E169# with unit given by MB per joule.170ppw=`echo "scale=4;($TIME_LIMIT-1)*$avg_tp/$avg_en" | bc | awk '{printf "%.4f", $0}'`171printf "Tbench-$1 performance per watt(MB/J): $ppw\n" | tee -a $OUTFILE_TBENCH.result172printf "\n" | tee -a $OUTFILE_TBENCH.result173174driver_name=`echo $(scaling_name)`175store_csv_tbench "$driver_name-$1" "Average" $avg_des_perf $avg_freq $avg_load $avg_tp $avg_en $ppw176}177178# $1: base scaling_driver $2: base governor $3: comparative scaling_driver $4: comparative governor179__calc_comp_tbench()180{181base=`grep "$1-$2" $OUTFILE_TBENCH.csv | grep "Average"`182comp=`grep "$3-$4" $OUTFILE_TBENCH.csv | grep "Average"`183184if [ -n "$base" -a -n "$comp" ]; then185printf "\n==================================================\n" | tee -a $OUTFILE_TBENCH.result186printf "Tbench comparison $1-$2 VS $3-$4" | tee -a $OUTFILE_TBENCH.result187printf "\n==================================================\n" | tee -a $OUTFILE_TBENCH.result188189# get the base values190des_perf_base=`echo "$base" | awk '{print $3}' | sed s/,//`191freq_base=`echo "$base" | awk '{print $4}' | sed s/,//`192load_base=`echo "$base" | awk '{print $5}' | sed s/,//`193perf_base=`echo "$base" | awk '{print $6}' | sed s/,//`194energy_base=`echo "$base" | awk '{print $7}' | sed s/,//`195ppw_base=`echo "$base" | awk '{print $8}' | sed s/,//`196197# get the comparative values198des_perf_comp=`echo "$comp" | awk '{print $3}' | sed s/,//`199freq_comp=`echo "$comp" | awk '{print $4}' | sed s/,//`200load_comp=`echo "$comp" | awk '{print $5}' | sed s/,//`201perf_comp=`echo "$comp" | awk '{print $6}' | sed s/,//`202energy_comp=`echo "$comp" | awk '{print $7}' | sed s/,//`203ppw_comp=`echo "$comp" | awk '{print $8}' | sed s/,//`204205# compare the base and comp values206des_perf_drop=`echo "scale=4;($des_perf_comp-$des_perf_base)*100/$des_perf_base" | bc | awk '{printf "%.4f", $0}'`207printf "Tbench-$1 des perf base: $des_perf_base comprison: $des_perf_comp percent: $des_perf_drop\n" | tee -a $OUTFILE_TBENCH.result208209freq_drop=`echo "scale=4;($freq_comp-$freq_base)*100/$freq_base" | bc | awk '{printf "%.4f", $0}'`210printf "Tbench-$1 freq base: $freq_base comprison: $freq_comp percent: $freq_drop\n" | tee -a $OUTFILE_TBENCH.result211212load_drop=`echo "scale=4;($load_comp-$load_base)*100/$load_base" | bc | awk '{printf "%.4f", $0}'`213printf "Tbench-$1 load base: $load_base comprison: $load_comp percent: $load_drop\n" | tee -a $OUTFILE_TBENCH.result214215perf_drop=`echo "scale=4;($perf_comp-$perf_base)*100/$perf_base" | bc | awk '{printf "%.4f", $0}'`216printf "Tbench-$1 perf base: $perf_base comprison: $perf_comp percent: $perf_drop\n" | tee -a $OUTFILE_TBENCH.result217218energy_drop=`echo "scale=4;($energy_comp-$energy_base)*100/$energy_base" | bc | awk '{printf "%.4f", $0}'`219printf "Tbench-$1 energy base: $energy_base comprison: $energy_comp percent: $energy_drop\n" | tee -a $OUTFILE_TBENCH.result220221ppw_drop=`echo "scale=4;($ppw_comp-$ppw_base)*100/$ppw_base" | bc | awk '{printf "%.4f", $0}'`222printf "Tbench-$1 performance per watt base: $ppw_base comprison: $ppw_comp percent: $ppw_drop\n" | tee -a $OUTFILE_TBENCH.result223printf "\n" | tee -a $OUTFILE_TBENCH.result224225store_csv_tbench "$1-$2 VS $3-$4" "Comprison(%)" "$des_perf_drop" "$freq_drop" "$load_drop" "$perf_drop" "$energy_drop" "$ppw_drop"226fi227}228229# calculate the comparison(%)230calc_comp_tbench()231{232# acpi-cpufreq-ondemand VS acpi-cpufreq-schedutil233__calc_comp_tbench ${all_scaling_names[0]} ${tbench_governors[0]} ${all_scaling_names[0]} ${tbench_governors[1]}234235# amd-pstate-ondemand VS amd-pstate-schedutil236__calc_comp_tbench ${all_scaling_names[1]} ${tbench_governors[0]} ${all_scaling_names[1]} ${tbench_governors[1]}237238# acpi-cpufreq-ondemand VS amd-pstate-ondemand239__calc_comp_tbench ${all_scaling_names[0]} ${tbench_governors[0]} ${all_scaling_names[1]} ${tbench_governors[0]}240241# acpi-cpufreq-schedutil VS amd-pstate-schedutil242__calc_comp_tbench ${all_scaling_names[0]} ${tbench_governors[1]} ${all_scaling_names[1]} ${tbench_governors[1]}243}244245# $1: file_name, $2: title, $3: ylable, $4: column246plot_png_tbench()247{248# all_scaling_names[1] all_scaling_names[0] flag249# amd-pstate acpi-cpufreq250# N N 0251# N Y 1252# Y N 2253# Y Y 3254ret=`grep -c "${all_scaling_names[1]}" $OUTFILE_TBENCH.csv`255if [ $ret -eq 0 ]; then256ret=`grep -c "${all_scaling_names[0]}" $OUTFILE_TBENCH.csv`257if [ $ret -eq 0 ]; then258flag=0259else260flag=1261fi262else263ret=`grep -c "${all_scaling_names[0]}" $OUTFILE_TBENCH.csv`264if [ $ret -eq 0 ]; then265flag=2266else267flag=3268fi269fi270271gnuplot << EOF272set term png273set output "$1"274275set title "$2"276set xlabel "Test Cycles (round)"277set ylabel "$3"278279set grid280set style data histogram281set style fill solid 0.5 border282set boxwidth 0.8283284if ($flag == 1) {285plot \286"<(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]}", \287"<(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]}"288} else {289if ($flag == 2) {290plot \291"<(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]}", \292"<(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]}"293} else {294if ($flag == 3 ) {295plot \296"<(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]}", \297"<(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]}", \298"<(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]}", \299"<(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]}"300}301}302}303quit304EOF305}306307amd_pstate_tbench()308{309printf "\n---------------------------------------------\n"310printf "*** Running tbench ***"311printf "\n---------------------------------------------\n"312313pre_clear_tbench314315get_lines_csv_tbench "Governor"316if [ $? -eq 0 ]; then317# add titles and unit for csv file318store_csv_tbench "Governor" "Round" "Des-perf" "Freq" "Load" "Performance" "Energy" "Performance Per Watt"319store_csv_tbench "Unit" "" "" "GHz" "" "MB/s" "J" "MB/J"320fi321322backup_governor323for governor in ${tbench_governors[*]} ; do324printf "\nSpecified governor is $governor\n\n"325switch_governor $governor326loop_tbench $governor327gather_tbench $governor328done329restore_governor330331plot_png_tbench "tbench_perfromance.png" "Tbench Benchmark Performance" "Performance" 6332plot_png_tbench "tbench_energy.png" "Tbench Benchmark Energy" "Energy (J)" 7333plot_png_tbench "tbench_ppw.png" "Tbench Benchmark Performance Per Watt" "Performance Per Watt (MB/J)" 8334335calc_comp_tbench336337post_clear_tbench338}339340341