Categories
SysOps

How to use lightweight compressed cache for swap pages

Configure a lightweight compressed cache for swap pages to mitigate the performance impact of swapping on your desktop.

This solution requires a configured swap partition as it is not intended to replace it.

$ swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-2 partition  16G 4.8G   -2

Enable zswap on a running system

Enable zswap module.

$ echo Y | sudo tee /sys/module/zswap/parameters/enabled
Y

Use z3fold  allocator as it is designed to store up to three compressed pages per physical page (default zbud). 

$ echo z3fold | sudo tee /sys/module/zswap/parameters/zpool
z3fold

Use fast zstd compression algorithm (default lzo).

$ echo zstd | sudo tee /sys/module/zswap/parameters/compressor
zstd

Define maximum memory pool in percent (default 20%)

$ echo 20 | sudo tee /sys/module/zswap/parameters/max_pool_percent

Ensure that checking for the same-value filled pages during store operation is enabled (default Y)

$ echo Y | sudo tee /sys/module/zswap/parameters/same_filled_pages_enabled
Y

Inspect zswap internals

Use debugfs to inspect zswap internals and determine that it is working as ex[ected.

$ sudo find /sys/kernel/debug/zswap/ -type f -exec grep -H . {} \;
/sys/kernel/debug/zswap/same_filled_pages:230585
/sys/kernel/debug/zswap/stored_pages:403937
/sys/kernel/debug/zswap/pool_total_size:287670272
/sys/kernel/debug/zswap/duplicate_entry:0
/sys/kernel/debug/zswap/written_back_pages:967347
/sys/kernel/debug/zswap/reject_compress_poor:0
/sys/kernel/debug/zswap/reject_kmemcache_fail:0
/sys/kernel/debug/zswap/reject_alloc_fail:0
/sys/kernel/debug/zswap/reject_reclaim_fail:242
/sys/kernel/debug/zswap/pool_limit_hit:334172

Enable zswap at boot

Install sysfs query tool and boot-time setup.

$ sudo apt install -y sysfsutils

Create zswap configuration.

$ cat <<EOF | sudo tee /etc/sysfs.d/zswap.conf
module/zswap/parameters/enabled = Y
module/zswap/parameters/zpool = z3fold
module/zswap/parameters/compressor = zstd
module/zswap/parameters/max_pool_percent = 20
module/zswap/parameters/same_filled_pages_enabled = Y
EOF
module/zswap/parameters/enabled = Y
module/zswap/parameters/zpool = z3fold
module/zswap/parameters/compressor = zstd
module/zswap/parameters/max_pool_percent = 20
module/zswap/parameters/same_filled_pages_enabled = Y

Additional information

zswap documentation