Last active
April 12, 2020 11:27
-
-
Save stanislavvv/0f1f5531b5d8fdf0ca9d81a0c24ff327 to your computer and use it in GitHub Desktop.
set scaling_max_freq for all cpu in system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -eu | |
# set max cpu scaling frequency | |
# get limits in MHz | |
declare -r MIN_KHZ=$( cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_min_freq ) | |
declare -r MAX_KHZ=$( cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq ) | |
declare -r MIN_MHZ=$(( MIN_KHZ / 1000 )) | |
declare -r MAX_MHZ=$(( MAX_KHZ / 1000 )) | |
# func used in errors | |
fatal() { | |
echo "$1" 1>&2 | |
exit 1 | |
} | |
# echo usage and limits if no parameters | |
[[ $# > 0 ]] || { | |
declare -r CURR=$( cat /sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq ) | |
declare -r CURR_MHZ=$(( $CURR / 1000 )) | |
echo "Usage: $0 <max_freq_in_MHz>" | |
echo "Min freq: $MIN_MHZ MHz" | |
echo "Max freq: $MAX_MHZ MHz" | |
echo "Current freq: $CURR_MHZ MHz" | |
exit 0 | |
} | |
# get freq parameter | |
declare -r FREQ_MHZ="${1:0}" | |
# non-numeric parameter? | |
[[ $FREQ_MHZ =~ ^[0-9]+$ ]] || fatal "Input frequency not a number !" | |
# frequency greater then max? | |
[[ $FREQ_MHZ -le $MAX_MHZ ]] || fatal "Input frequency > $MAX_MHZ !" | |
# frequency lower than min? | |
[[ $FREQ_MHZ -ge $MIN_MHZ ]] || fatal "Input frequency < $MIN_MHZ !" | |
# set frequency | |
declare -r FREQ=$(( FREQ_MHZ * 1000 )) | |
for i in /sys/devices/system/cpu/cpufreq/policy*/scaling_max_freq; do | |
echo "$FREQ" > $i | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment