Skip to content

Commit

Permalink
Allow custom profiles to be defined
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrd committed Aug 18, 2013
1 parent 7bba8c7 commit 6d74a61
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 94 deletions.
38 changes: 21 additions & 17 deletions README.md
Expand Up @@ -13,10 +13,10 @@ The config file is located at `/etc/conf.d/ftw`.

### universal options

**STATE_FILE**
**PROFILE_FILE**

* File to which the state (`adp` or `bat`) will be written after the
respective custom function (see below) is called.
* File to which the current profile name will be written after the profile is
called.

**MODULES**

Expand Down Expand Up @@ -68,13 +68,13 @@ The config file is located at `/etc/conf.d/ftw`.

**DIRTY_BACKGROUND_RATIO**

* Maximum percentage of memory dirty pages can occupy before pdflush begins
to write them.
* Maximum percentage of memory dirty pages can occupy before pdflush begins to
write them.

**DIRTY_EXPIRE_CENTISECS**

* How long data can be in the page cache before it expires, signifying it
must be written at the next opportunity.
* How long data can be in the page cache before it expires, signifying it must
be written at the next opportunity.

**DIRTY_WRITEBACK_CENTISECS**

Expand Down Expand Up @@ -122,36 +122,40 @@ The config file is located at `/etc/conf.d/ftw`.
* For max value (min is `0`), check
`/sys/class/backlight/acpi_video*/max_brightness`.

### custom functions
### custom profiles

The `custom-adp` or `custom-bat` function is called upon a change in state,
before the state file is updated.
Custom profiles added to the config file can be called with `ftw profile`.

### custom commands

Custom commands added to profile definitions will be executed when the profile
is called.

#### examples

Disable Wake-on-LAN on battery with
[ethtool](https://www.kernel.org/pub/software/network/ethtool/):

custom-bat() {
bat() {
ethtool -s eth0 wol d
}

custom-adp() {
adp() {
ethtool -s eth0 wol g
}

Manage backlight brightness per state using
Manage backlight brightness per profile using
[relight](http://xyne.archlinux.ca/projects/relight/):

custom-bat() {
if [[ -f $STATE_FILE && $(< $STATE_FILE) == 'adp' ]]; then
bat() {
if [[ -f $PROFILE_FILE && $(< $PROFILE_FILE) == 'adp' ]]; then
relight save adp
relight restore bat
fi
}

custom-adp() {
if [[ -f $STATE_FILE && $(< $STATE_FILE) == 'bat' ]]; then
adp() {
if [[ -f $PROFILE_FILE && $(< $PROFILE_FILE) == 'bat' ]]; then
relight save bat
relight restore adp
fi
Expand Down
55 changes: 31 additions & 24 deletions etc-conf.d/ftw
Expand Up @@ -2,9 +2,9 @@

# Comment out options to disable them.

# File to which the state (adp or bat) will be written after the
# respective custom function (see below) is called.
STATE_FILE=/tmp/ftw.state
# File to which the current profile name will be written after the profile is
# called.
PROFILE_FILE=/tmp/ftw.profile

# Array of modules to be removed when offline and inserted when online.
MODULES=(uvcvideo videodev)
Expand All @@ -16,7 +16,7 @@ PARTITIONS=/dev/sd*
# and HD_SPINDOWN_TIMEOUT.
DEVICES=/dev/sd?

config-bat() {
bat() {
# CPU power scheme governor.
CPUFREQ_GOVERNOR=ondemand

Expand All @@ -35,21 +35,20 @@ config-bat() {
# Will probably require pcie_aspm=force kernel boot parameter.
PCIE_ASPM_POLICY=powersave

# Attempt to maximize the amount of time disks spend in
# a low power state by submitting all future pending disk IO when
# performing an IO operation?
# Attempt to maximize the amount of time disks spend in a low power state
# by submitting all future pending disk IO when performing an IO operaiton?
LAPTOP_MODE=5

# Maximum percentage of memory dirty pages can occupy before
# processes are forced to write dirty buffers themselves.
# Maximum percentage of memory dirty pages can occupy before processes are
# forced to write dirty buffers themselves.
DIRTY_RATIO=90

# Maximum percentage of memory dirty pages can occupy before
# pdflush begins to write them.
# Maximum percentage of memory dirty pages can occupy before pdflush begins
# to write them.
DIRTY_BACKGROUND_RATIO=1

# How long data can be in the page cache before it expires,
# signifying it must be written at the next opportunity.
# How long data can be in the page cache before it expires, signifying it
# must be written at the next opportunity.
DIRTY_EXPIRE_CENTISECS=600

# How often pdflush wakes up to write data to disk.
Expand All @@ -58,8 +57,8 @@ config-bat() {
# Policy for SCSI host adapters.
SCSI_HOST_POLICY=min_power

# Comma-separated list (option1,option2,etc) of mount options
# to be applied when remounting devices.
# Comma-separated list (option1,option2,etc) of mount options to be applied
# when remounting devices.
REMOUNT_OPTIONS=noatime

# Block device readahead in kilobytes.
Expand All @@ -69,8 +68,8 @@ config-bat() {
# See hdparm(8) (-B flag section) for more information.
HD_POWER_MANAGEMENT=1

# How long to wait after disk activity stops before turning off
# hard drive's spindle motor.
# How long to wait after disk activity stops before turning off hard
# drive's spindle motor.
# See hdparm(8) (-S flag section) for more information.
HD_SPINDOWN_TIMEOUT=24

Expand All @@ -86,9 +85,17 @@ config-bat() {
# Dim backlight to save power? For max value (min is 0), check
# /sys/class/backlight/acpi_video*/max_brightness.
BACKLIGHT_BRIGHTNESS=10

# Add custom commands to be executed when profile is called.

if [[ $MODULES ]]; then
for i in $MODULES; do
modprobe -r $i &> /dev/null
done
fi
}

config-adp() {
adp() {
CPUFREQ_GOVERNOR=ondemand
NMI_WATCHDOG=1
BUS_CONTROL=on
Expand All @@ -108,10 +115,10 @@ config-adp() {
SND_AC97_POWER_SAVE=0
WIRELESS_POWER_SAVE=off
BACKLIGHT_BRIGHTNESS=15
}

#custom-bat() {
#}

#custom-adp() {
#}
if [[ $MODULES ]]; then
for i in $MODULES; do
modprobe $i &> /dev/null
done
fi
}
87 changes: 34 additions & 53 deletions ftw
@@ -1,9 +1,9 @@
#!/bin/bash

_defaults() {
STATE_FILE=/tmp/ftw.state
PROFILE_FILE=/tmp/ftw.profile
MODULES=(uvcvideo videodev)
config-adp() {
adp() {
CPUFREQ_GOVERNOR=ondemand
NMI_WATCHDOG=1
BUS_CONTROL=on
Expand All @@ -22,8 +22,13 @@ _defaults() {
SND_AC97_POWER_SAVE=0
WIRELESS_POWER_SAVE=off
BACKLIGHT_BRIGHTNESS=15
if [[ $MODULES ]]; then
for i in $MODULES; do
modprobe $i &> /dev/null
done
fi
}
config-bat() {
bat() {
CPUFREQ_GOVERNOR=ondemand
NMI_WATCHDOG=0
BUS_CONTROL=auto
Expand All @@ -43,26 +48,14 @@ _defaults() {
SND_AC97_POWER_SAVE=1
WIRELESS_POWER_SAVE=on
BACKLIGHT_BRIGHTNESS=10
if [[ $MODULES ]]; then
for i in $MODULES; do
modprobe -r $i &> /dev/null
done
fi
}
}

_init() {
if [[ $EUID -ne 0 ]]; then
echo 'must be run with root privileges'
exit 1
fi

local lock=/tmp/ftw.lock
[[ -f $lock ]] && exit 2
: > $lock
trap "rm -f $lock" EXIT

local rc=/etc/conf.d/ftw
[[ -f $rc ]] && source $rc || _defaults
}

_call_func() { declare -f $1 > /dev/null && $1; }

_common() {
opt() { [[ -f $1 ]] && echo $2 > $1 &> /dev/null; }

Expand Down Expand Up @@ -157,42 +150,30 @@ _common() {
fi
}

_state() { [[ $STATE_FILE ]] && echo $1 > $STATE_FILE; }

case $1 in
adp)
_init
_call_func config-adp
_common
profile=$1
if [[ $profile ]]; then
config=/etc/conf.d/ftw
[[ -f $config ]] && source $config || _defaults

if [[ $MODULES ]]; then
for i in $MODULES; do
modprobe $i &> /dev/null
done
if declare -f $profile > /dev/null; then
if [[ $EUID -ne 0 ]]; then
echo 'requires root privileges'
exit 1
fi

_call_func custom-adp
_state adp
lock=/tmp/ftw.lock
[[ -f $lock ]] && exit 2
: > $lock
trap "rm -f $lock" EXIT

exit
;;
bat)
_init
_call_func config-bat
$profile
_common

if [[ $MODULES ]]; then
for i in $MODULES; do
modprobe -r $i &> /dev/null
done
fi

_call_func custom-bat
_state bat

exit
;;
*)
echo 'usage: ftw [adp|bat]'
[[ $PROFILE_FILE ]] && echo $profile > $PROFILE_FILE
else
echo "profile '$profile' does not exist in $config"
exit 1
esac
fi
else
echo 'usage: ftw <profile>'
exit 1
fi

0 comments on commit 6d74a61

Please sign in to comment.