maxperfwiz script

After some discussion @kresimir and I have begun to rework the settings functions of the performance-tweaks package that is in the AUR and created/maintained by @Librewish

The aim was to create something that was more useful to more people
(by allowing for custom input and separated options)
As well as to make the process of applying tweaks 'safer'
(through explanation, prompts, use of /tmp and more)

For now it only includes certain sysctl tweaks, but not everything present in the original package.

To test it out you can save the following script, mark it executable, and run.

For setting custom values use the following flag

./maxperfwiz --custom-values

Thanks again to @kresimir and @Librewish

(edit - added gitlab source)

13 Likes

At present, we decided to comment out the tweaks to vm.laptop_mode and kernel.unprivileged_userns_clone. It is debatable whether they should be included or not. Any input on that is much appreciated.

Thanks in advance to everyone who is going to test this. :hamster:

this is also commented in original package.

this was never meant to be used for performance

what i had done is
reverse everything thats done in

so that was there so i commented it

1 Like

as for this
it is/was required to run flatpak apps
and also the much debatable brave browser

1 Like

Than that's settled, I'll remove these two from the script. Thanks!

Tested with default values, works as designed. I like it.
I am not sure about the watchdog bit though, as i don't have any clue regarding potential negative consequences.

1 Like

no negative consequences
as it will run when required if its disabled

but if its enabled it will always run

Aye, thanks!

Looks great!

  1. on vm.vfs_cache_pressure: 50% is a bit too early; 75% is a more reliable number (measured extensively when I still had a 4GB machine:)

    # (once cached, probably not immediately needed any more)
    #
    # This percentage value controls the tendency of the kernel to reclaim
    # the memory which is used for caching of directory and inode objects.
    #
    # At the default value of vfs_cache_pressure=100 the kernel will attempt to
    # reclaim dentries and inodes at a "fair" rate with respect to pagecache and
    # swapcache reclaim.  Decreasing vfs_cache_pressure causes the kernel to prefer
    # to retain dentry and inode caches.
    vm.vfs_cache_pressure = 75
    
  2. On vm.dirty_expire_centisecs: 3000 is way too high for an SSD: cat /sys/block/sda/queue/rotational is 0 for an SSD and then that value can be safely brought down to 500. (and 250 for the write-back)

All the rest: no constructive feed-back except: I'm going to run it right now! (as I've not implemented some of your parameters)

:innocent:

2 Likes

Thanks for the input, @Fabby!

I'll set the recommended value for vm.vfs_cache_pressure to be 75, that's a bit more conservative.

If you run the script, you'll notice how it gives a short description of each tweak. If you feel it could use improvement, and have a few minutes to spare, you could rewrite some of that text to be more clear. :hugs:

1 Like

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.

How about changing:

# by cscs and kresimir

to:

# Copyright (c) cscs & kresimir 2020
#
# This program is free software: you can redistribute it and/or modify it under 
# the terms of the GNU General Public License as published by the Free Software 
# Foundation, either version 3 of the License, or (at your option) any later 
# version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
# or FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
# THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
# See the GNU General Public License for more details.
#
# You DID NOT receive a copy of the GNU General Public License along with 
# this program as the license is bigger then this program.
# Therefore, see http://www.gnu.org/licenses/ for more details.

:grin:

Note 1: Please note: You DID NOT receive
Note2: chmod a+x now and providing further feed-back...

ew .. copyright ..

Whatever kind of :copyright: you want to use: Public domain is fine as well, but you have to provide some :copyright: as otherwise Amazon some corporation somewhere might be able to steal it and make tons of money of it which might not be what you intended with this...

¯\_(ツ)_/¯

I oscillate between things like the WTFPL/cat-supremacy/etc to things like CC/copyleft/GPL ... depending .. usually the more secure the license, the more likely some part of it was forked from somewhere else with one.



(sorry couldnt find the one I put under the catz)

2 Likes

Works for me: just include it...

:innocent:

Hey guys, i did installed that performance-tweaks aur few days ago. Should i uninstall it then install this new thing?

Your call.
I think this has safer defaults (and allows you to use custom values) .. but it isnt finished yet and doesnt have all of the same parameters yet.
(whether or not you want them or they have effect depends ... a lot of whats missing has no impact on systems using TLP [which is present by default on manjaro installs])

1 Like

this was the main reason i conflicted it with tlp

anyway i dont like what tlp does by default.

but its just me.
i never liked power saving

But if I understand correctly, if TLP is present, those tweaks do nothing. You have to disable TLP for them to have any effect, right?

Forum kindly sponsored by