Zswap
   HOME

TheInfoList



OR:

zswap is a Linux kernel feature that provides a compressed
write-back In computing, a cache ( ) is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhe ...
cache Cache, caching, or caché may refer to: Places United States * Cache, Idaho, an unincorporated community * Cache, Illinois, an unincorporated community * Cache, Oklahoma, a city in Comanche County * Cache, Utah, Cache County, Utah * Cache County ...
for swapped pages, as a form of
virtual memory compression Virtual memory compression (also referred to as RAM compression and memory compression) is a memory management technique that utilizes data compression to reduce the size or number of paging requests to and from the auxiliary storage. In a virtua ...
. Instead of moving
memory page A page, memory page, or virtual page is a fixed-length contiguous block of virtual memory, described by a single entry in the page table. It is the smallest unit of data for memory management in a virtual memory operating system. Similarly, a p ...
s to a swap device when they are to be swapped out, zswap performs their
compression Compression may refer to: Physical science *Compression (physics), size reduction due to forces *Compression member, a structural element such as a column *Compressibility, susceptibility to compression * Gas compression *Compression ratio, of a ...
and then stores them into a
memory pool Memory pools, also called fixed-size blocks allocation, is the use of pools for memory management that allows dynamic memory allocation comparable to malloc or C++'s operator new. As those implementations suffer from fragmentation because ...
dynamically allocated in the system
RAM Ram, ram, or RAM may refer to: Animals * A male sheep * Ram cichlid, a freshwater tropical fish People * Ram (given name) * Ram (surname) * Ram (director) (Ramsubramaniam), an Indian Tamil film director * RAM (musician) (born 1974), Dutch * ...
. 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 tradeoff is the need for additional CPU cycles to perform the compression. As a result of reduced I/O, zswap offers advantages to various devices that use flash-based storage, including
embedded device An embedded system is a computer system—a combination of a computer processor, computer memory, and input/output peripheral devices—that has a dedicated function within a larger mechanical or electronic system. It is ''embedded'' as ...
s, netbooks and similar low-end hardware devices, as well as to other devices that use
solid-state drive A solid-state drive (SSD) is a solid-state storage device that uses integrated circuit assemblies to store data persistently, typically using flash memory, and functioning as secondary storage in the hierarchy of computer storage. It is a ...
s (SSDs) for storage. Flash memory has a limited lifespan due to its nature, so avoiding it to be used for providing swap space prevents it from wearing out quickly.


Internals

zswap is integrated into the rest of Linux kernel's
virtual memory In computing, virtual memory, or virtual storage is a memory management technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "creates the illusion to users of a very ...
subsystem using the
API An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how ...
provided by frontswap, which is a mechanism of the Linux kernel that abstracts various types of storage that can be used as swap space. As a result, zswap operates as a backend driver for frontswap by providing what is internally visible as a pseudo-RAM device. In other words, the frontswap API makes zswap capable of intercepting
memory page A page, memory page, or virtual page is a fixed-length contiguous block of virtual memory, described by a single entry in the page table. It is the smallest unit of data for memory management in a virtual memory operating system. Similarly, a p ...
s while they are being swapped out, and capable of intercepting
page fault In computing, a page fault (sometimes called PF or hard fault) is an exception that the memory management unit (MMU) raises when a process accesses a memory page without proper preparations. Accessing the page requires a mapping to be added t ...
s for the 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 Broadly speaking, modularity is the degree to which a system's components may be separated and recombined, often with the benefit of flexibility and variety in use. The concept of modularity is used primarily to reduce complexity by breaking a s ...
provided by the Linux kernel's crypto API, which makes it possible, for example, to offload the compression tasks from the main CPU using any of the hardware compression accelerators supported by the Linux kernel. The selection of the desired compression module can be performed dynamically at the boot time through the value of kernel boot parameter ; if not specified, it selects the
Lempel–Ziv–Oberhumer Lempel–Ziv–Oberhumer (LZO) is a lossless data compression algorithm that is focused on decompression speed. Design The original "lzop" implementation, released in 1996, was developed by Markus Franz Xaver Johannes Oberhumer, based on earlie ...
(LZO) compression. As of version 3.13 of the Linux kernel, zswap also needs to be explicitly enabled by specifying value for the kernel boot parameter . The maximum size of the memory pool used by zswap is configurable through the parameter , which specifies the maximum percentage of total system RAM that can be occupied by the pool. The memory pool is not preallocated to its configured maximum size, and instead 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 an out-of-memory condition, swapped pages are
evicted Eviction is the removal of a tenant from rental property by the landlord. In some jurisdictions it may also involve the removal of persons from premises that were foreclosed by a mortgagee (often, the prior owners who defaulted on a mortgag ...
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, implemented as a rewrite of the zbud allocator used by the Oracle's zcache, which is another virtual memory compression implementation for the Linux kernel. Internally, zbud works by storing up to two compressed pages (" buddies", hence the allocator name) per physical memory page, which brings both advantages due to easy coalescing and reusing of freed space, and disadvantages due to possible lower memory utilization. However, as a result of its design, zbud cannot allocate more memory space than it would be originally occupied by the uncompressed pages.


History

Both zswap and zbud were created by Seth Jennings. The first public announcement was in December 2012, and the development continued until May 2013 at which point the
codebase In software development, a codebase (or code base) is a collection of source code used to build a particular software system, application, or software component. Typically, a codebase includes only human-written source code files; thus, a codeb ...
reached its maturity although still having the status of an experimental kernel feature. zswap (together with zbud) was merged into the
Linux kernel mainline The Linux kernel is a free and open-source, monolithic, modular, multitasking, Unix-like operating system kernel. It was originally authored in 1991 by Linus Torvalds for his i386-based PC, and it was soon adopted as the kernel for the GNU o ...
in kernel version 3.11, which was released on September 2, 2013. Since version 3.15 of the Linux kernel, which was released on June 8, 2014, zswap properly supports multiple swap devices.


Alternatives

One of the alternatives to zswap is
zram zram, formerly called compcache, is a Linux kernel module for creating a compressed block device in RAM, i.e. a RAM disk with on-the-fly disk compression. The block device created with zram can then be used for swap or as general-purpose RAM ...
, 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 In Unix-like operating systems, a device file or special file is an interface to a device driver that appears in a file system as if it were an ordinary file. There are also special files in DOS, OS/2, and Windows. These special files allow a ...
using RAM for storing data, which acts as a regular and separate swap device. In comparison, zswap acts as a RAM-based cache for swap devices. This provides zswap with an
eviction Eviction is the removal of a tenant from rental property by the landlord. In some jurisdictions it may also involve the removal of persons from premises that were foreclosed by a mortgagee (often, the prior owners who defaulted on a mortgag ...
mechanism for less used swapped pages, which zram lacked until the introduction of in kernel version 4.14. Though, as a result of its design, at least one already existing swap device is required for zswap to be used.


See also

* Cache (computing) * Linux swap *
Swap partitions on SSDs A solid-state drive (SSD) is a solid-state storage device that uses integrated circuit assemblies to store data persistently, typically using flash memory, and functioning as secondary storage in the hierarchy of computer storage. It i ...


References

{{Operating system Free software programmed in C Linux kernel features Memory management Virtual memory