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
6# gitsource test.
7# 1) Download and tar gitsource codes.
8# 2) Run gitsource benchmark on specific governors, ondemand or schedutil.
9# 3) Run tbench benchmark comparative test on acpi-cpufreq kernel driver.
10# 4) Get desire performance, frequency, load by perf.
11# 5) Get power consumption and throughput by amd_pstate_trace.py.
12# 6) Get run time by /usr/bin/time.
13# 7) Analyse test results and save it in file selftest.gitsource.csv.
14#8) Plot png images about time, energy and performance per watt for each test.
15
16# protect against multiple inclusion
17if [ $FILE_GITSOURCE ]; then
18	return 0
19else
20	FILE_GITSOURCE=DONE
21fi
22
23git_name="git-2.15.1"
24git_tar="$git_name.tar.gz"
25gitsource_url="https://github.com/git/git/archive/refs/tags/v2.15.1.tar.gz"
26gitsource_governors=("ondemand" "schedutil")
27
28# $1: governor, $2: round, $3: des-perf, $4: freq, $5: load, $6: time $7: energy, $8: PPW
29store_csv_gitsource()
30{
31	echo "$1, $2, $3, $4, $5, $6, $7, $8" | tee -a $OUTFILE_GIT.csv > /dev/null 2>&1
32}
33
34# clear some special lines
35clear_csv_gitsource()
36{
37	if [ -f $OUTFILE_GIT.csv ]; then
38		sed -i '/Comprison(%)/d' $OUTFILE_GIT.csv
39		sed -i "/$(scaling_name)/d" $OUTFILE_GIT.csv
40	fi
41}
42
43# find string $1 in file csv and get the number of lines
44get_lines_csv_gitsource()
45{
46	if [ -f $OUTFILE_GIT.csv ]; then
47		return `grep -c "$1" $OUTFILE_GIT.csv`
48	else
49		return 0
50	fi
51}
52
53pre_clear_gitsource()
54{
55	post_clear_gitsource
56	rm -rf gitsource_*.png
57	clear_csv_gitsource
58}
59
60post_clear_gitsource()
61{
62	rm -rf results/tracer-gitsource*
63	rm -rf $OUTFILE_GIT*.log
64	rm -rf $OUTFILE_GIT*.result
65}
66
67install_gitsource()
68{
69	if [ ! -d $git_name ]; then
70		printf "Download gitsource, please wait a moment ...\n\n"
71		wget -O $git_tar $gitsource_url > /dev/null 2>&1
72
73		printf "Tar gitsource ...\n\n"
74		tar -xzf $git_tar
75	fi
76}
77
78# $1: governor, $2: loop
79run_gitsource()
80{
81	echo "Launching amd pstate tracer for $1 #$2 tracer_interval: $TRACER_INTERVAL"
82	./amd_pstate_trace.py -n tracer-gitsource-$1-$2 -i $TRACER_INTERVAL > /dev/null 2>&1 &
83
84	printf "Make and test gitsource for $1 #$2 make_cpus: $MAKE_CPUS\n"
85	cd $git_name
86	perf stat -a --per-socket -I 1000 -e power/energy-pkg/ /usr/bin/time -o ../$OUTFILE_GIT.time-gitsource-$1-$2.log make test -j$MAKE_CPUS > ../$OUTFILE_GIT-perf-$1-$2.log 2>&1
87	cd ..
88
89	for job in `jobs -p`
90	do
91		echo "Waiting for job id $job"
92		wait $job
93	done
94}
95
96# $1: governor, $2: loop
97parse_gitsource()
98{
99	awk '{print $5}' results/tracer-gitsource-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_GIT-des-perf-$1-$2.log
100	avg_des_perf=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_GIT-des-perf-$1-$2.log)
101	printf "Gitsource-$1-#$2 avg des perf: $avg_des_perf\n" | tee -a $OUTFILE_GIT.result
102
103	awk '{print $7}' results/tracer-gitsource-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_GIT-freq-$1-$2.log
104	avg_freq=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_GIT-freq-$1-$2.log)
105	printf "Gitsource-$1-#$2 avg freq: $avg_freq\n" | tee -a $OUTFILE_GIT.result
106
107	awk '{print $11}' results/tracer-gitsource-$1-$2/cpu.csv | sed -e '1d' | sed s/,// > $OUTFILE_GIT-load-$1-$2.log
108	avg_load=$(awk 'BEGIN {i=0; sum=0};{i++; sum += $1};END {print sum/i}' $OUTFILE_GIT-load-$1-$2.log)
109	printf "Gitsource-$1-#$2 avg load: $avg_load\n" | tee -a $OUTFILE_GIT.result
110
111	grep user $OUTFILE_GIT.time-gitsource-$1-$2.log | awk '{print $1}' | sed -e 's/user//' > $OUTFILE_GIT-time-$1-$2.log
112	time_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_GIT-time-$1-$2.log)
113	printf "Gitsource-$1-#$2 user time(s): $time_sum\n" | tee -a $OUTFILE_GIT.result
114
115	grep Joules $OUTFILE_GIT-perf-$1-$2.log | awk '{print $4}' > $OUTFILE_GIT-energy-$1-$2.log
116	en_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_GIT-energy-$1-$2.log)
117	printf "Gitsource-$1-#$2 power consumption(J): $en_sum\n" | tee -a $OUTFILE_GIT.result
118
119	# Permance is the number of run gitsource per second, denoted 1/t, where 1 is the number of run gitsource in t
120	# seconds. It is well known that P=E/t, where P is power measured in watts(W), E is energy measured in joules(J),
121	# and t is time measured in seconds(s). This means that performance per watt becomes
122	#        1/t     1/t     1
123	#       ----- = ----- = ---
124	#         P      E/t     E
125	# with unit given by 1 per joule.
126	ppw=`echo "scale=9;1/$en_sum" | bc | awk '{printf "%.9f", $0}'`
127	printf "Gitsource-$1-#$2 performance per watt(1/J): $ppw\n" | tee -a $OUTFILE_GIT.result
128	printf "\n" | tee -a $OUTFILE_GIT.result
129
130	driver_name=`echo $(scaling_name)`
131	store_csv_gitsource "$driver_name-$1" $2 $avg_des_perf $avg_freq $avg_load $time_sum $en_sum $ppw
132}
133
134# $1: governor
135loop_gitsource()
136{
137	printf "\nGitsource total test times is $LOOP_TIMES for $1\n\n"
138	for i in `seq 1 $LOOP_TIMES`
139	do
140		run_gitsource $1 $i
141		parse_gitsource $1 $i
142	done
143}
144
145# $1: governor
146gather_gitsource()
147{
148	printf "Gitsource test result for $1 (loops:$LOOP_TIMES)" | tee -a $OUTFILE_GIT.result
149	printf "\n--------------------------------------------------\n" | tee -a $OUTFILE_GIT.result
150
151	grep "Gitsource-$1-#" $OUTFILE_GIT.result | grep "avg des perf:" | awk '{print $NF}' > $OUTFILE_GIT-des-perf-$1.log
152	avg_des_perf=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_GIT-des-perf-$1.log)
153	printf "Gitsource-$1 avg des perf: $avg_des_perf\n" | tee -a $OUTFILE_GIT.result
154
155	grep "Gitsource-$1-#" $OUTFILE_GIT.result | grep "avg freq:" | awk '{print $NF}' > $OUTFILE_GIT-freq-$1.log
156	avg_freq=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_GIT-freq-$1.log)
157	printf "Gitsource-$1 avg freq: $avg_freq\n" | tee -a $OUTFILE_GIT.result
158
159	grep "Gitsource-$1-#" $OUTFILE_GIT.result | grep "avg load:" | awk '{print $NF}' > $OUTFILE_GIT-load-$1.log
160	avg_load=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_GIT-load-$1.log)
161	printf "Gitsource-$1 avg load: $avg_load\n" | tee -a $OUTFILE_GIT.result
162
163	grep "Gitsource-$1-#" $OUTFILE_GIT.result | grep "user time(s):" | awk '{print $NF}' > $OUTFILE_GIT-time-$1.log
164	time_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_GIT-time-$1.log)
165	printf "Gitsource-$1 total user time(s): $time_sum\n" | tee -a $OUTFILE_GIT.result
166
167	avg_time=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_GIT-time-$1.log)
168	printf "Gitsource-$1 avg user times(s): $avg_time\n" | tee -a $OUTFILE_GIT.result
169
170	grep "Gitsource-$1-#" $OUTFILE_GIT.result | grep "power consumption(J):" | awk '{print $NF}' > $OUTFILE_GIT-energy-$1.log
171	en_sum=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum}' $OUTFILE_GIT-energy-$1.log)
172	printf "Gitsource-$1 total power consumption(J): $en_sum\n" | tee -a $OUTFILE_GIT.result
173
174	avg_en=$(awk 'BEGIN {sum=0};{sum += $1};END {print sum/'$LOOP_TIMES'}' $OUTFILE_GIT-energy-$1.log)
175	printf "Gitsource-$1 avg power consumption(J): $avg_en\n" | tee -a $OUTFILE_GIT.result
176
177	# Permance is the number of run gitsource per second, denoted 1/t, where 1 is the number of run gitsource in t
178	# seconds. It is well known that P=E/t, where P is power measured in watts(W), E is energy measured in joules(J),
179	# and t is time measured in seconds(s). This means that performance per watt becomes
180	#        1/t     1/t     1
181	#       ----- = ----- = ---
182	#         P      E/t     E
183	# with unit given by 1 per joule.
184	ppw=`echo "scale=9;1/$avg_en" | bc | awk '{printf "%.9f", $0}'`
185	printf "Gitsource-$1 performance per watt(1/J): $ppw\n" | tee -a $OUTFILE_GIT.result
186	printf "\n" | tee -a $OUTFILE_GIT.result
187
188	driver_name=`echo $(scaling_name)`
189	store_csv_gitsource "$driver_name-$1" "Average" $avg_des_perf $avg_freq $avg_load $avg_time $avg_en $ppw
190}
191
192# $1: base scaling_driver $2: base governor $3: comparison scaling_driver $4: comparison governor
193__calc_comp_gitsource()
194{
195	base=`grep "$1-$2" $OUTFILE_GIT.csv | grep "Average"`
196	comp=`grep "$3-$4" $OUTFILE_GIT.csv | grep "Average"`
197
198	if [ -n "$base" -a -n "$comp" ]; then
199		printf "\n==================================================\n" | tee -a $OUTFILE_GIT.result
200		printf "Gitsource comparison $1-$2 VS $3-$4" | tee -a $OUTFILE_GIT.result
201		printf "\n==================================================\n" | tee -a $OUTFILE_GIT.result
202
203		# get the base values
204		des_perf_base=`echo "$base" | awk '{print $3}' | sed s/,//`
205		freq_base=`echo "$base" | awk '{print $4}' | sed s/,//`
206		load_base=`echo "$base" | awk '{print $5}' | sed s/,//`
207		time_base=`echo "$base" | awk '{print $6}' | sed s/,//`
208		energy_base=`echo "$base" | awk '{print $7}' | sed s/,//`
209		ppw_base=`echo "$base" | awk '{print $8}' | sed s/,//`
210
211		# get the comparison values
212		des_perf_comp=`echo "$comp" | awk '{print $3}' | sed s/,//`
213		freq_comp=`echo "$comp" | awk '{print $4}' | sed s/,//`
214		load_comp=`echo "$comp" | awk '{print $5}' | sed s/,//`
215		time_comp=`echo "$comp" | awk '{print $6}' | sed s/,//`
216		energy_comp=`echo "$comp" | awk '{print $7}' | sed s/,//`
217		ppw_comp=`echo "$comp" | awk '{print $8}' | sed s/,//`
218
219		# compare the base and comp values
220		des_perf_drop=`echo "scale=4;($des_perf_comp-$des_perf_base)*100/$des_perf_base" | bc | awk '{printf "%.4f", $0}'`
221		printf "Gitsource-$1 des perf base: $des_perf_base comprison: $des_perf_comp percent: $des_perf_drop\n" | tee -a $OUTFILE_GIT.result
222
223		freq_drop=`echo "scale=4;($freq_comp-$freq_base)*100/$freq_base" | bc | awk '{printf "%.4f", $0}'`
224		printf "Gitsource-$1 freq base: $freq_base comprison: $freq_comp percent: $freq_drop\n" | tee -a $OUTFILE_GIT.result
225
226		load_drop=`echo "scale=4;($load_comp-$load_base)*100/$load_base" | bc | awk '{printf "%.4f", $0}'`
227		printf "Gitsource-$1 load base: $load_base comprison: $load_comp percent: $load_drop\n" | tee -a $OUTFILE_GIT.result
228
229		time_drop=`echo "scale=4;($time_comp-$time_base)*100/$time_base" | bc | awk '{printf "%.4f", $0}'`
230		printf "Gitsource-$1 time base: $time_base comprison: $time_comp percent: $time_drop\n" | tee -a $OUTFILE_GIT.result
231
232		energy_drop=`echo "scale=4;($energy_comp-$energy_base)*100/$energy_base" | bc | awk '{printf "%.4f", $0}'`
233		printf "Gitsource-$1 energy base: $energy_base comprison: $energy_comp percent: $energy_drop\n" | tee -a $OUTFILE_GIT.result
234
235		ppw_drop=`echo "scale=4;($ppw_comp-$ppw_base)*100/$ppw_base" | bc | awk '{printf "%.4f", $0}'`
236		printf "Gitsource-$1 performance per watt base: $ppw_base comprison: $ppw_comp percent: $ppw_drop\n" | tee -a $OUTFILE_GIT.result
237		printf "\n" | tee -a $OUTFILE_GIT.result
238
239		store_csv_gitsource "$1-$2 VS $3-$4" "Comprison(%)" "$des_perf_drop" "$freq_drop" "$load_drop" "$time_drop" "$energy_drop" "$ppw_drop"
240	fi
241}
242
243# calculate the comparison(%)
244calc_comp_gitsource()
245{
246	# acpi-cpufreq-ondemand VS acpi-cpufreq-schedutil
247	__calc_comp_gitsource ${all_scaling_names[0]} ${gitsource_governors[0]} ${all_scaling_names[0]} ${gitsource_governors[1]}
248
249	# amd-pstate-ondemand VS amd-pstate-schedutil
250	__calc_comp_gitsource ${all_scaling_names[1]} ${gitsource_governors[0]} ${all_scaling_names[1]} ${gitsource_governors[1]}
251
252	# acpi-cpufreq-ondemand VS amd-pstate-ondemand
253	__calc_comp_gitsource ${all_scaling_names[0]} ${gitsource_governors[0]} ${all_scaling_names[1]} ${gitsource_governors[0]}
254
255	# acpi-cpufreq-schedutil VS amd-pstate-schedutil
256	__calc_comp_gitsource ${all_scaling_names[0]} ${gitsource_governors[1]} ${all_scaling_names[1]} ${gitsource_governors[1]}
257}
258
259# $1: file_name, $2: title, $3: ylable, $4: column
260plot_png_gitsource()
261{
262	# all_scaling_names[1] all_scaling_names[0] flag
263	#    amd-pstate           acpi-cpufreq
264	#         N                   N             0
265	#         N                   Y             1
266	#         Y                   N             2
267	#         Y                   Y             3
268	ret=`grep -c "${all_scaling_names[1]}" $OUTFILE_GIT.csv`
269	if [ $ret -eq 0 ]; then
270		ret=`grep -c "${all_scaling_names[0]}" $OUTFILE_GIT.csv`
271		if [ $ret -eq 0 ]; then
272			flag=0
273		else
274			flag=1
275		fi
276	else
277		ret=`grep -c "${all_scaling_names[0]}" $OUTFILE_GIT.csv`
278		if [ $ret -eq 0 ]; then
279			flag=2
280		else
281			flag=3
282		fi
283	fi
284
285	gnuplot << EOF
286		set term png
287		set output "$1"
288
289		set title "$2"
290		set xlabel "Test Cycles (round)"
291		set ylabel "$3"
292
293		set grid
294		set style data histogram
295		set style fill solid 0.5 border
296		set boxwidth 0.8
297
298		if ($flag == 1) {
299			plot \
300			"<(sed -n -e 's/,//g' -e '/${all_scaling_names[0]}-${gitsource_governors[0]}/p' $OUTFILE_GIT.csv)" using $4:xtic(2) title "${all_scaling_names[0]}-${gitsource_governors[0]}", \
301			"<(sed -n -e 's/,//g' -e '/${all_scaling_names[0]}-${gitsource_governors[1]}/p' $OUTFILE_GIT.csv)" using $4:xtic(2) title "${all_scaling_names[0]}-${gitsource_governors[1]}"
302		} else {
303			if ($flag == 2) {
304				plot \
305				"<(sed -n -e 's/,//g' -e '/${all_scaling_names[1]}-${gitsource_governors[0]}/p' $OUTFILE_GIT.csv)" using $4:xtic(2) title "${all_scaling_names[1]}-${gitsource_governors[0]}", \
306				"<(sed -n -e 's/,//g' -e '/${all_scaling_names[1]}-${gitsource_governors[1]}/p' $OUTFILE_GIT.csv)" using $4:xtic(2) title "${all_scaling_names[1]}-${gitsource_governors[1]}"
307			} else {
308				if ($flag == 3 ) {
309					plot \
310					"<(sed -n -e 's/,//g' -e '/${all_scaling_names[0]}-${gitsource_governors[0]}/p' $OUTFILE_GIT.csv)" using $4:xtic(2) title "${all_scaling_names[0]}-${gitsource_governors[0]}", \
311					"<(sed -n -e 's/,//g' -e '/${all_scaling_names[0]}-${gitsource_governors[1]}/p' $OUTFILE_GIT.csv)" using $4:xtic(2) title "${all_scaling_names[0]}-${gitsource_governors[1]}", \
312					"<(sed -n -e 's/,//g' -e '/${all_scaling_names[1]}-${gitsource_governors[0]}/p' $OUTFILE_GIT.csv)" using $4:xtic(2) title "${all_scaling_names[1]}-${gitsource_governors[0]}", \
313					"<(sed -n -e 's/,//g' -e '/${all_scaling_names[1]}-${gitsource_governors[1]}/p' $OUTFILE_GIT.csv)" using $4:xtic(2) title "${all_scaling_names[1]}-${gitsource_governors[1]}"
314				}
315			}
316		}
317		quit
318EOF
319}
320
321amd_pstate_gitsource()
322{
323	printf "\n---------------------------------------------\n"
324	printf "*** Running gitsource                     ***"
325	printf "\n---------------------------------------------\n"
326
327	pre_clear_gitsource
328
329	install_gitsource
330
331	get_lines_csv_gitsource "Governor"
332	if [ $? -eq 0 ]; then
333		# add titles and unit for csv file
334		store_csv_gitsource "Governor" "Round" "Des-perf" "Freq" "Load" "Time" "Energy" "Performance Per Watt"
335		store_csv_gitsource "Unit" "" "" "GHz" "" "s" "J" "1/J"
336	fi
337
338	backup_governor
339	for governor in ${gitsource_governors[*]} ; do
340		printf "\nSpecified governor is $governor\n\n"
341		switch_governor $governor
342		loop_gitsource $governor
343		gather_gitsource $governor
344	done
345	restore_governor
346
347	plot_png_gitsource "gitsource_time.png" "Gitsource Benchmark Time" "Time (s)" 6
348	plot_png_gitsource "gitsource_energy.png" "Gitsource Benchmark Energy" "Energy (J)" 7
349	plot_png_gitsource "gitsource_ppw.png" "Gitsource Benchmark Performance Per Watt" "Performance Per Watt (1/J)" 8
350
351	calc_comp_gitsource
352
353	post_clear_gitsource
354}
355