Fixed a few minor issues (there was a typo with kernel.nmi_watchdog), set recommended value for vm.vfs_cache_pressure to be 75, edited some comments.
maxperfwiz
#!/bin/bash
# maxperfwiz is a script that assists the user in configuring a selection of
# kernel parameters with the purpose of increasing system performance.
# by cscs and kresimir
maxwiztmpdir="/tmp/maxperfwiz"
maxwizsysctl="99-maxperfwiz.conf"
# If called with --remove flag, remove any changes and exit.
if [ "$1" = "--remove" ]; then
if [ -f /etc/sysctl.d/$maxwizsysctl ]; then
if [ "$EUID" != 0 ]; then
echo "Removing /etc/sysctl.d/$maxwizsysctl"
rm -f /etc/sysctl.d/$maxwizsysctl
exit $?
fi
fi
>&2 echo "Nothing to remove."
exit 1;
fi
# Function checks whether input is an integer within a given range, inclusive.
# returns 0 if yes, 1 if out of range, 2 if not integer.
int_within_range() {
re='^[0-9]+$';
if [[ $1 =~ $re ]]; then
if (($1 >= $2 && $1 <= $3)); then
return 0 # integer is within range
fi
return 1 # out of range
fi
return 2 # not an integer
}
# Prompts user to enter a custom value. Parameters:
# $1 - name of the parameter
# $2 - type of the parameter (unused for now, assume integer)
# $3 - lowest value
# $4 - highest value
# $5 - prompt text
maxwiz_customvalue() {
while : ; do
echo -ne "$5"
read number;
if [ "$number" = "" ]; then
maxwiz_unsetparam $1
break
else
if int_within_range "$number" "$3" "$4"; then
maxwiz_setparam "$1" "$number"
break
else
>&2 echo "Wrong value."
fi
fi
done;
}
# Prompts user to set or unset param $1 to recommended value #2
maxwiz_toggleparam() {
if grep -q "^$1" $maxwiztmpdir/$maxwizsysctl; then
echo -ne "\033[1m$1\033[0m is already set. Unset (y/N)? "
read response
if [[ $response =~ ^(yes|y|Y|Yes)$ ]]; then
maxwiz_unsetparam "$1"
fi
else
echo -ne "Set \033[1m$1\033[0m to \033[1m$2\033[0m (y/N)? "
read response
if [[ $response =~ ^(yes|y|Y|Yes)$ ]]; then
maxwiz_setparam "$1" "$2"
fi
fi
}
# comments out parameter $1 in the conf file
maxwiz_unsetparam() {
sed -i "s/^$1/#$1/" $maxwiztmpdir/$maxwizsysctl
echo -ne "\033[1m$1\033[0m unset.\n"
}
# sets parameter $1 to value $2 in the conf file
maxwiz_setparam() {
sed -i "/^#$1/d; /^$1/d" $maxwiztmpdir/$maxwizsysctl
echo "$1=$2" >> $maxwiztmpdir/$maxwizsysctl
echo -ne "\033[1m$1\033[0m set to \033[1m$2\033[0m.\n"
}
echo
fold -s <<< "maxperfwiz is a script that assists the user in configuring a selection of kernel parameters with the purpose of increasing system performance."
echo
fold -s <<< "All changes to the configurations are first written to temporary files:"
echo -ne " "
echo $maxwiztmpdir/$maxwizsysctl
echo
fold -s <<< "When customizations are complete you will be prompted to apply them. The following files will be affected:"
echo -ne " "
echo /etc/sysctl.d/$maxwizsysctl
echo
echo "To remove these files and undo all changes applied by this wizard run"
echo " $0 --remove"
echo
# SYSCTL TWEAKS
mkdir $maxwiztmpdir
touch $maxwiztmpdir/$maxwizsysctl
if [ -f /etc/sysctl.d/$maxwizsysctl ]; then
echo -ne "File $maxwizsysctl already exists. "
cp -f /etc/sysctl.d/$maxwizsysctl $maxwiztmpdir/$maxwizsysctl
fi
read -p "Modify sysctl tweaks (Y/n)? " response
if [[ $response =~ ^(yes|y|Y|Yes|)$ ]]; then
# vm.swappiness
echo
fold -s <<< "The swappiness sysctl parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100, the default value is 60. A low value causes the kernel to avoid swapping, a higher value causes the kernel to try to use swap space. Using a low value on sufficient memory is known to improve responsiveness on many systems."
echo
echo -ne "The current value of vm.swappiness is: "
cat /proc/sys/vm/swappiness
echo
if [ "$1" = "--custom-values" ]; then
maxwiz_customvalue "vm.swappiness" "integer" 0 100 \
"Enter a value for \033[1mvm.swappiness\033[0m or leave empty to unset\n[integer between 0 and 100, default: 60; recommended: 10]: "
else
maxwiz_toggleparam "vm.swappiness" "10"
fi
# vm.vfs_cache_pressure
echo
fold -s <<< "VFS cache value controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. Lowering it from the default value of 100 makes the kernel less inclined to reclaim VFS cache. Warning: do not set it to 0, this may produce out-of-memory conditions."
echo
echo -ne "The current value of \033[1mvm.vfs_cache_pressure\033[0m is: "
cat /proc/sys/vm/vfs_cache_pressure
echo
if [ "$1" = "--custom-values" ]; then
maxwiz_customvalue "vm.vfs_cache_pressure" "integer" 1 1000 \
"Enter a value for \033[1mvm.vfs_cache_pressure\033[0m or leave empty to unset\n[integer between 1 and 1000, default: 100, recommended: 75]: "
else
maxwiz_toggleparam "vm.vfs_cache_pressure" "75"
fi
# kernel.nmi_watchdog
echo
fold -s <<< "Disabling watchdog will speed up your boot and shutdown, because one less module is loaded. Additionally disabling watchdog timers increases performance and lowers power consumption."
echo
echo -ne "The current value of \033[1mkernel.nmi_watchdog\033[0m is: "
cat /proc/sys/kernel/watchdog
echo
if [ "$1" = "--custom-values" ]; then
maxwiz_customvalue "kernel.nmi_watchdog" "integer" 0 2 \
"Enter a value for \033[1mkernel.nmi_watchdog\033[0m or leave empty to unset\n[integer between 0 and 2, default: 1, recommended: 0]: "
else
maxwiz_toggleparam "kernel.nmi_watchdog" "0"
fi
# vm.dirty_ratio
echo
fold -s <<< "Contains, as a percentage of total available memory that contains free pages and reclaimable pages, the number of pages at which a process which is generating disk writes will itself start writing out dirty data."
echo
echo -ne "The current value of \033[1mvm.dirty_ratio\033[0m is: "
cat /proc/sys/vm/dirty_ratio
echo
if [ "$1" = "--custom-values" ]; then
maxwiz_customvalue "vm.dirty_ratio" "integer" 1 100 \
"Enter a value for \033[1mvm.dirty_ratio\033[0m or leave empty to unset\n[integer between 1 and 100, default: 20, recommended: 10]: "
else
maxwiz_toggleparam "vm.dirty_ratio" "10"
fi
# vm.dirty_background_ratio
echo
fold -s <<< "Contains, as a percentage of total available memory that contains free pages and reclaimable pages, the number of pages at which the background kernel flusher threads will start writing out dirty data."
echo
echo -ne "The current value of \033[1mvm.dirty_background_ratio\033[0m is: "
cat /proc/sys/vm/dirty_background_ratio
echo
if [ "$1" = "--custom-values" ]; then
maxwiz_customvalue "vm.dirty_background_ratio" "integer" 1 100 \
"Enter a value for \033[1mvm.dirty_background_ratio\033[0m or leave empty to unset\n[integer between 1 and 100, default: 10, recommended: 5]: "
else
maxwiz_toggleparam "vm.dirty_background_ratio" "5"
fi
# vm.dirty_expire_centisecs ## probably overridden by TLP lines MAX_LOST_WORK_SECS*
echo
fold -s <<< "Dirty expire centisecs tunable is used to define when dirty data is old enough to be eligible for writeout by the kernel flusher threads, expressed in 100'ths of a second. Data which has been dirty in-memory for longer than this interval will be written out next time a flusher thread wakes up."
echo
echo -ne "The current value of \033[1mvm.dirty_expire_centisecs\033[0m is: "
cat /proc/sys/vm/dirty_expire_centisecs
echo
if [ "$1" = "--custom-values" ]; then
maxwiz_customvalue "vm.dirty_expire_centisecs" "integer" 1 9999 \
"Enter a value for \033[1mvm.dirty_expire_centisecs\033[0m or leave empty to unset\n[integer between 1 and 9999, default: 6000, recommended: 3000]: "
else
maxwiz_toggleparam "vm.dirty_expire_centisecs" "3000"
fi
# vm.dirty_writeback_centisecs ## probably overridden by TLP lines MAX_LOST_WORK_SECS*
echo
fold -s <<< "The kernel flusher threads will periodically wake up and write 'old' data out to disk. This tunable expresses the interval between those wakeups, in 100'ths of a second."
echo
echo -ne "The current value of \033[1mvm.dirty_writeback_centisecs\033[0m is: "
cat /proc/sys/vm/dirty_writeback_centisecs
echo
if [ "$1" = "--custom-values" ]; then
maxwiz_customvalue "vm.dirty_writeback_centisecs" "integer" 1 9999 \
"Enter a value for \033[1mvm.dirty_writeback_centisecs\033[0m or leave empty to unset\n[integer between 1 and 9999, default: 6000, recommended: 1500]: "
else
maxwiz_toggleparam "vm.dirty_writeback_centisecs" "1500"
fi
fi
if [ -f $maxwiztmpdir/$maxwizsysctl ]; then
echo
echo "Contents of temporary file $maxwiztmpdir/$maxwizsysctl:"
echo
cat $maxwiztmpdir/$maxwizsysctl
echo
read -p "Apply these tweaks (y/N)? " response
if [[ $response =~ ^(yes|y|Y|Yes)$ ]]; then
sudo cp -f $maxwiztmpdir/$maxwizsysctl /etc/sysctl.d/$maxwizsysctl && echo "Changes successfully applied to /etc/sysctl.d/$maxwizsysctl"
exit $?
fi
fi
This is still work in progress.