Skip to content

Instantly share code, notes, and snippets.

@jakelee8
Last active July 12, 2023 06:44
Show Gist options
  • Save jakelee8/d11248bcae380aeea630a890713ec16b to your computer and use it in GitHub Desktop.
Save jakelee8/d11248bcae380aeea630a890713ec16b to your computer and use it in GitHub Desktop.
Linux performance tweaks

Linux performance tweaks

All commands are run as root.

Enable swap file

Create the swap file using either of these commands. fallocate is faster but may not work on all filesystems.

fallocate -l 32G /swap/swap0
dd if=/dev/zero of=/swap/swap0 bs=1M count=32768

Next, secure and enable the swap file.

chmod 600 /swap/swap0
mkswap /swap/swap0
swapon /swap/swap0

Make it permanent by an entry for the swap file to /etc/fstab.

# /etc/fstab
/swap/swap0 none swap defaults 0 0

Linux can be configured to prioritize which swap files to use. Swap files with higher priority numbers are used first.

/swap/swap0 none swap defaults,prio=1000 0 0
/swap/swap1 none swap defaults,prio=0    0 0

Tweak swappiness

Swappiness controls how aggresively the kernel swaps memory from RAM. The default is 60% and a lower number is known to improve system responsiveness.

cat /proc/sys/vm/swappiness

Use sysctl to change the swappiness temporarily.

sysctl vm.swappiness=10

To make it permanent, add the line vm.swappiness=10 to a sysctl configuration file.

/etc/sysctl.d/local.conf

vm.swappiness=1

Shell script

touch /etc/sysctl.d/local.conf
sed -i -r '/^vm.swappiness=/D' /etc/sysctl.d/local.conf
echo 'vm.swappiness=1' >> /etc/sysctl.d/local.conf
sysctl vm.swappiness=1

Swapspace: dynamic swap allocation

Swapspace is an unmaintained dynamic swap manager for Linux. It creates swap files as needed based on system memory pressure.

Enable zswap

Zswap enables Linux to compress memory pages in response to memory pressure, thereby reducing how much memory needs to be swapped to disk.

First, check that zswap is enabled for kernel build:

cat /boot/config-* | grep ZSWAP

If so, add the appropriate kernel options to /etc/default/grub to enable it at boot.

  • zswap.enabled=1 enables zswap
  • zswap.compressor=lz4 configures zswap to use lz4 compression
  • zswap.max_pool_percent=50 limits zswap from using more than 50% of physical memory
# /etc/default/grub
GRUB_CMDLINE_LINUX="console=ttyS0,38400n8 elevator=noop zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=50"

Update the GRUB boot configuration using grub-mkconfig.

grub-mkconfig -o /boot/grub/grub.cfg

You may also need to load the lz4 and lz4_compress kernel modules by adding the entries to /etc/modules.

/etc/modules

# LZ4 compression support for zswap
lz4
lz4_compress

Regenerate initramfs after editing /etc/modules.

update-initramfs -u

Zswap should now be enabled on restart.

To check that zswap is enabled, check the system messages.

dmesg | grep 'zswap.*'

Adjust these steps as necessary if using a newer version of GRUB or a different bootloader.

Shell script

ZSWAP_FLAGS='zswap.enabled=1 zswap.compressor=lz4'
GRUB_CMDLINE_LINUX=$(sed -rn 's@^GRUB_CMDLINE_LINUX="\s*(.*)"$@\1@p' /etc/default/grub)
GRUB_CMDLINE_LINUX=$(echo "$GRUB_CMDLINE_LINUX" | sed -r 's/\bzswap.\w+=.+\s*\b//g')
sed -i -r 's@^(GRUB_CMDLINE_LINUX=").*(")$@\1'" $ZSWAP_FLAGS"'\2@' /etc/default/grub
sed -i -r '/^lz4(_compress)?/D' /etc/modules
cat <<EOL>> /etc/modules
lz4
lz4_compress
EOL
update-initramfs -u
echo 1 > /sys/module/zswap/parameters/enabled
echo lz4 > /sys/module/zswap/parameters/compressor
@jchevali
Copy link

It's not /etc/modules where the lz4 module entry should be added, it's /etc/initramfs-tools/modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment