[deepin exploration] swap, zram, zswap, swap space and memory compression scheme
Tofloor
poster avatar
yanjuner
Super Moderator
2024-04-26 11:00
Author

Let’s talk about the conclusion first:

zram : It is just memory compression technology. Data cannot be saved when shutting down. If you do not want to use the hibernation function, you do not need to set up any swap partition. Set zram directly. Judging from the compression ratio of zstd 1:4, you set a memory ZRAM (16G) with twice the space (8G) actually takes up 50% of the physical memory (that is, 4G) when it is full, so you can think that you have 4+16=20G of memory to use.
Swap : This thing is called swap space. It generally has two forms, swap partition and swap file. If you only use it to open large files, it may be written to the hard disk swap space. It can also be used to hibernate and save work progress when power is turned off.
zswap : zswap is a kernel function that provides a compressed memory cache for swap pages. Pages that would otherwise be swapped to disk are compressed and stored in an in-memory storage pool. The difference compared with zram is that zswap works together with the swap device, while zram is an in-memory swap device and does not require a backup swap device. If you want to hibernate, you need swap. You can do this by adjusting the exchange rate. Don't always write data to swap. Memory compression solves the need for normal boot use. The data only needs to be saved to the hard disk during hibernation. It is simply a problem. Perfect solution.

My ultimate solution is to use a combination of btrfs+swapfile+zswap.

Note: zswap and zram cannot coexist. Be sure to turn off zswap when using zram.

1. Enable btrfs swapfile:

Starting from kernel version 5.0, it is supported to use swap files in btrfs partitions as swap form.
From version btrfs-progs 6.0 onwards, swap files can be quickly created through its own disk utility command.

Systemd version 255 only supports hibernation and swap files under btrfs, which can systemctl --versionbe viewed, so the uos professional version is 241 and cannot be used.

For people who don’t have too many requirements for hard disk use, you can actually use the swap partition, because even the swap file takes up a fixed space, but it has a little flexibility and can adjust the file size later. Therefore, using swap partition can adapt to more scenarios.
Otherwise, follow the previous method to create: https://bbs.deepin.org/post/270299

You can query the btrfs version through btrfs version. Currently deepin23beta3 is v6.3.2

btrfs version
btrfs-progs v6.3.2
​

It should be noted here that the btrfs disk tool has some limitations when creating and using swap files:

filesystem - can only be created in a single hardware partition
filesystem - must have only a single data configuration file
subvolume - snapshots cannot be created if it contains any active swap file
swapfile - must be pre-allocated (i.e. no holes)
swapfile - must be NODATACOW (i.e. also NODATASUM , no compression)

Create a swap file:
This process can be configured at system startup without entering the live environment. @swap is an independent subvolume and cannot be used for snapshots.
Mount the parent volume.
sudo mount /dev/sda2 /mnt
Create a @swap subvolume.
sudo btrfs subvolume create /mnt/@swap
Create a swap file
sudo btrfs filesystem mkswapfile --size 8G /mnt/@swap/swapfile
输出:create swapfile /mnt/@swap/swapfile size 8.00GiB (8589934592)
. Enable the swap file:
sudo swapon /mnt/@swap/swapfile
after activation, The file will appear in /proc/swaps:

$ cat /proc/swaps
Filename                 Type    Size       Used      Priority
/mnt/@swap/swapfile      file    8388604    0         -2
​

The previous setting is to temporarily enable the swap file, which can be permanently enabled through fstab
. Edit: sudo nano /etc/fstab Add the following content and replace the uuid with yours.

#将子卷@swap挂载到/swap,进一步把/swap/swapfile这个文件挂载到swap,这里的处理很多资料都没写具体
UUID=0a79ef53-4ea9-4cdb-9b5e-5b7fb8ff0c64 /swap btrfs subvol=@swap 0 0
/swap/swapfile none swap sw 0 0
​

Enable hibernation to the swap file:
After enabling the swap file, the swap file can be used for hibernation, but this is not simple. Even if you see a sleep button on the deepin restart interface, an error will still occur and you cannot sleep normally. You need to further set up
hibernation here. Previously, the recovery offset had to be written to the file. /sys/power/resume_offset The value was a physical offset on the device.
Btrfs file system used a mapping between logical and physical addresses, but here, physical can still be mapped to one or more specific devices. Physical block address. This is a physical offset for a specific device and is suitable as a recovery offset.
Starting from btrfs-progs version 6.1, there is a command that can directly query the offset. Are you saying that some distributions do not update btrfs-progs?

$ sudo btrfs inspect-internal map-swapfile /mnt/@swap/swapfile
Physical start: 183341248512
Resume offset:      44761047
​

For some scripting and convenience reasons, the option -r will just print the offset, getting a unique output:

$ sudo btrfs inspect-internal map-swapfile -r /mnt/@swap/swapfile
44761047
​

sudo nano /sys/power/resume_offset
Write: 44761047Just save this value

In the case of non-btrfs file systems, you can use the following command to obtain the offset value. Using this command with btrfs is inaccurate:

sudo filefrag -v /swap/swapfile

Just take the value of this position in the picture
image.png

But the above file will become invalid after restarting. How to make it permanent?

Here comes the key point, add the following parameters to two places

sudo nano /etc/initramfs-tools/conf.d/resume
写入:resume=UUID=0a79ef53-4ea9-4cdb-9b5e-5b7fb8ff0c64 resume_offset=44761047
​
刷新配置 
sudo update-initramfs -c -k all
​
sudo nano /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="splash quiet resume=UUID=0a79ef53-4ea9-4cdb-9b5e-5b7fb8ff0c64 resume_offset=44761047"
​
刷新配置
sudo update-grub
​

2. Application of zswap:

关闭 zswap
sudo echo 0 > /sys/module/zswap/parameters/enabled
开启 zswap
sudo echo 1 > /sys/module/zswap/parameters/enabled
​

zswap parameter settings:
zswap has several customizable parameters. Live settings can be displayed using

$ 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/zpool:z3fold
/sys/module/zswap/parameters/accept_threshold_percent:90
​

For other parameters, please refer to https://docs.kernel.org/admin-guide/mm/zswap.html[](https://docs.kernel.org/admin-guide/mm/zswap.html)

Each setting can be changed at runtime through the sysfs interface. As an example, to change the compressor parameter

sudo echo zstd > /sys/module/zswap/parameters/compressor

Persist changes using kernel boot parameters:

zswap.enabled=1 zswap.compressor=zstd zswap.max_pool_percent=20 zswap.zpool=z3fold

A simpler way is to pass kernel parameters when booting through grub:

sudo nano /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="splash quiet 跟着加到这里面 zswap.compressor=zstd zswap.enabled=1"
​

After sudo update-grubthat

At the same time, we also need to put compression tools, etc. into the kernel module and write them

sudo nano /etc/initramfs-tools/modules

#写入
zstd
zatd_compress
z3fold
​

implement sudo update-initramfs -c -k all

3. Enable memory compression

Now that I’ve finished talking about my ultimate plan, let’s talk about how to enable zram in the debian system.

The zram-tools or systemd-zram-generator software packages can be used to automatically set up zram devices. The professional version of uos has zram-tools and deepin has the latter systemd-zram-generator.

uos method:
install zram-tools and start the corresponding service:
sudo apt install zram-tools
By default, the compression algorithm used by zRAM is lzo. If you want to use other algorithms or modify the size of zRAM, we can modify the configuration file /etc/default/zramswap
sudo nano /etc/default/zramswap

#压缩算法选择,注意系统必须安装这些压缩工具
#压缩速度:lz4 > zstd > lzo
#压缩率:zstd > lzo > lz4
ALGO=zstd
#指定用于zram的RAM量,基于可用内存总量的百分比,这优先并覆盖下面的SIZE
SIZEPERCENT=70
#指定应该用于的静态RAM量单位MiB中,如果上面设置了百分比,这条将不生效,也可以注释掉
SIZE=8192
#指定交换设备的优先级,数字越大=优先级越高,这应该高于硬盘swaps的优先级
PRIORITY=100
#建立几个zram设备,默认cpu内核几个就创建几个,以此控制数量
#CORES=1
​

Configure the above parameters to enable the service:
sudo service zramswap reload

You can view the zram currently used for swap with the following command
swapon -s

The method of deepin:
first install
sudo apt install systemd-zram-generator
, modify the configuration file
/etc/systemd/zram-generator.conf
and write:

[zram0]
compression-algorithm = zstd
zram-size = ram / 2
swap-priority = 100
​

Here a zram swap device is created using zstd compression and the size is half of all available memory capacity (ram/2 means the size is 1/2 of the RAM size; it can also be set to a value such as 4G or 512M)

For detailed extension parameters, see: https://github.com/systemd/zram-generator

Enable service:

systemctl daemon-reload
systemctl start /dev/zram0
​

Call zramctl or swapon to confirm that the device has been created and is being used.

$zramct
lNAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle       4.8G   4K   80B   12K       4[SWAP]
​

Note: Be sure to update initramfs before restarting after moving the disk and related operations.

sudo update-initramfs -c -k all

Screenshot_gpartedbin_20240414124544.png

Screenshot_deepin-system-monitor_20240414134910.png

  • This article brings the simplest partitioning scheme EFI+BTRFS with two global partitions;
  • This article truly realizes the usability of memory compression and swap files. There is almost no need to swap files at ordinary times. During hibernation, the work can be truly saved and restored to the original state.
  • It is undoubtedly a good choice for using a virtual machine and a browser with more than 20 tabs open at the same time, and you can get a smooth experience.

The above represents my personal final understanding of this solution. Partners who do not use btrfs can also start it and draw inferences from one example!

end!

Reply Favorite View the author
All Replies
m***[email protected]
deepin
2024-04-27 15:45
#1
It has been deleted!