#!/bin/dash
# set cpufreq governor for all CPUs
#

# config and vars
DEF_GOVERNOR=ondemand
GOVERNOR=${1:-$DEF_GOVERNOR}

read cpus < /sys/devices/system/cpu/present
startcpu=${cpus%-*}
endcpu=${cpus#*-}
cpus=$(seq $startcpu $endcpu)

set_governor() {
	for p in $cpus; do
		#echo cpufreq-set -c $p -g $GOVERNOR
		cpufreq-set -c $p -g $GOVERNOR
	done
}

show_governor() {
	for p in $cpus; do
		echo "cpu $p: $(cat /sys/devices/system/cpu/cpu$p/cpufreq/scaling_governor) $(cat /sys/devices/system/cpu/cpu$p/cpufreq/scaling_cur_freq)"
	done
}

# param
case "$1" in
	-h|--help)
		echo "${0##/} [-l|--list|-d|--display] [governor]"
		echo "If not specified, defaults to $DEF_GOVERNOR."
		echo "-l|--list    list available governors. May need to modprobe for more."
		echo "-d|--display display current governors"
		exit ;;
	-l|--list)
		echo "Available governors: " $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)
		exit ;;
	-d|--display)
		show_governor
		exit ;;
	*)  set_governor
esac


