|Developer(s)||Seth Jennings and others|
|Type||Linux kernel features|
|License||GNU General Public License|
zswap is a Linux kernel feature that provides a compressed write-back cache for swapped pages, as a form of virtual memory compression. Instead of moving memory pages to a swap device when they are to be swapped out, zswap performs their compression and then stores them into a memory pool dynamically allocated inside system's RAM. Later writeback to the actual swap device is deferred or even completely avoided, resulting in a significantly reduced I/O for Linux systems that require swapping; the trade-off are additional CPU cycles required for compression.
As a result of reduced I/O, zswap offers advantages for various devices using flash-based storage, including embedded devices, netbooks and similar low-end hardware devices, as well as other devices using SSDs for storage. Flash memory has a limited lifespan due to its nature, so avoiding its usage for providing swap space prevents it from wearing out quickly.
zswap is integrated into the rest of Linux kernel's virtual memory subsystem by using the API provided by frontswap, which is a Linux kernel's mechanism that abstracts various types of storage that can be used as swap space. Thus, by providing what is internally visible as a pseudo-RAM device, zswap operates as a backend driver for frontswap. In other words, the frontswap API makes zswap able to intercept memory pages while they are being swapped out, and to intercept page faults for already swapped pages; the access to those two paths allows zswap to act as a compressed write-back cache for swapped pages.
Internally, zswap uses compression modules provided by the Linux kernel's crypto API, making it possible (for example) to use any hardware compression accelerators supported by the Linux kernel, which allow the compression tasks to be offloaded from the main CPU. Selection of the desired compression module can be performed dynamically at the boot time, through value of the kernel boot parameter zswap.compressor; the default value is deflate, which indicates use of Lempel–Ziv–Oberhumer (LZO) compression. Also, as of version 3.13 of the Linux kernel, zswap needs to be explicitly enabled by specifying value 1 for the kernel boot parameter zswap.enabled.
Maximum size of the memory pool used by zswap is configurable through the sysfs parameter max_pool_percent, which specifies maximum percentage of total system RAM that can be occupied by the pool. The memory pool is not preallocated to its configured maximum size; instead, it grows and shrinks as required. When the configured maximum pool size is reached as the result of performed swapping, or when growing the pool is impossible due to out-of-memory conditions, swapped pages are evicted from the memory pool to a swap device on the least recently used (LRU) basis. This approach makes zswap a true swap cache, as the oldest cached pages are evicted to a swap device once the cache is full, making room for newer swapped pages to be compressed and cached.
zbud is a special-purpose memory allocator used internally by zswap for storing compressed pages; it was implemented as a rewrite of the zbud allocator used internally by the Oracle's zcache. zbud works by storing up to two compressed pages ("buddies", hence the allocator name) per physical memory page, what brings both advantages (easy coalescing and reusing of freed space) and disadvantages (possible lower memory utilization). However, due to its design, zbud cannot allocate more memory space than it would be originally used by uncompressed pages.
Both zswap and zbud are developed by Seth Jennings. The first public announcement was in December 2012, and the development continued until May 2013 at which point the codebase reached its maturity though still being marked as an experimental kernel feature.
One of the alternatives to zswap is zram, which provides a similar but still different "swap compressed pages to RAM" mechanism to the Linux kernel.
The main difference is that zram provides a compressed block device using RAM for storing data, which acts as a regular and separate swap device. Using zram requires additional configuration in userspace (by using mkswap and swapon) so the RAM-based swap device provided by zram is initialized and configured to be used. As a result of its design, zram can provide swap space even if no other swap devices are available, thus making itself more suitable for systems not already providing swap space, such as embedded devices.
In comparison, zswap operates transparently requiring no additional configuration in userspace, and acts as a RAM-based compressed cache for regular swap devices. This provides zswap with an eviction mechanism for less used swapped pages, which zram lacks. Though, as a result of its design, at least one already existing swap device is required in order for zswap to be used.
- Seth Jennings (February 12, 2013). "The zswap compressed swap cache". LWN.net. Retrieved January 22, 2014.
- Jenifer Hopper (December 11, 2012). "New Linux zswap compression functionality". IBM. Retrieved January 31, 2014.
- Michael Larabel (July 11, 2013). "Zswap Merged Into The Linux 3.11 Kernel". Phoronix. Retrieved February 5, 2014.
- "Linux kernel documentation: Documentation/vm/zswap.txt". kernel.org. November 22, 2013. Retrieved January 22, 2014.
- Dan Magenheimer (April 22, 2010). "Frontswap [PATCH 0/4] (was Transcendent Memory): Overview". gmane.org. Retrieved December 23, 2014.
- Jonathan Corbet (May 4, 2010). "Cleancache and Frontswap". LWN.net. Retrieved March 26, 2014.
- "kernel/git/torvalds/linux.git: zswap: add to mm/". Linux kernel source tree. kernel.org. July 11, 2013. Retrieved February 5, 2014.
- "kernel/git/torvalds/linux.git: zbud: add to mm/". Linux kernel source tree. kernel.org. July 11, 2013. Retrieved February 5, 2014.
- "[PATCH 0/8] zswap: compressed swap caching". gmane.org. December 11, 2012. Retrieved January 5, 2014.
- "[PATCHv10 0/4] zswap: compressed swap caching". gmane.org. May 8, 2013. Retrieved January 5, 2014.
- "Linux kernel 3.11, Section 9. Zswap: A compressed swap cache". kernelnewbies.org. September 2, 2013. Retrieved January 22, 2014.
- "Linux kernel 3.15, Section 4. Memory management". kernelnewbies.org. June 8, 2014. Retrieved June 15, 2014.
- "kernel/git/torvalds/linux.git: mm/zswap: support multiple swap devices". Linux kernel source tree. kernel.org. April 7, 2014. Retrieved June 15, 2014.
- Dan Magenheimer (April 3, 2013). "In-kernel memory compression". LWN.net. Retrieved March 8, 2014.
|Wikimedia Commons has media related to Linux kernel.|
- Linux Transparent Memory Compression on YouTube, September 30, 2013, by Seth Jennings
- Zswap – a compressed page add-on for the Linux kswapd, University of Liege, March 15, 2013, by Sylvain Martin
- The Compression Cache: Virtual Memory Compression for Handheld Computers, March 16, 2000, by Michael J. Freedman