Rev 34 | Blame | Compare with Previous | Last modification | View Log | RSS feed
# vim: set syn=sh :## This script contains functions which can be useful in ktune profile scripts.### DISK tuning#DISKS_DEV="$(command ls -d1 /dev/[shv]d*[a-z] 2>/dev/null)"DISKS_SYS="$(command ls -d1 /sys/block/{sd,cciss,dm-,vd,dasd,xvd}* 2>/dev/null)"# SATA Aggressive Link Power Management# usage: set_disk_alpm policyset_disk_alpm() {policy=$1for host in /sys/class/scsi_host/*; doif [ -f $host/ahci_port_cmd ]; thenport_cmd=`cat $host/ahci_port_cmd`;if [ $((0x$port_cmd & 0x240000)) = 0 -a -f $host/link_power_management_policy ]; thenecho $policy >$host/link_power_management_policy;elseecho "max_performance" >$host/link_power_management_policy;fifidone}# usage: set_disk_apm levelset_disk_apm() {level=$1for disk in $DISKS_DEV; dohdparm -B $level $disk &>/dev/nulldone}# usage: set_disk_spindown levelset_disk_spindown() {level=$1for disk in $DISKS_DEV; dohdparm -S $level $disk &>/dev/nulldone}DISK_READAHEAD_SAVE="/var/run/tuned/disk_readahead.save"# usage: multiply_disk_readahead bymultiply_disk_readahead() {by=$1rm -f "$DISK_READAHEAD_SAVE"# float multiplication not supported in bash# bc might not be installed, python is available for surefor disk in $DISKS_SYS; docontrol="${disk}/queue/read_ahead_kb"old=$(cat $control)new=$(echo "print int($old*$by)" | python)echo "echo $old > $control" >> "$DISK_READAHEAD_SAVE" 2>/dev/null(echo $new > $control) &>/dev/nulldone}restore_disk_readahead() {if [ -r "$DISK_READAHEAD_SAVE" ]; then/bin/sh "$DISK_READAHEAD_SAVE" &>/dev/nullrm -f "$DISK_READAHEAD_SAVE"fi}# usage: remount_disk options partition1 partition2 ...remount_partitions() {options=$1shiftfor partition in $@; domount -o remount,$options $partitiondone}_devices_no_write_back_cache() {for device in $@; dogrep -q "write back" /sys/block/"$device"/device/scsi_disk/*/cache_type 2>/dev/null && return 1donereturn 0}_disk_barriers_remount() {mount_options="$1"lsblk -lno TYPE,KNAME,MOUNTPOINT | \awk '{ type=$1; name=$2; mountpoint=$3; }(type == "disk") {device=namenext}(mountpoint ~ /^\// && mountpoint != "/" && mountpoint != "/boot") {mountpoints[mountpoint] = mountpoints[mountpoint] " " device}END {for (mountpoint in mountpoints) {print mountpoint, mountpoints[mountpoint]}}' | \while read mountpoint devices; doif _devices_no_write_back_cache $devices; thenmount -o remount,"$mount_options" "$mountpoint" >/dev/null 2>&1fidone}# remounts all non-root and non-boot partitions with nobarrier option# SCSI drives with write back cache are skippeddisable_disk_barriers() {_disk_barriers_remount nobarrier}# see: disable_disk_barriersenable_disk_barriers() {_disk_barriers_remount barrier}## CPU tuning#CPUSPEED_SAVE_FILE="/var/run/tuned/ktune-cpuspeed.save"CPUSPEED_ORIG_GOV="/var/run/tuned/ktune-cpuspeed-governor-%s.save"CPUSPEED_STARTED="/var/run/tuned/ktune-cpuspeed-started"CPUSPEED_CFG="/etc/sysconfig/cpuspeed"CPUSPEED_INIT="/etc/init.d/cpuspeed"# do not use cpuspeedCPUSPEED_USE="0"CPUS="$(ls -d1 /sys/devices/system/cpu/cpu* | sed 's;^.*/;;' | grep "cpu[0-9]\+")"# set CPU governor setting and store the old settings# usage: set_cpu_governor governorset_cpu_governor() {governor=$1# always patch cpuspeed configuration if exists, if it doesn't exist and is enabled,# explictly disable it with hintif [ -e $CPUSPEED_INIT ]; thenif [ ! -e $CPUSPEED_SAVE_FILE -a -e $CPUSPEED_CFG ]; thencp -p $CPUSPEED_CFG $CPUSPEED_SAVE_FILEsed -e 's/^GOVERNOR=.*/GOVERNOR='$governor'/g' $CPUSPEED_SAVE_FILE > $CPUSPEED_CFGfielseif [ "$CPUSPEED_USE" = "1" ]; thenecho >&2echo "Suggestion: install 'cpuspeed' package to get best tuning results." >&2echo "Falling back to sysfs control." >&2echo >&2fiCPUSPEED_USE="0"fiif [ "$CPUSPEED_USE" = "1" ]; thenservice cpuspeed status &> /dev/null[ $? -eq 3 ] && touch $CPUSPEED_STARTED || rm -f $CPUSPEED_STARTEDservice cpuspeed restart &> /dev/null# direct change using sysfselif [ -e /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; thenfor cpu in $CPUS; dogov_file=/sys/devices/system/cpu/$cpu/cpufreq/scaling_governorsave_file=$(printf $CPUSPEED_ORIG_GOV $cpu)rm -f $save_fileif [ -e $gov_file ]; thencat $gov_file > $save_fileecho $governor > $gov_filefidonefi}# re-enable previous CPU governor settings# usage: restore_cpu_governorrestore_cpu_governor() {if [ -e $CPUSPEED_INIT ]; thenif [ -e $CPUSPEED_SAVE_FILE ]; thencp -fp $CPUSPEED_SAVE_FILE $CPUSPEED_CFGrm -f $CPUSPEED_SAVE_FILEfiif [ "$CPUSPEED_USE" = "1" ]; thenif [ -e $CPUSPEED_STARTED ]; thenservice cpuspeed stop &> /dev/nullelseservice cpuspeed restart &> /dev/nullfifiif [ -e $CPUSPEED_STARTED ]; thenrm -f $CPUSPEED_STARTEDfielseCPUSPEED_USE="0"fiif [ "$CPUSPEED_USE" != "1" -a -e /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; thenfor cpu in $CPUS; docpufreq_dir=/sys/devices/system/cpu/$cpu/cpufreqsave_file=$(printf $CPUSPEED_ORIG_GOV $cpu)if [ -e $cpufreq_dir/scaling_governor ]; thenif [ -e $save_file ]; thencat $save_file > $cpufreq_dir/scaling_governorrm -f $save_fileelseecho userspace > $cpufreq_dir/scaling_governorcat $cpufreq_dir/cpuinfo_max_freq > $cpufreq_dir/scaling_setspeedfifidonefi}_cpu_multicore_powersave() {value=$1[ -e /sys/devices/system/cpu/sched_mc_power_savings ] && echo $value > /sys/devices/system/cpu/sched_mc_power_savings}# enable multi core power savings for low wakeup systemsenable_cpu_multicore_powersave() {_cpu_multicore_powersave 1}disable_cpu_multicore_powersave() {_cpu_multicore_powersave 0}## MEMORY tuning#THP_ENABLE="/sys/kernel/mm/redhat_transparent_hugepage/enabled"THP_SAVE="/var/run/tuned/ktune-thp.save"[ -e "$THP_ENABLE" ] || THP_ENABLE="/sys/kernel/mm/transparent_hugepage/enabled"set_transparent_hugepages() {if [ -e $THP_ENABLE ]; thencut -f2 -d'[' $THP_ENABLE | cut -f1 -d']' > $THP_SAVE(echo "$1" > $THP_ENABLE) &> /dev/nullfi}restore_transparent_hugepages() {if [ -e $THP_SAVE ]; then(echo $(cat $THP_SAVE) > $THP_ENABLE) &> /dev/nullrm -f $THP_SAVEfi}## WIFI tuning## usage: _wifi_set_power_level level_wifi_set_power_level() {# 0 auto, PM enabled# 1-5 least savings and lowest latency - most savings and highest latency# 6 disable power savingslevel=$1# apply the settings using iwprivifaces=$(cat /proc/net/wireless | grep -v '|' | sed 's@^ *\([^:]*\):.*@\1@')for iface in $ifaces; doiwpriv $iface set_power $leveldone# some adapters may relay on sysfsfor i in /sys/bus/pci/devices/*/power_level; do(echo $level > $i) &> /dev/nulldone}enable_wifi_powersave() {_wifi_set_power_level 5}disable_wifi_powersave() {_wifi_set_power_level 0}## BLUETOOTH tuning#disable_bluetooth() {hciconfig hci0 down >/dev/null 2>&1lsmod | grep -q btusb && rmmod btusb}enable_bluetooth() {modprobe btusbhciconfig hci0 up >/dev/null 2>&1}## USB tuning#_usb_autosuspend() {value=$1for i in /sys/bus/usb/devices/*/power/autosuspend; do echo $value > $i; done &> /dev/null}enable_usb_autosuspend() {_usb_autosuspend 1}disable_usb_autosuspend() {_usb_autosuspend 0}## SOUND CARDS tuning#_snd_ac97_powersave() {value=$1[ -e /sys/module/snd_ac97_codec/parameters/power_save ] && echo $value > /sys/module/snd_ac97_codec/parameters/power_save}enable_snd_ac97_powersave() {_snd_ac97_powersave Y}disable_snd_ac97_powersave() {_snd_ac97_powersave N}## CD DRIVES tuning#_cd_polling() {[ "$1" == "1" ] && opts=--enable-polling || opts=cddrives=$(command ls -1 /dev/scd* 2>/dev/null)for i in $cddrives; do hal-disable-polling $opts --device $(readlink -f $i); done &>/dev/null}enable_cd_polling() {_cd_polling 1}disable_cd_polling() {_cd_polling 0}## SOFTWARE tuning#RSYSLOG_CFG="/etc/rsyslog.conf"RSYSLOG_SAVE="/var/run/tuned/ktune-cpuspeed.save"disable_logs_syncing() {cp -p $RSYSLOG_CFG $RSYSLOG_SAVEsed -i 's/ \/var\/log/-\/var\/log/' $RSYSLOG_CFG}restore_logs_syncing() {mv $RSYSLOG_SAVE $RSYSLOG_CFG}## HARDWARE SPECIFIC tuning## Asus EEE with Intel Atom_eee_fsb_control() {value=$1if [ -e /sys/devices/platform/eeepc/she ]; thenecho $value > /sys/devices/platform/eeepc/sheelif [ -e /sys/devices/platform/eeepc/cpufv ]; thenecho $value > /sys/devices/platform/eeepc/cpufvfi}eee_set_reduced_fsb() {_eee_fsb_control 2}eee_set_normal_fsb() {_eee_fsb_control 1}## KTUNE ACTION PROCESSING#error_not_implemented() {echo "tuned: ktune script function '$1' is not implemented." >&2}# implicit actions, will be used if not provided by profile script:## * start must be implemented# * stop must be implemented# * reload runs start# * restart runs stop + start# * status returns 0start() {error_not_implemented startreturn 16}stop() {error_not_implemented stopreturn 16}reload() {startreturn $?}restart() {stop && startreturn $?}status() {return 0}# main processingprocess() {VAR_SUBSYS_KTUNE="/var/lock/subsys/ktune"ARG="$1"shiftcase "$ARG" instart)[ -f "$VAR_SUBSYS_KTUNE" ] && exit 0startRETVAL=$?;;stop)[ -f "$VAR_SUBSYS_KTUNE" ] || exit 0stop "$@"RETVAL=$?;;reload)[ -f "$VAR_SUBSYS_KTUNE" ] && reloadRETVAL=$?;;restart|force-reload)[ -f "$VAR_SUBSYS_KTUNE" ] && restartRETVAL=$?;;condrestart|try-restart)[ -f "$VAR_SUBSYS_KTUNE" ] || exit 0restartRETVAL=$?;;status)statusRETVAL=$?;;*)echo $"Usage: $0 {start|stop|restart|condrestart|status}"RETVAL=2;;esacexit $RETVAL}