8.1 KiB
Multi-Module Memory Statistics
Usage Scenarios
The system has clear service modules and users need to collect statistics on the memory usage of each module.
Function Description
Huawei LiteOS provides a set of encapsulation interfaces based on the kernel memory interface, adding the module ID input parameter. When performing memory operations in different modules and calling related encapsulation interfaces, users can collect statistics about memory usage in all modules and memory usage of a specified module by module ID.
How to Use
-
Run the make menuconfig command to enable the multi-module memory statistics function.
-
Currently, only the bestfit memory management algorithm supports this function. You need to enable LOSCFG_KERNEL_MEM_BESTFIT.
Kernel ---> Memory Management ---> Dynamic Memory Management Algorithm ---> bestfit
-
This function depends on LOSCFG_MEM_MUL_MODULE. Before using this function, enable Enable Memory module statistics.
Debug ---> Enable a Debug Version ---> Enable MEM Debug ---> Enable Memory module statistics
-
-
Each service module is configured with a unique module ID. In the module code, the memory operation calls the related interface and transfers the related module ID.
-
The LOS_MemMusedGet interface can obtain the memory usage of a specified module, which can be used for optimization analysis of module memory usage.
Precautions
- The value of module ID is specified by the macro MEM_MODULE_MAX. When the number of system modules exceeds the value specified, the MEM_MODULE_MAX needs to be modified accordingly.
- All memory operations in the module need to call the encapsulation interface. Otherwise, the statistics may be inaccurate.
- Currently, only the bestfit memory management algorithm supports this function. You need to enable LOSCFG_KERNEL_MEM_BESTFIT.
Programming Example
void test(void)
{
void *ptr = NULL;
void *ptrTmp = NULL;
UINT32 size = 0x10;
UINT32 module = 0;
UINT32 memUsed = 0;
ptr = LOS_MemMalloc(OS_SYS_MEM_ADDR, size, module);
if (ptr == NULL) {
PRINTK("module %d malloc failed\n", module);
} else {
PRINTK("module %d malloc successed\n", module);
}
memUsed = LOS_MemMusedGet(module);
PRINTK("module %d mem used size %d\n", module, memUsed);
module = 1;
ptrTmp = LOS_MemMalloc(OS_SYS_MEM_ADDR, size, module);
if (ptrTmp == NULL) {
PRINTK("module %d malloc failed\n", module);
} else {
PRINTK("module %d malloc successed\n", module);
}
memUsed = LOS_MemMusedGet(module);
PRINTK("module %d mem used size %d\n", module, memUsed);
module = 0;
LOS_MemMfree(OS_SYS_MEM_ADDR, ptr, module);
module = 1;
LOS_MemMfree(OS_SYS_MEM_ADDR, ptrTmp, module);
}
Logs:
module 0 malloc successed
module 0 mem used size 32
module 1 malloc successed
module 1 mem used size 32