brintos

brintos / linux-shallow public Read only

0
0
Text · 4.8 KiB · e350c52 Raw
257 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4# protect against multiple inclusion5if [ $FILE_CPUFREQ ]; then6	return 07else8	FILE_CPUFREQ=DONE9fi10 11source cpu.sh12 13 14# $1: cpu15cpu_should_have_cpufreq_directory()16{17	if [ ! -d $CPUROOT/$1/cpufreq ]; then18		printf "Warning: No cpufreq directory present for $1\n"19	fi20}21 22cpu_should_not_have_cpufreq_directory()23{24	if [ -d $CPUROOT/$1/cpufreq ]; then25		printf "Warning: cpufreq directory present for $1\n"26	fi27}28 29for_each_policy()30{31	policies=$(ls $CPUFREQROOT| grep "policy[0-9].*")32	for policy in $policies; do33		$@ $policy34	done35}36 37for_each_policy_concurrent()38{39	policies=$(ls $CPUFREQROOT| grep "policy[0-9].*")40	for policy in $policies; do41		$@ $policy &42	done43}44 45# $1: Path46read_cpufreq_files_in_dir()47{48	local files=`ls $1`49 50	printf "Printing directory: $1\n\n"51 52	for file in $files; do53		if [ -f $1/$file ]; then54			printf "$file:"55			cat $1/$file56		else57			printf "\n"58			read_cpufreq_files_in_dir "$1/$file"59		fi60	done61	printf "\n"62}63 64 65read_all_cpufreq_files()66{67	printf "** Test: Running ${FUNCNAME[0]} **\n\n"68 69	read_cpufreq_files_in_dir $CPUFREQROOT70 71	printf "%s\n\n" "------------------------------------------------"72}73 74 75# UPDATE CPUFREQ FILES76 77# $1: directory path78update_cpufreq_files_in_dir()79{80	local files=`ls $1`81 82	printf "Updating directory: $1\n\n"83 84	for file in $files; do85		if [ -f $1/$file ]; then86			# file is writable ?87			local wfile=$(ls -l $1/$file | awk '$1 ~ /^.*w.*/ { print $NF; }')88 89			if [ ! -z $wfile ]; then90				# scaling_setspeed is a special file and we91				# should skip updating it92				if [ $file != "scaling_setspeed" ]; then93					local val=$(cat $1/$file)94					printf "Writing $val to: $file\n"95					echo $val > $1/$file96				fi97			fi98		else99			printf "\n"100			update_cpufreq_files_in_dir "$1/$file"101		fi102	done103 104	printf "\n"105}106 107# Update all writable files with their existing values108update_all_cpufreq_files()109{110	printf "** Test: Running ${FUNCNAME[0]} **\n\n"111 112	update_cpufreq_files_in_dir $CPUFREQROOT113 114	printf "%s\n\n" "------------------------------------------------"115}116 117 118# CHANGE CPU FREQUENCIES119 120# $1: policy121find_current_freq()122{123	cat $CPUFREQROOT/$1/scaling_cur_freq124}125 126# $1: policy127# $2: frequency128set_cpu_frequency()129{130	printf "Change frequency for $1 to $2\n"131	echo $2 > $CPUFREQROOT/$1/scaling_setspeed132}133 134# $1: policy135test_all_frequencies()136{137	local filepath="$CPUFREQROOT/$1"138 139	backup_governor $1140 141	local found=$(switch_governor $1 "userspace")142	if [ $found = 1 ]; then143		printf "${FUNCNAME[0]}: userspace governor not available for: $1\n"144		return;145	fi146 147	printf "Switched governor for $1 to userspace\n\n"148 149	local freqs=$(cat $filepath/scaling_available_frequencies)150	printf "Available frequencies for $1: $freqs\n\n"151 152	# Set all frequencies one-by-one153	for freq in $freqs; do154		set_cpu_frequency $1 $freq155	done156 157	printf "\n"158 159	restore_governor $1160}161 162# $1: loop count163shuffle_frequency_for_all_cpus()164{165	printf "** Test: Running ${FUNCNAME[0]} for $1 loops **\n\n"166 167	for i in `seq 1 $1`; do168		for_each_policy test_all_frequencies169	done170	printf "\n%s\n\n" "------------------------------------------------"171}172 173# Basic cpufreq tests174cpufreq_basic_tests()175{176	printf "*** RUNNING CPUFREQ SANITY TESTS ***\n"177	printf "====================================\n\n"178 179	count=$(count_cpufreq_managed_cpus)180	if [ $count = 0 ]; then181		ktap_exit_fail_msg "No cpu is managed by cpufreq core, exiting\n"182	else183		printf "CPUFreq manages: $count CPUs\n\n"184	fi185 186	# Detect & print which CPUs are not managed by cpufreq187	print_unmanaged_cpus188 189	# read/update all cpufreq files190	read_all_cpufreq_files191	update_all_cpufreq_files192 193	# hotplug cpus194	reboot_cpus 5195 196	# Test all frequencies197	shuffle_frequency_for_all_cpus 2198 199	# Test all governors200	shuffle_governors_for_all_cpus 1201}202 203# Suspend/resume204# $1: "suspend" or "hibernate", $2: loop count205do_suspend()206{207	printf "** Test: Running ${FUNCNAME[0]}: Trying $1 for $2 loops **\n\n"208 209	# Is the directory available210	if [ ! -d $SYSFS/power/ -o ! -f $SYSFS/power/state ]; then211		printf "$SYSFS/power/state not available\n"212		return 1213	fi214 215	if [ $1 = "suspend" ]; then216		filename="mem"217	elif [ $1 = "hibernate" ]; then218		filename="disk"219	else220		printf "$1 is not a valid option\n"221		return 1222	fi223 224	if [ -n $filename ]; then225		present=$(cat $SYSFS/power/state | grep $filename)226 227		if [ -z "$present" ]; then228			printf "Tried to $1 but $filename isn't present in $SYSFS/power/state\n"229			return 1;230		fi231 232		for i in `seq 1 $2`; do233			printf "Starting $1\n"234 235			if [ "$3" = "rtc" ]; then236				if ! command -v rtcwake &> /dev/null; then237					printf "rtcwake could not be found, please install it.\n"238					return 1239				fi240 241				rtcwake -m $filename -s 15242 243				if [ $? -ne 0 ]; then244					printf "Failed to suspend using RTC wake alarm\n"245					return 1246				fi247			fi248 249			echo $filename > $SYSFS/power/state250			printf "Came out of $1\n"251 252			printf "Do basic tests after finishing $1 to verify cpufreq state\n\n"253			cpufreq_basic_tests254		done255	fi256}257