You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LiteOS-Reading/src/doc/LiteOS_Maintenance_Guide_en/multi-module-memory-statist...

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.

Function

API

Description

Allocate and free up dynamic memory to a specified module, and record the amount of the memory

LOS_MemMalloc

Allocates memory of the required size from a specified dynamic memory pool to a specified module.

LOS_MemMfree

Free ups the memory block of a specified module.

LOS_MemMallocAlign

Applies for a memory block of the required size whose address is aligned based on boundary, from a dynamic memory pool and allocates the memory block to the specified module.

LOS_MemMrealloc

Re-allocates a memory block of the required size to the specified module and copies content from the original block. If the new memory block is successfully applied for, the original one will be freed up.

Obtain the memory usage of a specified module

LOS_MemMusedGet

Obtains the memory usage of a specified module. The unit is byte.

How to Use

  1. 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
      
  2. 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.

  3. 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