3

Authors: Fan Chengyang, Chen Jie

OpenAtom OpenHarmony (hereinafter referred to as "OpenHarmony") is an operating system for all-scenario pan-terminal devices. The memory performance of the terminal device will directly affect the user experience. The memory of terminal devices varies greatly. For terminal devices with relatively small memory, the memory optimization solution is undoubtedly the key to enhancing memory performance and improving user experience. In view of the shortcomings of traditional memory solutions and management mechanisms, OpenHarmony has built a complete set of memory solutions - ESWAP.

1. Traditional memory scheme and management mechanism

In the traditional Linux memory optimization scheme, terminal devices usually use SWAP and ZRAM memory schemes.

1.1 SWAP

SWAP is memory swapping technology or virtual memory technology. As shown in Figure 1, when the physical memory of the system is insufficient, a part of the memory space that is not commonly used in the memory is released to increase the available memory of the system for the currently running programs. These released data are temporarily saved to the SWAP partition, and then restored to the memory from the SWAP partition when needed.

$$ Figure 1 SWAP virtual memory technology $$

It is not difficult to see from Figure 1 that the SWAP memory swapping technology increases the memory used in the device. However, the IO performance bottleneck will be encountered when the SWAP memory is swapped in/out, which may even affect the user's experience in severe cases, and the flash storage device The frequent reads and writes will also reduce its lifespan.

1.2 ZRAM

ZRAM is a memory compression technology. As shown in Figure 2, when the physical memory of the system is insufficient, a part of the physical memory of the system is divided as a ZRAM partition, and then the anonymous pages that are not commonly used are compressed and placed in the ZRAM partition, which is equivalent to sacrificing Some CPU efficiency is added to increase the system's available memory for currently running programs. When needed, extract the data from the ZRAM partition.

$$ Figure 2 ZRAM memory compression technology $$

Although ZRAM increases the memory used in the device to a certain extent, if there is no suitable way to manage the memory, the negative impact will be very obvious, which will cause frequent compression/decompression of memory pages, thus preempting normal business. CPU time, increasing the power consumption of the system. Moreover, if the compression/decompression speed is not fast enough, it will directly affect the user experience.

1.3 Memory Management Mechanism

In addition to insufficient memory solutions, traditional memory allocation and management methods cannot perceive the importance of business characteristics and data. If multiple processes or services of a terminal device share a piece of memory, when the memory load becomes heavier and the memory data is recycled, data movement and memory oscillations will occur frequently. These phenomena can increase the overhead of the kernel managing memory and cause the system CPU to be under high load for a long time, thereby increasing the system power consumption.

2. OpenHarmony memory solution

In view of the shortcomings of the original memory solution, OpenHarmony has built a complete memory solution ESWAP, which opens up the call stack from the upper-layer system to the kernel, so that the kernel can manage each piece of memory data reasonably under the guidance of the upper-layer configuration.

Below we will introduce the ESWAP solution and the analysis of its key technologies.

2.1 Introduction of ESWAP scheme

ESWAP (Enhanced SWAP) is a complete set of memory solutions provided by OpenHarmony for memory optimization problems. Combining memory compression and memory swapping technologies, a set of reasonable and efficient scheduling management strategies has been customized to enable both compression and swapping to work efficiently. and balanced. ESWAP divides memory into different groups for management based on correlation data aggregation technology and upper-layer guidance strategy, distinguishes the activity level of memory under different groups by recycling priority, and preferentially compresses and swaps out less active memory data, so that Improve data exchange performance and reduce life impact.

The overall framework of the ESWAP solution is shown in Figure 3:

$$ Figure 3 ESWAP Solution $$

The ESWAP solution adds a system resource scheduling module to the global resource scheduling subsystem. It senses the current account status and memory status by subscribing to the account subsystem for changes in local accounts, and then sets different recycling priorities for each account according to the account status. level, set the target available memory amount, set the compression and swap out ratio and other parameters, and send these parameters to ZSWAPD. ZSWAPD will determine the order of recycling based on the recycling priority; the amount of recycling will be determined based on the difference between the target available memory and the current available memory; Balance of performance and power consumption under the premise of memory expansion effect.

2.2 Analysis of key technologies

What key technologies are used in ESWAP memory solutions? The following is for you one by one.

2.2.1 Customized ZRAM and swap partitions

ESWAP combines memory compression and memory swap technology to provide the ability to customize new storage partitions as memory swap partitions, and create a resident process ZSWAPD in the kernel to encrypt and swap out ZRAM-compressed anonymous pages to ESWAP In the storage partition, a piece of available memory can be completely vacated, so as to achieve the goal of maintaining the Memavailable waterline, as shown in Figure 4.

$$ Figure 4 ESWAP Technology $$

At the same time, the ESWAP module can also record the hot and cold feature information of each anonymous page, and store these data correspondingly through correlation and cold and hot order, so that the anonymous pages stored continuously in the ESWAP exchange area have time and space locality. Therefore, when the anonymous page is swapped in, the adjacent anonymous pages in the swap area can be read into ZRAM together to ensure the data access speed and improve the IO performance.

2.2.2 Dynamic Memory Reclamation Mechanism

OpenHarmony provides an additional memory reclamation mechanism, ZSWAPD, and creates a "buffer" as an indicator to measure the current system memory capacity. buffer refers to the maximum available memory that the current system can provide. ZSWAPD will compress and swap out anonymous pages for recycling based on the amount of buffers and the various strategies described above. At the same time, ZSWAPD can dynamically control the balance between ZRAM and ESWAP according to the rationality of memory cold and hot separation and memory recycling status, so as to obtain a higher energy efficiency ratio.

2.2.3 Flexible memory reclamation strategy

OpenHarmony enhances the recycling strategy based on Memcg grouping, and uses the recycling priority to guide the order of ZSWAPD recycling. The reclamation strategy sends the established buffer-related configuration to ZSWAPD to instruct it to reclaim an appropriate amount of memory. In addition, since anonymous pages may be stored in the three modules of RAM, ZRAM, and ESWAP, the upper layer can flexibly configure the exchange strategy to control the proportion of storage in these three modules as needed, so as to avoid the negative effects caused by frequent swapping in and out. Influence.

So far, the three key technologies of ESWAP have been introduced. Let's summarize:

● Customized ZRAM and swap partition: The data is stored in correlation, hot and cold order, which ensures the data access speed and improves the IO performance.

● Dynamic memory reclamation mechanism: Dynamically control data reclamation from the three dimensions of reclamation priority, available memory amount, compression and swap ratio, so as to obtain a higher energy efficiency ratio.

● Flexible memory recycling strategy: flexibly control the data storage ratio of RAM, ZRAM, and ESWAP modules to ensure the balance of each module.

2.3 ESWAP related interfaces

The ESWAP solution supports system developers to customize their own recycling policies, and provides an interface under /dev/memcg that is only visible to upper-level recycling policies. System developers can customize their upper-layer policies through these interfaces. The specific interfaces are as follows:

The above is the whole content of this article. The ESWAP solution is still under construction. We look forward to the vast number of developers joining us to witness the infinite possibilities of the intelligent era of all scenarios! Interested partners can obtain the ESWAP source code through the following link for in-depth understanding:

https://gitee.com/openharmony/kernel_linux_5.10/pulls/43


OpenHarmony开发者
160 声望1.1k 粉丝

OpenHarmony是由开放原子开源基金会(OpenAtom Foundation)孵化及运营的开源项目,


引用和评论

0 条评论