From Gentoo Wiki
Jump to:navigation Jump to:search

Zswap is a lightweight compressed cache for swap pages.


Zswap is particularly interesting for Gentoo, because it can help make the most of limited RAM resources when emerging (compiling) large packages. It uses a part of the available RAM as compressed swap space.

Imagine a system with 8 GB of RAM with zswap using up to 25% of it, and zswap reaching a compression ratio of a factor two. The system will them have 25% of 8GB times two is 4GB of swap space in memory, and it only cost 2 GB. Not only is this a very effective use of RAM memory, but it can be faster then swapfiles on older mechanical harddrives.

For systems with swap files on SSD, zswap might help to relieve wear on the SSDs.

Zswap works in conjunction with a normal swap backing store.

Next to zswap there is also something called zram. Zram can also be used to create a swap device for compressed pages in memory. The difference between zswap and zram is that zswap is able to evict pages to a disk based backing store, and zram is not.

Kernel configuration

The kernel needs to have swap, frontswap, options for zswap, and compression algorithms configured:

KERNEL Enable zswap
General setup  ---> 
    [*] Support for paging of anonymous memory (swap)
Memory Management options  ---> 
    [*] Enable frontswap to cache swap pages if tmem is present
    [*] Compressed cache for swap pages (EXPERIMENTAL)
    -*- Common API for compressed memory storage
    <*> Low (Up to 2x) density storage for compressed pages
    <*> Up to 3x density storage for compressed pages
Cryptographic API  --->
    {*}   Deflate compression algorithm
    -*-   LZO compression algorithm
    <*>   LZ4 compression algorithm
    <*>   LZ4HC compression algorithm
    <*>   Zstd compression algorithm

Interactive configuration

The zswap parameters can be examined as follows:

root #cd /sys/module/zswap/parameters
root #grep "" *

Enabling zswap can be done by writing "1" to the enabled file:

root #echo 1 > /sys/module/zswap/parameters/enabled

LZ4 is a popular choice for the compression algorithm:

root #echo lz4 > /sys/module/zswap/parameters/compressor

Making the configuration permanent

Using the kernel commandline

Zswap can be configured permanently using the kernel commandline, e.g when using GRUB:

FILE /etc/default/grub
GRUB_CMDLINE_LINUX="zswap.enabled=1 zswap.compressor=lz4"

Do not forget to regenerate the GRUB configuration.

Alternative: using local.d

Create a file in /etc/local.d:

FILE /etc/local.d/50-zswap.start
# configure zswap
echo lz4 > /sys/module/zswap/parameters/compressor
echo 1 > /sys/module/zswap/parameters/enabled

Make the file executable:

root #chmod +x /etc/local.d/50-zswap.start

See also

  • Zram — a Linux kernel feature and userspace tools for creating compressible RAM-based block devices.

External resources