1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4# Testing and monitor the cpu desire performance, frequency, load,
5# power consumption and throughput etc.when this script trigger tbench
6# 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 inclusion
15if [ $FILE_TBENCH ]; then
16	return 0
17else
18	FILE_TBENCH=DONE
19fi
20
21tbench_governors=("ondemand" "schedutil")
22
23# $1: governor, $2: round, $3: des-perf, $4: freq, $5: load, $6: performance, $7: energy, $8: performance per watt
24store_csv_tbench()
25{
26	echo "$1, $2, $3, $4, $5, $6, $7, $8" | tee -a $OUTFILE_TBENCH.csv > /dev/null 2>&1
27}
28
29# clear some special lines
30clear_csv_tbench()
31{
32	if [ -f $OUTFILE_TBENCH.csv ]; then
33		sed -i '/Comprison(%)/d' $OUTFILE_TBENCH.csv
34		sed -i "/$(scaling_name)/d" $OUTFILE_TBENCH.csv
35	fi
36}
37
38# find string $1 in file csv and get the number of lines
39get_lines_csv_tbench()
40{
41	if [ -f $OUTFILE_TBENCH.csv ]; then
42		return `grep -c "$1" $OUTFILE_TBENCH.csv`
43	else
44		return 0
45	fi
46}
47
48pre_clear_tbench()
49{
50	post_clear_tbench
51	rm -rf tbench_*.png
52	clear_csv_tbench
53}
54
55post_clear_tbench()
56{
57	rm -rf results/tracer-tbench*
58	rm -rf $OUTFILE_TBENCH*.log
59	rm -rf $OUTFILE_TBENCH*.result
60
61}
62
63# $1: governor, $2: loop
64run_tbench()
65{
66	echo "Launching amd pstate tracer for $1 #$2 tracer_interval: $TRACER_INTERVAL"
67	./amd_pstate_trace.py -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>&1
72
73	pid=`pidof tbench_srv`
74	kill $pid
75
76	for job in `jobs -p`
77	do
78		echo "Waiting for job id $job"
79		wait $job
80	done
81}
82
83# $1: governor, $2: loop
84parse_tbench()
85{
86	awk '{print $5}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-des-perf-$1-$2.log
87	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.result
89
90	awk '{print $7}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-freq-$1-$2.log
91	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.result
93
94	awk '{print $11}' results/tracer-tbench-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_TBENCH-load-$1-$2.log
95	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.result
97
98	grep Throughput $OUTFILE_TBENCH-perf-$1-$2.log | awk '{print $2}' > $OUTFILE_TBENCH-throughput-$1-$2.log
99	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.result
101
102	grep Joules $OUTFILE_TBENCH-perf-$1-$2.log | awk '{print $4}' > $OUTFILE_TBENCH-energy-$1-$2.log
103	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.result
105
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 becomes
109	#       T/t   T/t    T
110	#       --- = --- = ---
111	#        P    E/t    E
112	# 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.result
115	printf "\n" | tee -a $OUTFILE_TBENCH.result
116
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 $ppw
119}
120
121# $1: governor
122loop_tbench()
123{
124	printf "\nTbench total test times is $LOOP_TIMES for $1\n\n"
125	for i in `seq 1 $LOOP_TIMES`
126	do
127		run_tbench $1 $i
128		parse_tbench $1 $i
129	done
130}
131
132# $1: governor
133gather_tbench()
134{
135	printf "Tbench test result for $1 (loops:$LOOP_TIMES)" | tee -a $OUTFILE_TBENCH.result
136	printf "\n--------------------------------------------------\n" | tee -a $OUTFILE_TBENCH.result
137
138	grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg des perf:" | awk '{print $NF}' > $OUTFILE_TBENCH-des-perf-$1.log
139	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.result
141
142	grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg freq:" | awk '{print $NF}' > $OUTFILE_TBENCH-freq-$1.log
143	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.result
145
146	grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "avg load:" | awk '{print $NF}' > $OUTFILE_TBENCH-load-$1.log
147	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.result
149
150	grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "throughput(MB/s):" | awk '{print $NF}' > $OUTFILE_TBENCH-throughput-$1.log
151	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.result
153
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.result
156
157	grep "Tbench-$1-#" $OUTFILE_TBENCH.result | grep "power consumption(J):" | awk '{print $NF}' > $OUTFILE_TBENCH-energy-$1.log
158	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.result
160
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.result
163
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 becomes
167	#       T/t   T/t    T
168	#       --- = --- = ---
169	#        P    E/t    E
170	# 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.result
173	printf "\n" | tee -a $OUTFILE_TBENCH.result
174
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 $ppw
177}
178
179# $1: base scaling_driver $2: base governor $3: comparative scaling_driver $4: comparative governor
180__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" ]; then
186		printf "\n==================================================\n" | tee -a $OUTFILE_TBENCH.result
187		printf "Tbench comparison $1-$2 VS $3-$4" | tee -a $OUTFILE_TBENCH.result
188		printf "\n==================================================\n" | tee -a $OUTFILE_TBENCH.result
189
190		# get the base values
191		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 values
199		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 values
207		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.result
209
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.result
212
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.result
215
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.result
218
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.result
221
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.result
224		printf "\n" | tee -a $OUTFILE_TBENCH.result
225
226		store_csv_tbench "$1-$2 VS $3-$4" "Comprison(%)" "$des_perf_drop" "$freq_drop" "$load_drop" "$perf_drop" "$energy_drop" "$ppw_drop"
227	fi
228}
229
230# calculate the comparison(%)
231calc_comp_tbench()
232{
233	# acpi-cpufreq-ondemand VS acpi-cpufreq-schedutil
234	__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-schedutil
237	__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-ondemand
240	__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-schedutil
243	__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: column
247plot_png_tbench()
248{
249	# all_scaling_names[1] all_scaling_names[0] flag
250	#    amd-pstate           acpi-cpufreq
251	#         N                   N             0
252	#         N                   Y             1
253	#         Y                   N             2
254	#         Y                   Y             3
255	ret=`grep -c "${all_scaling_names[1]}" $OUTFILE_TBENCH.csv`
256	if [ $ret -eq 0 ]; then
257		ret=`grep -c "${all_scaling_names[0]}" $OUTFILE_TBENCH.csv`
258		if [ $ret -eq 0 ]; then
259			flag=0
260		else
261			flag=1
262		fi
263	else
264		ret=`grep -c "${all_scaling_names[0]}" $OUTFILE_TBENCH.csv`
265		if [ $ret -eq 0 ]; then
266			flag=2
267		else
268			flag=3
269		fi
270	fi
271
272	gnuplot << EOF
273		set term png
274		set output "$1"
275
276		set title "$2"
277		set xlabel "Test Cycles (round)"
278		set ylabel "$3"
279
280		set grid
281		set style data histogram
282		set style fill solid 0.5 border
283		set boxwidth 0.8
284
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		quit
305EOF
306}
307
308amd_pstate_tbench()
309{
310	printf "\n---------------------------------------------\n"
311	printf "*** Running tbench                        ***"
312	printf "\n---------------------------------------------\n"
313
314	pre_clear_tbench
315
316	get_lines_csv_tbench "Governor"
317	if [ $? -eq 0 ]; then
318		# add titles and unit for csv file
319		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	fi
322
323	backup_governor
324	for governor in ${tbench_governors[*]} ; do
325		printf "\nSpecified governor is $governor\n\n"
326		switch_governor $governor
327		loop_tbench $governor
328		gather_tbench $governor
329	done
330	restore_governor
331
332	plot_png_tbench "tbench_perfromance.png" "Tbench Benchmark Performance" "Performance" 6
333	plot_png_tbench "tbench_energy.png" "Tbench Benchmark Energy" "Energy (J)" 7
334	plot_png_tbench "tbench_ppw.png" "Tbench Benchmark Performance Per Watt" "Performance Per Watt (MB/J)" 8
335
336	calc_comp_tbench
337
338	post_clear_tbench
339}
340