Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / amd-pstate / run.sh
blobb053eea8bb19ca42fc9739331908409ebee8adb7
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # protect against multiple inclusion
5 if [ $FILE_MAIN ]; then
6 return 0
7 else
8 FILE_MAIN=DONE
9 fi
11 SCRIPTDIR=`dirname "$0"`
12 TRACER=$SCRIPTDIR/../../../power/x86/amd_pstate_tracer/amd_pstate_trace.py
14 source $SCRIPTDIR/basic.sh
15 source $SCRIPTDIR/tbench.sh
16 source $SCRIPTDIR/gitsource.sh
18 # amd-pstate-ut only run on x86/x86_64 AMD systems.
19 ARCH=$(uname -m 2>/dev/null | sed -e 's/i.86/x86/' -e 's/x86_64/x86/')
20 VENDOR=$(cat /proc/cpuinfo | grep -m 1 'vendor_id' | awk '{print $NF}')
22 msg="Skip all tests:"
23 FUNC=all
24 OUTFILE=selftest
25 OUTFILE_TBENCH="$OUTFILE.tbench"
26 OUTFILE_GIT="$OUTFILE.gitsource"
28 PERF=/usr/bin/perf
29 SYSFS=
30 CPUROOT=
31 CPUFREQROOT=
32 MAKE_CPUS=
34 TIME_LIMIT=100
35 PROCESS_NUM=128
36 LOOP_TIMES=3
37 TRACER_INTERVAL=10
38 CURRENT_TEST=amd-pstate
39 COMPARATIVE_TEST=
41 # Kselftest framework requirement - SKIP code is 4.
42 ksft_skip=4
43 all_scaling_names=("acpi-cpufreq" "amd-pstate")
45 # Get current cpufreq scaling driver name
46 scaling_name()
48 if [ "$COMPARATIVE_TEST" = "" ]; then
49 echo "$CURRENT_TEST"
50 else
51 echo "$COMPARATIVE_TEST"
55 # Counts CPUs with cpufreq directories
56 count_cpus()
58 count=0;
60 for cpu in `ls $CPUROOT | grep "cpu[0-9].*"`; do
61 if [ -d $CPUROOT/$cpu/cpufreq ]; then
62 let count=count+1;
64 done
66 echo $count;
69 # $1: policy
70 find_current_governor()
72 cat $CPUFREQROOT/$1/scaling_governor
75 backup_governor()
77 policies=$(ls $CPUFREQROOT| grep "policy[0-9].*")
78 for policy in $policies; do
79 cur_gov=$(find_current_governor $policy)
80 echo "$policy $cur_gov" >> $OUTFILE.backup_governor.log
81 done
83 printf "Governor $cur_gov backup done.\n"
86 restore_governor()
88 i=0;
90 policies=$(awk '{print $1}' $OUTFILE.backup_governor.log)
91 for policy in $policies; do
92 let i++;
93 governor=$(sed -n ''$i'p' $OUTFILE.backup_governor.log | awk '{print $2}')
95 # switch governor
96 echo $governor > $CPUFREQROOT/$policy/scaling_governor
97 done
99 printf "Governor restored to $governor.\n"
102 # $1: governor
103 switch_governor()
105 policies=$(ls $CPUFREQROOT| grep "policy[0-9].*")
106 for policy in $policies; do
107 filepath=$CPUFREQROOT/$policy/scaling_available_governors
109 # Exit if cpu isn't managed by cpufreq core
110 if [ ! -f $filepath ]; then
111 return;
114 echo $1 > $CPUFREQROOT/$policy/scaling_governor
115 done
117 printf "Switched governor to $1.\n"
120 # All amd-pstate tests
121 amd_pstate_all()
123 printf "\n=============================================\n"
124 printf "***** Running AMD P-state Sanity Tests *****\n"
125 printf "=============================================\n\n"
127 count=$(count_cpus)
128 if [ $count = 0 ]; then
129 printf "No cpu is managed by cpufreq core, exiting\n"
130 exit;
131 else
132 printf "AMD P-state manages: $count CPUs\n"
135 # unit test for amd-pstate kernel driver
136 amd_pstate_basic
138 # tbench
139 amd_pstate_tbench
141 # gitsource
142 amd_pstate_gitsource
145 help()
147 printf "Usage: $0 [OPTION...]
148 [-h <help>]
149 [-o <output-file-for-dump>]
150 [-c <all: All testing,
151 basic: Basic testing,
152 tbench: Tbench testing,
153 gitsource: Gitsource testing.>]
154 [-t <tbench time limit>]
155 [-p <tbench process number>]
156 [-l <loop times for tbench>]
157 [-i <amd tracer interval>]
158 [-b <perf binary>]
159 [-m <comparative test: acpi-cpufreq>]
161 exit 2
164 parse_arguments()
166 while getopts ho:c:t:p:l:i:b:m: arg
168 case $arg in
169 h) # --help
170 help
173 c) # --func_type (Function to perform: basic, tbench, gitsource (default: all))
174 FUNC=$OPTARG
177 o) # --output-file (Output file to store dumps)
178 OUTFILE=$OPTARG
181 t) # --tbench-time-limit
182 TIME_LIMIT=$OPTARG
185 p) # --tbench-process-number
186 PROCESS_NUM=$OPTARG
189 l) # --tbench/gitsource-loop-times
190 LOOP_TIMES=$OPTARG
193 i) # --amd-tracer-interval
194 TRACER_INTERVAL=$OPTARG
197 b) # --perf-binary
198 PERF=`realpath $OPTARG`
201 m) # --comparative-test
202 COMPARATIVE_TEST=$OPTARG
206 help
208 esac
209 done
212 command_perf()
214 if ! $PERF -v; then
215 echo $msg please install perf or provide perf binary path as argument >&2
216 exit $ksft_skip
220 command_tbench()
222 if ! command -v tbench > /dev/null; then
223 if apt policy dbench > /dev/null 2>&1; then
224 echo $msg apt install dbench >&2
225 exit $ksft_skip
226 elif yum list available | grep dbench > /dev/null 2>&1; then
227 echo $msg yum install dbench >&2
228 exit $ksft_skip
232 if ! command -v tbench > /dev/null; then
233 echo $msg please install tbench. >&2
234 exit $ksft_skip
238 prerequisite()
240 if ! echo "$ARCH" | grep -q x86; then
241 echo "$0 # Skipped: Test can only run on x86 architectures."
242 exit $ksft_skip
245 if ! echo "$VENDOR" | grep -iq amd; then
246 echo "$0 # Skipped: Test can only run on AMD CPU."
247 echo "$0 # Current cpu vendor is $VENDOR."
248 exit $ksft_skip
251 scaling_driver=$(cat /sys/devices/system/cpu/cpufreq/policy0/scaling_driver)
252 if [ "$COMPARATIVE_TEST" = "" ]; then
253 if [ "$scaling_driver" != "$CURRENT_TEST" ]; then
254 echo "$0 # Skipped: Test can only run on $CURRENT_TEST driver or run comparative test."
255 echo "$0 # Please set X86_AMD_PSTATE enabled or run comparative test."
256 echo "$0 # Current cpufreq scaling driver is $scaling_driver."
257 exit $ksft_skip
259 else
260 case "$FUNC" in
261 "tbench" | "gitsource")
262 if [ "$scaling_driver" != "$COMPARATIVE_TEST" ]; then
263 echo "$0 # Skipped: Comparison test can only run on $COMPARISON_TEST driver."
264 echo "$0 # Current cpufreq scaling driver is $scaling_driver."
265 exit $ksft_skip
270 echo "$0 # Skipped: Comparison test are only for tbench or gitsource."
271 echo "$0 # Current comparative test is for $FUNC."
272 exit $ksft_skip
274 esac
277 if [ ! -w /dev ]; then
278 echo $msg please run this as root >&2
279 exit $ksft_skip
282 case "$FUNC" in
283 "all")
284 command_perf
285 command_tbench
288 "tbench")
289 command_perf
290 command_tbench
293 "gitsource")
294 command_perf
296 esac
298 SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
300 if [ ! -d "$SYSFS" ]; then
301 echo $msg sysfs is not mounted >&2
302 exit 2
305 CPUROOT=$SYSFS/devices/system/cpu
306 CPUFREQROOT="$CPUROOT/cpufreq"
308 if ! ls $CPUROOT/cpu* > /dev/null 2>&1; then
309 echo $msg cpus not available in sysfs >&2
310 exit 2
313 if ! ls $CPUROOT/cpufreq > /dev/null 2>&1; then
314 echo $msg cpufreq directory not available in sysfs >&2
315 exit 2
319 do_test()
321 # Check if CPUs are managed by cpufreq or not
322 count=$(count_cpus)
323 MAKE_CPUS=$((count*2))
325 if [ $count = 0 ]; then
326 echo "No cpu is managed by cpufreq core, exiting"
327 exit 2;
330 case "$FUNC" in
331 "all")
332 amd_pstate_all
335 "basic")
336 amd_pstate_basic
339 "tbench")
340 amd_pstate_tbench
343 "gitsource")
344 amd_pstate_gitsource
348 echo "Invalid [-f] function type"
349 help
351 esac
354 # clear dumps
355 pre_clear_dumps()
357 case "$FUNC" in
358 "all")
359 rm -rf $OUTFILE.log
360 rm -rf $OUTFILE.backup_governor.log
361 rm -rf *.png
364 "tbench")
365 rm -rf $OUTFILE.log
366 rm -rf $OUTFILE.backup_governor.log
367 rm -rf tbench_*.png
370 "gitsource")
371 rm -rf $OUTFILE.log
372 rm -rf $OUTFILE.backup_governor.log
373 rm -rf gitsource_*.png
378 esac
381 post_clear_dumps()
383 rm -rf $OUTFILE.log
384 rm -rf $OUTFILE.backup_governor.log
387 # Parse arguments
388 parse_arguments $@
390 # Make sure all requirements are met
391 prerequisite
393 # Run requested functions
394 pre_clear_dumps
395 do_test | tee -a $OUTFILE.log
396 post_clear_dumps