Zswap

From Gentoo Wiki
Jump to:navigation Jump to:search
Resources

Zswap is a lightweight compressed cache for swap pages.

Introduction

Zswap is a kernel feature that provides a compressed RAM cache for swap memory pages. Pages which would otherwise typically be swapped out to hard disk are instead compressed and stored into a memory pool in RAM. Once this memory pool is full or the available RAM is exhausted, the least recently used (LRU) page is decompressed and written to swap on hard disk, as if it had not been intercepted by zswap. After the page has been moved to swap, the compressed version in the memory pool can be freed and used again.

Note
Next to zswap there is something related called zram. Zram can also be used to create a swap device for compressed pages in memory.

Zswap and Gentoo

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 then have 25% of 8GB times two, what is 4GB of swap space in memory which only costs 2 GB of RAM. 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.

Differences between zswap and zram based swap

A short overview:

  • Zswap works in conjunction with regular swap while a zram based swap device does not require a backing swap device and may work standalone (if you don’t want to have any swap on hard disk, i.e. on SSD or kind of flash memory).
  • Zswap is a compressed swap cache in RAM and works as a type of proxy for regular swap (in this context also called backing swap device). Zswap gets filled up first and evicts pages from compressed cache on an LRU basis to the backing swap device when the compressed pool reaches its size limit. This not only speeds up swap usage but also reduces hits on backing swap device (i.e. SSD).
  • A zram based swap on the other hand works like regular swap (but compressed in RAM) without the opportunity to evict pages. So it gets filled up gradually until it’s full. After that, the next (but probably slower) swap in order (i.e. on hard disk) fills up. This way, you may have stored less frequently used memory pages within the faster zram based swap, while newer frequently used memory pages get swapped to slower hard disk.

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)
    [ ]   Enable the compressed cache for swap pages by default
    -*- 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 "" *
compressor:lzo
enabled:N
max_pool_percent:20
same_filled_pages_enabled:Y
zpool:zbud

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

Kernel level configuration

On newer kernels, enable default settings for zswap in the kernel:

KERNEL Enable zswap by default
[*] Compressed cache for swap pages (EXPERIMENTAL)
          Compressed cache for swap pages default compressor (LZ4)  --->
          Compressed cache for swap pages default allocator (zbud)  --->
    [*]   Enable the compressed cache for swap pages by default
    -*- Common API for compressed memory storage
    -*-   Low (Up to 2x) density storage for compressed pages
    < >   Up to 3x density storage for compressed pages
    < > Memory allocator for compressed pages
    [ ]   Export zsmalloc statistics

See also

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

External resources