Liteos_M轻智能系统RAM优化实践
init启动子系统动态内存优化
base/startup/init/begetd.gni 文件中定义的 init_lite_memory_size 产品 param workspace 大小可进行自定义优化,在运行xts测试时将其改为2560字节,xts测试用例可正常运行完成,可优化2.5kb。
# base/startup/init/begetd.gni
# 修改前
init_lite_memory_size = 5120
# 修改后,改为2560字节
init_lite_memory_size = 2560
Bootstrap线程栈内存优化
base/startup/bootstrap_lite/services/source/bootstrap_service.c 中 GetTaskConfig 方法返回了 Bootstrap 线程的参数,其中Bootstrap线程栈大小是2kb。
在运行xts测试时,Bootstrap可优化到1kb,但不是在bootstrap_service.c文件中进行修改,由于 Bootstrap为SHARED_TASK(共享线程),实际上共享线程的栈大小在 foundation/systemabilitymgr/samgr_lite/config.gni 文件中 config_ohos_systemabilitymgr_samgr_lite_shared_task_size 统一定义的,Bootstrap线程可优化1kb。
# foundation/systemabilitymgr/samgr_lite/config.gni
# 修改前
config_ohos_systemabilitymgr_samgr_lite_shared_task_size = 2048
# 修改后
config_ohos_systemabilitymgr_samgr_lite_shared_task_size = 1024
Broadcast线程栈内存优化
foundation/systemabilitymgr/samgr_lite/communication/broadcast/source/broadcast_service.c 中 GetTaskConfig 方法返回了Broadcast线程的参数,其中Broadcast线程栈大小是2kb,优化到1kb测试用例也可正常运行,Broadcast线程可优化1kb。
# foundation/systemabilitymgr/samgr_lite/communication/broadcast/source/broadcast_service.c
# 修改前
TaskConfig config = {LEVEL_HIGH, PRI_ABOVE_NORMAL, 0x800, 40, SPECIFIED_TASK};
# 修改后
TaskConfig config = {LEVEL_HIGH, PRI_ABOVE_NORMAL, 0x400, 40, SPECIFIED_TASK};
hiview线程栈内存优化
base/hiviewdfx/hiview_lite/hiview_service.c 中 GetTaskConfig 方法返回了 hiview 线程的参数,其中 HIVIEW_STACK_SIZE 线程栈大小在 base/hiviewdfx/hiview_lite/BUILD.gn 中定义 hiview_lite_stack_size 为 4096字节,xts测试用例正常运行完成的情况下可优化2kb。
# base/hiviewdfx/hiview_lite/BUILD.gn
# 修改前
hiview_lite_stack_size = 4096
# 修改后
hiview_lite_stack_size = 2048
HCTEST线程栈内存优化
test/xts/tools/lite/hctest/src/hctest_service.c 中 GetTaskConfig 方法返回了 HCTEST 线程的参数,其中线程栈大小为 0x1800,转为10进制为 6144 字节,改为0xC00测试用例正常运行完成,可优化3kb。
# test/xts/tools/lite/hctest/src/hctest_service.c
# 修改前
TaskConfig config = {LEVEL_MIDDLE, PRI_NORMAL, 0x1800, TASK_QUEUE_SIZE, SINGLE_TASK};
# 修改后
TaskConfig config = {LEVEL_MIDDLE, PRI_NORMAL, 0xC00, TASK_QUEUE_SIZE, SINGLE_TASK};
hilog_lite静态内存优化
在base/hiviewdfx/hilog_lite/frameworks/mini/BUILD.gn中:
hilog_lite_log_static_cache_size = 1024
hilog_lite_hiview_hilog_file_buf_size = 512
hilog_lite_log_static_cache_size 与 hilog_lite_hiview_hilog_file_buf_size 分别定义了 hilog_lite 的静态缓存和日志文件大小,这两个值可通过修改产品的 config.json 文件进行配置,在vendor/{xxx}/config.json文件中的"component": "hilog_lite", 的 "features" 中配置:
# vendor/{xxx}/config.json
{
"subsystem": "hiviewdfx",
"components": [
{
"component": "hilog_lite",
"features": [
"hilog_lite_log_static_cache_size = 512",
"hilog_lite_hiview_hilog_file_buf_size = 256"
]
},
...
}
调整到512后可运行完xts测试,优化512字节。
base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite/hiview_log.h文件中的HiLogModuleType枚举,定义了hilog使用的模块,可根据实际情况进行调整,这里修改HILOG_MODULE_OEM_CUSTOMIZE 和 HILOG_MODULE_MAX = 64 调整后xts测试可正常运行:
# base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite/hiview_log.h
# 修改前
HILOG_MODULE_OEM_CUSTOMIZE = 32,
HILOG_MODULE_MAX = 64
# 修改后
HILOG_MODULE_OEM_CUSTOMIZE,
HILOG_MODULE_MAX
可优化225字节。
base/hiviewdfx/hilog_lite/frameworks/mini/hiview_log.c 文件中定义的 static char newFmt[] = "The number of parameters is invalid."; 可直接使用字符串字面量,删除static char newFmt[] = "The number of parameters is invalid."; 修改fmt = newFmt;为fmt = "The number of parameters is invalid.";
#
# 修改前
static char newFmt[] = "The number of parameters is invalid.";
...
if (argsNum < 0 || argsNum > LOG_MULTI_PARA_MAX) {
fmt = newFmt;
argsNum = 0;
}
# 修改后
# static char newFmt[] = "The number of parameters is invalid."; // 删除此行
...
if (argsNum < 0 || argsNum > LOG_MULTI_PARA_MAX) {
fmt = "The number of parameters is invalid.";
argsNum = 0;
}
上述修改可优化37字节
samgr测试用例优化
在samgr测试用例中存在大量使用malloc分配内存,但是在测试完成后未释放的数据,这里举例说明。
在test/xts/acts/distributed_schedule_lite/system_ability_manager_hal/src/taskpool_sharedtask_func_test.c中的testSharedTask0010测试用例中使用malloc申请了两次内存:
...
request.data = malloc(request.len);
...
request2.data = malloc(request2.len);
...
申请后在用例执行完成时并没有释放操作,可在测试用例最后添加一下代码进行释放:
free(request.data);
request.data = NULL;
free(request2.data);
request2.data = NULL;
所有未释放的数据添加释放后可优化424字节。
另外SingleTS01、SingleTS02、SingleTS03、SingleTS04、SpecifiedT01线程栈大小可调整,分别在以下文件中:
test/xts/acts/distributed_schedule_lite/system_ability_manager_hal/src/taskpool_singletask_func_test.c
# 修改前
TaskConfig config = { LEVEL_HIGH, PRI_NORMAL, 1600, 2, SINGLE_TASK };
# 修改后
TaskConfig config = { LEVEL_HIGH, PRI_NORMAL, 1200, 2, SINGLE_TASK };
test/xts/acts/distributed_schedule_lite/system_ability_manager_hal/src/taskpool_specifiedtask_func_test.c
# 修改前
TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, 1600, 20, SPECIFIED_TASK};
# 修改后
TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, 1200, 20, SPECIFIED_TASK};
测试线程不能调太小,太小的话容易崩溃,调整到1200时测试用例可以运行完成,SingleTS01、SingleTS02、SingleTS03、SingleTS04、SpecifiedT01线程栈大小共可优化2000字节。
由于serviceName501 为SHARED_TASK(共享线程),实际上共享线程的栈大小在 foundation/systemabilitymgr/samgr_lite/config.gni 文件中。
可参考上面的[Bootstrap线程栈内存优化](# Bootstrap线程栈内存优化),serviceName501线程可优化1kb。
RAM优化结果
分析编译结果和xts测试过程中的内存使用情况。
优化前
# 编译结果
Memory region Used Size Region Size %age Used
FLASH: 231216 B 448 KB 50.40%
RAM: 115948 B 128 KB 88.46%
执行测试用例过程中内存使用最大峰值为50328字节。
0xc00 OsMemAlloc size: 16 Byte, AllocSize: 24 Byte, UsedMemSize: 50328 Byte.
优化后
# 编译结果
Memory region Used Size Region Size %age Used
FLASH: 231240 B 448 KB 50.41%
RAM: 115172 B 128 KB 87.87%
执行测试用例过程中内存使用最大峰值为36608字节。
0xc00 OsMemAlloc size: 16 Byte, AllocSize: 24 Byte, UsedMemSize: 36608 Byte.
对比结果,优化后编译结果的RAM优化了776字节,xts测试运行时最大使用内存峰值减少了13720字节,总共优化
14496字节,约14.15kb。
更多推荐

所有评论(0)