OpenHarmony is an operating system designed for all-scenario, general terminal devices, and the strength of device memory performance directly affects user experience. Memory varies greatly among terminal devices. For devices with relatively small memory, memory optimization solutions are undoubtedly key to enhancing memory performance and improving user experience. To address the shortcomings of traditional memory solutions and management mechanisms, OpenHarmony has built a comprehensive memory solution—ESWAP.
1. Traditional memory solutions and management mechanisms
In traditional Linux memory optimization solutions, terminal devices typically use SWAP and ZRAM memory solutions.
1.1 SWAP
SWAP stands for Memory Swap technology or virtual memory technology. As shown in Figure 1, when the system’s physical memory is insufficient, it frees up a portion of the unused memory space to increase the system’s available memory for the currently running program. This freed data is temporarily saved to the SWAP partition, and when needed, it is restored from the swap partition to memory.
From Figure 1, it is clear that SWAP memory swapping technology increases the device’s internal memory, but swapping in and out of SWAP memory encounters IO performance bottlenecks, which in severe cases can affect user experience, and frequent read/write of flash memory devices also shortens their lifespan.
1.2 ZRAM
ZRAM is a memory compression technology. As shown in Figure 2, when the system’s physical memory is insufficient, a portion of the system’s physical memory is partitioned as ZRAM, and unused anonymous pages are compressed and placed in the ZRAM partition. This essentially sacrifices some CPU efficiency to increase the system’s available memory for running programs. When needed, extract the data from the ZRAM partition.
Figure 2: ZRAM memory compression technology
Although ZRAM increases the device’s internal memory to some extent, without proper memory management, the negative impact can be very obvious, causing frequent compression/decompression of memory pages, which takes up CPU time during normal business and increases system power consumption. 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 fail to recognize business characteristics and the importance of data. If multiple processes or services on a terminal device share a single memory block, as memory load increases and memory data collection is performed, frequent data migration and memory oscillations occur. These phenomena increase the overhead of kernel memory management and cause the system CPU to remain under high load for extended periods, thereby increasing system power consumption.
2. OpenHarmony memory solution
To address the shortcomings of existing memory solutions, OpenHarmony has built a comprehensive memory solution called ESWAP, which connects the upper-layer system to the kernel call stack, allowing the kernel to manage each memory block rationally under the guidance of upper-layer configuration.
Below, we will introduce the ESWAP solution and analyze its key technologies.
2.1 Introduction to the ESWAP Solution
ESWAP (Enhanced SWAP) is a comprehensive memory solution provided by OpenHarmony for memory optimization issues. It combines memory compression and memory swapping technologies, customizing a reasonable and efficient scheduling management strategy to make compression and swapping work efficient and balanced. ESWAP uses correlated data aggregation technology and upper-level guidance strategies to divide memory into different groups for management. By prioritizing recycling, it distinguishes memory activity levels under different groups, prioritizing compression and swapping out less active memory data to improve data exchange performance and reduce lifespan 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. By subscribing changes to local accounts to the account subsystem, it senses the current account status and memory status. Then, based on the account status, it sets different recycling priorities, target available memory amounts, and compression and swap-out ratios for each account, sending these parameters to ZSWAPD. ZSWAPD determines the order of recycling based on the retrieval priority; The amount to be recovered is determined based on the difference between the target available memory and the current available memory; The amount of compression and swapping out is determined based on the ratio of compression to swapping, thereby achieving a balance of performance and power consumption while achieving memory expansion.
2.2 Key Technology Analysis
What key technologies does ESWAP’s memory solution use? Let me explain them one by one for you.
2.2.1 Customized ZRAM and Switching Partitions
ESWAP combines memory compression and memory swapping technologies, providing the ability to customize new storage partitions as memory swap partitions. It also creates a resident process in the kernel called ZSWAPD, which encrypts anonymous pages compressed from ZRAM and swaps them into ESWAP storage partitions, thereby completely freeing up a usable memory block to maintain the Memavailable waterline, as shown in Figure 4.
Figure 4: ESWAP technology
At the same time, the ESWAP module can record the hot and cold feature information of each anonymous page and store this data in correlation and hot and cold order, ensuring that anonymous pages stored continuously in the ESWAP exchange area have temporal and spatial locality. Therefore, when swapping anonymous pages, adjacent anonymous pages in the swap area can also be read into ZRAM to ensure data access speed and improve IO performance.
2.2.2 Dynamic Memory Recycling Mechanism
OpenHarmony provides an additional memory recycling mechanism called ZSWAPD and creates a “buffer” as an indicator to measure the current system’s memory capacity. A buffer refers to the maximum available memory that the current system can provide. ZSWAPD compresses anonymous pages based on the buffer amount and the various strategies described above to collect them. At the same time, ZSWAPD can dynamically balance ZRAM and ESWAP based on the rationality of memory hot and cold separation and memory recycling status, thereby achieving higher energy efficiency.
2.2.3 Flexible memory recovery strategies
OpenHarmony has enhanced its recycling strategy based on Memcg grouping, using recycling priorities to guide the order of ZSWAPD collections. The recycling strategy assigns the established buffer-related configuration to ZSWAPD to guide it in recovering the appropriate amount of memory. Additionally, since anonymous pages may be stored in RAM, ZRAM, and ESWAP modules, the upper layer can flexibly configure swap strategies as needed to control the storage ratio in these modules and avoid the negative impact of frequent swapping in and out.
That concludes the introduction to ESWAP’s three key technologies. Let’s summarize:
● Customized ZRAM and switching partitions: Data is stored in association and hot and cold sequential order, ensuring fast data access and improving IO performance.
● Dynamic memory recycling mechanism: Dynamically controls data collection from three dimensions: recycling priority, available memory volume, compression, and swap ratios to achieve higher energy efficiency.
● Flexible memory recycling strategy: Flexibly controls the data storage ratio of RAM, ZRAM, and ESWAP modules to ensure balance among modules.
2.3 ESWAP-related interfaces
The ESWAP solution allows system developers to customize their own recycling strategies and provides interfaces under /dev/memcg that are only visible to the upper-layer recycling policies. System developers can use these interfaces to customize their upper-level policies. The specific interfaces are as follows:
That’s all for this article. The ESWAP solution is still under construction, and we look forward to developers joining us to witness the infinite possibilities of the all-scenario intelligent era together!
作者:码量与发量对等
链接:https://juejin.cn/post/7395018314564157491
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Original article: OpenHarmony构建新的内存管理优化方案OpenHarmony是面向全场景泛终端设备的操作系统,终端设备内存性能的 - 掘金 (24th July 2024)








