Created
March 24, 2011 05:23
-
-
Save mrdaemon/884618 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
# Toggle CPU performance in a binary fashion. | |
# Alexandre Gauthier 2011-2012 | |
# | |
# Done out of disgust at how the example shell script in one of these | |
# "language-du-jour is better than a shell script" talks. | |
# | |
# Requirements --> | |
# Toggle the sysctl value of hw.setperf between '0' and '100'. | |
# Talk implied little error checking was done in shell scripts, which | |
# I feel is a matter of personal style over language suitability. | |
# | |
# NOTE: Since the 'off' value is '0' which is unixese for 'true' based on | |
# return codes, I could abuse that, making it clever but horrible. So I don't. | |
# It /would/ probably result in less code, but who gives a shit, I hate | |
# using side effects, it's a terrible idea. | |
# | |
# Path to sysctl binary | |
SYSCTL=/bin/sysctl | |
# On/Off values | |
# Should these ever change. | |
ONVAL=100 | |
OFFVAL=0 | |
# Simple sanity check. | |
[[ ! -x $SYSCTL ]] && { echo "Unable to exec /bin/sysctl" 1>2 ; exit 1 ;} | |
# Current performance value | |
PERF=$(${SYSCTL} hw.setperf | cut -d '=' -f 2) | |
# setperf helper function | |
# sets hw.setperf to $0 | |
setperf() { | |
echo "hw.setperf: $PERF -> $0" | |
sudo $SYSCTL -w hw.setperf=$0 || { echo "Failed to apply value." 1>2 ;} | |
} | |
# Validate that value is not suddendly unexpected | |
# TODO: Could very well be moved up here in the setperf() function. | |
if [[ $PERF -ne $ONVAL || $PERF -ne $OFFVAL ]] ; then | |
echo "Got invalid value from sysctl ($PERF)." 1>2 | |
echo "Will only toggle between ${ONVAL} and ${OFFVAL}." 1>2 | |
exit 1 | |
fi | |
# Toggle sysctl value. | |
[[ $PERF -eq $ONVAL ]] && setperf $OFFVAL || setperf $ONVAL | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment