18c2ecf20Sopenharmony_ci#!/bin/bash
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
38c2ecf20Sopenharmony_ci#
48c2ecf20Sopenharmony_ci# This test runs on Intel x86 based hardware which support the intel_pstate
58c2ecf20Sopenharmony_ci# driver.  The test checks the frequency settings from the maximum turbo
68c2ecf20Sopenharmony_ci# state to the minimum supported frequency, in decrements of 100MHz.  The
78c2ecf20Sopenharmony_ci# test runs the aperf.c program to put load on each processor.
88c2ecf20Sopenharmony_ci#
98c2ecf20Sopenharmony_ci# The results are displayed in a table which indicate the "Target" state,
108c2ecf20Sopenharmony_ci# or the requested frequency in MHz, the Actual frequency, as read from
118c2ecf20Sopenharmony_ci# /proc/cpuinfo, the difference between the Target and Actual frequencies,
128c2ecf20Sopenharmony_ci# and the value of MSR 0x199 (MSR_IA32_PERF_CTL) which indicates what
138c2ecf20Sopenharmony_ci# pstate the cpu is in, and the value of
148c2ecf20Sopenharmony_ci# /sys/devices/system/cpu/intel_pstate/max_perf_pct X maximum turbo state
158c2ecf20Sopenharmony_ci#
168c2ecf20Sopenharmony_ci# Notes: In some cases several frequency values may be placed in the
178c2ecf20Sopenharmony_ci# /tmp/result.X files.  This is done on purpose in order to catch cases
188c2ecf20Sopenharmony_ci# where the pstate driver may not be working at all.  There is the case
198c2ecf20Sopenharmony_ci# where, for example, several "similar" frequencies are in the file:
208c2ecf20Sopenharmony_ci#
218c2ecf20Sopenharmony_ci#
228c2ecf20Sopenharmony_ci#/tmp/result.3100:1:cpu MHz              : 2899.980
238c2ecf20Sopenharmony_ci#/tmp/result.3100:2:cpu MHz              : 2900.000
248c2ecf20Sopenharmony_ci#/tmp/result.3100:3:msr 0x199: 0x1e00
258c2ecf20Sopenharmony_ci#/tmp/result.3100:4:max_perf_pct 94
268c2ecf20Sopenharmony_ci#
278c2ecf20Sopenharmony_ci# and the test will error out in those cases.  The result.X file can be checked
288c2ecf20Sopenharmony_ci# for consistency and modified to remove the extra MHz values.  The result.X
298c2ecf20Sopenharmony_ci# files can be re-evaluated by setting EVALUATE_ONLY to 1 below.
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciEVALUATE_ONLY=0
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci# Kselftest framework requirement - SKIP code is 4.
348c2ecf20Sopenharmony_ciksft_skip=4
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciif ! uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ | grep -q x86; then
378c2ecf20Sopenharmony_ci	echo "$0 # Skipped: Test can only run on x86 architectures."
388c2ecf20Sopenharmony_ci	exit $ksft_skip
398c2ecf20Sopenharmony_cifi
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cimsg="skip all tests:"
428c2ecf20Sopenharmony_ciif [ $UID != 0 ] && [ $EVALUATE_ONLY == 0 ]; then
438c2ecf20Sopenharmony_ci    echo $msg please run this as root >&2
448c2ecf20Sopenharmony_ci    exit $ksft_skip
458c2ecf20Sopenharmony_cifi
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cimax_cpus=$(($(nproc)-1))
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cifunction run_test () {
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	file_ext=$1
528c2ecf20Sopenharmony_ci	for cpu in `seq 0 $max_cpus`
538c2ecf20Sopenharmony_ci	do
548c2ecf20Sopenharmony_ci		echo "launching aperf load on $cpu"
558c2ecf20Sopenharmony_ci		./aperf $cpu &
568c2ecf20Sopenharmony_ci	done
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	echo "sleeping for 5 seconds"
598c2ecf20Sopenharmony_ci	sleep 5
608c2ecf20Sopenharmony_ci	grep MHz /proc/cpuinfo | sort -u > /tmp/result.freqs
618c2ecf20Sopenharmony_ci	num_freqs=$(wc -l /tmp/result.freqs | awk ' { print $1 } ')
628c2ecf20Sopenharmony_ci	if [ $num_freqs -ge 2 ]; then
638c2ecf20Sopenharmony_ci		tail -n 1 /tmp/result.freqs > /tmp/result.$1
648c2ecf20Sopenharmony_ci	else
658c2ecf20Sopenharmony_ci		cp /tmp/result.freqs /tmp/result.$1
668c2ecf20Sopenharmony_ci	fi
678c2ecf20Sopenharmony_ci	./msr 0 >> /tmp/result.$1
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	max_perf_pct=$(cat /sys/devices/system/cpu/intel_pstate/max_perf_pct)
708c2ecf20Sopenharmony_ci	echo "max_perf_pct $max_perf_pct" >> /tmp/result.$1
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	for job in `jobs -p`
738c2ecf20Sopenharmony_ci	do
748c2ecf20Sopenharmony_ci		echo "waiting for job id $job"
758c2ecf20Sopenharmony_ci		wait $job
768c2ecf20Sopenharmony_ci	done
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#
808c2ecf20Sopenharmony_ci# MAIN (ALL UNITS IN MHZ)
818c2ecf20Sopenharmony_ci#
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci# Get the marketing frequency
848c2ecf20Sopenharmony_ci_mkt_freq=$(cat /proc/cpuinfo | grep -m 1 "model name" | awk '{print $NF}')
858c2ecf20Sopenharmony_ci_mkt_freq=$(echo $_mkt_freq | tr -d [:alpha:][:punct:])
868c2ecf20Sopenharmony_cimkt_freq=${_mkt_freq}0
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci# Get the ranges from cpupower
898c2ecf20Sopenharmony_ci_min_freq=$(cpupower frequency-info -l | tail -1 | awk ' { print $1 } ')
908c2ecf20Sopenharmony_cimin_freq=$(($_min_freq / 1000))
918c2ecf20Sopenharmony_ci_max_freq=$(cpupower frequency-info -l | tail -1 | awk ' { print $2 } ')
928c2ecf20Sopenharmony_cimax_freq=$(($_max_freq / 1000))
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci[ $EVALUATE_ONLY -eq 0 ] && for freq in `seq $max_freq -100 $min_freq`
968c2ecf20Sopenharmony_cido
978c2ecf20Sopenharmony_ci	echo "Setting maximum frequency to $freq"
988c2ecf20Sopenharmony_ci	cpupower frequency-set -g powersave --max=${freq}MHz >& /dev/null
998c2ecf20Sopenharmony_ci	run_test $freq
1008c2ecf20Sopenharmony_cidone
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci[ $EVALUATE_ONLY -eq 0 ] && cpupower frequency-set -g powersave --max=${max_freq}MHz >& /dev/null
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciecho "========================================================================"
1058c2ecf20Sopenharmony_ciecho "The marketing frequency of the cpu is $mkt_freq MHz"
1068c2ecf20Sopenharmony_ciecho "The maximum frequency of the cpu is $max_freq MHz"
1078c2ecf20Sopenharmony_ciecho "The minimum frequency of the cpu is $min_freq MHz"
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci# make a pretty table
1108c2ecf20Sopenharmony_ciecho "Target Actual Difference MSR(0x199) max_perf_pct" | tr " " "\n" > /tmp/result.tab
1118c2ecf20Sopenharmony_cifor freq in `seq $max_freq -100 $min_freq`
1128c2ecf20Sopenharmony_cido
1138c2ecf20Sopenharmony_ci	result_freq=$(cat /tmp/result.${freq} | grep "cpu MHz" | awk ' { print $4 } ' | awk -F "." ' { print $1 } ')
1148c2ecf20Sopenharmony_ci	msr=$(cat /tmp/result.${freq} | grep "msr" | awk ' { print $3 } ')
1158c2ecf20Sopenharmony_ci	max_perf_pct=$(cat /tmp/result.${freq} | grep "max_perf_pct" | awk ' { print $2 } ' )
1168c2ecf20Sopenharmony_ci	cat >> /tmp/result.tab << EOF
1178c2ecf20Sopenharmony_ci$freq
1188c2ecf20Sopenharmony_ci$result_freq
1198c2ecf20Sopenharmony_ci$((result_freq - freq))
1208c2ecf20Sopenharmony_ci$msr
1218c2ecf20Sopenharmony_ci$((max_perf_pct * max_freq))
1228c2ecf20Sopenharmony_ciEOF
1238c2ecf20Sopenharmony_cidone
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci# print the table
1268c2ecf20Sopenharmony_cipr -aTt -5 < /tmp/result.tab
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciexit 0
129