Difference between revisions of "Zswap"

From ArchWiki
Jump to navigation Jump to search
(A bit of formatting and added instructions for using lz4.)
(Using kernel boot parameters: Add link to section "Compression algorithm")
 
(82 intermediate revisions by 17 users not shown)
Line 1: Line 1:
 
[[Category:Kernel]]
 
[[Category:Kernel]]
{{Poor writing|Grammar.}}
+
[[ja:Zswap]]
 
{{Related articles start}}
 
{{Related articles start}}
 
{{Related|Kernel parameters}}
 
{{Related|Kernel parameters}}
 +
{{Related|Mkinitcpio}}
 
{{Related articles end}}
 
{{Related articles end}}
"Zswap is a Linux kernel feature providing a compressed write-back cache for swapped pages. 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." - Wikipedia
+
[[Wikipedia:zswap|Zswap]] is a kernel feature that provides a compressed RAM cache for swap 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 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 decompressed into the swap cache, the compressed version in the pool can be freed.
  
In simple words, zswap - no swap as zram(compcache) or swap file/partition.
+
The [[Improving performance#Zram_or_zswap|difference compared to zram]] is that zswap works in conjunction with a [[swap]] device while zram is a swap device in RAM that does not require a backing swap device.
Zswap only interlayer, for work this feature you need manually setup [[swap]] partition/file.
 
  
==Get in work==
+
==Enabling zswap==
====For enable zswap, add to kernel boot options (line) this====
 
  zswap.enabled=1
 
  
====Customize the maximum allowed size====
+
To enable zswap at runtime, execute the following command:
  zswap.max_pool_percent=25
+
# echo 1 > /sys/module/zswap/parameters/enabled
  
====Changing the compression algorithm ====
+
To enable zswap permanently:
  zswap.compressor=lzo #deflate #lz4
+
* Either add to your [[kernel parameters#Configuration|kernel parameters]] {{ic|1=zswap.enabled=1}}
  
====Enable LZ4 compression (faster than lzo and deflate, less compression) ====
+
* Alternatively, you can use {{pkg|systemd-swap}} which is a script to manage swap spaces, in this case the line {{ic|1=zswap_enabled=1}} must be present in {{ic|/etc/systemd/swap.conf}} and you must [[start/enable]] {{ic|systemd-swap.service}}.
  
#Add {{ic|1=lz4 lz4_compress}} to the [[Mkinitcpio#MODULES]] array.
+
==Customizing zswap==
#Rerun {{ic|mkinitcpio}}.
+
=== Current parameters ===
#Add {{ic|1=zswap.compressor=lz4}} to your kernel parameters [[Kernel_parameters#Configuration]].
+
 
#Reboot
+
Zswap has 4 customizable parameters. The live settings can be displayed using:
#Check dmesg :
+
 
    $ dmesg | grep 'zswap.* compressor'
+
{{hc|$ grep -R . /sys/module/zswap/parameters|
    [   0.307223] zswap: using lz4 compressor
+
/sys/module/zswap/parameters/enabled:Y
 +
/sys/module/zswap/parameters/max_pool_percent:25
 +
/sys/module/zswap/parameters/zpool:z3fold
 +
/sys/module/zswap/parameters/compressor:lz4
 +
}}
 +
 
 +
The boot time load message showing the initial configuration can be retrieved with:
 +
 
 +
{{hc|$ dmesg {{!}} grep zswap:|
 +
[    0.317569] zswap: loaded using pool lz4/z3fold
 +
}}
 +
 
 +
=== Set parameters ===
 +
==== Using sysfs ====
 +
Each setting can be changed at runtime via the [[Wikipedia:sysfs|sysfs]] interface. For example using the following command to amend the first parameter of the list, {{ic|compressor}}:
 +
# echo lz4 > /sys/module/zswap/parameters/compressor
 +
 
 +
==== Using kernel boot parameters ====
 +
To set the parameter permanently, the corresponding option, for example {{ic|1=zswap.compressor=lz4}}, must be added to the kernel boot parameter. Therefore to set permanently all the above settings, the following kernel parameters must be added: {{ic|1=zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=20 zswap.zpool=z3fold}}.
 +
 
 +
When changing the compression algorithm via boot parameter the corresponding kernel modules must be available, at least when using ''lz4'' (refer to [[#Compression algorithm]]).
 +
 
 +
==== Using systemd-swap ====
 +
For the ones using the ''systemd-swap'' script, it modifies the ''sysfs'' parameters at a later stage of the boot process based on its [https://github.com/Nefelim4ag/systemd-swap/blob/master/swap.conf configuration] stored in {{ic|/etc/systemd/swap.conf}}.
 +
 
 +
===Maximum pool size===
 +
The memory pool is not preallocated, it is allowed to grow up to a certain limit in percentage of the total memory available, by default up to 20% of the total RAM. Once this threshold is reached, pages are evicted from the pool into the swap device.
 +
The maximum compressed pool size is controlled with the parameter {{ic|max_pool_percent}}.
 +
 
 +
===Compressed memory pool allocator===
 +
The ''zpool'' parameter controls the management of the compressed memory pool, it is by default set to {{ic|zbud}}.
 +
With the ''zbud'' data allocator, 2 compressed objects are stored into 1 page which limits the compression ratio to 2 or less. The superior [https://www.kernel.org/doc/Documentation/vm/z3fold.txt z3fold] allocator allows up to 3 compressed objects by page. The compression ratio with ''z3fold'' typically averages 2.7 while it is 1.7 for ''zbud''.
 +
 
 +
A ''zpool'' of type ''zbud'' is created by default, use the kernel parameter {{ic|1=zswap.zpool=z3fold}} to select the ''z3fold'' method instead. The data allocator can also be changed at a later stage via the ''sysfs'' interface.
 +
 
 +
===Compression algorithm===
 +
For page compression, zswap uses compressor modules provided by the kernel's cryptographic API. It uses by default the ''lzo'' compression algorithm but this can be changed with {{ic|1=zswap.compressor}}. ''Lz4'' can be used instead of ''lzo'' for faster compression and decompression for a slightly lower compression ratio. Other possible choices are ''lz4hc'' or ''deflate''.
 +
 
 +
There is no issue setting the compression to ''lz4'' at runtime using ''sysfs'' or via ''systemd-swap'' but zswap starts in this case with ''lzo'' and switches at a later stage to ''lz4''. To use zswap with ''lz4'' straight away, this must be defined in the kernel boot parameter and the ''lz4'' module must be loaded early by the kernel. This can be achieved by following these steps:
 +
 
 +
#Add {{ic|1=lz4 lz4_compress}} to the [[mkinitcpio#MODULES]] array.
 +
#Replace the ramdisk environment with a newly generated one: {{bc|# mkinitcpio -g /boot/''initramfs''.img}}
 +
#Add {{ic|1=zswap.compressor=lz4}} to your [[kernel parameters]].
 +
 
 +
On next reboot, see [[#Current parameters]] to check if zswap now uses ''lz4'' as compressor.
  
 
== See also ==
 
== See also ==
  
* [https://www.kernel.org/doc/Documentation/vm/zswap.txt Zswap documentation].
+
* [https://www.kernel.org/doc/Documentation/vm/zswap.txt zswap documentation].
 
* [https://lkml.org/lkml/2013/7/17/147 zswap: How to determine whether it is compressing swap pages?].
 
* [https://lkml.org/lkml/2013/7/17/147 zswap: How to determine whether it is compressing swap pages?].
 +
* [https://www.ibm.com/developerworks/community/blogs/fe313521-2e95-46f2-817d-44a4f27eba32/entry/new_linux_zswap_compression_functionality7?lang=en IBM Developer Works Article (with benchmarks)].
 +
* [http://askubuntu.com/questions/471912/zram-vs-zswap-vs-zcache-ultimate-guide-when-to-use-which-one Ask Ubuntu: zram vs. zswap vs. zcache].
 
* [https://bbs.archlinux.org/viewtopic.php?id=169585 Archlinux forum thread].
 
* [https://bbs.archlinux.org/viewtopic.php?id=169585 Archlinux forum thread].
 +
* [https://lwn.net/Articles/537422/ LWN.net technical article by the main developer of zswap].

Latest revision as of 16:01, 4 January 2018

Zswap is a kernel feature that provides a compressed RAM cache for swap 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 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 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 is a swap device in RAM that does not require a backing swap device.

Enabling zswap

To enable zswap at runtime, execute the following command:

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

To enable zswap permanently:

  • Alternatively, you can use systemd-swap which is a script to manage swap spaces, in this case the line zswap_enabled=1 must be present in /etc/systemd/swap.conf and you must start/enable systemd-swap.service.

Customizing zswap

Current parameters

Zswap has 4 customizable parameters. The live settings can be displayed using:

$ grep -R . /sys/module/zswap/parameters
/sys/module/zswap/parameters/enabled:Y
/sys/module/zswap/parameters/max_pool_percent:25
/sys/module/zswap/parameters/zpool:z3fold
/sys/module/zswap/parameters/compressor:lz4

The boot time load message showing the initial configuration can be retrieved with:

$ dmesg | grep zswap:
[    0.317569] zswap: loaded using pool lz4/z3fold

Set parameters

Using sysfs

Each setting can be changed at runtime via the sysfs interface. For example using the following command to amend the first parameter of the list, compressor:

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

Using kernel boot parameters

To set the parameter permanently, the corresponding option, for example zswap.compressor=lz4, must be added to the kernel boot parameter. Therefore to set permanently all the above settings, the following kernel parameters must be added: zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=20 zswap.zpool=z3fold.

When changing the compression algorithm via boot parameter the corresponding kernel modules must be available, at least when using lz4 (refer to #Compression algorithm).

Using systemd-swap

For the ones using the systemd-swap script, it modifies the sysfs parameters at a later stage of the boot process based on its configuration stored in /etc/systemd/swap.conf.

Maximum pool size

The memory pool is not preallocated, it is allowed to grow up to a certain limit in percentage of the total memory available, by default up to 20% of the total RAM. Once this threshold is reached, pages are evicted from the pool into the swap device. The maximum compressed pool size is controlled with the parameter max_pool_percent.

Compressed memory pool allocator

The zpool parameter controls the management of the compressed memory pool, it is by default set to zbud. With the zbud data allocator, 2 compressed objects are stored into 1 page which limits the compression ratio to 2 or less. The superior z3fold allocator allows up to 3 compressed objects by page. The compression ratio with z3fold typically averages 2.7 while it is 1.7 for zbud.

A zpool of type zbud is created by default, use the kernel parameter zswap.zpool=z3fold to select the z3fold method instead. The data allocator can also be changed at a later stage via the sysfs interface.

Compression algorithm

For page compression, zswap uses compressor modules provided by the kernel's cryptographic API. It uses by default the lzo compression algorithm but this can be changed with zswap.compressor. Lz4 can be used instead of lzo for faster compression and decompression for a slightly lower compression ratio. Other possible choices are lz4hc or deflate.

There is no issue setting the compression to lz4 at runtime using sysfs or via systemd-swap but zswap starts in this case with lzo and switches at a later stage to lz4. To use zswap with lz4 straight away, this must be defined in the kernel boot parameter and the lz4 module must be loaded early by the kernel. This can be achieved by following these steps:

  1. Add lz4 lz4_compress to the mkinitcpio#MODULES array.
  2. Replace the ramdisk environment with a newly generated one:
    # mkinitcpio -g /boot/initramfs.img
  3. Add zswap.compressor=lz4 to your kernel parameters.

On next reboot, see #Current parameters to check if zswap now uses lz4 as compressor.

See also