Abstract: The memory commissioning method is designed to help locate problems related to dynamic memory. It provides a basic statistical method of dynamic memory pool information, and presents information such as memory pool water line and fragmentation rate to users.

This article is shared from huawei Cloud community “Hongmeng Light kernel – Memory Commissioning – Memory information statistics”, author: Zhushy.

The memory commissioning method helps locate problems related to dynamic memory. It provides basic statistics on dynamic memory pool information and displays information such as memory pool water line and fragmentation rate. The memory leak detection method is provided, which is convenient for users to accurately locate the code line with memory leak, and can also assist in analyzing the memory usage of each module of the system. The memory stamping detection method is provided to help locate the out-of-bounds memory stamping scenario.

This paper analyzes the memory information statistics.

1. Basic concepts

Memory information includes memory pool size, memory usage, remaining memory size, maximum free memory, memory line, memory node statistics, and fragmentation rate.

  • Memory waterline: specifies the maximum memory pool usage. The water line value is updated every time a memory pool is requested or released. You can optimize the memory pool size based on the water line value.

  • Fragment rate: measures the fragmentation degree of a memory pool. A high fragment rate indicates that the pool has a large amount of free memory but a small maximum free memory block. Run the fragment=100- Maximum free memory block size/remaining memory size formula to measure the fragment rate.

  • Other parameters: Scan the node information in the memory pool through the invocation interface of the memory management module.

2. Function configuration

LOSCFG_MEM_WATERLINE: Switch macro, default enabled; If this feature is turned off, the macro is defined as 0 in target_config.h. To obtain the memory waterline, enable the configuration.

3. Development guidance

Key structure introduction:

typedef struct { UINT32 totalUsedSize; // Memory usage of the memory pool UINT32 totalFreeSize; // Remaining memory size of the memory pool UINT32 maxFreeNodeSize; // Maximum free memory block size of a memory pool UINT32 usedNodeNum; // Number of non-free memory blocks in the memory pool UINT32 freeNodeNum; #if (LOSCFG_MEM_WATERLINE == 1) // This macro is enabled by default. If you want to disable it, set this macro in the target_config.h to 0, UINT32 usageWaterLine. // Memory pool waterline value #endif} LOS_MEM_POOL_STATUS;Copy the code
  • Obtaining memory waterline

Call the LOS_MemInfoGet interface. The first parameter is the first address of the memory pool. The second parameter is a handle of type LOS_MEM_POOL_STATUS, where the field usageWaterLine is the waterline value.

  • Memory fragmentation rate calculation

Using the LOS_MemInfoGet interface, you can obtain the remaining memory size and maximum free memory block size of the memory pool, and then obtain the dynamic memory pool fragmentation rate according to the formula (fragment=100- Maximum free memory block size/remaining memory size).

4. Programming examples

This example implements the following functions:

1. Create a monitoring thread to obtain information about the memory pool.

2. Invoke interface LOS_MemInfoGet to obtain basic memory pool information.

3. Calculate the utilization rate and fragmentation rate by using the formula.

Code implementation is as follows:

#include <stdio.h> #include <string.h> #include "los_task.h" #include "los_memory.h" #include "los_config.h" void MemInfoTaskFunc(void) { LOS_MEM_POOL_STATUS poolStatus = {0}; LOS_MemInfoGet(m_aucSysMem0, &poolStatus); / * work out pieces of memory pool current percentage rate * / unsigned char fragments = 100 - poolStatus. MaxFreeNodeSize * 100 / poolStatus totalFreeSize; Unsigned char Usage = LOS_MemTotalUsedGet(m_aucSysMem0) * 100 / LOS_MemPoolSizeGet(m_aucSysMem0); /* Unsigned char Usage = LOS_MemTotalUsedGet(m_aucSysMem0) * 100 / LOS_MemPoolSizeGet(m_aucSysMem0); printf("usage = %d, fragment = %d, maxFreeSize = %d, totalFreeSize = %d, waterLine = %d\n", usage, fragment, poolStatus.maxFreeNodeSize, poolStatus.totalFreeSize, poolStatus.usageWaterLine); } int MemTest(void) { unsigned int ret; unsigned int taskID; TSK_INIT_PARAM_S taskStatus = {0}; taskStatus.pfnTaskEntry = (TSK_ENTRY_FUNC)MemInfoTaskFunc; taskStatus.uwStackSize = 0x1000; taskStatus.pcName = "memInfo"; taskStatus.usTaskPrio = 10; ret = LOS_TaskCreate(&taskID, &taskStatus); if (ret ! = LOS_OK) { printf("task create failed\n"); return -1; } return 0; }Copy the code

The output of the compilation run is as follows:

usage = 22, fragment = 3, maxFreeSize = 49056, totalFreeSize = 50132, waterLine = 1414
Copy the code

Click follow to learn about the fresh technologies of Huawei Cloud