#!/bin/bash
#===============================================================================
#
#          FILE: maximize_cpu_frequency
#
#         USAGE: maximize_cpu_frequency
#
#   DESCRIPTION: Maximize CPU frequency
#
#  REQUIREMENTS: Run by root
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Tue Nov 19 09:19:52 ICT 2013
#      REVISION: 1.0
#
#          TODO: 
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control

# Must run by root.
[ "$UID" == "0" ] || exec echo "Error: You can only change CPU frequency with root."

# Change CPU frequency
(
echo "Current:"
grep MHz /proc/cpuinfo | nl -v 0
echo
echo "Change CPU frequency:"
for cpu in $(ls /sys/devices/system/cpu/ | grep "^cpu[0-9]" | sort -t u -k 2g);do
    SYSFS_PATH="/sys/devices/system/cpu/$cpu"
    echo -n "CPU: $cpu, "
    cat $SYSFS_PATH/cpufreq/scaling_max_freq > $SYSFS_PATH/cpufreq/scaling_min_freq
    echo -n "Scaling to max($(cat $SYSFS_PATH/cpufreq/scaling_min_freq)), "
    echo performance > $SYSFS_PATH/cpufreq/scaling_governor
    echo "Governor to $(cat $SYSFS_PATH/cpufreq/scaling_governor)"
done
echo
echo "Result: "
grep MHz /proc/cpuinfo | nl -v 0
) 2>&1 | tee /tmp/maximize_cpu_frequency.log
