Monday, June 27, 2022

Guide: Setting up zswap

This is a guide to setting up zswap on Linux-based operating systems. What is zswap and why should you use it? Zswap is a way of giving your computer additional virtual memory, like increasing your RAM. It is very useful for systems with low RAM (< 8 GB) and quite useful even for systems with more RAM. This guide will show you how to setup zswap with the lz4 compression algorithm, which is very fast. But first, to answer some questions.

Warning for BTRFS users: read the wiki before creating a swapfile. It is easier to use ZRAM instead.

Does this come with a performance penalty?

There is no such thing as free lunch – compressing and decompressing pages in virtual memory will tax your CPU. However, using compressed memory is faster than swapping to an SSD, and orders of magnitude faster than swapping to a spinning hard disk. It is also better than running out of memory, which results in either your system locking up or the out-of-memory killer killing some important process. As oom killers are not very intelligent, it is wise to avoid this.

Note: there is no performance penalty until you actually start swapping, just so we’re clear.

What about ZRAM?

ZRAM is good too; I have used it. But zswap does not compress pages which are incompressible, instead sending them to your swap file. This is a good thing, as it avoids wasting CPU cycles compressing pages that are not compressible anyway. ZRAM is good for hard disk or SD-backed computers; I feel zswap is more appropriate for SSD-backed devices as swapping to an SSD is not such a big problem.

Step 1

Note: run all the commands below in the terminal, copying them one line at a time and hitting enter. Make sure they are copied correctly.

First check if you have a swap file by running free -h. If you do have a swap file, continue to the next step. Otherwise run the code below.

sudo su
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab

Step 2

Note: the following assumes are you are using grub. PopOS users should follow the instructions here.

Run sudo nano /etc/default/grub and edit the line GRUB_CMDLINE_LINUX_DEFAULT to read:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=50 zswap.zpool=z3fold"

What does the max pool percent variable mean? This refers to the maximum % of your RAM that will be taken up with compressed storage. It is dynamically allocated, so it doesn’t take up any space until you actually start using it. For most systems, 50% is a good maximum. For really low memory systems, you can try 70%. Anything higher will make the system unusably slow (Google has actually benchmarked this for Chrome OS).

Save your changes (type Ctrl+X and type y and then enter). Now run:

sudo update-grub

Step 3

Run the following:

sudo su
echo lz4 >> /etc/initramfs-tools/modules
echo lz4_compress >> /etc/initramfs-tools/modules
echo z3fold >> /etc/initramfs-tools/modules
update-initramfs -u

You are done! Reboot and run cat /sys/module/zswap/parameters/enabled. If zswap is working, you should see a Y printed.

Sources

LKML

Linux Kernel Documentation

Make Tech Easier

IBM

No comments:

Post a Comment

EVs are not the future—hybrids are

There has been a wild surge in optimism in EVs—really, a kind of hysteria—with the EU and UK governments hoping to ban combustion engines in...