diff --git a/tune-profiles/laptop-battery-powersave/ktune.sh b/tune-profiles/laptop-battery-powersave/ktune.sh index 006e744..fd227d1 100755 --- a/tune-profiles/laptop-battery-powersave/ktune.sh +++ b/tune-profiles/laptop-battery-powersave/ktune.sh @@ -3,11 +3,11 @@ VAR_SUBSYS_KTUNE="/var/lock/subsys/ktune" CPUSPEED_SAVE_FILE="/var/run/tuned/ktune-cpuspeed.save" -CPUSPEED_ORIG_GOV="/var/run/tuned/ktune-cpuspeed-governor.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" -CPUS="/sys/devices/system/cpu*/cpufreq" +CPUS="$(ls -d1 /sys/devices/system/cpu/cpu* | sed 's;^.*/;;' | grep "cpu[0-9]\+")" ALPM="min_power" @@ -35,7 +35,6 @@ start() { [ -e /sys/devices/system/cpu/sched_mc_power_savings ] && echo 1 > /sys/devices/system/cpu/sched_mc_power_savings # Enable ondemand CPU governor (prefer cpuspeed) - cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > $CPUSPEED_ORIG_GOV if [ -e $CPUSPEED_INIT ]; then if [ ! -e $CPUSPEED_SAVE_FILE -a -e $CPUSPEED_CFG ]; then cp -p $CPUSPEED_CFG $CPUSPEED_SAVE_FILE @@ -53,7 +52,13 @@ start() { echo >/dev/stderr for cpu in $CPUS; do - echo ondemand > $cpu/scaling_governor + gov_file=/sys/devices/system/cpu/$cpu/cpufreq/scaling_governor + save_file=$(printf $CPUSPEED_ORIG_GOV $cpu) + rm -f $save_file + if [ -e $gov_file ]; then + cat $gov_file > $save_file + echo ondemand > $gov_file + fi done fi @@ -93,18 +98,20 @@ stop() { service cpuspeed restart >/dev/null 2>&1 fi else - if [ -e $CPUSPEED_ORIG_GOV ]; then - OLD_GOVERNOR=$(cat $CPUSPEED_ORIG_GOV) - rm -f $CPUSPEED_ORIG_GOV - for cpu in $CPUS; do - echo $OLD_GOVERNOR > $cpu/scaling_governor - done - else - for cpu in $CPUS; do - echo userspace > $cpu/scaling_governor - cat $cpu/cpuinfo_max_freq > $cpu/scaling_setspeed - done - fi + for cpu in $CPUS; do + cpufreq_dir=/sys/devices/system/cpu/$cpu/cpufreq + save_file=$(printf $CPUSPEED_ORIG_GOV $cpu) + + if [ -e $cpufreq_dir/scaling_governor ]; then + if [ -e $save_file ]; then + cat $save_file > $cpufreq_dir/scaling_governor + rm -f $save_file + else + echo userspace > $cpufreq_dir/scaling_governor + cat $cpufreq_dir/cpuinfo_max_freq > $cpufreq_dir/scaling_setspeed + fi + fi + done fi # Enable AC97 audio power saving diff --git a/tune-profiles/latency-performance/ktune.sh b/tune-profiles/latency-performance/ktune.sh index ee72d11..372801e 100755 --- a/tune-profiles/latency-performance/ktune.sh +++ b/tune-profiles/latency-performance/ktune.sh @@ -3,25 +3,38 @@ VAR_SUBSYS_KTUNE="/var/lock/subsys/ktune" CPUSPEED_SAVE_FILE="/var/run/tuned/ktune-cpuspeed.save" -CPUSPEED_ORIG_GOV="/var/run/tuned/ktune-cpuspeed-governor.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" -CPUS="/sys/devices/system/cpu*/cpufreq" +CPUS="$(ls -d1 /sys/devices/system/cpu/cpu* | sed 's;^.*/;;' | grep "cpu[0-9]\+")" start() { - # Save currently enabled governor and/or cpuspeed config - cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > $CPUSPEED_ORIG_GOV - if [ ! -e $CPUSPEED_SAVE_FILE -a -e $CPUSPEED_CFG ]; then - cp -p $CPUSPEED_CFG $CPUSPEED_SAVE_FILE - sed -e 's/^GOVERNOR=.*/GOVERNOR=performance/g' $CPUSPEED_SAVE_FILE > $CPUSPEED_CFG - fi - - # Enable performance governor (best for maximum i/o throughput in most cases) + # Enable performance CPU governor (prefer cpuspeed) if [ -e $CPUSPEED_INIT ]; then - /sbin/service cpuspeed restart > /dev/null 2>&1 + if [ ! -e $CPUSPEED_SAVE_FILE -a -e $CPUSPEED_CFG ]; then + cp -p $CPUSPEED_CFG $CPUSPEED_SAVE_FILE + sed -e 's/^GOVERNOR=.*/GOVERNOR=performance/g' $CPUSPEED_SAVE_FILE > $CPUSPEED_CFG + fi + + service cpuspeed status >/dev/null 2>&1 + [ $? -eq 3 ] && touch $CPUSPEED_STARTED || rm -f $CPUSPEED_STARTED + + service cpuspeed restart >/dev/null 2>&1 else + echo >/dev/stderr + echo "Suggestion: install 'cpuspeed' package to get best performance and latency." >/dev/stderr + echo "Falling back to 'performance' scaling governor for all CPUs." >/dev/stderr + echo >/dev/stderr + for cpu in $CPUS; do - echo performance > $cpu/scaling_governor + gov_file=/sys/devices/system/cpu/$cpu/cpufreq/scaling_governor + save_file=$(printf $CPUSPEED_ORIG_GOV $cpu) + rm -f $save_file + if [ -e $gov_file ]; then + cat $gov_file > $save_file + echo performance > $gov_file + fi done fi @@ -29,28 +42,35 @@ start() { } stop() { - # Restore previous cpuspeed config - if [ -e $CPUSPEED_SAVE_FILE ]; then - cp -fp $CPUSPEED_SAVE_FILE $CPUSPEED_CFG - rm -f $CPUSPEED_SAVE_FILE - fi - - # Re-enable previous governor + # Re-enable previous CPU governor if [ -e $CPUSPEED_INIT ]; then - /sbin/service cpuspeed restart > /dev/null 2>&1 - elif [ -e $CPUSPEED_ORIG_GOV ]; then - for cpu in $CPUS; do - echo $(cat $CPUSPEED_ORIG_GOV) > $cpu/scaling_governor - done + if [ -e $CPUSPEED_SAVE_FILE ]; then + cp -fp $CPUSPEED_SAVE_FILE $CPUSPEED_CFG + rm -f $CPUSPEED_SAVE_FILE + fi + + if [ -e $CPUSPEED_STARTED ]; then + rm -f $CPUSPEED_STARTED + service cpuspeed stop >/dev/null 2>&1 + else + service cpuspeed restart >/dev/null 2>&1 + fi else for cpu in $CPUS; do - echo userspace > $cpu/scaling_governor - cat $cpu/cpuinfo_max_freq > $cpu/scaling_setspeed + cpufreq_dir=/sys/devices/system/cpu/$cpu/cpufreq + save_file=$(printf $CPUSPEED_ORIG_GOV $cpu) + + if [ -e $cpufreq_dir/scaling_governor ]; then + if [ -e $save_file ]; then + cat $save_file > $cpufreq_dir/scaling_governor + rm -f $save_file + else + echo userspace > $cpufreq_dir/scaling_governor + cat $cpufreq_dir/cpuinfo_max_freq > $cpufreq_dir/scaling_setspeed + fi + fi done fi - if [ -e $CPUSPEED_ORIG_GOV ]; then - rm -f $CPUSPEED_ORIG_GOV - fi return 0 } diff --git a/tune-profiles/throughput-performance/ktune.sh b/tune-profiles/throughput-performance/ktune.sh index 7834dd8..79ef831 100755 --- a/tune-profiles/throughput-performance/ktune.sh +++ b/tune-profiles/throughput-performance/ktune.sh @@ -3,28 +3,41 @@ VAR_SUBSYS_KTUNE="/var/lock/subsys/ktune" CPUSPEED_SAVE_FILE="/var/run/tuned/ktune-cpuspeed.save" -CPUSPEED_ORIG_GOV="/var/run/tuned/ktune-cpuspeed-governor.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" -CPUS="/sys/devices/system/cpu*/cpufreq" +CPUS="$(ls -d1 /sys/devices/system/cpu/cpu* | sed 's;^.*/;;' | grep "cpu[0-9]\+")" THP_ENABLE="/sys/kernel/mm/redhat_transparent_hugepage/enabled" THP_SAVE="/var/run/tuned/ktune-thp.save" start() { - # Save currently enabled governor and/or cpuspeed config - cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > $CPUSPEED_ORIG_GOV - if [ ! -e $CPUSPEED_SAVE_FILE -a -e $CPUSPEED_CFG ]; then - cp -p $CPUSPEED_CFG $CPUSPEED_SAVE_FILE - sed -e 's/^GOVERNOR=.*/GOVERNOR=performance/g' $CPUSPEED_SAVE_FILE > $CPUSPEED_CFG - fi - - # Enable performance governor (best for maximum i/o throughput in most cases) + # Enable performance CPU governor (prefer cpuspeed) if [ -e $CPUSPEED_INIT ]; then - /sbin/service cpuspeed restart > /dev/null 2>&1 + if [ ! -e $CPUSPEED_SAVE_FILE -a -e $CPUSPEED_CFG ]; then + cp -p $CPUSPEED_CFG $CPUSPEED_SAVE_FILE + sed -e 's/^GOVERNOR=.*/GOVERNOR=performance/g' $CPUSPEED_SAVE_FILE > $CPUSPEED_CFG + fi + + service cpuspeed status >/dev/null 2>&1 + [ $? -eq 3 ] && touch $CPUSPEED_STARTED || rm -f $CPUSPEED_STARTED + + service cpuspeed restart >/dev/null 2>&1 else + echo >/dev/stderr + echo "Suggestion: install 'cpuspeed' package to get best performance and latency." >/dev/stderr + echo "Falling back to 'performance' scaling governor for all CPUs." >/dev/stderr + echo >/dev/stderr + for cpu in $CPUS; do - echo performance > $cpu/scaling_governor + gov_file=/sys/devices/system/cpu/$cpu/cpufreq/scaling_governor + save_file=$(printf $CPUSPEED_ORIG_GOV $cpu) + rm -f $save_file + if [ -e $gov_file ]; then + cat $gov_file > $save_file + echo performance > $gov_file + fi done fi @@ -36,28 +49,35 @@ start() { } stop() { - # Restore previous cpuspeed config - if [ -e $CPUSPEED_SAVE_FILE ]; then - cp -fp $CPUSPEED_SAVE_FILE $CPUSPEED_CFG - rm -f $CPUSPEED_SAVE_FILE - fi - - # Re-enable previous governor + # Re-enable previous CPU governor if [ -e $CPUSPEED_INIT ]; then - /sbin/service cpuspeed restart > /dev/null 2>&1 - elif [ -e $CPUSPEED_ORIG_GOV ]; then - for cpu in $CPUS; do - echo $(cat $CPUSPEED_ORIG_GOV) > $cpu/scaling_governor - done + if [ -e $CPUSPEED_SAVE_FILE ]; then + cp -fp $CPUSPEED_SAVE_FILE $CPUSPEED_CFG + rm -f $CPUSPEED_SAVE_FILE + fi + + if [ -e $CPUSPEED_STARTED ]; then + rm -f $CPUSPEED_STARTED + service cpuspeed stop >/dev/null 2>&1 + else + service cpuspeed restart >/dev/null 2>&1 + fi else for cpu in $CPUS; do - echo userspace > $cpu/scaling_governor - cat $cpu/cpuinfo_max_freq > $cpu/scaling_setspeed + cpufreq_dir=/sys/devices/system/cpu/$cpu/cpufreq + save_file=$(printf $CPUSPEED_ORIG_GOV $cpu) + + if [ -e $cpufreq_dir/scaling_governor ]; then + if [ -e $save_file ]; then + cat $save_file > $cpufreq_dir/scaling_governor + rm -f $save_file + else + echo userspace > $cpufreq_dir/scaling_governor + cat $cpufreq_dir/cpuinfo_max_freq > $cpufreq_dir/scaling_setspeed + fi + fi done fi - if [ -e $CPUSPEED_ORIG_GOV ]; then - rm -f $CPUSPEED_ORIG_GOV - fi # Restore transparent hugepages setting if [ -e $THP_SAVE ]; then