Zswap

From LinuxReviews
Jump to navigationJump to search
Tux-000f.svg

zswap is a Linux kernel module that facilitates swap page caching using compressed memory. Swap pages going to and from swap devices can be cached using a up to 3:1 compressed memory as cache storage. Compressed memory pages are only swapped to disk when the memory set aside for zswap is full. This is specially useful for systems without a fast SSD since swapping to and from disk takes time. The trade-off to be aware of is that the memory pool set aside for zswap will not be available to regular applications when it is used which means that the total system memory available for applications is reduced. zswap does not use memory if there is nothing in the cache.

zswap is a swap cache meant to be used with a regular swap device. See zram if you want to make a memory swap device.

Configuration

The max_pool_percent option tells zswap how much memory it should use to cache pages going to and from swap. It is set as a percentage of system RAM. 10% on a machine with 32 GiB RAM allocates more RAM to zswap than a machine with 2 GiB RAM has.

Memory set aside for zswap is not used until the swap cache is filled. It remains available until swap pages actually fill the cache.

Choosing the "right" number may be hard. A high number means you get a bigger cache which makes swapping faster and more responsive, which is good. There is also a penalty which can hamper performance: Less memory is available to actively running processes when the swap cache is filled. If you have 8 GiB total and you make a 3 GiB zswap cache with zstd and your machine starts swapping hard and the cache is filled you're left with either 8-1=7 GiB or 8-1.5=6.5 GiB memory available to running applications. How much memory a 3 GiB zswap cache actually uses will depend on the compression algorithm and the allocator. Both are configurable.

A simple way to enable zswap swap cache is to create simple script which configures it and a basic systemd service file that runs it at boot-time.

File: /usr/local/bin/zswapcache-on
#!/bin/sh
if [ "${1}"  == "off" ];then
  echo N >/sys/module/zswap/parameters/enabled
  exit 0
fi

# We must have the zswap technology so RAM is 
# compressed to a RAM pool before they are swapped
echo 1 > /sys/module/zswap/parameters/enabled

# % of total system memory can be used for compressed
# swap pages. These are sent to disk when zswap pool
# is full.
echo 10 > /sys/module/zswap/parameters/max_pool_percent

## Use this with kernels prior to 5.1
# echo lz4 > /sys/module/zswap/parameters/compressor

## lzo-rle is the best option for Linux 5.1-5.7
#echo lzo-rle > /sys/module/zswap/parameters/compressor

## Linux 5.7+ can use ZSTD to compress zswap
echo zstd   > /sys/module/zswap/parameters/compressor

# Only Kernel 4.8 and above. For 3:1 compression
echo z3fold > /sys/module/zswap/parameters/zpool

The above script can be started at boot by creating a very simple systemd service file:

File: /etc/systemd/system/zswapcache.service
[Unit]
Description=Configures zswap cache
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/zswapcache-on
ExecStop=/usr/local/bin/zswapcache-on off
RemainAfterExit=yes

[Install]
WantedBy = multi-user.target

Make the script executable with

chmod +x /usr/local/bin/zswapcache-on

then reload the systemd services with

systemctl daemon-reload

and you will be able start and enable the service with

systemctl enable --now zswapcache

Compression algorithms

zstd is the by far best-compressing algorithm. It will do something in the neighborhood of 5:1 compression of typical swap pages. It is NOT available in kernels <=5.7.

lzo-rle is the second best choice. It too is unavailable in older kernels. Not entirely sure when it was introduced, perhaps Linux 5.1. 4.xx kernels do not have this technology.

lzo is the best choice if neither zstd or lzo-rle are available.

Allocators

There are three zpool allocators available: zbud, zsmalloc and z3fold. zbud supports page eviction, it can throw old pages out if new ones are needed. zsmalloc can not do that, it has to reject new pages if the pool is full. The kernel documentation does not say if z3fold can evict or not. z3fold is only available in <=4.8 Linux kernels.

  • zbud will try to compress 2:1 and mostly do it. You get close to 2 GiB memory in a 1 GiB zswap memory pool using zbud.
  • zsmalloc has a "more complex compressed page storage method, and it can achieve greater storage densities" according to the kernel documentation. How much better compression it provides is not detailed.
  • z3fold is similar to zbud but it can compress 3:1, not just 2:1. It is a fairly recent addition to the Linux kernel so you may not have it available if you are using a LTS distribution.

See also

  • zram can be used to create compressed memory-backed devices such as a memory-compressed swap device. zram allows you to create an actual swap device, not just a swap drive cache.

Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.