1e41f4b71Sopenharmony_ci# Memory Information Statistics
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## Basic Concepts
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciMemory information includes the memory pool size, memory usage, remaining memory size, maximum free memory, memory waterline, number of memory nodes, and fragmentation rate.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci- The memory waterline indicates the maximum memory used in a memory pool. The waterline value is updated each time the memory is allocated or released. The memory pool size can be optimized based on this value.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci- The fragmentation rate indicates the fragmentation degree of the memory pool. If the fragmentation rate is high, there are a large number of free memory blocks in the memory pool but each block is small. You can use the following formula to calculate the fragmentation rate:<br>Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- You can use **LOS_MemInfoGet()** to scan the node information in the memory pool and collect the related statistics.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## Function Configuration
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci**LOSCFG_MEM_WATERLINE** specifies the setting of the memory information statistics function. This function is disabled by default. If you want to obtain the memory waterline, enable it in **Debug-&gt; Enable MEM Debug-&gt; Enable memory pool waterline or not**.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci## Development Guidelines
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci### How to Develop
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ciKey structure:
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci```c
29e41f4b71Sopenharmony_citypedef struct {
30e41f4b71Sopenharmony_ci    UINT32 totalUsedSize;       // Memory usage of the memory pool.
31e41f4b71Sopenharmony_ci    UINT32 totalFreeSize;       // Remaining size of the memory pool.
32e41f4b71Sopenharmony_ci    UINT32 maxFreeNodeSize;     // Maximum size of the free memory block in the memory pool.
33e41f4b71Sopenharmony_ci    UINT32 usedNodeNum;         // Number of non-free memory blocks in the memory pool.
34e41f4b71Sopenharmony_ci    UINT32 freeNodeNum;         // Number of free memory blocks in the memory pool.
35e41f4b71Sopenharmony_ci#if (LOSCFG_MEM_WATERLINE == 1) // This function is disabled by default and can be enabled using the **menuconfig** tool.
36e41f4b71Sopenharmony_ci    UINT32 usageWaterLine;      // Waterline of the memory pool.
37e41f4b71Sopenharmony_ci#endif
38e41f4b71Sopenharmony_ci} LOS_MEM_POOL_STATUS;
39e41f4b71Sopenharmony_ci```
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciTo obtain the memory waterline, call **LOS_MemInfoGet(VOID *pool, LOS_MEM_POOL_STATUS *poolStatus)**. The first parameter specifies the start address of the memory pool, and the second parameter specifies the handle of the **LOS_MEM_POOL_STATUS** type. The **usageWaterLine** field indicates the waterline.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ciTo calculate the memory fragmentation rate, call **LOS_MemInfoGet** to obtain the remaining memory size and the maximum free memory block size in the memory pool, and then calculate the fragmentation rate of the dynamic memory pool as follows:
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ciFragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci### Development Example
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ciThis example implements the following:
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci1. Create a monitoring task to obtain information about the memory pool.
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci2. Call **LOS_MemInfoGet** to obtain the basic information about the memory pool.
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci3. Calculate the memory usage and fragmentation rate.
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci**Sample Code**
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ciYou can compile and verify the sample code in **kernel/liteos_a/testsuites/kernel/src/osTest.c**. The **MemTest()** function is called in **TestTaskEntry**.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ciThe sample code is as follows:
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci```c
65e41f4b71Sopenharmony_ci#include <stdio.h>
66e41f4b71Sopenharmony_ci#include <string.h>
67e41f4b71Sopenharmony_ci#include "los_task.h"
68e41f4b71Sopenharmony_ci#include "los_memory.h"
69e41f4b71Sopenharmony_ci#include "los_config.h"
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_civoid MemInfoTaskFunc(void)
72e41f4b71Sopenharmony_ci{
73e41f4b71Sopenharmony_ci    LOS_MEM_POOL_STATUS poolStatus = {0};
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci    /* pool is the memory address of the information to be collected. OS_SYS_MEM_ADDR is used as an example. */
76e41f4b71Sopenharmony_ci    void *pool = OS_SYS_MEM_ADDR;
77e41f4b71Sopenharmony_ci    LOS_MemInfoGet(pool, &poolStatus);
78e41f4b71Sopenharmony_ci    /* Calculate the fragmentation rate of the memory pool. */
79e41f4b71Sopenharmony_ci    unsigned char fragment = 100 - poolStatus.maxFreeNodeSize * 100 / poolStatus.totalFreeSize;
80e41f4b71Sopenharmony_ci    /* Calculate the memory usage of the memory pool. */
81e41f4b71Sopenharmony_ci    unsigned char usage = LOS_MemTotalUsedGet(pool) * 100 / LOS_MemPoolSizeGet(pool);
82e41f4b71Sopenharmony_ci    dprintf("usage = %d, fragment = %d, maxFreeSize = %d, totalFreeSize = %d, waterLine = %d\n", usage, fragment,                            poolStatus.maxFreeNodeSize, poolStatus.totalFreeSize, poolStatus.usageWaterLine);
83e41f4b71Sopenharmony_ci}
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ciint MemTest(void)
86e41f4b71Sopenharmony_ci{
87e41f4b71Sopenharmony_ci    unsigned int ret;
88e41f4b71Sopenharmony_ci    unsigned int taskID;
89e41f4b71Sopenharmony_ci    TSK_INIT_PARAM_S taskStatus = {0};
90e41f4b71Sopenharmony_ci    taskStatus.pfnTaskEntry = (TSK_ENTRY_FUNC)MemInfoTaskFunc;
91e41f4b71Sopenharmony_ci    taskStatus.uwStackSize  = 0x1000;
92e41f4b71Sopenharmony_ci    taskStatus.pcName       = "memInfo";
93e41f4b71Sopenharmony_ci    taskStatus.usTaskPrio   = 10;
94e41f4b71Sopenharmony_ci    ret = LOS_TaskCreate(&taskID, &taskStatus);
95e41f4b71Sopenharmony_ci    if (ret != LOS_OK) {
96e41f4b71Sopenharmony_ci        dprintf("task create failed\n");
97e41f4b71Sopenharmony_ci        return LOS_NOK;
98e41f4b71Sopenharmony_ci    }
99e41f4b71Sopenharmony_ci    return LOS_OK;
100e41f4b71Sopenharmony_ci}
101e41f4b71Sopenharmony_ci```
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**Verification**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ciThe result is as follows:
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ciThe data may vary depending on the running environment.
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci```
111e41f4b71Sopenharmony_ciusage = 22, fragment = 3, maxFreeSize = 49056, totalFreeSize = 50132, waterLine = 1414
112e41f4b71Sopenharmony_ci```
113