Zswap - ArchWiki
Zswap - ArchWiki
Zswap - ArchWiki
org/title/Zswap
zswap
zswap is a kernel feature that provides a compressed RAM cache for swap Related articles
pages. Pages which would otherwise be swapped out to disk are instead
compressed and stored into a memory pool in RAM. Once the pool is full or Kernel parameters
the RAM is exhausted, the least recently used (LRU) page is decompressed
and written to disk, as if it had not been intercepted. After the page has been Mkinitcpio
decompressed into the swap cache, the compressed version in the pool can be
freed.
The difference compared to zram is that zswap works in conjunction with a swap device while zram with
created swap on top of it is a swap device in RAM that does not require a backing swap device.
1 Toggling zswap
All officially supported kernels have zswap enabled by default. This can be verified with
zgrep CONFIG_ZSWAP_DEFAULT_ON /proc/config.gz .
2 Customizing zswap
$ grep -r . /sys/module/zswap/parameters/
/sys/module/zswap/parameters/same_filled_pages_enabled:Y
/sys/module/zswap/parameters/enabled:Y
/sys/module/zswap/parameters/max_pool_percent:20
/sys/module/zswap/parameters/compressor:zstd
/sys/module/zswap/parameters/non_same_filled_pages_enabled:Y
/sys/module/zswap/parameters/zpool:zsmalloc
/sys/module/zswap/parameters/exclusive_loads:N
/sys/module/zswap/parameters/accept_threshold_percent:90
For more information about exclusive_loads parameter (which is not currently in zswap
documentation), see this commentary in linux kernel source code (https://github.com/torvalds/linux/
blob/9ed22ae6be817d7a3f5c15ca22cbc9d3963b481d/mm/Kconfig#L48).
1 of 4 15/11/2023, 18:08
zswap - ArchWiki https://wiki.archlinux.org/title/Zswap
The boot time load message showing the initial configuration can be retrieved with:
When changing the compression algorithm via boot parameter, one needs to ensure the corresponding
compression module is loaded early during boot (refer to #Compression algorithm).
With the zbud data allocator, 2 compressed objects are stored into 1 page which limits the compression
ratio to 2 or less.
A zpool of type z3fold is created by default. Use the kernel parameter zswap.zpool to select another
allocator at boot time. The data allocator can also be changed at a later stage via the sysfs interface.
2 of 4 15/11/2023, 18:08
zswap - ArchWiki https://wiki.archlinux.org/title/Zswap
There is no issue changing the compression at runtime using sysfs but zswap starts in this case with zstd
and switches at a later stage to the defined algorithm. To start zswap with another algorithm straight away,
this must be set via the kernel boot parameters and the corresponding module must be loaded early by the
kernel. This can be achieved by following these steps:
1. Add the modules required for the chosen compressor to the mkinitcpio#MODULES
array.
2. Regenerate the initramfs.
3. Set the compression algorithm using the zswap.compressor= kernel parameter.
On next boot, see #Current parameters to check if zswap now uses the requested compressor.
3 Zswap statistics
To see zswap statistics you can run this:
# grep -r . /sys/kernel/debug/zswap
duplicate_entry:0
pool_limit_hit:13404213
pool_total_size:6184960 (pool size in total in pages)
reject_alloc_fail:5
reject_compress_poor:0
reject_kmemcache_fail:0
reject_reclaim_fail:13404213
stored_pages:4251 (pool size after compression)
written_back_pages:0
4 See also
▪ zswap: How to determine whether it is compressing swap pages? (https://lore.ker
nel.org/lkml/1674223.HVFdAhB7u5@merkaba/).
▪ IBM Support Article "New Linux zswap compression functionality" (benchmarks
images do not load) (https://www.ibm.com/support/pages/new-linux-zswap-com
pression-functionality).
▪ Ask Ubuntu: zram vs. zswap vs. zcache (https://askubuntu.com/questions/471912
/zram-vs-zswap-vs-zcache-ultimate-guide-when-to-use-which-one). (zcache is
deprecated)
▪ Arch Linux forum thread (https://bbs.archlinux.org/viewtopic.php?id=169585).
▪ LWN.net technical article by the main developer of zswap (https://lwn.net/Article
s/537422/).
3 of 4 15/11/2023, 18:08
zswap - ArchWiki https://wiki.archlinux.org/title/Zswap
4 of 4 15/11/2023, 18:08