Browse Source

Scripts: Add scaling governor to cpuctl

master
Riyyi 3 years ago
parent
commit
d4ecfcdfcb
  1. 98
      .local/bin/cpuctl

98
.local/bin/cpuctl

@ -14,19 +14,26 @@ ${b}NAME${n}
cpuctl - control the processor characteristics cpuctl - control the processor characteristics
${b}SYNOPSIS${n} ${b}SYNOPSIS${n}
${b}cpuctl${n} ${u}OPTION${n} [${u}ARG${n}] ${b}cpuctl${n} ${u}OPERATION${n} [${u}OPTION${n}] [${u}ARG${n}]
${b}OPERATIONS${n}
${b}-E${n}, ${b}--energy${n}
Perform ${u}OPTION${n} on energy performance mode.
${b}-G${n}, ${b}--governor${n}
Perform ${u}OPTION${n} on scaling governor.
${b}OPTIONS${n}
${b}-h${n}, ${b}--help${n} Display usage message and exit. ${b}-h${n}, ${b}--help${n} Display usage message and exit.
${b}OPTIONS (APPLY TO -E or -G)${n}
${b}-l${n}, ${b}--list${n} ${b}-l${n}, ${b}--list${n}
List the available energy performance modes. List the available items.
${b}-g${n}, ${b}--get${n} ${b}-g${n}, ${b}--get${n}
Get the energy performance mode. Get the current item.
${b}-s${n}[${u}MODE${n}], ${b}--set [${n}${u}MODE${n}] ${b}-s${n}[${u}ITEM${n}], ${b}--set [${n}${u}ITEM${n}]
Set the energy performance to ${U}MODE${N}, asks for the mode if left empty. Set the ${u}ITEM${n}, prompt for ${u}ITEM${n} if left empty.
EOF EOF
} }
@ -36,36 +43,54 @@ EOF
# Main program # Main program
# -------------------------------------- # --------------------------------------
listModes() listItems()
{ {
if [ "$mode" = "energy" ]; then
modes="/sys/devices/system/cpu/cpufreq/policy0/energy_performance_available_preferences" modes="/sys/devices/system/cpu/cpufreq/policy0/energy_performance_available_preferences"
elif [ "$mode" = "governor" ]; then
modes="/sys/devices/system/cpu/cpufreq/policy0/scaling_available_governors"
fi
tr ' ' '\n' < "$modes" | sed '/^$/d' \ tr ' ' '\n' < "$modes" | sed '/^$/d' \
| awk -v line="$1" '{ if (line == "") print NR") "$0; else if (NR == line) print $0; }' | awk -v line="$1" '{ if (line == "") print NR") "$0; else if (NR == line) print $0; }'
} }
getMode() getItem()
{ {
if [ "$mode" = "energy" ]; then
mode="/sys/devices/system/cpu/cpufreq/policy0/energy_performance_preference" mode="/sys/devices/system/cpu/cpufreq/policy0/energy_performance_preference"
elif [ "$mode" = "governor" ]; then
mode="/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"
fi
cat "$mode" cat "$mode"
} }
setMode() setItem()
{ {
option="$1" select="$1"
if [ -z "$option" ]; then if [ -z "$select" ]; then
listModes listItems
printf "Enter the number to set: " printf "Enter the number to set: "
read -r option read -r select
fi
item="$(listItems "$select")"
if [ "$mode" = "energy" ]; then
message="energy performance mode"
type="/sys/devices/system/cpu/cpufreq/policy*/energy_performance_preference"
elif [ "$mode" = "governor" ]; then
message="scaling governor"
type="/sys/devices/system/cpu/cpufreq/policy*/scaling_governor"
fi fi
mode="$(listModes "$option")"
if ! sudo -v; then if ! sudo -v; then
echo "${b}${red}Error:${n} setting energy performance mode requires root privileges." >&2 echo "${b}${red}Error:${n} setting $message requires root privileges." >&2
exit 1 exit 1
fi fi
for i in /sys/devices/system/cpu/cpufreq/policy*/energy_performance_preference; do for i in $type; do
echo "$mode" | sudo tee "$i" > /dev/null echo "$item" | sudo tee "$i" > /dev/null
done done
} }
@ -73,7 +98,9 @@ setMode()
# -------------------------------------- # --------------------------------------
script="$(basename "$0")" script="$(basename "$0")"
parsed="$(getopt --options "hlgs::" --longoptions "help,list,get,set::" -n "$script" -- "$@" 2>&1)" parsed="$(getopt --options "hEGlgs::" \
--longoptions "help,energy,governor,list,get,set::" \
-n "$script" -- "$@" 2>&1)"
result="$?" result="$?"
# Exit if invalid option is provided # Exit if invalid option is provided
@ -91,6 +118,22 @@ while true; do
help help
exit exit
;; ;;
-E | --energy)
if [ -n "$mode" ]; then
echo "${b}${red}Error:${n} only one operation may be used at a time." >&2
exit 1
fi
mode="energy"
shift
;;
-G | --governor)
if [ -n "$mode" ]; then
echo "${b}${red}Error:${n} only one operation may be used at a time." >&2
exit 1
fi
mode="governor"
shift
;;
-l | --list) -l | --list)
option="list" option="list"
shift shift
@ -116,14 +159,21 @@ done
# Execute # Execute
# -------------------------------------- # --------------------------------------
case "$option" in if [ -z "$mode" ]; then
list) echo "${b}${red}Error:${n} no operation specified (use -h for help)" >&2
listModes exit 1
fi
if [ "$mode" = "energy" ] || [ "$mode" = "governor" ]; then
case "$option" in
list | "")
listItems
;; ;;
get) get)
getMode getItem
;; ;;
set) set)
setMode "$1" setItem "$1"
;; ;;
esac esac
fi

Loading…
Cancel
Save