• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..17-Mar-2025-

common/H17-Mar-2025-2,7072,058

figures/H17-Mar-2025-

interface/innerkits/H17-Mar-2025-2,0631,475

profile/H17-Mar-2025-157135

sa_profile/H17-Mar-2025-3330

services/memmgrservice/H17-Mar-2025-10,2667,929

test/H17-Mar-2025-4,3813,283

CODEOWNERSH A D17-Mar-2025778 1614

LICENSEH A D17-Mar-20259.9 KiB179150

OAT.xmlH A D17-Mar-20255.5 KiB8633

README.mdH A D17-Mar-20256.6 KiB11073

README_zh.mdH A D17-Mar-202510.2 KiB227172

bundle.jsonH A D17-Mar-20252.2 KiB7473

memmgr.gniH A D17-Mar-20251.2 KiB3727

README.md

1# The Part of Memory Manager
2
3-   [ Introduction](#section_introduction)
4-   [ Directory Structure](#section_catalogue)
5-   [Framework](#section_framework)
6    -   [The List of Process Reclaim Priority ](#section_prio)
7    -   [reclaim strategy/kill strategy](#section_reclaim)
8-   [ Usage Guidelines](#section_usage)
9-   [ Repositories Involved](#section_projects)
10
11##  Introduction<a name="section_introduction"></a>
12
13The part of *Memory Manager* belongs to the subsystem named *Resource Schedule Subsystem*.
14
15In order to ensure the memory supply, it will reclaim the memory of some processes or even kill these processes in a certain order named *Reclaim Priority*. The calculation and update of *Reclaim Priority* is triggered by the state changes of the applications in the system.
16
17##  Directory Structure<a name="section_catalogue"></a>
18
19```
20/foundation/resourceschedule/memmgr
21├── common           						# common header files
22├── sa_profile       						# System ability xml
23├── services
24|   └── memmgrservice
25|       ├── include
26|       |   ├── event						# even center
27|       |   ├── reclaim_priority_manager	# calculate the Reclaim Priority
28|       |   ├── reclaim_strategy			# reclaim the memory of process by its
29|       |   |                                 Reclaim Priority
30|       |   └── kill_strategy				# kill the process by its Reclaim Priority
31|       └── src
32|           ├── event
33|           ├── reclaim_priority_manager
34|           ├── reclaim_strategy
35|           └── kill_strategry
36└── test 									# usercase for test
37```
38## Framework<a name="section_framework"></a>
39
40*Memory Manager* updates the *Reclaim Priority* and triggers the reclaim and kill behaviors based on some events. The framework of *Memory Manager* is shown in the following picture, which includes the following modules.
41
421. *Event Center*: monitor all external events concerned by this part. The monitors in *Event Center* are registered by calling the interfaces provided by other subsystems, and events about applications and users will be received and notified to *Reclaim Priority Manager*, *Reclaim Strategy* and *Kill Strategy*.
432. *Reclaim Priority Manager*: based on the notifications from the *Event Center*, calculate the sorted list of *Reclaim Priority* ,and provide an interface to *Reclaim Strategy* and *Kill Strategy* for querying this list.
44
453. *Reclaim Strategy*: according to the list of *Reclaim Priority*, adjust the reclaim parameters (the memory waterline, the reclaim ratio of file page / anonymous page, the compaction / swap-out ratio, etc.) and coordinate different reclaim mechanisms to work together to ensure the performance of memory supply under low and medium memory pressure.
46
474. *Kill Strategy*: as the logical end of reclaim, it ensures the memory supply under heavy memory pressure according to the list of *Reclaim Priority*.
48
495. *Kernel Interface*: as an interface to interact with the kernel, issue control commands such as reclaim parameters and kill to the kernel.
50
516. *Memory Configuration Manager*: read and maintain the configurations required by the *Memory Manager*.
52
537. *Disk Life Manager* (under planning): limit the amount of data written to the disk to ensure the device lifetime of it.
54
55![](figures/zh-cn_image_fwk.png)
56
57### The List of Process Reclaim Priority<a name="section_prio"></a>
58
59The list of process *Reclaim Priority* provides the sorted list for reclaim and kill between processes. The value and description of the *Reclaim Priority* defined by this part are shown in the following table:
60
61| Reclaim Priority                                                            | Description |
62|------------------------------------------------------------------------------------------|-------------|
63| -1000 | Processes with this priority will never be killed by the *Kill Strategy*. |
64| 0 | foreground process |
65| 100 | process that is running any transient task |
66| 200 | perceptible process |
67| 600 | frozen process |
68| 800    | suspended process                |
69
70### Reclaim Strategy/Kill Strategy<a name="section_reclaim"></a>
71
72As the logical end of reclaim, kill is essentially for the same purpose as reclaim, so they can use the same list of *Reclaim Priority*. At the same time, they should be called reasonably to ensure the function and performance of memory supply under different memory pressures.
73
74- *Reclaim Strategy*: is the most important in vertical memory management, and controls a variety of different memory reclaim mechanisms, which need to be coordinated to work together. For example, the reclaim of the *Purgeable / Speculative Memory* (under planning) is prior to other types of memory, and the trigger time of reclaim should precede the low memory killing.
75
76  Hotspot files can be cached in memory to improve performance under low memory pressure. (under planning)
77
78  Reclaim processes (kswapd, zswapd, etc.) will work under the guidance of configuration (waterline, reclaim parameters) when under medium memory pressure. (under planning)
79
80  There are corresponding processing for single point special key events. In order to meet the requirements of large memory application, *pre-reclaim* will be started with configuration (under planning). The basic actions of reclaim processes should be stopped before killing to avoid invalid reclaim.
81
82- *Kill Strategy*: works under the heavy memory pressure, and is triggered by psi (pressure stall information) memory pressure event. Some low priority processes are selected from the list of *Reclaim Priority* and killed according to the memory waterline.
83
84  The default corresponding relationship between the memory waterline for killing and the process reclaim priority is shown in the following table, and it can be modified by changing the XML file
85
86  according to the RAM configuration, screen size and so on. (under planning)
87
88  | Memory Waterline for killing | process reclaim priority |
89  | ---------------------------- | ------------------------ |
90  | 500 MB                       | 800                      |
91  | 400 MB                       | 600                      |
92  | 300 MB                       | 200                      |
93  | 200 MB                       | 100                      |
94  | 100 MB                       | 0                        |
95
96###  Usage Guidelines<a name="section_usage"></a>
97
98System developers can add or remove this part by configuring the product definition JSON file under **/productdefine/common/products** to enable or disable this part:
99
100` "resourceschedule:plugins/memmgr":{} `
101
102##  Repositories Involved<a name="section_projects"></a>
103
104Resource Schedule subsystem
105
106resource_schedule_service
107
108
109
110

README_zh.md

1# 内存管理部件
2
3- [内存管理部件](#内存管理部件)
4	- [简介](#简介)
5	- [目录](#目录)
6	- [框架](#框架)
7		- [进程回收优先级列表](#进程回收优先级列表)
8			- [补充](#补充)
9		- [回收策略/查杀策略](#回收策略查杀策略)
10	- [使用说明](#使用说明)
11	- [参数配置说明](#参数配置说明)
12		- [availbufferSize](#availbuffersize)
13		- [ZswapdParam](#zswapdparam)
14		- [killConfig](#killconfig)
15		- [nandlife](#nandlife)
16	- [相关仓](#相关仓)
17
18## 简介<a name="section_introduction"></a>
19
20内存管理部件位于全局资源调度管控子系统中,基于应用的生命周期状态,更新进程回收优先级列表,通过内存回收、查杀等手段管理系统内存,保障内存供给。
21
22## 目录<a name="section_catalogue"></a>
23
24```
25/foundation/resourceschedule/memmgr
26├── common           						# 部件通用工具类
27│   ├── include
28│   │   ├── kernel_interface.h				# 封装了与Kernel交互的接口
29│   │   ├── memmgr_log.h					# 封装了hilog,用于日志打印
30│   │   └── single_instance.h				# 封装了单例模式的类模板
31│   └── src
32│       └── kernel_interface.cpp
3334|
35├── sa_profile       						# 组件服务配置
36|
37├── services         						# 组件服务实现
38|   └── memmgrservice    					# 对内接口目录
39|       ├── include
40|       |   ├── event						# 事件注册中心目录
41|       |   ├── reclaim_priority_manager	# 进程回收优先级管理目录
42|       |   ├── reclaim_strategy			# 回收策略目录
43|       |   └── kill_strategy				# 查杀策略目录
44|       └── src
45|           ├── event
46|           ├── reclaim_priority_manager
47|           ├── reclaim_strategy
48|           └── kill_strategry
49|
50└── test 									# 自测试用例目录
51```
52## 框架<a name="section_framework"></a>
53
54内存管理部件主要基于事件触发回收优先级更新,并基于回收优先级管理内存回收和查杀,其框架如下所示(下图虚线右侧为本部件),主要分为如下几个模块:
55
561、事件管理模块:统筹管理本部件所要对外响应的事件。主要功能为调用其他子系统提供的接口注册事件监听,并将事件通知到回收优先级管理模块、回收策略模块和查杀策略模块。
57
582、回收优先级管理模块:基于事件管理模块的应用和用户的事件通知,给出进程的回收和查杀的先后顺序列表,并向回收策略和查杀策略提供查询此列表的接口。
59
603、回收策略模块:根据回收优先级列表,调整回收内存水线、文件页/匿名页回收比例、压缩/换出比例等回收参数,以及协调不同回收机制协同工作,保障内存中低负载下的供给性能。
61
624、查杀策略模块:作为回收的逻辑末端,根据回收优先级列表,保障内存重负载下的内存供给。
63
645、Kernel接口管控模块:负责将回收参数、查杀等管控命令下发到Kernel。
65
666、内存特性配置:读取回收策略模块、查杀策略模块等需要的配置文件。
67
687、磁盘寿命管控(规划中):控制磁盘写入量,以保障磁盘寿命。
69
70![](figures/zh-cn_image_fwk.png)
71
72### 进程回收优先级列表<a name="section_prio"></a>
73
74进程回收优先级列表,提供了进程间回收和查杀的先后顺序列表。本部件定义的进程回收优先级及其对应的描述如下表所示:
75
76| 优先级                                                                                   | 描述     |
77|------------------------------------------------------------------------------------------|-------------|
78| -1000 | 系统进程,属于该优先级的进程不会被查杀策略查杀,支持白名单配置 |
79| -800 | 常驻进程,属于该优先级的进程可以被查杀,但优先级较低,且被杀后可以再被拉起,支持白名单配置 |
80| 0 | 前台应用。 |
81| 100 | 正在进行后台短时任务的应用; 或者有被进程关联的extension进程 |
82| 200 | 后台可感知应用,如正在执行导航、播放音乐等长时任务的应用 |
83| 260 | 连接着分布式设备的后台应用 |
84| 400 | 普通后台应用及无被任何进程关联的extension进程 |
85
86#### 补充
871. extension进程的优先级根据其关联进程组进行更新,其值在关联进程组最小优先级基础上增加100
88
89
90### 回收策略/查杀策略<a name="section_reclaim"></a>
91
92查杀作为回收的逻辑末端,与回收策略一脉相承,因此两者使用相同的进程回收优先级列表,同时也要协同管控,协调两者触发时机,共同保障不同内存负载下的内存供给功能和性能。
93
94- **回收策略**:回收策略是垂直内存管理的重中之重。回收策略管控了多种不同的内存回收机制,需要协调好不同机制,例如Purgeable/Speculative内存(规划中)的回收要先于其他内存,再例如回收触发时机要先于低内存查杀。
95
96  在内存低负载时,可以将热点文件cache在内存中以提升性能(规划中)。
97
98  内存中负载时,配置kswapd/zswapd等回收进程的内存水线(阈值)以及相应的回收参数,指导kswapd/zswapd工作。
99
100  对于如下单点关键事件,例如大内存需求的相机场景,可以通过“场景处理”模块单点配置,根据业务需求预回收(规划中);再例如查杀场景,要停止kswapd/zswapd的基本动作,避免无效回收。
101
102- **查杀策略**:查杀主要负责内存重负载场景。由内存压力事件( Pressure Stall Information )触发,根据查杀内存水线,从进程回收优先级列表中选择低优先级进程进行清理。
103
104  默认的查杀内存水线与可杀进程回收优先级对应关系如下表所示。未来,系统开发者和产品开发者可以根据内存RAM(Random Access Memory)规格、屏幕尺寸等需求通过修改xml配置修改该对应关系(xml配置暂不支持,规划中)。
105
106  | 查杀内存水线 | 进程回收优先级 |
107  | ------------ | -------------- |
108  | 500 MB       | 400            |
109  | 400 MB       | 300            |
110  | 300 MB       | 200            |
111  | 200 MB       | 100            |
112  | 100 MB       | 0              |
113
114
115
116## 使用说明<a name="section_usage"></a>
117
118系统开发者可以通过配置productdefine/common/products下的产品定义json文件,增加或移除本部件,来启用或停用本部件。
119
120` "resourceschedule:memmgr":{} `
121
122## 参数配置说明<a name="section_usage"></a>
123
124产品可通过memmgr_config.xml来配置本模块相关参数,路径为/etc/memmgr/memmgr_config.xml
125
126xml样例:
127
128```
129<?xml version="1.0" encoding="UTF-8"?>
130<Memmgr>
131	<reclaimConfig>
132		<availbufferSize>
133			<availBuffer>800</availBuffer>
134			<minAvailBuffer>750</minAvailBuffer>
135			<highAvailBuffer>850</highAvailBuffer>
136			<swapReserve>200</swapReserve>
137		</availbufferiSize>
138		<ZswapdParam id="1">
139			<minScore>0</minScore>
140			<maxScore>500</maxScore>
141			<mem2zramRatio>60</mem2zramRatio>
142			<zran2ufsRation>10</zran2ufsRation>
143			<refaultThreshold>50</refaultThreshold>
144		</ZswapdParam>
145		<ZswapdParam id="2">
146			<minScore>501</minScore>
147			<maxScore>1000</maxScore>
148			<mem2zramRatio>70</mem2zramRatio>
149			<zran2ufsRation>20</zran2ufsRation>
150			<refaultThreshold>60</refaultThreshold>
151		</ZswapdParam>
152	</reclaimConfig>
153	<killConfig>
154		<killLevel id="1">
155			<memoryMB>500</memoryMB>
156			<minPriority>400</minPriority>
157		</killLevel>
158		<killLevel id="2">
159			<memoryMB>400</memoryMB>
160			<minPriority>300</minPriority>
161		</killLevel>
162	</killConfig>
163	<nandlife>
164		<dailySwapOutQuotaMB>50</dailySwapOutQuotaMB>
165		<totalSwapOutQuotaMB>199</totalSwapOutQuotaMB>
166	</nandlife>
167</Memmgr>
168```
169
170功能参考: [详见Enhanced SWAP特性介绍](https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/kernel/kernel-standard-mm-eswap.md)
171
172### availbufferSize
173
174| 节点名                                                                                   | 功能说明     | 默认值 |
175|------------------------------------------------------------------------------------------|-------------|-------------|
176| availBuffer | 期望的内存正常状态buffer值 |800|
177| minAvailBuffer | 检测到当前的buffer低于min_avail_buffers时则会唤醒zswapd进行匿名页回收 |750|
178| highAvailBuffer | 期望的回收量为high_avail_buffers与当前系统buffer值的差值 |850|
179| swapReserve | 交换分区空闲容量的阈值 |200|
180
181
182限制:
183* 0 <= minAvailBuffer <= availBuffer <= highAvailBuffer <= memTotal
184* 0 <=swapReserve <=memTotal
185
186### ZswapdParam
187
188| 节点名                                                                                   | 功能说明     | 默认值 |
189|------------------------------------------------------------------------------------------|-------------|-------------|
190| minScore | 期望的内存正常状态buffer值 |0|
191| maxScore | 检测到当前的buffer低于min_avail_buffers时则会唤醒zswapd进行匿名页回收 |1000|
192| mem2zramRatio | 内存压缩到ZRAM的比率 |60|
193| zran2ufsRation | ZRAM换出到ESwap的比率 |10|
194| refaultThreshold | refault的阈值 |50|
195
196限制:
197* 0<=minScore<=1000
198* 0<=maxScore<=1000
199* 0<=ub_mem2zram_ratio<=100
200* 0<=ub_zram2ufs_ratio<=100
201* 0<=refault_threshold<=100
202
203### killConfig
204内存查杀相关配置
205
206| 节点名                                                                                   | 功能说明     | 默认值 |
207|------------------------------------------------------------------------------------------|-------------|-------------|
208| killLevel | 查杀级别 |无|
209| memoryMB | 查杀目标内存阈值 |无|
210| minPriority | 可被查杀的adj最小值 |无|
211
212### nandlife
213寿命管控相关配置
214| 节点名                                                                                   | 功能说明     | 默认值 |
215|------------------------------------------------------------------------------------------|-------------|-------------|
216| dailySwapOutQuotaMB | 每日换出量限制(单位MB),应为正数 |0|
217| totalSwapOutQuotaMB | 总换出量限制(单位MB),应为正数 |0|
218
219## 相关仓<a name="section_projects"></a>
220
221全局资源调度子系统
222
223resource_schedule_service
224
225
226
227