xref: /linux/tools/testing/selftests/intel_pstate/run.sh (revision 3eb66e91a25497065c5322b1268cbc3953642227)
1ed2d26d7SPrarit Bhargava#!/bin/bash
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
3ed2d26d7SPrarit Bhargava#
4ed2d26d7SPrarit Bhargava# This test runs on Intel x86 based hardware which support the intel_pstate
5ed2d26d7SPrarit Bhargava# driver.  The test checks the frequency settings from the maximum turbo
6ed2d26d7SPrarit Bhargava# state to the minimum supported frequency, in decrements of 100MHz.  The
7ed2d26d7SPrarit Bhargava# test runs the aperf.c program to put load on each processor.
8ed2d26d7SPrarit Bhargava#
9ed2d26d7SPrarit Bhargava# The results are displayed in a table which indicate the "Target" state,
10ed2d26d7SPrarit Bhargava# or the requested frequency in MHz, the Actual frequency, as read from
11ed2d26d7SPrarit Bhargava# /proc/cpuinfo, the difference between the Target and Actual frequencies,
12ed2d26d7SPrarit Bhargava# and the value of MSR 0x199 (MSR_IA32_PERF_CTL) which indicates what
13ed2d26d7SPrarit Bhargava# pstate the cpu is in, and the value of
14ed2d26d7SPrarit Bhargava# /sys/devices/system/cpu/intel_pstate/max_perf_pct X maximum turbo state
15ed2d26d7SPrarit Bhargava#
16ed2d26d7SPrarit Bhargava# Notes: In some cases several frequency values may be placed in the
17ed2d26d7SPrarit Bhargava# /tmp/result.X files.  This is done on purpose in order to catch cases
18ed2d26d7SPrarit Bhargava# where the pstate driver may not be working at all.  There is the case
19ed2d26d7SPrarit Bhargava# where, for example, several "similar" frequencies are in the file:
20ed2d26d7SPrarit Bhargava#
21ed2d26d7SPrarit Bhargava#
22ed2d26d7SPrarit Bhargava#/tmp/result.3100:1:cpu MHz              : 2899.980
23ed2d26d7SPrarit Bhargava#/tmp/result.3100:2:cpu MHz              : 2900.000
24ed2d26d7SPrarit Bhargava#/tmp/result.3100:3:msr 0x199: 0x1e00
25ed2d26d7SPrarit Bhargava#/tmp/result.3100:4:max_perf_pct 94
26ed2d26d7SPrarit Bhargava#
27ed2d26d7SPrarit Bhargava# and the test will error out in those cases.  The result.X file can be checked
28ed2d26d7SPrarit Bhargava# for consistency and modified to remove the extra MHz values.  The result.X
29ed2d26d7SPrarit Bhargava# files can be re-evaluated by setting EVALUATE_ONLY to 1 below.
30ed2d26d7SPrarit Bhargava
31ed2d26d7SPrarit BhargavaEVALUATE_ONLY=0
32ed2d26d7SPrarit Bhargava
335c30a038SShuah Khan (Samsung OSG)# Kselftest framework requirement - SKIP code is 4.
345c30a038SShuah Khan (Samsung OSG)ksft_skip=4
355c30a038SShuah Khan (Samsung OSG)
3667b2e30eSDaniel Díazif ! uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ | grep -q x86; then
3767b2e30eSDaniel Díaz	echo "$0 # Skipped: Test can only run on x86 architectures."
385c30a038SShuah Khan (Samsung OSG)	exit $ksft_skip
3967b2e30eSDaniel Díazfi
4067b2e30eSDaniel Díaz
41*9070ee31SJeffrin Jose Tmsg="skip all tests:"
42*9070ee31SJeffrin Jose Tif [ $UID != 0 ] && [ $EVALUATE_ONLY == 0 ]; then
43*9070ee31SJeffrin Jose T    echo $msg please run this as root >&2
44*9070ee31SJeffrin Jose T    exit $ksft_skip
45*9070ee31SJeffrin Jose Tfi
46*9070ee31SJeffrin Jose T
47ed2d26d7SPrarit Bhargavamax_cpus=$(($(nproc)-1))
48ed2d26d7SPrarit Bhargava
49ed2d26d7SPrarit Bhargavafunction run_test () {
50ed2d26d7SPrarit Bhargava
51ed2d26d7SPrarit Bhargava	file_ext=$1
52ed2d26d7SPrarit Bhargava	for cpu in `seq 0 $max_cpus`
53ed2d26d7SPrarit Bhargava	do
54ed2d26d7SPrarit Bhargava		echo "launching aperf load on $cpu"
55ed2d26d7SPrarit Bhargava		./aperf $cpu &
56ed2d26d7SPrarit Bhargava	done
57ed2d26d7SPrarit Bhargava
58ed2d26d7SPrarit Bhargava	echo "sleeping for 5 seconds"
59ed2d26d7SPrarit Bhargava	sleep 5
60e9d33f14SDaniel Díaz	grep MHz /proc/cpuinfo | sort -u > /tmp/result.freqs
61e9d33f14SDaniel Díaz	num_freqs=$(wc -l /tmp/result.freqs | awk ' { print $1 } ')
62e9d33f14SDaniel Díaz	if [ $num_freqs -ge 2 ]; then
63e9d33f14SDaniel Díaz		tail -n 1 /tmp/result.freqs > /tmp/result.$1
64ed2d26d7SPrarit Bhargava	else
65e9d33f14SDaniel Díaz		cp /tmp/result.freqs /tmp/result.$1
66ed2d26d7SPrarit Bhargava	fi
67ed2d26d7SPrarit Bhargava	./msr 0 >> /tmp/result.$1
68ed2d26d7SPrarit Bhargava
69ed2d26d7SPrarit Bhargava	max_perf_pct=$(cat /sys/devices/system/cpu/intel_pstate/max_perf_pct)
70ed2d26d7SPrarit Bhargava	echo "max_perf_pct $max_perf_pct" >> /tmp/result.$1
71ed2d26d7SPrarit Bhargava
72ed2d26d7SPrarit Bhargava	for job in `jobs -p`
73ed2d26d7SPrarit Bhargava	do
74ed2d26d7SPrarit Bhargava		echo "waiting for job id $job"
75ed2d26d7SPrarit Bhargava		wait $job
76ed2d26d7SPrarit Bhargava	done
77ed2d26d7SPrarit Bhargava}
78ed2d26d7SPrarit Bhargava
79ed2d26d7SPrarit Bhargava#
80ed2d26d7SPrarit Bhargava# MAIN (ALL UNITS IN MHZ)
81ed2d26d7SPrarit Bhargava#
82ed2d26d7SPrarit Bhargava
83ed2d26d7SPrarit Bhargava# Get the marketing frequency
84ed2d26d7SPrarit Bhargava_mkt_freq=$(cat /proc/cpuinfo | grep -m 1 "model name" | awk '{print $NF}')
85ed2d26d7SPrarit Bhargava_mkt_freq=$(echo $_mkt_freq | tr -d [:alpha:][:punct:])
86ed2d26d7SPrarit Bhargavamkt_freq=${_mkt_freq}0
87ed2d26d7SPrarit Bhargava
88ed2d26d7SPrarit Bhargava# Get the ranges from cpupower
89ed2d26d7SPrarit Bhargava_min_freq=$(cpupower frequency-info -l | tail -1 | awk ' { print $1 } ')
90ed2d26d7SPrarit Bhargavamin_freq=$(($_min_freq / 1000))
91ed2d26d7SPrarit Bhargava_max_freq=$(cpupower frequency-info -l | tail -1 | awk ' { print $2 } ')
92ed2d26d7SPrarit Bhargavamax_freq=$(($_max_freq / 1000))
93ed2d26d7SPrarit Bhargava
94ed2d26d7SPrarit Bhargava
95e9d33f14SDaniel Díaz[ $EVALUATE_ONLY -eq 0 ] && for freq in `seq $max_freq -100 $min_freq`
96ed2d26d7SPrarit Bhargavado
97ed2d26d7SPrarit Bhargava	echo "Setting maximum frequency to $freq"
98ed2d26d7SPrarit Bhargava	cpupower frequency-set -g powersave --max=${freq}MHz >& /dev/null
99e9d33f14SDaniel Díaz	run_test $freq
100ed2d26d7SPrarit Bhargavadone
101ed2d26d7SPrarit Bhargava
102e9d33f14SDaniel Díaz[ $EVALUATE_ONLY -eq 0 ] && cpupower frequency-set -g powersave --max=${max_freq}MHz >& /dev/null
103ed2d26d7SPrarit Bhargava
1047b04d1e9SDaniel Díazecho "========================================================================"
105ed2d26d7SPrarit Bhargavaecho "The marketing frequency of the cpu is $mkt_freq MHz"
106ed2d26d7SPrarit Bhargavaecho "The maximum frequency of the cpu is $max_freq MHz"
107ed2d26d7SPrarit Bhargavaecho "The minimum frequency of the cpu is $min_freq MHz"
108ed2d26d7SPrarit Bhargava
109ed2d26d7SPrarit Bhargava# make a pretty table
1107b04d1e9SDaniel Díazecho "Target Actual Difference MSR(0x199) max_perf_pct" | tr " " "\n" > /tmp/result.tab
111ed2d26d7SPrarit Bhargavafor freq in `seq $max_freq -100 $min_freq`
112ed2d26d7SPrarit Bhargavado
113ed2d26d7SPrarit Bhargava	result_freq=$(cat /tmp/result.${freq} | grep "cpu MHz" | awk ' { print $4 } ' | awk -F "." ' { print $1 } ')
114ed2d26d7SPrarit Bhargava	msr=$(cat /tmp/result.${freq} | grep "msr" | awk ' { print $3 } ')
115ed2d26d7SPrarit Bhargava	max_perf_pct=$(cat /tmp/result.${freq} | grep "max_perf_pct" | awk ' { print $2 } ' )
1167b04d1e9SDaniel Díaz	cat >> /tmp/result.tab << EOF
1177b04d1e9SDaniel Díaz$freq
1187b04d1e9SDaniel Díaz$result_freq
1197b04d1e9SDaniel Díaz$((result_freq - freq))
1207b04d1e9SDaniel Díaz$msr
1217b04d1e9SDaniel Díaz$((max_perf_pct * max_freq))
1227b04d1e9SDaniel DíazEOF
123ed2d26d7SPrarit Bhargavadone
1247b04d1e9SDaniel Díaz
1257b04d1e9SDaniel Díaz# print the table
1267b04d1e9SDaniel Díazpr -aTt -5 < /tmp/result.tab
1277b04d1e9SDaniel Díaz
128ed2d26d7SPrarit Bhargavaexit 0
129