Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tl/set governor on turbo state #80

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 84 additions & 128 deletions src/cpufreqctl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -e

# cpufreqctl - This script can configure the pstate driver of your intel CPU.
#
Expand All @@ -21,207 +21,163 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

set -e

log ()
{
function log {
echo "$@" >&2 || true
}

show_usage ()
{
function err {
show_usage
echo "error: $@" >&2 && false
}

function show_usage {
log "usage: cpufreqctl [-h] [turbo {get,0,1}] [min {get,check,VALUE}] [max {get,VALUE}]"
}

show_help ()
{
function show_help {
log "usage: cpufreqctl [-h] ACTION PARAMETER"
log
log "This script can configure the pstate driver of your intel CPU."
log

help_arguments
help_actions
help_turbo
help_min
help_max
help_examples
help_copyright
exit
}

help_arguments ()
{
function help_arguments {
log
log "optional arguments:"
log " -h, --help show this help message and exit"
log
}

help_actions ()
{
function help_actions {
log
log "available actions:"
log " turbo control or read the turbo boost state of your cpu"
log " min get or set the minimum cpu frequency (in %, [0;100])"
log " or get the smallest allowed value"
log " max get or set the maximum cpu frequency (in %, [0;100])"
log
}

help_turbo ()
{
function help_turbo {
log
log "turbo parameters:"
log " get get the current turbo boost state"
log " 0 turn turbo boost off"
log " 1 turn turbo boost on"
log
}

help_min ()
{
function help_min {
log
log "min parameters:"
log " get get the current set minimum frequency (in %, [0;100])"
log " check get the smallest allowed minimum frequency for your cpu model"
log " VALUE set the current minimum frequency to a value in [0;100]"
log " the values is automatically clamped to the minimum allowed cpu"
log " frequency"
log
}

help_max ()
{
function help_max {
log
log "max parameters:"
log " get get the current set maximum frequency (in %, [0;100])"
log " VALUE set the current maximum frequency to a value in [0;100]"
log
}

help_examples ()
{
function help_examples {
log
log "examples:"
log " Set the maximum frequency to 50%"
log " set the maximum frequency to 50%"
log " cpufreqctl max 50"
log
log " Get the turbo boost state"
log " get the turbo boost state"
log " cpufreqctl turbo get"
log
log " Get smallest allowed minimum cpu frequency for your model"
log " get smallest allowed minimum cpu frequency for your model"
log " cpufreqctl min check"
log
}

help_copyright ()
{
log "cpufreqctl Copyright (c) 2017 Martin Koppehel, Fin Christensen"
function help_copyright {
log
log "cpufreqctl Copyright (c) 2017-$(date '+%Y') Martin Koppehel, Fin Christensen"
}

if [ $# -lt 1 ]
then
show_usage
log "error: you must specify an action!"
exit
if [ $# -lt 1 ]; then
err "you must specify an action!"
fi

if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
exit
fi

if [ "$1" = "turbo" ]
then
if [ -z "$2" ]
then
if [[ $EUID -ne 0 ]]; then
err "This script must be run as root!"
fi

case "$1" in
"turbo")
if [ -z "$2" ]; then
log "usage: cpufreqctl turbo {get,0,1}"
log
help_turbo
help_copyright
exit
fi

if [ "$2" = "get" ]
then
value=$(<"/sys/devices/system/cpu/intel_pstate/no_turbo")
if [ "${value}" -eq 0 ]
then
echo 1
else
echo 0
fi
exit
fi
if [ "$2" -lt 0 ] || [ "$2" -gt 1 ];
then
elif [ "$2" = "get" ]; then
value=$(<"/sys/devices/system/cpu/intel_pstate/no_turbo")
[ "${value}" -eq 0 ] && echo 1 || echo 0
elif [ "$2" -lt 0 ] || [ "$2" -gt 1 ]; then
log "usage: cpufreqctl turbo {get,0,1}"
log "error: VALUE must be 0 or 1"
exit
fi

if [ "$2" -eq 1 ]
then
echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo
err "VALUE must be 0 or 1"
else
echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo
if [ "$2" -eq 1 ]; then
policy="performance"
echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo
elif [ "$2" -eq 0 ]; then
policy="powersave"
echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo
fi

for governor in /sys/devices/system/cpu/cpufreq/policy*/scaling_governor
do
echo "$policy" > "$governor"
done
fi
exit
fi

if [ "$1" = "max" ]
then
if [ -z "$2" ]
then
;;
"max")
if [ -z "$2" ]; then
log "usage: cpufreqctl max {get,VALUE}"
log
help_max
help_copyright
exit
fi

if [ "$2" = "get" ]
then
value=$(</sys/devices/system/cpu/intel_pstate/max_perf_pct)
echo "${value}"
exit
fi
if [ "$2" -lt 0 ] || [ "$2" -gt 100 ];
then
elif [ "$2" = "get" ]; then
cat /sys/devices/system/cpu/intel_pstate/max_perf_pct
elif [ "$2" -lt 0 ] || [ "$2" -gt 100 ]; then
log "usage: cpufreqctl max {get,VALUE}"
log "error: VALUE must be between 0 and 100"
exit
err "VALUE must be between 0 and 100"
else
echo "$2" > /sys/devices/system/cpu/intel_pstate/max_perf_pct
fi
echo "$2" > /sys/devices/system/cpu/intel_pstate/max_perf_pct
exit
fi

if [ "$1" = "min" ]
then
if [ -z "$2" ]
then
;;
"min")
if [ -z "$2" ]; then
log "usage: cpufreqctl mini {get,check,VALUE}"
log
help_min
help_copyright
exit
fi

if [ "$2" = "get" ]
then
value=$(</sys/devices/system/cpu/intel_pstate/min_perf_pct)
echo "${value}"
exit
fi
if [ "$2" = "check" ]
then
preValue=$(</sys/devices/system/cpu/intel_pstate/min_perf_pct)
echo 0 > /sys/devices/system/cpu/intel_pstate/min_perf_pct
postValue=$(</sys/devices/system/cpu/intel_pstate/min_perf_pct)
echo "${preValue}" > /sys/devices/system/cpu/intel_pstate/min_perf_pct
echo "${postValue}"
exit
fi
if [ "$2" -lt 0 ] || [ "$2" -gt 100 ];
then
elif [ "$2" = "get" ]; then
cat /sys/devices/system/cpu/intel_pstate/min_perf_pct
elif [ "$2" = "check" ]; then
preValue=$(</sys/devices/system/cpu/intel_pstate/min_perf_pct)
echo 0 > /sys/devices/system/cpu/intel_pstate/min_perf_pct
postValue=$(</sys/devices/system/cpu/intel_pstate/min_perf_pct)
echo "${preValue}" > /sys/devices/system/cpu/intel_pstate/min_perf_pct
echo "${postValue}"
elif [ "$2" -lt 0 ] || [ "$2" -gt 100 ]; then
log "usage: cpufreqctl min {get,check,VALUE}"
log "error: VALUE must be between 0 and 100"
exit
err "VALUE must be between 0 and 100"
else
echo "$2" > /sys/devices/system/cpu/intel_pstate/min_perf_pct
fi
echo "$2" > /sys/devices/system/cpu/intel_pstate/min_perf_pct
exit
fi
;;
*)
err "invalid option"
esac