Enhanced Swap (ESwap) allows a custom partition to serve as a swap partition and uses a resident process zswapd to encrypt and swap the anonymous pages compressed by [zram](https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html) to the ESwap partition. In this way, a block of memory can be completely released to ensure the available memory (Memavailable) waterline. In addition to this reclaiming mechanism, the entire memory framework is enhanced to improve the reclaiming efficiency of anonymous pages and file pages and streamline the reclaiming ratio of these two types of pages to prevent refaults caused by excessive reclamation.
## Configuring zram and ESwap
### Enabling ESwap
1. Enable related configuration items and dependencies.
To enable ESwap, you need to enable the corresponding configuration items and dependencies during kernel compilation. The configuration items related to ESwap are as follows:
```
CONFIG_HYPERHOLD=y
CONFIG_HYPERHOLD_DEBUG=y
CONFIG_HYPERHOLD_ZSWAPD=y
CONFIG_HYPERHOLD_FILE_LRU=y
CONFIG_HYPERHOLD_MEMCG=y
CONFIG_ZRAM_GROUP=y
CONFIG_ZRAM_GROUP_DEBUG=y
CONFIG_ZLIST_DEBUG=y
CONFIG_ZRAM_GROUP_WRITEBACK=y
```
Enable the following dependencies:
```
CONFIG_MEMCG=y
CONFIG_SWAP=y
CONFIG_ZSMALLOC=y
CONFIG_ZRAM=y
```
2. Create an ESwap device.
You can use any block device as the ESwap device. In this example, create the **hpdisk** file and mount it to the loop6 device.
```Bash
// Run the dd command to create the hpdisk file for ESwap. In this example, the file size is 512 MB. Set the file size based on service requirements.
By default, ESwap encrypts the data swapped out. If the ESwap device created in step 2 supports inline encryption, you can disable the ESwap software encryption function.
```Bash
// Check whether hardware-based encryption is supported and enabled. If not, do not perform this operation.
> For security purposes, all swapped content must be encrypted. If the ESwap device created does not support inline encryption or the inline encryption macro is not enabled during compilation, ESwap cannot be enabled after software encryption is disabled.
4. Enable ESwap.
After ESwap is enabled, the preceding configuration cannot be modified.
> Enable ESwap before zram is enabled. If ESwap is not used, you can enable zram only. If a device does not have the storage device for swap-out or have the corresponding storage partition created, you can enable ZRAM to reclaim memory using **zswapd**.
### Enabling zram
1. Initialize zram.
Configure the interaction between zram and ESwap and set the zram size.
```Bash
// Enable the swap-in and swap-out functions from zram to ESwap. Perform this step before setting the zram size.
echo readwrite > /sys/block/zram0/group
// Set the zram size. In this example, the zram size is set to 512 MB.
> The difference of the two commands is as follows:
>
> - **disable**: If there is no data in the ESwap partition, disable ESwap. Otherwise, changes ESwap to read-only mode.
> - **force_disable**: If there is no data in the ESwap partition, disable ESwap. Otherwise, changes ESwap to read-only mode and disable ESwap until all data in the ESWAP partition is read.
2. Disable zram and zram group.
```Bash
// If swapon is executed, run swapoff first.
swapoff /dev/block/zram0
echo 1 > /sys/block/zram0/reset
```
## ESwap APIs
ESwap provides APIs to control swap-in and swap-out policies and record the current status. These APIs are located in the directory to which memcg is mounted, for example, `/dev/memcg/`.
| Category| API| Description|
| -------- | -------- | -------- |
| Control| [avail_buffers](#avail_buffers) | Sets the buffer range.|
> Only **avail_buffers** proactively wakes up zswapd because the buffer waterline is adjusted. Other control APIs do not proactively wake up zswapd, but their configuration takes effect only after zswapd is woken up.
The APIs are described as follows:
### avail_buffers
The **avail_buffers** API sets the buffer range [min_avail_buffers, high_avail_buffers]. When the current buffer is less than the value of **min_avail_buffers**, zswapd will be woken up to reclaim anonymous pages. The expected amount of memory to reclaim is the difference between the value of **high_avail_buffers** and the current system buffer value. In fact, less memory is reclaimed due to reasons such as reclamation failure.
The parameters include the following:
-**avail_buffers** indicates the expected buffer value.
-**free_swap_threshold** indicates the threshold of the free capacity of the swap partition. After zswapd is woken up to reclaim memory, press events, such as medium press and critical press, will be recorded based on the current system status and the settings of these two parameters.
You can proactively adjust the values to trigger zswapd reclamation.
The **zram_wm_ratio** API sets the zram swap-out waterline. When the size of the compressed anonymous page in the zram partition is greater than the total size of zram multiplied by **zram_wm_ratio**, the page is swapped out to the ESwap partition. The swap is performed after zswapd is woken up by the buffer waterline. The system defaults the value **0** as **37**. You can change the value as required.
Example:
`echo 30 > /dev/memcg/memory.zram_wm_ratio`
Default value:
```
zram_wm_ratio: 0
```
Limit:
0<=zram_wm_ratio<=100
The value is an integer.
### zswapd_pressure_show
The **zswapd_pressure_show** API records the zswapd status. **buffer_size** indicates the current buffer size of the system, and **recent_refault** indicates the number of refaults occurred.
### stat
In addition to **memcg.stat**, the **stat** API is added with **Anon**, **File**, **zram**, and **Eswap** to monitor ESwap in real time.
### zswapd_vmstat_show
The **zswapd_vmstat_show** API records events occurred during the zswapd running.
## Triggering zswapd
You can check the current buffer value by running `cat /dev/memcg/memory.zswapd_pressure_show`. For example, if the current buffer value is 1200, you can adjust the buffer range to wake up zswapd.
Set zram and ESwap partition sizes based on service requirements and hardware features. For the rk3568 board whose RAM is 2 GB, the zram and ESwap partitions are set to 512 MB.